Sometimes, you want to have a breakpoint that only halts execution (breaks ) if a certain condition is true.
|
My user story: I have a GPS design that receives data from satellites. Sometimes, that radio receives "not guaranteed" data. The data isn't corrupt, but there are conditions (view blocked, ...) that render the data as unreliable.
I wrote a data parser for GPS data, that gets 30 messages per minute. In a debug session, I want the debugger to break when the GPS reports the issue. This info comes as one of many attributes of a payload string. So there is no interrupt, or unique code path, that indicates this situation. To let the debugger only break on this condition, I want to set a breakpoint in the part of the code that retrieves this data. And let it apply only when the payload indicates that data is not reliable (the break condition). |
My GPS data parser extracts all the fields of the payload it receives from a GPS module. That's its only job: cut payload into fields. It doesn't take decisions on the value of the fields.
But I want to make the debugger break when one of those fields (the field valid) has the value that flags that the payload is not reliable.
To achieve that, I put a breakpoint just after the code that parses the field:

Then, I put a condition: only break if the parsed value has that indicator that the payload can't be trusted.
There are several ways to edit a breakpoint in VSCode. The one I used, is to right-click on the breakpoint icon (the red dot), select Edit breakpoint.
Then I added the condition: ! gll.result.valid
When I debug the design, it 'll happily run the code, until there is an occurrence of this situation. Then the debugger stops, and I have access to all state and data of when this happened.
| Contrary to common hardware breakpoints, conditional breakpoints make your program run slower. Use them only when needed. |
There are other options, like checking this value in code, or raising a GPIO pin when this happens. Valid debug techniques, but they require code changes.
The conditional breakpoint is another tools in that toolkit.
