2.4 Continuing execution using ‘step’, ‘finish’, and ‘next’ commands
Sometimes one needs to walk through the program statements and execute them one
by one to understand the problem and fix errors. For debugging a slightly large
program, focusing on a portion of the program is helpful for verifying the correct
functionality. The commands you learn here helps you to execute programs
line-by-line or a whole function all at once.
To execute a block of a program step-by-step, a breakpoint has to be placed at
the first line of the block. Once the breakpoint is met, one can begin to
step into the next line. In order to show how the stepping command works,
let’s put another breakpoint at line 42 of stack.h (run ‘b stack.h:42’ in
GDB).
Stepping commands in GDB
-
1.
- Once the execution is paused one a line, one can execute the current line
and step into the next line using step command. If there is a function
ahead, the GDB jumps into the function and executes the first line of it.
In the example, since the breakpoint is placed in the line of a function
call, ‘step’ goes into the full() function and executes the return statement
which is the only statement in the function( Figure 17 ).
-
2.
- If one wants to return from a function and jumps into the caller statement
finish command. The Figure 18 shows how ‘finish’ command returns
from full() function to the push() function and resumes the statement. It
shows the returned value from the function full().
-
3.
- One can skip from stepping into a function and directly see its result using
‘next’ command. From our example, instead of ‘step’ command in Figure
17, the command ‘next’ goes to the next line and skips stepping into the
full() function (Figure 19).
-
4.
- The rest of code can be always resumed using ‘cont’ command.
Stepping commands in Visual Studio
-
1.
- While a program execution is parked on a statement, one can step into
the next statement by hitting on either ‘Step Into’ icon in Figure 20 or
‘Step Over’ icon in Figure 22.
-
2.
- Using ‘Step Into’ on a function call moves the program counter on the
first line of the function definition. The next hit on either icons begins
executing the body of function. For example, if a breakpoint on line 44 of
the ‘stack.h’ file is met, you can go inside the function full() by hitting
‘Step Into’ icon (Figure 22).