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.
| Question | |
|---|---|
| 1 | Why trace a program, when a debugger can stop it and look? |
| 2 | What does a trace source send, and what does it leave out? |
| 3 | How does a decoder rebuild the path of a program? |
| 4 | How does a trace show that an interrupt handler ran, and where? |
| 5 | What 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
| Word | Meaning |
|---|---|
| Program trace | A record of what a program did, made while it runs and without stopping it. Called a trace on this page. |
| Packet | One short message from a trace source. |
| Trace source | A block beside the processor that watches every instruction and sends packets. |
| Program listing | The program as a table: each address and the instruction at it. This page calls it the listing. |
| Program path | Every address that the program ran, in the order in which it ran them. Called the path on this page. |
| Trace decoder | A program on a laptop that rebuilds the path from the packets and the listing. Called the decoder on this page. |
| Plain instruction | An instruction after which the next address is always the one that follows. |
| Return of a handler | The 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.
| Packet | When the trace source sends it | What the decoder does |
|---|---|---|
| S and an address | At the start, and again from time to time | Starts to read the listing at that address |
| T | A branch was taken | Goes to the target of the branch |
| N | A branch was not taken | Goes on to the next address |
| I and an address | An interrupt broke in before the instruction at that address | Goes to the handler, which starts at 80 in this exercise |
| A and an address | A return went to that address | Goes to that address |
One Picture: From the Processor to the Path
- Processor
- Debug
- Memory
- Control / FSM
- Bus
- A program that must not stop
Some faults vanish when the processor halts. So the processor runs on, and nothing may slow it.
- 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.
- 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.
- 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.
- 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.
| Address | Instruction | Does the listing tell what comes next? |
|---|---|---|
| 40 | Branch to 4c if the work is done | No: 4c or 44 |
| 44 | Plain: look at the device | Yes: 48 |
| 48 | Jump to 40 | Yes: 40 |
| 4c | Stop | Yes: 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.
| Step | At address | The listing says | Packet used | Next address |
|---|---|---|---|---|
| 1 | 40 | Start here | S 40 | 40 |
| 2 | 40 | A branch: stuck | N | 44 |
| 3 | 44 | Plain | none | 48 |
| 4 | 48 | Jump to 40 | none | 40 |
| 5 | 40 | A branch: stuck | T | 4c |
| 6 | 4c | Stop | none | end |
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
Rule 1: A trace source does not stop the program.
It watches and sends. The processor runs as if it were not there.
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.
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.
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.
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.
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.
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=6Rule 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:
no start address: 5 packets cannot be placedFive 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 choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Send only what the listing cannot tell | Send the address of every instruction | Far less data: 6 packets for 11 instructions here. The decoder must have the exact listing. |
| A trace source in hardware | Print statements added to the program | The program and its timing are untouched. The chip pays in gates, and in a way out for the packets. |
| Repeat the start packet | Send it once, at the beginning | Decoding 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 section looks ahead. One trace source and six packets become a stream that fills a memory in a blink.
Time in the trace
A real trace source can add the time of each packet. The path then shows not only where, but when.
Data as well as the path
Some trace sources also report the address and value of loads and stores. That costs far more data.
Many sources
Each core has a trace source, and so may a bus. Their packets share one way out of the chip.
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
| Name | Meaning |
|---|---|
| packet S 40 | A start packet with the address 40. |
| 40 branch | The decoder is at address 40, and the listing says: a branch. |
| plain | An instruction after which the next address is always four higher. |
| path | Every address that the program ran, in order. |
| instructions | How many instructions are on the path. |
| packets | How many packets the decoder used. |
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=6Check Yourself
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.