Primer on Using gdb to Debug C Programs A debugger, such as gdb (GNU debugger), is one of the most magical pieces of software because it works and you can't fully explain it - like an OS. Before I get into the basics, I would just warn you to first try using printf() statements throughout your code. It's much easier to use, can help find easy bugs, and trains you to think like a computer running your program. 0) You must compile your source code with the debugging flag. In gcc, add the '-g' flag to the command line. Your compiled program will then have extra debugging info added. Try comparing the size of your program with and without the debugging flag. 1) Starting the debugger. Start by typing 'gdb' at the unix prompt. 2) Loading your program. Use the file command: 'file ' If you recompile your program, you must reload your program with this command. 3) Setting breakpoints. Before you run your program, you'll probably want to set a breakpoint which suspends execution and lets you examine any variables in scope. Breakpoints may be set at any point in your code. A common starting point is at the beginning, the main() function. Set the breakpoint with 'breakpoint main'. You can set breakpoints at any function entry with this command. When stepping through code, you can set a breakpoint at the current line with just 'breakpoint'. 4) Running your program. For the most part, you can run your program just like you would on the unix command line. Instead of typing your program name, just type 'run' plus any command line arguments. Your program will run like normal until it hits a breakpoint. Then you'll get the interactive prompt where you can examine variables or step through your code line by line. 5) Examining variables. Any variable in scope can be printed with the print command: 'print ' You probably want to check the value of variables before and after it changes to make sure it's what you expect. 6) Stepping through code. There are 3 relevant commands: step, next, continue. If you want to go through every single line of code (including entering into any function call), use 'step'. If you want to treat function calls as a single line of code, use 'next'. For lines of code without function calls, step and next will behave the same. You can also add a number argument to repeat that many times. So if you want to step through 4 lines of code, use 'step 4'. Lastly, 'continue' will continue execution until hitting the next breakpoint. This is useful when setting a breakpoint at a function or loop entry. 7) Quitting. Just type 'quit'. BONUS: gdb support is well integrated into emacs, and if you're using gdb, I highly recommand using it in emacs. Use the emacs command: 'M-x gdb' which will prompt you for the name of your program. After entering your program, gdb will load it. Now set breakpoints and run your code. Marvel at emacs as you step through your code. Enjoy!