Custom Search

Wednesday, October 21, 2015

Debugging with GDB C++

1)
$gdb
* Open gdb

2)
(gdb) attach [process-id]
* Attach to a process

* Detach process from GDB before quiting GDB.
(gdb) detach [process-id]
(gdb) quit

3)
(gdb) info files

4)
(gdb) info function

5)
(gdb) b FlowEntry::DoPolicy
* Add breakpoint

6)
(gdb) info breakpoints
(gdb) info break
* A backtrace is a summary of how your program got where it is. It shows one line per frame, for many frames, starting with the currently executing frame (frame zero), followed by its caller (frame one), and on up the stack.
* https://sourceware.org/gdb/onlinedocs/gdb/Backtrace.html

7)
(gdb) del 2
* Delete a breakpoint

8)
(gdb) run/r
* Let it run and stop at breakpoint.

9)
(gdb) bt
OR
(gdb) backtrace
* Print the location

10)
(gdb) next
* If you want to execute the entire function with one keypress, type "next" or "n".

11)
(gdb) step
* To execute one line of code, type "step" or "s". If the line to be executed is a function call, gdb will step into that function and start executing its code one line at a time.

12)
* Print variable
(gdb) p var1
(gdb) print var1

* Run following command to format the print output
(gdb) set print pretty

13)
* Print current line and line number with few lines before and after
(gdb) list *$rip

14)
* Print current line
(gdb) frame

15)
* Print current line and more info with the function name
(gdb) where

16)
* Display a few lines of your source program around the current location
(gdb) l
OR
(gdb) list

17)
* print the data type of a variable
* You can even use ptype to look at structures
(gdb) ptype var1
OR
(gdb) pt var1







8 comments:

  1. a)
    To See Source code
    >Ctrl + x + 1

    b)
    To see both source and assembly
    >Ctrl + x + 2

    ReplyDelete
  2. Stepping And Resuming
    http://www.dirac.org/linux/gdb/05-Stepping_And_Resuming.php

    ReplyDelete
  3. Debugging A Running Process
    http://www.dirac.org/linux/gdb/06-Debugging_A_Running_Process.php

    ReplyDelete
  4. Debugging sample Program
    http://www.dirac.org/linux/gdb/07-Debugging_Ncurses_Programs.php

    ReplyDelete
  5. Debugging sample Program 2
    https://sourceware.org/gdb/onlinedocs/gdb/Sample-Session.html

    ReplyDelete
  6. Debugging sample Program 3
    http://www.cprogramming.com/gdb.html

    ReplyDelete
  7. Debugging sample Program 4
    https://people.gnome.org/~newren/tutorials/developing-with-gnome/html/ch03.html
    http://www.ibm.com/developerworks/library/l-gdb/

    ReplyDelete
  8. Debugging sample Program 5
    http://betterexplained.com/articles/debugging-with-gdb/

    ReplyDelete