OSDN Git Service

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