user997112
user997112

Reputation: 30605

GDB print backtrace instead of breaking when watching an address

In GDB I'd like to watch a memory address being written to. However, is it possible to set the watch so when the address is written-to, instead of breaking to the command line we print the backtrace and continue execution?

Upvotes: 7

Views: 470

Answers (1)

francesco
francesco

Reputation: 7539

You first set a watch to the given variable:

(gdb) watch i

Now in the list of breakpoints you will see the watch. E.g.:

(gdb) info breakpoints
Num     Type           Disp Enb Address            What
1       hw watchpoint  keep y                      i

At this point you can define a breakpoint command to be executed at any breakpoint or watch point. In your case, you just want to execute "backtrace" and "c". To define this, enter commands 1, and then the list of commands to be executed whenever the watchpoint is hit. E.g.:

(gdb) commands 1
Type commands for breakpoint(s) 1, one per line.
End with a line saying just "end".
>backtrace
>c
>end

The "1" after "commands" refers to the watchpoint number that you can see in the "info breakpoints" output.

Upvotes: 4

Related Questions