aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPedro Alves <palves@redhat.com>2019-06-13 00:06:53 +0100
committerPedro Alves <palves@redhat.com>2019-06-13 00:18:24 +0100
commit7d8062de98203eeec70d4439ab460b9ef50a2e0f (patch)
tree3ac498d1e7a836b503e4a178ca35062255986f09 /gdb/valprint.c
parentIntroduce generic command options framework (diff)
downloadbinutils-gdb-7d8062de98203eeec70d4439ab460b9ef50a2e0f.tar.gz
binutils-gdb-7d8062de98203eeec70d4439ab460b9ef50a2e0f.tar.bz2
binutils-gdb-7d8062de98203eeec70d4439ab460b9ef50a2e0f.zip
Make "print" and "compile print" support -OPT options
This patch adds support for "print -option optval --", etc. Likewise for "compile print". We'll get: ~~~~~~ (gdb) help print Print value of expression EXP. Usage: print [[OPTION]... --] [/FMT] [EXP] Options: -address [on|off] Set printing of addresses. -array [on|off] Set pretty formatting of arrays. -array-indexes [on|off] Set printing of array indexes. -elements NUMBER|unlimited Set limit on string chars or array elements to print. "unlimited" causes there to be no limit. -max-depth NUMBER|unlimited Set maximum print depth for nested structures, unions and arrays. When structures, unions, or arrays are nested beyond this depth then they will be replaced with either '{...}' or '(...)' depending on the language. Use "unlimited" to print the complete structure. -null-stop [on|off] Set printing of char arrays to stop at first null char. -object [on|off] Set printing of C++ virtual function tables. -pretty [on|off] Set pretty formatting of structures. -repeats NUMBER|unlimited Set threshold for repeated print elements. "unlimited" causes all elements to be individually printed. -static-members [on|off] Set printing of C++ static members. -symbol [on|off] Set printing of symbol names when printing pointers. -union [on|off] Set printing of unions interior to structures. -vtbl [on|off] Set printing of C++ virtual function tables. Note: because this command accepts arbitrary expressions, if you specify any command option, you must use a double dash ("--") to mark the end of option processing. E.g.: "print -o -- myobj". ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ I want to highlight the comment above about "--". At first, I thought we could make the print command parse the options, and if the option wasn't recognized, fallback to parsing as an expression. Then, if the user wanted to disambiguate, he'd use the "--" option delimiter. For example, if you had a variable called "object" and you wanted to print its negative, you'd have to do: (gdb) print -- -object After getting that working, I saw that gdb.pascal/floats.exp regressed, in these tests: gdb_test "print -r" " = -1\\.2(499.*|5|500.*)" gdb_test "print -(r)" " = -1.2(499.*|5|500.*)" gdb_test "print -(r + s)" " = -3\\.4(499.*|5|500.*)" It's the first one that I found most concerning. It regressed because "-r" is the abbreviation of "-raw". I realized then that the behavior change was a bit risker than I'd like, considering scripts, wrappers around gdb, etc., and even user expectation. So instead, I made the print command _require_ the "--" options delimiter if you want to specify any option. So: (gdb) print -r is parsed as an expression, and (gdb) print -r -- is parsed as an option. I noticed that that's also what lldb's expr (the equivalent of print) does to handle the same problem. Going back the options themselves, note that: - you can shorten option names, as long as unambiguous. - For boolean options, 0/1 stand for off/on. - For boolean options, "true" is implied. So these are all equivalent: (gdb) print -object on -static-members off -pretty on -- foo (gdb) print -object -static-members off -pretty -- foo (gdb) print -object -static-members 0 -pretty -- foo (gdb) print -o -st 0 -p -- foo TAB completion is fully supported: (gdb) p -[TAB] -address -elements -pretty -symbol -array -null-stop -repeats -union -array-indexes -object -static-members -vtbl Note that the code is organized such that some of the options and the "set/show" commands code is shared. In particular, the "print" options and the corresponding "set print" commands are defined with the same structures. The commands are installed with the gdb::option::add_setshow_cmds_for_options function. gdb/ChangeLog: 2019-06-13 Pedro Alves <palves@redhat.com> * compile/compile.c: Include "cli/cli-option.h". (compile_print_value): Scope data pointer is now a value_print_options pointer; adjust. (compile_print_command): Process options. Scope data pointer is now a value_print_options pointer; adjust. (_initialize_compile): Update "compile print"'s help to include supported options. Install a completer for "compile print". * cp-valprint.c (show_vtblprint, show_objectprint) (show_static_field_print): Delete. (_initialize_cp_valprint): Don't install "set print static-members", "set print vtbl", "set print object" here. * printcmd.c: Include "cli/cli-option.h" and "common/gdb_optional.h". (print_command_parse_format): Rework to fill in a value_print_options instead of a format_data. (print_value): Change parameter type from format_data pointer to value_print_options reference. Adjust. (print_command_1): Process options. Adjust to pass down a value_print_options. (print_command_completer): New. (_initialize_printcmd): Install print_command_completer as handle_brkchars completer for the "print" command. Update "print"'s help to include supported options. * valprint.c: Include "cli/cli-option.h". (show_vtblprint, show_objectprint, show_static_field_print): Moved here from cp-valprint.c. (boolean_option_def, uinteger_option_def) (value_print_option_defs, make_value_print_options_def_group): New. Use gdb::option::add_setshow_cmds_for_options to install "set print elements", "set print null-stop", "set print repeats", "set print pretty", "set print union", "set print array", "set print address", "set print symbol", "set print array-indexes". * valprint.h: Include <string> and "cli/cli-option.h". (make_value_print_options_def_group): Declare. (print_value): Change parameter type from format_data pointer to value_print_options reference. (print_command_completer): Declare. gdb/testsuite/ChangeLog: 2019-06-13 Pedro Alves <palves@redhat.com> * gdb.base/options.exp: Build executable. (test-print): New procedure. (top level): Call it, once for "print" and another for "compile print".
Diffstat (limited to 'gdb/valprint.c')
-rw-r--r--gdb/valprint.c259
1 files changed, 178 insertions, 81 deletions
diff --git a/gdb/valprint.c b/gdb/valprint.c
index 4c3d67a9ff8..e3197e69192 100644
--- a/gdb/valprint.c
+++ b/gdb/valprint.c
@@ -36,6 +36,7 @@
#include <ctype.h>
#include <algorithm>
#include "common/byte-vector.h"
+#include "cli/cli-option.h"
/* Maximum number of wchars returned from wchar_iterate. */
#define MAX_WCHARS 4
@@ -3069,7 +3070,181 @@ show_print_raw (const char *args, int from_tty)
cmd_show_list (showprintrawlist, from_tty, "");
}
+/* Controls printing of vtbl's. */
+static void
+show_vtblprint (struct ui_file *file, int from_tty,
+ struct cmd_list_element *c, const char *value)
+{
+ fprintf_filtered (file, _("\
+Printing of C++ virtual function tables is %s.\n"),
+ value);
+}
+
+/* Controls looking up an object's derived type using what we find in
+ its vtables. */
+static void
+show_objectprint (struct ui_file *file, int from_tty,
+ struct cmd_list_element *c,
+ const char *value)
+{
+ fprintf_filtered (file, _("\
+Printing of object's derived type based on vtable info is %s.\n"),
+ value);
+}
+
+static void
+show_static_field_print (struct ui_file *file, int from_tty,
+ struct cmd_list_element *c,
+ const char *value)
+{
+ fprintf_filtered (file,
+ _("Printing of C++ static members is %s.\n"),
+ value);
+}
+
+
+/* A couple typedefs to make writing the options a bit more
+ convenient. */
+using boolean_option_def
+ = gdb::option::boolean_option_def<value_print_options>;
+using uinteger_option_def
+ = gdb::option::uinteger_option_def<value_print_options>;
+using zuinteger_unlimited_option_def
+ = gdb::option::zuinteger_unlimited_option_def<value_print_options>;
+
+/* Definions of options for the "print" and "compile print"
+ commands. */
+static const gdb::option::option_def value_print_option_defs[] = {
+
+ boolean_option_def {
+ "address",
+ [] (value_print_options *opt) { return &opt->addressprint; },
+ show_addressprint, /* show_cmd_cb */
+ N_("Set printing of addresses."),
+ N_("Show printing of addresses."),
+ NULL, /* help_doc */
+ },
+
+ boolean_option_def {
+ "array",
+ [] (value_print_options *opt) { return &opt->prettyformat_arrays; },
+ show_prettyformat_arrays, /* show_cmd_cb */
+ N_("Set pretty formatting of arrays."),
+ N_("Show pretty formatting of arrays."),
+ NULL, /* help_doc */
+ },
+
+ boolean_option_def {
+ "array-indexes",
+ [] (value_print_options *opt) { return &opt->print_array_indexes; },
+ show_print_array_indexes, /* show_cmd_cb */
+ N_("Set printing of array indexes."),
+ N_("Show printing of array indexes"),
+ NULL, /* help_doc */
+ },
+
+ uinteger_option_def {
+ "elements",
+ [] (value_print_options *opt) { return &opt->print_max; },
+ show_print_max, /* show_cmd_cb */
+ N_("Set limit on string chars or array elements to print."),
+ N_("Show limit on string chars or array elements to print."),
+ N_("\"unlimited\" causes there to be no limit."),
+ },
+
+ zuinteger_unlimited_option_def {
+ "max-depth",
+ [] (value_print_options *opt) { return &opt->max_depth; },
+ show_print_max_depth, /* show_cmd_cb */
+ N_("Set maximum print depth for nested structures, unions and arrays."),
+ N_("Show maximum print depth for nested structures, unions, and arrays."),
+ N_("When structures, unions, or arrays are nested beyond this depth then they\n\
+will be replaced with either '{...}' or '(...)' depending on the language.\n\
+Use \"unlimited\" to print the complete structure.")
+ },
+
+ boolean_option_def {
+ "null-stop",
+ [] (value_print_options *opt) { return &opt->stop_print_at_null; },
+ show_stop_print_at_null, /* show_cmd_cb */
+ N_("Set printing of char arrays to stop at first null char."),
+ N_("Show printing of char arrays to stop at first null char."),
+ NULL, /* help_doc */
+ },
+
+ boolean_option_def {
+ "object",
+ [] (value_print_options *opt) { return &opt->objectprint; },
+ show_objectprint, /* show_cmd_cb */
+ _("Set printing of C++ virtual function tables."),
+ _("Show printing of C++ virtual function tables."),
+ NULL, /* help_doc */
+ },
+
+ boolean_option_def {
+ "pretty",
+ [] (value_print_options *opt) { return &opt->prettyformat_structs; },
+ show_prettyformat_structs, /* show_cmd_cb */
+ N_("Set pretty formatting of structures."),
+ N_("Show pretty formatting of structures."),
+ NULL, /* help_doc */
+ },
+
+ uinteger_option_def {
+ "repeats",
+ [] (value_print_options *opt) { return &opt->repeat_count_threshold; },
+ show_repeat_count_threshold, /* show_cmd_cb */
+ N_("Set threshold for repeated print elements."),
+ N_("Show threshold for repeated print elements."),
+ N_("\"unlimited\" causes all elements to be individually printed."),
+ },
+
+ boolean_option_def {
+ "static-members",
+ [] (value_print_options *opt) { return &opt->static_field_print; },
+ show_static_field_print, /* show_cmd_cb */
+ N_("Set printing of C++ static members."),
+ N_("Show printing of C++ static members."),
+ NULL, /* help_doc */
+ },
+
+ boolean_option_def {
+ "symbol",
+ [] (value_print_options *opt) { return &opt->symbol_print; },
+ show_symbol_print, /* show_cmd_cb */
+ N_("Set printing of symbol names when printing pointers."),
+ N_("Show printing of symbol names when printing pointers."),
+ NULL, /* help_doc */
+ },
+
+ boolean_option_def {
+ "union",
+ [] (value_print_options *opt) { return &opt->unionprint; },
+ show_unionprint, /* show_cmd_cb */
+ N_("Set printing of unions interior to structures."),
+ N_("Show printing of unions interior to structures."),
+ NULL, /* help_doc */
+ },
+
+ boolean_option_def {
+ "vtbl",
+ [] (value_print_options *opt) { return &opt->vtblprint; },
+ show_vtblprint, /* show_cmd_cb */
+ N_("Set printing of C++ virtual function tables."),
+ N_("Show printing of C++ virtual function tables."),
+ NULL, /* help_doc */
+ },
+};
+
+/* See valprint.h. */
+
+gdb::option::option_def_group
+make_value_print_options_def_group (value_print_options *opts)
+{
+ return {{value_print_option_defs}, opts};
+}
+
void
_initialize_valprint (void)
{
@@ -3094,71 +3269,9 @@ Generic command for setting what things to print in \"raw\" mode."),
_("Generic command for showing \"print raw\" settings."),
&showprintrawlist, "show print raw ", 0, &showprintlist);
- add_setshow_uinteger_cmd ("elements", no_class,
- &user_print_options.print_max, _("\
-Set limit on string chars or array elements to print."), _("\
-Show limit on string chars or array elements to print."), _("\
-\"set print elements unlimited\" causes there to be no limit."),
- NULL,
- show_print_max,
- &setprintlist, &showprintlist);
-
- add_setshow_boolean_cmd ("null-stop", no_class,
- &user_print_options.stop_print_at_null, _("\
-Set printing of char arrays to stop at first null char."), _("\
-Show printing of char arrays to stop at first null char."), NULL,
- NULL,
- show_stop_print_at_null,
- &setprintlist, &showprintlist);
-
- add_setshow_uinteger_cmd ("repeats", no_class,
- &user_print_options.repeat_count_threshold, _("\
-Set threshold for repeated print elements."), _("\
-Show threshold for repeated print elements."), _("\
-\"set print repeats unlimited\" causes all elements to be individually printed."),
- NULL,
- show_repeat_count_threshold,
- &setprintlist, &showprintlist);
-
- add_setshow_boolean_cmd ("pretty", class_support,
- &user_print_options.prettyformat_structs, _("\
-Set pretty formatting of structures."), _("\
-Show pretty formatting of structures."), NULL,
- NULL,
- show_prettyformat_structs,
- &setprintlist, &showprintlist);
-
- add_setshow_boolean_cmd ("union", class_support,
- &user_print_options.unionprint, _("\
-Set printing of unions interior to structures."), _("\
-Show printing of unions interior to structures."), NULL,
- NULL,
- show_unionprint,
- &setprintlist, &showprintlist);
-
- add_setshow_boolean_cmd ("array", class_support,
- &user_print_options.prettyformat_arrays, _("\
-Set pretty formatting of arrays."), _("\
-Show pretty formatting of arrays."), NULL,
- NULL,
- show_prettyformat_arrays,
- &setprintlist, &showprintlist);
-
- add_setshow_boolean_cmd ("address", class_support,
- &user_print_options.addressprint, _("\
-Set printing of addresses."), _("\
-Show printing of addresses."), NULL,
- NULL,
- show_addressprint,
- &setprintlist, &showprintlist);
-
- add_setshow_boolean_cmd ("symbol", class_support,
- &user_print_options.symbol_print, _("\
-Set printing of symbol names when printing pointers."), _("\
-Show printing of symbol names when printing pointers."),
- NULL, NULL,
- show_symbol_print,
- &setprintlist, &showprintlist);
+ gdb::option::add_setshow_cmds_for_options
+ (class_support, &user_print_options, value_print_option_defs,
+ &setprintlist, &showprintlist);
add_setshow_zuinteger_cmd ("input-radix", class_support, &input_radix_1,
_("\
@@ -3192,20 +3305,4 @@ Without an argument, sets both radices back to the default value of 10."),
Show the default input and output number radices.\n\
Use 'show input-radix' or 'show output-radix' to independently show each."),
&showlist);
-
- add_setshow_boolean_cmd ("array-indexes", class_support,
- &user_print_options.print_array_indexes, _("\
-Set printing of array indexes."), _("\
-Show printing of array indexes"), NULL, NULL, show_print_array_indexes,
- &setprintlist, &showprintlist);
-
- add_setshow_zuinteger_unlimited_cmd ("max-depth", class_support,
- &user_print_options.max_depth, _("\
-Set maximum print depth for nested structures, unions and arrays."), _("\
-Show maximum print depth for nested structures, unions, and arrays."), _("\
-When structures, unions, or arrays are nested beyond this depth then they\n\
-will be replaced with either '{...}' or '(...)' depending on the language.\n\
-Use 'set print max-depth unlimited' to print the complete structure."),
- NULL, show_print_max_depth,
- &setprintlist, &showprintlist);
}