Debugging Optimized Code161
int num1, num2, total ;
num1 = 4;
num2 = 6;
total = num1 + num2;
printf("\nThe sum is : %d\n", total);
}
Now let us compile the code without optimization using the following command and then
step through it.
gcc -g sumopt.c -o sumopt
Following is the
gdb
session to step through the code. As you can see, it is quite straight-
forward.
gdb
executes all lines in order until it reaches end of the program.
[rr@conformix 5]$ gdb sumopt
GNU gdb 5.0rh-5 Red Hat Linux 7.1
Copyright 2001 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public
License, and you are
welcome to change it and/or distribute copies of it under
certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty"
for details.
This GDB was configured as "i386-redhat-linux"...
(gdb) break main
Breakpoint 1 at 0x8048466: file sumopt.c, line 6.
(gdb) run
Starting program: /home/rr/5/sumopt
Breakpoint 1, main () at sumopt.c:6
6 num1 = 4;
(gdb) n
7 num2 = 6;
(gdb) n
8 total = num1 + num2;
(gdb) n
10 printf("\nThe sum is : %d\n", total);
(gdb) n
The sum is : 10
11}
(gdb) n
Program exited with code 021.
(gdb)