Trace: Rebuilding the Path of a Program

A trace follows a program without stopping it. It sends only what the program listing cannot tell, and a decoder rebuilds every step from that.

You Will Be Able To

  • Say what a trace source sends, and what it leaves out.
  • Rebuild the path of a program by hand from a listing and a few packets.
  • Find an interrupt in a trace, and say what is lost when the start packet is lost.
Lesson
S8.4
Time
26 min
Runs on
Your browser
Check
5 questions

A board misses one timer event in an hour. You halt it and find it in its polling loop, as always. Did the program ever visit the interrupt handler? A halted processor looks the same either way.

Before You Start

You need A Processor in One Picture: the PC, a jump and a branch.

You need Reset and Interrupts in One Picture: an interrupt forces a jump to a handler, and the handler returns.

You need What a Debugger Needs From the Chip: a debugger looks at a processor that has stopped.

Five questions this page answers
Question
1Why trace a program, when a debugger can stop it and look?
2What does a trace source send, and what does it leave out?
3How does a decoder rebuild the path of a program?
4How does a trace show that an interrupt handler ran, and where?
5What is lost when the start packet is lost?

The Idea: Send Only What the Listing Cannot Tell

A debugger shows where a program is now. It cannot show how the program got there. And the act of stopping changes the timing, so a fault that depends on timing may never appear. A program trace is the other tool: a record of the way a program went, made while it runs. This page calls it a trace.

The record is made by a trace source, a block beside the processor. It sees every instruction. It cannot send every address, because that would be a flood. So it leans on something the engineer already has: the program listing, the list of every address and its instruction.

After most instructions the listing says what comes next. The trace source stays silent for those. It speaks only where the listing cannot know. That is at a branch, and when an interrupt breaks in. It is also at a jump whose target is worked out as the program runs. One more thing it must send is a place to start.

On your laptop a trace decoder does the reverse. It walks the listing from the start address and takes a message of the trace source only where it is stuck. Without the trace you would know one point of the program. With it you know the whole way.

New Words

New words on this page
WordMeaning
Program traceA record of what a program did, made while it runs and without stopping it. Called a trace on this page.
PacketOne short message from a trace source.
Trace sourceA block beside the processor that watches every instruction and sends packets.
Program listingThe program as a table: each address and the instruction at it. This page calls it the listing.
Program pathEvery address that the program ran, in the order in which it ran them. Called the path on this page.
Trace decoderA program on a laptop that rebuilds the path from the packets and the listing. Called the decoder on this page.
Plain instructionAn instruction after which the next address is always the one that follows.
Return of a handlerThe last instruction of an interrupt handler. It jumps back to the instruction that the interrupt held up. Called a return on this page.

The packets on this page are an exercise format of our own, with five kinds.

The five packets of the exercise format
PacketWhen the trace source sends itWhat the decoder does
S and an addressAt the start, and again from time to timeStarts to read the listing at that address
TA branch was takenGoes to the target of the branch
NA branch was not takenGoes on to the next address
I and an addressAn interrupt broke in before the instruction at that addressGoes to the handler, which starts at 80 in this exercise
A and an addressA return went to that addressGoes to that address

One Picture: From the Processor to the Path

A Few Packets and a Listing Give Back the Whole PathStep through the picture. It starts with a program that must not be stopped, and ends with its path on your screen.
  • Processor
  • Debug
  • Memory
  • Control / FSM
  • Bus
A Few Packets and a Listing Give Back the Whole PathStep through the picture. It starts with a program that must not be stopped, and ends with its path on your screen. component Processor runs the program atfull speed component Trace source watches everyinstruction, sends afew packets component Program listing every address and itsinstruction component Trace decoder a program on yourlaptop component Program path every address theprogram ran, in order every instructiona few packetsthe programrebuilt
  1. component Processorruns the program at full speed
    • every instruction → Trace source
  2. component Trace sourcewatches every instruction, sends a few packets
    • a few packets → Trace decoder
  3. component Program listingevery address and its instruction
    • the program → Trace decoder
  4. component Trace decodera program on your laptop
    • rebuilt → Program path
  5. component Program pathevery address the program ran, in order
  1. A program that must not stop

    Some faults vanish when the processor halts. So the processor runs on, and nothing may slow it.

  2. A witness beside the processor

    A trace source watches every instruction as it runs. It cannot send every address: that would be more data than the program itself.

  3. What the listing already says

    After most instructions the next address is certain. It is written in the program listing. The trace source says nothing about those.

  4. A packet where the listing is silent

    At a branch the listing shows two ways. The trace source sends one packet: taken, or not taken. It also reports where to start, and every interrupt.

  5. The decoder walks the listing

    The decoder follows the listing from the start address. Where the listing is silent it takes the next packet. Out comes the path.

