Skip to content

Latest commit

 

History

History
478 lines (411 loc) · 13.7 KB

File metadata and controls

478 lines (411 loc) · 13.7 KB

Debugging with gdb

Table of Contents

Compiler options

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 main

Starting and quitting the debugger

Starting the debugger using the command-line interface.

$ gdb ./main

Type 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) make

Quitting a running program.

(gdb) quit // alternatively q 

Quitting the Debugger. If a program is not running.

(gdb) quit // alternatively q 

Viewing help documentation

View gdb documentation.

(gdb) help // alternatively h

View help documentation on breakpoint commands.

(gdb) help breakpoints // alternatively h breakpoints

Running the program in the debugger

(gdb) run // alternatively r

Show program argument list

(gdb) show args

info command

View info on source file

Use the info command to view information about the program.

(gdb) info source // alternatively use: i source

View all local variables and values

(gdb) info locals

list command

List lines of code using the list command.

List all lines of code of the main function

(gdb) list // alternatively l

Press enter key to scroll through all lines.

List lines of code of the main function

(gdb) list 9 // alternatively l 9

List lines of code around a given line number of main.c

List 5 lines before and 5 lines after line 9.

(gdb) list 9 // alternatively l 9

List next 10 lines of code

(gdb) list + // alternatively l +

List previous 10 lines of code

(gdb) list - // alternatively l -

List previous x number of lines of code

(gdb) list -5 // alternatively l -5

List next x number of lines of code

(gdb) list +5 // alternatively l +5

List in range lines of code

(gdb) list 10,15 // alternatively l 10,15

List lines of code in a given function

(gdb) list function-name // alternatively l function-name

Find the execution line and list code around it

(gdb) where

print command

View variable values using the print command.

View variable values

View a main function variable value in decimal, if stepped into a function.

(gdb) print variable-name // p variable-name

View a int variable named x in decimal.

(gdb) p x
$1 = 42424242

View a main function variable value in binary, if stepped into a function.

(gdb) print/t variable-name // p/t variable-name

View a int variable named x in binary. Any leading zeros are removed.

(gdb) p /t x
$4 = 10100001110101011110110010

View a main function variable value in hexadecimal, if stepped into a function.

(gdb) print/x variable-name // p/x variable-name

View a int variable named x in hexadecimal.

(gdb) p /x x
$3 = 0x28757b2

View a main function variable address.

(gdb) print &variable-name // p &variable-name
(gdb) print &variable-name+1 // view next byte memory location using pointer arithmetic

View given name function variable value

(gdb) print function-name::variable-name // p function-name::variable-name

Arrays

Print 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 = 24

View structs and union

Use 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 = 32

Viewing Memory Contents

To 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:	0x028757b2

Display 4 bytes, note litte endian representation

(gdb) x/4bx &x
0x7fffffffdf0c:	0xb2	0x57	0x87	0x02
(gdb) x/4bt &x
0x7fffffffdf0c:	10110010	01010111	10000111	00000010

This 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 address

Actual 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:	0x02

Actual hexadecimal value is: 0x028757b2

Watch and display

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 breakpoints

To remove a watchpoint, use:

(gdb) disable <watchpoint_number>

Display an expression every time the program stops:

(gdb) display expression

Print a list of the automatically displayed expressions including the display number:

(gdb) info display

Remove an automatic display:

(gdb) delete display <display_number

Setting variable values

Set a variables value using the set command.

Setting main function variable values

(gdb) set var i = i * 2

Set to a binary value using 0b prefix

(gdb) set y = 0b10101010
(gdb) p /t y
$4 = 10101010
(gdb) p /x y
$5 = 0xaa

Set to a hexadecimal value using 0x prefix

(gdb) set y = 0x55
(gdb) p /t y
$7 = 1010101

Set a variable to a previously printed variable.

(gdb) print i
$1 = 5
(gdb) set var j = $1 // j = 5

Setting a given functions variable values

The function must be active at time.

(gdb) set var function-name::j = 5 // function-name j = 5

Setting global variables

(gdb) set var main.c::global-variable-name = 5

Setting array elements

After 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}

Breakpoints

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.

Break on line number

During program suspension further break points can be added.

(gdb) break 4 // alternatively b 4
Breakpoint 1 at 0x10040108d: file main.c, line 8.

Break on function name

(gdb) break main // alternatively b main
Breakpoint 1 at 0x10040108d: file main.c, line 8.

Break on source file function name

Given source file foo.c contains function named foo.

(gdb) break foo.c:foo // alternatively b foo.c:foo

Break on all Class member functions

Set 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::.*

Continue the program after breakpoint

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 c

View all set breakpoints

Use the info command to view any set breakpoints.

(gdb) info break // // alternatively use: i break

Delete a breakpoint

Use 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 point

Single step line by line

After a breakpoint has been hit, single step using the step command.

(gdb) step // alternatively s

Alternatively next or n can be used. Note next will not step into a function

Step to line number

(gdb) step 15 // alternatively s 15

Step into a function

By 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.

Viewing call stack

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 bt command
(gdb) bt

Navigate the call stack frame using up, down, frame and info args commands

View assembly code

(gdb) disassemble

Activate TUI mode

Press ctrl x + ctrl a

External References

Back to top