How to evaluate functions in GDB without painful typecasting?
Asked Answered
S

1

2

In an answer to How to evaluate functions in GDB? I found the recipe to call a function outside of my program called floor from within GDB like this:

(gdb) p floor
$20 = {<text variable, no debug info>} 0x38e10197b0 <floor>
(gdb) p ((double(*)(double))floor)(2.9999)
$21 = 2
(gdb) p ((double(*)(double))floor)(2.000001)
$22 = 2
(gdb) 

What do I need to do, short of a compiler upgrade which is not an option for me at the moment, in order for me to call the function in the same manner in which I would do inside the original C/C++ text file, e.g.,:

p floor(2.9999)

Specifically, I would like to not have to open up the man page on floor to find the function signature:

double floor(double x);

and then concoct an expression to feed to gdb's print command that is a pointer taking a double returning a double, with lots of parentheses.

So, is this the only way for us to call these types of low level functions in GDB? Or is there something I'm missing? Is it because I simply don't have some type of "debug symbols" loaded or something like that? Reference no debug info in the output above that leads me to believe that perhaps my machine is lacking some package for "debug symbols" or the equivalent that would allow me to express this in the native C or C++?

This is specifically for Linux. I'm not interested in platform-agnostic answers as they are irrelevant in my use-case (not in general, though, just for this question).

Stunner answered 24/3, 2020 at 21:4 Comment(1)
With a recent gdb, you should only need to cast the return type, so this should work: p (double)floor(2.9999)Chalk
G
3

What do I need to do

You should install libc6-dbg or similar package of debugging symbols for libc (floor is in libm, which is part of GLIBC).

Without debug symbols, GDB has no idea what the type of floor is.

Gigolo answered 24/3, 2020 at 22:16 Comment(1)
Marked this as the answer, because I know it is for sure, but I'm still stuck on actually getting it to work in practice on Ubuntu 19.10, so I posted another deeper question: https://mcmap.net/q/376418/-how-to-make-gdb-allow-me-to-call-quot-floor-quot-even-when-debug-info-files-are-installed/257924Stunner

© 2022 - 2024 — McMap. All rights reserved.