python: Make gdb.execute("show commands") work (PR 23669)
[external/binutils.git] / gdb / testsuite / gdb.python / python.exp
1 # Copyright (C) 2008-2018 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 # exposing values to Python.
18
19 load_lib gdb-python.exp
20
21 standard_testfile python.c python-1.c
22
23 if {[build_executable $testfile.exp $testfile \
24          [list $srcfile $srcfile2] debug] == -1} {
25     return -1
26 }
27
28 # Start with a fresh gdb.
29 gdb_exit
30 gdb_start
31 gdb_reinitialize_dir $srcdir/$subdir
32
33 set remote_source2_py [gdb_remote_download host \
34                            ${srcdir}/${subdir}/source2.py]
35
36 # Do this instead of the skip_python_check.
37 # We want to do some tests when Python is not present.
38 gdb_test_multiple "python print (23)" "verify python support" {
39     -re "not supported.*$gdb_prompt $"  {
40       unsupported "python support is disabled"
41
42       # If Python is not supported, verify that sourcing a python script
43       # causes an error.
44       gdb_test "source $remote_source2_py" \
45           "Error in sourced command file:.*" \
46           "source source2.py when python disabled"
47
48       # Verify multi-line python commands cause an error.
49       gdb_py_test_multiple "multi-line python command" \
50           "python" "" \
51           "print (23)" "" \
52           "end" "not supported.*"
53
54       return -1
55     }
56     -re "$gdb_prompt $" {}
57 }
58
59 gdb_py_test_multiple "multi-line python command" \
60   "python" "" \
61   "print (23)" "" \
62   "end" "23"
63
64 # Spawn interactive Python help from a multi-line command, thus, after
65 # a secondary prompt.
66
67 with_test_prefix "python interactive help" {
68     set test "python; help(); end"
69     gdb_test_multiple "python\nhelp()\nend" $test {
70         -re ".*help utility.*help> $" {
71             pass $test
72
73             # The "quit" must be seen on the output.  A buggy GDB
74             # would not display it.
75             gdb_test "quit" "^quit.*leaving help.*" "quit help"
76         }
77     }
78 }
79
80 gdb_py_test_multiple "show python command" \
81   "define zzq" "Type commands for definition of .* just \"end\"\\.*" \
82   "python" "" \
83   "print (23)" "" \
84   "end" "" \
85   "end" "" \
86   "show user zzq" "User command \"zzq\":.*  python.*print \\(23\\).*  end"
87
88 gdb_py_test_multiple "indented multi-line python command" \
89   "python" "" \
90   "def foo ():" "" \
91   "  print ('hello, world!')" "" \
92   "foo ()" "" \
93   "end" "hello, world!"
94
95 gdb_test "source $remote_source2_py" "yes" "source source2.py"
96
97 gdb_test "source -s source2.py" "yes" "source -s source2.py"
98
99 set remote_source2_symlink_notpy \
100     [gdb_remote_download host ${srcdir}/${subdir}/source2.py \
101          [standard_output_file "source2-symlink.notpy"]]
102 set remote_source2_symlink_py [standard_output_file "source2-symlink.py"]
103 remote_file host delete $remote_source2_symlink_py
104 set status [remote_exec host "ln -sf $remote_source2_symlink_notpy $remote_source2_symlink_py"]
105 set test "source -s source2-symlink.py"
106 if {[lindex $status 0] == 0} {
107     gdb_test "source -s $remote_source2_symlink_py" "yes" $test
108 } else {
109     unsupported "$test (host does not support symbolic links)"
110 }
111
112 gdb_test "python print (gdb.current_objfile())" "None"
113 gdb_test "python print (gdb.objfiles())" "\\\[\\\]"
114
115 # Test http://bugs.python.org/issue4434 workaround in configure.ac
116 gdb_test "python import itertools; print ('IMPOR'+'TED')" "IMPORTED" "pythonX.Y/lib-dynload/*.so"
117
118 gdb_test_no_output \
119     "python x = gdb.execute('printf \"%d\", 23', to_string = True)"
120 gdb_test "python print (x)" "23"
121
122 gdb_test "python gdb.execute('echo 2\\necho 3\\\\n\\n')" "23" \
123     "multi-line execute"
124
125 # Test post_event.
126 gdb_py_test_multiple "post event insertion" \
127   "python" "" \
128   "someVal = 0" "" \
129   "class Foo(object):" "" \
130   "  def __call__(self):" "" \
131   "    global someVal" "" \
132   "    someVal += 1" "" \
133   "gdb.post_event(Foo())" "" \
134   "end" ""
135
136 gdb_test "python print (someVal)" "1" "test post event execution"
137 gdb_test "python gdb.post_event(str(1))" "RuntimeError: Posted event is not callable.*" "test non callable class"
138
139 # Test (no) pagination of the executed command.
140 gdb_test "show height" {Number of lines gdb thinks are in a page is unlimited\.}
141 set lines 10
142 gdb_test_no_output "set height $lines"
143
144 set test "verify pagination beforehand"
145 gdb_test_multiple "python print (\"\\n\" * $lines)" $test {
146     -re "--Type <RET>" {
147         exp_continue
148     }
149     -re " for more, q to quit" {
150         exp_continue
151     }
152     -re ", c to continue without paging--$" {
153         pass $test
154     }
155 }
156 gdb_test "q" "Quit.*Error while executing Python.*" "verify pagination beforehand: q"
157
158 gdb_test "python if gdb.execute('python print (\"\\\\n\" * $lines)', to_string=True) == \"\\n\" * [expr $lines + 1]: print (\"yes\")" "yes" "gdb.execute does not page"
159
160 set test "verify pagination afterwards"
161 gdb_test_multiple "python print (\"\\n\" * $lines)" $test {
162     -re "--Type <RET>" {
163         exp_continue
164     }
165     -re " for more, q to quit" {
166         exp_continue
167     }
168     -re ", c to continue without paging--$" {
169         pass $test
170     }
171 }
172 gdb_test "q" "Quit.*Error while executing Python.*" "verify pagination afterwards: q"
173
174 gdb_test_no_output "set height 0"
175
176 gdb_test_no_output "python a = gdb.execute('help', to_string=True)" "collect help from uiout"
177
178 gdb_test "python print (a)" ".*aliases -- Aliases of other commands.*" "verify help to uiout"
179
180 # Test PR 12212, using InfThread.selected_thread() when no inferior is
181 # loaded.
182 gdb_py_test_silent_cmd "python nothread = gdb.selected_thread()" "Attempt to aquire thread with no inferior" 1
183 gdb_test "python print (nothread == None)" "True" "ensure that no threads are returned"
184
185 gdb_py_test_multiple "register atexit function" \
186     "python" "" \
187     "import atexit" "" \
188     "def printit(arg):" "" \
189     "  print (arg)" "" \
190     "atexit.register(printit, 'good bye world')" "" \
191     "end" ""
192
193 send_gdb "quit\n"
194 gdb_expect {
195     -re "good bye world" {
196         pass "atexit handling"
197     }
198     default {
199         fail "atexit handling"
200     }
201 }
202
203 # Start with a fresh gdb.
204 clean_restart ${testfile}
205
206 # The following tests require execution.
207
208 if ![runto_main] then {
209     fail "can't run to main"
210     return 0
211 }
212
213 set lineno [gdb_get_line_number "Break to end."]
214 runto $lineno
215
216 # Test gdb.decode_line.
217 gdb_test "python gdb.decode_line(\"main.c:43\")" \
218     "gdb.error: No source file named main.c.*" "test decode_line no source named main"
219
220 gdb_py_test_silent_cmd "python symtab = gdb.decode_line()" "test decode_line current location" 1
221 gdb_test "python print (len(symtab))" "2" "test decode_line current location"
222 gdb_test "python print (symtab\[0\])" "None" "test decode_line expression parse"
223 gdb_test "python print (len(symtab\[1\]))" "1" "test decode_line current location"
224
225 if { [is_remote host] } {
226     set python_c [string_to_regexp "python.c"]
227 } else {
228     set python_c [string_to_regexp "gdb.python/python.c"]
229 }
230 gdb_test "python print (symtab\[1\]\[0\].symtab)" ".*${python_c}" "test decode_line current location filename"
231 gdb_test "python print (symtab\[1\]\[0\].line)" "$lineno" "test decode_line current location line number"
232
233 gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"python.c:26 if foo\")" "test decode_line python.c:26" 1
234 gdb_test "python print (len(symtab))" "2" "test decode_line python.c:26 length"
235 gdb_test "python print (symtab\[0\])" "if foo" "test decode_line expression parse"
236 gdb_test "python print (len(symtab\[1\]))" "1" "test decode_line python.c:26 length"
237 gdb_test "python print (symtab\[1\]\[0\].symtab)" ".*${python_c}" "test decode_line python.c:26 filename"
238 gdb_test "python print (symtab\[1\]\[0\].line)" "26" "test decode_line python.c:26 line number"
239
240 gdb_test "python gdb.decode_line(\"randomfunc\")" \
241     "gdb.error: Function \"randomfunc\" not defined.*" "test decode_line randomfunc"
242 gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"func1\")" "test decode_line func1()" 1
243 gdb_test "python print (len(symtab))" "2" "test decode_line func1 length"
244 gdb_test "python print (len(symtab\[1\]))" "1" "test decode_line func1 length"
245
246 if { [is_remote host] } {
247     set python_1_c [string_to_regexp "python-1.c"]
248 } else {
249     set python_1_c [string_to_regexp "gdb.python/python-1.c"]
250 }
251 gdb_test "python print (symtab\[1\]\[0\].symtab)" ".*${python_1_c}" "test decode_line func1 filename"
252
253 # Set a default value for func1_lineno in case we fail to fetch the line number
254 # below.
255 set func1_lineno "noline"
256
257 # Fetch the line GDB thinks func1 starts at.  This may change depending
258 # on the architecture and on how GDB handles the prologue of the function.
259 gdb_test_multiple "info line func1" "info line func1" {
260     -re "Line ($decimal) of .* starts at address $hex <func1> and ends at $hex <func1\\+$decimal>\.\[\r\n\]+$gdb_prompt $" {
261         # Fetch the line number.
262         set func1_lineno $expect_out(1,string)
263     }
264 }
265
266 gdb_test "python print (symtab\[1\]\[0\].line)" "$func1_lineno" "test decode_line func1 line number"
267 gdb_py_test_silent_cmd {python symtab = gdb.decode_line ("func1,func2")} \
268     "test decode_line func1,func2" 1
269 gdb_test {python print (symtab[0])} ",func2" "stop at comma in linespec"
270
271 gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"*0\")" "Test decode_line *0" 1
272 gdb_test "python print (len(symtab))" "2" "test decode_line *0 result length"
273 gdb_test "python print (symtab\[0\])" "None" "test decode_line *0 unparsed"
274 gdb_test "python print (len(symtab\[1\]))" "1" "test decode_line *0 locations length"
275 gdb_test "python print (symtab\[1\]\[0\].symtab)" "None" "test decode_line *0 filename"
276 gdb_test "python print (symtab\[1\]\[0\].pc)" "0" "test decode_line *0 pc"
277
278 # gdb.write
279 gdb_test "python print (sys.stderr)" ".*gdb.GdbOutputErrorFile (instance|object) at.*" "test stderr location"
280 gdb_test "python print (sys.stdout)" ".*gdb.GdbOutputFile (instance|object) at.*" "test stdout location"
281 gdb_test "python gdb.write(\"Foo\\n\")" "Foo" "test default write"
282 gdb_test "python gdb.write(\"Error stream\\n\", stream=gdb.STDERR)" "Error stream" "test stderr write"
283 gdb_test "python gdb.write(\"Normal stream\\n\", stream=gdb.STDOUT)" "Normal stream" "test stdout write"
284 gdb_test "python gdb.write(\"Log stream\\n\", stream=gdb.STDLOG)" "Log stream" "test stdlog write"
285
286 # Turn on full stack printing for subsequent tests.
287 gdb_py_test_silent_cmd "set python print-stack full" \
288     "Set print-stack full for prompt tests" 1
289
290 # Test prompt substituion
291
292 gdb_py_test_multiple "prompt substitution" \
293   "python" "" \
294   "someCounter = 0" "" \
295   "def prompt(current):" "" \
296   "   global someCounter" "" \
297   "   if (current == \"testfake \"):" "" \
298   "      return None" "" \
299   "   someCounter = someCounter + 1" "" \
300   "   return \"py prompt \" + str (someCounter) + \" \"" "" \
301   "end" ""
302
303 gdb_py_test_multiple "prompt substitution readline" \
304   "python" "" \
305   "pCounter = 0" "" \
306   "def program_prompt(current):" "" \
307   "   global pCounter" "" \
308   "   if (current == \">\"):" "" \
309   "      pCounter = pCounter + 1" "" \
310   "      return \"python line \" + str (pCounter) + \": \"" "" \
311   "   return None" "" \
312   "end" ""
313
314 set newprompt "py prompt 1"
315 set newprompt2 "py prompt 2"
316 set testfake "testfake"
317
318 gdb_test_multiple "python gdb.prompt_hook = prompt" "set the hook" {
319     -re "\[\r\n\]$newprompt $" {
320         pass "set hook"
321     }
322 }
323
324 gdb_test_multiple "set prompt testfake " "set testfake prompt in GDB" {
325     -re "\[\r\n\]$testfake $" {
326         pass "set prompt testfake"
327     }
328 }
329
330 gdb_test_multiple "show prompt" "show testfake prompt" {
331     -re "Gdb's prompt is \"$testfake \"..* $" {
332         pass "show prompt shows guarded prompt"
333     }
334 }
335
336 gdb_test_multiple "set prompt blah " "set blah in GDB" {
337     -re "\[\r\n\]$newprompt2 $" {
338         pass "set prompt blah overriden"
339     }
340 }
341
342 gdb_test_multiple "python gdb.prompt_hook = None" "delete hook" {
343     -re "\[\r\n\]$newprompt2 $" {
344         pass "delete old hook"
345     }
346 }
347
348 gdb_test_multiple "set prompt $gdb_prompt " "set default prompt" {
349     -re "\[\r\n\]$gdb_prompt $" {
350         pass "set default prompt"
351     }
352 }
353
354 set working_dir ""
355 gdb_test_multiple "pwd" "pwd" {
356     -re "Working directory (.*)\\.\[\r\n\]+$gdb_prompt $" {
357         set working_dir $expect_out(1,string)
358     }
359 }
360
361 gdb_test_multiple "python gdb.prompt_hook = program_prompt" "set the hook" {
362     -re "\[\r\n\]$gdb_prompt $" {
363         pass "set programming hook"
364     }
365 }
366
367 gdb_test_multiple "python" "test we ignore substituion for seconday prompts" {
368     -re "\r\n>$" {
369         pass "readline secondary are not substituted"
370     }
371 }
372
373 gdb_test_multiple "end" "end programming" {
374     -re "\[\r\n\]$gdb_prompt $" {
375         pass "end programming"
376     }
377 }
378
379 gdb_py_test_multiple "prompt substitution readline" \
380   "python" "" \
381   "import gdb.command.prompt" "" \
382   "end" ""
383
384 gdb_test_multiple "set extended-prompt one two three " \
385     "set basic extended prompt" {
386     -re "\[\r\n\]one two three $" {
387         pass "set basic extended prompt"
388     }
389 }
390
391 gdb_test_multiple "set extended-prompt \\w " \
392     "set extended prompt working directory" {
393     -re "\[\r\n\][string_to_regexp $working_dir] $" {
394         pass "set extended prompt working directory"
395     }
396 }
397
398 gdb_test_multiple "set extended-prompt some param \\p{python print-stack} " \
399     "set extended prompt parameter" {
400     -re "\[\r\n\]some param full $" {
401         pass "set extended prompt parameter"
402     }
403 }
404
405 # Start with a fresh gdb.
406 clean_restart ${testfile}
407
408 # The following tests require execution.
409
410 if ![runto_main] then {
411     fail "can't run to main"
412     return 0
413 }
414
415 # print-stack settings
416 gdb_test "show python print-stack" \
417     "The mode of Python stack printing on error is \"message\".*" \
418     "Test print-stack show setting. Default is message."
419 gdb_py_test_silent_cmd "set python print-stack full" \
420     "Test print-stack set setting to full" 1
421 gdb_test "show python print-stack" \
422     "The mode of Python stack printing on error is \"full\".*" \
423     "Test print-stack show setting to full"
424 gdb_py_test_silent_cmd "set python print-stack none" \
425     "Test print-stack set setting to none" 1
426 gdb_test "show python print-stack" \
427     "The mode of Python stack printing on error is \"none\".*" \
428     "test print-stack show setting to none"
429
430 gdb_py_test_silent_cmd "set python print-stack message" \
431     "Test print-stack set setting to message" 1
432
433 gdb_py_test_multiple "prompt substitution readline" \
434   "python" "" \
435   "pCounter = 0" "" \
436   "def error_prompt(current):" "" \
437   "   raise RuntimeError(\"Python exception called\")" "" \
438   "end" ""
439
440 gdb_test_multiple "python gdb.prompt_hook = error_prompt" "set the hook" {
441     -re "Python Exception (exceptions.RuntimeError|<(type 'exceptions.|class ')RuntimeError'>) Python exception called.*" {
442         pass "set hook"
443     }
444 }
445
446 gdb_py_test_silent_cmd "python gdb.prompt_hook = None" \
447     "set the hook to default" 1
448
449 gdb_py_test_silent_cmd "set python print-stack full" \
450     "set print-stack full for prompt error test" 1
451
452 gdb_test_multiple "python gdb.prompt_hook = error_prompt" "set the hook" {
453     -re "Traceback.*File.*line.*RuntimeError.*Python exception called.*" {
454         pass "set hook"
455     }
456 }
457
458 gdb_py_test_silent_cmd "python gdb.prompt_hook = None" \
459     "set the hook to default" 1
460
461 # Start with a fresh gdb.
462 clean_restart ${testfile}
463
464 # The following tests require execution.
465
466 if ![runto_main] then {
467     fail "can't run to main"
468     return 0
469 }
470
471 runto [gdb_get_line_number "Break at func2 call site."]
472
473 gdb_py_test_silent_cmd "python line = gdb.selected_frame().find_sal().line" "Get line number of func2 call site" 1
474 gdb_test "python print (gdb.find_pc_line(gdb.selected_frame().pc()).line == line)" "True" "test find_pc_line at func2 call site"
475
476 gdb_py_test_silent_cmd "step" "Step into func2" 1
477 gdb_py_test_silent_cmd "up" "Step out of func2" 1
478
479 gdb_test "python print (gdb.find_pc_line(gdb.selected_frame().pc()).line > line)" "True" "test find_pc_line with resume address"
480
481 gdb_test_no_output "set variable \$cvar1 = 23" "set convenience variable"
482 gdb_test "python print(gdb.convenience_variable('cvar1'))" "23"
483 gdb_test "python print(gdb.convenience_variable('cvar2'))" "None"
484 gdb_test_no_output "python gdb.set_convenience_variable('cvar1', 89)" \
485     "change convenience variable from python"
486 gdb_test "python print(gdb.convenience_variable('cvar1'))" "89" \
487     "print new value of convenience variable from python"
488 gdb_test "print \$cvar1" " = 89" \
489     "print new value of convenience variable from CLI"
490 gdb_test_no_output "python gdb.set_convenience_variable('cvar3', -5)" \
491     "make convenience variable from python"
492 gdb_test "python print(gdb.convenience_variable('cvar3'))" "-5" \
493     "print value of new convenience variable from python"
494 gdb_test_no_output "python gdb.set_convenience_variable('cvar3', None)" \
495     "reset convenience variable from python"
496 gdb_test "python print(gdb.convenience_variable('cvar3'))" "None" \
497     "print reset convenience variable from python"
498 gdb_test "print \$cvar3" "= void" \
499     "print reset convenience variable from CLI"
500
501 # Test PR 23669, the following would invoke the "commands" command instead of
502 # "show commands".
503 gdb_test "python gdb.execute(\"show commands\")" "$decimal  print \\\$cvar3.*"