Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GDB step over function (next) doesn't seem to work

Tags:

c++

gdb

I'm trying to debug a program I wrote in C++. Here is the code:

void a() { }
void b() { a(); }
int main() { b(); return 0; }

I compiled it using: g++ -g3 -O0 -o cards.exe cards.cpp.

Here is the output of my GDB session:

(gdb) b main
Breakpoint 1 at 0x401421: file cards.cpp, line 10.
(gdb) r
Starting program: C:\workspace\Cards\src/cards.exe
[New thread 1624.0xa28]
Breakpoint 1, main () at cards.cpp:10
10    int main()
(gdb) n
12        b();
(gdb) n
b () at cards.cpp:5 5
void b()
(gdb) n
7        a();
(gdb) quit
The program is running.  Exit anyway? (y or n)

Why does sending a next command to GDB still step into a function?

I'm using g++ 4.2.1-sjlj and GDB 6.8.

like image 845
No Ordinary Love Avatar asked Sep 17 '10 04:09

No Ordinary Love


People also ask

What is difference between next and step in GDB?

To execute one line of code, type "step" or "s". If the line to be executed is a function call, gdb will step into that function and start executing its code one line at a time. If you want to execute the entire function with one keypress, type "next" or "n".

How do you get to the next breakpoint in GDB?

Just press c. It will continue execution until the next breakpoint. You can also disable intermediate breakpoints by using disable #breakpointnumber as stated here.

Why GDB is not working?

This happens because setting or removing breakpoints while the program is running involves temporarily stopping the debug session without waiting for it to hit a breakpoint. This is normally done by sending a Ctrl-C event to either GDB or the GDB stub (e.g. gdbserver or OpenOCD).

Which GDB command will display the next instruction to execute that is controlled by the program counter?

It is often useful to do ' display/i $pc ' when stepping by machine instructions. This makes GDB automatically display the next instruction to be executed, each time your program stops.


1 Answers

The step and next commands work one source line at a time, so when everything is all on one line a single next takes me right to the end of main().

3    int main() { b(); return 0; }
(gdb) n
0x00001faa in start ()

With the code formatted less densely I still do not see the results you see. I put the function calls on separate lines to get GDB to step over them one at a time. Here's what I get then:

jkugelman$ cat cards.cpp
void a() {
}

void b() {
    a();
}

int main() {
    b();
    return 0;
}
jkugelman$ g++ -g3 -O0 -o cards cards.cpp
jkugelman$ gdb ./cards
GNU gdb 6.3.50-20050815 (Apple version gdb-960) (Sun May 18 18:38:33 UTC 2008)
<snip>
Reading symbols for shared libraries .... done

(gdb) b main
Breakpoint 1 at 0x1ff2: file cards.cpp, line 9.
(gdb) r
Starting program: /Users/jkugelman/Development/StackOverflow/cards
Reading symbols for shared libraries +++. done

Breakpoint 1, main () at cards.cpp:9
9        b();
(gdb) n
10        return 0;
(gdb) n
11    }
(gdb) n
0x00001faa in start ()

I don't have an answer, but I just wanted to share that GDB behaves as expected on my iMac. In either case GDB treated the call to b() as one instruction and never entered the function call.

like image 194
John Kugelman Avatar answered Oct 21 '22 09:10

John Kugelman