Welcome to http://www.marssoft.de/
 
Tuesday, 11th December 2018 21:23:45 (GMT+1) 

Using the GNU Debugger GDB

A Sample Session with GDB

The following is a sample session using gdb:

gdb /path/to/program
GNU gdb 6.3.50-20050815 (Apple version gdb-962) (Sat Jul 26 08:14:40 UTC 2008)
Copyright 2004 Free Software Foundation, Inc.

(gdb) break malloc_error_break
Breakpoint 1 at 0x1374bc6a7e31a11

(gdb) break SomeClass::~SomeClass()
Breakpoint 2 at 0x1001a22e6: file ../../../../src/code/SomeClass.h, line 13.
Breakpoint 3 at 0x1001a2248: file ../../../../src/code/SomeClass.h, line 13.
warning: Multiple breakpoints were set.
Use the "delete" command to delete unwanted breakpoints.

(gdb) delete 3

(gdb) break myLicenseManager.cxx:330
Breakpoint 3 at 0xa2a9d: file ../../src/code/myLicenseManager.cxx, line 330.

(gdb) run
Breakpoint 1, myGraphInventor::UpdateColor (this=0x6692e00, aColor=@0x2e6c3cb0) at ../../src/myGraphInventor.cxx:639
639       std::cerr << "myGraphInventor::UpdateColor()" << std::endl;

