Internet Resources

webhome - jay stuffs

home
jay stuffs
abbie stuffs
contact jay
[webhome] [C++]

GNU Debugger (GDB) Cheat sheet


  • GDB help can be invoke by typing gdb -help at the terminal screen or type help while debugging.

  • Preparing the executable for GDB by compiling the code using -g option
      $   g++ -g demo.cpp -o demo Compile source code and prepare executable demo for gdb (debugger)
  • Invoke GDB debugger by typing gdb executable file
      $   gdb demo
      (gdb) await GDB command
      Place executable demo in symbolic debugging environment
  • GDB Command (short-hand)

      run (r) executable filename : run executable

      (gdb)   r demo
      Run executable demo

      quit (q) : quit GDB

      (gdb)   q
      Terminate GDB application

      break (b) : set breakpoint at current line
      break (b) line : set breakpoint at specified line
      break (b) function: set breakpoint at specified function

      (gdb)   b add_Student
      set break point at function add_Student( )
      Breakpoint 2 at 0x881131: file student.cc, line 12

      info break : display set breakpoint

      (gdb)   info break
      display current set breakpoints
      Num   Type        Disp   Enb   Address    What
      1      breakpoint keep    y     0x401052    in main() at demo.cc:17
      2      breakpoint keep    y     0x881131    in add_Student() at student.cc:12

      delete (d) : remove all breakpoints
      delete (d) num: remove specified breakpoint from info break output

      (gdb)   d 2
      (gdb)   info break
      remove breakpoint #2
      display current set breakpoints
      Num   Type        Disp   Enb   Address    What
      1      breakpoint keep    y     0x401052    in main() at demo.cc:17

      next (n) or : Execute the code line by line, executing the current line
      step (s)       and display the next line to be execute.

      cont (c) : Continue program execution

      print (p) variable : display content of a single variable

      (gdb)   p student_ID
      5340
      Output current value of variable student_ID

      print (p) &variable : display address of the variable

      (gdb)   p &student_ID
      0X80012
      Output address of variable student_ID

      print (p) *pointer : display content of variable the pointer is pointed to

      (gdb)   p *s_ID
      5340
      Output content of varible that the pointer s_IDm pointed to
      (gdb)   p s_ID
      0X80012
      (gdb)   p &student_ID
      0X80012
      Output value of pointer s_ID

      Note

      Most error occurs when pointer points to incorrect address of the variable, in
      which it should to be pointed to. Software developer/engineer should compare the
      address of the variable and the value of the pointer, if the application outputs
      garbage or unexpected result.

      set variable = value : assigned value to variable during debugging

      (gdb)   set student_ID = 5340
      5340
      Assigned value of 5340 to variable student_ID

      list (l) function name : display source code begin from the specified funtion name
      list (l) line-number : display 10 lines of source code begin from the specified line-number
      list (l) from line-number, to line-number : display source code within the inteval requested

      (gdb)   list 5, 7
      5     int *s_ID = &student_ID;
      6     char *s_FName;
      7     char *s_LName;
      Display source code from line 5 to 7

      where : display stack trace showing the sequence of function calls with function name and arguments

      (gdb)   where
      #1 main ( ) at demo.cc:17
      Output sequence of function calls with function name and arguments

      info locals : display defined variables and arguments

      (gdb)   info locals
      student_ID = 5340
      s_ID = 0X80012 "5340"
      s_FName = 0x1100FF "John"
      s_LName = 0x1200AF "Doe"
      Display variable and argument values currently defined.




  • copyright 2002