Useful GDB commands and tutorials

This is an introductory post I wrote on GDB, based on a lot of materials online. It includes compilation flags for the programs, multithreaded applicaitons and UI interface and many more aspects of GDB.

A lot of thanks for my officemate and mentor Vladimir Kiriansky, I learned a lot of gdb techniques from him.

https://www.clear.rice.edu/comp321/html/laboratories/lab03/

This is the post I started with GDB back at Rice University, from Prof. Alan Cox. It is still one of the best introductions out there.

The basic functionalities of a debugger (from comp321 Rice post below)

  • single-step, or execute just the next line of code,
  • set a breakpoint at some place in your code,
  • execute all code until it encounters a breakpoint, and
  • print a variable’s current value.

All other commands serve one of the four functions. Especially the last one,

execute the programs

Compile flags

Before running the programs, you want to compile with “-g” flag to make sure the symbols are there.

The more tricky part is the optimization flag. If you are just using the debugger to understand the flow of the program, then it is better to use no optimization flag, as this will make sure no code will be optimized out.

However, if you are debugging an optimized version of code, then it is better to debug through the optimized version, with perhaps -O2, -O3 flags.

My experience has also been that disabling “inline” flags (for icc, it would be -Ob0) was incredibly useful for me to understand which functions are being called.

If you have to debug with -O3 or -O2 flags, it is better to set breakpoints on certain lines instead of functions. This is because even when functions are inlined, the source code lines are still there. But there is no guarantee that this would work.

Multiple arguments

gdb –args (command line)   [this allows specifying multiple arguments)

UI interface (TUI)

I find the UI interface for GDB tremendously useful. Otherwise, I often have to use “list” to see what is the code I am looking at.

Ctrl-X, Ctrl-A for me

https://sourceware.org/gdb/current/onlinedocs/gdb/TUI.html

Multithreaded programs

  1. set follow-fork-mode child (GDB will detach at fork() and attach to the new/child process)
  2. set follow-exec-mode same/parent (Default)
  3. show detach­-on­-fork (see what is the current mode set)

Setting Breakpoints

For setting a breakpoint in gdb, you can break at a certain line or a certain function

http://www.delorie.com/gnu/docs/gdb/gdb_29.html

info breakpoints: shows all the breakpoints that are set

delete:  removes all break points (if no arguments given). Apparently you can also give a series of arguments.

clear linenumber: removes a specific break point at a linenumber

http://www.delorie.com/gnu/docs/gdb/gdb_32.html

Print Variable’s current value

p var

You can also print out a variable in binary, hex or other formats. For example, printing a variable in hex is

p /x var

http://www.delorie.com/gnu/docs/gdb/gdb_55.html

Additionally, I find the “list linenumber”  and “disp var ” commands useful when examining the code around a specific line in a file, and printing a specific variable everytime

http://heather.cs.ucdavis.edu/~matloff/UnixAndC/CLanguage/Debug.html

Adds a series of commands that I found pretty useful, “disp” and “list”

A good reference to a lot of useful commands in this cheatsheet

GDB last command

Ctrl + p

GDB move cursor left and right

Ctrl + b (left)

Ctrl +f (right)

ftp://ftp.gnu.org/old-gnu/Manuals/gdb/html_chapter/gdb_24.html

http://www.cheatography.com/fristle/cheat-sheets/closed-source-debugging-with-gdb/

See value of variables

  1. i locals (see the local variables) [info locals]
  2. i args (all the aruments to the current frame)
  3. i variables (all the variables, local and global)

Monitor programs that are running in real time

Sometimes programs are stuck at a certain point in execution, and we want to examine where the program is stuck at, and see the values of local variables. The following steps can be useful.

  1. get the pid of the program (ps, ps aux, top, whatever)
  2. gdb – pid (this will attach gdb to the running program)
  3. bt (back trace the program to see where we are stuck at)
  4. f #framenumber (switch to a certain frame)
  5. i locals (see the local variables) [info locals]

Extra good ones

https://www.recurse.com/blog/7-understanding-c-by-learning-assembly

https://www.recurse.com/blog/5-learning-c-with-gdb

Advertisements
This entry was posted in Tools, Uncategorized and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s