This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
[PATCH 08/12] Iterator varobj_items by their availability
- From: Yao Qi <yao at codesourcery dot com>
- To: <gdb-patches at sourceware dot org>
- Date: Fri, 14 Feb 2014 16:44:27 +0800
- Subject: [PATCH 08/12] Iterator varobj_items by their availability
- Authentication-results: sourceware.org; auth=none
- References: <1392367471-13527-1-git-send-email-yao at codesourcery dot com>
This patch adds a new implementation of varobj iterator, which is
based on the availability of children.
V2:
- Fix changelog entry.
- Add comments.
- Use XNEW.
- Fix typo.
- Update copyright year for new added file.
gdb:
2014-02-14 Pedro Alves <pedro@codesourcery.com>
Yao Qi <yao@codesourcery.com>
* Makefile.in (SFILES): Add varobj-iter-avail.c.
(COMMON_OBS): Add varobj-iter-avail.o.
* varobj-iter-avail.c: New file.
* varobj-iter.h (avail_varobj_get_iterator): Declare.
* varobj.c (varobj_get_iterator): Add one more argument
'lang_ops'. All callers updated.
Return iterator for available children.
---
gdb/Makefile.in | 4 +-
gdb/varobj-iter-avail.c | 162 +++++++++++++++++++++++++++++++++++++++++++++++
gdb/varobj-iter.h | 4 +
gdb/varobj.c | 8 ++-
4 files changed, 174 insertions(+), 4 deletions(-)
create mode 100644 gdb/varobj-iter-avail.c
diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 1da4055..88c8f68 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -831,7 +831,7 @@ SFILES = ada-exp.y ada-lang.c ada-typeprint.c ada-valprint.c ada-tasks.c \
ui-out.c utils.c ui-file.h ui-file.c \
user-regs.c \
valarith.c valops.c valprint.c value.c varobj.c common/vec.c \
- xml-tdesc.c xml-support.c \
+ varobj-iter-avail.c xml-tdesc.c xml-support.c \
inferior.c gdb_usleep.c \
record.c record-full.c gcore.c \
jit.c \
@@ -995,7 +995,7 @@ COMMON_OBS = $(DEPFILES) $(CONFIG_OBS) $(YYOBJ) \
ada-lang.o c-lang.o d-lang.o f-lang.o objc-lang.o \
ada-tasks.o ada-varobj.o c-varobj.o \
ui-out.o cli-out.o \
- varobj.o vec.o \
+ varobj.o varobj-iter-avail.o vec.o \
go-lang.o go-valprint.o go-typeprint.o \
jv-lang.o jv-valprint.o jv-typeprint.o jv-varobj.o \
m2-lang.o opencl-lang.o p-lang.o p-typeprint.o p-valprint.o \
diff --git a/gdb/varobj-iter-avail.c b/gdb/varobj-iter-avail.c
new file mode 100644
index 0000000..59d526a
--- /dev/null
+++ b/gdb/varobj-iter-avail.c
@@ -0,0 +1,162 @@
+/* Copyright (C) 2013-2014 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "defs.h"
+#include "gdb_assert.h"
+#include "value.h"
+#include "valprint.h"
+#include "varobj.h"
+#include "varobj-iter.h"
+
+/* A dynamic varobj iterator "class" for available-children-only
+ varobjs. This inherits struct varobj_iter. */
+
+struct avail_varobj_iter
+{
+ /* The 'base class'. */
+ struct varobj_iter base;
+
+ /* Varobj operations related to language. */
+ const struct lang_varobj_ops *lang_ops;
+};
+
+/* Returns true if VAL is not interesting for an
+ available-children-only varobj. */
+
+static int
+varobj_value_unavailable (struct value *val)
+{
+ volatile struct gdb_exception e;
+ int unavail = 0;
+
+ gdb_assert (val != NULL);
+
+ /* value_entirely_unavailable may need to try reading the value,
+ which may throw for example, a memory error, e.g., when not
+ inspecting a traceframe, we try to read the pointee of a dangling
+ or NULL pointer. */
+ TRY_CATCH (e, RETURN_MASK_ERROR)
+ {
+ struct type *type = value_type (val);
+
+ unavail = (value_entirely_unavailable (val)
+ /* A scalar object that does not have all bits available
+ is also considered unavailable, because all bits
+ contribute to its representation. */
+ || (val_print_scalar_type_p (type)
+ && !value_bytes_available (val,
+ value_embedded_offset (val),
+ TYPE_LENGTH (type))));
+ }
+ if (e.reason < 0)
+ return 1;
+ else
+ return unavail;
+}
+
+/* Implementation of the `next' method for available-children-only
+ varobj iterators. */
+
+static varobj_item *
+avail_varobj_iter_next (struct varobj_iter *self)
+{
+ struct avail_varobj_iter *dis = (struct avail_varobj_iter *) self;
+ int num_children = dis->lang_ops->number_of_children (self->var);
+ int raw_index = self->next_raw_index;
+ varobj_item *item = NULL;
+
+ for (; raw_index < num_children; raw_index++)
+ {
+ struct value *child_value
+ = dis->lang_ops->value_of_child (self->var, raw_index);
+
+ /* The "fake" children will have NULL values. */
+ if (child_value == NULL || !varobj_value_unavailable (child_value))
+ {
+ item = xmalloc (sizeof *item);
+
+ item->name
+ = dis->lang_ops->name_of_child (self->var, raw_index);
+ item->value = child_value;
+
+ raw_index++;
+ self->next_raw_index = raw_index;
+ break;
+ }
+ }
+ return item;
+}
+
+/* Implementation of the `dtor' method of available-children-only
+ varobj iterators. */
+
+static void
+avail_varobj_iter_dtor (struct varobj_iter *self)
+{
+ /* nothing to do */
+}
+
+/* The 'vtable' of available-children-only varobj iterators. */
+
+static const struct varobj_iter_ops avail_varobj_iter_ops =
+{
+ avail_varobj_iter_dtor,
+ avail_varobj_iter_next
+};
+
+/* Constructor of available-children-only varobj iterators. VAR is
+ the varobj whose children the iterator will be iterating over. */
+
+static void
+avail_varobj_iter_ctor (struct avail_varobj_iter *self, struct varobj *var,
+ const struct lang_varobj_ops *lang_ops)
+{
+ self->base.var = var;
+ self->base.ops = &avail_varobj_iter_ops;
+ self->base.next_raw_index = 0;
+ self->lang_ops = lang_ops;
+}
+
+/* Allocate and construct an available-children-only varobj iterator.
+ VAR is the varobj whose children the iterator will be iterating
+ over. */
+
+static struct avail_varobj_iter *
+avail_varobj_iter_new (struct varobj *var, const struct lang_varobj_ops *lang_ops)
+{
+ struct avail_varobj_iter *self;
+
+ self = XNEW (struct avail_varobj_iter);
+ avail_varobj_iter_ctor (self, var, lang_ops);
+ return self;
+}
+
+/* Return a new available-children-only varobj iterator suitable to
+ iterate over VAR's children. */
+
+struct varobj_iter *
+avail_varobj_get_iterator (struct varobj *var,
+ const struct lang_varobj_ops *lang_ops)
+{
+ struct avail_varobj_iter *iter;
+
+ /* Avoid the needless allocation/deallocation of the iterator if
+ there are no raw children to iterate over anyway. */
+ if (lang_ops->number_of_children (var) == 0)
+ return NULL;
+
+ iter = avail_varobj_iter_new (var, lang_ops);
+ return &iter->base;
+}
diff --git a/gdb/varobj-iter.h b/gdb/varobj-iter.h
index 9eb672d..ef95a9b 100644
--- a/gdb/varobj-iter.h
+++ b/gdb/varobj-iter.h
@@ -70,3 +70,7 @@ struct varobj_iter_ops
xfree (ITER); \
} \
} while (0)
+
+struct varobj_iter *
+ avail_varobj_get_iterator (struct varobj *var,
+ const struct lang_varobj_ops *lang_ops);
diff --git a/gdb/varobj.c b/gdb/varobj.c
index 61309b2..bfa6b1d 100644
--- a/gdb/varobj.c
+++ b/gdb/varobj.c
@@ -768,8 +768,11 @@ dynamic_varobj_has_child_method (struct varobj *var)
iterator object suitable for iterating over VAR's children. */
static struct varobj_iter *
-varobj_get_iterator (struct varobj *var)
+varobj_get_iterator (struct varobj *var, const struct lang_varobj_ops *lang_ops)
{
+ if (var->dynamic->available_children_only)
+ return avail_varobj_get_iterator (var, lang_ops);
+
#if HAVE_PYTHON
if (var->dynamic->pretty_printer)
return py_varobj_get_iterator (var, var->dynamic->pretty_printer);
@@ -810,7 +813,8 @@ update_dynamic_varobj_children (struct varobj *var,
if (update_children || var->dynamic->child_iter == NULL)
{
varobj_iter_delete (var->dynamic->child_iter);
- var->dynamic->child_iter = varobj_get_iterator (var);
+ var->dynamic->child_iter = varobj_get_iterator (var,
+ var->root->lang_ops);
varobj_clear_saved_item (var->dynamic);
--
1.7.7.6