Upload
others
View
12
Download
0
Embed Size (px)
Citation preview
gdb – TheGNU
Debugger
Kurt Schmidt
Intro
Invocation
CommandsGetting Help
Essentials
Running
Listing
Breakpoints
Execution
Data
Call Stack
Trickier
Corefiles
Summary
gdb – The GNU Debugger
Kurt Schmidt
Dept. of Computer Science, Drexel University
November 28, 2018
gdb – TheGNU
Debugger
Kurt Schmidt
Intro
Invocation
CommandsGetting Help
Essentials
Running
Listing
Breakpoints
Execution
Data
Call Stack
Trickier
Corefiles
Summary
Intro
gdb – TheGNU
Debugger
Kurt Schmidt
Intro
Invocation
CommandsGetting Help
Essentials
Running
Listing
Breakpoints
Execution
Data
Call Stack
Trickier
Corefiles
Summary
The GNU Debugger
A debugger is closely tied to the compilergdb is the command-line debugger for all GNUcompilers
Language is irrelevantBack end of the compiler is the same (for a givenplatform)An executable is just a program; it’s not a “C program”,nor a “FORTRAN program”, etc.
gdb – TheGNU
Debugger
Kurt Schmidt
Intro
Invocation
CommandsGetting Help
Essentials
Running
Listing
Breakpoints
Execution
Data
Call Stack
Trickier
Corefiles
Summary
Invocation
gdb – TheGNU
Debugger
Kurt Schmidt
Intro
Invocation
CommandsGetting Help
Essentials
Running
Listing
Breakpoints
Execution
Data
Call Stack
Trickier
Corefiles
Summary
Debugging a Program
First, use the -g option, compile your program withextra (debuggin) information
$ gcc -g source files... -o prog
Then, load the executable into the debugger:
$ gdb progGNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1...(gdb) _
gdb – TheGNU
Debugger
Kurt Schmidt
Intro
Invocation
CommandsGetting Help
Essentials
Running
Listing
Breakpoints
Execution
Data
Call Stack
Trickier
Corefiles
Summary
Commands
gdb – TheGNU
Debugger
Kurt Schmidt
Intro
Invocation
CommandsGetting Help
Essentials
Running
Listing
Breakpoints
Execution
Data
Call Stack
Trickier
Corefiles
Summary
Using GDB
GDB is very powerfulAttach to a running processExamine a corefileDebug multi-threaded programs
Lots of commandsDon’t be intimidatedI don’t know many of themJust knowing some of the basics will get you far
gdb – TheGNU
Debugger
Kurt Schmidt
Intro
Invocation
CommandsGetting Help
Essentials
Running
Listing
Breakpoints
Execution
Data
Call Stack
Trickier
Corefiles
Summary
Getting Help
GDB commands are divided into categoriesType help to see these categories:
(gdb) helpList of classes of commands:
aliases -- Aliases of other commandsbreakpoints -- Making program stop at certain pointsdata -- Examining datafiles -- Specifying and examining filesinternals -- Maintenance commandsobscure -- Obscure featuresrunning -- Running the programstack -- Examining the stackstatus -- Status inquiriessupport -- Support facilitiestracepoints -- Tracing of program execution without ...user-defined -- User-defined commands
gdb – TheGNU
Debugger
Kurt Schmidt
Intro
Invocation
CommandsGetting Help
Essentials
Running
Listing
Breakpoints
Execution
Data
Call Stack
Trickier
Corefiles
Summary
Getting Help – Listing a Class
To see commands in a category (class):
(gdb) help runningRunning the program.
List of commands:
continue -- Continue program being debuggedfinish -- Execute until selected stack frame returnsjump -- Continue program being debugged at specified ...kill -- Kill execution of program being debuggednext -- Step programrun -- Start debugged programstart -- Run the debugged program until the beginning ...step -- Step program until it reaches a different source line
I’ve only listed some of the handier commands
gdb – TheGNU
Debugger
Kurt Schmidt
Intro
Invocation
CommandsGetting Help
Essentials
Running
Listing
Breakpoints
Execution
Data
Call Stack
Trickier
Corefiles
Summary
Getting Help on a Command
Use help cmd for help on that command:
(gdb) help breakSet breakpoint at specified location.break [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]PROBE_MODIFIER shall be present if the command is to be placed in aprobe point. Accepted values are ‘-probe’ (for a generic,automatically guessed probe type), ‘-probe-stap’ (for a SystemTapprobe) or ‘-probe-dtrace’ (for a DTrace probe).LOCATION may be a linespec, address, or explicit location asdescribed below.
With no LOCATION, uses current execution address of the selectedstack frame. This is useful for breaking on return to a stackframe.
THREADNUM is the number from "info threads".CONDITION is a boolean expression....
gdb – TheGNU
Debugger
Kurt Schmidt
Intro
Invocation
CommandsGetting Help
Essentials
Running
Listing
Breakpoints
Execution
Data
Call Stack
Trickier
Corefiles
Summary
Some Essential Commands
Note, many of the commands can be abbreviated.
break b [location ] Set breakpointkill Kill running processrun [arglist ] Run your programprint p [expr ] Print exprstep s Next line, stepping into functionsnext n Next line, stepping over functionscontinue c Continue to next breakquit q Exit GDB
gdb – TheGNU
Debugger
Kurt Schmidt
Intro
Invocation
CommandsGetting Help
Essentials
Running
Listing
Breakpoints
Execution
Data
Call Stack
Trickier
Corefiles
Summary
Running Your Program
set args args Set command-line argumentsset env var val Set environment var to val (for
next run)show args Show command-line argsshow env [var ] Show environment variables [or
var ]run [args ] Run your program [with args ]start [args ] Run your program until beginning of
main procedurekill Kill running process
gdb – TheGNU
Debugger
Kurt Schmidt
Intro
Invocation
CommandsGetting Help
Essentials
Running
Listing
Breakpoints
Execution
Data
Call Stack
Trickier
Corefiles
Summary
Looking at Your Code
list or l
list
list line_no
list beg,end
list file:line_no
list func_name
gdb – TheGNU
Debugger
Kurt Schmidt
Intro
Invocation
CommandsGetting Help
Essentials
Running
Listing
Breakpoints
Execution
Data
Call Stack
Trickier
Corefiles
Summary
Setting Breakpoints
A place (and/or condition) where execution pauses,waits for a user commandCan break conditionally at a function or a line number
break func_namebreak line_nobreak file:line_nobreak ... if cond
info break show breakpointsdelete [n ] delete breakpoints [breakpoint n ]disable [n ] disable breakpoints [breakpoint n ]enable [n ] enable breakpoints [breakpoint n ]
gdb – TheGNU
Debugger
Kurt Schmidt
Intro
Invocation
CommandsGetting Help
Essentials
Running
Listing
Breakpoints
Execution
Data
Call Stack
Trickier
Corefiles
Summary
Execution Control
step s Next line, stepping into functionsnext n Next line, stepping over functionscontinue c Continue to next breakuntil loc Run until loc ; same args as breakfinish Run until frame returnsreturn [expr ] Pop frame w/out executing [using
expr ] as return value
gdb – TheGNU
Debugger
Kurt Schmidt
Intro
Invocation
CommandsGetting Help
Essentials
Running
Listing
Breakpoints
Execution
Data
Call Stack
Trickier
Corefiles
Summary
Examining Data
print p [/f ] expr Prints expr . f is a format characterdisplay [/f ] expr Prints expr each time execution
pausesinfo display Lists displayed expressionsundisplay n Removes n from display list
gdb – TheGNU
Debugger
Kurt Schmidt
Intro
Invocation
CommandsGetting Help
Essentials
Running
Listing
Breakpoints
Execution
Data
Call Stack
Trickier
Corefiles
Summary
The Call Stack
backtrace or bt Print trace of all frames in stackframe [n ] Select current frame [frame # n ]info frame Information on selected frameinfo args Arguments of selected frameinfo locals Local variables of selected frame
gdb – TheGNU
Debugger
Kurt Schmidt
Intro
Invocation
CommandsGetting Help
Essentials
Running
Listing
Breakpoints
Execution
Data
Call Stack
Trickier
Corefiles
Summary
Some Trickier (but Useful) Commands
set var VAR =expr Actually modify variables in theprogram being debugged
Assignment operator from the language(e.g., :=)Keyword var is optionalUseful when symbol name clashes with aGDB command
jump line Resume execution at line
jump *address Resume execution at address
gdb – TheGNU
Debugger
Kurt Schmidt
Intro
Invocation
CommandsGetting Help
Essentials
Running
Listing
Breakpoints
Execution
Data
Call Stack
Trickier
Corefiles
Summary
Corefiles
gdb – TheGNU
Debugger
Kurt Schmidt
Intro
Invocation
CommandsGetting Help
Essentials
Running
Listing
Breakpoints
Execution
Data
Call Stack
Trickier
Corefiles
Summary
Examining Corefiles
A corefile is a snapshot of a process (image) inmemory, when it diedTo allow corefiles on Linux (Bash)$ ulimit -c unlimited
Upon a crash, find the corefile, coreLoad the executable, along with the corefile, into thedebugger$ gdb prog -c core
Examine the program:(gdb) bt
Note, prog needn’t have been compiled with debuginformation
gdb – TheGNU
Debugger
Kurt Schmidt
Intro
Invocation
CommandsGetting Help
Essentials
Running
Listing
Breakpoints
Execution
Data
Call Stack
Trickier
Corefiles
Summary
Summary
gdb – TheGNU
Debugger
Kurt Schmidt
Intro
Invocation
CommandsGetting Help
Essentials
Running
Listing
Breakpoints
Execution
Data
Call Stack
Trickier
Corefiles
Summary
More Power
Only common commands (and uses) are shown hereThere is more functionality available
You can catch events and signalsDebuggers handle multi-threaded programsLook at machine instructions
Get comfortable with basic commandsThis much will prove quite useful
As you need more, explore