[PATCH 2/2] Add options to skip unavailable locals

Yao Qi
2013-07-03T01:48:57Z

This is the patch to add new option '--skip-unavailable' to MI
commands '-stack-list-{locals, arguments, variables}'. This patch
extends list_args_or_locals to add a new parameter 'skip_unavailable',
and don't list locals or arguments if values are unavailable and
'skip_unavailable' is true.

This is inspecting a trace frame (tfind mode), where only a few
locals have been collected.

-stack-list-locals, no switch vs new switch:

-stack-list-locals --simple-values
^done,locals=[{name="array",type="unsigned char [2]"},{name="i",type="int",value="<unavailable>"}]
-stack-list-locals --skip-unavailable --simple-values
^done,locals=[{name="array",type="unsigned char [2]"}]

-stack-list-arguments, no switch vs new switch:

-stack-list-arguments --simple-values
^done,stack-args=[frame={level="0",args=[{name="j",type="int",value="4"},{name="s",type="char *",value="<unavailable>"}]},frame={level="1",args=[]}]
-stack-list-arguments --skip-unavailable --simple-values
^done,stack-args=[frame={level="0",args=[{name="j",type="int",value="4"}]},frame={level="1",args=[]}]

-stack-list-variables, no switch vs new switch:

-stack-list-variables --simple-values
^done,variables=[{name="j",arg="1",type="int",value="4"},{name="s",arg="1",type="char *",value="<unavailable>"},{name="array",type="unsigned char [2]"},{name="i",type="int",value="<unavailable>"}]
-stack-list-variables --skip-unavailable --simple-values
^done,variables=[{name="j",arg="1",type="int",value="4"},{name="array",type="unsigned char [2]"}]

tests are added to test these new options.

gdb:

2013-07-03 Pedro Alves <pedro@codesourcery.com>
Yao Qi <yao@codesourcery.com>

* mi/mi-cmd-stack.c (list_args_or_locals): Adjust prototype.
(parse_no_frames_option): Remove.
(mi_cmd_stack_list_locals): Handle --skip-unavailable.
(mi_cmd_stack_list_args): Adjust.
(mi_cmd_stack_list_variables): Handle --skip-unavailable.
(list_args_or_locals): New parameter 'skip_unavailable'.
Handle it.
* valprint.c (scalar_type_p): Rename to ...
(val_print_scalar_type_p): ... this. Make extern.
(val_print, value_check_printable): Adjust.
* valprint.h (val_print_scalar_type_p): Declare.
* value.c (value_entirely_unavailable): New function.
* value.h (value_entirely_unavailable): Declare.

* NEWS: Mention the new option "--skip-unavailable" to these
MI commands.

gdb/doc:

2013-07-03 Pedro Alves <pedro@codesourcery.com>
Yao Qi <yao@codesourcery.com>

* gdb.texinfo (GDB/MI Stack Manipulation)<-stack-list-locals>:
Document new --skip-unavailable option.
<-stack-list-arguments>: Likewise.
<-stack-list-variables>: Likewise.

gdb/testsuite:

2013-07-03 Yao Qi <yao@codesourcery.com>

* gdb.trace/mi-trace-unavailable.exp (test_trace_unavailable):
Add tests for new option '--skip-unavailable'.
---
gdb/NEWS | 5 +
gdb/doc/gdb.texinfo | 17 ++-
gdb/mi/mi-cmd-stack.c | 161 ++++++++++++++--------
gdb/testsuite/gdb.trace/mi-trace-unavailable.exp | 9 ++
gdb/valprint.c | 8 +-
gdb/valprint.h | 2 +
gdb/value.c | 20 +++
gdb/value.h | 4 +
8 files changed, 164 insertions(+), 62 deletions(-)

diff --git a/gdb/NEWS b/gdb/NEWS
index e469f1e..8309c8b 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -84,6 +84,11 @@ show range-stepping
** The new command -trace-frame-collected dumps collected variables,
computed expressions, tvars, memory and registers in a traceframe.

