views:

910

answers:

3

Hi, I wonder why evaluate function doesn't work in gdb? In my source file I include, when debugging in gdb, these examples are wrongly evaludations.

(gdb) p pow(3,2)

$10 = 1

(gdb) p pow(3,3)

$11 = 1

(gdb) p sqrt(9)

$12 = 0

Thanks and regards!

A: 

The syntax for calling a function in gdb is

call pow(3,2)

Type

help call

at the gdb prompt for more information.

Charles E. Grant
Thanks! But call is still not correctly working(gdb) call sqrt(9)$14 = 0(gdb) call pow(3,3)$15 = 1
Tim
print will also call functions. In fact, I think the only difference is that call won't clutter the output when calling void functions
Isak Savo
A: 
NAME
   pow, powf, powl - power functions

SYNOPSIS
   #include <math.h>

   double pow(double x, double y);

You shouldn't pass an int in the place of a double

 call pow( 3. , 2. )

Also, passing a single argument is not enough, you need two arguments just like the function expects

 wrong: call pow ( 3. )
Adrian Panasiuk
actually, passing ints work fine. not sure if it is just coincidence, but calling mypow (see my answer) with integers produce the correct result. I get no output from gdb when calling pow(2.0,2.0), but I do when calling mypow() with any numbers
Isak Savo
+2  A: 

My guess is that the compiler and linker does some magic with those particular functions. Most likely to increase performance.

If you absolutely need pow() to be available in gdb then you can create your own wrapper function:

double mypow(double a, double b)
{
    return pow(a,b);
}

Maybe also wrap it into a #ifdef DEBUG or something to not clutter the final binary.

BTW, you will notice that other library functions can be called (and their return value printed), for instance:

(gdb) print printf("hello world")
$4 = 11
Isak Savo