# STUDY 'info gdb' (or web equivalent); ALSO, use `help' inside gdb # There are a huge number of valuable tricks there. # WARNING: turn off _all_ optimization for gdb, to avoid confusion # gcc -O0 -g myfile.c # -gstabs also an option for gcc #TRICK 1: Note that TAB completion usually works. Try after # partial command when stuck. Also, the cursor keys, # and a subset of emacs commands work on the command line. #TRICK 2: means execute the last command. #TRICK 3: After `list 100', `list foo', etc., `list' means list the # the following few lines. # `search REGEXP' lists the next line matching REGEXP # `reverse-search REGEXP' looks backwards #TRICK 4: For segfault: gdb a.out (gdb) r (gdb) where # and then use list, print, etc. to find out why it segfaulted. #COMMON COMMANDS: # run, where, print, ptype, list, break, step, next, finish, until # where: show stack; print: expression; ptype: print type; # list: current source code, or can specify line number, function, file # break: at line number of at function # step: step to next line or step _into_ current function # next: step to next line or step _over_ current function to next line # finish: continues until current program finishes # until: similar idea for loops, continues until reaching a line number # greater than current line number; can specify line number argument # Equivalent of adding a conditional print statement, but does not require # one to recompile break main.c:121 if x > 0 commands silent printf "x is %d]n", x c end #The convenience variable, $bpnum, is set to the number of the last breakpoint. #This is convenient for recording and referring back to a breakpoint. break bar ignore $bpnum 100 set $barBreak $bpnum # Now the variable $barBreak has as its value the breakpoint number for bar. #QUESTION TO MYSELF: # How can one set a breakpoint for 15 lines after the beginning of foo? # For list, one can do: list foo list +15 # But shat is the analog of this for break? #An alternative method, that works before and after a function, foo, is: define hook-foo ... end define hookpost-foo ... end #Execute every time your program stops: define hook-stop ... end # hook-echo and hookpost-echo, as well as other gdb built-in commends. # We want: break foo if localVariable == 100 # But gdb complains that localVariable is not part of current scope. # So, we set a temporary tbreak (one-time only), that creates the # desired break, and then continues. tbreak foo commands silent b if localVariable == 100 c end # If error occurs after 100 iterations break foo ignore XXXX 100 watch x if x == 0 # Other break commands include: enable, disable, delete, clear # Show value of foo(x) at each breakpoint display foo(x) # Create new command define ps shell ps -elfww | grep a.out end # Force printing in hex p/x myvariable # p and ptype exist. ptype prints the type. ptype works on variable or on type # p *foo, ptype *foo can also be useful. ptype struct bar p (*x)->field p programFunction(x, 17) # whatis is more compact than ptype (gdb) whatis v type = struct complex (gdb) ptype v type = struct complex { double real; double imag; } # Variables can also be reset # This is useful for confirming if you have found the cause of a bug. # Set breakpoint to stop at bug, correct variable to right value, and then cont set myVar = 3 set variable myVar = 3 info types complex info variables REGEXP info functions REGEXP # Print all local variables of foo info scope foo # Signals that get in the way. # For example, some O/S's will pass SIGWINCH to your program, and suppose # you don't have a signal handler for SIGWINCH handle SIGWINCH nostop noprint nopass #C++ # overloaded methods b Foo::bar # should show a menu of choices #Alternative b 'Foo::bar:: # If using namespace: b Name::Foo::bar # If static, global in a specific file p 'file.c'::x # Threads # By default, break sets breakpoint for all threads, but break thread THREADNO # exists. # Note that when you single-step one thread, the O/S may also allow # another thread to execute, and even stop the other thread in the # middle of a statement. # Setting below to on allows only current thread to run. Setting it to step # makes it more likely to follow current thread # when using step (and maybe when using next) set scheduler-locking on/off/step #TUI mode: Test User Interface # Type: C-x a # to switch back and forth between TUI mode. TUI mode uses curses # to give you full-screen mode # `man info' has commands, but some to try are: help layout help focus refresh, update, winheight #Tracepoints # If the act of using gdb changes your output due to added delays before # an instruction is reached, use tracepoint