138Chapter5 • Working with GNU Debugger
call, it will not go into the function code and you will not see what happens inside the function
code. This is equivalent to the
step over
action used in many debuggers. On the other hand if you
use the
step
command, it will move to the next line but if the current line is a function call, it
will go into the function code and will take you to the first line inside the function. This is also
called a
step into
action in some debuggers.
5.2.2A Sample Session with gdb
In this section you go through a sample
gdb
session. You will use the following C code in
this session and will step through the code from top to bottom. This code is saved in a file
sum.c
and the output executable file is
sum
. The
sum.c
file is listed below.
#include
main ()
{
int num1, num2, total ;
printf("Enter first number : ");
scanf("%d", &num1);
printf("Enter second number : ");
scanf("%d", &num2);
total = num1 + num2;
printf("\nThe sum is : %d\n", total);
}
As you can see, the
sum.c
file takes two numbers as input. It then calculates the sum and
displays it as standard output. The compilation is done using the following command.
gcc –g sum.c –o sum
Table5-1 Common gdb commands
CommandDescription
run
Start a program execution inside
gdb
from the beginning
quit
Quit
gdb
print expr
Print expression, where expression may be a variable name
next
Go to next line
step
Step into next line
continue
Continue from the current place until end of program reaches or you find a break
point
Next Page >>
<< Previous Page
Back to the Table of Contents