+ ** The commands -stack-list-locals, -stack-list-arguments and
+ -stack-list-variables now accept an optional "--skip-unavailable"
+ option. When used, only the available locals or arguments are
+ displayed.
+
* New system-wide configuration scripts
A GDB installation now provides scripts suitable for use as system-wide
configuration scripts for the following systems:
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index d75a3d1..790c9d2 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -31242,7 +31242,7 @@ For a stack with frame levels 0 through 11:
@subsubheading Synopsis

@smallexample
- -stack-list-arguments [ --no-frame-filters ] @var{print-values}
+ -stack-list-arguments [ --no-frame-filters ] [ --skip-unavailable ] @var{print-values}
[ @var{low-frame} @var{high-frame} ]
@end smallexample

@@ -31262,6 +31262,9 @@ type and value for simple data types, and the name and type for arrays,
structures and unions. If the option @code{--no-frame-filters} is
supplied, then Python frame filters will not be executed.

+If the @code{--skip-unavailable} option is specified, arguments that
+have not been available are not listed. Partially available objects
+are still displayed, however.

Use of this command to obtain arguments in a single frame is
deprecated in favor of the @samp{-stack-list-variables} command.
@@ -31446,7 +31449,7 @@ Show a single frame:
@subsubheading Synopsis

@smallexample
- -stack-list-locals [ --no-frame-filters ] @var{print-values}
+ -stack-list-locals [ --no-frame-filters ] [ --skip-unavailable ] @var{print-values}
@end smallexample

Display the local variable names for the selected frame. If
@@ -31460,6 +31463,10 @@ other data types when the user wishes to explore their values in
more detail. If the option @code{--no-frame-filters} is supplied, then
Python frame filters will not be executed.

+If the @code{--skip-unavailable} option is specified, local variables
+that have not been available are not listed. Partially available
+objects are still displayed, however.
+
This command is deprecated in favor of the
@samp{-stack-list-variables} command.

@@ -31490,7 +31497,7 @@ This command is deprecated in favor of the
@subsubheading Synopsis

@smallexample
- -stack-list-variables [ --no-frame-filters ] @var{print-values}
+ -stack-list-variables [ --no-frame-filters ] [ --skip-unavailable ] @var{print-values}
@end smallexample

Display the names of local variables and function arguments for the selected frame. If
@@ -31501,6 +31508,10 @@ type and value for simple data types, and the name and type for arrays,
structures and unions. If the option @code{--no-frame-filters} is
supplied, then Python frame filters will not be executed.

+If the @code{--skip-unavailable} option is specified, then, local
+variables and arguments that have not been collected are not listed.
+Partially available objects are still displayed, however.
+
@subsubheading Example

@smallexample
diff --git a/gdb/mi/mi-cmd-stack.c b/gdb/mi/mi-cmd-stack.c
index 3212c03..1c0464d 100644
--- a/gdb/mi/mi-cmd-stack.c
+++ b/gdb/mi/mi-cmd-stack.c
@@ -39,9 +39,10 @@

enum what_to_list { locals, arguments, all };

-static void list_args_or_locals (enum what_to_list what,
+static void list_args_or_locals (enum what_to_list what,
enum print_values values,
- struct frame_info *fi);
+ struct frame_info *fi,
+ int skip_unavailable);

/* True if we want to allow Python-based frame filters. */
static int frame_filters = 0;
@@ -54,17 +55,6 @@ mi_cmd_enable_frame_filters (char *command, char **argv, int argc)
frame_filters = 1;
}

-/* Parse the --no-frame-filters option in commands where we cannot use
- mi_getopt. */
-static int
-parse_no_frames_option (const char *arg)
-{
- if (arg && (strcmp (arg, "--no-frame-filters") == 0))
- return 1;
-
- return 0;
-}
-
/* Print a list of the stack frames. Args can be none, in which case
we want to print the whole backtrace, or a pair of numbers
specifying the frame numbers at which to start and stop the
@@ -213,6 +203,8 @@ mi_cmd_stack_list_locals (char *command, char **argv, int argc)
enum py_bt_status result = PY_BT_ERROR;
int print_value;
int oind = 0;
+ int skip_unavailable = 0;
+ int i;

if (argc > 1)
{
@@ -221,11 +213,13 @@ mi_cmd_stack_list_locals (char *command, char **argv, int argc)
char **new_argv = xmalloc ((argc - 1) * sizeof (char *));
enum opt
{
- NO_FRAME_FILTERS
+ NO_FRAME_FILTERS,
+ SKIP_UNAVAILABLE,
};
static const struct mi_opt opts[] =
{
{"-no-frame-filters", NO_FRAME_FILTERS, 0},
+ {"-skip-unavailable", SKIP_UNAVAILABLE, 0},
{ 0, 0, 0 }
};

@@ -249,6 +243,8 @@ mi_cmd_stack_list_locals (char *command, char **argv, int argc)
{
case NO_FRAME_FILTERS:
raw_arg = oind;
+ case SKIP_UNAVAILABLE:
+ skip_unavailable = 1;
break;
}
}
@@ -258,7 +254,8 @@ mi_cmd_stack_list_locals (char *command, char **argv, int argc)
/* After the last option is parsed, there should be only
'print-values'. */
if (argc - oind != 1)
- error (_("-stack-list-locals: Usage: [--no-frame-filters] PRINT_VALUES"));
+ error (_("-stack-list-locals: Usage: [--no-frame-filters] "
+ "[--skip-unavailable] PRINT_VALUES"));

frame = get_selected_frame (NULL);
print_value = mi_parse_print_values (argv[oind]);
@@ -275,7 +272,8 @@ mi_cmd_stack_list_locals (char *command, char **argv, int argc)
if "--no-frame-filters" has been specified from the command. */
if (! frame_filters || raw_arg || result == PY_BT_NO_FILTERS)
{
- list_args_or_locals (locals, print_value, frame);
+ list_args_or_locals (locals, print_value, frame,
+ skip_unavailable);
}
}

@@ -295,18 +293,39 @@ mi_cmd_stack_list_args (char *command, char **argv, int argc)
struct ui_out *uiout = current_uiout;
int raw_arg = 0;
enum py_bt_status result = PY_BT_ERROR;
+ int skip_unavailable = 0;
+ int oind = 0;

- if (argc > 0)
- raw_arg = parse_no_frames_option (argv[0]);
+ /* We can't use mi_getopt here, because the number of options is not
+ determined. */
+ for (oind = 0; oind < argc; oind++)
+ {
+ int found = 0;

- if (argc < 1 || (argc > 3 && ! raw_arg) || (argc == 2 && ! raw_arg))
+ if (strcmp (argv[oind], "--no-frame-filters") == 0)
+ {
+ raw_arg = oind + 1;
+ found = 1;
+ }
+ else if (strcmp (argv[oind], "--skip-unavailable") == 0)
+ {
+ skip_unavailable = 1;
+ found = 1;
+ }
+
+ if (!found)
+ break;
+ }
+
+ if (argc - oind != 1 && argc - oind != 3)
error (_("-stack-list-arguments: Usage: " \
- "[--no-frame-filters] PRINT_VALUES [FRAME_LOW FRAME_HIGH]"));
+ "[--no-frame-filters] [--skip-unavailable] "
+ "PRINT_VALUES [FRAME_LOW FRAME_HIGH]"));

- if (argc >= 3)
+ if (argc - oind == 3)
{
- frame_low = atoi (argv[1 + raw_arg]);
- frame_high = atoi (argv[2 + raw_arg]);
+ frame_low = atoi (argv[1 + oind]);
+ frame_high = atoi (argv[2 + oind]);
}
else
{
@@ -316,7 +335,7 @@ mi_cmd_stack_list_args (char *command, char **argv, int argc)
frame_high = -1;
}

- print_values = mi_parse_print_values (argv[raw_arg]);
+ print_values = mi_parse_print_values (argv[oind]);

/* Let's position fi on the frame at which to start the
display. Could be the innermost frame if the whole stack needs
@@ -362,7 +381,8 @@ mi_cmd_stack_list_args (char *command, char **argv, int argc)
QUIT;
cleanup_frame = make_cleanup_ui_out_tuple_begin_end (uiout, "frame");
ui_out_field_int (uiout, "level", i);
- list_args_or_locals (arguments, print_values, fi);
+ list_args_or_locals (arguments, print_values, fi,
+ skip_unavailable);
do_cleanups (cleanup_frame);
}
}
@@ -382,6 +402,7 @@ mi_cmd_stack_list_variables (char *command, char **argv, int argc)
enum py_bt_status result = PY_BT_ERROR;
int print_value;
int oind = 0;
+ int skip_unavailable = 0;

if (argc > 1)
{
@@ -390,11 +411,13 @@ mi_cmd_stack_list_variables (char *command, char **argv, int argc)
char **new_argv = xmalloc ((argc - 1) * sizeof (char *));
enum opt
{
- NO_FRAME_FILTERS
+ NO_FRAME_FILTERS,
+ SKIP_UNAVAILABLE,
};
static const struct mi_opt opts[] =
{
{"-no-frame-filters", NO_FRAME_FILTERS, 0},
+ {"-skip-unavailable", SKIP_UNAVAILABLE, 0},
{ 0, 0, 0 }
};
cleanup = make_cleanup (xfree, new_argv);
@@ -418,6 +441,9 @@ mi_cmd_stack_list_variables (char *command, char **argv, int argc)
case NO_FRAME_FILTERS:
raw_arg = oind;
break;
+ case SKIP_UNAVAILABLE:
+ skip_unavailable = 1;
+ break;
}
}
do_cleanups (cleanup);
@@ -426,8 +452,8 @@ mi_cmd_stack_list_variables (char *command, char **argv, int argc)
/* After the last option is parsed, there should be only
'print-values'. */
if (argc - oind != 1)
- error (_("-stack-list-variables: Usage: " \
- "[--no-frame-filters] PRINT_VALUES"));
+ error (_("-stack-list-variables: Usage: [--no-frame-filters] " \
+ "[--skip-unavailable] PRINT_VALUES"));

frame = get_selected_frame (NULL);
print_value = mi_parse_print_values (argv[oind]);
@@ -444,7 +470,8 @@ mi_cmd_stack_list_variables (char *command, char **argv, int argc)
if "--no-frame-filters" has been specified from the command. */
if (! frame_filters || raw_arg || result == PY_BT_NO_FILTERS)
{
- list_args_or_locals (all, print_value, frame);
+ list_args_or_locals (all, print_value, frame,
+ skip_unavailable);
}
}

@@ -525,11 +552,12 @@ list_arg_or_local (const struct frame_arg *arg, enum what_to_list what,
/* Print a list of the locals or the arguments for the currently
selected frame. If the argument passed is 0, printonly the names
of the variables, if an argument of 1 is passed, print the values
- as well. */
+ as well. If SKIP_UNAVAILABLE is true, print the arguments or local
+ variables whose values are available. */

static void
list_args_or_locals (enum what_to_list what, enum print_values values,
- struct frame_info *fi)
+ struct frame_info *fi, int skip_unavailable)
{
struct block *block;
struct symbol *sym;
@@ -564,6 +592,7 @@ list_args_or_locals (enum what_to_list what, enum print_values values,
ALL_BLOCK_SYMBOLS (block, iter, sym)
{
int print_me = 0;
+ struct value *val = NULL;

switch (SYMBOL_CLASS (sym))
{
@@ -597,7 +626,6 @@ list_args_or_locals (enum what_to_list what, enum print_values values,
if (print_me)
{
struct symbol *sym2;
- struct frame_arg arg, entryarg;

if (SYMBOL_IS_ARGUMENT (sym))
sym2 = lookup_symbol (SYMBOL_LINKAGE_NAME (sym),
@@ -607,33 +635,56 @@ list_args_or_locals (enum what_to_list what, enum print_values values,
sym2 = sym;
gdb_assert (sym2 != NULL);

- memset (&arg, 0, sizeof (arg));
- arg.sym = sym2;
- arg.entry_kind = print_entry_values_no;
- memset (&entryarg, 0, sizeof (entryarg));
- entryarg.sym = sym2;
- entryarg.entry_kind = print_entry_values_no;
-
- switch (values)
+ /* Need to read the value before being able to determine
+ whether its unavailable. */
+ if (values == PRINT_ALL_VALUES
+ || values == PRINT_SIMPLE_VALUES
+ || skip_unavailable)
+ val = read_var_value (sym2, fi);
+
+ if (skip_unavailable
+ && (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 (value_type (val))
+ && !value_bytes_available (val,
+ value_embedded_offset (val),
+ TYPE_LENGTH (value_type (val))))))
+ ;
+ else
{
- case PRINT_SIMPLE_VALUES:
- type = check_typedef (sym2->type);
- if (TYPE_CODE (type) != TYPE_CODE_ARRAY
- && TYPE_CODE (type) != TYPE_CODE_STRUCT
- && TYPE_CODE (type) != TYPE_CODE_UNION)
+ struct frame_arg arg, entryarg;
+
+ memset (&arg, 0, sizeof (arg));
+ arg.sym = sym2;
+ arg.entry_kind = print_entry_values_no;
+ memset (&entryarg, 0, sizeof (entryarg));
+ entryarg.sym = sym2;
+ entryarg.entry_kind = print_entry_values_no;
+
+ switch (values)
{
- case PRINT_ALL_VALUES:
- read_frame_arg (sym2, fi, &arg, &entryarg);
+ case PRINT_SIMPLE_VALUES:
+ type = check_typedef (sym2->type);
+ if (TYPE_CODE (type) != TYPE_CODE_ARRAY
+ && TYPE_CODE (type) != TYPE_CODE_STRUCT
+ && TYPE_CODE (type) != TYPE_CODE_UNION)
+ {
+ case PRINT_ALL_VALUES:
+ read_frame_arg (sym2, fi, &arg, &entryarg);
+ }
+ break;
}
- break;
- }

- if (arg.entry_kind != print_entry_values_only)
- list_arg_or_local (&arg, what, values);
- if (entryarg.entry_kind != print_entry_values_no)
- list_arg_or_local (&entryarg, what, values);
- xfree (arg.error);
- xfree (entryarg.error);
+ if (arg.entry_kind != print_entry_values_only)
+ list_arg_or_local (&arg, what, values);
+ if (entryarg.entry_kind != print_entry_values_no)
+ list_arg_or_local (&entryarg, what, values);
+ xfree (arg.error);
+ xfree (entryarg.error);
+ }
}
}

diff --git a/gdb/testsuite/gdb.trace/mi-trace-unavailable.exp b/gdb/testsuite/gdb.trace/mi-trace-unavailable.exp
index 42f6e32..f147899 100644
--- a/gdb/testsuite/gdb.trace/mi-trace-unavailable.exp
+++ b/gdb/testsuite/gdb.trace/mi-trace-unavailable.exp
@@ -93,16 +93,25 @@ proc test_trace_unavailable { data_source } {
mi_gdb_test "-stack-list-locals --simple-values" \
".*\\^done,locals=\\\[\{name=\"array\",type=\"unsigned char \\\[2\\\]\"\},\{name=\"i\",type=\"int\",value=\"<unavailable>\"\}\\\]" \
"-stack-list-locals --simple-values"
+ mi_gdb_test "-stack-list-locals --skip-unavailable --simple-values" \
+ ".*\\^done,locals=\\\[\{name=\"array\",type=\"unsigned char \\\[2\\\]\"\}\\\]" \
+ "-stack-list-locals --skip-unavailable --simple-values"

# Test MI command '-stack-list-arguments'.
mi_gdb_test "-stack-list-arguments --simple-values" \
".*\\^done,stack-args=\\\[frame=\{level=\"0\",args=\\\[\{name=\"j\",type=\"int\",value=\"4\"\},\{name=\"s\",type=\"char \\\*\",value=\"<unavailable>\"\}\\\]\},.*\}.*" \
"-stack-list-arguments --simple-values"
+ mi_gdb_test "-stack-list-arguments --skip-unavailable --simple-values" \
+ ".*\\^done,stack-args=\\\[frame=\{level=\"0\",args=\\\[\{name=\"j\",type=\"int\",value=\"4\"\}\\\]\},.*\}.*" \
+ "-stack-list-arguments --skip-unavailable --simple-values"

# Test MI command '-stack-list-variables'.
mi_gdb_test "-stack-list-variables --simple-values" \
".*\\^done,variables=\\\[\{name=\"j\",arg=\"1\",type=\"int\",value=\"4\"\},\{name=\"s\",arg=\"1\",type=\"char \\\*\",value=\"<unavailable>\"\},\{name=\"array\",type=\"unsigned char \\\[2\\\]\"\},\{name=\"i\",type=\"int\",value=\"<unavailable>\"\}\\\]" \
"-stack-list-variables --simple-values"
+ mi_gdb_test "-stack-list-variables --skip-unavailable --simple-values" \
+ ".*\\^done,variables=\\\[\{name=\"j\",arg=\"1\",type=\"int\",value=\"4\"\},\{name=\"array\",type=\"unsigned char \\\[2\\\]\"\}\\\]" \
+ "-stack-list-variables --skip-unavailable --simple-values"

mi_gdb_test "-trace-find frame-number 1" \
".*\\^done,found=\"1\",tracepoint=\"${decimal}\",traceframe=\"1\",frame=\{.*" \
diff --git a/gdb/valprint.c b/gdb/valprint.c
index bc21f4f..2368ea8 100644
--- a/gdb/valprint.c
+++ b/gdb/valprint.c
@@ -272,8 +272,8 @@ show_symbol_print (struct ui_file *file, int from_tty,
we want to print scalar arguments, but not aggregate arguments.
This function distinguishes between the two. */

-static int
-scalar_type_p (struct type *type)
+int
+val_print_scalar_type_p (struct type *type)
{
CHECK_TYPEDEF (type);
while (TYPE_CODE (type) == TYPE_CODE_REF)
@@ -767,7 +767,7 @@ val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,

/* Handle summary mode. If the value is a scalar, print it;
otherwise, print an ellipsis. */
- if (options->summary && !scalar_type_p (type))
+ if (options->summary && !val_print_scalar_type_p (type))
{
fprintf_filtered (stream, "...");
return;
@@ -799,7 +799,7 @@ value_check_printable (struct value *val, struct ui_file *stream,

if (value_entirely_optimized_out (val))
{
- if (options->summary && !scalar_type_p (value_type (val)))
+ if (options->summary && !val_print_scalar_type_p (value_type (val)))
fprintf_filtered (stream, "...");
else
val_print_optimized_out (stream);
diff --git a/gdb/valprint.h b/gdb/valprint.h
index 7baef2f..eeb0cc4 100644
--- a/gdb/valprint.h
+++ b/gdb/valprint.h
@@ -209,4 +209,6 @@ extern void generic_printstr (struct ui_file *stream, struct type *type,

extern void output_command_const (const char *args, int from_tty);

+extern int val_print_scalar_type_p (struct type *type);
+
#endif
diff --git a/gdb/value.c b/gdb/value.c
index fae8b98..a1b9742 100644
--- a/gdb/value.c
+++ b/gdb/value.c
@@ -352,6 +352,26 @@ value_entirely_available (struct value *value)
return 0;
}

+int
+value_entirely_unavailable (struct value *value)
+{
+ /* We can only tell whether the whole value is available when we try
+ to read it. */
+ if (value->lazy)
+ value_fetch_lazy (value);
+
+ if (VEC_length (range_s, value->unavailable) == 1)
+ {
+ struct range *t = VEC_index (range_s, value->unavailable, 0);
+
+ if (t->offset == 0
+ && t->length == TYPE_LENGTH (value_enclosing_type (value)))
+ return 1;
+ }
+
+ return 0;
+}
+
void
mark_value_bytes_unavailable (struct value *value, int offset, int length)
{
diff --git a/gdb/value.h b/gdb/value.h
index 8a66aa4..c224f10 100644
--- a/gdb/value.h
+++ b/gdb/value.h
@@ -429,6 +429,10 @@ extern int value_bytes_available (const struct value *value,
whole object is unavailable. */
extern int value_entirely_available (struct value *value);

+/* Like value_entirely_available, but return false if any byte in the
+ whole object is available. */
+extern int value_entirely_unavailable (struct value *value);
+
/* Mark VALUE's content bytes starting at OFFSET and extending for
LENGTH bytes as unavailable. */

--
1.7.7.6