Debugging with GBD

Debugging with GBD - The GNU Debugger - GDB A debugger is a...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
The GNU Debugger - GDB
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
A debugger is a program that runs other programs, allowing the user to exercise control over these programs, and to examine variables when problems arise. GNU Debugger which is called gdb is the most popular debugger for UNIX systems to debug C and C++ Programs. gdb can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act: Start your program, specifying anything that might affect its behavior. Make your program stop on specified conditions. Examine what has happened, when your program has stopped. Change things in your program, so you can experiment with correcting the effects of one bug and go on to learn about another. GDB uses a simple command line interface and allows you to do things like run the program up to a certain point then stop and print out the values of certain variables at that point, or step through the program one line at a time and print out the values of each variable after executing each line. Starting and Stopping Compiles the program with the debugging option (-g). The out file contains debugging information that lets you use variables and function names inside GDB, rather than raw memory locations. gcc –g -o program You can invoke gdb in three ways: The most usual way is with one argument, specifying an executable program: gdb program Or start with both an executable program and a core file specified: gdb program core Or specify a process ID as a second argument, if you want to debug a running process: gdb program PID Opens GDB with the executable, but does not run the program. You’ll see a prompt (gdb) gdb -help to display all available options and briefly describe their use (`gdb -h'). quit to exit GDB, use the quit command (q). Run Program You can run a program directly, pass arguments, or feed in a file.
Background image of page 2
r r arg1 arg2 r < file1 Stepping through Code l l line# l function List 10 lines of source code for current line, a specific line, or for a function. where Show current line number and which function you are in. next Run program until next line, then pause. If the current line is a function, execute the entire function, then pause. Next is good for walking through your code quickly. step Run the next instruction, not line. If the current instruction is setting a variable, it is the same as next. If it’s a function, it will jump into the function, execute the first statement, then pause. Step is good for diving into the details of your code. finish Finish executing the current function, then pause (also called step out). Useful if you accidentally stepped into a function. Breakpoints and Watchpoints Breakpoints pause (break) a program when it reaches a certain location. You can examine and change variables, then resume execution. This is helpful when seeing why certain inputs fail, or testing inputs. You can set a breakpoint at a certain line, or at a function. The program will pause when it reaches the breakpoint. break
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 10/15/2010.

Page1 / 11

Debugging with GBD - The GNU Debugger - GDB A debugger is a...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online