What a Debugger Needs from the Chip
A debugger looks like a window into the processor. It is a chain of four links, and every request waits for a reply from the far end.
You Will Be Able To
- Name the four things a debugger asks of a chip, and the blocks that carry each request.
- Say what each link of the debug chain does to a request.
- Explain why a halt request is not a halted processor, and what to wait for.
- Lesson
- S8.1
- Time
- 24 min
- Runs on
- Your browser
- Check
- 5 questions
Your firmware should print a letter when a switch is pressed. Today the terminal stays blank. You click Stop in the debugger, and a moment later it shows you a register. Your laptop has no wire to that register.
Before You Start
You need A Processor in One Picture: registers and the PC. An instruction that uses the bus takes several clock edges.
You need From Board to Chip: the blocks of our chip and its pins.
| Question | |
|---|---|
| 1 | What four things does a debugger need from a chip? |
| 2 | Which blocks carry a request from the laptop to the processor? |
| 3 | What does each link of the chain do to the request? |
| 4 | Why is a halt request not the same as a halted processor? |
| 5 | A halt request arrives in the middle of an instruction. When does the reply come? |
The Idea: Four Requests and a Chain
A debugger is a program on your laptop. It needs four things from the chip. It must stop the processor. It must look at registers and memory. It must change them. Then it must let the processor go on.
None of that state is on a pin. So the chip carries a block whose only job is to serve the debugger: the debug module. It sits beside the processor. It can ask the processor to stop, and it can reach the registers of a stopped processor.
The laptop reaches the debug module through a few pins, one bit at a time. So one click passes through a chain of five boxes. They are the debugger, a cable adapter, a collector of bits, the debug module and the processor. Each link hands the request to the next in a new form.
One thing follows from the chain, and it is the heart of this page. A request is not a result. The processor cannot stop in the middle of an instruction. It stops a little later, and only it knows when. Without that reply, the debugger would show you a register that is still changing.
New Words
| Word | Meaning |
|---|---|
| Debugger | A program on a laptop that stops a processor, shows its state, changes it and lets it run again. |
| Debug probe | A small box between the laptop and a few pins of the chip that are kept for the debugger. It turns commands into serial bits. Called the probe on this page. |
| Debug transport | A block inside the chip that collects serial bits from the debug pins into one request. |
| Debug module | A block inside the chip that serves the debugger: it asks the processor to stop and reaches its registers. |
| Hart | Short for hardware thread: one processor, seen as one stream of instructions. Our chip has one hart. |
| x5 | One register of the processor. The labs of this module work on it. |
| Halt request | A wire from the debug module to the hart. 1 means: please stop. |
| Halted | A wire from the hart to the debug module. 1 means: I have stopped. It is the reply to a halt request. |
| Resume request | A wire from the debug module to the hart. A pulse on it means: go on. |
| Debug chain | The five boxes in a row: debugger, debug probe, debug transport, debug module, hart. |
One Picture: The Debug Chain
- Control / FSM
- Pads
- Debug
- Processor
- No way in
The debugger knows which register you want to see. The register is a few flip-flops deep inside the chip. No pin of the package shows it.
- A block that serves the debugger
The chip carries a debug module beside the processor. It can ask the hart to stop, and it can reach the registers of a stopped hart.
- A few pins and a cable
Pins cost money, so the way in is serial: a few pins, one bit at a time. The debug transport collects the bits. A debug probe drives the pins for the laptop.
- The request goes inward
One click crosses four links. Its meaning stays the same. Only its form changes: a command, serial bits, one request, one wire.
- The reply comes outward
Only the hart knows when it has stopped. Its answer crosses the same four links in the other direction. Until it arrives, the debugger knows nothing.
Read the diagram as text
- Debugger: a program on your laptop
- Debug probe: a small box between the laptop and the debug pins
- Debug transport: inside the chip: collects serial bits into one request
- Debug module: inside the chip: serves the debugger
- Hart: the processor, as one stream of instructions
- Debugger → Debug probe: a command
- Debug probe → Debug transport: serial bits on a few pins
- Debug transport → Debug module: one request
- Debug module → Hart: halt request
- Hart → Debug module: halted
- Debug module → Debug transport: one reply
- Debug transport → Debug probe: serial bits
- Debug probe → Debugger: a result
The debug chain. The lab on this page has only the last two boxes: test code stands in for the first three.
The Smallest Example, Worked by Hand
Follow one click on Stop. Nothing is simulated yet. The first table shows the same request in each link.
| Link | What it receives | What it hands on |
|---|---|---|
| Debugger | Your click on Stop | A command for the probe |
| Probe | The command | Serial bits on the debug pins |
| Debug transport | The bits, one at a time | One whole request for the debug module |
| Debug module | The request | A 1 on the halt request wire |
| Hart | The halt request | Nothing yet: it is in the middle of an instruction |
Now the clock. Say the hart is in a load that reads a slow device. The load needs four clock edges, e1 to e4. The halt request arrives after e2.
| After edge | Halt request | Halted | What the hart is doing |
|---|---|---|---|
| e1 | 0 | 0 | The load, first edge |
| e2 | 0 | 0 | The load, second edge |
| e3 | 1 | 0 | The load, third edge. It has seen the request and goes on. |
| e4 | 1 | 1 | The load has finished. Now the hart stops. |
The request is 1 for two edges before the reply is 1. A debugger that reads the register after e3 reads it while the load is still filling it. After e4 the register is whole, and the reply says so.
The reply then goes back along the chain. It is a wire, then a status word, then serial bits, then a line on your screen.
The Rules
Rule 1: The debugger never touches state directly.
Every action is a request to the debug module: stop, look, change, go on.
Rule 2: A request crosses every link of the chain, and so does its reply.
A break in any link looks, on the laptop, like a chip that does not answer.
Rule 3: A link changes the form of a request, never its meaning.
A command, serial bits, one request, one wire.
Rule 4: A hart stops only between two instructions.
The instruction in progress finishes first, however long it takes.
Rule 5: A request is not a result.
The debugger waits for halted before it reads or changes anything.
Rule 6: Registers can be reached only while the hart is halted.
A resume request ends that, and the hart runs on from the next instruction.
How a halt lands, and how to run one instruction at a time, is the subject of Halt, Resume and Step. The debug pins have a page of their own.
Sources for These Rules
The names hart, debug module and debug transport, and rules 4 to 6, follow the RISC-V External Debug Support specification. That document is not in our reference library. These statements are well-known facts about it and were not checked against its text.
Registers, the PC and the fetch of an instruction: Harris and Harris, Digital Design and Computer Architecture, ARM edition, sections 6.2.2 and 7.3.1.
The four clock edges of the load, and the moment of the request, are numbers of our lab.
Now Run It
The lab at the end of this page has the last two boxes of the chain: a debug module and one hart. Test code plays the debugger. A row that starts with e shows the two wires after that clock edge.
read x5, hart running: reply=00000000
e1 halt request=0 halted=0
e2 halt request=0 halted=0
debugger: halt request sent
e3 halt request=1 halted=0
e4 halt request=1 halted=1
read status: reply=00000001
read x5: reply=00000041
read next pc: reply=00000004
e5 halt request=0 halted=1
write x5=00000042, then read x5: reply=00000042
debugger: resume request sent
e6 halt request=0 halted=0
read x5, hart running: reply=00000000Rule 5 at work, in rows e3 and e4. After e3 the halt request is 1 and halted is still 0. After e4 both are 1: the load has finished.
Rule 6, in the first row and the last. The debugger asks for x5 while the hart runs, and gets 00000000. That is not the content of x5. After the halt the same question returns 00000041.
Rule 1 at work, in the three "read" rows after e4. Status is 1, x5 is 41 and the next pc is 4. Then the debugger writes 42 into x5 and reads it back. Last, a resume request: after e6 halted is 0 again.
This lab leaves things out. No processor runs here: a model only says how many edges an instruction takes. The probe and the debug transport are missing, so test code drives the debug module directly. And the 00000000 for a running hart is a choice of this model. A real debug module answers such a read with an error flag.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| A debug module in hardware | A small debug program that the processor runs itself | It works when the program is broken, or when there is no program yet. It costs gates, pins and a door that must be locked. |
| A serial way in, on a few pins | A wide port with one pin for each bit | Pins are saved for the product. Each request takes many clock edges on the cable. |
| Stop between two instructions | Stop the clock of the processor at once | Registers and the PC agree, and no bus transfer is cut in half. The reply comes late by up to one instruction. |
The Trap: A Register Read Too Early
Back to the blank terminal. You click Stop and look at the register that should hold the switch. It shows the old value. So you decide that the switch was never read, and you spend the morning on the switch.
The read was fine. Your script had sent the halt request and read the register in the same breath. The load was still waiting for its slow device. The fix was one line: wait for halted, then read.
A second trap hides in rule 4. A team once stopped the clock of the processor to "halt" it. The processor froze in the middle of a bus transfer. The device at the other end waited for an end that never came.
The third trap is in what a halt does not do. It stops one hart. Timers still count, a serial port still sends its last byte, and a transfer by another block still writes memory. The chip you look at is not frozen. Only its processor is.
At Scale
This section looks ahead. One hart and one debug module become many, and the way in becomes something to guard.
Many harts, one debug module
The debug module holds a selector for the hart it talks to. A request to stop goes to the chosen hart, or to a group of them.
Replies at different times
Each hart stops at the end of its own instruction. Two harts asked together still answer at two moments.
A door to lock
Whoever holds the probe can read memory. The Debug Module on the Bus shows the lock.
When stopping hides the fault
Some faults vanish when the processor stops. Trace watches a program without stopping it.
Try It
Run the lab. Read it as two columns: what the debugger asked for, and what came back.
In the program, change the cost of the first instruction from 4 to 7. On which edge does halted become 1 now?
Change after_edge(2) to after_edge(4). The first instruction has already finished. Which instruction does the hart finish before it stops?
Names Printed by the Lab
| Name | Meaning |
|---|---|
| e1, e2, ... | Rising edges of the clock, counted from the release of reset. |
| halt request | The wire from the debug module to the hart. 1 means: please stop. |
| halted | The wire from the hart to the debug module. 1 means: I have stopped. |
| reply | The word that the debug module hands back for a read. |
| status | A word of the debug module. Its bit 0 is a copy of halted. |
| x5 | One register of the processor. |
| next pc | The address of the instruction that the hart will run next. |
read x5, hart running: reply=00000000 e1 halt request=0 halted=0 e2 halt request=0 halted=0 debugger: halt request sent e3 halt request=1 halted=0 e4 halt request=1 halted=1 read status: reply=00000001 read x5: reply=00000041 read next pc: reply=00000004 e5 halt request=0 halted=1 write x5=00000042, then read x5: reply=00000042 debugger: resume request sent e6 halt request=0 halted=0 read x5, hart running: reply=00000000Read included file: soc-s81-debug.sv
// Teaching model of a hart's debug boundary. A retiring instruction advances
// the model PC by four. The driver supplies its result; there is no ISA core.
module debug_boundary (
input logic clk, reset_n, retire, halt_req, resume_req, step_req,
input logic [31:0] result, input logic increment,
input logic trigger_en,
input logic [31:0] trigger_pc,
input logic reg_write, select_pc,
input logic [31:0] write_data,
output logic halted,
output logic [31:0] read_data
);
logic [31:0] next_pc, x5;
logic stepping;
assign read_data = halted ? (select_pc ? next_pc : x5) : 32'b0;
always_ff @(posedge clk) begin
if (!reset_n) begin
next_pc <= 0;
x5 <= 0;
halted <= 0;
stepping <= 0;
end else if (halted) begin
if (reg_write && !select_pc)
x5 <= write_data;
if (resume_req || step_req) begin
halted <= 0;
stepping <= step_req;
end
end else if (trigger_en && next_pc == trigger_pc) begin
halted <= 1;
end else if (retire) begin
next_pc <= next_pc + 4;
x5 <= increment ? x5 + 1 : result;
halted <= halt_req || stepping;
stepping <= 0;
end
end
endmodule
// The teaching module uses word selectors, independent of the SoC address map.
// Its bench driver has already performed transport framing.
module debug_register_port (
input logic halted, write_en,
input logic [1:0] selector,
input logic [31:0] hart_data,
output logic select_pc, write_x5,
output logic [31:0] reply
);
always_comb begin
select_pc = selector == 2;
write_x5 = halted && write_en && selector == 1;
case (selector)
0: reply = {31'b0, halted};
1, 2: reply = hart_data;
default: reply = 0;
endcase
end
endmodule
Check Yourself
Common Mistakes
Wrong Read a register as soon as the halt request is sent.
The instruction in progress may still be changing it.
Fix Wait for halted, then read.
Wrong Stop the clock of the processor to halt it.
A bus transfer can be cut in half.
Fix Let the hart stop itself between two instructions.
Wrong Treat a halted hart as a frozen chip.
Timers, serial ports and other bus managers keep running.
Fix Ask what else can change the state you are looking at.
Remember
- A debugger needs four things: stop, look, change, go on. All four are requests to the debug module.
- A request crosses the debug chain: debugger, probe, debug transport, debug module, hart. The reply crosses it back.
- A request is not a result. The hart stops between two instructions and then says halted.
- A halted hart is not a frozen chip.