Home | History | Annotate | Line # | Download | only in gdb.python
py-unwind-user-regs.exp revision 1.1
      1  1.1  christos # Copyright (C) 2021-2023 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  christos standard_testfile
     42  1.1  christos 
     43  1.1  christos if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } {
     44  1.1  christos     return -1
     45  1.1  christos }
     46  1.1  christos 
     47  1.1  christos # Skip all tests if Python scripting is not enabled.
     48  1.1  christos if { [skip_python_tests] } { continue }
     49  1.1  christos 
     50  1.1  christos if {![runto_main]} {
     51  1.1  christos     return 0
     52  1.1  christos }
     53  1.1  christos 
     54  1.1  christos set pyfile [gdb_remote_download host ${srcdir}/${subdir}/${testfile}.py]
     55  1.1  christos 
     56  1.1  christos gdb_breakpoint [gdb_get_line_number "Break here"]
     57  1.1  christos gdb_continue_to_breakpoint "stop at test breakpoint"
     58  1.1  christos 
     59  1.1  christos # Load the script containing the unwinders.  There are actually two
     60  1.1  christos # unwinders defined here that will catch the same function, so we
     61  1.1  christos # immediately disable one of the unwinders.
     62  1.1  christos gdb_test_no_output "source ${pyfile}"\
     63  1.1  christos     "import python scripts"
     64  1.1  christos gdb_test "disable unwinder global \"break unwinding using strings\"" \
     65  1.1  christos     "1 unwinder disabled" "disable the unwinder that uses strings"
     66  1.1  christos 
     67  1.1  christos # At this point we are using the unwinder that passes a
     68  1.1  christos # gdb.RegisterDescriptor to add_saved_register.
     69  1.1  christos gdb_test_sequence "bt"  "Backtrace corrupted by descriptor based unwinder" {
     70  1.1  christos     "\\r\\n#0 \[^\r\n\]* foo \\(\\) at "
     71  1.1  christos     "\\r\\n#1 \[^\r\n\]* bar \\(\\) at "
     72  1.1  christos     "Backtrace stopped: previous frame inner to this frame \\(corrupt stack\\?\\)"
     73  1.1  christos }
     74  1.1  christos 
     75  1.1  christos # Disable the unwinder that calls add_saved_register with a
     76  1.1  christos # gdb.RegisterDescriptor, and enable the unwinder that calls
     77  1.1  christos # add_saved_register with a string (containing the register name).
     78  1.1  christos gdb_test "disable unwinder global \"break unwinding using descriptors\"" \
     79  1.1  christos     "1 unwinder disabled" "disable the unwinder that uses descriptors"
     80  1.1  christos gdb_test "enable unwinder global \"break unwinding using strings\"" \
     81  1.1  christos     "1 unwinder enabled" "enable the unwinder that uses strings"
     82  1.1  christos gdb_test_sequence "bt"  "Backtrace corrupted by string based unwinder" {
     83  1.1  christos     "\\r\\n#0 \[^\r\n\]* foo \\(\\) at "
     84  1.1  christos     "\\r\\n#1 \[^\r\n\]* bar \\(\\) at "
     85  1.1  christos     "Backtrace stopped: previous frame inner to this frame \\(corrupt stack\\?\\)"
     86  1.1  christos }
     87  1.1  christos 
     88  1.1  christos # Just for completeness, disable the string unwinder again (neither of
     89  1.1  christos # our special unwinders are now enabled), and check the backtrace.  We
     90  1.1  christos # now get the complete stack back to main.
     91  1.1  christos gdb_test "disable unwinder global \"break unwinding using strings\"" \
     92  1.1  christos     "1 unwinder disabled" "disable the unwinder that uses strings again"
     93  1.1  christos gdb_test_sequence "bt"  "Backtrace not corrupted when using no unwinder" {
     94  1.1  christos     "\\r\\n#0 \[^\r\n\]* foo \\(\\) at "
     95  1.1  christos     "\\r\\n#1 \[^\r\n\]* bar \\(\\) at "
     96  1.1  christos     "\\r\\n#2 \[^\r\n\]* main \\(\\) at "
     97  1.1  christos }
     98