Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to evaluate functions in GDB?

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 
like image 455
Tim Avatar asked Aug 30 '09 19:08

Tim


People also ask

How do you evaluate expressions in GDB?

evaluate the expression in gdb, doing print strcmp(current_node->word,min_node->word) . Surprisingly, this works: gdb can evaluate function calls, by injecting code into the running program and having it execute the code.

Can you run functions in GDB?

You can use the command inside of gdb info functions to print every function that has been loaded up to this point of execution. Additionally you can use info functions regexp to look for specific functions.

How does GDB calculate return value?

Yes, just examine the EAX register by typing print $eax . For most functions, the return value is stored in that register, even if it's not used. The exceptions to this are functions returning types larger than 32 bits, specifically 64-bit integers ( long long ), double s, and structs or classes .


2 Answers

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

like image 187
anon Avatar answered Oct 11 '22 02:10

anon


The syntax for calling a function in gdb is

call pow(3,2) 

Type

help call 

at the gdb prompt for more information.

like image 34
Charles E. Grant Avatar answered Oct 11 '22 02:10

Charles E. Grant