]>
Commit | Line | Data |
---|---|---|
213516ef | 1 | # Copyright (C) 2009-2023 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 | ||
f39a75d0 | 19 | load_lib gdb-python.exp |
bc3b79fd | 20 | |
d82e5429 | 21 | require allow_python_tests |
79749205 | 22 | |
9b2234b0 | 23 | clean_restart |
bc3b79fd | 24 | |
2a17c803 | 25 | gdb_test_multiline "input convenience function" \ |
bc3b79fd TJB |
26 | "python" "" \ |
27 | "class test_func (gdb.Function):" "" \ | |
28 | " def __init__ (self):" "" \ | |
29 | " super (test_func, self).__init__ (\"test_func\")" "" \ | |
30 | " def invoke (self, arg):" "" \ | |
31 | " return \"test_func output, arg = %s\" % arg.string ()" "" \ | |
32 | "test_func ()" "" \ | |
33 | "end" "" | |
34 | ||
35 | gdb_test "print \$test_func (\"ugh\")" "= \"test_func output, arg = ugh\"" "call function" | |
cc924cad TJB |
36 | |
37 | # Test returning a gdb.Value from the function. This segfaulted GDB at one point. | |
38 | ||
2a17c803 | 39 | gdb_test_multiline "input value-returning convenience function" \ |
cc924cad TJB |
40 | "python" "" \ |
41 | "class Double (gdb.Function):" "" \ | |
42 | " def __init__ (self):" "" \ | |
43 | " super (Double, self).__init__ (\"double\")" "" \ | |
44 | " def invoke (self, n):" "" \ | |
45 | " return n*2" "" \ | |
46 | "Double ()" "" \ | |
47 | "end" "" | |
48 | ||
d7df6549 AB |
49 | # Different languages can have different parsers, so lets check that |
50 | # internal functions are understood by every language. Place auto | |
51 | # last in the list so we end up back in 'auto' language mode. | |
52 | foreach lang [concat [gdb_supported_languages] auto] { | |
53 | gdb_test_no_output "set language $lang" | |
54 | gdb_test "print \$double (1)" "= 2" "call value-returning function, language = $lang" | |
55 | } | |
329719ec | 56 | |
2a17c803 | 57 | gdb_test_multiline "input int-returning function" \ |
329719ec TT |
58 | "python" "" \ |
59 | "class Yes(gdb.Function):" "" \ | |
60 | " def __init__(self):" "" \ | |
61 | " gdb.Function.__init__(self, 'yes')" "" \ | |
62 | " def invoke(self):" "" \ | |
63 | " return 1" "" \ | |
64 | "Yes ()" "" \ | |
65 | "end" "" | |
66 | ||
67 | gdb_test "print \$yes() && \$yes()" " = 1" "call yes with &&" | |
68 | gdb_test "print \$yes() || \$yes()" " = 1" "call yes with ||" | |
05775840 | 69 | |
2a17c803 | 70 | gdb_test_multiline "Test GDBError" \ |
05775840 PM |
71 | "python" "" \ |
72 | "class GDBError(gdb.Function):" "" \ | |
73 | " def __init__(self):" "" \ | |
74 | " gdb.Function.__init__(self, 'gdberror')" "" \ | |
75 | " def invoke(self):" "" \ | |
76 | " raise gdb.GdbError(\"This is a GdbError\")" "" \ | |
77 | "GDBError ()" "" \ | |
78 | "end" "" | |
79 | ||
80 | gdb_test "print \$gdberror()" "This is a GdbError.*" \ | |
81 | "Test GdbError. There should not be a stack trace" | |
82 | ||
2a17c803 | 83 | gdb_test_multiline "Test Normal Error" \ |
05775840 PM |
84 | "python" "" \ |
85 | "class NormalError(gdb.Function):" "" \ | |
86 | " def __init__(self):" "" \ | |
87 | " gdb.Function.__init__(self, 'normalerror')" "" \ | |
88 | " def invoke(self):" "" \ | |
89 | " raise RuntimeError(\"This is a Normal Error\")" "" \ | |
90 | "NormalError ()" "" \ | |
91 | "end" "" | |
92 | ||
80b6e756 | 93 | gdb_test_no_output "set python print-stack full" |
05775840 PM |
94 | gdb_test "print \$normalerror()" "Traceback.*File.*line 5.*in invoke.*RuntimeError.*This is a Normal Error.*" \ |
95 | "Test a Runtime error. There should be a stack trace." | |
028d0ed5 | 96 | |
2a17c803 | 97 | gdb_test_multiline "input command-calling function" \ |
028d0ed5 TJB |
98 | "python" "" \ |
99 | "class CallCommand(gdb.Function):" "" \ | |
100 | " def __init__(self):" "" \ | |
101 | " gdb.Function.__init__(self, 'call_command')" "" \ | |
102 | " def invoke(self):" "" \ | |
103 | " return gdb.execute('print 1', to_string=True)" "" \ | |
104 | "CallCommand ()" "" \ | |
105 | "end" "" | |
106 | ||
9f058c10 | 107 | gdb_test_no_output "set var \$foo = \$call_command()" "setting a value from a function which executes a command." |
028d0ed5 | 108 | # There was a bug where GDB would segfault in the second call, so try calling again. |
9f058c10 | 109 | gdb_test_no_output "set var \$foo = \$call_command()" "setting a value from a function which executes a command, again." |