1 # Copyright (C) 2009-2013 Free Software Foundation, Inc.
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.
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.
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/>.
16 # This file is part of the GDB testsuite. It tests the mechanism
17 # for defining new GDB commands in Python.
19 load_lib gdb-python.exp
21 # Start with a fresh gdb.
25 gdb_reinitialize_dir $srcdir/$subdir
27 # Skip all tests if Python scripting is not enabled.
28 if { [skip_python_tests] } { continue }
30 # Test a simple command.
32 gdb_py_test_multiple "input simple command" \
34 "class test_cmd (gdb.Command):" "" \
35 " def __init__ (self):" "" \
36 " super (test_cmd, self).__init__ (\"test_cmd\", gdb.COMMAND_OBSCURE)" "" \
37 " def invoke (self, arg, from_tty):" "" \
38 " print (\"test_cmd output, arg = %s\" % arg)" "" \
42 gdb_test "test_cmd ugh" "test_cmd output, arg = ugh" "call simple command"
44 # Test a prefix command, and a subcommand within it.
46 gdb_py_test_multiple "input prefix command" \
48 "class prefix_cmd (gdb.Command):" "" \
49 " def __init__ (self):" "" \
50 " super (prefix_cmd, self).__init__ (\"prefix_cmd\", gdb.COMMAND_OBSCURE, gdb.COMPLETE_NONE, True)" "" \
51 " def invoke (self, arg, from_tty):" "" \
52 " print (\"prefix_cmd output, arg = %s\" % arg)" "" \
56 gdb_test "prefix_cmd ugh" "prefix_cmd output, arg = ugh" "call prefix command"
58 gdb_py_test_multiple "input subcommand" \
60 "class subcmd (gdb.Command):" "" \
61 " def __init__ (self):" "" \
62 " super (subcmd, self).__init__ (\"prefix_cmd subcmd\", gdb.COMMAND_OBSCURE)" "" \
63 " def invoke (self, arg, from_tty):" "" \
64 " print (\"subcmd output, arg = %s\" % arg)" "" \
68 gdb_test "prefix_cmd subcmd ugh" "subcmd output, arg = ugh" "call subcmd"
70 # Test prefix command using keyword arguments.
72 gdb_py_test_multiple "input prefix command, keyword arguments" \
74 "class prefix_cmd2 (gdb.Command):" "" \
75 " def __init__ (self):" "" \
76 " super (prefix_cmd2, self).__init__ (\"prefix_cmd2\", gdb.COMMAND_OBSCURE, prefix = True, completer_class = gdb.COMPLETE_FILENAME)" "" \
77 " def invoke (self, arg, from_tty):" "" \
78 " print (\"prefix_cmd2 output, arg = %s\" % arg)" "" \
82 gdb_test "prefix_cmd2 argh" "prefix_cmd2 output, arg = argh" "call prefix command, keyword arguments"
84 gdb_py_test_multiple "input subcommand under prefix_cmd2" \
86 "class subcmd (gdb.Command):" "" \
87 " def __init__ (self):" "" \
88 " super (subcmd, self).__init__ (\"prefix_cmd2 subcmd\", gdb.COMMAND_OBSCURE)" "" \
89 " def invoke (self, arg, from_tty):" "" \
90 " print (\"subcmd output, arg = %s\" % arg)" "" \
94 gdb_test "prefix_cmd2 subcmd ugh" "subcmd output, arg = ugh" "call subcmd under prefix_cmd2"
96 # Test a subcommand in an existing GDB prefix.
98 gdb_py_test_multiple "input new subcommand" \
100 "class newsubcmd (gdb.Command):" "" \
101 " def __init__ (self):" "" \
102 " super (newsubcmd, self).__init__ (\"info newsubcmd\", gdb.COMMAND_OBSCURE)" "" \
103 " def invoke (self, arg, from_tty):" "" \
104 " print (\"newsubcmd output, arg = %s\" % arg)" "" \
108 gdb_test "info newsubcmd ugh" "newsubcmd output, arg = ugh" "call newsubcmd"
110 # Test a command that throws gdb.GdbError.
112 gdb_py_test_multiple "input command to throw error" \
114 "class test_error_cmd (gdb.Command):" "" \
115 " def __init__ (self):" "" \
116 " super (test_error_cmd, self).__init__ (\"test_error_cmd\", gdb.COMMAND_OBSCURE)" "" \
117 " def invoke (self, arg, from_tty):" "" \
118 " raise gdb.GdbError ('you lose!')" "" \
119 "test_error_cmd ()" "" \
122 gdb_test "test_error_cmd ugh" "you lose!" "call error command"
124 # Test gdb.string_to_argv.
126 gdb_test "python print (gdb.string_to_argv (\"1 2 3\"))" \
127 {\['1', '2', '3'\]} \
128 "string_to_argv (\"1 2 3\")"
130 gdb_test "python print (gdb.string_to_argv (\"'1 2' 3\"))" \
132 "string_to_argv (\"'1 2' 3\")"
134 gdb_test "python print (gdb.string_to_argv ('\"1 2\" 3'))" \
136 "string_to_argv ('\"1 2\" 3')"
138 gdb_test "python print (gdb.string_to_argv ('1\\ 2 3'))" \
140 "string_to_argv ('1\\ 2 3')"
142 # Test user-defined python commands.
143 gdb_py_test_multiple "input simple user-defined command" \
145 "class test_help (gdb.Command):" "" \
146 " \"\"\"Docstring\"\"\"" "" \
147 " def __init__ (self):" "" \
148 " super (test_help, self).__init__ (\"test_help\", gdb.COMMAND_USER)" "" \
149 " def invoke (self, arg, from_tty):" "" \
150 " print (\"test_cmd output, arg = %s\" % arg)" "" \
154 gdb_test "test_help ugh" "test_cmd output, arg = ugh" "call simple user-defined command"
156 # Make sure the command shows up in `help user-defined`.
157 gdb_test "help user-defined" "User-defined commands.\[\r\n\]+The commands in this class are those defined by the user.\[\r\n\]+Use the \"define\" command to define a command.\[\r\n\]+\[\r\n\]+List of commands:\[\r\n\]+\[\r\n\]+test_help -- Docstring\[\r\n\]+\[\r\n\]+Type \"help\" followed by command name for full documentation.\[\r\n\]+Type \"apropos word\" to search for commands related to \"word\".\[\r\n\]+Command name abbreviations are allowed if unambiguous.\[\r\n\]+" "see user-defined command in `help user-defined`"