Difference between revisions of "GNU Debugger (GDB)"

From UCT EE Wiki
Jump to navigation Jump to search
Line 10: Line 10:
  
 
= Usage =
 
= Usage =
 +
This section defines simple usage of GDB. It is by no means comprehensive, but it should be sufficient for a fair amount of debugging scenarios.
 +
 +
# Compile
 +
# Launch
 +
# Create breakpoints
 +
# Execute
 +
 +
= GDB Commands =
 +
Some useful gdb commands include:
 +
{| class="wikitable"
 +
|-
 +
!colspan="2"|General
 +
|-
 +
|help
 +
|Shows details about commands
 +
|-
 +
!colspan="2"|Controlling Execution flow
 +
|-
 +
|break <line_number>
 +
|Adds a breakpoint at the given line number. Other options include   
 +
break [file_name]:line_number 
 +
break <file_name:function_name>
 +
|-
 +
|run
 +
|Executes the program in gdb
 +
|-
 +
|next
 +
|Debugger will execute the next line as single instruction. This includes whole functions
 +
Can be abbreviated to n.
 +
|-
 +
|step
 +
|Runs a single line of code. Will enter into a called function, unlike next.
 +
Can be abbreviated to s.
 +
|-
 +
|continue
 +
|Continues execution until the next breakpoint.   
 +
Can be abbreviated to c.
 +
|-
 +
|finish
 +
|If you're in a function which returns at some point, you can use the finish command to execute the program until the end of the function.
 +
It's equivalent to setting a breakpoint at the return line of a function, and then letting the program run.
 +
|-
 +
!colspan="2"|Gathering information
 +
|-
 +
|print <variable>
 +
|prints the value of a specified variable.
 +
Can be abbreviated to p.
 +
|-
 +
|info variables
 +
|Will give information on all variables, including some that you haven't defined
 +
|-
 +
|info locals
 +
|Will give information on variables in the current scope
 +
|-
 +
|info args
 +
|Will return information on the arguments passed
 +
|-
 +
|}
 +
 +
== Additional gdb commands when using openOCD ==
 +
See the openOCD page for more details on using it. Here are some additional gdb commands that might be useful when debugging on hardware:
 +
{| class="wikitable"
 +
|monitor <command>
 +
|[asses <command> directly to openOCD. Allows openOCD commands to be passed through gdb
 +
|-
 +
|info registers
 +
|prints the names and values of the CPU registers
 +
|-
 +
|info registers rX
 +
|Prints the value of the specifed register rX, where X is a number of a register (0-15 in ARM).
 +
|-
 +
|set $rX=val
 +
|sets register X (O-15 in ARM) to the value val
 +
|-
 +
|x/format address
 +
|Reads the contents of address and displays it according to format.
 +
See [http://sourceware.org/gdb/current/onlinedocs/gdb/Memory.html |this link] for more.
 +
|-
 +
|}

Revision as of 10:48, 24 July 2020

The GNU Project Debugger (GDB)

Installation

Ubuntu

GDB comes isntalled by default on most Unix-like systems, but if it's not installed, you can run the following:

sudo apt install gdb

Usage

This section defines simple usage of GDB. It is by no means comprehensive, but it should be sufficient for a fair amount of debugging scenarios.

  1. Compile
  2. Launch
  3. Create breakpoints
  4. Execute

GDB Commands

Some useful gdb commands include:

General
help Shows details about commands
Controlling Execution flow
break <line_number> Adds a breakpoint at the given line number. Other options include

break [file_name]:line_number break <file_name:function_name>

run Executes the program in gdb
next Debugger will execute the next line as single instruction. This includes whole functions

Can be abbreviated to n.

step Runs a single line of code. Will enter into a called function, unlike next.

Can be abbreviated to s.

continue Continues execution until the next breakpoint.

Can be abbreviated to c.

finish If you're in a function which returns at some point, you can use the finish command to execute the program until the end of the function.

It's equivalent to setting a breakpoint at the return line of a function, and then letting the program run.

Gathering information
print <variable> prints the value of a specified variable.

Can be abbreviated to p.

info variables Will give information on all variables, including some that you haven't defined
info locals Will give information on variables in the current scope
info args Will return information on the arguments passed

Additional gdb commands when using openOCD

See the openOCD page for more details on using it. Here are some additional gdb commands that might be useful when debugging on hardware:

monitor <command> [asses <command> directly to openOCD. Allows openOCD commands to be passed through gdb
info registers prints the names and values of the CPU registers
info registers rX Prints the value of the specifed register rX, where X is a number of a register (0-15 in ARM).
set $rX=val sets register X (O-15 in ARM) to the value val
x/format address Reads the contents of address and displays it according to format.

See |this link for more.