This is the mail archive of the
mailing list for the GDB project.
Re: Checking variable scope
On 27/11/06, Jim Blandy <email@example.com> wrote:
"Rob Quill" <firstname.lastname@example.org> writes:
> I am stepping through the program, so I suppose I'd be looking to see
> if the variable is in scope at the location of the PC.
> How would I call lookup_symbol, given that information? I had a brief
> look, but I wasn't sure what parameters to pass to the function, or
> how to get the right values.
You can pass NULL for 'symtab' and 'is_a_field_of_this', and
VAR_DOMAIN for domain. block is the scoping block containing the PC.
I don't know where you're calling things from, so I can't really tell
you how to get the current PC.
The aim would be to add a command to gdb, in_scope (or similar) which
can be used from a GDB script to check if a variable is in scope
without throwing an error if is isn't. As currently if I do "print a"
and a is not in scope then I get an error and the script stops, which
is fine. But even better would be if I were able to check if a was in
scope and only print (or do other things with it) if it was.
Does that help explain where I would be calling things from? As I
understand it, current when you do "print a" it tries to parse a, as
it is possible to print expressions etc, and when is parsing in, in
exp_c.y, it throws errors if the variable is not in scope.
So currently my code sets a global variable check_scope, and in
exp_c.y, if check_scope is 1, then an internal variable is set if the
variable is not in scope, and an _error() is thrown, with an empty
string, and when running a script and an error is encountered, if the
error message is the empty string then the error is ignored.
Obviously this is not a particularly neat way of doing it, and I'm
sure there is a better way, so any help is appreciated.