(gdb) list
634                                                              myColor*& aColor)
638     {
639       std::cerr << "myGraphInventor::UpdateColor()" << std::endl;
640
641       // switch off color coding
642       switch (mCurrentColor) {

(gdb) run
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000001
0x00007fff8320c89c in std::string::find_last_of ()

(gdb) backtrace
#0  0x00007fff8320c89c in std::string::find_last_of ()
#1  0x000000010059e3d9 in myFileTools::GetPath (aFileName=@0x1) at ../../src/myFileTools.cpp:44
#2  0x0000000103b8e646 in QAction::triggered ()
#3  0x0000000103b90814 in QAction::activate ()
#4  0x0000000103f09e6f in QAbstractButtonPrivate::click ()

(gdb) info threads
  4 process 9011 thread 0x6647  0x00007fff83288642 in kevent ()
* 3 process 9011 thread 0x363f  0x00007fffffe0026d in ?? ()
  2 process 9011 thread 0x3a07  0x00007fff83261d02 in __semwait_signal ()
  1 process 9011 local thread 0x2d03  0x00007fff8320c89c in std::string::find_last_of ()

(gdb) thread 1
[Switching to thread 1 (process 9011 local thread 0x2d03)]
0x00007fff8320c89c in std::string::find_last_of ()

# using the Tab key (denoted [TAB] here) to auto-complete code lines:
(gdb) break myTexture[TAB]
Display all 964 possibilities? (y or n) n

(gdb) break myTextureFragmentProgram[TAB]
myTextureFragmentProgram.cxx
myTextureFragmentProgram.h
myTextureFragmentProgram::Bias(float, float)
myTextureFragmentProgram::Colorize(Color const&)
myTextureFragmentProgram::Disable() const
myTextureFragmentProgram::Enable() const
myTextureFragmentProgram::Interpolate() const
myTextureFragmentProgram::Interpolate(bool)
myTextureFragmentProgram::SetSource() const
myTextureFragmentProgram::SetNumber(int)

# use single quotation marks to enclose spaces:
(gdb) break 'bpTextureFragmentProgram::Enable() const'
Breakpoint 4 at 0x1003f5604: file ../../src/myTextureFragmentProgram.cxx, line 50.



(gdb) quit
The program is running.  Exit anyway? (y or n) y

Collection of Commands

A nice collection of commonly used GDB commands can be found here: http://www.yolinux.com/TUTORIALS/GDB-Commands.html

helpList gdb command topics.
help topic-classesList gdb command within class.
help commandCommand description.
apropos search-wordSearch for commands and command topics containing search-word.
info args i argsList program command line arguments
info breakpointsList breakpoints
info breakList breakpoint numbers.
info break breakpoint-numberList info about specific breakpoint.
info watchpointsList breakpoints
info registersList registers in use
info threadsList threads in use
info setList set-able option
break funtion-nameSuspend program at specified function.
break line-numberSuspend program at specified line number.
break +offset break -offsetSet a breakpoint specified number of lines forward or back from the position at which execution stopped.
break filename:functionDon't specify path, just the file name and function name.
break filename:line-numberDon't specify path, just the file name and line number.
break *addressSuspend processing at an instruction address. Used when you do not have source.
break line-number if conditionWhere condition is an expression. i.e. <tt>x > 5</tt> Suspend when boolean expression is true.
break line thread thread-numberBreak in thread at specified line number. Use <tt>info threads</tt> to display thread numbers.
tbreakTemporary break. Break once only. Break is then removed. See “break” above for options.
watch conditionSuspend processing when condition is met. i.e. <tt>x > 5</tt>
clearDelete breakpoints as identified by command option.
clear functionDelete breakpoints as identified by command option.
clear line-numberDelete breakpoints as identified by command option.
delete dDelete all breakpoints, watchpoints, or catchpoints.
delete bp-numberDelete the breakpoints, watchpoints, or catchpoints of the breakpoint ranges specified as arguments.
delete rangeDelete the breakpoints, watchpoints, or catchpoints of the breakpoint ranges specified as arguments.
disable bp-number-or-rangeDoes not delete breakpoints. Just enables/disables them.
enable bp-number-or-rangeDoes not delete breakpoints. Just enables/disables them.
enable bp-number onceEnables once
continue cContinue executing until next break point/watchpoint.
continue numberContinue but ignore current breakpoint number times. Usefull for breakpoints within a loop.
finishContinue to end of function.
step sStep to next line of code. Will step into a function.
step number-of-stepsStep number of steps. Will step into a function.
next nExecute next line of code. Will not enter functions.
next numberExecute next line of code. Will not enter functions.
until until line-numberContinue processing until you reach a function or filename:line-number.
stepi si nexti nistep/next assembly/processor instruction.
info signalsShow information on signals.
handle SIGNAL-NAME optionPerform the following option when signal recieved: nostop, stop, print, noprint, pass/noignore or nopass/ignore
whereShows current line number and which function you are in.
backtrace btShow trace of where you are currently.
bt inner-function-nesting-depthShow trace of where you are currently.
bt -outer-function-nesting-depthShow trace of where you are currently.
backtrace fullPrint values of local variables.
frame f number Select frame number.
up number down numberMove up/down the specified number of frames in the stack.
info frameList address, language, address of arguments/local variables and which registers were saved in frame.
info args info locals info catchInfo arguments of selected frame, local variables and exception handlers.
list lList source code.
list line-numberList source code.
list functionList source code.
list -List source code.
list start#,end#List source code.
list filename:functionList source code.
set listsize countNumber of lines listed when <tt>list command given.</tt>
directory dir directory-nameAdd specified directory to front of source code path.
show directoriesList directories in source code path.
directoryClear sourcepath when nothing specified.
print p variable-namePrint value stored in variable.
p file-name::variable-namePrint value stored in variable.
p 'file-name'::variable-namePrint value stored in variable.
p *array-variable@lengthPrint first # values of array specified by length. Good for pointers to dynamicaly allocated memory.
p/x variablePrint as integer variable in hex.
p/d variablePrint variable as a signed integer.
p/u variablePrint variable as a un-signed integer.
p/o variablePrint variable as a octal.
p/t variable x/b address x/b !variablePrint as integer value in binary. (1 byte/8bits)
p/c variablePrint integer as character.
p/f variablePrint variable as floating point number.
p/a variablePrint as a hex address.
x/w address x/4b !variablePrint binary representation of 4 bytes (1 32 bit word) of memory pointed to by address.
set gdb-option valueSet a GDB option
set logging onTurn on/off logging. Default name of file is <tt>gdb.txt</tt>
set logging offTurn on/off logging. Default name of file is <tt>gdb.txt</tt>
set logging file log-fileTurn on/off logging. Default name of file is <tt>gdb.txt</tt>
set print array onConvient readable format for arrays turned on/off.
set print array offDefault is off.
show print arrayDefault is off. Convient readable format for arrays turned on/off.
set print array-indexes onPrint index of array elements.
set print array-indexes offDefault off.
show print array-indexesDefault off. Print index of array elements.
set print pretty onFormat printing of C structures.
set print pretty offFormat printing of C structures.
show print prettyFormat printing of C structures.
set print union onPrint C unions.
set print union offDefault is on.
show print unionDefault is on. Print C unions.
set print demangle onControls printing of C++ names.
set print demangle offDefault is on.
show print demangleDefault is on. Controls printing of C++ names.
run r
run command-line-arguments
Start program execution from the beginning of the program.
run < infile > outfileThe command <tt>break main</tt> will get you started. Also allows basic I/O redirection.
continue cContinue execution to next break point.
killStop program execution.
quit qExit GDB debugger.
guides/gdb.txt · Last modified: 2014/04/02 22:39 (external edit)