This chapter contains the following sections:
Source Positioning
Changing the Viewing Position
Changing the Execution Position
Synchronizing the Execution and Viewing Positions
Controlling Program Execution
Starting the Program
Halting and Continuing Execution
Single-Step Execution
Stepping through at the Machine Level
Notes About Program Execution
Searching through the Source Window
Searching for a Function
Searching for a String
Jumping to a Source Line
When you have the Source Window open and it displays a source file, there are two points of reference to keep in mind: the execution position and the viewing position.
The execution position refers to the line of source at the Program Counter address. This line is always the next statement or instruction to be executed. When you load a file into the Source Window, CrossView Pro automatically displays the portion of the source code that contains the execution position.
The viewing position (also called 'cursor') is the line currently being examined in the displayed source file. Since many Source Window operations act on this line, you can think of the viewing position as the 'current line'. For instance, if you set a breakpoint without specifying a line number, CrossView Pro sets the breakpoint at the line marked by the viewing position. Please note that it is the viewing position that appears to the left of the Source Window (NOT the execution position!).
The execution position and the viewing position refer to the same line when a source file is first loaded into the Source Window. You can then change the viewing position, if you wish.
The execution position and the viewing position appear different to distinguish them from the rest of the source code. The execution position line appears in the execution position highlight colors, while the viewing position appears as a broken-line frame, also called the cursor. Note that a line containing a breakpoint appears in the breakpoint highlight colors.
A combination of execution position, cursor and breakpoint (all of which are potentially active on the same line) appear accordingly.
When a program is active the viewing position is always visible in the Source Window. You can change the viewing position to move throughout the source file. Usually, whenever the execution position changes, the viewing position automatically follows suit. But you may easily change the viewing position without affecting the execution position.
To change the viewing position use any of the following possibilities:
In the upper-left corner of the Source Window, there are two text fields. These fields show the line number of the current viewing position and the address of the first machine instruction for that line. CrossView Pro updates the Line and Address values each time the viewing position changes.
You can change the viewing position
to the first executable line of a particular function with the e
command. For instance:
e main
will make the first executable line of main() the current viewing position and display it in the Source window. You may also use the stack depth as an argument, if you place it before the e:
1 e
This will change the viewing position to stack depth 1, that is, the line that called the current function.
FUNCTION: Change the viewing position.
COMMAND: stack e
e function
To change the viewing position to a specified address, you can use the ei command. This command is useful for viewing some code in the assembly window, without changing the program counter, since the execution position is not changed.
FUNCTION: Change the viewing position to address.
COMMAND: address ei
There may be times when you want to start or resume execution at a different line than the one marked by the current execution position.
Exercise caution when changing the execution position. Often each line of C source code compiles into several machine language instructions. Moving the program counter to a new address in the middle of a series of related assembly instructions is sometimes risky. Moreover, even though you change the program counter, registers and variables may not have the expected values if you bypass parts of the code.
In the Source Window you can
change the execution position to the viewing position with the menu entry
Run | Jump to Cursor. This menu entry is disabled in Source file window
mode to prevent problems by skidding pieces of C code which are required to
be executed. See also the g and gi commands
below.
When the program halts, you can change
the execution position with the g command in the Command Window.
The g command moves the execution position, but does not continue the program. To resume execution from your new execution position, use the C
command.
Although risky, the g command does have its uses, especially in conjunction with breakpoints to patch code. Refer to the Breakpoints and Assertions chapter for more information.
For example, to change the execution position from the current line, 54, to line 62, enter:
g 62
When you resume execution in this program, it is from line 62 instead of line 54.
FUNCTION: Change the execution position to a specified C source line
COMMAND: g line_number
You can also change the execution position to a specified address directly, although the same warnings apply. To do so, use the gi command. For instance:
0x800 gi
FUNCTION: Change the execution position to address.
COMMAND: address gi
Of course, moving the program counter (gi command) is even more potentially reckless than using the g command. Use both with caution especially when debugging a program which has instructions re-ordered due to optimizations.
To determine the address of a line of source, use the P command:
80 P 80:(0x1486): sum = sum + 1;
The hexadecimal number in parentheses is the instruction address for line 80.
FUNCTION: Print a source line and its instruction address.
COMMAND: line_number P
Each time you stop execution, the position of the program counter (PC) is visible in the source window. However, it may disappear from the window when scrolling through the source or when you loaded a new program. To find the program counter again:
Click on the Find PC button in the Source Window or select the Edit | Find PC menu item.
From the Command Window, use the L command.
The L command is shorthand for 0 e. It synchronizes the viewing and the execution positions, adjusting the viewing position if the two are different. The L command never affects the execution position. The L command is useful if you have changed your viewing position and do not remember where your execution position is.
FUNCTION: Synchronize viewing and execution position.
COMMAND: L
Using the mouse in the Source Window, you can direct the execution of your source programs. Among your options are:
To restart a program from its first instruction:
Click on the Restart program button in the Source Window.
or:
Type the R command
from the Command Window.
This is NOT a target system reset. Refer to the rst command for information about side effects that may be introduced due to a target system reset.
After restarting a program, you can stop execution only by a breakpoint, an assertion or a halt operation from the user.
FUNCTION: Reset program; run program.
COMMAND: R
To stop or continue execution:
Click on the Halt button in the Source Window to stop execution. Click on the Run/Continue button to resume execution.
Select the Run | Halt menu item to stop execution. Select the Run | Run
menu item to resume execution.
Use the C command or
function key F5 to resume execution.
When you halt the program, all the active windows update automatically to reflect the program's current status. For instance, if you have any expressions monitored in the Data Window, their current value appears.
Note that when you use any of the above methods to stop the program, CrossView Pro halts at the machine instruction that was on when interrupted. While this is a convenient way to stop the program, it is hardly an accurate one -- you may stop execution in the middle of a C source statement.
To stop a program at a precise line of C source code, set a breakpoint. For more about breakpoints see the Breakpoints and Assertions chapter.
When continuing, CrossView Pro resumes execution as if the program had never stopped.
FUNCTION: Continue execution from the current execution position.
COMMAND: C
When the program stops, you can continue execution, or you can step through it one line or instruction at a time. This is called single-step execution.
Single-stepping is a valuable tool for debugging your programs. The effect is to watch your programs run in stop motion. You can observe the values of variables, registers, and the stack at a precise point in a program's execution. You can catch many potential bugs by watching a program run line by line.
When you single step, CrossView Pro normally executes one line of your source and advances to the next sequential line of the program. If you single step to a line that contains a function call, however, you have two options: step into the function or step over the function call.
There are several methods you can use to single step into:
Click on the Step Into button in the Source Window or select the Run | Step Into menu item.
Press function key
F8 or type
the s command in the Command Window. You have the option of setting the number of lines you want to execute. For example, to execute 2 lines of the program,
type: 2 s.
FUNCTION: Step through a program one source line at a time.
COMMAND: number s
Stepping into a function means that CrossView Pro enters the function and executes its prologue machine instructions, halting at the first C statement. When you reach the end of the function, CrossView Pro brings you back to the line after the function call and continues with the flow of the program. The debugger changes the source code file displayed in the Source Window, if necessary.
If you accidentally step into a function that you meant
to step over, you can select the Run | Return from Function menu item to escape quickly.
For example, suppose you are at line 59 of a file, which contains a call to the function factorial():
main#59: table[ loopvar ] = factorial(loopvar)
By performing one Step Into action, you can step into the source code for factorial(). Your Execution and viewing position change to:
factorial#103: char locvar = 'x';
CrossView Pro shows you the current function and line number and the C source code for the current execution position.
To step over a statement or a function call:
Click on the Step Over button in the Source Window or select the Run | Step Over menu item.
Press function key
F10 or enter
the S command in the Command Window. You have the option of setting the number of lines you want the debugger to execute. For example, to execute three lines
of source, single stepping over functions, enter:
3 S.
FUNCTION: Single step, but treat function calls as single statements.
COMMAND: number S
Stepping over a function means that CrossView Pro treats function calls as a single statements and advances to the next line in the source. This is a useful operation if a function has already been debugged or if you do not want to take the time to step through a function line by line.
For example, suppose you reach line 59 in demo.c, which calls the function factorial(), as in the example above. If you give a Step Over command, the execution position moves to line 60 of the source code in the main() function immediately, without entering the source code for factorial(). CrossView Pro has executed the function call as a single statement.
If you try to step over a function that contains a breakpoint or that calls another function with a breakpoint, CrossView Pro halts at that breakpoint. Once execution stops, the step over command is complete. Therefore, if you resume execution by clicking on the Run button or with the C command, you do not regain control at the entrance to the function with the breakpoint. You can either single step through the rest of the function, or select the Run | Return from Function menu item to return to the line after the point of entry.
While single stepping through code at the source level is informative, you might need a lower level approach. CrossView Pro can step through a program at the assembly language instruction level.
While more time-consuming than a source level step-through, an instruction level step-through allows you to examine how your code has been compiled. As you advance through the assembly instructions, notice how CrossView Pro translates data addresses to variable names, and correlates branch addresses to points in the source code. This makes it much easier to follow the source at the instruction level.
The default step modes are:
Source lines Window: Source line step
Disassembly Window: Instruction step
Source and Disassembly Window: mode of previous window!
(assumes the step mode of the previous Source Window setting)
Mouse and menu actions:
To control this function from the Command
Window, use the Si and si commands. The Si and the si commands are analogous to the S and s commands, Si
will treat function calls (more precisely, jump to subroutine instructions) as single statements, while si
will enter the function.
FUNCTION: Single step at instruction level. Step into functions.
COMMAND: number si
FUNCTION: Single step at instruction level. Step over functions.
COMMAND: number Si
As an example of stepping through instruction level code, restart the program. Then select Run | Step Mode | Instruction step. Once it stops at the breakpoint you installed, advance execution one assembly language instruction at a time by using the Step Over and Step Into buttons. Or give the Si or si commands.
CrossView Pro will display disassembly of the next machine instruction that forms part of the C code in the Command Output Window:
main#47+0x4: disassembled instruction
Different types of targets, of course, have different assembly code, so debugging at the assembly level is hardware dependent.
Notice that a single C statement is usually compiled into several, sometimes many, machine instructions.
CrossView Pro supports
debugging on machine instruction level using the Intermixed or Assembly mode of the Source Window.
If you stop the program in a module without debug symbols, then an S or s command attempts to step to a module with symbols. CrossView Pro does this by searching the run-time stack for a return address in a module with symbols, then setting a temporary breakpoint there, and running. This process relies on two assumptions: that the stack layout is uniform, and that each function eventually returns. In the unlikely event that these assumptions are violated, the program may run away when you attempt to single step.
CrossView Pro can search for addresses and functions in the entire application and for line numbers, and strings in the current source file. A string search starts from the current viewing position and "wraps around" the end (or begin) of the current source file. The string search ends when a matching string is found or when it returns to the starting point.
There are several ways to find a function:
Using the mouse:
From the Command
Window, you can either specify e followed by the function name, or a stack position followed by e. For example:
e main Find the function main( ). 1 e Find the line that called the current function.
CrossView Pro searches through all the relevant source code files to find the one containing the body of the function. The part of the file containing the function appears in the Source Window.
CrossView Pro allows you to search for a particular string in the current source file. CrossView Pro searches the Source Window from the current viewing position. If it finds the string, it moves the viewing position to the corresponding line. This does not affect the execution position.
To find a string:
Open the Search String dialog box by clicking on the
Find Text String button, or selecting the
Edit | Search String... menu item. Click on the Case Sensitive check box to turn case sensitivity on or off.
You can also highlight a text fragment in the source code and click on the Find Next Text String button to find that fragment again.
In the Command Window, use the / or ? commands. The / command searches forwards and the ?
command searches backwards. For example, to find the string initval, enter:
/initval Search forward for the string "initval"
CrossView Pro's searches "wrap around" beyond the top or bottom of the file if necessary.
FUNCTION: Search forward for a string.
COMMAND: / string
FUNCTION: Search backward for a string.
COMMAND: ? string
If no string is supplied to the / or ? command, or if you hit carriage return, or press the function key F3 or select the Edit | Search Next String menu item, CrossView Pro searches again for the last string requested.
As mentioned earlier in the Changing the Viewing Position section, you can use the scroll bar to scroll through the source code or use the arrow keys or the + and - keys. To find a specific line, you can use one of several methods:
Select the Edit | Find Line... menu item to open the Find Line dialog box.
After you enter a line number (or select one from the history list) in this dialog box and click on the Find button, CrossView Pro will change the viewing position to the indicated line number. At the first use, the Find Line dialog box contains no line number, but on subsequent invocations it will show the line number you entered before.
Enter the line number on the command
line.