9

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
No Ordinary Love
  • 569
  • 1
  • 4
  • 15
  • Have you tried something more complex? – stefanB Sep 17 '10 at 04:29
  • Actually yes, I have tried making looped invocations to a(). Still won't work. I have also tried disabling inlining with __attribute__((noinline)) but to no avail. I have tried this on my Hackintosh and it worked. – No Ordinary Love Sep 17 '10 at 04:54

2 Answers2

5

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • I tried using your code but step over still doesn't work on it. Maybe because we're using different versions of gdb? – No Ordinary Love Sep 17 '10 at 04:46
  • What do `help next` and `help step` say on your machine? – John Kugelman Sep 17 '10 at 04:48
  • (gdb) help next Step program, proceeding through subroutine calls. Like the "step" command as long as subroutine calls do not happen; when they do, the call is treated as one instruction. Argument N means do this N times (or till program stops for another reason). (gdb) help step Step program until it reaches a different source line. Argument N means do this N times (or till program stops for another reason). – No Ordinary Love Sep 17 '10 at 04:57
  • 1
    setting the debug symbol format to STABS seemed to fix the problem. – No Ordinary Love Sep 17 '10 at 07:55
-1

'n' is the next statement and will not step into the function.

For stepping into the function, use 's'. That is step.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sashi
  • 3,069
  • 6
  • 20
  • 18