Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ specific debugging tricks with gdb [closed]

Tags:

c++

debugging

gdb

What are some of your favorite tricks to debug C++ programs with gdb ?

Interested in all tricks but also

  1. how you call methods (which may be virtual) on objects from within gdb

  2. inspecting STL objects (pretty printing them)

  3. preventing gdb from going into STL code with continue

  4. dealing with inlining, threads, tcmalloc (or custom allocators)

  5. Keeping history of gdb commands across different sessions

like image 708
user231536 Avatar asked Oct 20 '10 14:10

user231536


People also ask

How do I close a process in GDB?

Use ctrl-c to interrupt the application. Then run "signal SIGINT" from the GDB prompt, which will send this signal to your application, causing it to do the same things it would normally have done when you do ctrl-c from the command line.

Can you backtrack in GDB?

If the target environment supports it, gdb can allow you to “rewind” the program by running it backward. A target environment that supports reverse execution should be able to “undo” the changes in machine state that have taken place as the program was executing normally.

How do you stop an infinite loop in GDB?

Diagnose the infinite loop. Start calc from within gdb using the run command. It will go into an infinite loop. Press Ctrl-C (like before) to stop your program.


2 Answers

Try DDD when you debug C++; DDD can dynamically load source code for shared libraries, and display multiple variables while you debug.

like image 57
Michael Shopsin Avatar answered Oct 11 '22 06:10

Michael Shopsin


1.

set print object

This enables the evaluation of the object hierarchy by looking at the vtable. So you can see what type a reference/pointer to a base class is.

2.

Get the debug infos or unstripped libraries for the system libs (most importantly: pthread and libstdc++) since otherwise debugging multithreaded apps isn't working nicely in gdb.

like image 32
mmmmmmmm Avatar answered Oct 11 '22 05:10

mmmmmmmm