diff options
Diffstat (limited to 'gdb/findvar.c')
-rw-r--r-- | gdb/findvar.c | 168 |
1 files changed, 164 insertions, 4 deletions
diff --git a/gdb/findvar.c b/gdb/findvar.c index 83b4fca..1c077f7 100644 --- a/gdb/findvar.c +++ b/gdb/findvar.c @@ -32,6 +32,7 @@ #include "block.h" #include "objfiles.h" #include "language.h" +#include "dwarf2loc.h" /* Basic byte-swapping routines. All 'extract' functions return a host-format integer from a target-format integer at ADDR which is @@ -409,11 +410,166 @@ minsym_lookup_iterator_cb (struct objfile *objfile, void *cb_data) return (data->result.minsym != NULL); } +/* Given static link expression and the frame it lives in, look for the frame + the static links points to and return it. Return NULL if we could not find + such a frame. */ + +static struct frame_info * +follow_static_link (struct frame_info *frame, + const struct dynamic_prop *static_link) +{ + CORE_ADDR upper_frame_base; + + if (!dwarf2_evaluate_property (static_link, frame, NULL, &upper_frame_base)) + return NULL; + + /* Now climb up the stack frame until we reach the frame we are interested + in. */ + for (; frame != NULL; frame = get_prev_frame (frame)) + { + struct symbol *framefunc = get_frame_function (frame); + + /* Stacks can be quite deep: give the user a chance to stop this. */ + QUIT; + + /* If we don't know how to compute FRAME's base address, don't give up: + maybe the frame we are looking for is upper in the stace frame. */ + if (framefunc != NULL + && SYMBOL_BLOCK_OPS (framefunc)->get_frame_base != NULL + && (SYMBOL_BLOCK_OPS (framefunc)->get_frame_base (framefunc, frame) + == upper_frame_base)) + break; + } + + return frame; +} + +/* Assuming VAR is a symbol that can be reached from FRAME thanks to lexical + rules, look for the frame that is actually hosting VAR and return it. If, + for some reason, we found no such frame, return NULL. + + This kind of computation is necessary to correctly handle lexically nested + functions. + + Note that in some cases, we know what scope VAR comes from but we cannot + reach the specific frame that hosts the instance of VAR we are looking for. + For backward compatibility purposes (with old compilers), we then look for + the first frame that can host it. */ + +static struct frame_info * +get_hosting_frame (struct symbol *var, const struct block *var_block, + struct frame_info *frame) +{ + const struct block *frame_block = NULL; + + if (!symbol_read_needs_frame (var)) + return NULL; + + /* Some symbols for local variables have no block: this happens when they are + not produced by a debug information reader, for instance when GDB creates + synthetic symbols. Without block information, we must assume they are + local to FRAME. In this case, there is nothing to do. */ + else if (var_block == NULL) + return frame; + + /* We currently assume that all symbols with a location list need a frame. + This is true in practice because selecting the location description + requires to compute the CFA, hence requires a frame. However we have + tests that embed global/static symbols with null location lists. + We want to get <optimized out> instead of <frame required> when evaluating + them so return a frame instead of raising an error. */ + else if (var_block == block_global_block (var_block) + || var_block == block_static_block (var_block)) + return frame; + + /* We have to handle the "my_func::my_local_var" notation. This requires us + to look for upper frames when we find no block for the current frame: here + and below, handle when frame_block == NULL. */ + if (frame != NULL) + frame_block = get_frame_block (frame, NULL); + + /* Climb up the call stack until reaching the frame we are looking for. */ + while (frame != NULL && frame_block != var_block) + { + /* Stacks can be quite deep: give the user a chance to stop this. */ + QUIT; + + if (frame_block == NULL) + { + frame = get_prev_frame (frame); + if (frame == NULL) + break; + frame_block = get_frame_block (frame, NULL); + } + + /* If we failed to find the proper frame, fallback to the heuristic + method below. */ + else if (frame_block == block_global_block (frame_block)) + { + frame = NULL; + break; + } + + /* Assuming we have a block for this frame: if we are at the function + level, the immediate upper lexical block is in an outer function: + follow the static link. */ + else if (BLOCK_FUNCTION (frame_block)) + { + const struct dynamic_prop *static_link + = block_static_link (frame_block); + int could_climb_up = 0; + + if (static_link != NULL) + { + frame = follow_static_link (frame, static_link); + if (frame != NULL) + { + frame_block = get_frame_block (frame, NULL); + could_climb_up = frame_block != NULL; + } + } + if (!could_climb_up) + { + frame = NULL; + break; + } + } + + else + /* We must be in some function nested lexical block. Just get the + outer block: both must share the same frame. */ + frame_block = BLOCK_SUPERBLOCK (frame_block); + } + + /* Old compilers may not provide a static link, or they may provide an + invalid one. For such cases, fallback on the old way to evaluate + non-local references: just climb up the call stack and pick the first + frame that contains the variable we are looking for. */ + if (frame == NULL) + { + frame = block_innermost_frame (var_block); + if (frame == NULL) + { + if (BLOCK_FUNCTION (var_block) + && !block_inlined_p (var_block) + && SYMBOL_PRINT_NAME (BLOCK_FUNCTION (var_block))) + error (_("No frame is currently executing in block %s."), + SYMBOL_PRINT_NAME (BLOCK_FUNCTION (var_block))); + else + error (_("No frame is currently executing in specified" + " block")); + } + } + + return frame; +} + /* A default implementation for the "la_read_var_value" hook in the language vector which should work in most situations. */ struct value * -default_read_var_value (struct symbol *var, struct frame_info *frame) +default_read_var_value (struct symbol *var, const struct block *var_block, + struct frame_info *frame) { struct value *v; struct type *type = SYMBOL_TYPE (var); @@ -427,7 +583,10 @@ default_read_var_value (struct symbol *var, struct frame_info *frame) check_typedef (type); if (symbol_read_needs_frame (var)) - gdb_assert (frame); + gdb_assert (frame != NULL); + + if (frame != NULL) + frame = get_hosting_frame (var, var_block, frame); if (SYMBOL_COMPUTED_OPS (var) != NULL) return SYMBOL_COMPUTED_OPS (var)->read_variable (var, frame); @@ -610,14 +769,15 @@ default_read_var_value (struct symbol *var, struct frame_info *frame) /* Calls VAR's language la_read_var_value hook with the given arguments. */ struct value * -read_var_value (struct symbol *var, struct frame_info *frame) +read_var_value (struct symbol *var, const struct block *var_block, + struct frame_info *frame) { const struct language_defn *lang = language_def (SYMBOL_LANGUAGE (var)); gdb_assert (lang != NULL); gdb_assert (lang->la_read_var_value != NULL); - return lang->la_read_var_value (var, frame); + return lang->la_read_var_value (var, var_block, frame); } /* Install default attributes for register values. */ |