aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.base
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/testsuite/gdb.base
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/testsuite/gdb.base')
-rw-r--r--gdb/testsuite/gdb.base/options.exp122
1 files changed, 122 insertions, 0 deletions
diff --git a/gdb/testsuite/gdb.base/options.exp b/gdb/testsuite/gdb.base/options.exp
index 1891176..3b4e7ee 100644
--- a/gdb/testsuite/gdb.base/options.exp
+++ b/gdb/testsuite/gdb.base/options.exp
@@ -19,9 +19,18 @@
# The test uses the "maintenance test-options" subcommands to exercise
# TAB-completion and option processing.
+#
+# It also tests option integration in various commands, including
+# "print" and "compile print".
load_lib completion-support.exp
+standard_testfile .c
+
+if {[build_executable "failed to prepare" $testfile $srcfile debug]} {
+ return -1
+}
+
clean_restart
if { ![readline_is_used] } {
@@ -117,6 +126,111 @@ set all_options {
"-zuinteger-unlimited"
}
+# Basic option-machinery + "print" command integration tests.
+proc_with_prefix test-print {{prefix ""}} {
+ clean_restart
+
+ # Completing "print" with no argument completes on symbols only,
+ # no options are offered. Since we haven't loaded any symbols,
+ # the match list should be empty.
+ test_gdb_complete_none "${prefix}print "
+
+ # OTOH, completing at "-" should list all options.
+ test_gdb_complete_multiple "${prefix}print " "-" "" {
+ "-address"
+ "-array"
+ "-array-indexes"
+ "-elements"
+ "-max-depth"
+ "-null-stop"
+ "-object"
+ "-pretty"
+ "-repeats"
+ "-static-members"
+ "-symbol"
+ "-union"
+ "-vtbl"
+ }
+
+ global binfile
+ clean_restart $binfile
+
+ if ![runto_main] {
+ fail "cannot run to main"
+ return
+ }
+
+ # Mix options and format.
+ gdb_test "${prefix}print -pretty -- /x 1" " = 0x1"
+
+ # Smoke test that options actually work.
+ gdb_test "${prefix}print -pretty -- g_s" \
+ [multi_line \
+ " = {" \
+ " a = 1," \
+ " b = 2," \
+ " c = 3" \
+ "}"]
+
+ test_gdb_complete_unique \
+ "${prefix}print xxx" \
+ "${prefix}print xxx1"
+ test_gdb_complete_unique \
+ "${prefix}print -- xxx" \
+ "${prefix}print -- xxx1"
+
+ # Error messages when testing with "compile" are different from
+ # the error messages gdb's internal parser throws. This procedure
+ # hides the difference. EXPECTED_RE is only considered when not
+ # testing with "compile".
+ proc test_invalid_expression {cmd expected_re} {
+ upvar prefix prefix
+
+ if {$prefix != "compile "} {
+ gdb_test $cmd $expected_re
+ } else {
+ # Error messages depend on compiler version, so we just
+ # look for the last line indicating a failure.
+ gdb_test $cmd "Compilation failed\\."
+ }
+ }
+
+ # Check that '-XXX' without a "--" is handled as an
+ # expression.
+ gdb_test "${prefix}print -1" " = -1"
+ test_invalid_expression \
+ "${prefix}print --1" \
+ "Left operand of assignment is not an lvalue\\."
+ test_invalid_expression \
+ "${prefix}print -object" \
+ "No symbol \"object\".*"
+
+ # Test printing with options and no expression.
+ set test "${prefix}print -object --"
+ if {$prefix != "compile "} {
+ # Regular "print" repeats the last history value.
+ gdb_test $test " = -1"
+ } else {
+ # "compile print" starts a multiline expression.
+ gdb_test_multiple $test $test {
+ -re ">$" {
+ gdb_test "-1\nend" " = -1" \
+ $test
+ }
+ }
+ }
+
+ # Check that everything after "-- " is treated as an
+ # expression, not confused with an option.
+ test_invalid_expression \
+ "${prefix}print -- -address" \
+ "No symbol.*"
+ gdb_test "${prefix}print -- -1" " = -1"
+ test_invalid_expression \
+ "${prefix}print -- --1" \
+ "Left operand of assignment is not an lvalue\\."
+}
+
# Miscellaneous tests.
proc_with_prefix test-misc {variant} {
global all_options
@@ -552,3 +666,11 @@ foreach_with_prefix cmd {
}
test-enum $cmd
}
+
+# Run the print integration tests.
+test-print ""
+
+# Same for "compile print".
+if ![skip_compile_feature_tests] {
+ test-print "compile "
+}