Linux Bible

Home



After you load the program and its core dump into the debugger, run the program in the debugger. To do so, type the command run at the GDB command prompt,(gdb), as the following example shows: (gdb) run


Starting program: /home/kwall/code/debugme
Program received signal SIGSEGV, Segmentation fault.
0x0804483db in index_to_the_moon (ary=0xbffff4b0) at debugme.c:24
24 ary[i] = i;


This short output listing shows that the segmentation fault occurred in the function index_to_the_moon at line 24 of debugme.c. Notice the last line of the output; GDB displays the line of code, prefixed with the line number (24), where the segmentation fault occurred. It also shows the memory address (in hexadecimal format) at which the fault occurred:

0xbffff4b0. You can pass any arguments to the run command that your program would ordinarily accept. GDB also creates a full shell environment in which to run the program. Ordinarily, GDB uses the value of the environment variable $SHELL to create the simulated environment. If you want, however, you can use GDB's set and unset commands to set or unset arguments and environment variables before you use the run command to run the program in the debugger.


To set command-line arguments to pass to the program, type set args arg1 arg2 , where arg1 and arg2 (or any number of arguments) are options and arguments the program being debugged expects. Use set environment env1 env2 to set environment variables (again, env1 and env2 are placeholders for the environment variables you want to set or unset).


Inspecting Code in the Debugger

What is happening in the function index_to_the_moon that's causing the error? You can execute the backtrace (or bt or back ) command to generate the function tree that led to the segmenta- tion fault. The backtrace doesn't usually show you what the problem is, but it does show you more precisely where the problem occurred. Here's how the function trace for the example looks on my system:
(gdb) backtrace
#0 0x080483db index_to_the_moon (ary=0x7ffffc90) at debugme.c:24
#1 0x080483a6 in main (argc=104,argv=0x69) at debugme.c:15


The continuation/full version of this article read on site - www.podgrid.org - Linux Bible


 
© 2009