[contents] [usage] [execution] [stack] [breakpoints] [watchpoints] [advanced]

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...?

  1. stop execution
  2. continue execution
  3. see where my program stopped?
  4. step through my code line-by-line?
  5. examine variables?
  6. modify variables?
  7. call functions linked into my program?
  8. return from a function?



2.1 How do I stop execution? [top]   [toc]

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.

(gdb) run
Starting Program: /home/ug/ryansc/a.out

Program received signal SIGINT, Interrupt.
0x80483b4 in main(argc=1, argv=0xbffffda4) at loop.c:5
5   while(1){
...
(gdb)


2.2 How do I continue execution? [top]   [toc]

Use the continue command to restart execution of your program whenever it is stopped.


2.3 How do I see where my program stopped? [top]   [toc]

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.

(gdb) list
3       int main(int argc, char **argv)
4       {
5         int x = 30;
6         int y = 10;
7       
8         x = y;
9       
10        return 0;
11      }


2.4 How do I step through my code line-by-line? [top]   [toc]

First stop your program by sending it signals or using breakpoints. Then use the next and step commands.

5   while(1){
(gdb) next
7   }
(gdb)

*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:

(gdb)
11     fun1();
(gdb) next
12 }

The step command:

(gdb)
11     fun1();
(gdb) step;
fun1 () at loop.c:5
5    return 0;
(gdb)


2.5 How do I examine variables? [top]   [toc]

Use the print command with a variable name as the argument. For example, if you have int x and char *s:

(gdb) print x
$1 = 900
(gdb) print s
$3 = 0x8048470 "Hello World!\n"
(gdb)

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.


2.6 How do I modify variables? [top]   [toc]

Use the set command with a C assignment statement as the argument. For example, to change int x to have the value 3:

(gdb) set x = 3
(gdb) print x
$4 = 3

NOTE: in newer versions of gdb, it may be necessary to use the command 'set var', as in 'set var x = 3'


2.7 How do I call functions linked into my program? [top]   [toc]

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:

(gdb) call abort()


2.8 How do I return from a function? [top]   [toc]

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.

(gdb) finish
Run till exit from #0  fun1 () at test.c:5
main (argc=1, argv=0xbffffaf4) at test.c:17
17        return 0;
Value returned is $1 = 1





[contents] [usage] [execution] [stack] [breakpoints] [watchpoints] [advanced]

Questions? Comments? Flames? email rms@unknownroad.com