Home arrow Guides arrow GDB Quick Tutorial
GDB Quick Tutorial PDF
Sunday, 29 May 2011
GDB is a very useful tool for debuging C and C++ programs from command line.

In oder to make use of GDB you must compile your programs with debug informations.

To do that we use:

  g++ -g app.cc -o app

Sometimes we get more debug info if we compile the app with -Wall.
Then we start debuging it with:

  gdb app

or

  gdb -tui app                   # this will also open a window to show the source code of the app.

Afterthat we will be inside of gdb debuger command line.
We run the app until it crashes:

(gdb)   run

Then we can go up to the stack and see values of variables.

To go up and down we use up and down commands:

(gdb)   up

Then to see the variable and the memory address we use print:

(gdb)  print var_name;


We can also use the core dump to  debug our app by running from command line:

  gdb app app.core


A useful for GUI GDB Debug is DDD application. With DDD you can debug your C/C++ in a X.org GUI:

  cd /usr/ports/devel/ddd
  make install clean
  rehash
  ddd

There is also Electric Fence and Valgrind, usefull to debug memory leaks.

For more info: man gdb

Last Updated ( Friday, 08 June 2012 )
 
< Prev   Next >

Other BSD Systems

OpenBSD

Misc

Solaris

Polls

Best BSD firewall?