Volume C1 · Basic concepts · TAOCP Section 1.4.3.2
Trace routines
Draft. This chapter is written and checked by the tools of the project, and a person has not reviewed it yet. Read it with care.
Read it next to Section 1.4.3.2 of the book. The nodes and the exercises of the section.
Contents
Trace routines
Overview
A trace routine is a debugging tool. It takes control of a program, lets each instruction of the program run on the real machine, and writes down, before each step, the location of the instruction and the state of the machine. The programmer reads the list and sees where the program went wrong. The method is different from the method of a simulator. A simulator carries out the instruction by its own code. A trace routine copies the instruction to a cell, runs it, and goes on, so the speed of one step is the speed of the instruction plus a fixed overhead.
Only two kinds of instruction cannot be run as they are. A jump would take the program counter away from the trace routine, and the instruction STJ would store the jump register of the trace routine in place of the jump register of the traced program. The routine of this chapter turns each jump into a jump to a stub that takes control back, and it turns STJ into a store of a simulated jump register. The routine uses one data register of the machine, the register A, and it does not change the comparison indicator or the other registers.
The ideas
The step is: fetch, record, run, advance. The routine has a cell for the location of the next instruction. It reads the instruction at this location, records the pair, runs the instruction in a cell of its own, and adds one to the location.
Registers belong to the traced program. The registers, the comparison indicator and the overflow toggle must have the values that the traced program expects when its instruction runs. The routine therefore keeps its own data in memory cells, and it loads and stores the register A around each instruction. It uses no other register and no instruction that sets the comparison indicator.
A jump is turned into a call of the routine. The address and the index of the jump are replaced by the address of a stub of the routine, and the original address and index are copied to an instruction that computes the target. If the condition of the jump holds, the machine goes to the stub with the registers unchanged, and the stub finds the target. If it does not hold, the machine goes on to the next instruction of the routine, and the step ends as for a plain instruction.
The jump register is simulated. A taken jump sets the real jump register to a place in the routine, and the traced program would find this value wrong. The routine keeps the value that the traced program should see in a cell, and it changes this cell at each taken jump that is not JSJ. The instruction STJ is run as a store of that cell.
The routine does not trace itself. The traced program must not store in the cells of the routine, and it must not use the device on which the record is written.
Definitions, results and proofs
Definition (trace routine, TAOCP Section 1.4.3.2). Let be a program of a machine and let be the states of the machine when it runs alone, with the location of the instruction that runs at the state . A trace routine is a program that, when it is given the starting location of , runs so that goes through the same states, and that writes down, for each , the location and the instruction at , and some of the state, before the instruction runs.
Node: 1.4.3.2-def-trace · Lean: not yet stated
Explanation. The states that goes through are the states of alone, so the trace is a transparent copy: it does not change what computes. The extra record is the reason to use it. Some parts of the state cannot be exactly the same. The jump register and the registers of the routine are in the state of the real machine, and the routine has to hold the jump register of the traced program in a cell. The next theorem says which parts are held.
Example. Take the program of two instructions ENT1 2 and DEC1 1 that starts at location and then stops at . Run alone, it has the locations , , and the states with rI1 , , . A trace routine for it records the pairs and , and then lets the program reach with rI1 .
Proof that the example agrees with the definition. The program alone does ENT1 2 at , which gives rI1 , then DEC1 1 at , which gives rI1 . The trace routine runs these two instructions in the same order with the registers of the program. So the sequence of states is the same, and the list of pairs has one entry for each instruction that ran.
Theorem (transparency of the trace routine, TAOCP Section 1.4.3.2). Let a program not store in any cell of the trace routine, let it not use the instructions JBUS, JRED or IOC with the device of the log, and let it not read the cell of the jump register of the routine. Then the routine below runs so that, before each instruction of , the register A, the registers I1 to I6 and X, the comparison indicator and the overflow toggle of the machine have the values that they have when runs alone, and the simulated jump register has the value of the real jump register of alone.
Node: 1.4.3.2-thm-transparent · Lean: not yet stated
Explanation. The theorem lists what is preserved, and the restrictions state the condition. The routine stores the register A in the cell AREG after each plain instruction and loads it before. It does not touch the other registers. The comparison indicator and the overflow toggle are changed only by the instructions of and by an arithmetic instruction of the routine that overflows, which does not occur, since the numbers in the routine are the small codes of instructions and locations.
Example. Let contain DEC1 followed by J1P. When the DEC1 runs, rI1 is the register of . The routine has not changed it, since all its own work is done in the cells and in the register A. The instruction J1P then sees this value of rI1, because the routine runs the jump with the original condition on the real rI1.
Proof. The proof follows the two kinds of instruction. A plain instruction runs in the cell INST after LDA AREG, and after it STA AREG. Since the routine between two instructions uses only the register A and the instructions LDA, STA, INCA, DECA, JAZ, JAN and JAP, which change no other register and not the comparison indicator, the registers I1 to I6, X, the indicator and the toggle are as left them. The register A is saved in AREG by STA AREG after the instruction of and loaded before the next. So the state before the next instruction is the state that the instruction of produced. A jump instruction runs in INST with the address and index of the stub JUMP. The condition of the jump tests the registers, and A has been loaded before the test. So the jump is taken exactly when it would be taken in . When it is taken, no register has changed, and the stub reads the target by ENTA from the copy of the original address and index, which uses the same index register, and the value in the register A is the target. The cell AREG still holds the register A of , because the jump does not change it. If it is not taken, the instruction takes its place as a plain instruction, and STA AREG stores the unchanged A. For the jump register, the stub sets the cell JREG to the location of the jump plus when the instruction is not JSJ. This is the value that the machine sets in a taken jump, so the cell holds the real value of the jump register of alone. The instruction STJ is run as a store of the register A in the field of its own, and the register A holds JREG, which is the value that the store STJ would write.
Algorithms and programs
Program (a trace routine in MIX, TAOCP Section 1.4.3.2). The routine has the entry TRACE. A program calls it by JMP TRACE, and the routine starts to trace the instruction after the call. It writes, for each instruction, two words in a log: the location in the field of the first word, and the instruction in the second. It stops when the traced program jumps to the location LEAVE, and then the traced program continues with the instruction after that jump, outside the trace.
Node: 1.4.3.2-prog-trace · Lean: not yet stated
The control. The first part of each step is the same for every instruction. The location comes in rA. The routine stores it, writes it with the word at that location in the log, which has a pointer LOGP, and stores the word in INST. The writing into the log and the reading of the instruction use two instructions whose address field is changed by the routine, PUT1 STA * and FETCH LDA *.
CYCLE STA PREG(0:2) the location of this step
STA FETCH(0:2)
LDA LOGP(0:2) the place in the log
STA PUT1(0:2)
INCA 1
STA PUT2(0:2)
INCA 1
STA LOGP(0:2)
LDA PREG
PUT1 STA * write the location
FETCH LDA * read the instruction
STA INST
PUT2 STA * write the instruction
The second part decides the kind of instruction by its code. The routine loads the code, takes away and goes to ISSTJ when the result is zero. It takes away more and goes to PLAIN when the result is negative, which is the case of the codes below . It takes away more and goes to PLAIN when the result is positive, which is the case of the codes above . The codes to fall through to the part that makes a stub.
LDA INST(5:5)
DECA 32
JAZ ISSTJ
DECA 7
JAN PLAIN
DECA 8
JAP PLAIN
LDA INST(0:3) a jump: keep the address and index
STA INST1(0:3)
LDA 8F(0:3) put the address of JUMP in its place
STA INST(0:3)
PLAIN LDA AREG
INST NOP * the instruction runs here
STA AREG
INCP LDA PREG(0:2) next location
INCA 1
JMP CYCLE
The word at the label 8H is the instruction JSJ JUMP, and the fields of it are the address of the stub and the index zero. The stub is at JUMP.
JUMP LDA 8B(4:5) was it a JSJ?
SUB INST(4:5)
JAZ *+4
LDA PREG(0:2) no: JREG gets the location plus one
INCA 1
STA JREG(0:2)
INST1 ENTA * rA gets the address of the jump
DECA LEAVE
JAZ LEAVE1
INCA LEAVE
JMP CYCLE
The instruction STJ is run as a store of the simulated jump register. The routine changes its code to the code of STA, loads the register A from JREG, runs the copy in the cell INSTS, and does not store A, so that AREG keeps its value.
ISSTJ ENTA 24
STA INST(5:5)
LDA INST
STA INSTS
LDA JREG(0:2)
INSTS NOP *
JMP INCP
At LEAVE1 the routine sets the exit to the location after the jump to LEAVE, loads the register A and jumps out.
The traced program. The test program has the instructions in the table at the locations to and a subroutine at . The call at is the call of the trace routine. The subroutine adds to the register A and returns through the cell EXIT by the linkage of Section 1.4.1.
| Loc | Instruction | Meaning |
|---|---|---|
| 2000 | JMP TRACE | start the trace |
| 2001 | ENT1 2 | rI1 |
| 2002 | JMP 2010 | call the subroutine |
| 2003 | DEC1 1 | rI1 rI1 |
| 2004 | J1P 2002 | repeat while rI1 |
| 2005 | JMP LEAVE | end of the trace |
| 2006 | HLT | stop |
| 2010 | STJ 2012 | the entrance of the subroutine |
| 2011 | INCA 7 | rA rA |
| 2012 | JMP * | the exit, set by STJ |
A run. The register A is when the trace starts, and rI3 and rX hold and . The run was made in a simulator of MIX. It reaches the HLT at at time with rA , rI1 , rI3 and rX , which are the values of the program run alone. The log has pairs, and its locations are , in the same order as the locations of the run alone. The word that STJ stored in the cell is the jump to , so the first return is right. The next table shows the start of each step. The column Clock is the clock at the entrance of CYCLE for that step, and rJ is the simulated jump register at that time.
| Step | Loc | Instruction | Clock | rA | rI1 | rJ |
|---|---|---|---|---|---|---|
| 1 | 2001 | ENT1 2 | 9 | 5 | 0 | 2001 |
| 2 | 2002 | JMP 2010 | 50 | 5 | 2 | 2001 |
| 3 | 2010 | STJ 2012 | 108 | 5 | 2 | 2003 |
| 4 | 2011 | INCA 7 | 152 | 5 | 2 | 2003 |
| 5 | 2012 | JMP * | 193 | 12 | 2 | 2003 |
| 6 | 2003 | DEC1 1 | 251 | 12 | 2 | 2013 |
| 7 | 2004 | J1P 2002 | 292 | 12 | 1 | 2013 |
| 8 | 2002 | JMP 2010 | 350 | 12 | 1 | 2005 |
| 9 | 2010 | STJ 2012 | 408 | 12 | 1 | 2003 |
| 10 | 2011 | INCA 7 | 452 | 12 | 1 | 2003 |
| 11 | 2012 | JMP * | 493 | 19 | 1 | 2003 |
| 12 | 2003 | DEC1 1 | 551 | 19 | 1 | 2013 |
| 13 | 2004 | J1P 2002 | 592 | 19 | 0 | 2013 |
| 14 | 2005 | JMP LEAVE | 641 | 19 | 0 | 2013 |
Correctness. The correctness is the theorem of transparency, together with these two points. First, the log. At each entrance of CYCLE, rA is the location of the next instruction: the entry sets it from the call, INCP sets it to the location plus one, and the stub sets it to the target. The routine stores the location, reads the cell at it, and stores both in the log at the place LOGP, which it advances by two. The reading uses an instruction whose address is changed by the routine, and the change is made before the instruction runs. So the log has the location and the instruction for each step, in the order of the run. Second, the end. The target of a jump is compared with LEAVE by DECA and JAZ, which do not change the comparison indicator. When it is equal, the exit is set to the location of the jump plus one, which is the instruction after JMP LEAVE, and the register A is loaded from AREG, so that the traced program continues with its own A.
Time. Let be the time of the instruction that runs. The common start of CYCLE, up to the test for STJ, takes u. The time of a step is then
| Kind of step | Time in u |
|---|---|
| a plain instruction with code below | |
| a plain instruction with code above | |
| STJ | |
| a jump that is not taken | |
| a jump that is taken and is not JSJ | |
| a taken JSJ | |
| a taken jump to LEAVE, with the leaving |
The total for the run is u for the call and the entry, then the times of the steps: the sum , and u for the HLT. The total is u. The traced instructions take u when they run alone, so the average overhead is about u for each step, and the factor of slowing is .
Derivation of the entries of the table. The u are STA (), STA (), LDA (), STA (), INCA (), STA (), INCA (), STA (), LDA (), the store of the location (), the read (), STA INST (), the store of the instruction (), LDA (), DECA () and JAZ (). A plain instruction with a code below then runs DECA () and JAN (), which is u. The part from PLAIN takes LDA AREG (), the instruction (), STA AREG (), and INCP, which is LDA (), INCA () and JMP (). The sum is . For the codes above two more lines run, DECA and JAP, so the sum is . A jump runs four tests ( u) and the four lines that make the stub ( u), and then PLAIN as above: . A taken jump costs u for the jump itself in place of , and then the stub: LDA (), SUB (), JAZ (), LDA, INCA and STA of JREG (), ENTA (), DECA (), JAZ (), INCA () and JMP CYCLE (), which is u. So the step takes u. A JSJ skips the u of the JREG lines, which gives . A taken jump to LEAVE has JAZ taken in place of INCA and JMP CYCLE, and the lines of LEAVE1 (LDA, INCA, STA, LDA, JMP, which are u), so it takes . The STJ step takes , then ENTA (), STA (), LDA (), STA (), LDA (), the store of the instruction (), the jump to INCP () and INCP (), which is .
The exercises
The table has the exercises of Section 1.4.3.2. The book marks no exercise of this section as recommended in the text that C1 reads. The kind is the class of spec 06.3 that C1 proposes.
| Exercise | Rating | Markers | Kind | Lean | Solution |
|---|---|---|---|---|---|
| 1.4.3.2-1 | 22 | K6 | not yet stated | X1, 1.4.3.2-1 | |
| 1.4.3.2-2 | 26 | K6 | not yet stated | X1, 1.4.3.2-2 | |
| 1.4.3.2-3 | 10 | informal | not yet stated | X1, 1.4.3.2-3 | |
| 1.4.3.2-4 | 25 | K6 | not yet stated | X1, 1.4.3.2-4 | |
| 1.4.3.2-5 | 28 | K6 | not yet stated | X1, 1.4.3.2-5 | |
| 1.4.3.2-6 | 40 | K6 | not yet stated | X1, 1.4.3.2-6 | |
| 1.4.3.2-7 | 25 | informal | not yet stated | X1, 1.4.3.2-7 |
Exercise 1 asks for a trace routine that restores the jump register when it leaves. The routine above has the right value in the cell JREG. Only a taken jump sets the real register, so the exit must make a jump that gives it this value, and the exercise is to find how. Exercise 2 asks for a record on tape in a fixed format, and the log of this chapter is the first step: a record of fixed size for each step. Exercise 3 asks why a tape is better than the printer for the record, and the reason is the speed of the output: the program is slowed by the output more than by anything else. Exercises 4, 5 and 6 are about the routine that traces itself. The restriction of the theorem, that the traced program must not store in the cells of the routine, is exactly what a tracing routine breaks, since the inner routine runs and changes its own cells. Exercise 7 asks for a jump trace. The routine above shows the base: the stub JUMP is the only place that sees a taken jump, so a jump trace logs there and does not log in CYCLE.
Lean
The Lean code of tamnd/taocp-lean at main has the model of MIX in TAOCP/MIX/ and the program logic in TAOCP/MIX/Logic/. It has no file for Section 1.4.3.2. The definition of a trace is about the sequence of states of the machine, which the Lean model has as the iterate of its step function. A trace routine would be a program with a statement that its run maps to the same sequence, and the logic of TAOCP/MIX/Logic/Hoare.lean would carry the statement for one step as a triple whose frame says that the registers I1 to I6, X and the indicator are unchanged. The frame lemmas of TAOCP/MIX/Logic/Frame.lean have this kind of statement for one instruction. The theorem of transparency is not stated, and the Node lines name no declaration.
Notes and history
A trace on a printer or on a tape was a usual way to find an error before interactive debuggers were common. The design rules are given in the book as a short list: the routine should be short, so that it can coexist with a large program, because the time is spent on the output anyway; it should use as few registers as possible, because the less it uses the less it must restore; and the traced program should have only the restrictions that cannot be removed.
The jump register is the hard part of a trace on this machine, because it is a register that the machine changes as a side effect of a jump, and a trace routine is itself made of jumps. The solution of this chapter, to simulate the register in a cell and to run STJ as a store of that cell, is the same one that every simulator has to use.
Section 1.4.3.2 describes a program that runs another program one instruction at a time and records what happens. It is the simplest useful simulator: it does not model the machine, it lets the machine do the work and keeps control between instructions. The only instructions that the trace routine cannot run as they are, the jumps and the store of the jump register, need special care. This chapter defines a trace routine, then builds one of its own design in MIX that logs the location and the word of each step, proves that it is transparent for the traced program, runs it on a program with a subroutine call, and counts its time.
The source of this page. Something is wrong? Report it.