Print nonexisting/optimized out static fields gracefully.
authorPedro Alves <palves@redhat.com>
Fri, 25 Oct 2013 10:37:13 +0000 (11:37 +0100)
committerTom Tromey <tromey@sourceware.org>
Fri, 25 Oct 2013 14:03:01 +0000 (14:03 +0000)
With:

 struct static_struct { static int aaa; };
 struct static_struct sss;
 int main () { return 0; }

We get:

 (gdb) p sss
 $1 = {static aaa = <optimized out>}
 (gdb) p sss.aaa
 field aaa is nonexistent or has been optimized out

Note that the "field aaa ..." message is an error being thrown.

GDB is graceful everywhere else when printing optimized out values.
IOW it usually prints an <optimized out> value and puts that in the
value history.  I see no reason for here to be different, more so that
when the print the whole "containing" object (well, it's a static
field, so it's not really a container), we already print <optimized
out>.

After the patch:

 (gdb) p sss
 $1 = {static aaa = <optimized out>}
 (gdb) p sss.aaa
 $2 = <optimized out>

The value_entirely_optimized_out checks are there to preserve
behavior.  Without those, if the static field is a struct/union, GDB
would go and print its fields one by one (and print <optimized out>
for each).

Tested on x86_64 Fedora 17.

gdb/
2013-10-25  Pedro Alves  <palves@redhat.com>

* cp-valprint.c (cp_print_value_fields): No longer handle a NULL
static field value.
(cp_print_static_field): If the value is entirely optimized out,
print <optimized out> here.
* jv-valprint.c (java_print_value_fields): No longer handle a NULL
static field value.
* p-valprint.c (pascal_object_print_static_field): If the value is
entirely optimized out, print <optimized out> here.
* valops.c (do_search_struct_field)
(value_struct_elt_for_reference): No longer handle a NULL static
field value.
* value.c (value_static_field): Return an optimized out value
instead of NULL.

gdb/testsuite/
2013-10-25  Pedro Alves  <palves@redhat.com>

* gdb.cp/m-static.exp: Adjust expected output of printing a
nonexistent or optimized out static field.  Also test printing the
the "container" object.

gdb/ChangeLog
gdb/cp-valprint.c
gdb/jv-valprint.c
gdb/p-valprint.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.cp/m-static.exp
gdb/valops.c
gdb/value.c

index ec491c55efa6524214e985545edb0f7b0b18cb09..88b8340206b5ea3f8d5488537409466c00329e52 100644 (file)
@@ -1,3 +1,19 @@
+2013-10-25  Pedro Alves  <palves@redhat.com>
+
+       * cp-valprint.c (cp_print_value_fields): No longer handle a NULL
+       static field value.
+       (cp_print_static_field): If the value is entirely optimized out,
+       print <optimized out> here.
+       * jv-valprint.c (java_print_value_fields): No longer handle a NULL
+       static field value.
+       * p-valprint.c (pascal_object_print_static_field): If the value is
+       entirely optimized out, print <optimized out> here.
+       * valops.c (do_search_struct_field)
+       (value_struct_elt_for_reference): No longer handle a NULL static
+       field value.
+       * value.c (value_static_field): Return an optimized out value
+       instead of NULL.
+
 2013-10-25  Yao Qi  <yao@codesourcery.com>
 
        * remote.c (remote_traceframe_info): Return early if
index 1d7147cdd23e4cfb94efeca5532166e692d1193c..4b625d1c4ae57b56938ee72eb76c0c7fc10c91aa 100644 (file)
@@ -333,12 +333,9 @@ cp_print_value_fields (struct type *type, struct type *real_type,
                    fprintf_filtered (stream,
                                      _("<error reading variable: %s>"),
                                      ex.message);
-                 else if (v == NULL)
-                   val_print_optimized_out (NULL, stream);
-                 else
-                   cp_print_static_field (TYPE_FIELD_TYPE (type, i),
-                                          v, stream, recurse + 1,
-                                          options);
+                 cp_print_static_field (TYPE_FIELD_TYPE (type, i),
+                                        v, stream, recurse + 1,
+                                        options);
                }
              else if (i == vptr_fieldno && type == vptr_basetype)
                {
@@ -640,7 +637,13 @@ cp_print_static_field (struct type *type,
                       const struct value_print_options *options)
 {
   struct value_print_options opts;
-  
+
+  if (value_entirely_optimized_out (val))
+    {
+      val_print_optimized_out (val, stream);
+      return;
+    }
+
   if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
     {
       CORE_ADDR *first_dont_print;
index cb89a854008ae483ba1315228b52d1b0d90f1729..2c60cc088ae66d0496e08956f12100f0d42e5e47 100644 (file)
@@ -417,22 +417,16 @@ java_print_value_fields (struct type *type, const gdb_byte *valaddr,
                }
              else if (field_is_static (&TYPE_FIELD (type, i)))
                {
+                 struct value_print_options opts;
                  struct value *v = value_static_field (type, i);
+                 struct type *t = check_typedef (value_type (v));
 
-                 if (v == NULL)
-                   val_print_optimized_out (NULL, stream);
-                 else
-                   {
-                     struct value_print_options opts;
-                     struct type *t = check_typedef (value_type (v));
-
-                     if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
-                       v = value_addr (v);
-                     opts = *options;
-                     opts.deref_ref = 0;
-                     common_val_print (v, stream, recurse + 1,
-                                       &opts, current_language);
-                   }
+                 if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
+                   v = value_addr (v);
+                 opts = *options;
+                 opts.deref_ref = 0;
+                 common_val_print (v, stream, recurse + 1,
+                                   &opts, current_language);
                }
              else if (TYPE_FIELD_TYPE (type, i) == NULL)
                fputs_filtered ("<unknown type>", stream);
index e6d4b91c3adffe0cd925c57475ffc68e99e19e5d..7854bc0c856d72fdb905689bd386bb28e58cf230 100644 (file)
@@ -844,6 +844,12 @@ pascal_object_print_static_field (struct value *val,
   struct type *type = value_type (val);
   struct value_print_options opts;
 
+  if (value_entirely_optimized_out (val))
+    {
+      val_print_optimized_out (val, stream);
+      return;
+    }
+
   if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
     {
       CORE_ADDR *first_dont_print, addr;
index 40391c9bafc1f77e453906a9fc7cc4ca6c300c1c..0c1dbefd17488625ef68d2b9a1a5ba1881267a47 100644 (file)
@@ -1,3 +1,9 @@
+2013-10-25  Pedro Alves  <palves@redhat.com>
+
+       * gdb.cp/m-static.exp: Adjust expected output of printing a
+       nonexistent or optimized out static field.  Also test printing the
+       the "container" object.
+
 2013-10-24  Maciej W. Rozycki  <macro@codesourcery.com>
 
        * lib/gdb.exp (gdb_finish): Send a kill request to `gdbserver'
index 9b0e642d50b4dab05fc6b2123cec34d70934a9cc..a5d388c804e45ee6fe556a90ee78af6fed9f3535 100644 (file)
@@ -169,7 +169,10 @@ if {[test_compiler_info {gcc-[0-3]-*}]
     # and DW_AT_MIPS_linkage_name = _ZN9gnu_obj_47nowhereE .
     setup_xfail *-*-*
 }
-gdb_test "print test4.nowhere" "field nowhere is nonexistent or has been optimized out" "static const int initialized nowhere"
+gdb_test "print test4.nowhere" "<optimized out>" "static const int initialized nowhere (print field)"
+
+# Same, but print the whole struct.
+gdb_test "print test4" "static nowhere = <optimized out>.*" "static const int initialized nowhere (whole struct)"
 
 # static const initialized in the class definition, PR gdb/11702.
 if { $non_dwarf } { setup_xfail *-*-* }
index 15fd7c384be4781c1b8d1ff149bf0fa895d3589d..8bff6868100d2b418243741a23d2347c78d61cab 100644 (file)
@@ -1853,13 +1853,7 @@ do_search_struct_field (const char *name, struct value *arg1, int offset,
            struct value *v;
 
            if (field_is_static (&TYPE_FIELD (type, i)))
-             {
-               v = value_static_field (type, i);
-               if (v == 0)
-                 error (_("field %s is nonexistent or "
-                          "has been optimized out"),
-                        name);
-             }
+             v = value_static_field (type, i);
            else
              v = value_primitive_field (arg1, offset, i, type);
            *result_ptr = v;
@@ -3123,9 +3117,6 @@ value_struct_elt_for_reference (struct type *domain, int offset,
          if (field_is_static (&TYPE_FIELD (t, i)))
            {
              v = value_static_field (t, i);
-             if (v == NULL)
-               error (_("static field %s has been optimized out"),
-                      name);
              if (want_address)
                v = value_addr (v);
              return v;
index d96d285cbdd3ab3a165714498c14d2ea4d10835c..1f562f52d4a934bcfa44882ed5a26704c0b7a40c 100644 (file)
@@ -2590,8 +2590,7 @@ unpack_pointer (struct type *type, const gdb_byte *valaddr)
 
 \f
 /* Get the value of the FIELDNO'th field (which must be static) of
-   TYPE.  Return NULL if the field doesn't exist or has been
-   optimized out.  */
+   TYPE.  */
 
 struct value *
 value_static_field (struct type *type, int fieldno)
@@ -2618,7 +2617,7 @@ value_static_field (struct type *type, int fieldno)
                                                               NULL, NULL);
 
          if (!msym)
-           return NULL;
+           return allocate_optimized_out_value (type);
          else
            {
              retval = value_at_lazy (TYPE_FIELD_TYPE (type, fieldno),
This page took 0.043228 seconds and 4 git commands to generate.