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