2

If I have some C++ program, e.g.:

#include <string>

std::string foo()
{
        return "bar";
}

int main()
{
        foo();
        return 0;
}

where there is a call to some function (e.g. foo) and I step over it (and thus I cannot perform finish that would otherwise show the return value). If this function returns some value (e.g. of type std::string), how can I print the value returned by the function I just stepped over?

If the returned value is scalar, I can see the returned value in the appropriate register (i.e. in $rax on Intel's x64 architecture).

If the returned value is structured value, in rax register I just see a pointer. I failed to persuade gdb to pretty-print the underlying structured value to which $rax points. I would be able to specify the C++ type by casing $rax to the appropriate pointer type as long as I do not have to cite mangled version of C++ types.

If, instead of just stepping over the function (with next gdb command) I "step in" in the function (with step gdb command) and then subsequently finishing the function (with finish gdb command), the returned value is pretty-printed and I can see it. This is a usable workaround, but I am interested in proper way, if it exists.

If gdb does not provide such command out of the box, is it possible to define some Python or Scheme macro that would (pretty-) print the returned value?

  • The linked "answer" describes a workaround that I have found (because I have read the linked answer). It does not answer the original question. I was asking, how can I see the value returned by a function to which I did not step into but only stepped over (and thus I does not make sense to perform the "finish" command they mention). – Matej Kosik Dec 01 '22 at 11:03
  • `p *(std::string *)$rax` will work in some cases. But when you're stopping at the statement after the statement containing a function call, rax may have been clobbered by other copy/move constructors, destructors, and calls to `_Unwind_Resume`. – Mark Plotnick Dec 03 '22 at 15:20

0 Answers0