tags:

views:

132

answers:

2

Hi,

Ive been looking around but was unable to figure out how one could print out in gdb the result of an evaluation. ie for instance 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 wasnt 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 was wondering if there is a simple way to watch the evaluation of a test on the line where gdb / program flow currently is.

many thanks!

+1  A: 

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.

yves Baumes
+7  A: 

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.
Martin v. Löwis
great, thank you - this helps!
nero