Difference between revisions of "GNU Debugger (GDB)"

From UCT EE Wiki
Jump to navigation Jump to search
 
(7 intermediate revisions by the same user not shown)
Line 3: Line 3:
  
 
= Installation =
 
= Installation =
 +
== Windows ==
 +
On Windows, gdb is a part of MinGW. See [[MinGW]] for more information.
 
== Ubuntu ==
 
== Ubuntu ==
GDB comes isntalled by default on most Unix-like systems, but if it's not installed, you can run the following:
+
GDB comes installed by default on most Unix-like systems, but if it's not installed, you can run the following:
 
<syntaxhighlight lang="Bash">
 
<syntaxhighlight lang="Bash">
 
sudo apt install gdb
 
sudo apt install gdb
Line 12: Line 14:
 
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.
 
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  
+
<ol>
# Launch
+
<li>Compile <br>
# Create breakpoints
+
You would compile your file as per usual, but attach the g flag:
# Execute
+
<pre>$ cc -g main.c</pre>
 +
</li>
 +
<li>Launch
 +
To launch gdb, run the following, where "a.out" is the output of the compile command above
 +
</li>
 +
<li> Configure Debug
 +
See below for useful debugging commands such as adding breakpoints, stepping through functions or printing the values of variables
 +
</li>
 +
<li>Run
 +
Run the program with your added breakpoints by typing the "run" command.
 +
</li>
 +
<li>Finish
 +
To close gdb, you can enter "q"
 +
</li>
 +
</ol>
  
 
= GDB Commands =  
 
= GDB Commands =  
Line 29: Line 45:
 
|-
 
|-
 
|break <line_number>
 
|break <line_number>
|Adds a breakpoint at the given line number. Other options include  
+
|Adds a breakpoint at the given line number. Other options include<br>
break [file_name]:line_number  
+
break <file_name:line_number><br>
 
break <file_name:function_name>
 
break <file_name:function_name>
 
|-
 
|-
Line 37: Line 53:
 
|-
 
|-
 
|next
 
|next
|Debugger will execute the next line as single instruction. This includes whole functions
+
|Debugger will execute the next line as single instruction. This includes whole functions<br>
 
Can be abbreviated to n.
 
Can be abbreviated to n.
 
|-
 
|-
Line 66: Line 82:
 
|info args
 
|info args
 
|Will return information on the arguments passed  
 
|Will return information on the arguments passed  
 +
|-
 +
!colspan="2"|Dealing with Pointers
 +
|-
 +
|print *<pointer>
 +
|Prints what the pointer is pointing to
 +
|-
 +
|print &<variable>
 +
|Displays the address of the variable
 
|-
 
|-
 
|}
 
|}
  
== Additional gdb commands when using openOCD ==
+
== gdb for 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:
+
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"  
 
{| class="wikitable"  
 
|monitor <command>
 
|monitor <command>
Line 86: Line 110:
 
|x/format address
 
|x/format address
 
|Reads the contents of address and displays it according to format.  
 
|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.
+
See [http://sourceware.org/gdb/current/onlinedocs/gdb/Memory.html this link] for more.
 
|-
 
|-
 
|}
 
|}

Latest revision as of 12:04, 18 August 2020

The GNU Project Debugger (GDB)

Installation[edit]

Windows[edit]

On Windows, gdb is a part of MinGW. See MinGW for more information.

Ubuntu[edit]

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

sudo apt install gdb

Usage[edit]

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
    You would compile your file as per usual, but attach the g flag:
    $ cc -g main.c
  2. Launch To launch gdb, run the following, where "a.out" is the output of the compile command above
  3. Configure Debug See below for useful debugging commands such as adding breakpoints, stepping through functions or printing the values of variables
  4. Run Run the program with your added breakpoints by typing the "run" command.
  5. Finish To close gdb, you can enter "q"

GDB Commands[edit]

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
Dealing with Pointers
print *<pointer> Prints what the pointer is pointing to
print &<variable> Displays the address of the variable

gdb for openOCD[edit]

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.