Home | History | Annotate | Line # | Download | only in gdb.guile
scm-cmd.exp revision 1.9
      1 # Copyright (C) 2009-2024 Free Software Foundation, Inc.
      2 
      3 # This program is free software; you can redistribute it and/or modify
      4 # it under the terms of the GNU General Public License as published by
      5 # the Free Software Foundation; either version 3 of the License, or
      6 # (at your option) any later version.
      7 #
      8 # This program is distributed in the hope that it will be useful,
      9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     11 # GNU General Public License for more details.
     12 #
     13 # You should have received a copy of the GNU General Public License
     14 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
     15 
     16 # This file is part of the GDB testsuite.  It tests the mechanism
     17 # for defining new GDB commands in Scheme.
     18 
     19 load_lib gdb-guile.exp
     20 
     21 require allow_guile_tests
     22 
     23 standard_testfile
     24 
     25 if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } {
     26     return
     27 }
     28 
     29 if ![gdb_guile_runto_main] {
     30     return
     31 }
     32 
     33 # Test a simple command, and command? while we're at it.
     34 
     35 gdb_test_multiline "input simple command" \
     36     "guile" "" \
     37     "(define test-cmd" "" \
     38     " (make-command \"test-cmd\"" "" \
     39     "  #:command-class COMMAND_OBSCURE" "" \
     40     "  #:invoke (lambda (self arg from-tty)" "" \
     41     "    (display (format #f \"test-cmd output, arg = ~a\\n\" arg)))))" "" \
     42     "(register-command! test-cmd)" "" \
     43     "end" ""
     44 
     45 gdb_test "guile (print (command? test-cmd))" "= #t"
     46 gdb_test "guile (print (command? 42))" "= #f"
     47 
     48 gdb_test "test-cmd ugh" "test-cmd output, arg = ugh" "call simple command"
     49 
     50 # Test a prefix command, and a subcommand within it.
     51 
     52 gdb_test_multiline "input prefix command" \
     53     "guile" "" \
     54     "(register-command! (make-command \"prefix-cmd\"" "" \
     55     "  #:command-class COMMAND_OBSCURE" "" \
     56     "  #:completer-class COMPLETE_NONE" "" \
     57     "  #:prefix? #t" "" \
     58     "  #:invoke (lambda (self arg from-tty)" "" \
     59     "    (display (format #f \"prefix-cmd output, arg = ~a\\n\" arg)))))" "" \
     60     "end" ""
     61 
     62 gdb_test "prefix-cmd ugh" "prefix-cmd output, arg = ugh" "call prefix command"
     63 
     64 gdb_test_multiline "input subcommand" \
     65     "guile" "" \
     66     "(register-command! (make-command \"prefix-cmd subcmd\"" "" \
     67     "  #:command-class COMMAND_OBSCURE" "" \
     68     "  #:invoke (lambda (self arg from-tty)" "" \
     69     "    (display (format #f \"subcmd output, arg = ~a\\n\" arg)))))" "" \
     70     "end" ""
     71 
     72 gdb_test "prefix-cmd subcmd ugh" "subcmd output, arg = ugh" "call subcmd"
     73 
     74 # Test a subcommand in an existing GDB prefix.
     75 
     76 gdb_test_multiline "input new subcommand" \
     77     "guile" "" \
     78     "(register-command! (make-command \"info newsubcmd\"" "" \
     79     "  #:command-class COMMAND_OBSCURE" "" \
     80     "  #:invoke (lambda (self arg from-tty)" "" \
     81     "    (display (format #f \"newsubcmd output, arg = ~a\\n\" arg)))))" "" \
     82     "end" ""
     83 
     84 gdb_test "info newsubcmd ugh" "newsubcmd output, arg = ugh" "call newsubcmd"
     85 
     86 # Test a command that throws gdb:user-error.
     87 
     88 gdb_test_multiline "input command to throw error" \
     89     "guile" "" \
     90     "(register-command! (make-command \"test-error-cmd\"" "" \
     91     "  #:command-class COMMAND_OBSCURE" "" \
     92     "  #:invoke (lambda (self arg from-tty)" "" \
     93     "    (throw-user-error \"you lose! ~a\" arg))))" "" \
     94     "end" ""
     95 
     96 gdb_test "test-error-cmd ugh" "ERROR: you lose! ugh\r\n" "call error command"
     97 
     98 # Test string->argv.
     99 
    100 gdb_test "guile (raw-print (string->argv \"1 2 3\"))" \
    101     {= \("1" "2" "3"\)} \
    102     "(string->argv \"1 2 3\")"
    103 
    104 gdb_test "guile (raw-print (string->argv \"'1 2' 3\"))" \
    105     {= \("1 2" "3"\)} \
    106     "(string->argv \"'1 2' 3\")"
    107 
    108 gdb_test "guile (raw-print (string->argv \"\\\"1 2\\\" 3\"))" \
    109     {= \("1 2" "3"\)} \
    110     "(string->argv (\"\\\"1 2\\\" 3\")"
    111 
    112 gdb_test "guile (raw-print (string->argv \"1\\\\ 2 3\"))" \
    113     {= \("1 2" "3"\)} \
    114     "(string->argv \"1\\\\ 2 3\")"
    115 
    116 # Test user-defined guile commands.
    117 
    118 gdb_test_multiline "input simple user-defined command" \
    119     "guile" "" \
    120     "(register-command! (make-command \"test-help\"" "" \
    121     "  #:doc \"Docstring\"" "" \
    122     "  #:command-class COMMAND_USER" "" \
    123     "  #:invoke (lambda (self arg from-tty)" "" \
    124     "    (display (format #f \"test-cmd output, arg = ~a\\n\" arg)))))" "" \
    125     "end" ""
    126 
    127 gdb_test "test-help ugh" "test-cmd output, arg = ugh" \
    128     "call simple user-defined command"
    129 
    130 # Make sure the command shows up in `help user-defined`.
    131 test_user_defined_class_help {"test-help -- Docstring[\r\n]"}
    132 
    133 # Make sure the command does not show up in `show user`.
    134 gdb_test "show user test-help" "Not a user command\." \
    135     "don't show user-defined scheme command in `show user command`"
    136 
    137 # Test expression completion on fields.
    138 
    139 gdb_test_multiline "expression completion command" \
    140     "guile" "" \
    141     "(register-command! (make-command \"expr-test\"" "" \
    142     "  #:command-class COMMAND_USER" ""\
    143     "  #:completer-class COMPLETE_EXPRESSION" "" \
    144     "  #:invoke (lambda (self arg from-tty)" "" \
    145     "    (display (format #f \"invoked on = ~a\\n\" arg)))))" "" \
    146     "end" ""
    147 
    148 gdb_test "complete expr-test bar\." \
    149     "expr-test bar\.bc.*expr-test bar\.ij.*" \
    150     "test completion through complete command"
    151 
    152 
    153 if { [readline_is_used] } {
    154     set test "complete 'expr-test bar.i'"
    155     send_gdb "expr-test bar\.i\t\t"
    156     gdb_test_multiple "" "$test" {
    157 	-re "expr-test bar\.ij \\\x07$" {
    158 	    send_gdb "\n"
    159 	    gdb_test_multiple "" $test {
    160 		-re "invoked on = bar.ij.*$gdb_prompt $" {
    161 		    pass "$test"
    162 		}
    163 	    }
    164 	}
    165     }
    166 }
    167 
    168 # Test using a function for completion.
    169 
    170 gdb_test_multiline "completer-as-function command" \
    171     "guile" "" \
    172     "(register-command! (make-command \"completer-as-function\"" "" \
    173     "  #:command-class COMMAND_USER" ""\
    174     "  #:completer-class (lambda (self text word)" "" \
    175     "    (list \"1\" \"2\" \"3\"))" "" \
    176     "  #:invoke (lambda (self arg from-tty)" "" \
    177     "    (display (format #f \"invoked on = ~a\\n\" arg)))))" "" \
    178     "end" ""
    179 
    180 gdb_test "complete completer-as-function 42\." \
    181     "completer-as-function 42\.1.*completer-as-function 42\.2.*completer-as-function 42\.3" \
    182     "test completion with completion function"
    183 
    184 # Test Scheme error in invoke function.
    185 
    186 gdb_test_multiline "input command with Scheme error" \
    187     "guile" "" \
    188     "(register-command! (make-command \"test-scheme-error-cmd\"" "" \
    189     "  #:command-class COMMAND_OBSCURE" "" \
    190     "  #:invoke (lambda (self arg from-tty)" "" \
    191     "    oops-bad-spelling)))" "" \
    192     "end" ""
    193 
    194 gdb_test "test-scheme-error-cmd ugh" \
    195     "Error occurred in Scheme-implemented GDB command." \
    196     "call scheme-error command"
    197 
    198 # If there is a problem with object management, this can often trigger it.
    199 # It is useful to do this last, after we've created a bunch of command objects.
    200 
    201 gdb_test_no_output "guile (gc)"
    202