6

I'm trying to write a GDB script (legacy, not Python) that will print information on members of a local variable (a C or C++ struct), but only if that local variable exists. Something like:

# 'magic' should be evaluate to "if 'info locals' has a variable named foo, then
# evaluate to true, otherwise evaluate to false.
if (magic)
    print foo->member
end

I know this is somewhat contrived, because the locals are dependent on the stack frame (so I'm probably better off making it conditional on the frame), but I'd still like to know if something along these lines is possible.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Rob
  • 1,350
  • 12
  • 21

1 Answers1

2

First -- Python is just far superior for this kind of thing. That's why we added it to gdb!

However, this can still be done with an older gdb. However, it's awful, and after doing it I think you'll appreciate the Python approach even more. What you do is: first, use the various "set logging" commands to redirect the output to a temporary file . Then use gdb commands to print the information you need, in this case something like "info local". Then, use the "shell" command to shell out to rewrite the temporary file into a file that is itself a gdb script. For example, use "sed" to detect that the variable exists in your output, and then emit "set $var_exists=1". Finally, "source" the result of this scripting and test the convenience variable that was set.

Eww. But it works.

Tom Tromey
  • 21,507
  • 2
  • 45
  • 63
  • Thanks Tom! I abondoned Python scripts earlier as I couldn't get module gdb and python-2.6gdb.py to play nicely on my platform (https://docs.python.org/devguide/gdb.html), so I figured it's easier to stick with legacy scripts for now. In retrospect it looks like I've thrown the baby out with the bathwater, so I'll definitely give Python another spin. – Rob Apr 03 '14 at 06:27