- Compiler options
- Starting and quitting the debugger
- Viewing help documentation
- Running the program in the debugger
- Show program argument list
- info command
- list command
- List all lines of code of the main function
- List lines of code of the main function
- List lines of code around a given line number of main.c
- List next 10 lines of code
- List previous 10 lines of code
- List previous x number of lines of code
- List next x number of lines of code
- List in range lines of code
- List lines of code in a given function
- print command
- Viewing Memory Contents
- Watch and display
- Setting variable values
- Breakpoints
- Viewing call stack
- External References
To use the gdb debugger the -g flag should be set at program compile time. Debugging information is then built into the executable file.
$ gcc -g -o -og main.c -o mainStarting the debugger using the command-line interface.
$ gdb ./mainType c to continue, then you are presented with the gdb prompt (gdb) as shown below.
...
Reading symbols from d...
(gdb)Use -q to start in quiet mode.
$ gdb -q ./main
Reading symbols from main.exe...
(gdb)Re-compile program without leaving GDB using make command. Makefile must exists in same location as the executable being tested.
(gdb) makeQuitting a running program.
(gdb) quit // alternatively q Quitting the Debugger. If a program is not running.
(gdb) quit // alternatively q View gdb documentation.
(gdb) help // alternatively hView help documentation on breakpoint commands.
(gdb) help breakpoints // alternatively h breakpoints(gdb) run // alternatively r(gdb) show argsUse the info command to view information about the program.
(gdb) info source // alternatively use: i source(gdb) info localsList lines of code using the list command.
(gdb) list // alternatively lPress enter key to scroll through all lines.
(gdb) list 9 // alternatively l 9List 5 lines before and 5 lines after line 9.
(gdb) list 9 // alternatively l 9(gdb) list + // alternatively l +(gdb) list - // alternatively l -(gdb) list -5 // alternatively l -5(gdb) list +5 // alternatively l +5(gdb) list 10,15 // alternatively l 10,15(gdb) list function-name // alternatively l function-name(gdb) whereView variable values using the print command.
View a main function variable value in decimal, if stepped into a function.
(gdb) print variable-name // p variable-nameView a int variable named x in decimal.
(gdb) p x
$1 = 42424242View a main function variable value in binary, if stepped into a function.
(gdb) print/t variable-name // p/t variable-nameView a int variable named x in binary. Any leading zeros are removed.
(gdb) p /t x
$4 = 10100001110101011110110010View a main function variable value in hexadecimal, if stepped into a function.
(gdb) print/x variable-name // p/x variable-nameView a int variable named x in hexadecimal.
(gdb) p /x x
$3 = 0x28757b2View a main function variable address.
(gdb) print &variable-name // p &variable-name
(gdb) print &variable-name+1 // view next byte memory location using pointer arithmeticView given name function variable value
(gdb) print function-name::variable-name // p function-name::variable-namePrint arrays and array size. Note: For char arrays, the null terminator is included in the size returned.
(gdb) print arrayName; /* char array */
$4 = "This is a short string."
(gdb) ptype arrayName; /* find array size */
type = char [24]
(gdb) p sizeof(arrayName)/sizeof(*arrayName)
$3 = 24Use the print command followed by the name of the struct/union. Use -> operator to view members.
(gdb) print myCompany
$1 = {{id = 0, age = 0, wage = 0}, numberOfEmployees = 7, CEO = {id = 1, age = 32, wage = 55000}}
(gdb) print myCompany->age
$2 = 0
(gdb) print myCompany->CEO
$3 = {id = 1, age = 32, wage = 55000}
(gdb) print myCompany->CEO->age
$4 = 32To examine the contents of memory directly (actual bit patterns), you can use the x command followed by the memory address. For example:
int x = 42424242; // 0x028757b2(gdb) x &x // defaults to display 1 word (4bytes) in hex
0x7fffffffdf0c: 0x028757b2(gdb) x /1wx &x // display 1 word (4bytes) in hex
0x7fffffffdf0c: 0x028757b2Display 4 bytes, note litte endian representation
(gdb) x/4bx &x
0x7fffffffdf0c: 0xb2 0x57 0x87 0x02
(gdb) x/4bt &x
0x7fffffffdf0c: 10110010 01010111 10000111 00000010This command displays the 4 bytes (1 word) from the specified low order bytes starting address in binary format.
To display the 4bytes used to store a integer variable named x use:-
Note: Low order byte displayed first, need to reverse byte order for actual binary equivalent of int x.
To view in binary the four bytes move up the memory addresses 1 byte at a time.
(gdb) x/tb 0x7fffffffdfac // Starting point
0x7fffffffdfac: 10110010
(gdb) x/tb 0x7fffffffdfad // next address
0x7fffffffdfad: 01010111
(gdb) x/tb 0x7fffffffdfae // next address
0x7fffffffdfae: 10000111
(gdb) x/tb 0x7fffffffdfaf
0x7fffffffdfaf: 00000010 // last addressActual binary value is: 00000010100001110101011110110010
To view in hexadecimal the four bytes move up the memory addresses.
Note: Low order byte displayed first, need to reverse byte order for actual hexadecimal equivalent of int x.
(gdb) x/tx 0x7fffffffdfac // Starting point
0x7fffffffdfac: 0xb2
(gdb) x/tx 0x7fffffffdfad
0x7fffffffdfad: 0x57
(gdb) x/tx 0x7fffffffdfae
0x7fffffffdfae: 0x87
(gdb) x/tx 0x7fffffffdfaf
0x7fffffffdfaf: 0x02Actual hexadecimal value is: 0x028757b2
To set a watchpoint every time the value of a particular variable changes set a watchpoint on that variable using:
(gdb) watch <variable_name>You can view both watchpoints and breakpoints using:
(gdb) info breakpointsTo remove a watchpoint, use:
(gdb) disable <watchpoint_number>Display an expression every time the program stops:
(gdb) display expressionPrint a list of the automatically displayed expressions including the display number:
(gdb) info displayRemove an automatic display:
(gdb) delete display <display_numberSet a variables value using the set command.
(gdb) set var i = i * 2Set to a binary value using 0b prefix
(gdb) set y = 0b10101010
(gdb) p /t y
$4 = 10101010
(gdb) p /x y
$5 = 0xaaSet to a hexadecimal value using 0x prefix
(gdb) set y = 0x55
(gdb) p /t y
$7 = 1010101Set a variable to a previously printed variable.
(gdb) print i
$1 = 5
(gdb) set var j = $1 // j = 5The function must be active at time.
(gdb) set var function-name::j = 5 // function-name j = 5(gdb) set var main.c::global-variable-name = 5After hitting a breakpoint. Set the values of an initialised array. Any undefined array elements are set to 0.
(gdb) set var array = {100, 200}
(gdb) print array
$3 = {100, 200, 0, 0, 0}Before running the program, use the break command to set breakpoint to suspend the program at a given line. Use list command to view the lines of code first if needed.
During program suspension further break points can be added.
(gdb) break 4 // alternatively b 4
Breakpoint 1 at 0x10040108d: file main.c, line 8.(gdb) break main // alternatively b main
Breakpoint 1 at 0x10040108d: file main.c, line 8.Given source file foo.c contains function named foo.
(gdb) break foo.c:foo // alternatively b foo.c:fooSet a breakpoint on all Class member functions using rbreak with a regular expression.
(gdb) rbreak ^Soundex::.*`^` Matches the start of the function name string
`Soundex::` Matches the literal class name followed by C++ scope resolution operator
`.*` Matches any character (.) zero or more times (*). This covers all member functions including constructors, deconstructors and overloaded functions
NOTE If your class is defined within a namespace, you must include the namespace in the regular expression:
(gdb) rbreak ^MyNamespace::MyClass::.*
Run the program using run command, program will suspend at set breakpoint. Use continue command to resume program execution until second breakpoint (if exists)
(gdb) continue // alternatively cUse the info command to view any set breakpoints.
(gdb) info break // // alternatively use: i breakUse the clear command to delete a breakpoint at a line or function name.
(gdb) clear 8 // line 8 breakpoint deleted
(gdb) clear main // clear main function break pointAfter a breakpoint has been hit, single step using the step command.
(gdb) step // alternatively sAlternatively next or n can be used. Note next will not step into a function
(gdb) step 15 // alternatively s 15By default gdb with step into any non system defined functions.
Use next command to not step into a function but skip over the function.
use finish to finish a function stepped into.
Stack trace is a call stack which show the hierarchy of function calls.
View call stack using the backtrace or bt command.
- Set a breakpoint
- Run the program,
- At breakpoint view call stack using
btcommand
(gdb) btNavigate the call stack frame using up, down, frame and info args commands
(gdb) disassemblePress ctrl x + ctrl a