Debugging a Failing C Test
A C test fails at night and someone must say why by morning. Use the three views a chip bench gives you: the retire trace, the listing and the waves. Go from a symptom to its first wrong transfer. Learn the three shapes most failures take: a hang, a trap, and a stack that ran over.
You Will Be Able To
- Find the loop of a hang in the retire trace and name its C line from the listing.
- Read a trap record: its cause, its pc, and the instruction behind it.
- Recognise stack damage from a trace that writes where no variable should be written.
- Go back from the symptom to the first wrong transfer, and say which side it belongs to.
- Lesson
- V4.9
- Time
- 30 min
- Runs on
- Your browser
- Check
- 5 questions
The report says "cycle limit" and nothing else. The test has no fail code, the console has one line, and the chip team asks whether it is their bug. You have a trace, a listing and the waves. Where do you start?
- External
- Debug
- Bus
- Processor
- Debug
- The result
The run reached its cycle limit: something waited for ever.
- The trace
Its last records repeat 3 program counters. That is the loop.
- The listing
The listing turns those addresses into one line of C: a while loop that polls a register.
- The waves
The data bus shows the address the load used and the value it got back.
- Meant against did
The listing says what the code meant; the waves say what the chip did. Where they differ is the bug.
Read the diagram as text
- Result block: reason=cycle_limit
- Retire trace: a loop of 3 addresses: 800002a0, 800002a4, 800002a8
- Listing: which C line those addresses are
- Waves: the address and data the load really used
- The cause: a poll of the wrong register
- Result block → Retire trace: where did it stop?
- Retire trace → Listing: pc to source
- Retire trace → Waves: which cycle?
- Listing → The cause: meant
- Waves → The cause: did
The hang of HANG_C on ap_soc v1.5 (riscv64-unknown-elf-gcc 14.2.0); every value is from the run below.
Before You Start
Read The First Run for the three views and Traps for mcause and mepc.
| Question | |
|---|---|
| 1 | Why does a chip test need its own way of debugging? |
| 2 | What do the trace, the listing and the waves each tell you? |
| 3 | How does a hang look in the trace? |
| 4 | How do you find the instruction behind a trap? |
| 5 | How do you tell a chip bug from a test bug? |
The Idea: From Symptom Back to Cause
A failing C test gives a symptom: a cycle limit, a fail code, a trap's 0xFFFF. The symptom is where the run stopped, not where it went wrong. Debugging is walking back from it to the first transfer that was not what it should have been.
Three views make the walk possible. The retire trace lists every instruction the core finished, with its loads and stores. The listing maps each address to an instruction and a line of C. The waves show the buses cycle by cycle. The trace tells you where, the listing tells you what was meant, the waves tell you what the chip did.
Most failures take one of three shapes. A hang is a loop that waits for something that never happens. A trap is an access the chip refused, or a jump to nowhere. Stack damage is a store that lands where a variable lives.
New Words
| Word | Meaning |
|---|---|
| Retire trace | The bench's record of every finished instruction: cycle, pc, instruction and its effect. |
| Listing | The disassembly apcc writes beside the image: each address with its instruction. |
| Hang | A run that never reaches a status word, usually a loop waiting for an event that never comes. |
| Trap record | The trace line of an instruction that trapped, with its mcause. |
| Stack damage | A store through the stack pointer that lands outside the stack, on data or code. |
| First wrong transfer | The earliest load or store whose address or data differs from what the test meant. |
The Smallest Example: A Loop by Hand
The last records of a run that reached its cycle limit read, by program counter: a0, a4, a8, a0, a4, a8, a0. A hang is a short loop, so look for the smallest period that repeats.
| Question | Where to look | In this run |
|---|---|---|
| Which loop? | The repeating pcs in the trace | Three instructions |
| Which C line? | The listing at those pcs | A while loop around a load and a test |
| Waiting for what? | The load's address in the trace | A register of the SPI block |
| Why does it never end? | The value the load returns, and the register it should be | The bit never clears there |
The method; the values are in the lab below.
The Rules
Rule 1: Start at the symptom, walk backwards.
The run stopped at the symptom; it went wrong earlier.
Rule 2: A hang is a loop: find it, then find what it waits for.
The repeating pcs give the loop; its load gives the register.
Rule 3: A trap names its instruction.
mcause says why, the trap record's pc says which instruction, the listing says which C line.
Rule 4: Compare what was meant with what happened.
The listing and the source say what was meant; the trace and waves say what the chip did.
Rule 5: Ask which side was wrong.
Firmware, bench or chip: the first wrong transfer tells you whose promise broke.
Sources for These Rules
The trace format and the result block: tools/soc/ap_soc/bench/soc_tb/README.md. The listing: tools/soc/fwkit/README.md (apcc writes .lst).
mcause 5 is a load access fault, mcause 2 an illegal instruction: the RISC-V privileged specification (library key RV-PRIV), machine cause register.
The kit's trap entry and its fail code 0xFFFF for an unexpected cause: tools/soc/fwkit/trap.S and irq.c.
Now Run It: A Hang and a Trap
#include "drivers/spi.h"
#include "tb.h"
/* Wait until the SPI frame is over. */
static void spi_wait(void)
{
while (reg_read32(SPI_IRQ_ENABLE) & SPI_STATUS_BUSY_MASK) { }
}
int main(void)
{
reg_write32(SPI_IRQ_ENABLE, SPI_IRQ_ENABLE_DONE_MASK);
reg_write32(SPI_DATA, 0x9f); /* read the flash identity */
spi_wait();
unsigned id = FIELD_GET(SPI_DATA_RECEIVED, reg_read32(SPI_DATA));
tb_puts("flash ");
tb_put_hex(id);
tb_putc('\n');
return id != 0xc0ffee;
}The run ended with reason=cycle_limit after 6008 cycles, and no fail code: rule 1, start at that symptom. The last records of the retire trace:
5994 | 800002a0 | lw a4, 0(a5) | x14 = 00000001, read mem[10005008] = 00000001
5996 | 800002a4 | andi a4, a4, 1 | x14 = 00000001
5997 | 800002a8 | bne a4, zero, 0x800002a0 |
6001 | 800002a0 | lw a4, 0(a5) | x14 = 00000001, read mem[10005008] = 00000001
6003 | 800002a4 | andi a4, a4, 1 | x14 = 00000001
6004 | 800002a8 | bne a4, zero, 0x800002a0 |Three program counters repeat: rule 2. The listing says what they are.
8000027c <main>:
8000027c: ff010113 addi sp,sp,-16
80000280: 100056b7 lui a3,0x10005
80000284: 00112623 sw ra,12(sp)
80000288: 00812423 sw s0,8(sp)
8000028c: 00100713 li a4,1
80000290: 00e6a423 sw a4,8(a3)
80000294: 09f00713 li a4,159
80000298: 00868793 addi a5,a3,8
8000029c: 00e6a023 sw a4,0(a3)
800002a0: 0007a703 lw a4,0(a5)
800002a4: 00177713 andi a4,a4,1
800002a8: fe071ce3 bnez a4,800002a0 <main+0x24>
800002ac: 0006a403 lw s0,0(a3)The loop is lw a4, 0(a5), andi a4, a4, 1, bnez. The base a5 was made by addi a5, a3, 8: offset 8 of the SPI block, which is IRQ_ENABLE, not STATUS at offset 4. The firmware set bit 0 of IRQ_ENABLE itself, so the bit it polls never clears. The waves below confirm it on the data bus.
One column a cycle of clk_sys from cycle 5868. Recorded by a probe beside soc_tb (Icarus Verilog version 12.0) on the same image. htrans 10 is a transfer; the instruction port fetches two words past the branch before it turns back.
In the waves the load's address phase carries 0x1000_5008, and the read data is 1. Rule 4 at work: meant STATUS, used IRQ_ENABLE.
#include "drivers/timer.h"
#include "tb.h"
static void timer_ack(void)
{
reg_write32(TIMER_IRQ_CLEAR, reg_read32(TIMER_IRQ_CLEAR) | TIMER_IRQ_CLEAR_MATCH_MASK);
}
int main(void)
{
timer_start(15, 2, 3);
for (volatile int i = 0; i < 40; i++) { }
timer_ack();
tb_puts("ack done\n");
return 0;
} 854 | 80000278 | lw a5, 12(sp) | x15 = 00000028, read mem[8000ffec] = 00000028
856 | 8000027c | bge a4, a5, 0x800002ac |
858 | 80000280 | lui a5, 0x10002 | x15 = 10002000
861 | 80000284 | lw a4, 20(a5) | trap, mcause = 5
865 | 80000090 | addi sp, sp, -64 | x2 = 8000ffa0
866 | 80000094 | sw ra, 0(sp) | mem[8000ffa0] = 80000084The trap record at pc 80000284 has mcause 5, a load access fault: rule 3. The listing at that address is the load of the read-modify-write.
80000278: 00c12783 lw a5,12(sp)
8000027c: 02f75863 bge a4,a5,800002ac <main+0x74>
80000280: 100027b7 lui a5,0x10002
80000284: 0147a703 lw a4,20(a5)
80000288: 80000537 lui a0,0x80000
8000028c: 2d050513 addi a0,a0,720
80000290: 00176713 ori a4,a4,1a5 holds 0x1000_2000 and the offset is 20: TIMER_IRQ_CLEAR, which can only be written. A read of it answers ERROR on this chip, the core traps, and the kit's handler ends the test with 0xFFFF. The cure is a plain write of the mask, the way write-only registers are meant to be used.
Why It Is Built This Way
A debugger on a board shows one moment at a time and changes the timing when it stops the core. A simulation keeps everything: every retired instruction, every bus cycle, and the listing of the exact image that ran. You can walk backwards as far as you need, which no board allows.
The price is volume. A trace of a long run is large, and waves of a whole chip larger still. The bench caps the trace and the waves to a window, so you choose where to look, usually just before the symptom.
The Trap: Debugging the Symptom
A test hangs in a poll of RX_READY. The first fix is a time limit, and the test now fails cleanly with a code. That is good practice, and it is not a fix: the byte still never comes. The cause is earlier, in a hand-shake, a pin mux or the bench's rows.
The same holds for stack damage. A test that runs its stack down over its variables fails much later, often in a trap that looks unrelated. The first wrong transfer is the store that landed on a variable, thousands of cycles before the trap.
Stack damage has a signature in the trace. The stack pointer falls by one frame at each call, and stores go to addresses just above it. The symbol table gives the end of .bss (__bss_end). The first store through sp below that address is the first wrong transfer. The practice problems are three recorded failures: a hang, a trap and a stack that ran over.
At Scale
This section looks ahead. One core, a few thousand cycles.
Long runs
Tests of millions of cycles keep a trace only near the end. Some keep a ring buffer that the bench dumps on a failure.
Many cores
Each core has its own trace; debugging starts by finding which core went wrong first.
Silicon
On silicon the same method uses the debug module and on-chip trace, with less to see.
Try It
Run the slice: it finds the loop at the end of the hang's trace.
Add a record that breaks the pattern at the start. Does it still find the loop?
The predict question below feeds it the twelve records before the trap.
a loop of 3 instructions: 800002a0 800002a4 800002a8Check Yourself
Common Mistakes
Wrong Fix the symptom with a time limit and stop there.
The test now fails cleanly, but the cause is still there.
Fix Walk back to the first wrong transfer.
Wrong Read a register that can only be written, to change one bit.
On this chip the read answers ERROR and the core traps.
Fix Write write-only registers directly, with the mask.
Wrong Debug from the console alone.
It says what the test chose to print, not what the chip did.
Fix Use the trace, the listing and the waves together.
Remember
- Start at the symptom and walk back to the first wrong transfer.
- A hang is a loop: here 3 pcs, a poll of the wrong register.
- A trap names its instruction: mcause, pc, and the listing.
- Trace, listing and waves together say what was meant and what the chip did.