/* Implementation of the GDB variable objects API.
- Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
+ Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
#include "varobj.h"
#include "vec.h"
+#include "gdbthread.h"
+#include "inferior.h"
/* Non-zero if we want to see trace of varobj level stuff. */
/* Block for which this expression is valid */
struct block *valid_block;
- /* The frame for this expression */
+ /* The frame for this expression. This field is set iff valid_block is
+ not NULL. */
struct frame_id frame;
+ /* The thread ID that this varobj_root belong to. This field
+ is only valid if valid_block is not NULL.
+ When not 0, indicates which thread 'frame' belongs to.
+ When 0, indicates that the thread list was empty when the varobj_root
+ was created. */
+ int thread_id;
+
/* If 1, "update" always recomputes the frame & valid block
using the currently selected frame. */
int use_selected_frame;
struct varobj_root *next;
};
-typedef struct varobj *varobj_p;
-
-DEF_VEC_P (varobj_p);
-
/* Every variable in the system has a structure of this type defined
for it. This structure holds all information necessary to manipulate
a particular object variable. Members which must be freed are noted. */
we must select the appropriate frame before parsing
the expression, otherwise the value will not be current.
Since select_frame is so benign, just call it for all cases. */
- if (fi != NULL)
+ if (innermost_block && fi != NULL)
{
var->root->frame = get_frame_id (fi);
+ var->root->thread_id = pid_to_thread_id (inferior_ptid);
old_fi = get_selected_frame (NULL);
- select_frame (fi);
+ select_frame (fi);
}
/* We definitely need to catch errors here.
var->format = variable_default_display (var);
}
+ if (varobj_value_is_changeable_p (var)
+ && var->value && !value_lazy (var->value))
+ {
+ free (var->print_value);
+ var->print_value = value_get_print_value (var->value, var->format);
+ }
+
return var->format;
}
return var->format;
}
+/* If the variable object is bound to a specific thread, that
+ is its evaluation can always be done in context of a frame
+ inside that thread, returns GDB id of the thread -- which
+ is always positive. Otherwise, returns -1. */
+int
+varobj_get_thread_id (struct varobj *var)
+{
+ if (var->root->valid_block && var->root->thread_id > 0)
+ return var->root->thread_id;
+ else
+ return -1;
+}
+
void
varobj_set_frozen (struct varobj *var, int frozen)
{
/* Creates a list of the immediate children of a variable object;
the return code is the number of such children or -1 on error */
-int
-varobj_list_children (struct varobj *var, struct varobj ***childlist)
+VEC (varobj_p)*
+varobj_list_children (struct varobj *var)
{
struct varobj *child;
char *name;
int i;
- /* sanity check: have we been passed a pointer? */
- if (childlist == NULL)
- return -1;
-
- *childlist = NULL;
-
if (var->num_children == -1)
var->num_children = number_of_children (var);
/* If that failed, give up. */
if (var->num_children == -1)
- return -1;
+ return var->children;
/* If we're called when the list of children is not yet initialized,
allocate enough elements in it. */
while (VEC_length (varobj_p, var->children) < var->num_children)
VEC_safe_push (varobj_p, var->children, NULL);
- /* List of children */
- *childlist = xmalloc ((var->num_children + 1) * sizeof (struct varobj *));
-
for (i = 0; i < var->num_children; i++)
{
- varobj_p existing;
-
- /* Mark as the end in case we bail out */
- *((*childlist) + i) = NULL;
-
- existing = VEC_index (varobj_p, var->children, i);
+ varobj_p existing = VEC_index (varobj_p, var->children, i);
if (existing == NULL)
{
existing = create_child (var, i, name);
VEC_replace (varobj_p, var->children, i, existing);
}
-
- *((*childlist) + i) = existing;
}
- /* End of list is marked by a NULL pointer */
- *((*childlist) + i) = NULL;
-
- return var->num_children;
+ return var->children;
}
/* Obtain the type of an object Variable as a string similar to the one gdb
struct value *new;
VEC (varobj_p) *stack = NULL;
VEC (varobj_p) *result = NULL;
- struct frame_id old_fid;
struct frame_info *fi;
/* sanity check: have we been passed a pointer? */
if ((*varp)->root->rootvar == *varp)
{
- /* Save the selected stack frame, since we will need to change it
- in order to evaluate expressions. */
- old_fid = get_frame_id (deprecated_safe_get_selected_frame ());
-
/* Update the root variable. value_of_root can return NULL
if the variable is no longer around, i.e. we stepped out of
the frame in which a local existed. We are letting the
has changed. */
type_changed = 1;
new = value_of_root (varp, &type_changed);
-
- /* Restore selected frame. */
- fi = frame_find_by_id (old_fid);
- if (fi)
- select_frame (fi);
/* If this is a "use_selected_frame" varobj, and its type has changed,
them note that it's changed. */
|| TYPE_CODE (target_type) == TYPE_CODE_UNION)
{
if (value && *value)
- gdb_value_ind (*value, value);
+ {
+ int success = gdb_value_ind (*value, value);
+ if (!success)
+ *value = NULL;
+ }
*type = target_type;
if (was_ptr)
*was_ptr = 1;
*cname = xstrprintf ("*%s", parent->name);
if (cvalue && value)
- gdb_value_ind (value, cvalue);
+ {
+ int success = gdb_value_ind (value, cvalue);
+ if (!success)
+ *cvalue = NULL;
+ }
/* Don't use get_target_type because it calls
check_typedef and here, we want to show the true
return child->path_expr;
}
+/* If frame associated with VAR can be found, switch
+ to it and return 1. Otherwise, return 0. */
+static int
+check_scope (struct varobj *var)
+{
+ struct frame_info *fi;
+ int scope;
+
+ fi = frame_find_by_id (var->root->frame);
+ scope = fi != NULL;
+
+ if (fi)
+ {
+ CORE_ADDR pc = get_frame_pc (fi);
+ if (pc < BLOCK_START (var->root->valid_block) ||
+ pc >= BLOCK_END (var->root->valid_block))
+ scope = 0;
+ else
+ select_frame (fi);
+ }
+ return scope;
+}
+
static struct value *
c_value_of_root (struct varobj **var_handle)
{
struct value *new_val = NULL;
struct varobj *var = *var_handle;
struct frame_info *fi;
- int within_scope;
-
+ int within_scope = 0;
+ struct cleanup *back_to;
+
/* Only root variables can be updated... */
if (!is_root_p (var))
/* Not a root var */
return NULL;
+ back_to = make_cleanup_restore_current_thread (
+ inferior_ptid, get_frame_id (deprecated_safe_get_selected_frame ()));
/* Determine whether the variable is still around. */
if (var->root->valid_block == NULL || var->root->use_selected_frame)
within_scope = 1;
+ else if (var->root->thread_id == 0)
+ {
+ /* The program was single-threaded when the variable object was
+ created. Technically, it's possible that the program became
+ multi-threaded since then, but we don't support such
+ scenario yet. */
+ within_scope = check_scope (var);
+ }
else
{
- fi = frame_find_by_id (var->root->frame);
- within_scope = fi != NULL;
- /* FIXME: select_frame could fail */
- if (fi)
+ ptid_t ptid = thread_id_to_pid (var->root->thread_id);
+ if (in_thread_list (ptid))
{
- CORE_ADDR pc = get_frame_pc (fi);
- if (pc < BLOCK_START (var->root->valid_block) ||
- pc >= BLOCK_END (var->root->valid_block))
- within_scope = 0;
- else
- select_frame (fi);
- }
+ switch_to_thread (ptid);
+ within_scope = check_scope (var);
+ }
}
if (within_scope)
return new_val;
}
+ do_cleanups (back_to);
+
return NULL;
}
gdb_assert (varobj_value_is_changeable_p (var));
gdb_assert (!value_lazy (var->value));
- return value_get_print_value (var->value, var->format);
+ return xstrdup (var->print_value);
}
}
}
adjust_value_for_child_access (&value, &type, &was_ptr);
if (TYPE_CODE (type) == TYPE_CODE_STRUCT
- || TYPE_CODE (type) == TYPE_CODE_STRUCT)
+ || TYPE_CODE (type) == TYPE_CODE_UNION)
{
char *join = was_ptr ? "->" : ".";
if (CPLUS_FAKE_CHILD (parent))