summaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.python/pretty-print-call-by-hand.exp
blob: 76386bb6df14ab01a257722d90f2c11cc985123c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Copyright (C) 2022 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# This file is part of the GDB testsuite.  It tests a pretty printer that
# calls an inferior function by hand, triggering a Use-after-Free bug
# (PR gdb/28856).

load_lib gdb-python.exp

standard_testfile

# gdb needs to be started here for skip_python_tests to work.
# prepare_for_testing could be used instead, but it could compile the program
# unnecessarily, so starting GDB like this is preferable.
gdb_start

# Skip all tests if Python scripting is not enabled.
if { [skip_python_tests] } { continue }

if { [prepare_for_testing "failed to prepare" $testfile $srcfile debug] } {
    untested "failed to compile"
    return -1
}

# This proc restarts GDB, makes the inferior reach the desired spot - marked
# by a comment in the .c file - and turns on the pretty printer for testing.
# Starting with a new GDB is important because the test may crash GDB.  The
# return values are here to avoid us trying to test the pretty printer if
# there was a problem getting to main.
proc start_test { breakpoint_comment } {
    global srcdir subdir testfile binfile

    # Start with a fresh gdb.
    # This is important because the test can crash GDB.

    clean_restart ${binfile}

    if {![runto_main]} {
	untested "couldn't run to breakpoint"
	return -1
    }

    # Let GDB get to the return line.
    gdb_breakpoint [gdb_get_line_number ${breakpoint_comment} ${testfile}.c ]
    gdb_continue_to_breakpoint ${breakpoint_comment} ".*"

    gdb_test_no_output "set print pretty on" "starting to pretty print"

    set remote_python_file [gdb_remote_download host ${srcdir}/${subdir}/${testfile}.py]
    gdb_test_no_output "source ${remote_python_file}" "load python file"

    return 0
}

# Start by testing the "run" command, it can't leverage start_test
with_test_prefix "run to frame" {
    if {![runto_main]} {
	untested "couldn't run to main"
    }

    gdb_test_no_output "set print pretty on" "starting to pretty print"

    set remote_python_file [gdb_remote_download host ${srcdir}/${subdir}/${testfile}.py]
    gdb_test_no_output "source ${remote_python_file}" "load python file"

    gdb_breakpoint [gdb_get_line_number "TAG: final frame" ${testfile}.c]
    gdb_continue_to_breakpoint "TAG: final frame" ".*"
}

# Testing the backtrace command.
with_test_prefix "frame print" {
    if { [start_test "TAG: final frame"] == 0 } {
	gdb_test "backtrace -frame-arguments all" [multi_line \
	"#0 .*g \\(mt=mytype is .*\\, depth=0\\).*"\
	"#1 .*g \\(mt=mytype is .*\\, depth=1\\).*"\
	"#2 .*g \\(mt=mytype is .*\\, depth=2\\).*"\
	"#3 .*g \\(mt=mytype is .*\\, depth=3\\).*"\
	"#4 .*g \\(mt=mytype is .*\\, depth=4\\).*"\
	"#5 .*g \\(mt=mytype is .*\\, depth=5\\).*"\
	"#6 .*g \\(mt=mytype is .*\\, depth=6\\).*"\
	"#7 .*g \\(mt=mytype is .*\\, depth=7\\).*"\
	"#8 .*g \\(mt=mytype is .*\\, depth=8\\).*"\
	"#9 .*g \\(mt=mytype is .*\\, depth=9\\).*"\
	"#10 .*g \\(mt=mytype is .*\\, depth=10\\).*"\
	"#11 .*main \\(\\).*"] \
	"backtrace test"
    }
}

# Test the "info frame" command
with_test_prefix "info frame" {
    if { [start_test "TAG: first frame"] == 0 } {
	gdb_test "info frame" "mytype is $hex \"hello world\".*"
    }
}

# Testing the down command.
with_test_prefix "frame movement down" {
    if { [start_test "TAG: first frame"] == 0 } {
	gdb_test "up" [multi_line "#1 .*in main \\(\\) at .*" ".*outside the frame.*"]
	gdb_test "down" [multi_line "#0\\s+g \\(mt=mytype is .*\\, depth=10\\).*" ".*first frame.*"]
    }
}

# Testing the up command.
with_test_prefix "frame movement up" {
    if { [start_test "TAG: final frame"] == 0 } {
	gdb_test "up" [multi_line "#1 .*in g \\(mt=mytype is .*\\, depth=1\\).*" ".*first frame.*"]
    }
}

# Testing the finish command.
with_test_prefix "frame exit through finish" {
    if { [start_test "TAG: final frame"] == 0 } {
	gdb_test "finish" [multi_line ".*.*g \\(mt=mytype is .*\\, depth=0\\).*" ".*g \\(mt=mytype is .*\\, depth=1\\).*" ".*"]
    }
}

# Testing the step command.
with_test_prefix "frame enter through step" {
    if { [start_test "TAG: outside the frame"] == 0 } {
	gdb_test "step" [multi_line "g \\(mt=mytype is .*\\, depth=10\\).*" "41.*if \\(depth \\<= 0\\)"]
    }
}

# Testing the continue command.
with_test_prefix "frame enter through continue" {
    if { [start_test "TAG: outside the frame"] == 0 } {
	gdb_breakpoint [gdb_get_line_number "TAG: first frame" ${testfile}.c ]
	gdb_continue_to_breakpoint "TAG: first frame" ".*TAG: first frame.*"
    }
}