1 # Copyright (C) 2008-2018 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 # exposing values to Python.
19 load_lib gdb-python.exp
21 standard_testfile python.c python-1.c
23 if {[build_executable $testfile.exp $testfile \
24 [list $srcfile $srcfile2] debug] == -1} {
28 # Start with a fresh gdb.
31 gdb_reinitialize_dir $srcdir/$subdir
33 set remote_source2_py [gdb_remote_download host \
34 ${srcdir}/${subdir}/source2.py]
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"
42 # If Python is not supported, verify that sourcing a python script
44 gdb_test "source $remote_source2_py" \
45 "Error in sourced command file:.*" \
46 "source source2.py when python disabled"
48 # Verify multi-line python commands cause an error.
49 gdb_py_test_multiple "multi-line python command" \
52 "end" "not supported.*"
56 -re "$gdb_prompt $" {}
59 gdb_py_test_multiple "multi-line python command" \
64 # Spawn interactive Python help from a multi-line command, thus, after
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> $" {
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"
80 gdb_py_test_multiple "show python command" \
81 "define zzq" "Type commands for definition of .* just \"end\"\\.*" \
86 "show user zzq" "User command \"zzq\":.* python.*print \\(23\\).* end"
88 gdb_py_test_multiple "indented multi-line python command" \
91 " print ('hello, world!')" "" \
95 gdb_test "source $remote_source2_py" "yes" "source source2.py"
97 gdb_test "source -s source2.py" "yes" "source -s source2.py"
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
109 unsupported "$test (host does not support symbolic links)"
112 gdb_test "python print (gdb.current_objfile())" "None"
113 gdb_test "python print (gdb.objfiles())" "\\\[\\\]"
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"
119 "python x = gdb.execute('printf \"%d\", 23', to_string = True)"
120 gdb_test "python print (x)" "23"
122 gdb_test "python gdb.execute('echo 2\\necho 3\\\\n\\n')" "23" \
126 gdb_py_test_multiple "post event insertion" \
129 "class Foo(object):" "" \
130 " def __call__(self):" "" \
131 " global someVal" "" \
133 "gdb.post_event(Foo())" "" \
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.*" \
138 "test non callable class"
140 send_gdb "python gdb.post_event(lambda: invalid())\n"
142 -re "name 'invalid' is not defined" {
143 pass "test post_event error on receipt"
146 fail "test post_event error on receipt"
150 # Test (no) pagination of the executed command.
151 gdb_test "show height" {Number of lines gdb thinks are in a page is unlimited\.}
153 gdb_test_no_output "set height $lines"
155 set test "verify pagination beforehand"
156 gdb_test_multiple "python print (\"\\n\" * $lines)" $test {
160 -re " for more, q to quit" {
163 -re ", c to continue without paging--$" {
167 gdb_test "q" "Quit.*Error while executing Python.*" "verify pagination beforehand: q"
169 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"
171 set test "verify pagination afterwards"
172 gdb_test_multiple "python print (\"\\n\" * $lines)" $test {
176 -re " for more, q to quit" {
179 -re ", c to continue without paging--$" {
183 gdb_test "q" "Quit.*Error while executing Python.*" "verify pagination afterwards: q"
185 gdb_test_no_output "set height 0"
187 gdb_test_no_output "python a = gdb.execute('help', to_string=True)" "collect help from uiout"
189 gdb_test "python print (a)" ".*aliases -- Aliases of other commands.*" "verify help to uiout"
191 # Test PR 12212, using InfThread.selected_thread() when no inferior is
193 gdb_py_test_silent_cmd "python nothread = gdb.selected_thread()" "Attempt to aquire thread with no inferior" 1
194 gdb_test "python print (nothread == None)" "True" "ensure that no threads are returned"
196 gdb_py_test_multiple "register atexit function" \
199 "def printit(arg):" "" \
201 "atexit.register(printit, 'good bye world')" "" \
206 -re "good bye world" {
207 pass "atexit handling"
210 fail "atexit handling"
214 # Start with a fresh gdb.
215 clean_restart ${testfile}
217 # The following tests require execution.
219 if ![runto_main] then {
220 fail "can't run to main"
224 set lineno [gdb_get_line_number "Break to end."]
227 # Test gdb.decode_line.
228 gdb_test "python gdb.decode_line(\"main.c:43\")" \
229 "gdb.error: No source file named main.c.*" "test decode_line no source named main"
231 gdb_py_test_silent_cmd "python symtab = gdb.decode_line()" "test decode_line current location" 1
232 gdb_test "python print (len(symtab))" "2" "test decode_line current location"
233 gdb_test "python print (symtab\[0\])" "None" "test decode_line expression parse"
234 gdb_test "python print (len(symtab\[1\]))" "1" "test decode_line current location"
236 if { [is_remote host] } {
237 set python_c [string_to_regexp "python.c"]
239 set python_c [string_to_regexp "gdb.python/python.c"]
241 gdb_test "python print (symtab\[1\]\[0\].symtab)" ".*${python_c}" "test decode_line current location filename"
242 gdb_test "python print (symtab\[1\]\[0\].line)" "$lineno" "test decode_line current location line number"
244 gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"python.c:26 if foo\")" "test decode_line python.c:26" 1
245 gdb_test "python print (len(symtab))" "2" "test decode_line python.c:26 length"
246 gdb_test "python print (symtab\[0\])" "if foo" "test decode_line expression parse"
247 gdb_test "python print (len(symtab\[1\]))" "1" "test decode_line python.c:26 length"
248 gdb_test "python print (symtab\[1\]\[0\].symtab)" ".*${python_c}" "test decode_line python.c:26 filename"
249 gdb_test "python print (symtab\[1\]\[0\].line)" "26" "test decode_line python.c:26 line number"
251 gdb_test "python gdb.decode_line(\"randomfunc\")" \
252 "gdb.error: Function \"randomfunc\" not defined.*" "test decode_line randomfunc"
253 gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"func1\")" "test decode_line func1()" 1
254 gdb_test "python print (len(symtab))" "2" "test decode_line func1 length"
255 gdb_test "python print (len(symtab\[1\]))" "1" "test decode_line func1 length"
257 if { [is_remote host] } {
258 set python_1_c [string_to_regexp "python-1.c"]
260 set python_1_c [string_to_regexp "gdb.python/python-1.c"]
262 gdb_test "python print (symtab\[1\]\[0\].symtab)" ".*${python_1_c}" "test decode_line func1 filename"
264 # Set a default value for func1_lineno in case we fail to fetch the line number
266 set func1_lineno "noline"
268 # Fetch the line GDB thinks func1 starts at. This may change depending
269 # on the architecture and on how GDB handles the prologue of the function.
270 gdb_test_multiple "info line func1" "info line func1" {
271 -re "Line ($decimal) of .* starts at address $hex <func1> and ends at $hex <func1\\+$decimal>\.\[\r\n\]+$gdb_prompt $" {
272 # Fetch the line number.
273 set func1_lineno $expect_out(1,string)
277 gdb_test "python print (symtab\[1\]\[0\].line)" "$func1_lineno" "test decode_line func1 line number"
278 gdb_py_test_silent_cmd {python symtab = gdb.decode_line ("func1,func2")} \
279 "test decode_line func1,func2" 1
280 gdb_test {python print (symtab[0])} ",func2" "stop at comma in linespec"
282 gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"*0\")" "Test decode_line *0" 1
283 gdb_test "python print (len(symtab))" "2" "test decode_line *0 result length"
284 gdb_test "python print (symtab\[0\])" "None" "test decode_line *0 unparsed"
285 gdb_test "python print (len(symtab\[1\]))" "1" "test decode_line *0 locations length"
286 gdb_test "python print (symtab\[1\]\[0\].symtab)" "None" "test decode_line *0 filename"
287 gdb_test "python print (symtab\[1\]\[0\].pc)" "0" "test decode_line *0 pc"
290 gdb_test "python print (sys.stderr)" ".*gdb.GdbOutputErrorFile (instance|object) at.*" "test stderr location"
291 gdb_test "python print (sys.stdout)" ".*gdb.GdbOutputFile (instance|object) at.*" "test stdout location"
292 gdb_test "python gdb.write(\"Foo\\n\")" "Foo" "test default write"
293 gdb_test "python gdb.write(\"Error stream\\n\", stream=gdb.STDERR)" "Error stream" "test stderr write"
294 gdb_test "python gdb.write(\"Normal stream\\n\", stream=gdb.STDOUT)" "Normal stream" "test stdout write"
295 gdb_test "python gdb.write(\"Log stream\\n\", stream=gdb.STDLOG)" "Log stream" "test stdlog write"
297 # Turn on full stack printing for subsequent tests.
298 gdb_py_test_silent_cmd "set python print-stack full" \
299 "Set print-stack full for prompt tests" 1
301 # Test prompt substituion
303 gdb_py_test_multiple "prompt substitution" \
305 "someCounter = 0" "" \
306 "def prompt(current):" "" \
307 " global someCounter" "" \
308 " if (current == \"testfake \"):" "" \
310 " someCounter = someCounter + 1" "" \
311 " return \"py prompt \" + str (someCounter) + \" \"" "" \
314 gdb_py_test_multiple "prompt substitution readline" \
317 "def program_prompt(current):" "" \
318 " global pCounter" "" \
319 " if (current == \">\"):" "" \
320 " pCounter = pCounter + 1" "" \
321 " return \"python line \" + str (pCounter) + \": \"" "" \
325 set newprompt "py prompt 1"
326 set newprompt2 "py prompt 2"
327 set testfake "testfake"
329 gdb_test_multiple "python gdb.prompt_hook = prompt" "set the hook" {
330 -re "\[\r\n\]$newprompt $" {
335 gdb_test_multiple "set prompt testfake " "set testfake prompt in GDB" {
336 -re "\[\r\n\]$testfake $" {
337 pass "set prompt testfake"
341 gdb_test_multiple "show prompt" "show testfake prompt" {
342 -re "Gdb's prompt is \"$testfake \"..* $" {
343 pass "show prompt shows guarded prompt"
347 gdb_test_multiple "set prompt blah " "set blah in GDB" {
348 -re "\[\r\n\]$newprompt2 $" {
349 pass "set prompt blah overriden"
353 gdb_test_multiple "python gdb.prompt_hook = None" "delete hook" {
354 -re "\[\r\n\]$newprompt2 $" {
355 pass "delete old hook"
359 gdb_test_multiple "set prompt $gdb_prompt " "set default prompt" {
360 -re "\[\r\n\]$gdb_prompt $" {
361 pass "set default prompt"
366 gdb_test_multiple "pwd" "pwd" {
367 -re "Working directory (.*)\\.\[\r\n\]+$gdb_prompt $" {
368 set working_dir $expect_out(1,string)
372 gdb_test_multiple "python gdb.prompt_hook = program_prompt" "set the hook" {
373 -re "\[\r\n\]$gdb_prompt $" {
374 pass "set programming hook"
378 gdb_test_multiple "python" "test we ignore substituion for seconday prompts" {
380 pass "readline secondary are not substituted"
384 gdb_test_multiple "end" "end programming" {
385 -re "\[\r\n\]$gdb_prompt $" {
386 pass "end programming"
390 gdb_py_test_multiple "prompt substitution readline" \
392 "import gdb.command.prompt" "" \
395 gdb_test_multiple "set extended-prompt one two three " \
396 "set basic extended prompt" {
397 -re "\[\r\n\]one two three $" {
398 pass "set basic extended prompt"
402 gdb_test_multiple "set extended-prompt \\w " \
403 "set extended prompt working directory" {
404 -re "\[\r\n\][string_to_regexp $working_dir] $" {
405 pass "set extended prompt working directory"
409 gdb_test_multiple "set extended-prompt some param \\p{python print-stack} " \
410 "set extended prompt parameter" {
411 -re "\[\r\n\]some param full $" {
412 pass "set extended prompt parameter"
416 # Start with a fresh gdb.
417 clean_restart ${testfile}
419 # The following tests require execution.
421 if ![runto_main] then {
422 fail "can't run to main"
426 # print-stack settings
427 gdb_test "show python print-stack" \
428 "The mode of Python stack printing on error is \"message\".*" \
429 "Test print-stack show setting. Default is message."
430 gdb_py_test_silent_cmd "set python print-stack full" \
431 "Test print-stack set setting to full" 1
432 gdb_test "show python print-stack" \
433 "The mode of Python stack printing on error is \"full\".*" \
434 "Test print-stack show setting to full"
435 gdb_py_test_silent_cmd "set python print-stack none" \
436 "Test print-stack set setting to none" 1
437 gdb_test "show python print-stack" \
438 "The mode of Python stack printing on error is \"none\".*" \
439 "test print-stack show setting to none"
441 gdb_py_test_silent_cmd "set python print-stack message" \
442 "Test print-stack set setting to message" 1
444 gdb_py_test_multiple "prompt substitution readline" \
447 "def error_prompt(current):" "" \
448 " raise RuntimeError(\"Python exception called\")" "" \
451 gdb_test_multiple "python gdb.prompt_hook = error_prompt" "set the hook" {
452 -re "Python Exception (exceptions.RuntimeError|<(type 'exceptions.|class ')RuntimeError'>) Python exception called.*" {
457 gdb_py_test_silent_cmd "python gdb.prompt_hook = None" \
458 "set the hook to default" 1
460 gdb_py_test_silent_cmd "set python print-stack full" \
461 "set print-stack full for prompt error test" 1
463 gdb_test_multiple "python gdb.prompt_hook = error_prompt" "set the hook" {
464 -re "Traceback.*File.*line.*RuntimeError.*Python exception called.*" {
469 gdb_py_test_silent_cmd "python gdb.prompt_hook = None" \
470 "set the hook to default" 1
472 # Start with a fresh gdb.
473 clean_restart ${testfile}
475 # The following tests require execution.
477 if ![runto_main] then {
478 fail "can't run to main"
482 runto [gdb_get_line_number "Break at func2 call site."]
484 gdb_py_test_silent_cmd "python line = gdb.selected_frame().find_sal().line" "Get line number of func2 call site" 1
485 gdb_test "python print (gdb.find_pc_line(gdb.selected_frame().pc()).line == line)" "True" "test find_pc_line at func2 call site"
487 gdb_py_test_silent_cmd "step" "Step into func2" 1
488 gdb_py_test_silent_cmd "up" "Step out of func2" 1
490 gdb_test "python print (gdb.find_pc_line(gdb.selected_frame().pc()).line > line)" "True" "test find_pc_line with resume address"
492 gdb_test_no_output "set variable \$cvar1 = 23" "set convenience variable"
493 gdb_test "python print(gdb.convenience_variable('cvar1'))" "23"
494 gdb_test "python print(gdb.convenience_variable('cvar2'))" "None"
495 gdb_test_no_output "python gdb.set_convenience_variable('cvar1', 89)" \
496 "change convenience variable from python"
497 gdb_test "python print(gdb.convenience_variable('cvar1'))" "89" \
498 "print new value of convenience variable from python"
499 gdb_test "print \$cvar1" " = 89" \
500 "print new value of convenience variable from CLI"
501 gdb_test_no_output "python gdb.set_convenience_variable('cvar3', -5)" \
502 "make convenience variable from python"
503 gdb_test "python print(gdb.convenience_variable('cvar3'))" "-5" \
504 "print value of new convenience variable from python"
505 gdb_test_no_output "python gdb.set_convenience_variable('cvar3', None)" \
506 "reset convenience variable from python"
507 gdb_test "python print(gdb.convenience_variable('cvar3'))" "None" \
508 "print reset convenience variable from python"
509 gdb_test "print \$cvar3" "= void" \
510 "print reset convenience variable from CLI"
512 # Test PR 23669, the following would invoke the "commands" command instead of
514 gdb_test "python gdb.execute(\"show commands\")" "$decimal print \\\$cvar3.*"