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?

A Hang, Seen Three WaysStep through three views of one failing run. The trace shows the loop, the listing names it, and the waves show the bus.
  • External
  • Debug
  • Bus
  • Processor
  • Debug
A Hang, Seen Three WaysStep through three views of one failing run. The trace shows the loop, the listing names it, and the waves show the bus. external Result block reason=cycle_limit debug Retire trace a loop of 3addresses:800002a0,800002a4,800002a8 external Listing which C line thoseaddresses are bus Waves the address and datathe load really used processor The cause a poll of the wrongregister where did it stop?pc to sourcewhich cycle?meantdid
  1. external Result blockreason=cycle_limit
    • where did it stop? → Retire trace
  2. debug Retire tracea loop of 3 addresses: 800002a0, 800002a4, 800002a8
    • pc to source → Listing
    • which cycle? → Waves
  3. external Listingwhich C line those addresses are
    • meant → The cause
  4. bus Wavesthe address and data the load really used
    • did → The cause
  5. processor The causea poll of the wrong register
  1. The result

    The run reached its cycle limit: something waited for ever.

  2. The trace

    Its last records repeat 3 program counters. That is the loop.

  3. The listing

    The listing turns those addresses into one line of C: a while loop that polls a register.

  4. The waves

    The data bus shows the address the load used and the value it got back.

  5. 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.

Five questions this page answers
Question
1Why does a chip test need its own way of debugging?
2What do the trace, the listing and the waves each tell you?
3How does a hang look in the trace?
4How do you find the instruction behind a trap?
5How 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

New words on this page
WordMeaning
Retire traceThe bench's record of every finished instruction: cycle, pc, instruction and its effect.
ListingThe disassembly apcc writes beside the image: each address with its instruction.
HangA run that never reaches a status word, usually a loop waiting for an event that never comes.
Trap recordThe trace line of an instruction that trapped, with its mcause.
Stack damageA store through the stack pointer that lands outside the stack, on data or code.
First wrong transferThe 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.

Reading the tail of a trace
QuestionWhere to lookIn this run
Which loop?The repeating pcs in the traceThree instructions
Which C line?The listing at those pcsA while loop around a load and a test
Waiting for what?The load's address in the traceA register of the SPI block
Why does it never end?The value the load returns, and the register it should beThe bit never clears there

The method; the values are in the lab below.

The Rules

  1. Rule 1: Start at the symptom, walk backwards.

    The run stopped at the symptom; it went wrong earlier.

  2. 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.

  3. Rule 3: A trap names its instruction.

    mcause says why, the trap record's pc says which instruction, the listing says which C line.

  4. 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.

  5. 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

HANG_C: it waits for the SPI frame to end.
#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:

The tail of the retire trace (cycle, pc, instruction, effect).
 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.

main in the listing (riscv64-unknown-elf-gcc 14.2.0).
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.

The Hang on the Core's Buses
01234567891011121314151617181920212223i_haddr800002b0800002a0800002a4800002a8800002ac800002b0800002a0800002a4800002a8800002ac800002b0800002a0800002a4800002a8800002ac800002b0800002a0800002a4i_htrans10d_haddr00000001fffffff910005000100050080000000000000001fffffff910005000100050080000000000000001fffffff910005000100050080000000000000001fffffff910005000d_htrans00100010001000d_hrdata00000000000000010000000000000001000000000000000100000000fetchaddressdata
Scroll horizontally ↔

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.

RMW_C: it clears the timer's interrupt by read-modify-write.
#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;
}
Around the trap in the retire trace.
  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] = 80000084

The 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.

main around the trap (riscv64-unknown-elf-gcc 14.2.0).
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,1

a5 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

ap_soc~12 blocks
Phone-class SoChundreds of blocks

This section looks ahead. One core, a few thousand cycles.

  1. 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.

  2. Many cores

    Each core has its own trace; debugging starts by finding which core went wrong first.

  3. 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.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: a loop of 3 instructions: 800002a0 800002a4 800002a8

Check Yourself

Predict the outputThe slice now reads the twelve records before the trap of RMW_C. What is the last line it prints?
// The program counters of the last 12 retired instructions of a run that never ended.
// A hang is a short loop: find the smallest period with which the list repeats.
module tb;
  localparam int N = 12;
  logic [31:0] pc [N];
  int period = 0;
  initial begin
    pc[0] = 32'h800002b0;
    pc[1] = 32'h800002b4;
    pc[2] = 32'h800002b8;
    pc[3] = 32'h80000278;
    pc[4] = 32'h8000027c;
    pc[5] = 32'h800002ac;
    pc[6] = 32'h800002b0;
    pc[7] = 32'h800002b4;
    pc[8] = 32'h800002b8;
    pc[9] = 32'h80000278;
    pc[10] = 32'h8000027c;
    pc[11] = 32'h80000280;
    for (int p = 1; p <= N / 2 && period == 0; p++) begin
      bit same;
      same = 1;
      for (int i = p; i < N; i++) if (pc[i] != pc[i - p]) same = 0;
      if (same) period = p;
    end
    if (period == 0) $display("no loop in these %0d records", N);
    else begin
      $display("a loop of %0d instructions:", period);
      for (int i = N - period; i < N; i++) $display("  %h", pc[i]);
    end
  end
endmodule
ConceptWhy does the symptom rarely point at the cause?
ConceptWhat does the listing add to the retire trace?
ConceptA trap record shows mcause 5. What happened?
ConceptA test hangs because the bench never sends a byte. Whose side was wrong?

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.

Practice

Report a problem with this page