Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use "watch" GDB?

Tags:

gdb

watch

I tried to watch the change of the "int a" by the command "watch a". 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; 
 }
like image 850
Léo Léopold Hertz 준영 Avatar asked May 07 '09 16:05

Léo Léopold Hertz 준영


People also ask

How does watch work in GDB?

watch allows us to stop the execution every time the value of a variable changes. display prints variables every time the program's execution stops (i.e. at a watchpoint, breakpoint, etc…) Using both allows us to automatically stop at various points throughout a loop, and print all the relevant variables.

How do I use watch points in GDB?

You can force gdb to use only software watchpoints with the set can-use-hw-watchpoints 0 command. With this variable set to zero, gdb will never try to use hardware watchpoints, even if the underlying system supports them.

How do I run a GDB program?

Starting your program. Use the run command to start your program under GDB. You must first specify the program name (except on VxWorks) with an argument to GDB (see section Getting In and Out of GDB), or by using the file or exec-file command (see section Commands to specify files).

How do watchpoints work?

Watchpoints. A watchpoint is similar to a breakpoint, but it is the address of a data access that is monitored rather than an instruction being executed. You specify a global variable or a memory address to monitor. Watchpoints are sometimes known as data breakpoints, emphasizing that they are data dependent.


1 Answers

It may help to specify your platform, version of GDB, and exact sequence of GDB commands you used.

Here is what I see (GDB appears to work just 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
like image 92
Employed Russian Avatar answered Nov 06 '22 18:11

Employed Russian