146Chapter5 • Working with GNU Debugger
Enter second number : 5
14 total = sum(num1, num2);
(gdb) s
sum (num1=4, num2=5) at sumf.c:21
21 result = num1 + num2 ;
(gdb) finish
Run till exit from #0 sum (num1=4, num2=5) at sumf.c:21
Calculation complete. Returning ...
0x080484ec in main () at sumf.c:14
14 total = sum(num1, num2);
Value returned is $1 = 9
(gdb) n
15 printf("\nThe sum is : %d\n", total);
(gdb) n
The sum is : 9
16}
(gdb) n
Program exited with code 020.
(gdb)
As you can see from the above example, all
gdb
commands can be abbreviated as long as
they can’t be confused with any other command. You can use
n
for
next
,
bre
for the
break
command and
s
for the
step
command. Also note that when you return from the function call,
the return value of the function is displayed.
5.4Working with the Stack
One of the most important tasks during the debugging process is to deal with the stack. The
stack provides a lot of information about what happened in the past. You can backtrace to higher
levels in the stack using
gdb
. Using the backtrace feature, you can move step-by-step backward
to find out how you reached the current position. To demonstrate some of the commands used
with stack, let us take the example of
sumf.c
program that you used in the last section. The
program takes two numbers as input and then calls a function to
sum()
to add these two num-
bers. The function returns the result back to the main function.
When you start debugging this program, first you set up a
break
point right at the begin-
ning of the main function and then use the
next
command repeatedly until you reach the line
where the function is called. At this point you use the
step
command instead of the
next
com-
mand to move into the function code. After entering the function code, some other commands
related to stack are used in the following
gdb
session. These commands are explained at the end
of this session listing.
Next Page >>
<< Previous Page
Back to the Table of Contents