2. How do I watch the execution of my program?Gdb functions somewhat like an interpreter for your programs. You can stop your program at any time by sending it signals. Normally this is done using key combinations like Ctrl-C for the interrupt signal SIGINT. Outside of gdb this would terminate your program. Gdb traps this signal and stops executing your program. Also, using breakpoints you can have your program stop executing at any line of code or function call. Once your program is stopped, you can examine 'where' it is in your code. You can look at the variables currently in scope, as well as your memory space and the cpu registers. You can also change variables and memory to see what effect it has on your code.
How do I...?
You can stop execution by sending your program UNIX symbols like SIGINT. This is done using the Ctrl-C key combination. In the following example, I pressed Ctrl-C after 'Starting Program...' appeared.
Use the continue command to restart execution of your program whenever it is stopped.
Use the list command to have gdb print out the lines of code above and below the line the program is stopped at. In the example below, the breakpoint is on line 8.
First stop your program by sending it signals or using breakpoints. Then use the next and step commands.
*NOTE* the next and step commands are different. On a line of code that has a function call, next will go 'over' the function call to the next line of code, while step will go 'into' the function call.
The next command:
The step command:
Use the print command with a variable name as the argument. For example,
if you have
NOTE: The output from the print command is always formatted $## = (value). The $## is simply a counter that keeps track of the variables you have examined.
Use the set command with a C assignment statement as the argument. For
example, to change
NOTE: in newer versions of gdb, it may be necessary to use the command 'set var', as in 'set var x = 3'
From the debugger command line you can use the call command to call any function linked into the program. This includes your own code as well as standard library functions. For example, if you wish to have your program dump core:
Use the finish command to have a function finish executing and return to it's caller. This command also shows you what value the function returned.
[contents] [usage] [execution] [stack] [breakpoints] [watchpoints] [advanced]
Questions? Comments? Flames? email email@example.com