144Chapter5 • Working with GNU Debugger
5.3Controlling Execution
Now you are already familiar with some of the
gdb
commands used for controlling execution of
a program. The commands you have already used are
run
,
quit
and
next
commands. In this
section you will learn some more commands that can be used to control execution of the program.
The most commonly used commands to control execution of a program in
gdb
are as follows:
•The
run
command is used to start execution of a program. If the program is already
running, you can restart the execution right from the beginning using the
run
command.
•The
quit
command will quit the debugger.
•The
kill
command stops debugging but does not quit the debugger.
•The
continue
command starts executing program from the current position. The
difference between the
continue
and
run
commands is that the
run
commands
starts execution from the entry point of the program while the
continue
command
starts execution from the current location.
•The
next
command goes to the next line in the code. If the current line is a function
call, it completes the function call without going into the function code.
•The
step
command goes to the next line in the code. If the current line is a function
call, it goes to the first line inside that function.
•The
finish
command takes you out of the function call, if you are already inside one.
•The
return
command returns to the caller of the current frame in the stack. This
means that you can return from a function without actually completing the function
code execution.
There are many ways to control execution of a program. Mastery of these methods is nec-
essary to debug a program more efficiently.
5.3.1The step and finish Commands
Here we have a short demonstration of how to use
finish
and
step
commands when
you are dealing with a function. Consider the following
sumf.c
program that adds two num-
bers and prints the result of addition. The addition is completed inside a function called
sum()
.
#include
int sum(int num1, int num2);
main ()
{
int num1, num2, total ;
printf("Enter first number : ");
scanf("%d", &num1);
Next Page >>
<< Previous Page
Back to the Table of Contents