]> Git Repo - binutils.git/blob - gdb/testsuite/gdb.python/python.exp
Update copyright year range in all GDB files.
[binutils.git] / gdb / testsuite / gdb.python / python.exp
1 # Copyright (C) 2008-2020 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"
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 gdb_test " " "23" "gdb.execute does not affect repeat history"
125
126 # Test post_event.
127 gdb_py_test_multiple "post event insertion" \
128   "python" "" \
129   "someVal = 0" "" \
130   "class Foo(object):" "" \
131   "  def __call__(self):" "" \
132   "    global someVal" "" \
133   "    someVal += 1" "" \
134   "gdb.post_event(Foo())" "" \
135   "end" ""
136
137 gdb_test "python print (someVal)" "1" "test post event execution"
138 gdb_test "python gdb.post_event(str(1))" "RuntimeError: Posted event is not callable.*" \
139     "test non callable class"
140
141 send_gdb "python gdb.post_event(lambda: invalid())\n"
142 gdb_expect {
143     -re "name 'invalid' is not defined" {
144         pass "test post_event error on receipt"
145     }
146     default {
147         fail "test post_event error on receipt"
148     }
149 }
150
151 # Test (no) pagination of the executed command.
152 gdb_test "show height" {Number of lines gdb thinks are in a page is unlimited\.}
153 set lines 10
154 gdb_test_no_output "set height $lines"
155
156 set test "verify pagination beforehand"
157 gdb_test_multiple "python print (\"\\n\" * $lines)" $test {
158     -re "--Type <RET>" {
159         exp_continue
160     }
161     -re " for more, q to quit" {
162         exp_continue
163     }
164     -re ", c to continue without paging--$" {
165         pass $test
166     }
167 }
168 gdb_test "q" "Quit.*Error while executing Python.*" "verify pagination beforehand: q"
169
170 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
172 set test "verify pagination afterwards"
173 gdb_test_multiple "python print (\"\\n\" * $lines)" $test {
174     -re "--Type <RET>" {
175         exp_continue
176     }
177     -re " for more, q to quit" {
178         exp_continue
179     }
180     -re ", c to continue without paging--$" {
181         pass $test
182     }
183 }
184 gdb_test "q" "Quit.*Error while executing Python.*" "verify pagination afterwards: q"
185
186 gdb_test_no_output "set height 0"
187
188 gdb_test_no_output "python a = gdb.execute('help', to_string=True)" "collect help from uiout"
189
190 gdb_test "python print (a)" ".*aliases -- Aliases of other commands.*" "verify help to uiout"
191
192 # Test PR 12212, using InfThread.selected_thread() when no inferior is
193 # loaded.
194 gdb_py_test_silent_cmd "python nothread = gdb.selected_thread()" "Attempt to aquire thread with no inferior" 1
195 gdb_test "python print (nothread == None)" "True" "ensure that no threads are returned"
196
197 gdb_py_test_multiple "register atexit function" \
198     "python" "" \
199     "import atexit" "" \
200     "def printit(arg):" "" \
201     "  print (arg)" "" \
202     "atexit.register(printit, 'good bye world')" "" \
203     "end" ""
204
205 send_gdb "quit\n"
206 gdb_expect {
207     -re "good bye world" {
208         pass "atexit handling"
209     }
210     default {
211         fail "atexit handling"
212     }
213 }
214
215 # Start with a fresh gdb.
216 clean_restart ${testfile}
217
218 # The following tests require execution.
219
220 if ![runto_main] then {
221     fail "can't run to main"
222     return 0
223 }
224
225 set lineno [gdb_get_line_number "Break to end."]
226 runto $lineno
227
228 # Test gdb.decode_line.
229 gdb_test "python gdb.decode_line(\"main.c:43\")" \
230     "gdb.error: No source file named main.c.*" "test decode_line no source named main"
231
232 gdb_py_test_silent_cmd "python symtab = gdb.decode_line()" "test decode_line current location" 1
233 gdb_test "python print (len(symtab))" "2" "test decode_line current location"
234 gdb_test "python print (symtab\[0\])" "None" "test decode_line expression parse"
235 gdb_test "python print (len(symtab\[1\]))" "1" "test decode_line current location"
236
237 if { [is_remote host] } {
238     set python_c [string_to_regexp "python.c"]
239 } else {
240     set python_c [string_to_regexp "gdb.python/python.c"]
241 }
242 gdb_test "python print (symtab\[1\]\[0\].symtab)" ".*${python_c}" "test decode_line current location filename"
243 gdb_test "python print (symtab\[1\]\[0\].line)" "$lineno" "test decode_line current location line number"
244
245 gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"python.c:26 if foo\")" "test decode_line python.c:26" 1
246 gdb_test "python print (len(symtab))" "2" "test decode_line python.c:26 length"
247 gdb_test "python print (symtab\[0\])" "if foo" "test decode_line expression parse"
248 gdb_test "python print (len(symtab\[1\]))" "1" "test decode_line python.c:26 length"
249 gdb_test "python print (symtab\[1\]\[0\].symtab)" ".*${python_c}" "test decode_line python.c:26 filename"
250 gdb_test "python print (symtab\[1\]\[0\].line)" "26" "test decode_line python.c:26 line number"
251
252 gdb_test "python gdb.decode_line(\"randomfunc\")" \
253     "gdb.error: Function \"randomfunc\" not defined.*" "test decode_line randomfunc"
254 gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"func1\")" "test decode_line func1()" 1
255 gdb_test "python print (len(symtab))" "2" "test decode_line func1 length"
256 gdb_test "python print (len(symtab\[1\]))" "1" "test decode_line func1 length"
257
258 if { [is_remote host] } {
259     set python_1_c [string_to_regexp "python-1.c"]
260 } else {
261     set python_1_c [string_to_regexp "gdb.python/python-1.c"]
262 }
263 gdb_test "python print (symtab\[1\]\[0\].symtab)" ".*${python_1_c}" "test decode_line func1 filename"
264
265 # Set a default value for func1_lineno in case we fail to fetch the line number
266 # below.
267 set func1_lineno "noline"
268
269 # Fetch the line GDB thinks func1 starts at.  This may change depending
270 # on the architecture and on how GDB handles the prologue of the function.
271 gdb_test_multiple "info line func1" "info line func1" {
272     -re "Line ($decimal) of .* starts at address $hex <func1> and ends at $hex <func1\\+$decimal>\.\[\r\n\]+$gdb_prompt $" {
273         # Fetch the line number.
274         set func1_lineno $expect_out(1,string)
275     }
276 }
277
278 gdb_test "python print (symtab\[1\]\[0\].line)" "$func1_lineno" "test decode_line func1 line number"
279 gdb_py_test_silent_cmd {python symtab = gdb.decode_line ("func1,func2")} \
280     "test decode_line func1,func2" 1
281 gdb_test {python print (symtab[0])} ",func2" "stop at comma in linespec"
282
283 gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"*0\")" "Test decode_line *0" 1
284 gdb_test "python print (len(symtab))" "2" "test decode_line *0 result length"
285 gdb_test "python print (symtab\[0\])" "None" "test decode_line *0 unparsed"
286 gdb_test "python print (len(symtab\[1\]))" "1" "test decode_line *0 locations length"
287 gdb_test "python print (symtab\[1\]\[0\].symtab)" "None" "test decode_line *0 filename"
288 gdb_test "python print (symtab\[1\]\[0\].pc)" "0" "test decode_line *0 pc"
289
290 # gdb.write
291 gdb_test "python print (sys.stderr)" ".*gdb._GdbOutputErrorFile (instance|object) at.*" "test stderr location"
292 gdb_test "python print (sys.stdout)" ".*gdb._GdbOutputFile (instance|object) at.*" "test stdout location"
293 gdb_test "python gdb.write(\"Foo\\n\")" "Foo" "test default write"
294 gdb_test "python gdb.write(\"Error stream\\n\", stream=gdb.STDERR)" "Error stream" "test stderr write"
295 gdb_test "python gdb.write(\"Normal stream\\n\", stream=gdb.STDOUT)" "Normal stream" "test stdout write"
296
297 if ![gdb_debug_enabled] {
298   gdb_test "python gdb.write(\"Log stream\\n\", stream=gdb.STDLOG)" "Log stream" "test stdlog write"
299 }
300
301 # Turn on full stack printing for subsequent tests.
302 gdb_py_test_silent_cmd "set python print-stack full" \
303     "Set print-stack full for prompt tests" 1
304
305 # Test prompt substituion
306
307 gdb_py_test_multiple "prompt substitution" \
308   "python" "" \
309   "someCounter = 0" "" \
310   "def prompt(current):" "" \
311   "   global someCounter" "" \
312   "   if (current == \"testfake \"):" "" \
313   "      return None" "" \
314   "   someCounter = someCounter + 1" "" \
315   "   return \"py prompt \" + str (someCounter) + \" \"" "" \
316   "end" ""
317
318 gdb_py_test_multiple "prompt substitution readline" \
319   "python" "" \
320   "pCounter = 0" "" \
321   "def program_prompt(current):" "" \
322   "   global pCounter" "" \
323   "   if (current == \">\"):" "" \
324   "      pCounter = pCounter + 1" "" \
325   "      return \"python line \" + str (pCounter) + \": \"" "" \
326   "   return None" "" \
327   "end" ""
328
329 set newprompt "py prompt 1"
330 set newprompt2 "py prompt 2"
331 set testfake "testfake"
332
333 gdb_test_multiple "python gdb.prompt_hook = prompt" "set the hook" {
334     -re "\[\r\n\]$newprompt $" {
335         pass "set hook"
336     }
337 }
338
339 gdb_test_multiple "set prompt testfake " "set testfake prompt in GDB" {
340     -re "\[\r\n\]$testfake $" {
341         pass "set prompt testfake"
342     }
343 }
344
345 gdb_test_multiple "show prompt" "show testfake prompt" {
346     -re "Gdb's prompt is \"$testfake \"..* $" {
347         pass "show prompt shows guarded prompt"
348     }
349 }
350
351 gdb_test_multiple "set prompt blah " "set blah in GDB" {
352     -re "\[\r\n\]$newprompt2 $" {
353         pass "set prompt blah overriden"
354     }
355 }
356
357 gdb_test_multiple "python gdb.prompt_hook = None" "delete hook" {
358     -re "\[\r\n\]$newprompt2 $" {
359         pass "delete old hook"
360     }
361 }
362
363 gdb_test_multiple "set prompt $gdb_prompt " "set default prompt" {
364     -re "\[\r\n\]$gdb_prompt $" {
365         pass "set default prompt"
366     }
367 }
368
369 set working_dir ""
370 gdb_test_multiple "pwd" "pwd" {
371     -re "Working directory (.*)\\.\[\r\n\]+$gdb_prompt $" {
372         set working_dir $expect_out(1,string)
373     }
374 }
375
376 gdb_test_multiple "python gdb.prompt_hook = program_prompt" "set the hook" {
377     -re "\[\r\n\]$gdb_prompt $" {
378         pass "set programming hook"
379     }
380 }
381
382 gdb_test_multiple "python" "test we ignore substituion for seconday prompts" {
383     -re "\r\n>$" {
384         pass "readline secondary are not substituted"
385     }
386 }
387
388 gdb_test_multiple "end" "end programming" {
389     -re "\[\r\n\]$gdb_prompt $" {
390         pass "end programming"
391     }
392 }
393
394 gdb_py_test_multiple "prompt substitution readline" \
395   "python" "" \
396   "import gdb.command.prompt" "" \
397   "end" ""
398
399 gdb_test_multiple "set extended-prompt one two three " \
400     "set basic extended prompt" {
401     -re "\[\r\n\]one two three $" {
402         pass "set basic extended prompt"
403     }
404 }
405
406 gdb_test_multiple "set extended-prompt \\w " \
407     "set extended prompt working directory" {
408     -re "\[\r\n\][string_to_regexp $working_dir] $" {
409         pass "set extended prompt working directory"
410     }
411 }
412
413 gdb_test_multiple "set extended-prompt some param \\p{python print-stack} " \
414     "set extended prompt parameter" {
415     -re "\[\r\n\]some param full $" {
416         pass "set extended prompt parameter"
417     }
418 }
419
420 # Start with a fresh gdb.
421 clean_restart ${testfile}
422
423 # The following tests require execution.
424
425 if ![runto_main] then {
426     fail "can't run to main"
427     return 0
428 }
429
430 # print-stack settings
431 gdb_test "show python print-stack" \
432     "The mode of Python stack printing on error is \"message\".*" \
433     "Test print-stack show setting. Default is message."
434 gdb_py_test_silent_cmd "set python print-stack full" \
435     "Test print-stack set setting to full" 1
436 gdb_test "show python print-stack" \
437     "The mode of Python stack printing on error is \"full\".*" \
438     "Test print-stack show setting to full"
439 gdb_py_test_silent_cmd "set python print-stack none" \
440     "Test print-stack set setting to none" 1
441 gdb_test "show python print-stack" \
442     "The mode of Python stack printing on error is \"none\".*" \
443     "test print-stack show setting to none"
444
445 gdb_py_test_silent_cmd "set python print-stack message" \
446     "Test print-stack set setting to message" 1
447
448 gdb_py_test_multiple "prompt substitution readline" \
449   "python" "" \
450   "pCounter = 0" "" \
451   "def error_prompt(current):" "" \
452   "   raise RuntimeError(\"Python exception called\")" "" \
453   "end" ""
454
455 gdb_test_multiple "python gdb.prompt_hook = error_prompt" "set the hook" {
456     -re "Python Exception (exceptions.RuntimeError|<(type 'exceptions.|class ')RuntimeError'>) Python exception called.*$gdb_prompt $" {
457         pass "set hook"
458     }
459 }
460
461 gdb_py_test_silent_cmd "python gdb.prompt_hook = None" \
462     "set the hook to default" 1
463
464 gdb_py_test_silent_cmd "set python print-stack full" \
465     "set print-stack full for prompt error test" 1
466
467 gdb_test_multiple "python gdb.prompt_hook = error_prompt" "set the hook" {
468     -re "Traceback.*File.*line.*RuntimeError.*Python exception called.*$gdb_prompt $" {
469         pass "set hook"
470     }
471 }
472
473 gdb_py_test_silent_cmd "python gdb.prompt_hook = None" \
474     "set the hook to default" 1
475
476 # Start with a fresh gdb.
477 clean_restart ${testfile}
478
479 # The following tests require execution.
480
481 if ![runto_main] then {
482     fail "can't run to main"
483     return 0
484 }
485
486 runto [gdb_get_line_number "Break at func2 call site."]
487
488 gdb_py_test_silent_cmd "python line = gdb.selected_frame().find_sal().line" "Get line number of func2 call site" 1
489 gdb_test "python print (gdb.find_pc_line(gdb.selected_frame().pc()).line == line)" "True" "test find_pc_line at func2 call site"
490
491 gdb_py_test_silent_cmd "step" "Step into func2" 1
492 gdb_py_test_silent_cmd "up" "Step out of func2" 1
493
494 gdb_test "python print (gdb.find_pc_line(gdb.selected_frame().pc()).line > line)" "True" "test find_pc_line with resume address"
495
496 gdb_test_no_output "set variable \$cvar1 = 23" "set convenience variable"
497 gdb_test "python print(gdb.convenience_variable('cvar1'))" "23"
498 gdb_test "python print(gdb.convenience_variable('cvar2'))" "None"
499 gdb_test_no_output "python gdb.set_convenience_variable('cvar1', 89)" \
500     "change convenience variable from python"
501 gdb_test "python print(gdb.convenience_variable('cvar1'))" "89" \
502     "print new value of convenience variable from python"
503 gdb_test "print \$cvar1" " = 89" \
504     "print new value of convenience variable from CLI"
505 gdb_test_no_output "python gdb.set_convenience_variable('cvar3', -5)" \
506     "make convenience variable from python"
507 gdb_test "python print(gdb.convenience_variable('cvar3'))" "-5" \
508     "print value of new convenience variable from python"
509 gdb_test_no_output "python gdb.set_convenience_variable('cvar3', None)" \
510     "reset convenience variable from python"
511 gdb_test "python print(gdb.convenience_variable('cvar3'))" "None" \
512     "print reset convenience variable from python"
513 gdb_test "print \$cvar3" "= void" \
514     "print reset convenience variable from CLI"
515
516 # Test PR 23669, the following would invoke the "commands" command instead of
517 # "show commands".
518 gdb_test "python gdb.execute(\"show commands\")" "$decimal  print \\\$cvar3.*"
This page took 0.056365 seconds and 4 git commands to generate.