Home > Software engineering >  Finding return value of a function in GDB
Finding return value of a function in GDB

Time:04-06

I am very new to using gdb and very new to C programming. There is a line of code in the function I am stepping through that is:

append(MACRO, F1());

append is just a append string function. I want to know how to find the value of the function F1(). How would I go about doing this. I tried display but then realised it wasnt giving me the correct answer. The output from using display F1 is:

{<text variable, no debug info>} 0x4037bc

CodePudding user response:

first break F1, continue to run until F1 is interrupted, and then execute finish, then $rax(or $eax) is the return value of F1

e.g:

char *F1() {
    return "1234";
}

(gdb) break F1
Breakpoint 1 at 0x1151
(gdb) c
Breakpoint 1, 0x0000555555555151 in F1 ()
(gdb) fin
Run till exit from #0  0x0000555555555151 in F1 ()
0x00005555555551a2 in main ()
(gdb) p (char*)$rax
$1 = 0x555555556004 "1234"
(gdb) quit

CodePudding user response:

To print the value, it was needed to be cast to an integer first. E.g. print (int)F1()

  • Related