Rodrigo Gurgel
Rodrigo Gurgel

Reputation: 1736

How to step using, in a C++ code, on a specif function inside the line

In the following code:

 1 #include <iostream>
 2
 3 using namespace std;
 4
 5 int funcA(){
 6    cout << "A" << endl;
 7    return 1;
 8 }
 9
10 int funcB(){
11    cout << "B" << endl;
12    return 1;
13 }
14
15 int funcC(){
16    cout << "C" << endl;
17    return 1;
18 }
19
20 int funcAll( int a, int b, int c ){
21    return 1;
22 }
23
24 int main(){
25    cout << funcAll( funcA(), funcB(), funcC() ) << endl;
26    return 0;
27 }
28

Will be printed C, B, then A.

But when debuging and staying on line 25 if we command next on gdb the cursor goes to the line 26, if we command step gdb will step from funcC until funcA, but how to directly step on funcB OR funcA without setting a breakpoint or steping inside funcC.

Upvotes: 1

Views: 242

Answers (4)

Raman Zhylich
Raman Zhylich

Reputation: 3697

You step into C, then step out and step in to B

step
finish
step

Upvotes: 1

Adam Rosenfield
Adam Rosenfield

Reputation: 400146

The order of evaluation of the arguments of a function in C and C++ is unspecified. The compiler is free to reorder/interleave them however it sees fit, within certain constraints. So you cannot rely on funcC() getting called before funcA().

If you want to ensure that the arguments will get evaluated in a certain order, break them up into separate statements like this:

int a = funcA();
int b = funcB();
int c = funcC();
cout << funcAll(a, b, c,) << endl;

This will also make debugging easier.

If you don't want to rewrite your code, you still have some alternatives. The easiest thing to do is just set a breakpoint inside your function of interest, e.g.:

break funcB

Or if you only need to do this once, use a tbreak instead of break to set a temporary breakpoint which will clear itself after it's hit for the first time.

You can also use the nexti and stepi instructions to step one assembly instruction at a time. By looking at the disassembly with the disassemble command, you can step up to the appropriate call site (e.g. the call instruction on x86 or x86-64, or bl on PowerPC) using nexti, then stepi to step into it. stepi and nexti work like step and next, except they operate on assembly instructions instead of lines of code.

Upvotes: 2

user1118321
user1118321

Reputation: 26325

You can use the until command to run until you reach a specific line. It does not set a breakpoint, it only stops there once. See the docs here. (Specifically, see the docs on until *location*.)

Upvotes: 0

Greg Hewgill
Greg Hewgill

Reputation: 992707

There isn't any direct way to do this; the usual method is to set a breakpoint in the function of interest.

Upvotes: 3

Related Questions