Controlling Execution145
printf("Enter second number : ");
scanf("%d", &num2);
total = sum(num1, num2);
printf("\nThe sum is : %d\n", total);
}
int sum(int num1, int num2)
{
int result;
result = num1 + num2 ;
printf("\nCalculation complete. Returning ...\n");
return (result);
}
Let us create an executable
sumf
from this source code using the
gcc
compiler. This exe-
cutable then can be debugged using GNU debugger as shown in the next session. Note that we
use the
step
command (abbreviated as
s
) to step into the function code. We use the
finish
command to complete execution of code in the function
sum()
instead of tracing it line by line.
After the
finish
command is executed, you go to the next line in the
main()
function just
after the call to the
sum()
function.
[rr@conformix 5]$ gdb sumf
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) bre main
Breakpoint 1 at 0x8048496: file sumf.c, line 9.
(gdb) run
Starting program: /home/rr/5/sumf
Breakpoint 1, main () at sumf.c:9
9 printf("Enter first number : ");
(gdb) n
10 scanf("%d", &num1);
(gdb) n
Enter first number : 4
11 printf("Enter second number : ");
(gdb) n
12 scanf("%d", &num2);
(gdb) n
Next Page >>
<< Previous Page
Back to the Table of Contents