2

Consider the following loop:

28:  while (true) {
29:    chThdSleepMilliseconds(1);
30:    txbuffer[1] = num_zero + offset;
31:    spiExchange(&SPID1, 2, txbuffer, rxbuffer);
32:    // we want to examine rxbuffer at this point 
33:  }

I want to examine contents of rxbuffer in GDB after spiExchange function is executed. The following doesn't work:

(gdb) break main.c:32
(gdb) continue

GDB doesn't hit above breakpoint (probably as expected). If I put a breakpoint main.c:31 and then execute next, context is totally changed, so there is no rxbuffer at that point.

Currently I insert an unused variable and put a breakpoint at that point:

31:    spiExchange(&SPID1, 2, txbuffer, rxbuffer);
32:    int x; // we want to examine rxbuffer at this point 
33:  }

How can I put a breakpoint at that point without defining the temporary variable?

ceremcem
  • 1,386
  • 17
  • 35

2 Answers2

1

My current approach as a global¹ and extandable² solution is declaring a NOOP and placing breakpoint at that line:

5: #define NOOP ({;})
...
31:    spiExchange(&SPID1, 2, txbuffer, rxbuffer);
32:    NOOP; // put a breakpoint at this point
33:  }
...

(gdb) break main.c:32

The NOOP macro will be removed by optimization, so optimization shouln't be used in order this approach to work.

¹: "Global" means this approach is not tailored to be applied to a loop but can also be applied to after last line of an if or a switch statement.

²: "Extandable" means you may add code after line 31 that might change rxbuffer contents. With this approach you don't have to relocate your breakpoint in such cases.

Edit

If #define NOOP ({;}) does not work for you for some reason, you may change NOOP definition temporarily by whatever works for you, eg:

//#define NOOP ({;})
#define NOOP chThdYield()
#define NOOP someSleepFunction(0)
ceremcem
  • 1,386
  • 17
  • 35
0

Put your breakpoint at line 29. That's the statement that will be executed after line 31, and the breakpoint will break before line 29 is executed. The only disadvantage is that you will break one extra time, before line 31 is executed the first time.

Elliot Alderson
  • 31,192
  • 5
  • 29
  • 67