1# Copyright 2014-2023 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 debug methods 17# feature in the Python extension language. 18 19load_lib gdb-python.exp 20 21if { [skip_cplus_tests] } { 22 untested "skipping C++ tests" 23 return 24} 25 26standard_testfile py-xmethods.cc 27 28if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug c++}]} { 29 return -1 30} 31 32# Skip all tests if Python scripting is not enabled. 33if { [skip_python_tests] } { 34 untested "skipping Python tests" 35 return 36} 37 38if ![runto_main] { 39 return -1 40} 41 42set xmethods_script [gdb_remote_download host \ 43 ${srcdir}/${subdir}/${testfile}.py] 44 45gdb_breakpoint [gdb_get_line_number "Break here."] 46gdb_continue_to_breakpoint "Break here" ".*Break here.*" 47 48# Tests before loading the debug methods. 49gdb_test "p a1 + a2" ".* = 15" "before: a1 + a2" 50gdb_test "p a_plus_a" ".* = 1" "before: a_plus_a 1" 51 52gdb_test "p a2 - a1" ".* = 5" "before: a2 - a1" 53gdb_test "p a_minus_a" ".* = 1" "before: a_minus_a 1" 54 55gdb_test "p b1 - a1" ".* = 25" "before: b1 - a1" 56gdb_test "p a_minus_a" ".* = 2" "before: a_minus_a 2" 57 58gdb_test "p a1.geta()" ".* = 5" "before: a1.geta()" 59gdb_test "p a_geta" ".* = 1" "before: a_geta 1" 60 61gdb_test "p ++a1" "No symbol.*" "before: ++a1" 62gdb_test "p a1.getarrayind(5)" "Couldn't find method.*" \ 63 "before: a1.getarrayind(5)" 64 65gdb_test "p a_ptr->geta()" ".* = 60" "before: a_ptr->geta()" 66gdb_test "p b_geta" ".* = 1" "before: b_geta 1" 67 68gdb_test "p e.geta()" ".* = 100" "before: e.geta()" 69gdb_test "p a_geta" ".* = 2" "before: a_geta 2" 70 71# Since g.size_diff operates of sizes of int and float, do not check for 72# actual result value as it could be different on different platforms. 73gdb_test "p g.size_diff<float>()" ".*" "before: call g.size_diff<float>()" 74gdb_test "p g_size_diff" ".* = 2" "before: g_size_diff 2" 75 76gdb_test "p g.size_diff<unsigned long>()" "Couldn't find method.*" \ 77 "before: g.size_diff<unsigned long>()" 78 79gdb_test "p g.size_mul<2>()" ".*" "before: g.size_mul<2>()" 80gdb_test "p g_size_mul" ".* = 2" "before: g_size_mul 2" 81 82gdb_test "p g.size_mul<5>()" "Couldn't find method.*" \ 83 "before: g.size_mul<5>()" 84 85gdb_test "p g.mul<double>(2.0)" ".* = 10" "before: g.mul<double>(2.0)" 86gdb_test "p g_mul" ".* = 2" "before: g_mul 2" 87 88gdb_test "p g.mul<char>('a')" "Couldn't find method.*" \ 89 "before: g.mul<char>('a')" 90 91# Load the script which adds the debug methods. 92gdb_test_no_output "source ${xmethods_script}" "load the script file" 93 94# Tests after loading debug methods. 95gdb_test "p a1 + a2" "From Python <A_plus_A>.*15" "after: a1 + a2" 96 97gdb_test "p a2 - a1" ".* = 5" "after: a2 - a1" 98gdb_test "p a_minus_a" ".* = 3" "after: a_minus_a 3" 99 100gdb_test "p b1 + a1" "From Python <A_plus_A>.*35" "after: b1 + a1" 101 102gdb_test "p b1 - a1" ".* = 25" "after: b1 - a1" 103gdb_test "p a_minus_a" ".* = 4" "after: a_minus_a 4" 104 105gdb_test "p a1.geta()" "From Python <A_geta>.*5" "after: a1.geta()" 106gdb_test "p ++a1" "From Python <plus_plus_A>.*6" "after: ++a1" 107gdb_test "p a1.getarrayind(5)" "From Python <A_getarrayind>.*5" \ 108 "after: a1.getarrayind(5)" 109gdb_test "p a1\[6\]" ".*int &.*6" "after a1\[\]" 110gdb_test "p b1\[7\]" ".*const int &.*7" "after b1\[\]" 111# Note the following test. Xmethods on dynamc types are not looked up 112# currently. Hence, even though a_ptr points to a B object, the xmethod 113# defined for A objects is invoked. 114gdb_test "p a_ptr->geta()" "From Python <A_geta>.*30" "after: a_ptr->geta()" 115gdb_test "p e.geta()" "From Python <A_geta>.*100" "after: e.geta()" 116gdb_test "p e_ptr->geta()" "From Python <A_geta>.*100" "after: e_ptr->geta()" 117gdb_test "p e_ref.geta()" "From Python <A_geta>.*100" "after: e_ref.geta()" 118gdb_test "p e.method(10)" "From Python <E_method_int>.* = void" \ 119 "after: e.method(10)" 120gdb_test "p e.method('a')" "From Python <E_method_char>.* = void" \ 121 "after: e.method('a')" 122gdb_test "p g.size_diff<float> ()" "From Python G<>::size_diff.*" \ 123 "after: g.size_diff<float>()" 124gdb_test "p g.size_diff< unsigned long >()" "From Python G<>::size_diff.*" \ 125 "after: g.size_diff<unsigned long>()" 126gdb_test "p g.size_mul<2>()" "From Python G<>::size_mul.*" \ 127 "after: g.size_mul<2>()" 128gdb_test "p g.size_mul< 5 >()" "From Python G<>::size_mul.*" \ 129 "after: g.size_mul< 5 >()" 130gdb_test "p g.mul<double>(2.0)" "From Python G<>::mul.*" \ 131 "after: g.mul<double>(2.0)" 132gdb_test "p g.mul<char>('a')" "From Python G<>::mul.*" \ 133 "after: g.mul<char>('a')" 134gdb_test "p g_ptr->mul<char>('a')" "From Python G<>::mul.*" \ 135 "after: g_ptr->mul<char>('a')" 136 137# Tests for 'disable/enable xmethod' command. 138gdb_test_no_output "disable xmethod progspace G_methods" \ 139 "disable G_methods" 140gdb_test "p g.mul<char>('a')" "Couldn't find method.*" \ 141 "g.mul<char>('a') after disabling G_methods" 142gdb_test_no_output "enable xmethod progspace G_methods" \ 143 "dnable G_methods" 144gdb_test "p g.mul<char>('a')" "From Python G<>::mul.*" \ 145 "after enabling G_methods" 146gdb_test_no_output "disable xmethod progspace G_methods;mul" \ 147 "disable G_methods;mul" 148gdb_test "p g.mul<char>('a')" "Couldn't find method.*" \ 149 "g.mul<char>('a') after disabling G_methods;mul" 150gdb_test_no_output "enable xmethod progspace G_methods;mul" \ 151 "enable G_methods;mul" 152gdb_test "p g.mul<char>('a')" "From Python G<>::mul.*" \ 153 "after enabling G_methods;mul" 154 155# Test for 'info xmethods' command 156gdb_test "info xmethod global plus" "global.*plus_plus_A" \ 157 "info xmethod global plus 1" 158gdb_test_no_output "disable xmethod progspace E_methods;method_int" \ 159 "disable xmethod progspace E_methods;method_int" 160gdb_test "info xmethod progspace E_methods;method_int" ".* \\\[disabled\\\]" \ 161 "info xmethod xmethods E_methods;method_int" 162gdb_test_no_output "disable xmethod progspace G_methods" "disable G_methods 2" 163gdb_test "info xmethod progspace" ".*G_methods \\\[disabled\\\].*" 164 165# PR 18285 166# First make sure both are enabled. 167gdb_test_no_output "enable xmethod progspace E_methods;method_char" 168gdb_test_no_output "enable xmethod progspace E_methods;method_int" 169gdb_test "pt e.method('a')" "type = void" 170gdb_test "pt e.method(10)" \ 171 "NotImplementedError.*Error while fetching result type of an xmethod worker defined in Python." 172