23

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?

Deqing
  • 14,098
  • 15
  • 84
  • 131

2 Answers2

42

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).

Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
26

Yes, with breakpoint command lists:

$ break myfile.cpp:180
Breakpoint 1 at 0x46ba0e: file myfile.cpp, line 180.

$ commands 1
> print decoder.m_msg
> end
$
Mohamed Moanis
  • 477
  • 7
  • 18
Michael Foukarakis
  • 39,737
  • 6
  • 87
  • 123
  • Nice! I wasn't familiar with this! `display` may not always work because a variable may be out of scope, but a command list solves that. – Nathan Fellman Mar 21 '13 at 12:02