8

I'm debugging the goldfish android kernel (version 3.4), with kernel sources.

Now I found that gdb sometimes jump back and forth between lines, e.g consider c source code like the following:

char *XXX;
int a;
...

if (...)
{

}

When I reached the if clause, I type in n and it will jump back to the int a part. Why is that?

If I execute that command again, it would enter the brackets in the if.

If possible, I want to avoid that part, and enter the if directly (of course, if condition matches)

daisy
  • 22,498
  • 29
  • 129
  • 265
  • I've not debugged in that environment, but I would guess it's to do with optimisations / code-rearragnement that the compiler has done. For example, if the `a` was only used inside the `if`, then it may not get created/initialised unless the `if` is entered. – TripeHound Jun 24 '14 at 14:32

1 Answers1

10

When I reached the if clause, I type in n and it will jump back to the int a part. Why is that?

Because your code is compiled with optimization on, and the compiler can (and often does) re-arrange instructions of your program in such a way that instructions "belonging" to different source lines are interleaved (code motion optimizations attempt (among other things) to move load instructions to long before their results are needed; this helps to hide memory latency).

If you are using gcc-4.8 or later, build your sources with -Og. Else, see this answer.

Community
  • 1
  • 1
Employed Russian
  • 199,314
  • 34
  • 295
  • 362