tags:

views:

72

answers:

2

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)

My question is, 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.

A: 

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

for stepping into the function use 's' that is step

Sashi
A: 

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.

John Kugelman
I tried using your code but step over still doesn't work on it. Maybe because we're using different versions of gdb?
Jay
What do `help next` and `help step` say on your machine?
John Kugelman
(gdb) help nextStep 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 stepStep program until it reaches a different source line.Argument N means do this N times (or till program stops for another reason).
Jay
setting the debug symbol format to STABS seemed to fix the problem.
Jay