Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

debugging - How to inspect the return value of a function in GDB?

Is it possible to inspect the return value of a function in gdb assuming the return value is not assigned to a variable?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I imagine there are better ways to do it, but the finish command executes until the current stack frame is popped off and prints the return value -- given the program

int fun() {
    return 42;
}

int main( int argc, char *v[] ) {
    fun();
    return 0;
}

You can debug it as such --

(gdb) r
Starting program: /usr/home/hark/a.out 

Breakpoint 1, fun () at test.c:2
2               return 42;
(gdb) finish
Run till exit from #0  fun () at test.c:2
main () at test.c:7
7               return 0;
Value returned is $1 = 42
(gdb) 

The finish command can be abbreviated as fin. Do NOT use the f, which is abbreviation of frame command!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

62 comments

56.6k users

...