Stev_k
Stev_k

Reputation: 2126

run floor() or ceil() in gdb

EDIT: I am on Linux Mint 18.3 (Ubuntu 16.04) X64

I have read in a number of other questions that the best way to execute arbitrary functions in gdb is to ensure the input / output variables are cast properly.

The expression p ((double(*)())floor)(2.12) yields $1 = 2.1200000000000001 as does the expression p ((double(*)())ceil)(2.12)

but the other functions in the math library seem to work, e.g. p ((double(*)())fmod)(12.,5.) gives 2; p ((double(*)())sqrt)(64.) gives 8. p ((double(*)())pow)(2.,2.) gives 4 as expected.

So my question is what is the difference for the double / ceil functions which don't give the right value. It also appears the trigonometrical functions also don't return the proper values for some reason. They all seem to have the same or similar function signatures, so I am confused what is going wrong.

The math.h c library is the header being included, but the code is also linking to c++ libraries - not sure if that would make a difference.

Upvotes: 2

Views: 365

Answers (1)

Employed Russian
Employed Russian

Reputation: 213586

This:

p ((double(*)())floor)(2.12)

asks GDB to call a function floor which takes no arguments.

Since on many platforms (including Linux/x86_64) argument passing depends on the type of the argument (integer parameters are passed in RDI, RSI, etc. double parameters in XMM0, XMM1, etc.), you must tell GDB what type of parameter the function expects (unless libm debug info is present, in which case no cast would be necessary). Try

p ((double(*)(double))floor)(2.12)

Upvotes: 1

Related Questions