Improve MI -var-info-path-expression for nested struct/union case.

https://sourceware.org/ml/gdb-patches/2014-05/msg00383.html

The MI command -var-info-path-expression currently does not handle
non-anonymous structs / unions nested within other structs / unions,
it will skip parts of the expression.  Consider this example:

  ## START EXAMPLE ##
  $ cat ex.c
  #include <string.h>

  int
  main ()
  {
    struct s1
    {
      int a;
    };

    struct ss
    {
      struct s1 x;
    };

    struct ss an_ss;
    memset (&an_ss, 0, sizeof (an_ss));
    return 0;
  }
  $ gcc -g -o ex.x ex.c
  $ gdb ex.x
  (gdb) break 18
  Breakpoint 1 at 0x80483ba: file ex.c, line 18.
  (gdb) run
  Starting program: /home/user/ex.x

  Breakpoint 1, main () at ex.c:18
  18	  return 0;
  (gdb) interpreter-exec mi "-var-create an_ss * an_ss"
  (gdb) interpreter-exec mi "-var-list-children an_ss"
  ^done,numchild="1",children=[child={name="an_ss.x",exp="x",numchild="1",type="struct s1",thread-id="1"}],has_more="0"
  (gdb) interpreter-exec mi "-var-list-children an_ss.x"
  ^done,numchild="1",children=[child={name="an_ss.x.a",exp="a",numchild="0",type="int",thread-id="1"}],has_more="0"
  (gdb) interpreter-exec mi "-var-list-children an_ss.x.a"
  ^done,numchild="0",has_more="0"
  (gdb) interpreter-exec mi "-var-info-path-expression an_ss.x.a"
  ^done,path_expr="(an_ss).a"
  (gdb) print (an_ss).a
  There is no member named a.
  ## END EXAMPLE ##

Notice that the path expression returned is wrong, and as a result
the print command fails.

This patch adds a new method to the varobj_ops structure called
is_path_expr_parent, to allow language specific control over finding
the parent varobj, the current logic becomes the C/C++ version and is
extended to handle the nested cases.  No other language currently uses
this code, so all other languages just get a default method.

With this patch, the above example now finishes like this:

  ## START EXAMPLE ##
  $ gdb ex.x
  (gdb) break 18
  Breakpoint 1 at 0x80483ba: file ex.c, line 18.
  (gdb) run
  Starting program: /home/user/ex.x

  Breakpoint 1, main () at ex.c:18
  18	  return 0;
  (gdb) interpreter-exec mi "-var-list-children an_ss"
  ^done,numchild="1",children=[child={name="an_ss.x",exp="x",numchild="1",type="struct s1",thread-id="1"}],has_more="0"
  (gdb) interpreter-exec mi "-var-list-children an_ss.x"
  ^done,numchild="1",children=[child={name="an_ss.x.a",exp="a",numchild="0",type="int",thread-id="1"}],has_more="0"
  (gdb) interpreter-exec mi "-var-list-children an_ss.x.a"
  ^done,numchild="0",has_more="0"
  (gdb) interpreter-exec mi "-var-info-path-expression an_ss.x.a"
  ^done,path_expr="((an_ss).x).a"
  (gdb) print ((an_ss).x).a
  $1 = 0
  ## END EXAMPLE ##

Notice that the path expression is now correct, and the print is a
success.

gdb/ChangeLog:

	* ada-varobj.c (ada_varobj_ops): Fill in is_path_expr_parent
	field.
	* c-varobj.c (c_is_path_expr_parent): New function, moved core
	from varobj.c, with additional checks.
	(c_varobj_ops): Fill in is_path_expr_parent field.
	(cplus_varobj_ops): Fill in is_path_expr_parent field.
	* jv-varobj.c (java_varobj_ops): Fill in is_path_expr_parent
	field.
	* varobj.c (is_path_expr_parent): Call is_path_expr_parent varobj
	ops method.
	(varobj_default_is_path_expr_parent): New function.
	* varobj.h (lang_varobj_ops): Add is_path_expr_parent field.
	(varobj_default_is_path_expr_parent): Declare new function.

gdb/testsuite/ChangeLog:

	* gdb.mi/var-cmd.c (do_nested_struct_union_tests): New function
	setting up test structures.
	(main): Call new test function.
	* gdb.mi/mi2-var-child.exp: Create additional breakpoint in new
	test function, continue into test function and walk test
	structures.
This commit is contained in:
Andrew Burgess
2014-07-07 19:22:36 +01:00
parent e051745c83
commit 9a9a760829
9 changed files with 246 additions and 14 deletions

View File

@@ -126,6 +126,56 @@ adjust_value_for_child_access (struct value **value,
}
}
/* Is VAR a path expression parent, i.e., can it be used to construct
a valid path expression? */
static int
c_is_path_expr_parent (struct varobj *var)
{
struct type *type;
/* "Fake" children are not path_expr parents. */
if (CPLUS_FAKE_CHILD (var))
return 0;
type = varobj_get_gdb_type (var);
/* Anonymous unions and structs are also not path_expr parents. */
if ((TYPE_CODE (type) == TYPE_CODE_STRUCT
|| TYPE_CODE (type) == TYPE_CODE_UNION)
&& TYPE_NAME (type) == NULL
&& TYPE_TAG_NAME (type) == NULL)
{
struct varobj *parent = var->parent;
while (parent != NULL && CPLUS_FAKE_CHILD (parent))
parent = parent->parent;
if (parent != NULL)
{
struct type *parent_type;
int was_ptr;
parent_type = varobj_get_value_type (parent);
adjust_value_for_child_access (NULL, &parent_type, &was_ptr, 0);
if (TYPE_CODE (parent_type) == TYPE_CODE_STRUCT
|| TYPE_CODE (parent_type) == TYPE_CODE_UNION)
{
const char *field_name;
gdb_assert (var->index < TYPE_NFIELDS (parent_type));
field_name = TYPE_FIELD_NAME (parent_type, var->index);
return !(field_name == NULL || *field_name == '\0');
}
}
return 0;
}
return 1;
}
/* C */
static int
@@ -493,7 +543,8 @@ const struct lang_varobj_ops c_varobj_ops =
c_type_of_child,
c_value_of_variable,
varobj_default_value_is_changeable_p,
NULL /* value_has_mutated */
NULL, /* value_has_mutated */
c_is_path_expr_parent /* is_path_expr_parent */
};
/* A little convenience enum for dealing with C++/Java. */
@@ -904,7 +955,8 @@ const struct lang_varobj_ops cplus_varobj_ops =
cplus_type_of_child,
cplus_value_of_variable,
varobj_default_value_is_changeable_p,
NULL /* value_has_mutated */
NULL, /* value_has_mutated */
c_is_path_expr_parent /* is_path_expr_parent */
};