#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
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);
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);
if (is_dynamic_type (type))
{
/* Value is a constant byte-sequence and needs no memory access. */
- type = resolve_dynamic_type (type, /* Unused address. */ 0);
+ type = resolve_dynamic_type (type, NULL, /* Unused address. */ 0);
}
/* Put the constant back in target format. */
v = allocate_value (type);
if (is_dynamic_type (type))
{
/* Value is a constant byte-sequence and needs no memory access. */
- type = resolve_dynamic_type (type, /* Unused address. */ 0);
+ type = resolve_dynamic_type (type, NULL, /* Unused address. */ 0);
}
v = allocate_value (type);
memcpy (value_contents_raw (v), SYMBOL_VALUE_BYTES (var),
symbol_objfile (var));
msym = lookup_data.result.minsym;
+ /* If we can't find the minsym there's a problem in the symbol info.
+ The symbol exists in the debug info, but it's missing in the minsym
+ table. */
if (msym == NULL)
- error (_("No global symbol \"%s\"."), SYMBOL_LINKAGE_NAME (var));
- if (overlay_debugging)
- addr = symbol_overlayed_address (BMSYMBOL_VALUE_ADDRESS (lookup_data.result),
- MSYMBOL_OBJ_SECTION (lookup_data.result.objfile,
- msym));
- else
- addr = BMSYMBOL_VALUE_ADDRESS (lookup_data.result);
-
+ {
+ const char *flavour_name
+ = objfile_flavour_name (symbol_objfile (var));
+
+ /* We can't get here unless we've opened the file, so flavour_name
+ can't be NULL. */
+ gdb_assert (flavour_name != NULL);
+ error (_("Missing %s symbol \"%s\"."),
+ flavour_name, SYMBOL_LINKAGE_NAME (var));
+ }
obj_section = MSYMBOL_OBJ_SECTION (lookup_data.result.objfile, msym);
+ /* Relocate address, unless there is no section or the variable is
+ a TLS variable. */
+ if (obj_section == NULL
+ || (obj_section->the_bfd_section->flags & SEC_THREAD_LOCAL) != 0)
+ addr = MSYMBOL_VALUE_RAW_ADDRESS (msym);
+ else
+ addr = BMSYMBOL_VALUE_ADDRESS (lookup_data.result);
+ if (overlay_debugging)
+ addr = symbol_overlayed_address (addr, obj_section);
+ /* Determine address of TLS variable. */
if (obj_section
&& (obj_section->the_bfd_section->flags & SEC_THREAD_LOCAL) != 0)
addr = target_translate_tls_address (obj_section->objfile, addr);
/* 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. */
int offset = 0;
int reg_offset = value_offset (value);
int regnum = VALUE_REGNUM (value);
- int len = TYPE_LENGTH (check_typedef (value_type (value)));
+ int len = type_length_units (check_typedef (value_type (value)));
gdb_assert (VALUE_LVAL (value) == lval_register);
while (len > 0)
{
struct value *regval = get_frame_register_value (frame, regnum);
- int reg_len = TYPE_LENGTH (value_type (regval)) - reg_offset;
+ int reg_len = type_length_units (value_type (regval)) - reg_offset;
/* If the register length is larger than the number of bytes
remaining to copy, then only copy the appropriate bytes. */
pointer types. Avoid constructing a value object in those cases. */
if (gdbarch_convert_register_p (gdbarch, regnum, type))
{
- gdb_byte *buf = alloca (TYPE_LENGTH (type));
+ gdb_byte *buf = (gdb_byte *) alloca (TYPE_LENGTH (type));
int optim, unavail, ok;
ok = gdbarch_register_to_value (gdbarch, frame, regnum, type,