C129 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

C12 · Lesson 9 · Debugging with breakpoints and stepping

Stopping the program to look inside it

When a program on a microcontroller misbehaves, there's often no screen to print to, and printing changes the timing anyway. A debugger stops the processor instead: at a breakpoint, or one line at a time, and shows the registers, the variables and memory exactly as they are at that instant. Firmware engineers spend much of their time here. The method that works is the scientific one: form a guess about what's wrong, predict what a variable should hold at some line, stop there, and look.

  • This program should print the average of eight ADC samples, about 510, but prints 8010. Press Run to see it.
  • Press Debug. Click the line number of sum += samples[i]; to set a breakpoint (it lights), then press Continue: the program stops there. Continue again and watch i and sum in the variables panel.
  • When i reaches 8, look at what samples[8] reads. The array has elements 0 to 7, so element 8 is whatever the linker put next in memory: here, marker. Fix the loop and run it again.

Predict

A breakpoint sits on a line inside a loop that runs 25 times, nested inside a loop that runs 4 times. How many times does Continue stop there?

Name it

What a debugger can do

  • Breakpoint: stop when execution reaches a line (or an address). Hardware debuggers on real chips have a few hardware breakpoints, often 2 to 8, because they compare the PC in silicon.
  • Step: run one line (stepping into a function call it makes) or one instruction, then stop.
  • Continue: run until the next breakpoint.
  • Watch: read variables, registers and memory at the stop. A watchpoint stops when a memory location is written, which catches the line that corrupts a variable.

On real hardware this works through a debug port (JTAG or SWD) connected to a probe; the chip's debug logic halts the CPU, and the probe reads its state.

Debugging well

  1. Reproduce the bug reliably.
  2. Guess where it starts, and predict what a variable should hold there.
  3. Stop there and compare. If the value is already wrong, move earlier; if right, later.
  4. Fix the cause, then rerun the failing case.

Common firmware bugs: an index one past the end of an array; a missing volatile; the wrong bit in a mask or shift; integer overflow; a variable changed by an interrupt halfway through being used; the stack growing into other data.

Printing

Printing (print, or text on a UART) is simple and works everywhere, but it costs time (a UART at 115,200 baud takes 87 µs per character), so it can hide or create timing bugs.

Worked example

samples is an array of uint16_t starting at 0x1000. What address does samples[8] read?

Show the solution
  1. Where element i sits. Each element is 2 bytes, so element i is at 0x1000+2i
  2. Element 8. 0x1000+16=0x1010
  3. What lives there. That's the first byte past the array, where marker lives.

Answer samples[8] reads 0x1010, the first byte past the array, where marker lives.

Worked example

Printing a 20-character line over a 115,200-baud UART: how long does the program spend, if it waits for each character?

Show the solution
  1. Bits per character. Each character is 10 bits on the wire: a start bit, 8 data bits and a stop bit.
  2. Time for the line. t=20×10115200s=1.74ms
  3. Compare with the loop. In a loop meant to run every millisecond, that print alone breaks the timing.

Answer the program spends 1.74 ms on the line.

Worked example

The debugger's memory view shows the bytes 34 12 00 00 at the address of a uint32_t. What value does it hold?

Show the solution
  1. Byte order. The processor is little-endian: the first byte is the least significant.
  2. Assemble the value. Reading 34 12 00 00 from the last byte back to the first gives 0x00001234=4660
  3. A classic mistake. Reading memory dumps byte by byte in the wrong order is one of the classic debugging mistakes.

Answer it holds 0x00001234, which is 4660.

Check

An array of 2-byte elements starts at 1040. What address does element 10 read, in hex?

Check

A breakpoint sits inside a loop of 8 passes, nested in a loop of 4 passes. How many times does it stop the program?

Check

A program prints 20 characters over a UART at 9600 baud, waiting for each. How long does it take?

Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.

Check

A loop meant to average 8 samples, each 512, runs one pass too many and adds the next value in memory, 60000, before dividing by 8. What does it compute?

Check

Put the steps of tracking down a bug in order.

  1. Fix the cause and rerun the failing case
  2. Guess where it starts and predict a variable's value there
  3. Move earlier or later depending on whether it was already wrong
  4. Stop the program there and compare the value with the prediction
  5. Make the bug happen reliably

Check

A variable is being corrupted somewhere, but you don't know which line writes to it. Which tool finds that line fastest?

Check

Fix the averaging program from the play so it prints the true average of its eight samples, 510.

C on the course's RISC-V board (10 MHz, no floating point). #include "board.h" for the registers; print(x) shows a number. Tab indents; press Esc and then Tab to move on.

Check

This program should print the largest of its readings, 21, but prints 13. Find the bug (print values along the way if that helps) and fix it.

MicroPython on the course's board: machine (Pin, PWM, ADC, DAC, UART, I2C, SPI, Timer, WDT) and time. Each call into the hardware takes about 40 µs. Tab indents; press Esc and then Tab to move on.

Check

An array of 8-byte elements starts at 4096.

(a)

What address does element 3 occupy?

(b)

How many bytes does the debugger's memory view show for one element?

Check

A program prints a 40-character line over a 9600-baud UART and waits for each character.

(a)

How long does it spend printing?

Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.

(b)

How many 10 MHz cycles is that?

Explain why

Why is stopping at a breakpoint often better than adding prints to find a bug in firmware?

Practice

Keep practicing

9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.