Some notes on using the gdb debugger to help debug C/C++ code. The following will get you started. There is much more to it! Also see: http://www.yolinux.com/TUTORIALS/GDB-Commands.html *************************************************************************************************** Important information: **you may want to read this twice** *************************************************************************************************** In the following: means for you to insert your specific name (without <>) $ at the beginning of a line is the command line prompt (don't type it) (gdb) at the beginning of a line is the gdb prompt (don't type it) # on a line begins a coment. Don't type it or the rest of a line Note that on the (gdb) command line Up and down cursor steps you through previous commands and you can edit/repeat them Tab completion works: (e.g., he completes the command to help; h lists options - try it!) Hitting (aka carriage return) without a command, repeats the previous command For many gdb commands you need only type the first letter; for most others only the first few are required. - In the following, commands that require more that one letter have the required letters in parenthesis - e.g., run --> typing only "r" will do (cl)ear --> type at least "cl" and as many more characters as you wish ********************************************* Compiling and running ********************************************* Add the "-g" switch to your compile comand This includes debugger info in the executable. It makes your executable file bigger and perhaps slower, so in general, don't use -g unless you plan on debugging. $ gcc -g -o $ gdb # This runs your program with the debugger $ gdbtui # Alternate. This gives you a "window" to see your code ********************************************* Basic running and quitting ********************************************* Now you are in the debugger ready to start. (gdb) help # For help --- or just type "h" (gdb) help # For help on command (gdb) quit # To quit gdb --- or just type "q" Ctrl-d will also exit gdb (gdb) run # To run code --- but you might want to first set breakpoints # Note: Ctrl-c will "break" (stop) a running program (gdb) run arg1 arg2 # run with command line arguments arg1 and arg2 (gdb) run arg1 arg2 >file.out # also redirects results to file.out (gdb) (fil)e # to load another program for debugging ********************************************* Debugging - working with breakpoints ********************************************* Before you start running code (and during debugging), you may want to set one or more breakpoints Running code will stop before executing a breakpoint line (gdb) break # "break" or just "b" by itself sets a breakpoint on the current line (gdb) break # break on line of the current file (gdb) break + # break lines ahead of the current file (gdb) break - # break lines before the current file (gdb) break # break on entering the given function (gdb) break : # break on line of another source file (if code is broken into multiple files) (gdb) break : # break entering the function in the given file (gdb) exit # break on exiting the program (follow by "bt" to see where you're exiting) (gdb) (cl)ear # deletes breakpoint on the current line (gdb) (cl)ear # clears breakpoint where is any method above that identifies a breakpoint (gdb) delete # delete all breakpoints (prompts for confirmation) (gdb) delete # delete one or more breakpoints specified by line number (see info breakpoints) (gdb) info breakpoints # get info on breakpoints (or just type "i b") note line number is given at the end (gdb) (save) break # save breakpoints to file (gdb) (so)urce # restore breakpoints from a file, append to current list of breakpoints (gdb) (wa)tch # break when the variable changes (must be in the current scope --- may slow gdb) # to remove watch, use "i b" to list breakpoints (find #), then "delete" that number (gdb) bt # "backtrace" -- print the function calls shown on the stack (where you are) ********************************************* Running and stepping (stops on breakpoints) ********************************************* (gdb) run # Runs from the beginning (restarts); prompts if you are currently debugging (gdb) continue # Continue running from the current location (gdb) continue # Continue continuing until breakpoints have been encountered times (gdb) next # Next line (step over any functions - don't go in) (gdb) next # Break after next commands (gdb) step # Step into a function (or to next line of code if there isn't a function on this line) (gdb) step # Break after step commands (gdb) until # Continues, but stops on 1st line after current line (steps out of loop when you get to end) (gdb) until # Continues until is reached (like a temporary breakpoint) (gdb) (fin)ish # Run to end of current function, then break (gdb) (ret)urn # Returns from current function now (skipping remaining code) (gdb) (ret)urn # Returns from current function now; function will return the value ********************************************* Looking at variables, sections of code ********************************************* (gdb) list # list (print) 10 lines of code around the current location (if previous command wasn't a list) (gdb) list # lists next 10 lines if the previous command was a list command (gdb) # lists next 10 lines if the previous command was a list command (gdb) list # list 10 lines of code around line in this file (gdb) list + # List 10 lines lines of code after the previous list command (or current location) (gdb) list - # List 10 lines lines of code before the previous list command (or current location) (gdb) list # list 10 lines of code around (gdb) list : # list 10 lines of code around in the given file (gdb) frame # frame --- gives the current execution line (gdb) print # print the current value of the variable in the current scope (may be array or struct, etc.) (gdb) print # evaluates expression and prints result (you can even call a function and print the result) (gdb) (pt)ype # print the type of the variable (e.g., unsigned int) (gdb) (set) # set variable to a value (e.g., set x = 3) --- must type "set"; no shortcut