c++gdb

How to print result of C++ evaluation with 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.


Solution

  • 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.