This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH 03/10] vla: enable sizeof operator to work with variable length arrays


In C99 the sizeof operator computes the size of a variable length array
at runtime (6.5.3.4 The sizeof operator). This patch reflects the semantic
change in the debugger.

We now are able to get the size of a vla:

1| void foo (size_t n) {
2|   int vla[n];
3| }

(gdb) p sizeof(vla)

yields N.

2013-10-18  Sanimir Agovic  <sanimir.agovic@intel.com>
            Keven Boell  <keven.boell@intel.com>

	* eval.c (evaluate_subexp_for_sizeof): If the type passed to sizeof is
	dynamic	evaluate the argument to compute the length.
	* gdbtypes.c (is_dynamic_type): Make extern.
	* gdbtypes.c (is_dynamic_type): Make extern.

Change-Id: I490f695f7a164be5f1f824fe3ba33f805d1ab689
---
 gdb/eval.c     |    8 +++++++-
 gdb/gdbtypes.c |    2 +-
 gdb/gdbtypes.h |    2 ++
 3 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/gdb/eval.c b/gdb/eval.c
index e83bfdf..9e4afa3 100644
--- a/gdb/eval.c
+++ b/gdb/eval.c
@@ -3041,8 +3041,14 @@ evaluate_subexp_for_sizeof (struct expression *exp, int *pos)
       return value_from_longest (size_type, (LONGEST) TYPE_LENGTH (type));
 
     case OP_VAR_VALUE:
-      (*pos) += 4;
       type = check_typedef (SYMBOL_TYPE (exp->elts[pc + 2].symbol));
+      if (is_dynamic_type (type))
+	{
+	  val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_NORMAL);
+	  type = value_type (val);
+	}
+      else
+	(*pos) += 4;
       return
 	value_from_longest (size_type, (LONGEST) TYPE_LENGTH (type));
 
diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
index c0f375d..9774a4b 100644
--- a/gdb/gdbtypes.c
+++ b/gdb/gdbtypes.c
@@ -1624,7 +1624,7 @@ resolve_dynamic_prop (const struct dwarf2_prop *prop, CORE_ADDR address,
 
 /* Predicates if the type has dynamic values, which are not resolved yet.  */
 
-static int
+int
 is_dynamic_type (const struct type *type)
 {
   if (type == NULL)
diff --git a/gdb/gdbtypes.h b/gdb/gdbtypes.h
index 96e0ed7..1252fd1 100644
--- a/gdb/gdbtypes.h
+++ b/gdb/gdbtypes.h
@@ -1580,6 +1580,8 @@ extern struct type *lookup_unsigned_typename (const struct language_defn *,
 extern struct type *lookup_signed_typename (const struct language_defn *,
 					    struct gdbarch *, const char *);
 
+extern int is_dynamic_type (const struct type *type);
+
 extern struct type *resolve_dynamic_type (struct type *, CORE_ADDR);
 
 extern struct type *check_typedef (struct type *);
-- 
1.7.0.7


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]