Commit | Line | Data |
---|---|---|
4c38e0a4 | 1 | # Copyright (C) 2008, 2009, 2010 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 | ||
19 | if $tracelevel then { | |
20 | strace $tracelevel | |
21 | } | |
22 | ||
a2c09bd0 DE |
23 | load_lib gdb-python.exp |
24 | ||
cb2e07a6 PM |
25 | set testfile "python" |
26 | set srcfile ${testfile}.c | |
e7fbb131 | 27 | set srcfile1 ${testfile}-1.c |
cb2e07a6 PM |
28 | set binfile ${objdir}/${subdir}/${testfile} |
29 | ||
e7fbb131 PA |
30 | if { [gdb_compile "${srcdir}/${subdir}/${srcfile} ${srcdir}/${subdir}/${srcfile1}" \ |
31 | ${binfile} executable {debug}] != "" } { | |
cb2e07a6 PM |
32 | untested "Could not compile $binfile." |
33 | return -1 | |
34 | } | |
d57a3c85 | 35 | |
cb2e07a6 | 36 | # Start with a fresh gdb. |
d57a3c85 TJB |
37 | gdb_exit |
38 | gdb_start | |
39 | gdb_reinitialize_dir $srcdir/$subdir | |
40 | ||
cb2e07a6 PM |
41 | # Skip all tests if Python scripting is not enabled. |
42 | if { [skip_python_tests] } { continue } | |
43 | ||
d57a3c85 TJB |
44 | gdb_test_multiple "python print 23" "verify python support" { |
45 | -re "not supported.*$gdb_prompt $" { | |
46 | unsupported "python support is disabled" | |
9e14a9ba JB |
47 | |
48 | # If Python is not supported, verify that sourcing a python script | |
49 | # causes an error. | |
50 | gdb_test "source $srcdir/$subdir/source2.py" "Error in sourced command file:.*" | |
d57a3c85 TJB |
51 | return -1 |
52 | } | |
53 | -re "$gdb_prompt $" {} | |
54 | } | |
55 | ||
56 | # Usage: gdb_py_test_multiple NAME INPUT RESULT {INPUT RESULT}... | |
57 | # Run a test named NAME, consisting of multiple lines of input. | |
58 | # After each input line INPUT, search for result line RESULT. | |
59 | # Succeed if all results are seen; fail otherwise. | |
60 | proc gdb_py_test_multiple {name args} { | |
61 | global gdb_prompt | |
62 | foreach {input result} $args { | |
63 | if {[gdb_test_multiple $input "$name - $input" { | |
64 | -re "\[\r\n\]*($result)\[\r\n\]+($gdb_prompt | *>)$" { | |
65 | pass "$name - $input" | |
66 | } | |
67 | }]} { | |
68 | return 1 | |
69 | } | |
70 | } | |
71 | return 0 | |
72 | } | |
73 | ||
74 | gdb_py_test_multiple "multi-line python command" \ | |
75 | "python" "" \ | |
76 | "print 23" "" \ | |
77 | "end" "23" | |
78 | ||
79 | gdb_py_test_multiple "show python command" \ | |
80 | "define zzq" "Type commands for definition of .* just \"end\"\\.*" \ | |
81 | "python" "" \ | |
82 | "print 23" "" \ | |
83 | "end" "" \ | |
84 | "end" "" \ | |
adb483fe | 85 | "show user zzq" "User command \"zzq\":.* python.*print 23.* end" |
311a4e6b TJB |
86 | |
87 | gdb_py_test_multiple "indented multi-line python command" \ | |
88 | "python" "" \ | |
89 | "def foo ():" "" \ | |
90 | " print 'hello, world!'" "" \ | |
91 | "foo ()" "" \ | |
92 | "end" "hello, world!" | |
89c73ade | 93 | |
9e14a9ba JB |
94 | gdb_test "source $srcdir/$subdir/source2.py" "yes" |
95 | ||
89c73ade TT |
96 | gdb_test "python print gdb.current_objfile()" "None" |
97 | gdb_test "python print gdb.objfiles()" "\\\[\\\]" | |
3bebe2f2 JK |
98 | |
99 | # Test http://bugs.python.org/issue4434 workaround in configure.ac | |
100 | gdb_test "python import itertools; print 'IMPOR'+'TED'" "IMPORTED" "pythonX.Y/lib-dynload/*.so" | |
bc9f0842 TT |
101 | |
102 | gdb_test_no_output \ | |
103 | "python x = gdb.execute('printf \"%d\", 23', to_string = True)" | |
104 | gdb_test "python print x" "23" | |
5da1313b | 105 | |
ca5c20b6 PM |
106 | # Test post_event. |
107 | gdb_py_test_multiple "post event insertion" \ | |
108 | "python" "" \ | |
109 | "someVal = 0" "" \ | |
70ebf4ed | 110 | "class Foo(object):" "" \ |
ca5c20b6 PM |
111 | " def __call__(self):" "" \ |
112 | " global someVal" "" \ | |
113 | " someVal += 1" "" \ | |
114 | "gdb.post_event(Foo())" "" \ | |
115 | "end" "" | |
116 | ||
117 | gdb_test "python print someVal" "1" "test post event execution" | |
118 | gdb_test "python gdb.post_event(str(1))" "RuntimeError: Posted event is not callable.*" "Test non callable class" | |
119 | ||
5da1313b JK |
120 | # Test (no) pagination of the executed command. |
121 | gdb_test "show height" {Number of lines gdb thinks are in a page is unlimited\.} | |
122 | set lines 10 | |
123 | gdb_test_no_output "set height $lines" | |
124 | ||
125 | set test "verify pagination beforehand" | |
126 | gdb_test_multiple "python print \"\\n\" * $lines" $test { | |
127 | -re "---Type <return> to continue, or q <return> to quit---$" { | |
128 | pass $test | |
129 | } | |
130 | } | |
131 | gdb_test "q" "Quit" "verify pagination beforehand: q" | |
132 | ||
133 | 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" | |
134 | ||
135 | set test "verify pagination afterwards" | |
136 | gdb_test_multiple "python print \"\\n\" * $lines" $test { | |
137 | -re "---Type <return> to continue, or q <return> to quit---$" { | |
138 | pass $test | |
139 | } | |
140 | } | |
141 | gdb_test "q" "Quit" "verify pagination afterwards: q" | |
cb2e07a6 | 142 | |
8d4d924b JK |
143 | gdb_test_no_output "set height 0" |
144 | ||
145 | gdb_test_no_output "python a = gdb.execute('help', to_string=True)" "collect help from uiout" | |
146 | ||
147 | gdb_test "python print a" ".*aliases -- Aliases of other commands.*" "verify help to uiout" | |
148 | ||
ea976c60 PM |
149 | # Test PR 12212, using InfThread.selected_thread() when no inferior is |
150 | # loaded. | |
151 | gdb_py_test_silent_cmd "python nothread = gdb.selected_thread()" "Attempt to aquire thread with no inferior" 1 | |
152 | gdb_test "python print nothread == None" "True" "Ensure that no threads are returned" | |
153 | ||
cb2e07a6 PM |
154 | # Start with a fresh gdb. |
155 | clean_restart ${testfile} | |
156 | ||
157 | # The following tests require execution. | |
158 | ||
159 | if ![runto_main] then { | |
160 | fail "Can't run to main" | |
161 | return 0 | |
162 | } | |
163 | ||
164 | runto [gdb_get_line_number "Break to end."] | |
165 | ||
166 | # Test gdb.decode_line. | |
167 | gdb_test "python gdb.decode_line(\"main.c:43\")" \ | |
621c8364 | 168 | "gdb.error: No source file named main.c.*" "test decode_line no source named main" |
cb2e07a6 PM |
169 | |
170 | gdb_py_test_silent_cmd "python symtab = gdb.decode_line()" "test decode_line current location" 1 | |
171 | gdb_test "python print len(symtab)" "2" "Test decode_line current location" | |
172 | gdb_test "python print symtab\[0\]" "None" "Test decode_line expression parse" | |
173 | gdb_test "python print len(symtab\[1\])" "1" "Test decode_line current location" | |
174 | gdb_test "python print symtab\[1\]\[0\].symtab" "gdb/testsuite/gdb.python/python.c.*" "Test decode_line current locationn filename" | |
175 | gdb_test "python print symtab\[1\]\[0\].line" "22" "Test decode_line current location line number" | |
176 | ||
177 | gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"python.c:26 if foo\")" "test decode_line python.c:26" 1 | |
178 | gdb_test "python print len(symtab)" "2" "Test decode_line python.c:26 length" | |
179 | gdb_test "python print symtab\[0\]" "if foo" "Test decode_line expression parse" | |
180 | gdb_test "python print len(symtab\[1\])" "1" "Test decode_line python.c:26 length" | |
181 | gdb_test "python print symtab\[1\]\[0\].symtab" "gdb/testsuite/gdb.python/python.c.*" "Test decode_line python.c:26 filename" | |
182 | gdb_test "python print symtab\[1\]\[0\].line" "26" "Test decode_line python.c:26 line number" | |
183 | ||
184 | gdb_test "python gdb.decode_line(\"randomfunc\")" \ | |
621c8364 | 185 | "gdb.error: Function \"randomfunc\" not defined.*" "test decode_line randomfunc" |
cb2e07a6 PM |
186 | gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"func1\")" "test decode_line func1()" 1 |
187 | gdb_test "python print len(symtab)" "2" "Test decode_line func1 length" | |
188 | gdb_test "python print len(symtab\[1\])" "1" "Test decode_line func1 length" | |
e7fbb131 | 189 | gdb_test "python print symtab\[1\]\[0\].symtab" "gdb/testsuite/gdb.python/python-1.c.*" "Test decode_line func1 filename" |
cb2e07a6 | 190 | gdb_test "python print symtab\[1\]\[0\].line" "19" "Test decode_line func1 line number" |