1# Copyright 2019-2021 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# Tests GDB's handling of 'set print max-depth'.
17
18proc compile_and_run_tests { lang } {
19    global testfile
20    global srcfile
21    global binfile
22    global hex
23
24    standard_testfile max-depth.c
25
26    # Create the additional flags.
27    set flags "debug"
28    lappend flags $lang
29    if { "$lang" == "c++" } {
30	lappend flags "additional_flags=-std=c++11"
31    }
32
33    if { [prepare_for_testing "failed to prepare" "${binfile}" "${srcfile}" "${flags}"] } {
34	return 0
35    }
36
37    # Advance to main.
38    if { ![runto_main] } then {
39	fail "can't run to main"
40	return 0
41    }
42
43    # The max-depth setting has no effect as the anonymous scopes are
44    # ignored and the members are aggregated into the parent scope.
45    gdb_print_expr_at_depths "s1" {"{...}" \
46				       "{x = 0, y = 0}"\
47				       "{x = 0, y = 0}"}
48
49    gdb_print_expr_at_depths "s2" {"{...}" \
50				       "{x = 0, y = 0, {z = 0, a = 0}}" \
51				       "{x = 0, y = 0, {z = 0, a = 0}}"}
52
53    gdb_print_expr_at_depths "s3" {"{...}" \
54				       "{x = 0, y = 0, {z = 0, a = 0, {b = 0, c = 0}}}" \
55				       "{x = 0, y = 0, {z = 0, a = 0, {b = 0, c = 0}}}" \
56				       "{x = 0, y = 0, {z = 0, a = 0, {b = 0, c = 0}}}"}
57
58    # Increasing max-depth unfurls more of the object.
59    gdb_print_expr_at_depths "s4" {"{...}" \
60				       "{x = 0, y = 0, l1 = {...}}" \
61				       "{x = 0, y = 0, l1 = {x = 0, y = 0, l2 = {...}}}" \
62				       "{x = 0, y = 0, l1 = {x = 0, y = 0, l2 = {x = 0, y = 0}}}"}
63
64    # Check handling of unions, in this case 'raw' is printed instead of
65    # just {...} as this is not useful.
66    gdb_print_expr_at_depths "s5" {"{...}" \
67				       "{{raw = {...}, {x = 0, y = 0, z = 0}}}" \
68				       "{{raw = \\{0, 0, 0\\}, {x = 0, y = 0, z = 0}}}"}
69
70    # Check handling of typedefs.
71    gdb_print_expr_at_depths "s6" {"{...}" \
72				       "{{raw = {...}, {x = 0, y = 0, z = 0}}}" \
73				       "{{raw = \\{0, 0, 0\\}, {x = 0, y = 0, z = 0}}}"}
74
75    # Multiple anonymous structures in parallel.
76    gdb_print_expr_at_depths "s7" {"{...}" \
77				       "{{x = 0, y = 0}, {z = 0, a = 0}, {b = 0, c = 0}}" \
78				       "{{x = 0, y = 0}, {z = 0, a = 0}, {b = 0, c = 0}}"}
79
80    # Flip flop between named and anonymous.  Expected to unfurl to the
81    # first non-anonymous type.
82    gdb_print_expr_at_depths "s8" {"{...}" \
83				       "{x = 0, y = 0, d1 = {...}}" \
84				       "{x = 0, y = 0, d1 = {z = 0, a = 0, {b = 0, c = 0}}}"}
85
86    # Imbalanced tree, this will unfurl one size more than the other as
87    # one side has more anonymous levels.
88    gdb_print_expr_at_depths "s9" {"{...}" \
89				       "{x = 0, y = 0, {k = 0, j = 0, d1 = {...}}, d2 = {...}}" \
90				       "{x = 0, y = 0, {k = 0, j = 0, d1 = {z = 0, a = 0, {b = 0, c = 0}}}, d2 = {z = 0, a = 0, {b = 0, c = 0}}}"}
91
92    # Arrays are treated as an extra level, while scalars are not.
93    gdb_print_expr_at_depths "s10" {"{...}" \
94					"{x = {...}, y = 0, {k = {...}, j = 0, d1 = {...}}, d2 = {...}}" \
95					"{x = \\{0, 0, 0, 0, 0, 0, 0, 0, 0, 0\\}, y = 0, {k = \\{0, 0, 0, 0, 0, 0, 0, 0, 0, 0\\}, j = 0, d1 = {z = 0, a = 0, {b = {...}, c = 0}}}, d2 = {z = 0, a = 0, {b = {...}, c = 0}}}" \
96					"{x = \\{0, 0, 0, 0, 0, 0, 0, 0, 0, 0\\}, y = 0, {k = \\{0, 0, 0, 0, 0, 0, 0, 0, 0, 0\\}, j = 0, d1 = {z = 0, a = 0, {b = \\{0, 0, 0, 0, 0, 0, 0, 0, 0, 0\\}, c = 0}}}, d2 = {z = 0, a = 0, {b = \\{0, 0, 0, 0, 0, 0, 0, 0, 0, 0\\}, c = 0}}}"}
97
98    # Strings are treated as scalars.
99    gdb_print_expr_at_depths "s11" {"{...}" \
100					"{x = 0, s = \"\\\\000\\\\000\\\\000\\\\000\\\\000\\\\000\\\\000\\\\000\\\\000\", {z = 0, a = 0}}"}
101
102
103    if { $lang == "c++" } {
104	gdb_print_expr_at_depths "c1" {"{...}" \
105					   "{c1 = 1}" }
106	gdb_print_expr_at_depths "c2" { "{...}" "{c2 = 2}" }
107	gdb_print_expr_at_depths "c3" { "{...}" \
108					    "{<C2> = {...}, c3 = 3}" \
109					    "{<C2> = {c2 = 2}, c3 = 3}" }
110	gdb_print_expr_at_depths "c4" { "{...}" "{c4 = 4}" }
111	gdb_print_expr_at_depths "c5" { "{...}" \
112					    "{<C4> = {...}, c5 = 5}" \
113					    "{<C4> = {c4 = 4}, c5 = 5}" }
114	gdb_print_expr_at_depths "c6" { "{...}" \
115					    "{<C5> = {...}, c6 = 6}" \
116					    "{<C5> = {<C4> = {...}, c5 = 5}, c6 = 6}" \
117					    "{<C5> = {<C4> = {c4 = 4}, c5 = 5}, c6 = 6}" }
118	gdb_print_expr_at_depths "c7" { "{...}" \
119					    "{<C1> = {...}, <C3> = {...}, <C6> = {...}, c7 = 7}" \
120					    "{<C1> = {c1 = 1}, <C3> = {<C2> = {...}, c3 = 3}, <C6> = {<C5> = {...}, c6 = 6}, c7 = 7}" \
121					    "{<C1> = {c1 = 1}, <C3> = {<C2> = {c2 = 2}, c3 = 3}, <C6> = {<C5> = {<C4> = {...}, c5 = 5}, c6 = 6}, c7 = 7}" \
122					    "{<C1> = {c1 = 1}, <C3> = {<C2> = {c2 = 2}, c3 = 3}, <C6> = {<C5> = {<C4> = {c4 = 4}, c5 = 5}, c6 = 6}, c7 = 7}" }
123
124	gdb_print_expr_at_depths "v1" [list "{...}" "{v1 = 1}" ]
125	gdb_print_expr_at_depths "v2" [list "{...}" \
126					    "{<V1> = {...}, _vptr.V2 = $hex <VTT for V2>, v2 = 2}" \
127					    "{<V1> = {v1 = 1}, _vptr.V2 = $hex <VTT for V2>, v2 = 2}" ]
128	gdb_print_expr_at_depths "v3" [list "{...}" \
129					    "{<V1> = {...}, _vptr.V3 = $hex <VTT for V3>, v3 = 3}" \
130					    "{<V1> = {v1 = 1}, _vptr.V3 = $hex <VTT for V3>, v3 = 3}" ]
131	gdb_print_expr_at_depths "v4" [list "{...}" \
132					    "{<V2> = {...}, _vptr.V4 = $hex <vtable for V4\[^>\]+>, v4 = 4}" \
133					    "{<V2> = {<V1> = {...}, _vptr.V2 = $hex <VTT for V4>, v2 = 2}, _vptr.V4 = $hex <vtable for V4\[^>\]+>, v4 = 4}" \
134					    "{<V2> = {<V1> = {v1 = 1}, _vptr.V2 = $hex <VTT for V4>, v2 = 2}, _vptr.V4 = $hex <vtable for V4\[^>\]+>, v4 = 4}" ]
135	gdb_print_expr_at_depths "v5" [list "{...}" \
136					    "{<V2> = {...}, _vptr.V5 = $hex <vtable for V5\[^>\]+>, v5 = 1}" \
137					    "{<V2> = {<V1> = {...}, _vptr.V2 = $hex <VTT for V5>, v2 = 2}, _vptr.V5 = $hex <vtable for V5\[^>\]+>, v5 = 1}" \
138					    "{<V2> = {<V1> = {v1 = 1}, _vptr.V2 = $hex <VTT for V5>, v2 = 2}, _vptr.V5 = $hex <vtable for V5\[^>\]+>, v5 = 1}" ]
139	gdb_print_expr_at_depths "v6" [list "{...}" \
140					    "{<V2> = {...}, <V3> = {...}, _vptr.V6 = $hex <vtable for V6\[^>\]+>, v6 = 1}" \
141					    "{<V2> = {<V1> = {...}, _vptr.V2 = $hex <vtable for V6\[^>\]+>, v2 = 2}, <V3> = {_vptr.V3 = $hex <VTT for V6>, v3 = 3}, _vptr.V6 = $hex <vtable for V6\[^>\]+>, v6 = 1}" \
142					    "{<V2> = {<V1> = {v1 = 1}, _vptr.V2 = $hex <vtable for V6\[^>\]+>, v2 = 2}, <V3> = {_vptr.V3 = $hex <VTT for V6>, v3 = 3}, _vptr.V6 = $hex <vtable for V6\[^>\]+>, v6 = 1}" ]
143	gdb_print_expr_at_depths "v7" [list "{...}" \
144					    "{<V4> = {...}, <V5> = {...}, <V6> = {...}, _vptr.V7 = $hex <vtable for V7\[^>\]+>, v7 = 1}" \
145					    "{<V4> = {<V2> = {...}, _vptr.V4 = $hex <vtable for V7\[^>\]+>, v4 = 4}, <V5> = {_vptr.V5 = $hex <vtable for V7\[^>\]+>, v5 = 1}, <V6> = {<V3> = {...}, _vptr.V6 = $hex <vtable for V7\[^>\]+>, v6 = 1}, _vptr.V7 = $hex <vtable for V7\[^>\]+>, v7 = 1}" \
146					    "{<V4> = {<V2> = {<V1> = {...}, _vptr.V2 = $hex <vtable for V7\[^>\]+>, v2 = 2}, _vptr.V4 = $hex <vtable for V7\[^>\]+>, v4 = 4}, <V5> = {_vptr.V5 = $hex <vtable for V7\[^>\]+>, v5 = 1}, <V6> = {<V3> = {_vptr.V3 = $hex <VTT for V7>, v3 = 3}, _vptr.V6 = $hex <vtable for V7\[^>\]+>, v6 = 1}, _vptr.V7 = $hex <vtable for V7\[^>\]+>, v7 = 1}" \
147					    "{<V4> = {<V2> = {<V1> = {v1 = 1}, _vptr.V2 = $hex <vtable for V7\[^>\]+>, v2 = 2}, _vptr.V4 = $hex <vtable for V7\[^>\]+>, v4 = 4}, <V5> = {_vptr.V5 = $hex <vtable for V7\[^>\]+>, v5 = 1}, <V6> = {<V3> = {_vptr.V3 = $hex <VTT for V7>, v3 = 3}, _vptr.V6 = $hex <vtable for V7\[^>\]+>, v6 = 1}, _vptr.V7 = $hex <vtable for V7\[^>\]+>, v7 = 1}" ]
148    }
149}
150
151compile_and_run_tests $lang
152