How to evaluate functions in GDB?

72,128

Solution 1

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

Solution 2

You need to tell gdb that it will find the return value in the floating point registers, not the normal ones, in addition to give the parameters the right types.

I.e.:

(gdb) p ((double(*)())pow)(2.,2.)

$1 = 4

Solution 3

The syntax for calling a function in gdb is

call pow(3,2)

Type

help call

at the gdb prompt for more information.

Solution 4

Actually, at least on my LINUX implementation of gcc, many of the math functions are replaced with variants specific to the types of their arguments via some fancy substitutions pulled in by math.h and bits/mathcalls.h (included from within math.h). As a consequence, functions like pow and exp are called instead as __pow or *__GI___exp (your results may vary depending on the types of the arguments and perhaps the particular version).

To identify what exactly the function is that is linked in to my code I put a break at a line where just that function is called, e.g. have a line in my code with b=exp(c);. Then I run in gdb up till that break point and then use the "step" command to enter the call from that line. Then I can use the "where" command to identify the name of the called routine. In my case that was *__GI___exp.

There are probably cleverer ways to get this information, however, I was not able to find the right name just by running the preprocessor alone (the -E option) or by looking at the assembly code generated (-s).

Solution 5

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. )
Share:
72,128
Tim
Author by

Tim

Elitists are oppressive, anti-intellectual, ultra-conservative, and cancerous to the society, environment, and humanity. Please help make Stack Exchange a better place. Expose elite supremacy, elitist brutality, and moderation injustice to https://stackoverflow.com/contact (complicit community managers), in comments, to meta, outside Stack Exchange, and by legal actions. Push back and don't let them normalize their behaviors. Changes always happen from the bottom up. Thank you very much! Just a curious self learner. Almost always upvote replies. Thanks for enlightenment! Meanwhile, Corruption and abuses have been rampantly coming from elitists. Supportive comments have been removed and attacks are kept to control the direction of discourse. Outright vicious comments have been removed only to conceal atrocities. Systematic discrimination has been made into policies. Countless users have been harassed, persecuted, and suffocated. Q&amp;A sites are for everyone to learn and grow, not for elitists to indulge abusive oppression, and cover up for each other. https://softwareengineering.stackexchange.com/posts/419086/revisions https://math.meta.stackexchange.com/q/32539/ (https://i.stack.imgur.com/4knYh.png) and https://math.meta.stackexchange.com/q/32548/ (https://i.stack.imgur.com/9gaZ2.png) https://meta.stackexchange.com/posts/353417/timeline (The moderators defended continuous harassment comments showing no reading and understanding of my post) https://cs.stackexchange.com/posts/125651/timeline (a PLT academic had trouble with the books I am reading and disparaged my self learning posts, and a moderator with long abusive history added more insults.) https://stackoverflow.com/posts/61679659/revisions (homework libels) Much more that have happened.

Updated on March 29, 2020

Comments

  • Tim
    Tim about 4 years

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

    (gdb) p pow(3,2)
    
    $10 = 1
    
    (gdb) p pow(3,3)
    
    $11 = 1
    
    (gdb) p sqrt(9)
    
    $12 = 0
    
  • Tim
    Tim almost 15 years
    Thanks! But call is still not correctly working (gdb) call sqrt(9) $14 = 0 (gdb) call pow(3,3) $15 = 1
  • Isak Savo
    Isak Savo almost 15 years
    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
    Isak Savo almost 15 years
    print will also call functions. In fact, I think the only difference is that call won't clutter the output when calling void functions
  • falstro
    falstro over 10 years
    I know this is old, but if someone comes looking, here goes: For me it worked to just do p pow which gave me: $28 = {<text variable, no debug info>} 0x7ffff77ffbd0 <__pow>
  • daj
    daj almost 9 years
    That's odd I get a value of $1 = 2
  • mattypiper
    mattypiper over 6 years
    Correct answer is given below by anon
  • Bebe Carabina
    Bebe Carabina over 4 years
    Hello! I am an idiot and have been bashing my head open to figure out what the hell does double() mean?? I thought it meant a function pointer that can "return double pointer, and takes void args". So I tried reconverting to ((double)())pow)(2.,2.) -- failed! Why?
  • bgoodr
    bgoodr over 4 years
    @mattypiper "given below by anon" needs a link in that comment. I think you intended to point to anon's answer .
  • bgoodr
    bgoodr over 4 years
    @BebeCarabina I feel your frustration: I desire to avoid this typecasting-induced severe head-trauma. I posted a related question at How to evaluate functions in GDB without painful typecasting? to see if there is any solution.