06 October 2020

Basic GDB Primer

  • What can I do with GDB?
  • Get a stack trace, similar to Python when it hits an error
  • Step through code and print out values of objects and variables
  • Print out arbitrary expressions as you step through code
    • may require compilation with appropriate debug flags

Useful documentation:https://sourceware.org/gdb/onlinedocs/gdb/index.html#SEC_Contents

Basic Commands

  • Note that there are more commands and that these commands may have more options
command   function
b <func>   Set breakpoint at function <func>
b N   Set bp at line # N
b file.c:N   Set bp at line # N in file file.c
d N   Remove bp number N
c   Continue running program until bp
finish   Run until current func is finished
s   Run next line, stepping into functions
s N   Run next N lines
n   Run next line, don’t step into functions
p   Prints variables and arbitrary expressions
set var=val   Sets variable ‘var’ with value ‘val’
bt   Prints a stack trace, with frame args
f N   Switch to stack frame number N
q   quit, exit gdb

Launch GDB with a program

  • on the command line call gdb with your cpp program as an argument
gdb <program main>
  • Entering in the “run” command will run the cpp program
  • If the program works it will run and finish

Breakpoints

  • While we can run the program with “r” or manually step through each line with “n” or “s”, we usually have a section of code we are interested in
  • Instead, we can set a ‘breakpoint’ at the section of code of interest, and let the program run until it reaches the breakpoint
  • At the break point we can print variables or expressions with “p”
  • Or we can step through normally with “n” or “s”

Program Navigation: Progressing Program Execution

  • When we want to step through code we can use “s” or “n”
  • “n” runs the current line of code and progresses to the next line
  • “n” steps over function calls and resumes debugging after the function call
  • ”s” also runs the current line of code and progresses
  • ”s” however will step into functions and resume debugging inside the function
  • While inside a function, we can also finish running the function and resume debugging after the function call with the command “finish”

Program Navigation: Through the Stack

  • While at any point in debugging, we may be interested in what the current call stack looks like
  • Or in other words, what functions (if any) have been called within functions
int main() {
  int i = 42;
  int result = 0;
  result = foo(i);
  return 0;
}

int foo(int a) {
  return a*a;
}
  • In the short code snippet, if we were debugging in the function foo we may have wanted to see which function had called foo
  • By using the command “backtrace” or “bt” gdb prints out the stack trace, showing the function that had called the current function and it’s calling function and so forth
#1 main() at file.cpp:N
#2 foo( int a = 42 ) at file.cpp:N+5
  • To take a look at the program state of the functions which have called the current function, we can jump to the stack frame’s number as shown on the left with “f N”

Overview of a Debugging Run

  • Compile the program, optionally with debug flags
    • (-ggdb3)
  • Run the program with GDB
    • gdb <program name>
  • In gdb, either
    • set up breakpoints at places of interest and run
      • set breakpoints
        • b <function name>
        • b filename.cpp:N
      • then run
        • r
      • if we hit a break point, print,
        • p <var name>
      • or goto the next line
        • n
      • or step into the next function
        • s
    • run the program until it hits an error
      • run program
        • r
      • get a stack trace
        • backtrace
      • navigate to the stack frame of interest
        • f N
      • print variables from the stack frame
        • p <var name>

Intermediate GDB Primer

Intermediate Commands

command   function
b <location> if <expression>   Set bp which breaks on a condition

GDB Settings