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