How to use a step rather than keep referring to a watchpoint in GDB
I've used the following steps to keep track of variables in GDB and it works great for me:
1. set some breakpoints
2. run
3. watch a variable
4. continue
However, in this gdb youtube tutorial (starting at 12:10), the author uses this step instead of continuing to invoke watchpoint messages, a position I was unable to reproduce.
The example code was copied from the tutorial:
int main(int argc, char *argv[])
{
int sum, i;
char string[120];
sum=0;
strcpy(string, "Hello!");
for(i=0;i<10;i++) {
sum+=i;
sum=square(sum, i);
}
printf("sum=%d\n", sum);
return 0;
}
int square(int big, int temp)
{
int i;
i=big;
if(big>10) i=i%10;
return(i*temp);
}
Here are the steps I tried to call watchpoint "step" but failed (That is, even if I set the watchpoint but the message "Old value =" and "New value =" was not shown):
1. start
2. step to main
3. watch i
4. step several times but none of watchpoint message was shown
What do I need to do to get it to work with a "step"? Thank.
Here is my working environment:
gdb version: 7.11.1
OS: Ubuntu 16.04 on VMware player 12 in Win10
Edited:
1. This is the code I used "continue" and worked fine with.
syuan@ubuntu:~/Downloads$ gdb test
(gdb) b main
Breakpoint 1 at 0x4005ae: file test.c, line 8.
(gdb) run
Starting program: /home/syuan/Downloads/test
Breakpoint 1, main (argc=1, argv=0x7fffffffde48) at test.c:8
8 {
(gdb) watch i
Hardware watchpoint 2: i
(gdb) c
Continuing.
Hardware watchpoint 2: i
Old value = 0
New value = 1
0x0000000000400615 in main (argc=1, argv=0x7fffffffde48) at test.c:15
15 for(i=0;i<10;i++) {
(gdb) c
Continuing.
Hardware watchpoint 2: i
Old value = 1
New value = 2
0x0000000000400615 in main (argc=1, argv=0x7fffffffde48) at test.c:15
15 for(i=0;i<10;i++) {
(gdb) c
Continuing.
Hardware watchpoint 2: i
Old value = 2
New value = 3
0x0000000000400615 in main (argc=1, argv=0x7fffffffde48) at test.c:15
2. This is the code that I used "step" or "next" but had no success.
syuan@ubuntu:~/Downloads$ gdb test
(gdb) b main
Breakpoint 1 at 0x4005ae: file test.c, line 8.
(gdb) start
Temporary breakpoint 2 at 0x4005ae: file test.c, line 8.
Starting program: /home/syuan/Downloads/test
Breakpoint 1, main (argc=1, argv=0x7fffffffde48) at test.c:8
8 {
(gdb) step
12 sum=0;
(gdb) watch i
Hardware watchpoint 3: i
(gdb) step
13 strcpy(string, "Hello!");
(gdb) step
15 for(i=0;i<10;i++) {
(gdb) step
16 sum+=i;
(gdb) step
17 sum=square(sum, i);
(gdb) step
square (big=0, temp=0) at test.c:26
26 i=big;
(gdb) step
27 if(big>10) i=i%10;
(gdb) step
28 return(i*temp);
(gdb) step
29 }
(gdb) step
main (argc=1, argv=0x7fffffffde48) at test.c:15
15 for(i=0;i<10;i++) {
(gdb) step
16 sum+=i;
(gdb) step
17 sum=square(sum, i);
(gdb) step
square (big=1, temp=1) at test.c:26
26 i=big;
(gdb) step
27 if(big>10) i=i%10;
(gdb) step
28 return(i*temp);
(gdb) step
29 }
(gdb) step
main (argc=1, argv=0x7fffffffde48) at test.c:15
15 for(i=0;i<10;i++) {
(gdb) step
16 sum+=i;
(gdb) step
17 sum=square(sum, i);
(gdb) step
square (big=3, temp=2) at test.c:26
26 i=big;
(gdb) step
27 if(big>10) i=i%10;
(gdb) step
28 return(i*temp);
(gdb) step
29 }
(gdb) step
main (argc=1, argv=0x7fffffffde48) at test.c:15
15 for(i=0;i<10;i++) {
(gdb) next
16 sum+=i;
(gdb) next
17 sum=square(sum, i);
(gdb) next
15 for(i=0;i<10;i++) {
(gdb) next
16 sum+=i;
(gdb) next
17 sum=square(sum, i);
(gdb) next
15 for(i=0;i<10;i++) {
(gdb) next
16 sum+=i;
(gdb) info watchpoint
Num Type Disp Enb Address What
3 hw watchpoint keep y i
Edited again
I tested it on an online gdb tool with gdb 7.7.1, a Linux server with gdb 7.11.1, and a Linux PC as the HOST OS. They all work, leading to speculation that this might be a problem for the VM, but this requires additional verification.
source to share
No one has answered this question yet
Check out similar questions: