Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 498
fuad Avatar asked Nov 06 '08 04:11

fuad


People also ask

How do I find my GDB return address?

To get the location of the stored return address of a specific function, you can place a breakpoint at that function and use the info frame command.

What is RET in GDB?

A blank line as input to GDB (typing just RET ) means to repeat the previous command. Certain commands (for example, run ) will not repeat this way; these are commands whose unintentional repetition might cause trouble and which you are unlikely to want to repeat.

How do I go to a specific function 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".


1 Answers

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!

like image 71
hark Avatar answered Oct 28 '22 10:10

hark