View this PageEdit this PageUploads to this PageVersions of this PageHomeRecent ChangesSearchHelp Guide

gdb cheat sheet

Antes de empezar, este cheat sheet es muy completo y conciso.

NOTA: Muy interesante como referencia adicional: GDB in a nutshell.

File: ~rlt/cs240/general/gdb -- gdb cheat sheet

Copyright 1988 Paul English (pme@umb.umb.edu), edited by eoneil by permission. Reformatted by dario@darionomono.com without permission (for local use only). Added some infor about hardware breakpoints.

gdb is the GNU source level debugger. To use gdb, you must make sure you compile your programs with the `-g' flag, e.g., gcc -g subs.c

To run gdb on a program, just enter `gdb progname' (or M-x gdb in emacs). Note that most commands can be abbreviated to the initial character. Pressing RETURN repeats the last command in a useful way. This is often done with `list', for example.

Here are the basic gdb commands:

Run


set args SinRPCsponer como parametros del prgrama "SinRPCs"
r run program
r smallrun program, and give it "small" as an argument.
r infile run program, redirect input from file infile.

Breakpoints


b main set a breakpoint in function main().
b 36 break at line 36 of current source file.
i b info breakpoints. Lists all breakpoints now set up.
d delete all breakpoints (d 1 for just #1).

Breakpoints on read or write a variable (hardware watchpoints)


rwatch *0xfeedface set read watchpoint on memory location (doesn't support variable names, only raw pointers)
watch *0xfeedface set write watchpoint on memory location
awatch *0xfeedface set read/write (access) watchpoint on memory location (doesn't support variable names, only raw pointers)
show can-use-hw-watchpoints return 0 if not supported, 1 if supported


Execute


n execute next line of code.
s like next, but steps inside function calls.
c continue. Run from this point until breakpoint or program exit.

Examine


p foo print variable foo
p/x foo print foo in hex (/o = octal, /d = decimal)
i lo info locals: values of all local vars, current function
i var info variables: values of global/static vars
i s info stack: calls made to get to this execution point.
x 0x20034 examine memory address 0x20034
x/s 0x20034 examine memory, addr 0x20034, as a string (also x/f

Source


l list source code (list 18 = code near line 18, list main, etc.)
i fun list of all functions in the program.

Help

h top-level help, lists topics
h b help on break command
h breakp help on topic of breakpoints

Exit


q quit gdb



Para que pare cuando se modifica una variable se usa watch (ver post):

One little subtlety, if you do something like:



(gdb) watch foo->bar->baz


gdb will watch all three locations so you will know if the result of the expression changes for any reason. This is sometimes what you want, and sometimes not. If you know you just want to watch a simple memory location, it is sometimes easier to do:


(gdb) print &(foo->bar->baz)
(gdb) watch *[HEX address returned from the previous command]


The * in this case tells gdb that you are giving it an address, and not an expression... And of course, without the []'s...




Otro cheat sheet:
http://web.cecs.pdx.edu/~jrb/cs201/lectures/handouts/gdbcomm.txt

GDB commands by function - simple guide
---------------------------------------
More important commands have a (*) by them.

Startup 
% gdb -help         	print startup help, show switches
*% gdb object      	normal debug 
*% gdb object core 	core debug (must specify core file)
%% gdb object pid  	attach to running process
% gdb        		use file command to load object 

Help
*(gdb) help        	list command classes
(gdb) help running      list commands in one command class
(gdb) help run        	bottom-level help for a command "run" 
(gdb) help info         list info commands (running program state)
(gdb) help info line    help for a particular info command
(gdb) help show         list show commands (gdb state)
(gdb) help show commands        specific help for a show command

Breakpoints
*(gdb) break main       set a breakpoint on a function
*(gdb) break 101        set a breakpoint on a line number
*(gdb) break basic.c:101        set breakpoint at file and line (or function)
*(gdb) info breakpoints        show breakpoints
*(gdb) delete 1         delete a breakpoint by number
(gdb) delete        	delete all breakpoints (prompted)
(gdb) clear             delete breakpoints at current line
(gdb) clear function    delete breakpoints at function
(gdb) clear line        delete breakpoints at line
(gdb) disable 2         turn a breakpoint off, but don't remove it
(gdb) enable 2          turn disabled breakpoint back on
(gdb) tbreak function|line        set a temporary breakpoint
(gdb) commands break-no ... end   set gdb commands with breakpoint
(gdb) ignore break-no count       ignore bpt N-1 times before activation
(gdb) condition break-no expression         break only if condition is true
(gdb) condition 2 i == 20         example: break on breakpoint 2 if i equals 20
(gdb) watch expression        set software watchpoint on variable
(gdb) info watchpoints        show current watchpoints

Running the program
*(gdb) run        	run the program with current arguments
*(gdb) run args redirection        run with args and redirection
(gdb) set args args...        set arguments for run 
(gdb) show args        show current arguments to run
*(gdb) cont            continue the program
*(gdb) step            single step the program; step into functions
(gdb) step count       singlestep \fIcount\fR times
*(gdb) next            step but step over functions 
(gdb) next count       next \fIcount\fR times
*(gdb) CTRL-C          actually SIGINT, stop execution of current program 
*(gdb) attach process-id        attach to running program
*(gdb) detach        detach from running program
*(gdb) finish        finish current function's execution
(gdb) kill           kill current executing program 

Stack backtrace
*(gdb) bt        	print stack backtrace
(gdb) frame        	show current execution position
(gdb) up        	move up stack trace  (towards main)
(gdb) down        	move down stack trace (away from main)
*(gdb) info locals      print automatic variables in frame
(gdb) info args         print function parameters 

Browsing source
*(gdb) list 101        	list 10 lines around line 101
*(gdb) list 1,10        list lines 1 to 10
*(gdb) list main  	list lines around function 
*(gdb) list basic.c:main        list from another file basic.c
*(gdb) list -        	list previous 10 lines
(gdb) list *0x22e4      list source at address
(gdb) cd dir        	change current directory to \fIdir\fR
(gdb) pwd          	print working directory
(gdb) search regexpr    forward current for regular expression
(gdb) reverse-search regexpr        backward search for regular expression
(gdb) dir dirname       add directory to source path
(gdb) dir        	reset source path to nothing
(gdb) show directories        show source path

Browsing Data
*(gdb) print expression        print expression, added to value history
*(gdb) print/x expressionR        print in hex
(gdb) print array[i]@count        artificial array - print array range
(gdb) print $        	print last value
(gdb) print *$->next    print thru list
(gdb) print $1        	print value 1 from value history
(gdb) print ::gx        force scope to be global
(gdb) print 'basic.c'::gx        global scope in named file (>=4.6)
(gdb) print/x &main     print address of function
(gdb) x/countFormatSize address        low-level examine command
(gdb) x/x &gx        	print gx in hex
(gdb) x/4wx &main       print 4 longs at start of \fImain\fR in hex
(gdb) x/gf &gd1         print double
(gdb) help x        	show formats for x
*(gdb) info locals      print local automatics only
(gdb) info functions regexp         print function names
(gdb) info variables  regexp        print global variable names
*(gdb) ptype name        print type definition
(gdb) whatis expression       print type of expression
*(gdb) set variable = expression        assign value
(gdb) display expression        display expression result at stop
(gdb) undisplay        delete displays
(gdb) info display     show displays
(gdb) show values      print value history (>= gdb 4.0)
(gdb) info history     print value history (gdb 3.5)

Object File manipulation
(gdb) file object      		load new file for debug (sym+exec)
(gdb) file             		discard sym+exec file info
(gdb) symbol-file object        load only symbol table
(gdb) exec-file object 		specify object to run (not sym-file)
(gdb) core-file core   		post-mortem debugging

Signal Control
(gdb) info signals        	print signal setup
(gdb) handle signo actions      set debugger actions for signal
(gdb) handle INT print          print message when signal occurs
(gdb) handle INT noprint        don't print message
(gdb) handle INT stop        	stop program when signal occurs
(gdb) handle INT nostop         don't stop program
(gdb) handle INT pass        	allow program to receive signal
(gdb) handle INT nopass         debugger catches signal; program doesn't
(gdb) signal signo        	continue and send signal to program
(gdb) signal 0        		continue and send no signal to program

Machine-level Debug
(gdb) info registers        	print registers sans floats
(gdb) info all-registers        print all registers
(gdb) print/x $pc        	print one register
(gdb) stepi        		single step at machine level
(gdb) si        		single step at machine level
(gdb) nexti        		single step (over functions) at machine level
(gdb) ni        		single step (over functions) at machine level
(gdb) display/i $pc        	print current instruction in display
(gdb) x/x &gx        		print variable gx in hex
(gdb) info line 22        	print addresses for object code for line 22
(gdb) info line *0x2c4e         print line number of object code at address
(gdb) x/10i main        	disassemble first 10 instructions in \fImain\fR
(gdb) disassemble addr          dissassemble code for function around addr

History Display
(gdb) show commands        	print command history (>= gdb 4.0)
(gdb) info editing       	print command history (gdb 3.5)
(gdb) ESC-CTRL-J        	switch to vi edit mode from emacs edit mode
(gdb) set history expansion on       turn on c-shell like history
(gdb) break class::member       set breakpoint on class member. may get menu
(gdb) list class::member        list member in class
(gdb) ptype class               print class members
(gdb) print *this        	print contents of this pointer
(gdb) rbreak regexpr     	useful for breakpoint on overloaded member name

Miscellaneous
(gdb) define command ... end        define user command
*(gdb) RETURN        		repeat last command
*(gdb) shell command args       execute shell command 
*(gdb) source file        	load gdb commands from file
*(gdb) quit        		quit gdb




Para poder hacer hexdumps, añade lo siguiente a ~/.gdbinit
# https://stackoverflow.com/questions/25786982/how-can-gdb-show-both-hex-and-ascii-when-examing-memory
define hexdump
    dont-repeat
    if $argc == 3
        set $width = $arg2
    else
        set $width = 8
    end
    set $addr = (char *)($arg0)
    set $endaddr = $addr + $arg1
    while $addr < $endaddr
        printf "%p: ", $addr
        set $lineendaddr = $addr + $width
        if $lineendaddr > $endaddr
            set $lineendaddr = $endaddr
        end
        set $a = $addr
        while $a < $lineendaddr
            printf "0x%02x ", *(unsigned char *)$a
            set $a++
        end
        while $a < $addr + $width
            printf "     "
            set $a++
        end
        printf "'"
        set $a = $addr
        while $a < $lineendaddr
            printf "%c", *(char *)$a < 32 || *(char *)$a > 126 ? '.' : *(char *)$a
            set $a++
        end
        printf "'\n"
        set $addr = $addr + $width
    end
end

document hexdump
    usage: hexdump address count [width=8]
end


Uso (ver 20 bytes desde la dirección que tenga el puntero "ptr"):
 (gdb) hexdump ptr 20



Para hacer que muestre algo cada vez que se hace next usando "n" (visto aquí):
(gdb) def n   
Type commands for definition of "n".
End with a line saying just "end".
>next
>hexdump buf+i 20
>end
(gdb) n
53          for (; i < size; i++)
0x7ffffffbe037: 0x1e 0x00 0x00 0x00 0x01 0x00 0x00 0x00 '........'
0x7ffffffbe03f: 0x01 0x06 0x05 0x5b 0x40 0xf7 0x16 0x6b '...[@..k'
0x7ffffffbe047: 0xb9 0x5d 0x47 0x46                     '.]GF'
(gdb)