1# Copyright 1998, 1999, 2001, 2003, 2004, 2007, 2008, 2009, 2010, 2011
2# Free Software Foundation, Inc.
3
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17# This file was written by Elena Zannoni. (ezannoni@cygnus.com)
18
19if $tracelevel then {
20	strace $tracelevel
21	}
22
23global usestubs
24
25
26#
27# test running programs
28#
29
30set testfile "break"
31set srcfile ${testfile}.c
32set srcfile1 ${testfile}1.c
33set binfile ${objdir}/${subdir}/${testfile}
34
35if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}0.o" object {debug nowarnings}] != "" } {
36     untested define.exp
37     return -1
38}
39
40if  { [gdb_compile "${srcdir}/${subdir}/${srcfile1}" "${binfile}1.o" object {debug nowarnings}] != "" } {
41     untested define.exp
42     return -1
43}
44
45if  { [gdb_compile "${binfile}0.o ${binfile}1.o" "${binfile}" executable {debug nowarnings}] != "" } {
46     untested define.exp
47     return -1
48}
49
50gdb_exit
51gdb_start
52gdb_reinitialize_dir $srcdir/$subdir
53gdb_load ${binfile}
54
55set bp_location1 [gdb_get_line_number "set breakpoint 1 here"]
56set bp_location11 [gdb_get_line_number "set breakpoint 11 here"]
57
58if ![runto_main] then { fail "define tests suppressed" }
59
60# Verify that GDB allows a user to define their very own commands.
61#
62gdb_test_multiple "define nextwhere" "define user command: nextwhere" {
63    -re "Type commands for definition of \"nextwhere\".\r\nEnd with a line saying just \"end\".\r\n>$" {
64	gdb_test "next\nbt\nend" "" \
65	    "define user command: nextwhere"
66    }
67}
68
69# Verify that those commands work as gdb_expected.
70#
71gdb_test "nextwhere" \
72    "$bp_location1\[ \t\]*printf.*#0\[ \t\]*main.*:$bp_location1.*" \
73    "use user command: nextwhere"
74
75# Verify that a user can define a command whose spelling is a
76# proper substring of another user-defined command.
77#
78gdb_test_multiple "define nextwh" "define user command: nextwh" {
79    -re "Type commands for definition of \"nextwh\".\r\nEnd with a line saying just \"end\".\r\n>$" {
80	gdb_test "next 2\nbt\nend" "" \
81	    "define user command: nextwh"
82    }
83}
84
85# Verify that a user can redefine their commands.  (Test both the
86# confirmed and unconfirmed cases.)
87#
88gdb_test "define nextwhere" \
89    "Command \"nextwhere\" not redefined.*" \
90    "redefine user command aborted: nextwhere" \
91    "Redefine command \"nextwhere\".*y or n. $" \
92    "n"
93
94send_gdb "define nextwhere\n"
95gdb_expect {
96  -re "Redefine command \"nextwhere\".*y or n. $"\
97          {send_gdb "y\n"
98           gdb_expect {
99             -re "Type commands for definition of \"nextwhere\".\r\nEnd with a line saying just \"end\".\r\n>$"\
100                     {send_gdb "bt\nnext\nend\n"
101                      gdb_expect {
102                        -re "$gdb_prompt $"\
103                                {pass "redefine user command: nextwhere"}
104                        timeout {fail "(timeout) redefine user command: nextwhere"}
105                      }
106                     }
107             timeout {fail "(timeout) redefine user command: nextwhere"}
108           }
109          }
110  -re "$gdb_prompt $"\
111          {fail "redefine user command: nextwhere"}
112  timeout {fail "(timeout) redefine user command: nextwhere"}
113}
114
115# Verify that GDB gracefully handles an attempt to redefine the
116# help text for a builtin command.
117#
118gdb_test "document step" "Command \"step\" is built-in.*" \
119    "redocumenting builtin command disallowed"
120
121# Verify that a user can document their own commands.  (And redocument
122# them.)
123#
124gdb_test_multiple "document nextwhere" "document user command: nextwhere" {
125    -re "Type documentation for \"nextwhere\".\r\nEnd with a line saying just \"end\".\r\n>$" {
126	gdb_test "A next command that first shows you where you're stepping from.\nend" \
127	    "" \
128	    "document user command: nextwhere"
129    }
130}
131
132gdb_test_multiple "document nextwhere" "re-document user command: nextwhere" {
133    -re "Type documentation for \"nextwhere\".\r\nEnd with a line saying just \"end\".\r\n>$" {
134	gdb_test "A next command that first shows you where you're stepping from.\nend" \
135	    "" \
136	    "re-document user command: nextwhere"
137    }
138}
139
140gdb_test "help nextwhere" \
141    "A next command that first shows you where you're stepping from.*" \
142    "help user command: nextwhere"
143
144# Verify that the document command preserves whitespace in the beginning of the line.
145#
146gdb_test_multiple "document nextwhere" "set up whitespace in help string" {
147    -re "Type documentation for \"nextwhere\".\r\nEnd with a line saying just \"end\".\r\n>$" {
148	gdb_test "   A next command that first shows you where you're stepping from.\nend" \
149	    "" \
150	    "set up whitespace in help string"
151    }
152}
153
154gdb_test "help nextwhere" \
155    "   A next command that first shows you where you're stepping from.*" \
156    "preserve whitespace in help string"
157
158# Verify that the command parser doesn't require a space after an 'if'
159# command in a user defined function.
160#
161gdb_test_multiple "define ifnospace" "define user command: ifnospace" \
162{
163  -re "Type commands for definition of \"ifnospace\".\r\nEnd with a line saying just \"end\".\r\n>$" \
164    {
165      gdb_test_multiple "if(3<4)\nprint \"hi there\\n\"\nend\nend" "send body of ifnospace"  \
166        {
167         -re "$gdb_prompt $"\
168                 {pass "define user command: ifnospace"}
169        }
170    }
171}
172
173gdb_test "ifnospace" ".*hi there.*" "test ifnospace is parsed correctly"
174
175# Verify that the command parser doesn't require a space after an 'while'
176# command in a user defined function.
177#
178gdb_test_multiple "define whilenospace" "define user command: whilenospace" \
179{
180  -re "Type commands for definition of \"whilenospace\".\r\nEnd with a line saying just \"end\".\r\n>$" \
181    {
182      gdb_test_multiple "set \$i=1\nwhile(\$i<2)\nset \$i=2\nprint \"hi there\\n\"\nend\nend" "send body of whilenospace" \
183         {
184           -re "$gdb_prompt $" \
185                   {pass "define user command: whilenospace"}
186         }
187    }
188}
189
190gdb_test "whilenospace" ".*hi there.*" "test whilenospace is parsed correctly"
191
192# Verify that the user can "hook" a builtin command.  We choose to
193# hook the "stop" pseudo command, and we'll define it to use a user-
194# define command.
195#
196gdb_test_multiple "define user-bt" "define user command: user-bt" {
197    -re "Type commands for definition of \"user-bt\".\r\nEnd with a line saying just \"end\".\r\n>$" {
198	gdb_test "bt\nend" \
199	    "" \
200	    "define user command: user-bt"
201    }
202}
203
204gdb_test_multiple "define hook-stop" "define hook-stop command" {
205    -re "Type commands for definition of \"hook-stop\".\r\nEnd with a line saying just \"end\".\r\n>$" {
206	gdb_test "user-b\nend" \
207	    "" \
208	    "define hook-stop command"
209    }
210}
211
212gdb_test "next" "#0\[ \t\]*main.*:$bp_location11.*" \
213    "use hook-stop command"
214
215# Verify that GDB responds gracefully to an attempt to define a "hook
216# command" which doesn't exist.  (Test both the confirmed and unconfirmed
217# cases.)
218#
219gdb_test "define hook-bar" \
220    "Not confirmed.*" \
221    "define hook undefined command aborted: bar" \
222    "warning: Your new `hook-bar' command does not hook any existing command.\r\nProceed.*y or n. $" \
223    "n"
224
225send_gdb "define hook-bar\n"
226gdb_expect {
227  -re "warning: Your new `hook-bar' command does not hook any existing command.\r\nProceed.*y or n. $"\
228          {send_gdb "y\n"
229           gdb_expect {
230             -re "Type commands for definition of \"hook-bar\".\r\nEnd with a line saying just \"end\".\r\n>$"\
231                     {send_gdb "nextwhere\nend\n"
232                      gdb_expect {
233                        -re "$gdb_prompt $"\
234                                {pass "define hook undefined command: bar"}
235                        timeout {fail "(timeout) define hook undefined command: bar"}
236                      }
237                     }
238             -re "$gdb_prompt $"\
239                     {fail "define hook undefined command: bar"}
240             timeout {fail "(timeout) define hook undefined command: bar"}
241           }
242          }
243  -re "$gdb_prompt $"\
244          {fail "define hook undefined command: bar"}
245  timeout {fail "(timeout) define hook undefined command: bar"}
246}
247
248# Test creation of an additional target subcommand.
249gdb_test_multiple "define target testsuite" "" {
250    -re "Type commands for definition of \"target testsuite\".\r\nEnd with a line saying just \"end\".\r\n>$" {
251	gdb_test "printf \"hello\\n\"\nend" "" "define target testsuite"
252    }
253}
254gdb_test_multiple "document target testsuite" "" {
255    -re "Type documentation for \"target testsuite\".\r\nEnd with a line saying just \"end\".\r\n>$" {
256	gdb_test "A test target.\nend" "" "document target testsuite"
257    }
258}
259
260gdb_test "help target" ".*A test target.*"
261gdb_test "target testsuite" "hello"
262gdb_test "show user target testsuite" "User command \"target testsuite\":\r\n  printf \"hello\\\\n\"\r\n"
263
264# We should even be able to hook subcommands.
265gdb_test_multiple "define target hook-testsuite" "" {
266    -re "Type commands for definition of \"target hook-testsuite\".\r\nEnd with a line saying just \"end\".\r\n>$" {
267	gdb_test "printf \"one\\n\"\nend" "" "define target hook-testsuite"
268    }
269}
270
271gdb_test_multiple "define target hookpost-testsuite" "" {
272    -re "Type commands for definition of \"target hookpost-testsuite\".\r\nEnd with a line saying just \"end\".\r\n>$" {
273	gdb_test "printf \"two\\n\"\nend" "" "define target hookpost-testsuite"
274    }
275}
276
277gdb_test "target testsuite" "one\r\nhello\r\ntwo" "target testsuite with hooks"
278
279# This is a quasi-define command: Verify that the user can redefine
280# GDB's gdb_prompt.
281#
282gdb_test_multiple "set prompt \\(blah\\) " "set gdb_prompt" {
283    -re "\\(blah\\) $" {
284	pass "set gdb_prompt"
285    }
286}
287
288gdb_test_multiple "set prompt \\(gdb\\) " "reset gdb_prompt" {
289    -re "$gdb_prompt $" {
290	pass "reset gdb_prompt"
291    }
292}
293
294gdb_exit
295return 0
296