GNU Binutils with patches for OS216
Revisión | 55ff4e382699e4f414b91067764f49d3e31da398 (tree) |
---|---|
Tiempo | 2019-06-05 07:30:02 |
Autor | Pedro Alves <palves@redh...> |
Commiter | Pedro Alves |
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:
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:
After getting that working, I saw that gdb.pascal/floats.exp
regressed, in these tests:
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:
is parsed as an expression, and
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:
So these are all equivalent:
TAB completion is fully supported:
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:
yyyy-mm-dd 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:
yyyy-mm-dd 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".
@@ -23,6 +23,7 @@ | ||
23 | 23 | #include "command.h" |
24 | 24 | #include "cli/cli-script.h" |
25 | 25 | #include "cli/cli-utils.h" |
26 | +#include "cli/cli-option.h" | |
26 | 27 | #include "completer.h" |
27 | 28 | #include "gdbcmd.h" |
28 | 29 | #include "compile.h" |
@@ -328,9 +329,9 @@ compile_code_command (const char *arg, int from_tty) | ||
328 | 329 | void |
329 | 330 | compile_print_value (struct value *val, void *data_voidp) |
330 | 331 | { |
331 | - const struct format_data *fmtp = (const struct format_data *) data_voidp; | |
332 | + const value_print_options *print_opts = (value_print_options *) data_voidp; | |
332 | 333 | |
333 | - print_value (val, fmtp); | |
334 | + print_value (val, *print_opts); | |
334 | 335 | } |
335 | 336 | |
336 | 337 | /* Handle the input from the 'compile print' command. The "compile |
@@ -342,22 +343,30 @@ static void | ||
342 | 343 | compile_print_command (const char *arg, int from_tty) |
343 | 344 | { |
344 | 345 | enum compile_i_scope_types scope = COMPILE_I_PRINT_ADDRESS_SCOPE; |
345 | - struct format_data fmt; | |
346 | + value_print_options print_opts; | |
346 | 347 | |
347 | 348 | scoped_restore save_async = make_scoped_restore (¤t_ui->async, 0); |
348 | 349 | |
349 | - /* Passing &FMT as SCOPE_DATA is safe as do_module_cleanup will not | |
350 | - touch the stale pointer if compile_object_run has already quit. */ | |
351 | - print_command_parse_format (&arg, "compile print", &fmt); | |
350 | + get_user_print_options (&print_opts); | |
351 | + /* Override global settings with explicit options, if any. */ | |
352 | + auto group = make_value_print_options_def_group (&print_opts); | |
353 | + gdb::option::process_options | |
354 | + (&arg, gdb::option::PROCESS_OPTIONS_REQUIRE_DELIMITER, group); | |
355 | + | |
356 | + print_command_parse_format (&arg, "compile print", &print_opts); | |
357 | + | |
358 | + /* Passing &PRINT_OPTS as SCOPE_DATA is safe as do_module_cleanup | |
359 | + will not touch the stale pointer if compile_object_run has | |
360 | + already quit. */ | |
352 | 361 | |
353 | 362 | if (arg && *arg) |
354 | - eval_compile_command (NULL, arg, scope, &fmt); | |
363 | + eval_compile_command (NULL, arg, scope, &print_opts); | |
355 | 364 | else |
356 | 365 | { |
357 | 366 | counted_command_line l = get_command_line (compile_control, ""); |
358 | 367 | |
359 | 368 | l->control_u.compile.scope = scope; |
360 | - l->control_u.compile.scope_data = &fmt; | |
369 | + l->control_u.compile.scope_data = &print_opts; | |
361 | 370 | execute_control_command_untraced (l.get ()); |
362 | 371 | } |
363 | 372 | } |
@@ -946,11 +955,19 @@ Usage: compile file [-r|-raw] [FILENAME]\n\ | ||
946 | 955 | &compile_command_list); |
947 | 956 | set_cmd_completer (c, filename_completer); |
948 | 957 | |
949 | - add_cmd ("print", class_obscure, compile_print_command, | |
950 | - _("\ | |
958 | + const auto compile_print_opts = make_value_print_options_def_group (nullptr); | |
959 | + | |
960 | + static const std::string compile_print_help | |
961 | + = gdb::option::build_help (N_("\ | |
951 | 962 | Evaluate EXPR by using the compiler and print result.\n\ |
952 | 963 | \n\ |
953 | -Usage: compile print[/FMT] [EXPR]\n\ | |
964 | +Usage: compile print [[OPTION]... --] [/FMT] [EXPR]\n\ | |
965 | +\n\ | |
966 | +Options:\n\ | |
967 | +%OPTIONS%\ | |
968 | +Note: because this command accepts arbitrary expressions, if you\n\ | |
969 | +specify any command option, you must use a double dash (\"--\")\n\ | |
970 | +to mark the end of option processing. E.g.: \"compile print -o -- myobj\".\n\ | |
954 | 971 | \n\ |
955 | 972 | The expression may be specified on the same line as the command, e.g.:\n\ |
956 | 973 | \n\ |
@@ -963,7 +980,12 @@ indicate the end of the expression.\n\ | ||
963 | 980 | \n\ |
964 | 981 | EXPR may be preceded with /FMT, where FMT is a format letter\n\ |
965 | 982 | but no count or size letter (see \"x\" command)."), |
966 | - &compile_command_list); | |
983 | + compile_print_opts); | |
984 | + | |
985 | + c = add_cmd ("print", class_obscure, compile_print_command, | |
986 | + compile_print_help.c_str (), | |
987 | + &compile_command_list); | |
988 | + set_cmd_completer_handle_brkchars (c, print_command_completer); | |
967 | 989 | |
968 | 990 | add_setshow_boolean_cmd ("compile", class_maintenance, &compile_debug, _("\ |
969 | 991 | Set compile command debugging."), _("\ |
@@ -37,39 +37,6 @@ | ||
37 | 37 | #include "typeprint.h" |
38 | 38 | #include "common/byte-vector.h" |
39 | 39 | |
40 | -/* Controls printing of vtbl's. */ | |
41 | -static void | |
42 | -show_vtblprint (struct ui_file *file, int from_tty, | |
43 | - struct cmd_list_element *c, const char *value) | |
44 | -{ | |
45 | - fprintf_filtered (file, _("\ | |
46 | -Printing of C++ virtual function tables is %s.\n"), | |
47 | - value); | |
48 | -} | |
49 | - | |
50 | -/* Controls looking up an object's derived type using what we find in | |
51 | - its vtables. */ | |
52 | -static void | |
53 | -show_objectprint (struct ui_file *file, int from_tty, | |
54 | - struct cmd_list_element *c, | |
55 | - const char *value) | |
56 | -{ | |
57 | - fprintf_filtered (file, _("\ | |
58 | -Printing of object's derived type based on vtable info is %s.\n"), | |
59 | - value); | |
60 | -} | |
61 | - | |
62 | -static void | |
63 | -show_static_field_print (struct ui_file *file, int from_tty, | |
64 | - struct cmd_list_element *c, | |
65 | - const char *value) | |
66 | -{ | |
67 | - fprintf_filtered (file, | |
68 | - _("Printing of C++ static members is %s.\n"), | |
69 | - value); | |
70 | -} | |
71 | - | |
72 | - | |
73 | 40 | static struct obstack dont_print_vb_obstack; |
74 | 41 | static struct obstack dont_print_statmem_obstack; |
75 | 42 | static struct obstack dont_print_stat_array_obstack; |
@@ -821,30 +788,6 @@ cp_print_class_member (const gdb_byte *valaddr, struct type *type, | ||
821 | 788 | void |
822 | 789 | _initialize_cp_valprint (void) |
823 | 790 | { |
824 | - add_setshow_boolean_cmd ("static-members", class_support, | |
825 | - &user_print_options.static_field_print, _("\ | |
826 | -Set printing of C++ static members."), _("\ | |
827 | -Show printing of C++ static members."), NULL, | |
828 | - NULL, | |
829 | - show_static_field_print, | |
830 | - &setprintlist, &showprintlist); | |
831 | - | |
832 | - add_setshow_boolean_cmd ("vtbl", class_support, | |
833 | - &user_print_options.vtblprint, _("\ | |
834 | -Set printing of C++ virtual function tables."), _("\ | |
835 | -Show printing of C++ virtual function tables."), NULL, | |
836 | - NULL, | |
837 | - show_vtblprint, | |
838 | - &setprintlist, &showprintlist); | |
839 | - | |
840 | - add_setshow_boolean_cmd ("object", class_support, | |
841 | - &user_print_options.objectprint, _("\ | |
842 | -Set printing of object's derived type based on vtable info."), _("\ | |
843 | -Show printing of object's derived type based on vtable info."), NULL, | |
844 | - NULL, | |
845 | - show_objectprint, | |
846 | - &setprintlist, &showprintlist); | |
847 | - | |
848 | 791 | obstack_begin (&dont_print_stat_array_obstack, |
849 | 792 | 32 * sizeof (struct type *)); |
850 | 793 | obstack_begin (&dont_print_statmem_obstack, |
@@ -45,11 +45,13 @@ | ||
45 | 45 | #include "charset.h" |
46 | 46 | #include "arch-utils.h" |
47 | 47 | #include "cli/cli-utils.h" |
48 | +#include "cli/cli-option.h" | |
48 | 49 | #include "cli/cli-script.h" |
49 | 50 | #include "cli/cli-style.h" |
50 | 51 | #include "common/format.h" |
51 | 52 | #include "source.h" |
52 | 53 | #include "common/byte-vector.h" |
54 | +#include "common/gdb_optional.h" | |
53 | 55 | |
54 | 56 | /* Last specified output format. */ |
55 | 57 |
@@ -1117,40 +1119,41 @@ validate_format (struct format_data fmt, const char *cmdname) | ||
1117 | 1119 | fmt.format, cmdname); |
1118 | 1120 | } |
1119 | 1121 | |
1120 | -/* Parse print command format string into *FMTP and update *EXPP. | |
1122 | +/* Parse print command format string into *OPTS and update *EXPP. | |
1121 | 1123 | CMDNAME should name the current command. */ |
1122 | 1124 | |
1123 | 1125 | void |
1124 | 1126 | print_command_parse_format (const char **expp, const char *cmdname, |
1125 | - struct format_data *fmtp) | |
1127 | + value_print_options *opts) | |
1126 | 1128 | { |
1127 | 1129 | const char *exp = *expp; |
1128 | 1130 | |
1129 | 1131 | if (exp && *exp == '/') |
1130 | 1132 | { |
1133 | + format_data fmt; | |
1134 | + | |
1131 | 1135 | exp++; |
1132 | - *fmtp = decode_format (&exp, last_format, 0); | |
1133 | - validate_format (*fmtp, cmdname); | |
1134 | - last_format = fmtp->format; | |
1136 | + fmt = decode_format (&exp, last_format, 0); | |
1137 | + validate_format (fmt, cmdname); | |
1138 | + last_format = fmt.format; | |
1139 | + | |
1140 | + opts->format = fmt.format; | |
1141 | + opts->raw = fmt.raw; | |
1135 | 1142 | } |
1136 | 1143 | else |
1137 | 1144 | { |
1138 | - fmtp->count = 1; | |
1139 | - fmtp->format = 0; | |
1140 | - fmtp->size = 0; | |
1141 | - fmtp->raw = 0; | |
1145 | + opts->format = 0; | |
1146 | + opts->raw = 0; | |
1142 | 1147 | } |
1143 | 1148 | |
1144 | 1149 | *expp = exp; |
1145 | 1150 | } |
1146 | 1151 | |
1147 | -/* Print VAL to console according to *FMTP, including recording it to | |
1148 | - the history. */ | |
1152 | +/* See valprint.h. */ | |
1149 | 1153 | |
1150 | 1154 | void |
1151 | -print_value (struct value *val, const struct format_data *fmtp) | |
1155 | +print_value (value *val, const value_print_options &opts) | |
1152 | 1156 | { |
1153 | - struct value_print_options opts; | |
1154 | 1157 | int histindex = record_latest_value (val); |
1155 | 1158 | |
1156 | 1159 | annotate_value_history_begin (histindex, value_type (val)); |
@@ -1159,28 +1162,31 @@ print_value (struct value *val, const struct format_data *fmtp) | ||
1159 | 1162 | |
1160 | 1163 | annotate_value_history_value (); |
1161 | 1164 | |
1162 | - get_formatted_print_options (&opts, fmtp->format); | |
1163 | - opts.raw = fmtp->raw; | |
1164 | - | |
1165 | - print_formatted (val, fmtp->size, &opts, gdb_stdout); | |
1165 | + print_formatted (val, 0, &opts, gdb_stdout); | |
1166 | 1166 | printf_filtered ("\n"); |
1167 | 1167 | |
1168 | 1168 | annotate_value_history_end (); |
1169 | 1169 | } |
1170 | 1170 | |
1171 | -/* Evaluate string EXP as an expression in the current language and | |
1172 | - print the resulting value. EXP may contain a format specifier as the | |
1173 | - first argument ("/x myvar" for example, to print myvar in hex). */ | |
1171 | +/* Implementation of the "print" and "call" commands. */ | |
1174 | 1172 | |
1175 | 1173 | static void |
1176 | -print_command_1 (const char *exp, int voidprint) | |
1174 | +print_command_1 (const char *args, int voidprint) | |
1177 | 1175 | { |
1178 | 1176 | struct value *val; |
1179 | - struct format_data fmt; | |
1177 | + value_print_options print_opts; | |
1178 | + | |
1179 | + get_user_print_options (&print_opts); | |
1180 | + /* Override global settings with explicit options, if any. */ | |
1181 | + auto group = make_value_print_options_def_group (&print_opts); | |
1182 | + gdb::option::process_options | |
1183 | + (&args, gdb::option::PROCESS_OPTIONS_REQUIRE_DELIMITER, group); | |
1184 | + | |
1185 | + print_command_parse_format (&args, "print", &print_opts); | |
1180 | 1186 | |
1181 | - print_command_parse_format (&exp, "print", &fmt); | |
1187 | + const char *exp = args; | |
1182 | 1188 | |
1183 | - if (exp && *exp) | |
1189 | + if (exp != nullptr && *exp) | |
1184 | 1190 | { |
1185 | 1191 | expression_up expr = parse_expression (exp); |
1186 | 1192 | val = evaluate_expression (expr.get ()); |
@@ -1190,7 +1196,23 @@ print_command_1 (const char *exp, int voidprint) | ||
1190 | 1196 | |
1191 | 1197 | if (voidprint || (val && value_type (val) && |
1192 | 1198 | TYPE_CODE (value_type (val)) != TYPE_CODE_VOID)) |
1193 | - print_value (val, &fmt); | |
1199 | + print_value (val, print_opts); | |
1200 | +} | |
1201 | + | |
1202 | +/* See valprint.h. */ | |
1203 | + | |
1204 | +void | |
1205 | +print_command_completer (struct cmd_list_element *ignore, | |
1206 | + completion_tracker &tracker, | |
1207 | + const char *text, const char * /*word*/) | |
1208 | +{ | |
1209 | + const auto group = make_value_print_options_def_group (nullptr); | |
1210 | + if (gdb::option::complete_options | |
1211 | + (tracker, &text, gdb::option::PROCESS_OPTIONS_REQUIRE_DELIMITER, group)) | |
1212 | + return; | |
1213 | + | |
1214 | + const char *word = advance_to_expression_complete_word_point (tracker, text); | |
1215 | + expression_completer (ignore, tracker, text, word); | |
1194 | 1216 | } |
1195 | 1217 | |
1196 | 1218 | static void |
@@ -2761,7 +2783,7 @@ Usage: call EXP\n\ | ||
2761 | 2783 | The argument is the function name and arguments, in the notation of the\n\ |
2762 | 2784 | current working language. The result is printed and saved in the value\n\ |
2763 | 2785 | history, if it is not void.")); |
2764 | - set_cmd_completer (c, expression_completer); | |
2786 | + set_cmd_completer_handle_brkchars (c, print_command_completer); | |
2765 | 2787 | |
2766 | 2788 | add_cmd ("variable", class_vars, set_command, _("\ |
2767 | 2789 | Evaluate expression EXP and assign result to variable VAR\n\ |
@@ -2775,9 +2797,18 @@ This may usually be abbreviated to simply \"set\"."), | ||
2775 | 2797 | &setlist); |
2776 | 2798 | add_alias_cmd ("var", "variable", class_vars, 0, &setlist); |
2777 | 2799 | |
2778 | - c = add_com ("print", class_vars, print_command, _("\ | |
2800 | + const auto print_opts = make_value_print_options_def_group (nullptr); | |
2801 | + | |
2802 | + static const std::string print_help = gdb::option::build_help (N_("\ | |
2779 | 2803 | Print value of expression EXP.\n\ |
2780 | -Usage: print[/FMT] EXP\n\ | |
2804 | +Usage: print [[OPTION]... --] [/FMT] [EXP]\n\ | |
2805 | +\n\ | |
2806 | +Options:\n\ | |
2807 | +%OPTIONS%\ | |
2808 | +Note: because this command accepts arbitrary expressions, if you\n\ | |
2809 | +specify any command option, you must use a double dash (\"--\")\n\ | |
2810 | +to mark the end of option processing. E.g.: \"print -o -- myobj\".\n\ | |
2811 | +\n\ | |
2781 | 2812 | Variables accessible are those of the lexical environment of the selected\n\ |
2782 | 2813 | stack frame, plus all those whose scope is global or an entire file.\n\ |
2783 | 2814 | \n\ |
@@ -2797,8 +2828,11 @@ where FOO is stored, etc. FOO must be an expression whose value\n\ | ||
2797 | 2828 | resides in memory.\n\ |
2798 | 2829 | \n\ |
2799 | 2830 | EXP may be preceded with /FMT, where FMT is a format letter\n\ |
2800 | -but no count or size letter (see \"x\" command).")); | |
2801 | - set_cmd_completer (c, expression_completer); | |
2831 | +but no count or size letter (see \"x\" command)."), | |
2832 | + print_opts); | |
2833 | + | |
2834 | + c = add_com ("print", class_vars, print_command, print_help.c_str ()); | |
2835 | + set_cmd_completer_handle_brkchars (c, print_command_completer); | |
2802 | 2836 | add_com_alias ("p", "print", class_vars, 1); |
2803 | 2837 | add_com_alias ("inspect", "print", class_vars, 1); |
2804 | 2838 |
@@ -19,9 +19,18 @@ | ||
19 | 19 | |
20 | 20 | # The test uses the "maintenance test-options" subcommands to exercise |
21 | 21 | # TAB-completion and option processing. |
22 | +# | |
23 | +# It also tests option integration in various commands, including | |
24 | +# "print" and "compile print". | |
22 | 25 | |
23 | 26 | load_lib completion-support.exp |
24 | 27 | |
28 | +standard_testfile .c | |
29 | + | |
30 | +if {[build_executable "failed to prepare" $testfile $srcfile debug]} { | |
31 | + return -1 | |
32 | +} | |
33 | + | |
25 | 34 | clean_restart |
26 | 35 | |
27 | 36 | if { ![readline_is_used] } { |
@@ -117,6 +126,111 @@ set all_options { | ||
117 | 126 | "-zuinteger-unlimited" |
118 | 127 | } |
119 | 128 | |
129 | +# Basic option-machinery + "print" command integration tests. | |
130 | +proc_with_prefix test-print {{prefix ""}} { | |
131 | + clean_restart | |
132 | + | |
133 | + # Completing "print" with no argument completes on symbols only, | |
134 | + # no options are offered. Since we haven't loaded any symbols, | |
135 | + # the match list should be empty. | |
136 | + test_gdb_complete_none "${prefix}print " | |
137 | + | |
138 | + # OTOH, completing at "-" should list all options. | |
139 | + test_gdb_complete_multiple "${prefix}print " "-" "" { | |
140 | + "-address" | |
141 | + "-array" | |
142 | + "-array-indexes" | |
143 | + "-elements" | |
144 | + "-max-depth" | |
145 | + "-null-stop" | |
146 | + "-object" | |
147 | + "-pretty" | |
148 | + "-repeats" | |
149 | + "-static-members" | |
150 | + "-symbol" | |
151 | + "-union" | |
152 | + "-vtbl" | |
153 | + } | |
154 | + | |
155 | + global binfile | |
156 | + clean_restart $binfile | |
157 | + | |
158 | + if ![runto_main] { | |
159 | + fail "cannot run to main" | |
160 | + return | |
161 | + } | |
162 | + | |
163 | + # Mix options and format. | |
164 | + gdb_test "${prefix}print -pretty -- /x 1" " = 0x1" | |
165 | + | |
166 | + # Smoke test that options actually work. | |
167 | + gdb_test "${prefix}print -pretty -- g_s" \ | |
168 | + [multi_line \ | |
169 | + " = {" \ | |
170 | + " a = 1," \ | |
171 | + " b = 2," \ | |
172 | + " c = 3" \ | |
173 | + "}"] | |
174 | + | |
175 | + test_gdb_complete_unique \ | |
176 | + "${prefix}print xxx" \ | |
177 | + "${prefix}print xxx1" | |
178 | + test_gdb_complete_unique \ | |
179 | + "${prefix}print -- xxx" \ | |
180 | + "${prefix}print -- xxx1" | |
181 | + | |
182 | + # Error messages when testing with "compile" are different from | |
183 | + # the error messages gdb's internal parser throws. This procedure | |
184 | + # hides the difference. EXPECTED_RE is only considered when not | |
185 | + # testing with "compile". | |
186 | + proc test_invalid_expression {cmd expected_re} { | |
187 | + upvar prefix prefix | |
188 | + | |
189 | + if {$prefix != "compile "} { | |
190 | + gdb_test $cmd $expected_re | |
191 | + } else { | |
192 | + # Error messages depend on compiler version, so we just | |
193 | + # look for the last line indicating a failure. | |
194 | + gdb_test $cmd "Compilation failed\\." | |
195 | + } | |
196 | + } | |
197 | + | |
198 | + # Check that '-XXX' without a "--" is handled as an | |
199 | + # expression. | |
200 | + gdb_test "${prefix}print -1" " = -1" | |
201 | + test_invalid_expression \ | |
202 | + "${prefix}print --1" \ | |
203 | + "Left operand of assignment is not an lvalue\\." | |
204 | + test_invalid_expression \ | |
205 | + "${prefix}print -object" \ | |
206 | + "No symbol \"object\".*" | |
207 | + | |
208 | + # Test printing with options and no expression. | |
209 | + set test "${prefix}print -object --" | |
210 | + if {$prefix != "compile "} { | |
211 | + # Regular "print" repeats the last history value. | |
212 | + gdb_test $test " = -1" | |
213 | + } else { | |
214 | + # "compile print" starts a multiline expression. | |
215 | + gdb_test_multiple $test $test { | |
216 | + -re ">$" { | |
217 | + gdb_test "-1\nend" " = -1" \ | |
218 | + $test | |
219 | + } | |
220 | + } | |
221 | + } | |
222 | + | |
223 | + # Check that everything after "-- " is treated as an | |
224 | + # expression, not confused with an option. | |
225 | + test_invalid_expression \ | |
226 | + "${prefix}print -- -address" \ | |
227 | + "No symbol.*" | |
228 | + gdb_test "${prefix}print -- -1" " = -1" | |
229 | + test_invalid_expression \ | |
230 | + "${prefix}print -- --1" \ | |
231 | + "Left operand of assignment is not an lvalue\\." | |
232 | +} | |
233 | + | |
120 | 234 | # Miscellaneous tests. |
121 | 235 | proc_with_prefix test-misc {variant} { |
122 | 236 | global all_options |
@@ -552,3 +666,11 @@ foreach_with_prefix cmd { | ||
552 | 666 | } |
553 | 667 | test-enum $cmd |
554 | 668 | } |
669 | + | |
670 | +# Run the print integration tests. | |
671 | +test-print "" | |
672 | + | |
673 | +# Same for "compile print". | |
674 | +if ![skip_compile_feature_tests] { | |
675 | + test-print "compile " | |
676 | +} |
@@ -36,6 +36,7 @@ | ||
36 | 36 | #include <ctype.h> |
37 | 37 | #include <algorithm> |
38 | 38 | #include "common/byte-vector.h" |
39 | +#include "cli/cli-option.h" | |
39 | 40 | |
40 | 41 | /* Maximum number of wchars returned from wchar_iterate. */ |
41 | 42 | #define MAX_WCHARS 4 |
@@ -3069,7 +3070,181 @@ show_print_raw (const char *args, int from_tty) | ||
3069 | 3070 | cmd_show_list (showprintrawlist, from_tty, ""); |
3070 | 3071 | } |
3071 | 3072 | |
3073 | +/* Controls printing of vtbl's. */ | |
3074 | +static void | |
3075 | +show_vtblprint (struct ui_file *file, int from_tty, | |
3076 | + struct cmd_list_element *c, const char *value) | |
3077 | +{ | |
3078 | + fprintf_filtered (file, _("\ | |
3079 | +Printing of C++ virtual function tables is %s.\n"), | |
3080 | + value); | |
3081 | +} | |
3082 | + | |
3083 | +/* Controls looking up an object's derived type using what we find in | |
3084 | + its vtables. */ | |
3085 | +static void | |
3086 | +show_objectprint (struct ui_file *file, int from_tty, | |
3087 | + struct cmd_list_element *c, | |
3088 | + const char *value) | |
3089 | +{ | |
3090 | + fprintf_filtered (file, _("\ | |
3091 | +Printing of object's derived type based on vtable info is %s.\n"), | |
3092 | + value); | |
3093 | +} | |
3094 | + | |
3095 | +static void | |
3096 | +show_static_field_print (struct ui_file *file, int from_tty, | |
3097 | + struct cmd_list_element *c, | |
3098 | + const char *value) | |
3099 | +{ | |
3100 | + fprintf_filtered (file, | |
3101 | + _("Printing of C++ static members is %s.\n"), | |
3102 | + value); | |
3103 | +} | |
3104 | + | |
3072 | 3105 | |
3106 | + | |
3107 | +/* A couple typedefs to make writing the options a bit more | |
3108 | + convenient. */ | |
3109 | +using boolean_option_def | |
3110 | + = gdb::option::boolean_option_def<value_print_options>; | |
3111 | +using uinteger_option_def | |
3112 | + = gdb::option::uinteger_option_def<value_print_options>; | |
3113 | +using zuinteger_unlimited_option_def | |
3114 | + = gdb::option::zuinteger_unlimited_option_def<value_print_options>; | |
3115 | + | |
3116 | +/* Definions of options for the "print" and "compile print" | |
3117 | + commands. */ | |
3118 | +static const gdb::option::option_def value_print_option_defs[] = { | |
3119 | + | |
3120 | + boolean_option_def { | |
3121 | + "address", | |
3122 | + [] (value_print_options *opt) { return &opt->addressprint; }, | |
3123 | + show_addressprint, /* show_cmd_cb */ | |
3124 | + N_("Set printing of addresses."), | |
3125 | + N_("Show printing of addresses."), | |
3126 | + NULL, /* help_doc */ | |
3127 | + }, | |
3128 | + | |
3129 | + boolean_option_def { | |
3130 | + "array", | |
3131 | + [] (value_print_options *opt) { return &opt->prettyformat_arrays; }, | |
3132 | + show_prettyformat_arrays, /* show_cmd_cb */ | |
3133 | + N_("Set pretty formatting of arrays."), | |
3134 | + N_("Show pretty formatting of arrays."), | |
3135 | + NULL, /* help_doc */ | |
3136 | + }, | |
3137 | + | |
3138 | + boolean_option_def { | |
3139 | + "array-indexes", | |
3140 | + [] (value_print_options *opt) { return &opt->print_array_indexes; }, | |
3141 | + show_print_array_indexes, /* show_cmd_cb */ | |
3142 | + N_("Set printing of array indexes."), | |
3143 | + N_("Show printing of array indexes"), | |
3144 | + NULL, /* help_doc */ | |
3145 | + }, | |
3146 | + | |
3147 | + uinteger_option_def { | |
3148 | + "elements", | |
3149 | + [] (value_print_options *opt) { return &opt->print_max; }, | |
3150 | + show_print_max, /* show_cmd_cb */ | |
3151 | + N_("Set limit on string chars or array elements to print."), | |
3152 | + N_("Show limit on string chars or array elements to print."), | |
3153 | + N_("\"unlimited\" causes there to be no limit."), | |
3154 | + }, | |
3155 | + | |
3156 | + zuinteger_unlimited_option_def { | |
3157 | + "max-depth", | |
3158 | + [] (value_print_options *opt) { return &opt->max_depth; }, | |
3159 | + show_print_max_depth, /* show_cmd_cb */ | |
3160 | + N_("Set maximum print depth for nested structures, unions and arrays."), | |
3161 | + N_("Show maximum print depth for nested structures, unions, and arrays."), | |
3162 | + N_("When structures, unions, or arrays are nested beyond this depth then they\n\ | |
3163 | +will be replaced with either '{...}' or '(...)' depending on the language.\n\ | |
3164 | +Use \"unlimited\" to print the complete structure.") | |
3165 | + }, | |
3166 | + | |
3167 | + boolean_option_def { | |
3168 | + "null-stop", | |
3169 | + [] (value_print_options *opt) { return &opt->stop_print_at_null; }, | |
3170 | + show_stop_print_at_null, /* show_cmd_cb */ | |
3171 | + N_("Set printing of char arrays to stop at first null char."), | |
3172 | + N_("Show printing of char arrays to stop at first null char."), | |
3173 | + NULL, /* help_doc */ | |
3174 | + }, | |
3175 | + | |
3176 | + boolean_option_def { | |
3177 | + "object", | |
3178 | + [] (value_print_options *opt) { return &opt->objectprint; }, | |
3179 | + show_objectprint, /* show_cmd_cb */ | |
3180 | + _("Set printing of C++ virtual function tables."), | |
3181 | + _("Show printing of C++ virtual function tables."), | |
3182 | + NULL, /* help_doc */ | |
3183 | + }, | |
3184 | + | |
3185 | + boolean_option_def { | |
3186 | + "pretty", | |
3187 | + [] (value_print_options *opt) { return &opt->prettyformat_structs; }, | |
3188 | + show_prettyformat_structs, /* show_cmd_cb */ | |
3189 | + N_("Set pretty formatting of structures."), | |
3190 | + N_("Show pretty formatting of structures."), | |
3191 | + NULL, /* help_doc */ | |
3192 | + }, | |
3193 | + | |
3194 | + uinteger_option_def { | |
3195 | + "repeats", | |
3196 | + [] (value_print_options *opt) { return &opt->repeat_count_threshold; }, | |
3197 | + show_repeat_count_threshold, /* show_cmd_cb */ | |
3198 | + N_("Set threshold for repeated print elements."), | |
3199 | + N_("Show threshold for repeated print elements."), | |
3200 | + N_("\"unlimited\" causes all elements to be individually printed."), | |
3201 | + }, | |
3202 | + | |
3203 | + boolean_option_def { | |
3204 | + "static-members", | |
3205 | + [] (value_print_options *opt) { return &opt->static_field_print; }, | |
3206 | + show_static_field_print, /* show_cmd_cb */ | |
3207 | + N_("Set printing of C++ static members."), | |
3208 | + N_("Show printing of C++ static members."), | |
3209 | + NULL, /* help_doc */ | |
3210 | + }, | |
3211 | + | |
3212 | + boolean_option_def { | |
3213 | + "symbol", | |
3214 | + [] (value_print_options *opt) { return &opt->symbol_print; }, | |
3215 | + show_symbol_print, /* show_cmd_cb */ | |
3216 | + N_("Set printing of symbol names when printing pointers."), | |
3217 | + N_("Show printing of symbol names when printing pointers."), | |
3218 | + NULL, /* help_doc */ | |
3219 | + }, | |
3220 | + | |
3221 | + boolean_option_def { | |
3222 | + "union", | |
3223 | + [] (value_print_options *opt) { return &opt->unionprint; }, | |
3224 | + show_unionprint, /* show_cmd_cb */ | |
3225 | + N_("Set printing of unions interior to structures."), | |
3226 | + N_("Show printing of unions interior to structures."), | |
3227 | + NULL, /* help_doc */ | |
3228 | + }, | |
3229 | + | |
3230 | + boolean_option_def { | |
3231 | + "vtbl", | |
3232 | + [] (value_print_options *opt) { return &opt->vtblprint; }, | |
3233 | + show_vtblprint, /* show_cmd_cb */ | |
3234 | + N_("Set printing of C++ virtual function tables."), | |
3235 | + N_("Show printing of C++ virtual function tables."), | |
3236 | + NULL, /* help_doc */ | |
3237 | + }, | |
3238 | +}; | |
3239 | + | |
3240 | +/* See valprint.h. */ | |
3241 | + | |
3242 | +gdb::option::option_def_group | |
3243 | +make_value_print_options_def_group (value_print_options *opts) | |
3244 | +{ | |
3245 | + return {{value_print_option_defs}, opts}; | |
3246 | +} | |
3247 | + | |
3073 | 3248 | void |
3074 | 3249 | _initialize_valprint (void) |
3075 | 3250 | { |
@@ -3094,71 +3269,9 @@ Generic command for setting what things to print in \"raw\" mode."), | ||
3094 | 3269 | _("Generic command for showing \"print raw\" settings."), |
3095 | 3270 | &showprintrawlist, "show print raw ", 0, &showprintlist); |
3096 | 3271 | |
3097 | - add_setshow_uinteger_cmd ("elements", no_class, | |
3098 | - &user_print_options.print_max, _("\ | |
3099 | -Set limit on string chars or array elements to print."), _("\ | |
3100 | -Show limit on string chars or array elements to print."), _("\ | |
3101 | -\"set print elements unlimited\" causes there to be no limit."), | |
3102 | - NULL, | |
3103 | - show_print_max, | |
3104 | - &setprintlist, &showprintlist); | |
3105 | - | |
3106 | - add_setshow_boolean_cmd ("null-stop", no_class, | |
3107 | - &user_print_options.stop_print_at_null, _("\ | |
3108 | -Set printing of char arrays to stop at first null char."), _("\ | |
3109 | -Show printing of char arrays to stop at first null char."), NULL, | |
3110 | - NULL, | |
3111 | - show_stop_print_at_null, | |
3112 | - &setprintlist, &showprintlist); | |
3113 | - | |
3114 | - add_setshow_uinteger_cmd ("repeats", no_class, | |
3115 | - &user_print_options.repeat_count_threshold, _("\ | |
3116 | -Set threshold for repeated print elements."), _("\ | |
3117 | -Show threshold for repeated print elements."), _("\ | |
3118 | -\"set print repeats unlimited\" causes all elements to be individually printed."), | |
3119 | - NULL, | |
3120 | - show_repeat_count_threshold, | |
3121 | - &setprintlist, &showprintlist); | |
3122 | - | |
3123 | - add_setshow_boolean_cmd ("pretty", class_support, | |
3124 | - &user_print_options.prettyformat_structs, _("\ | |
3125 | -Set pretty formatting of structures."), _("\ | |
3126 | -Show pretty formatting of structures."), NULL, | |
3127 | - NULL, | |
3128 | - show_prettyformat_structs, | |
3129 | - &setprintlist, &showprintlist); | |
3130 | - | |
3131 | - add_setshow_boolean_cmd ("union", class_support, | |
3132 | - &user_print_options.unionprint, _("\ | |
3133 | -Set printing of unions interior to structures."), _("\ | |
3134 | -Show printing of unions interior to structures."), NULL, | |
3135 | - NULL, | |
3136 | - show_unionprint, | |
3137 | - &setprintlist, &showprintlist); | |
3138 | - | |
3139 | - add_setshow_boolean_cmd ("array", class_support, | |
3140 | - &user_print_options.prettyformat_arrays, _("\ | |
3141 | -Set pretty formatting of arrays."), _("\ | |
3142 | -Show pretty formatting of arrays."), NULL, | |
3143 | - NULL, | |
3144 | - show_prettyformat_arrays, | |
3145 | - &setprintlist, &showprintlist); | |
3146 | - | |
3147 | - add_setshow_boolean_cmd ("address", class_support, | |
3148 | - &user_print_options.addressprint, _("\ | |
3149 | -Set printing of addresses."), _("\ | |
3150 | -Show printing of addresses."), NULL, | |
3151 | - NULL, | |
3152 | - show_addressprint, | |
3153 | - &setprintlist, &showprintlist); | |
3154 | - | |
3155 | - add_setshow_boolean_cmd ("symbol", class_support, | |
3156 | - &user_print_options.symbol_print, _("\ | |
3157 | -Set printing of symbol names when printing pointers."), _("\ | |
3158 | -Show printing of symbol names when printing pointers."), | |
3159 | - NULL, NULL, | |
3160 | - show_symbol_print, | |
3161 | - &setprintlist, &showprintlist); | |
3272 | + gdb::option::add_setshow_cmds_for_options | |
3273 | + (class_support, &user_print_options, value_print_option_defs, | |
3274 | + &setprintlist, &showprintlist); | |
3162 | 3275 | |
3163 | 3276 | add_setshow_zuinteger_cmd ("input-radix", class_support, &input_radix_1, |
3164 | 3277 | _("\ |
@@ -3192,20 +3305,4 @@ Without an argument, sets both radices back to the default value of 10."), | ||
3192 | 3305 | Show the default input and output number radices.\n\ |
3193 | 3306 | Use 'show input-radix' or 'show output-radix' to independently show each."), |
3194 | 3307 | &showlist); |
3195 | - | |
3196 | - add_setshow_boolean_cmd ("array-indexes", class_support, | |
3197 | - &user_print_options.print_array_indexes, _("\ | |
3198 | -Set printing of array indexes."), _("\ | |
3199 | -Show printing of array indexes"), NULL, NULL, show_print_array_indexes, | |
3200 | - &setprintlist, &showprintlist); | |
3201 | - | |
3202 | - add_setshow_zuinteger_unlimited_cmd ("max-depth", class_support, | |
3203 | - &user_print_options.max_depth, _("\ | |
3204 | -Set maximum print depth for nested structures, unions and arrays."), _("\ | |
3205 | -Show maximum print depth for nested structures, unions, and arrays."), _("\ | |
3206 | -When structures, unions, or arrays are nested beyond this depth then they\n\ | |
3207 | -will be replaced with either '{...}' or '(...)' depending on the language.\n\ | |
3208 | -Use 'set print max-depth unlimited' to print the complete structure."), | |
3209 | - NULL, show_print_max_depth, | |
3210 | - &setprintlist, &showprintlist); | |
3211 | 3308 | } |
@@ -20,6 +20,8 @@ | ||
20 | 20 | #ifndef VALPRINT_H |
21 | 21 | #define VALPRINT_H |
22 | 22 | |
23 | +#include "cli/cli-option.h" | |
24 | + | |
23 | 25 | /* This is used to pass formatting options to various value-printing |
24 | 26 | functions. */ |
25 | 27 | struct value_print_options |
@@ -100,6 +102,11 @@ struct value_print_options | ||
100 | 102 | int finish_print; |
101 | 103 | }; |
102 | 104 | |
105 | +/* Create an option_def_group for the value_print options, with OPTS | |
106 | + as context. */ | |
107 | +extern gdb::option::option_def_group make_value_print_options_def_group | |
108 | + (value_print_options *opts); | |
109 | + | |
103 | 110 | /* The global print options set by the user. In general this should |
104 | 111 | not be directly accessed, except by set/show commands. Ordinary |
105 | 112 | code should call get_user_print_options instead. */ |
@@ -233,8 +240,17 @@ struct format_data | ||
233 | 240 | }; |
234 | 241 | |
235 | 242 | extern void print_command_parse_format (const char **expp, const char *cmdname, |
236 | - struct format_data *fmtp); | |
237 | -extern void print_value (struct value *val, const struct format_data *fmtp); | |
243 | + value_print_options *opts); | |
244 | + | |
245 | +/* Print VAL to console according to OPTS, including recording it to | |
246 | + the history. */ | |
247 | +extern void print_value (value *val, const value_print_options &opts); | |
248 | + | |
249 | +/* Completer for the "print", "call", and "compile print" | |
250 | + commands. */ | |
251 | +extern void print_command_completer (struct cmd_list_element *ignore, | |
252 | + completion_tracker &tracker, | |
253 | + const char *text, const char *word); | |
238 | 254 | |
239 | 255 | /* Given an address ADDR return all the elements needed to print the |
240 | 256 | address in a symbolic form. NAME can be mangled or not depending |