Read the diagram as text
  • Processor: runs the program at full speed
  • Trace source: watches every instruction, sends a few packets
  • Program listing: every address and its instruction
  • Trace decoder: a program on your laptop
  • Program path: every address the program ran, in order
  • Processor → Trace source: every instruction
  • Trace source → Trace decoder: a few packets
  • Program listing → Trace decoder: the program
  • Trace decoder → Program path: rebuilt

Instruction trace. This page is about the packets and the decoder. How the packets leave the chip is the next page.

The Smallest Example, Worked by Hand

Here is a polling loop of four instructions. No simulator yet. Addresses are in hexadecimal and four apart.

The listing
AddressInstructionDoes the listing tell what comes next?
40Branch to 4c if the work is doneNo: 4c or 44
44Plain: look at the deviceYes: 48
48Jump to 40Yes: 40
4cStopYes: nothing

The trace source sent three packets: S 40, then N, then T. Walk the listing and take a packet only when you are stuck.

Three packets, five instructions, by hand
StepAt addressThe listing saysPacket usedNext address
140Start hereS 4040
240A branch: stuckN44
344Plainnone48
448Jump to 40none40
540A branch: stuckT4c
64cStopnoneend

The path is 40, 44, 48, 40, 4c. Five instructions ran, and three packets were enough.

Now the interrupt. It can break in between any two instructions, and no listing can say where. So the trace source sends the packet I with the address of the instruction that was held up. The decoder checks for it before every instruction. The handler ends in a return, and its target is not in the listing either: the packet A carries it.

The Rules

  1. Rule 1: A trace source does not stop the program.

    It watches and sends. The processor runs as if it were not there.

  2. Rule 2: A trace source sends only what the listing cannot tell.

    Where to start, which way a branch went, where a return went, and where an interrupt broke in.

  3. Rule 3: The decoder needs the listing of the program that really ran.

    With another listing the packets still fit somewhere, and the path is false.

  4. Rule 4: The decoder takes a packet only when it is stuck.

    A plain instruction and a jump with its target in the listing use no packet.

  5. Rule 5: An interrupt is in the trace as a packet of its own.

    It names the instruction that was held up. So the trace shows that the handler ran, and where.

  6. Rule 6: Without a start address no packet can be placed.

    So a trace source sends a start packet again from time to time.

Sources for These Rules

Branches, jumps and the order in which a program runs: Harris and Harris, Digital Design and Computer Architecture, ARM edition, section 6.3.

Real trace units work on the same idea as rules 2 to 6. Arm calls its unit an Embedded Trace Macrocell, and RISC-V has a trace specification named E-Trace. Neither document is in our reference library. These are well-known facts and were not checked against their text.

That a trace macrocell generates instruction trace from a processor: Arm AMBA ATB Protocol Specification, IHI 0032 issue C, section 1.2.

The five packets, their letters and the handler at 80 are an exercise format of our own. No real trace unit uses this format.

Now Run It

The lab at the end of this page is a decoder. It holds the polling loop of the hand example and a handler of three instructions at 80. It also holds six packets: S 40, N, N, I 48, A 48, T.

Printed by the lab on Icarus. Addresses are in hexadecimal.
packet S 40: start at 40
40 branch: packet N, not taken, go on to 44
44 plain
48 jump to 40
40 branch: packet N, not taken, go on to 44
44 plain
packet I 48: interrupt before 48, go to the handler at 80
80 plain
84 plain
88 return: packet A 48, go to 48
48 jump to 40
40 branch: packet T, taken, go to 4c
4c stop
path: 40 44 48 40 44 80 84 88 48 40 4c
instructions=11 packets=6

