Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a watch point for an instance variable?

My class is defined in a header file, and I need to watch for its private non-static member in GDB, like this:

class foo {
    int bar;
};
like image 617
Hailiang Zhang Avatar asked Jun 03 '13 19:06

Hailiang Zhang


People also ask

How to set watchpoint in GDB?

Set a watchpoint that will break when watch expr is read by the program. Set a watchpoint that will break when expr is either read or written into by the program. This command prints a list of watchpoints, breakpoints, and catchpoints; it is the same as info break . GDB sets a hardware watchpoint if possible.

What does GDB's watch command do?

Able to set a watchpoint on a variable in order to break a program when a variable changes. Use display to automatically print how variables change throughout the program's execution. watch allows us to stop the execution every time the value of a variable changes.

What is an instance variable in CPP?

An instance variable is a variable which is declared in a class but outside of constructors, methods, or blocks. Instance variables are created when an object is instantiated, and are accessible to all the constructors, methods, or blocks in the class. Access modifiers can be given to the instance variable.


1 Answers

You can set watchpoint on memory address.

You should stop in your code somewhere after foo constructor execution and print bar variable address. Then you can set watchpoint on address like this:

(gdb) p &bar
$1 = (int *) 0x10793ad0
(gdb) watch *0x10793ad0
like image 79
ks1322 Avatar answered Oct 24 '22 22:10

ks1322