If I have a C function and I'm debugging it with gdb, how do I find the return value of a function?

c, cpu-registers, debugging, gdb, stack

Solution

In general, look at the calling convention.

In x86, all calling conventions return small integer results on EAX, and large (64-bit) results on EDX:EAX (EDX holding the higher bits), and floating point results in FP0.

In x64, small integer results are returned on RAX, and floating point results in FP0.

In ARM (including thumb-mode), integer results are returned in R0.

If you're trying to work out where to put your breakpoint, my suggestion is to put a breakpoint at the start of the main function. If you do that, the return address (i.e. where main will return to once it is finished executing) will be the value on the top of the stack. If you put a breakpoint there, you'll break just after the main function has finished executing.

Since main has the return type of int, you can look at EAX (or RAX or R0) to see what value main returned.

Problem

I have an assignment that asks me to find the return value of main though register inspection (we're learning gdb), how would I go about doing that?

Original source

Related problems