Gdb: Difference between revisions
No edit summary |
|||
Line 7: | Line 7: | ||
== GDB front-ends == |
== GDB front-ends == |
||
Reference list: |
|||
* [https://sourceware.org/gdb/wiki/GDB%20Front%20Ends GDB Front Ends] |
|||
Ideally front-ends must use the GDB/MI2 interface. |
|||
These are using the old "annotation" mechanisms: |
|||
* [http://www.gnu.org/software/ddd/ DDD] |
* [http://www.gnu.org/software/ddd/ DDD] |
||
* [http://www.kdbg.org/index.php KDBG] |
* [http://www.kdbg.org/index.php KDBG] |
Revision as of 12:32, 23 November 2016
References
- 7.2 Example Debugging Session: Segmentation Fault Example (unkownroad.com)
- Debugging with GDB (delorie.com)
- GDB commands (tutorialspoint.com)
- An Interactive Guide to Faster, Less Frustrating Debugging
- Norm Matloff's Debugging Tutorial (also guide to fast editing and gui debuggers DDD, GVD)
GDB front-ends
Reference list:
Ideally front-ends must use the GDB/MI2 interface.
These are using the old "annotation" mechanisms:
There is also the built-in Text User Interface to GDB (C-x C-a: http://davis.lbl.gov/Manuals/GDB/gdb_21.html
GDB configuratino
GDB reads file ~/.gdbinit at start.
Some references:
Bare minimum configuration
From StackOverflow [1]:
set history save on
set print pretty
set output-radix 16
set height 0
GDB dashboard
GDB dashboard is a modular visual interface for GDB in Python.
To install simply copy .gdbinit as ~/.gdbinit
cp gdb-dashboard/.gdbinit ~/.gdbinit
Alternatively, source it from ~/.gdbinit:
source ~/.gdbinit-dashboard
- Install pygments
Install pygments to get source highlighting
sudo pip install Pygments # Globally pip install Pygments # Locally
If GDB uses python3 (ldd $(which gdb))
), you'll need to install with pip3
:
sudo pip3 install Pygments # Globally pip3 install Pygments # Locally
To get the list of available styles:
python from pygments.styles import get_all_styles as styles python for s in styles(): print(s)
Alternative styles:
Prepare debug session
- Compile with debug symbols, use option -g:
gcc -g program.c # -g : debug symbols
gcc -g -O0 program.c # ... -O0: disable optimization
- Force core dumps (see bash help ulimit):
ulimit -c unlimited
./a.out
# Segmentation fault (core dumped)
GDB invocation
gdb a.out
gdb a.out core.1234 # If coredump available
GDB commands
Reference:
- GDB manual
- https://beej.us/guide/bggdb/
- RETURN
- repeat last command
- Break points and watch points
|
Set a breakpoint at current line, at given line NUMBER or NUMBER lines after/before current line. |
|
Set breakpoint at LOCATION.
|
|
Stop execution when EXPR changes |
|
Stop execution when EXPR is accessed |
|
list breakpoints |
|
Clear breakpoint by LOCATION |
|
Delete all breakpoints |
|
Clear breakpoint by NUMBER (as listed by i b )
|
|
Disable breakpoint by NUMBER (as listed by i b )
|
|
Save current breakpoints as script FILE. Use source to reload.
|
- Execute program
|
Start (or restart) program. Arguments may include wildcards (*) and redirections (<, <<...) |
|
Kill current program. |
|
Continue and interrupted program. |
|
Step (into) current line, or NUMBER lines. |
|
Run to next line (over current line) |
|
Execute till returning from current selected frame. |
|
Run until temporary breakpoint set at LOCATION. |
|
Execute until the program reaches a source line greater than current. |
- View stack
|
Print backtrace of all stack frames, or innermost (outermost) COUNT frames if COUNT>0 (COUNT<0) |
|
Select frame FRAME and print stack frame |
|
Go up a level in the stack (frame calling current frame). |
|
Go down a level in the stack (frame called by current frame). |
- View memory
|
Display EXPR at each prompt (if within scope). |
|
Print information on local variables / function arguments in the current frame |
|
print EXPR. |
|
Undisplay expression by NUMBER. |
- View code
|
List (10 by default) lines of current frame |
|
Disassemble a specified section of memory |
- Miscellaneous
|
Quit gdb. |
|
Get help on COMMAND, or search commands related to WORD. |
|
Source script FILE. |
GDB examples
Simple Segmentation Fault Example
(From [2])
Example program segfault.c:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
char *buf;
buf = malloc(1<<31);
fgets(buf, 1024, stdin);
printf("%s\n", buf);
return 1;
}
|
Compile and launch gdb:
gcc -g segfault.c
gdb a.out
The debug session run
backtrace
frame 3
print buf
kill
break segfault.c:8
run
print buf
next
print buf Fix the bug, then start again, watching now watch buf
# Start again, answer 'y' when asked to start from beginning
run
# Break at watch point, let's _c_ontinue
c
|