Home | History | Annotate | Line # | Download | only in gdb.python
      1  1.1.1.2  christos # Copyright (C) 2021-2024 Free Software Foundation, Inc.
      2      1.1  christos 
      3      1.1  christos # This program is free software; you can redistribute it and/or modify
      4      1.1  christos # it under the terms of the GNU General Public License as published by
      5      1.1  christos # the Free Software Foundation; either version 3 of the License, or
      6      1.1  christos # (at your option) any later version.
      7      1.1  christos #
      8      1.1  christos # This program is distributed in the hope that it will be useful,
      9      1.1  christos # but WITHOUT ANY WARRANTY; without even the implied warranty of
     10      1.1  christos # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     11      1.1  christos # GNU General Public License for more details.
     12      1.1  christos #
     13      1.1  christos # You should have received a copy of the GNU General Public License
     14      1.1  christos # along with this program.  If not, see <http://www.gnu.org/licenses/>.
     15      1.1  christos 
     16      1.1  christos # Setup an unwinder that uses gdb.UnwindInfo.add_saved_register with
     17      1.1  christos # the register's 'pc' and 'sp'.  On some (all?) targets, these
     18      1.1  christos # registers are implemented as user-registers, and so can't normally
     19      1.1  christos # be written to directly.
     20      1.1  christos #
     21      1.1  christos # The Python unwinder now includes code similar to how the expression
     22      1.1  christos # evaluator would handle something like 'set $pc=0x1234', we fetch the
     23      1.1  christos # value of '$pc', and then use the value's location to tell us which
     24      1.1  christos # register to write to.
     25      1.1  christos #
     26      1.1  christos # The unwinder defined here deliberately breaks the unwind by setting
     27      1.1  christos # the unwound $pc and $sp to be equal to the current frame's $pc and
     28      1.1  christos # $sp.  GDB will spot this as a loop in the backtrace and terminate
     29      1.1  christos # the unwind.
     30      1.1  christos #
     31      1.1  christos # However, by the time the unwind terminates we have already shown
     32      1.1  christos # that it is possible to call add_saved_register with a user-register,
     33      1.1  christos # so the test is considered passed.
     34      1.1  christos #
     35      1.1  christos # For completeness this test checks two cases, calling
     36      1.1  christos # add_saved_register with a gdb.RegisterDescriptor and calling
     37      1.1  christos # add_saved_register with a string containing the register name.
     38      1.1  christos 
     39      1.1  christos load_lib gdb-python.exp
     40      1.1  christos 
     41  1.1.1.2  christos require allow_python_tests
     42  1.1.1.2  christos 
     43      1.1  christos standard_testfile
     44      1.1  christos 
     45      1.1  christos if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } {
     46      1.1  christos     return -1
     47      1.1  christos }
     48      1.1  christos 
     49      1.1  christos if {![runto_main]} {
     50      1.1  christos     return 0
     51      1.1  christos }
     52      1.1  christos 
     53      1.1  christos set pyfile [gdb_remote_download host ${srcdir}/${subdir}/${testfile}.py]
     54      1.1  christos 
     55      1.1  christos gdb_breakpoint [gdb_get_line_number "Break here"]
     56      1.1  christos gdb_continue_to_breakpoint "stop at test breakpoint"
     57      1.1  christos 
     58      1.1  christos # Load the script containing the unwinders.  There are actually two
     59      1.1  christos # unwinders defined here that will catch the same function, so we
     60      1.1  christos # immediately disable one of the unwinders.
     61      1.1  christos gdb_test_no_output "source ${pyfile}"\
     62      1.1  christos     "import python scripts"
     63      1.1  christos gdb_test "disable unwinder global \"break unwinding using strings\"" \
     64      1.1  christos     "1 unwinder disabled" "disable the unwinder that uses strings"
     65      1.1  christos 
     66      1.1  christos # At this point we are using the unwinder that passes a
     67      1.1  christos # gdb.RegisterDescriptor to add_saved_register.
     68      1.1  christos gdb_test_sequence "bt"  "Backtrace corrupted by descriptor based unwinder" {
     69      1.1  christos     "\\r\\n#0 \[^\r\n\]* foo \\(\\) at "
     70      1.1  christos     "\\r\\n#1 \[^\r\n\]* bar \\(\\) at "
     71      1.1  christos     "Backtrace stopped: previous frame inner to this frame \\(corrupt stack\\?\\)"
     72      1.1  christos }
     73      1.1  christos 
     74      1.1  christos # Disable the unwinder that calls add_saved_register with a
     75      1.1  christos # gdb.RegisterDescriptor, and enable the unwinder that calls
     76      1.1  christos # add_saved_register with a string (containing the register name).
     77      1.1  christos gdb_test "disable unwinder global \"break unwinding using descriptors\"" \
     78      1.1  christos     "1 unwinder disabled" "disable the unwinder that uses descriptors"
     79      1.1  christos gdb_test "enable unwinder global \"break unwinding using strings\"" \
     80      1.1  christos     "1 unwinder enabled" "enable the unwinder that uses strings"
     81      1.1  christos gdb_test_sequence "bt"  "Backtrace corrupted by string based unwinder" {
     82      1.1  christos     "\\r\\n#0 \[^\r\n\]* foo \\(\\) at "
     83      1.1  christos     "\\r\\n#1 \[^\r\n\]* bar \\(\\) at "
     84      1.1  christos     "Backtrace stopped: previous frame inner to this frame \\(corrupt stack\\?\\)"
     85      1.1  christos }
     86      1.1  christos 
     87      1.1  christos # Just for completeness, disable the string unwinder again (neither of
     88      1.1  christos # our special unwinders are now enabled), and check the backtrace.  We
     89      1.1  christos # now get the complete stack back to main.
     90      1.1  christos gdb_test "disable unwinder global \"break unwinding using strings\"" \
     91      1.1  christos     "1 unwinder disabled" "disable the unwinder that uses strings again"
     92      1.1  christos gdb_test_sequence "bt"  "Backtrace not corrupted when using no unwinder" {
     93      1.1  christos     "\\r\\n#0 \[^\r\n\]* foo \\(\\) at "
     94      1.1  christos     "\\r\\n#1 \[^\r\n\]* bar \\(\\) at "
     95      1.1  christos     "\\r\\n#2 \[^\r\n\]* main \\(\\) at "
     96      1.1  christos }
     97