Pages

Thursday, 10 May 2012

How to debug a c program using GDB

Consider the cprogram example.c
#include
void main()
{
int x,y,z;
x=10;
z=x/y;
printf("Z=%d\n",z);
}


Step 1. Compile the C program with debugging option -g

Compile your C program with -g option. This allows the compiler to collect the debugging information.

gcc -g example.c

The above command creates a.out file which will be used for debugging as shown below.

Step 2. Launch gdb

Launch the C debugger (gdb) as shown below.
gdb a.out

This will give a gdb prompt
(gdb)

Step 3. Set up a break point inside C program

Syntax:

break line_number

Other formats:

break [file_name]:line_number
break [file_name]:func_name

Places break point in the C program, where you suspect errors. While executing the program, the debugger will stop at the break point, and gives you the prompt to debug.

So before starting up the program, let us place the following break point in our program.


(gdb) break 3
Breakpoint 1 at 0x4004fc: file example.c, line 3.
(gdb) break 5
Note: breakpoint 1 also set at pc 0x4004fc.
Breakpoint 2 at 0x4004fc: file example.c, line 5.
(gdb)


Step 4. Execute the C program in gdb debugger


run [args]

You can start running the program using the run command in the gdb debugger. You can also give command line arguments to the program via run args. The example program we used here does not requires any command line arguments so let us give run, and start the program execution.

Step 5. Printing the variable values inside gdb debugger


Syntax: print {variable}

Examples:
print i
print j
print num

Step 6. Continue, stepping over and in – gdb commands


There are three kind of gdb operations you can choose when the program stops at a break point. They are continuing until the next break point, stepping in, or stepping over the next program lines.

c or continue: Debugger will continue executing until the next break point.
n or next: Debugger will execute the next line as single instruction.
s or step: Same as next, but does not treats function as a single instruction, instead goes into the function and executes it line by line.
(gdb) c
Continuing.

Program received signal SIGFPE, Arithmetic exception.
0x000000000040050b in main () at example1.c:6
6 z=x/y;
(gdb)
Continuing.

Program terminated with signal SIGFPE, Arithmetic exception.
The program no longer exists.





Step 7.Kill the Program
 
Once you find the bug, you might want to kill the program (that crashed halfway through) using the kill command:
  (gdb) kill 
Kill the program being debugged? (y or n) y 
(gdb)

Exiting from gdb

(gdb) quit


IN this program the variable y is not initialized so value of y is 0. so 10/0 is infinity.
So initializing y to some value the program will give a right output

#include
void main()
{
int x,y=5,z;
x=10;
z=x/y;
printf("Z=%d\n",z);
}

sunitha@sunitha-laptop:~/cdpgm$ gcc -g example1.c
sunitha@sunitha-laptop:~/cdpgm$ gdb a.out
GNU gdb (Ubuntu/Linaro 7.4-2012.02-0ubuntu2) 7.4-2012.02
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
...
Reading symbols from /home/sunitha/cdpgm/a.out...done.
(gdb) break 5
Breakpoint 1 at 0x400503: file example1.c, line 5.
(gdb) break 7
Breakpoint 2 at 0x400518: file example1.c, line 7.
(gdb) run
Starting program: /home/sunitha/cdpgm/a.out

Breakpoint 1, main () at example1.c:5
5 x=10;
Continuing.

Breakpoint 2, main () at example1.c:7
7 printf("Z=%d\n",z);
(gdb) c
Continuing.
Z=2
[Inferior 1 (process 3184) exited with code 04]
(gdb) quit
sunitha@sunitha-laptop:~/cdpgm$

Note:
Step 3 and step 5 is optional.

No comments:

Post a Comment