0

This question was asked a long time ago but were not really answered. I get some weird wrong results when I try to evaluate math functions in gdb. I started with the following file

#include <math.h>
#include <stdio.h>

int main()
{
  double x ;
  x=sin(3.14) ;
  printf("%f",x) ;
}

Launch gdb on it and break just before the printf. Now at the gdb prompt:

(gdb) p x
$1 = 0.0015926529164868282
(gdb) p sin(3.14)
$2 = 1074339512
(gdb) p ((double(*)())sin)(3.14)
$3 = 3.1399999999999997

Does anybody have an explanation of this? How can I evaluate the sin function in gdb and if I can't why?

Edited: The answer provided previously does not seems to work, at least for sin (see my comment).

Olivier Esser
  • 431
  • 1
  • 4
  • 12
  • Did you try `ptype sin` ? How did you compile your code? Try with `gcc -Wall -g source.c -lm -o prog` – Basile Starynkevitch Dec 25 '15 at 09:04
  • `(gdb) ptype sin` `type = int ()()` which is completely weird. The mini program compile without warning with your command (I compiled initially with -ggdb3; but that does not change anything) – Olivier Esser Dec 25 '15 at 09:18
  • Perhaps `sin` is a macro using `_Generic`? Please check that in your system header files – Basile Starynkevitch Dec 25 '15 at 09:20
  • There is explanation in http://stackoverflow.com/q/8570196/72178 – ks1322 Dec 25 '15 at 09:23
  • 1
    Why are the previous answers to this question unclear to you? What previous answers are you referring to? GDB does not know teh return type of sin, so assumes an int. – Clifford Dec 25 '15 at 09:25
  • You might well be right. The header is not that easy to follow, I am just beginning with C (it containq a lot of other inclusions and conditionals). I did believe that such macro were only defined in tgmath.h and not math.h but I am probably wrong. But even then, to what does correspond the real sin function then? Is there any way to call a macro from gdb? I am using archlinux current, which ship the more recent GNU glibc and headers – Olivier Esser Dec 25 '15 at 09:26
  • @Clifford `p ((double(*)(double))sin)(3.14)` `$2 = 3.1399999999999997` which is still wrong. – Olivier Esser Dec 25 '15 at 09:34
  • @Olivier : Perhaps the return value is in a FP register and GDB is not using the appropriate calling convention for the target, and simply casting the register holding the operand. Either way, the answer is that GDB does not have the necessary debug information for the FP library functions to process the return value correctly. The behaviour is target specific. The header file is largely irrelevant - GDB only uses information the linker places in the executable, and the library code is linked object code rather then source. – Clifford Dec 25 '15 at 10:07
  • @cliford I do not know the internal of gdb but I think I understand the linking process. The compiler know what sin is exactly from the headers. It seems to be possible, at least in theory, to include this information in the debug symbols. But apparently this is not the case. Is there any way to do it (with some magic flags I do not understand? Is it fundamentally impossible or difficult to implement? Or the only possibility is to rebuild glibc with debug symbols. – Olivier Esser Dec 25 '15 at 10:21

0 Answers0