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