How can I use "watch" GDB?

I tried to watch the "int a" change with the "watch a" command. However, the program does not stop when it changes to 12. Why?

 /* FILE: test.c */
 #include <stdio.h>
 #include <stdlib.h>

 int main(int argc, char** argv){
  printf("Hello world\n");

  int a = 12;
  a = 10;                                                                                                                                                                      
  return 0; 
 }

      

+8


a source to share


3 answers


This can help indicate your platform, GDB version, and the exact sequence of GDB commands that you used.

This is what I see (GDB is working fine):



$ gcc -g test.c

$ gdb a.out
GNU gdb (GDB) 6.8.50.20090430-cvs
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-unknown-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
(gdb) list
1       #include <stdio.h>
2       #include <stdlib.h>
3
4       int main(int argc, char** argv){
5         printf("Hello world\n");
6
7         int a = 12;
8         a = 10;                                                                                                                                                                      
9         return 0; 
10      }
11
(gdb) b 5
Breakpoint 1 at 0x4004a7: file test.c, line 5.
(gdb) r

Breakpoint 1, main (argc=1, argv=0x7fffffffdb28) at test.c:5
5         printf("Hello world\n");
(gdb) watch a
Hardware watchpoint 2: a
(gdb) c
Hello world
Hardware watchpoint 2: a

Old value = 0
New value = 12
main (argc=1, argv=0x7fffffffdb28) at test.c:8
8         a = 10;                                                                                                                                                                      
(gdb) c
Hardware watchpoint 2: a

Old value = 12
New value = 10
main (argc=1, argv=0x7fffffffdb28) at test.c:9
9         return 0; 
(gdb) c

Watchpoint 2 deleted because the program has left the block in
which its expression is valid.
0x00007ffff7ab3033 in exit () from /lib/libc.so.6
(gdb) c

Program exited normally.
(gdb) q

      

+11


a source


If you want to debug a program, you should always build with -O0 -g3 (I assume you are using gcc, unless you are your compiler, you probably support other flags to disable optimizations and enable debug information).

On my system (x86_64 running Gentoo GNU / Linux) I cannot get the line "int a = 12" when I use any optimization greater than or equal to -O, when the compiler then applies deadcode removal . (Taken from here , this is the -fdce flag in the -O section)



Always keep this in mind when you are debugging! Check your code, either by disassembling it with objdump -D or tell the compiler to show you the generated assembly (on gcc with the -S flag)

+3


a source


The compiler probably doesn't even generate code for assignment 12 "a", it would be nice to parse the generated code for confirmation. You will probably need a slightly more complex test to try this.

+1


a source







All Articles