Rule 4 at work, in the rows "44 plain" and "48 jump to 40". The decoder moved on without a packet.

Rule 5, in the row "packet I 48". This is the answer to the question at the top of the page. Yes, the handler ran: once, on the second pass through the loop, between the instructions at 44 and 48. The rows 80, 84 and 88 are its three instructions.

Rule 2, in the last row. Eleven instructions ran and six packets described them.

Rule 6 needs a second run. Take the same capture and lose its first packet:

The same lab with the packet S 40 removed.
no start address: 5 packets cannot be placed

Five good packets, and not one address. The decoder cannot even begin.

This lab leaves things out. No processor and no trace source are in it. The packets are typed into the test code, as a trace source would have sent them. The decoder is test code too. A real format packs its packets into a few bits each and adds the time of each event.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
Send only what the listing cannot tellSend the address of every instructionFar less data: 6 packets for 11 instructions here. The decoder must have the exact listing.
A trace source in hardwarePrint statements added to the programThe program and its timing are untouched. The chip pays in gates, and in a way out for the packets.
Repeat the start packetSend it once, at the beginningDecoding can begin in the middle of a capture. A few more packets are sent.

The Trap: The Request Wire That Went High

Back to the missed timer event. The first idea was cheap: watch the interrupt request wire of the timer on a logic analyser. The wire went high every time. So the handler must have run every time.

It had not. A request is a wire. Whether the processor takes the interrupt depends on its enable bits at that moment. Only the processor knows that it jumped, and only the packet I says so. In the capture of the missed event there was no packet I at all.

The second trap is the wrong listing. The firmware was built again after the capture. The decoder used the new listing with the old packets. Every packet fitted somewhere, and the path it printed never happened.

The third trap is the missing start. The capture kept the last few packets before the fault, and the start packet was not among them. The next page shows how that happens.

At Scale

This page11 instructions, 6 packets
One second of a fast corea billion instructions

This section looks ahead. One trace source and six packets become a stream that fills a memory in a blink.

  1. Time in the trace

    A real trace source can add the time of each packet. The path then shows not only where, but when.

  2. Data as well as the path

    Some trace sources also report the address and value of loads and stores. That costs far more data.

  3. Many sources

    Each core has a trace source, and so may a bus. Their packets share one way out of the chip.

  4. The way out

    Packets must be stored on the chip or sent through pins. The next page follows them.

Try It

Run the lab. It is a decoder: it holds a listing and six packets, and prints the path.

Delete the line "packet(I, 8'h48);" and the line after it. Did the handler run in that capture? How many instructions are on the path now?

Delete the first packet line, the one with S. Run again and read the one row that is left.

Names Printed by the Lab
NameMeaning
packet S 40A start packet with the address 40.
40 branchThe decoder is at address 40, and the listing says: a branch.
plainAn instruction after which the next address is always four higher.
pathEvery address that the program ran, in order.
instructionsHow many instructions are on the path.
packetsHow many packets the decoder used.
RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: packet S 40: start at 40 40 branch: packet N, not taken, go on to 44 44 plain 48 jump to 40 40 branch: packet N, not taken, go on to 44 44 plain packet I 48: interrupt before 48, go to the handler at 80 80 plain 84 plain 88 return: packet A 48, go to 48 48 jump to 40 40 branch: packet T, taken, go to 4c 4c stop path: 40 44 48 40 44 80 84 88 48 40 4c instructions=11 packets=6

Check Yourself

