C H A P T E R 6 |
Debugging |
OpenBoot provides debugging tools that include a Forth language decompiler, a machine language disassembler, register display commands, a symbolic debugger, breakpoint commands, a Forth source-level debugger, a high-level language patching facility, and exception tracing. This chapter describes the capabilities specified by IEEE Standard 1275-1994 .
The built-in Forth language decompiler can be used to recreate the source code for any previously-defined Forth word. The command:
displays a listing of the source for old-name (without the source comments, of course).
A companion to see is (see) which is used to decompile the Forth word whose execution token is taken from the stack. For example:
(see) produces a listing in a format identical to see .
The preceding listing shows that:
see itself is composed only of Forth source words that were compiled as external or as headers with fcode-debug? set to true.
(see) is a defer word. (see) also contains words that were compiled as headerless and are, consequently, displayed as hex addresses surrounded by parentheses.
Decompiling a word with (see) produces a listing identical to that produced by see .
For words implemented in Forth assembler language, see displays a Forth assembler listing. For example, decompiling dup displays:
ok see dup code dup f0008c98 sub %g7, 8, %g7 f0008c9c stx %g4, [%g0 + %g7] f0008ca0 ld [%g5], %l0 f0008ca4 jmp %l0, %g2, %g0 f0008ca8 add %g5, 4, %g5 |
The built-in disassembler translates the contents of memory into equivalent assembly language.
TABLE 6-1 lists commands that disassemble memory into equivalent opcodes.
Continues disassembling where the last disassembly left off. |
||
dis begins to disassemble the data content of any desired location. The system pauses when:
Any key is pressed while disassembly is taking place.
The disassembler output fills the display screen.
Disassembly can then be stopped or the +dis command can be used to continue disassembling at the location where the last disassembly stopped.
Memory addresses are normally shown in hexadecimal. However, if a symbol table is present, memory addresses are displayed symbolically whenever possible.
You can enter the user interface from the middle of an executing program as a result of a program crash, a user abort, or an encountered breakpoint. (Breakpoints are discussed in Breakpoints .) In all these cases, the user interface automatically saves all the CPU data register values in a buffer area. These values can then be inspected or altered for debugging purposes.
TABLE 6-2 lists the SPARC register commands.
Changes the value stored in any of the above registers.
|
||
.pstate | ||
.ver | ||
.ccr | ||
.trap-registers |
The values of all of these registers are saved and can be altered with to . After the values have been inspected and/or modified, program execution can be continued with the go command. The saved (and possibly modified) register values are copied back into the CPU, and execution resumes at the location specified by the saved program counter.
If you change %pc with to , you should also change %npc . (It is easier to use set-pc , which changes both registers automatically.)
On SPARC V9 systems, if N is the current window, N-1 specifies the window for the caller, N-2 specifies the callers's caller, etc.
The user interface provides a breakpoint capability to assist in the development and debugging of stand-alone programs. (Programs that run over the operating system generally do not use this OpenBoot feature, but use other debuggers designed to run with the operating system.) The breakpoint feature lets you stop the program under test at desired points. After program execution has stopped, registers or memory can be inspected or changed, and new breakpoints can be set or cleared. You can resume program execution with the go command.
TABLE 6-4 lists the breakpoint commands that control and monitor program execution.
To debug a program using breakpoints, use the following procedure.
1. Load the test program into memory.
2. See Chapter 5 for more information. The register values are initialized automatically.
3. (Optional) Disassemble the downloaded program to verify a properly-loaded file.
4. Begin single-stepping the test program using the step command.
5. You can also set a breakpoint, then execute (for example, using the commands addr +bp and go ) or perform other variations.
The Forth source-level Debugger allows single-stepping and tracing of Forth programs. Each step represents the execution of one Forth word.
The debugger commands are shown in TABLE 6-5 .
Every Forth word is defined as a series of one or more words that could be called "component" words. While debugging a specified word, the debugger displays information about the contents of the stack while executing each of the word's "component" words. Immediately before executing each component word, the debugger displays the contents of the stack and the name of the component word that is about to be executed.
In trace mode, that component word is then executed, and the process continues with the next component word.
In step mode (the default), the user controls the debugger's execution behavior. Before the execution of each component word, the user is prompted for one of the keystrokes specified in TABLE 6-5 .
OpenBoot provides the ability to change the definition of a previously compiled Forth word using high-level Forth language. While the changes will typically be made in the appropriate source code, the patch facility provides a means of quickly correcting errors uncovered during debugging.
patch reads the input stream for the following information:
The name of the new code to be inserted.
The name of the old code to be replaced.
The name of the word containing the old code.
For example, consider the following example in which the word test is replaced with the number 555 :
ok : patch-me test 0 do i . cr loop ; ok patch 555 test patch-me ok see patch-me : patch-me h# 555 0 do i . cr loop ; |
When using patch , some care must be taken to select the right word to replace. This is especially true if the word you are replacing is used several times within the target word and the occurrence of the word that you want to replace is not the first occurrence within the target word. In such a case, some subterfuge is required.
ok : patch-me2 dup dup dup ( This third dup should be drop) ; ok : xx dup ; ok patch xx dup patch-me2 ok patch xx dup patch-me2 ok patch drop dup patch-me2 ok see patch-me2 : patch-me2 xx xx drop ; |
Another use for patch is the case where the word to be patched contains some functionality that needs to be completely discarded. In this case, the word exit should be patched over the first word whose functionality is to be eliminated. For example, consider a word whose definition is:
In this example, the functionality of bad is incorrect and the functionality of unneeded should be discarded. A first attempt to patch foo might be:
on the expectation that the use of exit in the word right would prevent the execution of unneeded . Unfortunately, exit terminates the execution of the word which contains it, in this case right . The correct way to patch foo is:
(patch) is similar to patch except that (patch) obtains its arguments from the stack. The stack diagram for (patch) is:
( new-n1 num1? old-n2 num2? xt -- )
For example, consider the following example in which we reverse the affect of our first patch example by replacing the number 555 with test :
ok see patch-me : patch-me h# 555 0 do i . cr loop ; ok ['] test false 555 true ['] patch-me (patch) ok see patch-me : patch-me test 0 do i . cr loop ; |
The ftrace command shows the sequence of Forth words that were being executed at the time of the last exception. An example of ftrace follows.
In this example, test2 calls test1 , which tries to store a value to an unaligned address. This results in the exception: Memory address not aligned .
The first line of ftrace output shows the last command that caused the exception to occur. The next lines show locations from which the subsequent commands were being called.
The last few lines are usually the same in any ftrace output, because that is the calling sequence in effect when the Forth interpreter interprets a word from the input stream.
Copyright © 2002, Sun Microsystems, Inc. All rights reserved.