Home | History | Annotate | Line # | Download | only in gdb.python
      1 # Copyright (C) 2022-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 # This file is part of the GDB testsuite.  It tests a pretty printer that
     17 # calls an inferior function by hand, triggering a Use-after-Free bug
     18 # (PR gdb/28856).
     19 
     20 load_lib gdb-python.exp
     21 
     22 require allow_python_tests
     23 
     24 standard_testfile
     25 
     26 if { [prepare_for_testing "failed to prepare" $testfile $srcfile debug] } {
     27     untested "failed to compile"
     28     return -1
     29 }
     30 
     31 # This proc restarts GDB, makes the inferior reach the desired spot - marked
     32 # by a comment in the .c file - and turns on the pretty printer for testing.
     33 # Starting with a new GDB is important because the test may crash GDB.  The
     34 # return values are here to avoid us trying to test the pretty printer if
     35 # there was a problem getting to main.
     36 proc start_test { breakpoint_comment } {
     37     global srcdir subdir testfile binfile
     38 
     39     # Start with a fresh gdb.
     40     # This is important because the test can crash GDB.
     41 
     42     clean_restart ${binfile}
     43 
     44     if {![runto_main]} {
     45 	untested "couldn't run to breakpoint"
     46 	return -1
     47     }
     48 
     49     # Let GDB get to the return line.
     50     gdb_breakpoint [gdb_get_line_number ${breakpoint_comment} ${testfile}.c ]
     51     gdb_continue_to_breakpoint ${breakpoint_comment} ".*"
     52 
     53     set remote_python_file [gdb_remote_download host ${srcdir}/${subdir}/${testfile}.py]
     54     gdb_test_no_output "source ${remote_python_file}" "load python file"
     55 
     56     return 0
     57 }
     58 
     59 # Start by testing the "run" command, it can't leverage start_test
     60 with_test_prefix "run to frame" {
     61     if {![runto_main]} {
     62 	untested "couldn't run to main"
     63     }
     64 
     65     set remote_python_file [gdb_remote_download host ${srcdir}/${subdir}/${testfile}.py]
     66     gdb_test_no_output "source ${remote_python_file}" "load python file"
     67 
     68     gdb_breakpoint [gdb_get_line_number "TAG: final frame" ${testfile}.c]
     69     gdb_continue_to_breakpoint "TAG: final frame" ".*"
     70 }
     71 
     72 # Testing the backtrace command.
     73 with_test_prefix "frame print" {
     74     if { [start_test "TAG: final frame"] == 0 } {
     75 	gdb_test "backtrace -frame-arguments all" [multi_line \
     76 	"#0 .*g \\(mt=mytype is .*\\, depth=0\\).*"\
     77 	"#1 .*g \\(mt=mytype is .*\\, depth=1\\).*"\
     78 	"#2 .*g \\(mt=mytype is .*\\, depth=2\\).*"\
     79 	"#3 .*g \\(mt=mytype is .*\\, depth=3\\).*"\
     80 	"#4 .*g \\(mt=mytype is .*\\, depth=4\\).*"\
     81 	"#5 .*g \\(mt=mytype is .*\\, depth=5\\).*"\
     82 	"#6 .*g \\(mt=mytype is .*\\, depth=6\\).*"\
     83 	"#7 .*g \\(mt=mytype is .*\\, depth=7\\).*"\
     84 	"#8 .*g \\(mt=mytype is .*\\, depth=8\\).*"\
     85 	"#9 .*g \\(mt=mytype is .*\\, depth=9\\).*"\
     86 	"#10 .*g \\(mt=mytype is .*\\, depth=10\\).*"\
     87 	"#11 .*main \\(\\).*"] \
     88 	"backtrace test"
     89     }
     90 }
     91 
     92 # Test the "info frame" command
     93 with_test_prefix "info frame" {
     94     if { [start_test "TAG: first frame"] == 0 } {
     95 	gdb_test "info frame" "mytype is $hex \"hello world\".*"
     96     }
     97 }
     98 
     99 # Testing the down command.
    100 with_test_prefix "frame movement down" {
    101     if { [start_test "TAG: first frame"] == 0 } {
    102 	gdb_test "up" [multi_line "#1 .*in main \\(\\) at .*" ".*outside the frame.*"]
    103 	gdb_test "down" [multi_line "#0\\s+g \\(mt=mytype is .*\\, depth=10\\).*" ".*first frame.*"]
    104     }
    105 }
    106 
    107 # Testing the up command.
    108 with_test_prefix "frame movement up" {
    109     if { [start_test "TAG: final frame"] == 0 } {
    110 	gdb_test "up" [multi_line "#1 .*in g \\(mt=mytype is .*\\, depth=1\\).*" ".*first frame.*"]
    111     }
    112 }
    113 
    114 # Testing the finish command.
    115 with_test_prefix "frame exit through finish" {
    116     if { [start_test "TAG: final frame"] == 0 } {
    117 	gdb_test "finish" [multi_line ".*.*g \\(mt=mytype is .*\\, depth=0\\).*" ".*g \\(mt=mytype is .*\\, depth=1\\).*" ".*"]
    118     }
    119 }
    120 
    121 # Testing the step command.
    122 with_test_prefix "frame enter through step" {
    123     if { [start_test "TAG: outside the frame"] == 0 } {
    124 	gdb_test "step" [multi_line "g \\(mt=mytype is .*\\, depth=10\\).*" "41.*if \\(depth \\<= 0\\)"]
    125     }
    126 }
    127 
    128 # Testing the continue command.
    129 with_test_prefix "frame enter through continue" {
    130     if { [start_test "TAG: outside the frame"] == 0 } {
    131 	gdb_breakpoint [gdb_get_line_number "TAG: first frame" ${testfile}.c ]
    132 	gdb_continue_to_breakpoint "TAG: first frame" ".*TAG: first frame.*"
    133     }
    134 }
    135