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