Debugging: From a Failure to a Cause
Walk back from the first wrong output to the first wrong signal. Read the log before the waves, find the earliest difference, and cut the search in half by stimulus and by time.
You Will Be Able To
- Start from the log: the first error, its time and its values.
- Walk back from an output to the signal that first went wrong, one step at a time.
- Find the earliest difference between what happened and what the specification says.
- Cut a search in half: shorter stimulus, earlier time.
- Lesson
- B4.4
- Time
- 24 min
- Runs on
- Your browser
- Check
- 5 questions
Four bytes went out and three came back right. The last one, c3, arrived as 00. Somewhere in a few hundred clocks one signal went wrong first, and every later error just followed it.
- Result
- I/O
- Storage
- Control / FSM
- Driver
- Data
- Start at the log
The first wrong output: byte 3 arrived as 00 instead of c3.
- Back to the wire
The receiver decoded what tx carried. The data bits on tx were all 0.
- Back to the register
tx shows frame[0], so the wrong bits were in frame.
- Back to the clock
frame was filled one clock after start, when data had already changed to 00.
Read the diagram as text
- Log: byte 3 wrong: got 00, want c3
- tx wire: data bits all 0
- frame register: holds 200, should hold 386
- The clock that filled it: one clock after start
- data input: already 00 by then
- tx wire → Log: byte 3 wrong: decoded by the receiver
- frame register → tx wire: shifted out
- The clock that filled it → frame register: filled
- data input → The clock that filled it: read too late
An idea drawing of the walk in the lab below. The hex values are from the recorded run.
Before You Start
Read How a Simulator Thinks and Regressions, Seeds and the Life of a Bug first. The transmitter is the one from The Verification Plan.
| Question | |
|---|---|
| 1 | Why read the log before the waveform? |
| 2 | What does it mean to walk back from an output? |
| 3 | What is the earliest difference, and why look for it? |
| 4 | How do you cut a search in half? |
| 5 | When is a bug found, and when is it only described? |
The Idea: Every Error Has a First Wrong Signal
A failure in a log is the end of a chain. Some signal went wrong first. Then the signals it drives went wrong, and so on, until something reached the bench's checks. Debugging means walking that chain backwards, from the error you see to the signal that started it.
The log comes first, because it is fast to read and says where to look. Which check failed, at what time, with what values? Then the waveform, but only around that time and only for the signals on the path. Ask of each signal: is this what the specification says it should be, right now? The first one that is not is the earliest difference.
When the search is too wide, cut it in half. Make the stimulus shorter until the failure barely survives. Then look at half the time, then half again. Each cut halves what is left to read.
New Words
| Word | Meaning |
|---|---|
| Symptom | What the bench sees: a wrong value, a missing answer, a timeout. |
| Root cause | The line of code, or the missing line, that made the first wrong signal. |
| Earliest difference | The first moment a signal differs from what the specification says it should be. |
| Walk back | Going from a wrong signal to the signals that drive it, one step at a time. |
| Bisect | Cutting a search in half, again and again: half the stimulus, or half the time. |
The Smallest Example: The Path, Worked by Hand
A frame is ten bits: a start bit 0, eight data bits low bit first, a stop bit 1. As a register, frame = {1, data, 0}. For data = c3, that is the ten bits 1 11000011 0, which is 386 in hex. Work out what frame would hold if data were 00.
| data | frame in bits | frame in hex |
|---|---|---|
| c3 | 1 11000011 0 | 386 |
| 00 | 1 00000000 0 | 200 |
So a frame of 200 means the transmitter sent the byte 00 with a correct start and stop bit. The question becomes: why did frame get data = 00 instead of c3?
The Rules
Rule 1: Read the log first.
The first error, its time and its values tell you where and when to look.
Rule 2: Walk back one step at a time.
From the wrong output, to the signal that drives it, to the signal that drives that.
Rule 3: Find the earliest difference.
Compare each signal with the specification, not with your guess. The first one that differs is where the bug acts.
Rule 4: Bisect when the search is wide.
Shorten the stimulus until the failure barely survives; halve the time window each step.
Rule 5: A cause explains every symptom.
Before you fix, check the cause explains why the other bytes passed too.
Sources for These Rules
Debugging from the first failing check backwards is common practice; the walk and the rules are stated in this track's own words.
Reading what a flip-flop sampled at an edge: see the previous lesson and IEEE 1800-2023, clause 4.
The Lab: Byte 3 Arrived as 00
The lab sends four bytes. For the last one, the bench tidies data back to 00 straight after the start clock, which the specification allows. It also prints one row per clock around that start:
e126 start=0 data=21 busy=0 frame=3ff tx=1
e127 start=1 data=c3 busy=0 frame=3ff tx=1
e128 start=0 data=00 busy=1 frame=3ff tx=1
e129 start=0 data=00 busy=1 frame=200 tx=0
e130 start=0 data=00 busy=1 frame=200 tx=0
e131 start=0 data=00 busy=1 frame=200 tx=0
e132 start=0 data=00 busy=1 frame=300 tx=0
byte 0: 48 ok
byte 1: 69 ok
byte 2: 21 ok
byte 3: got 00, want c3Rule 1 at work: the log says byte 3 is wrong, and the other three are right. The rows show the clocks around the last start, so the walk can begin there.
- Cycle 0
Clock 126: the transmitter is idle; frame holds all ones.
- Cycle 1
Clock 127: start is 1 with data = c3. The specification says data is read in this clock.
- Cycle 2
Clock 128: busy is 1 and the bench has tidied data to 00. frame still holds 3ff: it was not filled at start.
- Cycle 3
Clock 129: frame becomes 200, which is a stop bit, eight zeros and a start bit. The first wrong signal.
Recorded rows, sampled at each rising edge (Icarus Verilog version 12.0). The correct transmitter holds frame = 386 at clock 128.
Rule 3 at work. At clock 127, start is 1 and data is c3, the clock the specification says data is read. At clock 128 frame should hold 386, and it still holds 3ff. That is the earliest difference. At clock 129 frame fills with 200, read from data after the bench changed it.
Now the code. The transmitter does not fill frame at start. It sets a flag and fills frame on the next clock.
else if (!busy) begin
if (start) begin load <= 1; bits <= 10; ticks <= 0; busy <= 1; end
end else begin
if (load) begin frame <= {1'b1, data, 1'b0}; load <= 0; end // one clock late
...Rule 5 at work: does this explain the passing bytes too? Yes. For bytes 0 to 2 the bench held data steady after start, so reading it one clock late gave the same value. Only the tidy bench exposed it, and only on the last byte. The Verification Plan flagged that gap in the plan.
Why Walk Back to the Earliest Difference
Why walk back instead of reading the design from the top? Because a design has thousands of signals and the failure touches a handful. Walking back from the symptom only ever visits signals on the path, so the search stays small however big the design.
Why insist on the earliest difference? Because every later difference is a consequence. Fixing a consequence, such as patching the receiver to accept the byte, hides the bug and leaves the cause in place.
Traps
The first trap is debugging in the waveform first. A full waveform of a long run is a wall of signals, and the eye drifts to whatever looks odd. The log narrows it to one time and a few signals. Open the waves second.
The second trap is comparing with your guess instead of the specification. You expect frame to change at clock 129, it does, and you move on. The specification says it should already hold the byte at clock 128. Write the expected value down from the specification before you look.
The third trap is stopping at the first plausible story. "The bench changed data too early" sounds right, and blames the bench. But the specification says data is read in the start clock, so the bench is allowed to change it next. Check the cause against the specification before you assign it.
Where This Goes Next
Debugging is learned by doing. These pages give you more cases to walk back.
| The idea from this page | Where it is taught |
|---|---|
| An unknown that became a zero | Debug Case: The X That Became Zero |
| A race between a monitor and the design | Debug Case: A Racing Monitor |
| Debugging a failing assertion | Debugging a Failing Assertion |
| Reading a formal counterexample | Reading a Counterexample |
| The toolbox around a design | The Toolbox and the Map of This Site |
Try It
Run it: four bytes, the last with data tidied to 00 straight after start, and rows around the last start.
Fix uart_tx so frame is filled in the start clock itself. Predict the last line.
Keep the bug, and make the bench hold data at c3 for two more clocks after start. Does byte 3 still fail?
e126 start=0 data=21 busy=0 frame=3ff tx=1 e127 start=1 data=c3 busy=0 frame=3ff tx=1 e128 start=0 data=00 busy=1 frame=3ff tx=1 e129 start=0 data=00 busy=1 frame=200 tx=0 e130 start=0 data=00 busy=1 frame=200 tx=0 e131 start=0 data=00 busy=1 frame=200 tx=0 e132 start=0 data=00 busy=1 frame=300 tx=0 byte 0: 48 ok byte 1: 69 ok byte 2: 21 ok byte 3: got 00, want c3Check Yourself
Common Mistakes
Wrong Open the full waveform first.
Thousands of signals hide the few on the path.
Fix Read the log first; open the waves only around the first error.
Wrong Fix the first difference you notice.
It may be a consequence; the cause stays and returns.
Fix Walk back to the earliest difference and fix that.
Wrong Compare signals with what you expect.
Your expectation may share the bug.
Fix Compare with the specification, written down before you look.
Remember
- Every failure has a first wrong signal; debugging walks back to it.
- Read the log first, then the waves around the first error, for the signals on the path.
- Compare each signal with the specification; the earliest difference is where the bug acts.
- Bisect stimulus and time when the search is wide; a true cause explains the passes too.