Search code examples
cgdbmsp430

GDB moves breakpoint to different line


I'm debugging C code running on a MSP430 microprocessor using GDB.

When I set a breakpoint on the line double average = sum / 10; using break 172, it confirms by responding Breakpoint 1 at 0xc01c: file main.c, line 172, but when I continue with c, the code runs until it hits Breakpoint 1, main () at main.c:184.

I wasn't having issues debugging until recently, so I tried reverting everything to the previous version and I still have this issue. I have also tried:

  • Turning my laptop off and on.
  • Unplugging and re-plugging every cable related to the microprocessor and its circuit.
  • Closing and re-opening all terminal windows.
  • Re-compiling and re-loading my C code into the microprocessor.
  • Print statements to help debugging aren't an option because the microprocessor can't hold #include <stdio.h>.
  • Clearing all breakpoints present before setting this one, but none are found.

The code looks something like:

void main(void)
{
  OtherMethod();

  while(1)
  {
    int sum = 0;

    for(int i = 0; i < 10; i++)
    {
          sum += i;
    }

    double average = sum / 10; // Line 172
  }  
}

void OtherMethod(void)
{    
  P1DIR |= LED1 + LED2; // Line 184
}

Other information that might be helpful is that I can successfully set a breakpoint on the line sum += i;.

Any ideas are appreciated.


Solution

  • If you compile with optimization, several "strange" things might happen, see your compiler's documentation. This might lead to statements being removed or re-arranged, and when debugging, surprising behaviour.

    To debug a program "by the line", compile without optimization.

    Or live with the surprises; it's a source of delight, in any case.