Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can gdb print a specific variable every time it breaks? [duplicate]

Tags:

gdb

I need to check a variable to find out if it is set correctly, which might be happening after several loops.

What I am doing now is something like follows:

(gdb) b myfile.cpp:180
(gdb) c
(gdb) p decoder.m_msg
(gdb) c
(gdb) p decoder.m_msg
(gdb) c
...

Can I have this variable decoder.m_msg automatically printed every time the program breaks?

like image 431
Deqing Avatar asked Mar 21 '13 10:03

Deqing


People also ask

What does print do in gdb?

The usual way to examine data in your program is with the print command (abbreviated p ), or its synonym inspect . It evaluates and prints the value of an expression of the language your program is written in (see section Using GDB with Different Languages).

How do you set breakpoints in gdb?

Setting breakpoints A breakpoint is like a stop sign in your code -- whenever gdb gets to a breakpoint it halts execution of your program and allows you to examine it. To set breakpoints, type "break [filename]:[linenumber]". For example, if you wanted to set a breakpoint at line 55 of main.

How do I delete a breakpoint in gdb?

With the clear command you can delete breakpoints according to where they are in your program. With the delete command you can delete individual breakpoints, watchpoints, or catchpoints by specifying their breakpoint numbers. It is not necessary to delete a breakpoint to proceed past it.

How do I create a variable in gdb?

You can create variables in the context of gdb for your convenience, like set $foo = ... and later reference $foo . Obviously such variables are in no way visible to the running code, however. it's not only for inspection. you can change variable values in gdb: stackoverflow.com/questions/3305164/….


1 Answers

Use the display command:

(gdb> display decoder.m_msg

This will cause decoder.m_msg to be printed every time that the prompt is shown (not only after a breakpoint).

like image 188
Nathan Fellman Avatar answered Oct 17 '22 04:10

Nathan Fellman