1 # Copyright (C) 2021-2024 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 # Setup an unwinder that uses gdb.UnwindInfo.add_saved_register with 17 # the register's 'pc' and 'sp'. On some (all?) targets, these 18 # registers are implemented as user-registers, and so can't normally 19 # be written to directly. 20 # 21 # The Python unwinder now includes code similar to how the expression 22 # evaluator would handle something like 'set $pc=0x1234', we fetch the 23 # value of '$pc', and then use the value's location to tell us which 24 # register to write to. 25 # 26 # The unwinder defined here deliberately breaks the unwind by setting 27 # the unwound $pc and $sp to be equal to the current frame's $pc and 28 # $sp. GDB will spot this as a loop in the backtrace and terminate 29 # the unwind. 30 # 31 # However, by the time the unwind terminates we have already shown 32 # that it is possible to call add_saved_register with a user-register, 33 # so the test is considered passed. 34 # 35 # For completeness this test checks two cases, calling 36 # add_saved_register with a gdb.RegisterDescriptor and calling 37 # add_saved_register with a string containing the register name. 38 39 load_lib gdb-python.exp 40 41 require allow_python_tests 42 43 standard_testfile 44 45 if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } { 46 return -1 47 } 48 49 if {![runto_main]} { 50 return 0 51 } 52 53 set pyfile [gdb_remote_download host ${srcdir}/${subdir}/${testfile}.py] 54 55 gdb_breakpoint [gdb_get_line_number "Break here"] 56 gdb_continue_to_breakpoint "stop at test breakpoint" 57 58 # Load the script containing the unwinders. There are actually two 59 # unwinders defined here that will catch the same function, so we 60 # immediately disable one of the unwinders. 61 gdb_test_no_output "source ${pyfile}"\ 62 "import python scripts" 63 gdb_test "disable unwinder global \"break unwinding using strings\"" \ 64 "1 unwinder disabled" "disable the unwinder that uses strings" 65 66 # At this point we are using the unwinder that passes a 67 # gdb.RegisterDescriptor to add_saved_register. 68 gdb_test_sequence "bt" "Backtrace corrupted by descriptor based unwinder" { 69 "\\r\\n#0 \[^\r\n\]* foo \\(\\) at " 70 "\\r\\n#1 \[^\r\n\]* bar \\(\\) at " 71 "Backtrace stopped: previous frame inner to this frame \\(corrupt stack\\?\\)" 72 } 73 74 # Disable the unwinder that calls add_saved_register with a 75 # gdb.RegisterDescriptor, and enable the unwinder that calls 76 # add_saved_register with a string (containing the register name). 77 gdb_test "disable unwinder global \"break unwinding using descriptors\"" \ 78 "1 unwinder disabled" "disable the unwinder that uses descriptors" 79 gdb_test "enable unwinder global \"break unwinding using strings\"" \ 80 "1 unwinder enabled" "enable the unwinder that uses strings" 81 gdb_test_sequence "bt" "Backtrace corrupted by string based unwinder" { 82 "\\r\\n#0 \[^\r\n\]* foo \\(\\) at " 83 "\\r\\n#1 \[^\r\n\]* bar \\(\\) at " 84 "Backtrace stopped: previous frame inner to this frame \\(corrupt stack\\?\\)" 85 } 86 87 # Just for completeness, disable the string unwinder again (neither of 88 # our special unwinders are now enabled), and check the backtrace. We 89 # now get the complete stack back to main. 90 gdb_test "disable unwinder global \"break unwinding using strings\"" \ 91 "1 unwinder disabled" "disable the unwinder that uses strings again" 92 gdb_test_sequence "bt" "Backtrace not corrupted when using no unwinder" { 93 "\\r\\n#0 \[^\r\n\]* foo \\(\\) at " 94 "\\r\\n#1 \[^\r\n\]* bar \\(\\) at " 95 "\\r\\n#2 \[^\r\n\]* main \\(\\) at " 96 } 97