2011-12-16 Phil Muldoon <pmuldoon@redhat.com>
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.python / py-function.exp
CommitLineData
7b6bb8da 1# Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
bc3b79fd
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 convenience functions to Python.
18
19if $tracelevel then {
20 strace $tracelevel
21}
22
f39a75d0 23load_lib gdb-python.exp
bc3b79fd
TJB
24
25# Start with a fresh gdb.
26
27gdb_exit
28gdb_start
29gdb_reinitialize_dir $srcdir/$subdir
30
f6bbabf0
PM
31# Skip all tests if Python scripting is not enabled.
32if { [skip_python_tests] } { continue }
bc3b79fd
TJB
33
34gdb_py_test_multiple "input convenience function" \
35 "python" "" \
36 "class test_func (gdb.Function):" "" \
37 " def __init__ (self):" "" \
38 " super (test_func, self).__init__ (\"test_func\")" "" \
39 " def invoke (self, arg):" "" \
40 " return \"test_func output, arg = %s\" % arg.string ()" "" \
41 "test_func ()" "" \
42 "end" ""
43
44gdb_test "print \$test_func (\"ugh\")" "= \"test_func output, arg = ugh\"" "call function"
cc924cad
TJB
45
46# Test returning a gdb.Value from the function. This segfaulted GDB at one point.
47
48gdb_py_test_multiple "input value-returning convenience function" \
49 "python" "" \
50 "class Double (gdb.Function):" "" \
51 " def __init__ (self):" "" \
52 " super (Double, self).__init__ (\"double\")" "" \
53 " def invoke (self, n):" "" \
54 " return n*2" "" \
55 "Double ()" "" \
56 "end" ""
57
58gdb_test "print \$double (1)" "= 2" "call value-returning function"
329719ec
TT
59
60gdb_py_test_multiple "input int-returning function" \
61 "python" "" \
62 "class Yes(gdb.Function):" "" \
63 " def __init__(self):" "" \
64 " gdb.Function.__init__(self, 'yes')" "" \
65 " def invoke(self):" "" \
66 " return 1" "" \
67 "Yes ()" "" \
68 "end" ""
69
70gdb_test "print \$yes() && \$yes()" " = 1" "call yes with &&"
71gdb_test "print \$yes() || \$yes()" " = 1" "call yes with ||"
05775840
PM
72
73gdb_py_test_multiple "Test GDBError" \
74 "python" "" \
75 "class GDBError(gdb.Function):" "" \
76 " def __init__(self):" "" \
77 " gdb.Function.__init__(self, 'gdberror')" "" \
78 " def invoke(self):" "" \
79 " raise gdb.GdbError(\"This is a GdbError\")" "" \
80 "GDBError ()" "" \
81 "end" ""
82
83gdb_test "print \$gdberror()" "This is a GdbError.*" \
84 "Test GdbError. There should not be a stack trace"
85
86gdb_py_test_multiple "Test Normal Error" \
87 "python" "" \
88 "class NormalError(gdb.Function):" "" \
89 " def __init__(self):" "" \
90 " gdb.Function.__init__(self, 'normalerror')" "" \
91 " def invoke(self):" "" \
92 " raise RuntimeError(\"This is a Normal Error\")" "" \
93 "NormalError ()" "" \
94 "end" ""
95
80b6e756 96gdb_test_no_output "set python print-stack full"
05775840
PM
97gdb_test "print \$normalerror()" "Traceback.*File.*line 5.*in invoke.*RuntimeError.*This is a Normal Error.*" \
98 "Test a Runtime error. There should be a stack trace."
028d0ed5
TJB
99
100gdb_py_test_multiple "input command-calling function" \
101 "python" "" \
102 "class CallCommand(gdb.Function):" "" \
103 " def __init__(self):" "" \
104 " gdb.Function.__init__(self, 'call_command')" "" \
105 " def invoke(self):" "" \
106 " return gdb.execute('print 1', to_string=True)" "" \
107 "CallCommand ()" "" \
108 "end" ""
109
110gdb_test_no_output "set var \$foo = \$call_command()" "Setting a value from a function which executes a command."
111# There was a bug where GDB would segfault in the second call, so try calling again.
112gdb_test_no_output "set var \$foo = \$call_command()" "Setting a value from a function which executes a command, again."
This page took 0.345701 seconds and 4 git commands to generate.