Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print result of C++ evaluation with GDB?

Tags:

c++

gdb

I've been looking around but was unable to figure out how one could print out in GDB the result of an evaluation. For example, in the code below:

if (strcmp(current_node->word,min_node->word) > 0)
      min_node = current_node;

(above I was trying out a possible method for checking alphabetical order for strings, and wasn't absolutely certain it works correctly.)

Now I could watch min_node and see if the value changes but in more involved code this is sometimes more complicated. I am wondering if there is a simple way to watch the evaluation of a test on the line where GDB / program flow currently is.

like image 763
nero Avatar asked Oct 17 '09 20:10

nero


People also ask

What does print do in GDB?

The usual way to examine data in your program is with the print command (abbreviated p ), or its synonym inspect . It evaluates and prints the value of an expression of the language your program is written in (see section Using GDB with Different Languages). expr is an expression (in the source language).

What does incomplete type mean in GDB?

but no definition for struct foo itself, gdb will say: (gdb) ptype foo $1 = <incomplete type> “Incomplete type” is C terminology for data types that are not completely specified.

What does evaluate mean C++?

"Evaluate" means "obtain the value of".


2 Answers

There is no expression-level single stepping in gdb, if that's what you are asking for.

Your options are (from most commonly to most infrequently used):

  1. 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. Of course, this is fairly dangerous if the functions have side effects or may crash; in this case, it is so harmless that people typically won't think about potential problems.
  2. perform instruction-level (assembly) single-stepping (ni/si). When the call instruction is done, you find the result in a register, according to the processor conventions (%eax on x86).
  3. edit the code to assign intermediate values to variables, and split that into separate lines/statements; then use regular single-stepping and inspect the variables.
like image 167
Martin v. Löwis Avatar answered Sep 17 '22 18:09

Martin v. Löwis


you may simply try to type in :

call "my_funtion()"

as far as i rember, though it won't work when a function is inlined.

like image 40
yves Baumes Avatar answered Sep 21 '22 18:09

yves Baumes