1
votes

I am doing remote debugging with gdb and gdbserver on an ARM9 platform. Both these programs are compiled on an Ubuntu system with gdbserver compiled using a cross-compiler.

In general, the program under debug is able to stop at the set breakpoints. However, single-stepping using next or step works most of the time except at some locations of the code. In particular, the one that I find consistently giving me problems are switch blocks. For eg.

1    inchar = getchar();
2
3    switch (inchar)
4    {
5        case 'a':
6            ....
7            break;
8        case 'b':
9            ....
10           break;
11       case 'c':
12           ....
13           break;
14   }

If I set a breakpoint at line 1, the program stops at this line without issues. But if I subsequently used next or step, after hitting line 3, the next step will not cause the program to stop at either of the cases. Instead, it continues to run until the next breakpoint is hit or until I do a ctrl-c.

However, if I put a breakpoint at line 6 (for eg), without the breakpoint at line 1, the program can stop correctly at this line.

Has anyone encountered this? Where does the problem lie? How can I fix this behaviour?

Thanks.

1
Did you by any chance compile with optimisation (-Ox with x > 0)? - alk
^^^^^^^^ my first thought too. You should debug with as much optimization turned off as possible. With max opt, much weirdness occurs while debugging :) - ThingyWotsit
@alk and ThingyWotsit There could be a chance that optimisation was turned on even in debug mode. The compilation process was handled by scripts. I can check and get back again. But why would optimisation affect gdb's operations here? - David C
@alk I have checked and optimization was turned off when building the debug version of the firmware. So the mystery remains... - David C
Did you try -Og? (details here: gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html) - alk

1 Answers

0
votes

First of all, disable optimizations. They can cause the compiler to swap certain lines, or omit entire sections if you mistakenly created code that does not run, e.g.

bool condition = false;
xxx;
if (condition)
   printf("this code will not run");

The default level of optimization is -O2, and the best option for debug is -Og. Try finding and changing those.