Predict the outputIn this run the listing is the same and the packets are S 40, N, N, T. What is the last row? Its form is: instructions=. packets=.
`timescale 1ns/1ns
module tb;
  // Kinds of instruction in the listing.
  localparam PLAIN = 0, BRANCH = 1, JUMP = 2, RETURN = 3, STOP = 4;
  // Kinds of packet. S: start here. T, N: a branch was taken, or not taken.
  // I: an interrupt was taken before this address. A: a jump went to this address.
  localparam S = 1, T = 2, N = 3, I = 4, A = 5;
  localparam logic [7:0] HANDLER = 8'h80;       // where an interrupt handler starts in this exercise

  int kind [0:63];
  logic [7:0] target [0:63];
  task automatic ins(input logic [7:0] at, input int k, input logic [7:0] to);
    kind[at/4] = k; target[at/4] = to;
  endtask

  int pk [0:15];
  logic [7:0] pv [0:15];
  int count = 0;
  task automatic packet(input int k, input logic [7:0] v);
    pk[count] = k; pv[count] = v; count++;
  endtask

  logic [7:0] pc, path [0:63];
  int at = 0, steps = 0;
  bit going = 1;
  initial begin
    // The program listing: what the decoder already has.
    ins(8'h40, 1, 8'h4c);
    ins(8'h44, 0, 8'h00);
    ins(8'h48, 2, 8'h40);
    ins(8'h4c, 4, 8'h00);
    ins(8'h80, 0, 8'h00);
    ins(8'h84, 0, 8'h00);
    ins(8'h88, 3, 8'h00);
    // The packets: what the trace source sent.
    packet(S, 8'h40);
    packet(N, 8'h00);
    packet(N, 8'h00);
    packet(T, 8'h00);

    if (count == 0 || pk[0] != S) begin
      $display("no start address: %0d packets cannot be placed", count);
      $finish;
    end
    pc = pv[0]; at = 1;
    $display("packet S %02h: start at %02h", pc, pc);
    while (going) begin
      if (at < count && pk[at] == I && pv[at] == pc) begin
        $display("packet I %02h: interrupt before %02h, go to the handler at %02h", pc, pc, HANDLER);
        at++; pc = HANDLER;
      end
      path[steps] = pc; steps++;
      case (kind[pc/4])
        PLAIN: begin $display("%02h plain", pc); pc = pc + 4; end
        JUMP: begin $display("%02h jump to %02h", pc, target[pc/4]); pc = target[pc/4]; end
        BRANCH: begin
          if (at >= count || (pk[at] != T && pk[at] != N)) going = 0;
          else if (pk[at] == T) begin $display("%02h branch: packet T, taken, go to %02h", pc, target[pc/4]); pc = target[pc/4]; end
          else begin $display("%02h branch: packet N, not taken, go on to %02h", pc, pc + 8'd4); pc = pc + 4; end
          if (going) at++;
        end
        RETURN: begin
          if (at >= count || pk[at] != A) going = 0;
          else begin
            $display("%02h return: packet A %02h, go to %02h", pc, pv[at], pv[at]); pc = pv[at];
            at++;
          end
        end
        default: begin $display("%02h stop", pc); going = 0; end
      endcase
    end
    if (kind[pc/4] != STOP) $display("%02h needs a packet and none fits: the path ends here", pc);
    $write("path:");
    for (int i = 0; i < steps; i++) $write(" %02h", path[i]);
    $write("\n");
    $display("instructions=%0d packets=%0d", steps, at);
    $finish;
  end
endmodule
ConceptWhy trace a program, when a debugger can stop it and look?
ConceptWhat does a trace source leave out?
ConceptHow does a decoder rebuild the path?
ConceptWhat is lost when the start packet of a capture is lost?

Common Mistakes

Wrong Take a high interrupt request wire as proof that the handler ran.

The processor may not have taken the interrupt.

Fix Look for the packet of the interrupt in the trace.

Wrong Decode a capture with the listing of a newer build.

The packets fit somewhere, and the path is false.

Fix Keep the listing of the program that ran with each capture.

Wrong Expect a packet for every instruction.

A trace source sends only what the listing cannot tell.

Fix Walk the listing, and take a packet only when stuck.

Remember

  • A trace records the way a program went, without stopping it.
  • A trace source sends only what the listing cannot tell. That is a start, the way of each branch, the target of a return, and each interrupt.
  • A decoder walks the listing and takes a packet only when it is stuck.
  • Without a start address, and without the right listing, a capture cannot be decoded.

Practice

Report a problem with this page