aboutsummaryrefslogtreecommitdiff
path: root/gdb/compile/compile.c
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/compile/compile.c
parent9d0faba9f52b898f0be539bc4d6fbd084772259d (diff)
downloadgdb-7d8062de98203eeec70d4439ab460b9ef50a2e0f.zip
gdb-7d8062de98203eeec70d4439ab460b9ef50a2e0f.tar.gz
gdb-7d8062de98203eeec70d4439ab460b9ef50a2e0f.tar.bz2
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/compile/compile.c')
-rw-r--r--gdb/compile/compile.c46
1 files changed, 34 insertions, 12 deletions
diff --git a/gdb/compile/compile.c b/gdb/compile/compile.c
index 7292064..6693809 100644
--- a/gdb/compile/compile.c
+++ b/gdb/compile/compile.c
@@ -23,6 +23,7 @@
#include "command.h"
#include "cli/cli-script.h"
#include "cli/cli-utils.h"
+#include "cli/cli-option.h"
#include "completer.h"
#include "gdbcmd.h"
#include "compile.h"
@@ -328,9 +329,9 @@ compile_code_command (const char *arg, int from_tty)
void
compile_print_value (struct value *val, void *data_voidp)
{
- const struct format_data *fmtp = (const struct format_data *) data_voidp;
+ const value_print_options *print_opts = (value_print_options *) data_voidp;
- print_value (val, fmtp);
+ print_value (val, *print_opts);
}
/* Handle the input from the 'compile print' command. The "compile
@@ -342,22 +343,30 @@ static void
compile_print_command (const char *arg, int from_tty)
{
enum compile_i_scope_types scope = COMPILE_I_PRINT_ADDRESS_SCOPE;
- struct format_data fmt;
+ value_print_options print_opts;
scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
- /* Passing &FMT as SCOPE_DATA is safe as do_module_cleanup will not
- touch the stale pointer if compile_object_run has already quit. */
- print_command_parse_format (&arg, "compile print", &fmt);
+ get_user_print_options (&print_opts);
+ /* Override global settings with explicit options, if any. */
+ auto group = make_value_print_options_def_group (&print_opts);
+ gdb::option::process_options
+ (&arg, gdb::option::PROCESS_OPTIONS_REQUIRE_DELIMITER, group);
+
+ print_command_parse_format (&arg, "compile print", &print_opts);
+
+ /* Passing &PRINT_OPTS as SCOPE_DATA is safe as do_module_cleanup
+ will not touch the stale pointer if compile_object_run has
+ already quit. */
if (arg && *arg)
- eval_compile_command (NULL, arg, scope, &fmt);
+ eval_compile_command (NULL, arg, scope, &print_opts);
else
{
counted_command_line l = get_command_line (compile_control, "");
l->control_u.compile.scope = scope;
- l->control_u.compile.scope_data = &fmt;
+ l->control_u.compile.scope_data = &print_opts;
execute_control_command_untraced (l.get ());
}
}
@@ -946,11 +955,19 @@ Usage: compile file [-r|-raw] [FILENAME]\n\
&compile_command_list);
set_cmd_completer (c, filename_completer);
- add_cmd ("print", class_obscure, compile_print_command,
- _("\
+ const auto compile_print_opts = make_value_print_options_def_group (nullptr);
+
+ static const std::string compile_print_help
+ = gdb::option::build_help (N_("\
Evaluate EXPR by using the compiler and print result.\n\
\n\
-Usage: compile print[/FMT] [EXPR]\n\
+Usage: compile print [[OPTION]... --] [/FMT] [EXPR]\n\
+\n\
+Options:\n\
+%OPTIONS%\
+Note: because this command accepts arbitrary expressions, if you\n\
+specify any command option, you must use a double dash (\"--\")\n\
+to mark the end of option processing. E.g.: \"compile print -o -- myobj\".\n\
\n\
The expression may be specified on the same line as the command, e.g.:\n\
\n\
@@ -963,7 +980,12 @@ indicate the end of the expression.\n\
\n\
EXPR may be preceded with /FMT, where FMT is a format letter\n\
but no count or size letter (see \"x\" command)."),
- &compile_command_list);
+ compile_print_opts);
+
+ c = add_cmd ("print", class_obscure, compile_print_command,
+ compile_print_help.c_str (),
+ &compile_command_list);
+ set_cmd_completer_handle_brkchars (c, print_command_completer);
add_setshow_boolean_cmd ("compile", class_maintenance, &compile_debug, _("\
Set compile command debugging."), _("\