Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is GDB "jumping back" when debugging with c source codes

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)

like image 647
daisy Avatar asked Jun 24 '14 14:06

daisy


People also ask

What does jump do in GDB?

Description. This command jumps the program counter to the specified location. The debugger resumes program execution at that point unless it encounters a breakpoint there.

How do I stop GDB from running?

Quitting GDB To exit GDB, use the quit command (abbreviated q ), or type an end-of-file character (usually C-d ). If you do not supply expression , GDB will terminate normally; otherwise it will terminate using the result of expression as the error code.

How do I step over a breakpoint in GDB?

If you want to execute the entire function with one keypress, type "next" or "n". This is equivalent to the "step over" command of most debuggers. If you want gdb to resume normal execution, type "continue" or "c". gdb will run until your program ends, your program crashes, or gdb encounters a breakpoint.


1 Answers

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.

like image 122
Employed Russian Avatar answered Sep 28 '22 07:09

Employed Russian