Automatic Copyright Year update after running gdb/copyright.py
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / ptype-offsets.exp
CommitLineData
7c161838
SDJ
1# This testcase is part of GDB, the GNU debugger.
2
88b9d363 3# Copyright 2017-2022 Free Software Foundation, Inc.
7c161838
SDJ
4
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 3 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18# This testcase exercises the "ptype /o" feature, which can be used to
19# print the offsets and sizes of each field of a struct/union/class.
20
21standard_testfile .cc
22
23# Test only works on LP64 targets. That's how we guarantee that the
24# expected holes will be present in the struct.
25if { ![is_lp64_target] } {
26 untested "test work only on lp64 targets"
27 return 0
28}
29
30if { [prepare_for_testing "failed to prepare" $testfile $srcfile \
31 { debug c++ }] } {
32 return -1
33}
34
35# Test general offset printing, ctor/dtor printing, union, formatting.
36gdb_test "ptype /o struct abc" \
844333e2 37 [string_to_regexp [multi_line \
fbb46296
LS
38"/* offset | size */ type = struct abc \{" \
39" public:" \
40"/* 8 | 8 */ void *field1;" \
41"/* 16: 0 | 4 */ unsigned int field2 : 1;" \
42"/* XXX 7-bit hole */" \
43"/* XXX 3-byte hole */" \
44"/* 20 | 4 */ int field3;" \
45"/* 24 | 1 */ signed char field4;" \
46"/* XXX 7-byte hole */" \
47"/* 32 | 8 */ uint64_t field5;" \
48"/* 40 | 8 */ union \{" \
49"/* 8 */ void *field6;" \
50"/* 4 */ int field7;" \
844333e2 51"" \
fbb46296
LS
52" /* total size (bytes): 8 */" \
53" \} field8;" \
54"/* 48 | 2 */ my_int_type field9;" \
55"/* XXX 6-byte padding */" \
844333e2 56"" \
fbb46296
LS
57" /* total size (bytes): 56 */" \
58" \}"]]
59
60# test "ptype /ox"
61gdb_test "ptype /ox struct abc" \
62 [string_to_regexp [multi_line \
63"/* offset | size */ type = struct abc {" \
64" public:" \
65"/* 0x0008 | 0x0008 */ void *field1;" \
66"/* 0x0010: 0x0 | 0x0004 */ unsigned int field2 : 1;" \
67"/* XXX 7-bit hole */" \
68"/* XXX 3-byte hole */" \
69"/* 0x0014 | 0x0004 */ int field3;" \
70"/* 0x0018 | 0x0001 */ signed char field4;" \
71"/* XXX 7-byte hole */" \
72"/* 0x0020 | 0x0008 */ uint64_t field5;" \
73"/* 0x0028 | 0x0008 */ union \{" \
74"/* 0x0008 */ void *field6;" \
75"/* 0x0004 */ int field7;" \
76"" \
77" /* total size (bytes): 8 */" \
78" \} field8;" \
79"/* 0x0030 | 0x0002 */ my_int_type field9;" \
80"/* XXX 6-byte padding */" \
81"" \
82" /* total size (bytes): 56 */" \
83" \}"]]
7c161838
SDJ
84
85# Test "ptype /oTM".
86gdb_test "ptype /oTM struct abc" \
844333e2 87 [string_to_regexp [multi_line \
fbb46296
LS
88"/* offset | size */ type = struct abc \{" \
89" public:" \
90"/* 8 | 8 */ void *field1;" \
91"/* 16: 0 | 4 */ unsigned int field2 : 1;" \
92"/* XXX 7-bit hole */" \
93"/* XXX 3-byte hole */" \
94"/* 20 | 4 */ int field3;" \
95"/* 24 | 1 */ signed char field4;" \
96"/* XXX 7-byte hole */" \
97"/* 32 | 8 */ uint64_t field5;" \
98"/* 40 | 8 */ union \{" \
99"/* 8 */ void *field6;" \
100"/* 4 */ int field7;" \
844333e2 101"" \
fbb46296
LS
102" /* total size (bytes): 8 */" \
103" \} field8;" \
104"/* 48 | 2 */ my_int_type field9;" \
844333e2 105"" \
fbb46296
LS
106" abc(void);" \
107" ~abc();" \
844333e2 108"" \
fbb46296
LS
109" typedef short my_int_type;" \
110"/* XXX 6-byte padding */" \
844333e2 111"" \
fbb46296
LS
112" /* total size (bytes): 56 */" \
113" \}"]]
7c161838
SDJ
114
115# Test "ptype /TMo". This should be the same as "ptype /o".
116gdb_test "ptype /TMo struct abc" \
844333e2 117 [string_to_regexp [multi_line \
fbb46296
LS
118"/* offset | size */ type = struct abc \{" \
119" public:" \
120"/* 8 | 8 */ void *field1;" \
121"/* 16: 0 | 4 */ unsigned int field2 : 1;" \
122"/* XXX 7-bit hole */" \
123"/* XXX 3-byte hole */" \
124"/* 20 | 4 */ int field3;" \
125"/* 24 | 1 */ signed char field4;" \
126"/* XXX 7-byte hole */" \
127"/* 32 | 8 */ uint64_t field5;" \
128"/* 40 | 8 */ union \{" \
129"/* 8 */ void *field6;" \
130"/* 4 */ int field7;" \
844333e2 131"" \
fbb46296
LS
132" /* total size (bytes): 8 */" \
133" \} field8;" \
134"/* 48 | 2 */ my_int_type field9;" \
135"/* XXX 6-byte padding */" \
844333e2 136"" \
fbb46296
LS
137" /* total size (bytes): 56 */" \
138" \}"]]
7c161838
SDJ
139
140# Test nested structs.
141gdb_test "ptype /o struct pqr" \
844333e2 142 [string_to_regexp [multi_line \
fbb46296
LS
143"/* offset | size */ type = struct pqr \{" \
144"/* 0 | 4 */ int ff1;" \
145"/* XXX 4-byte hole */" \
146"/* 8 | 40 */ struct xyz \{" \
147"/* 8 | 4 */ int f1;" \
148"/* 12 | 1 */ signed char f2;" \
149"/* XXX 3-byte hole */" \
150"/* 16 | 8 */ void *f3;" \
151"/* 24 | 24 */ struct tuv \{" \
152"/* 24 | 4 */ int a1;" \
153"/* XXX 4-byte hole */" \
154"/* 32 | 8 */ signed char *a2;" \
155"/* 40 | 4 */ int a3;" \
156"/* XXX 4-byte padding */" \
844333e2 157"" \
fbb46296
LS
158" /* total size (bytes): 24 */" \
159" \} f4;" \
844333e2 160"" \
fbb46296
LS
161" /* total size (bytes): 40 */" \
162" \} ff2;" \
163"/* 48 | 1 */ signed char ff3;" \
164"/* XXX 7-byte padding */" \
844333e2 165"" \
fbb46296
LS
166" /* total size (bytes): 56 */" \
167" \}"]]
7c161838 168
fbb46296
LS
169# Test nested struct with /x
170gdb_test "ptype /ox struct pqr" \
844333e2 171 [string_to_regexp [multi_line \
fbb46296
LS
172"/* offset | size */ type = struct pqr \{" \
173"/* 0x0000 | 0x0004 */ int ff1;" \
174"/* XXX 4-byte hole */" \
175"/* 0x0008 | 0x0028 */ struct xyz \{" \
176"/* 0x0008 | 0x0004 */ int f1;" \
177"/* 0x000c | 0x0001 */ signed char f2;" \
178"/* XXX 3-byte hole */" \
179"/* 0x0010 | 0x0008 */ void *f3;" \
180"/* 0x0018 | 0x0018 */ struct tuv \{" \
181"/* 0x0018 | 0x0004 */ int a1;" \
182"/* XXX 4-byte hole */" \
183"/* 0x0020 | 0x0008 */ signed char *a2;" \
184"/* 0x0028 | 0x0004 */ int a3;" \
185"/* XXX 4-byte padding */" \
844333e2 186"" \
fbb46296
LS
187" /* total size (bytes): 24 */" \
188" \} f4;" \
844333e2 189"" \
fbb46296
LS
190" /* total size (bytes): 40 */" \
191" \} ff2;" \
192"/* 0x0030 | 0x0001 */ signed char ff3;" \
193"/* XXX 7-byte padding */" \
844333e2 194"" \
fbb46296
LS
195" /* total size (bytes): 56 */" \
196" \}"]]
7c161838 197
fbb46296
LS
198
199# Test that the offset is properly reset when we are printing a union
200# and go inside two inner structs.
201# This also tests a struct inside a struct inside a union.
202gdb_test "ptype /o union qwe" \
844333e2 203 [string_to_regexp [multi_line \
fbb46296
LS
204"/* offset | size */ type = union qwe \{" \
205"/* 24 */ struct tuv \{" \
206"/* 0 | 4 */ int a1;" \
207"/* XXX 4-byte hole */" \
208"/* 8 | 8 */ signed char *a2;" \
209"/* 16 | 4 */ int a3;" \
210"/* XXX 4-byte padding */" \
844333e2
TT
211"" \
212" /* total size (bytes): 24 */" \
213" \} fff1;" \
fbb46296
LS
214"/* 40 */ struct xyz \{" \
215"/* 0 | 4 */ int f1;" \
216"/* 4 | 1 */ signed char f2;" \
217"/* XXX 3-byte hole */" \
218"/* 8 | 8 */ void *f3;" \
219"/* 16 | 24 */ struct tuv \{" \
220"/* 16 | 4 */ int a1;" \
221"/* XXX 4-byte hole */" \
222"/* 24 | 8 */ signed char *a2;" \
223"/* 32 | 4 */ int a3;" \
224"/* XXX 4-byte padding */" \
844333e2
TT
225"" \
226" /* total size (bytes): 24 */" \
227" \} f4;" \
228"" \
229" /* total size (bytes): 40 */" \
230" \} fff2;" \
844333e2
TT
231"" \
232" /* total size (bytes): 40 */" \
fbb46296
LS
233" \}"]]
234
235# Test printing a struct that contains a union, and that also
236# contains a struct.
237gdb_test "ptype /o struct poi" \
238 [string_to_regexp [multi_line \
239"/* offset | size */ type = struct poi \{" \
240"/* 0 | 4 */ int f1;" \
241"/* XXX 4-byte hole */" \
242"/* 8 | 40 */ union qwe \{" \
243"/* 24 */ struct tuv \{" \
244"/* 8 | 4 */ int a1;" \
245"/* XXX 4-byte hole */" \
246"/* 16 | 8 */ signed char *a2;" \
247"/* 24 | 4 */ int a3;" \
248"/* XXX 4-byte padding */" \
844333e2
TT
249"" \
250" /* total size (bytes): 24 */" \
fbb46296
LS
251" \} fff1;" \
252"/* 40 */ struct xyz \{" \
253"/* 8 | 4 */ int f1;" \
254"/* 12 | 1 */ signed char f2;" \
255"/* XXX 3-byte hole */" \
256"/* 16 | 8 */ void *f3;" \
257"/* 24 | 24 */ struct tuv \{" \
258"/* 24 | 4 */ int a1;" \
259"/* XXX 4-byte hole */" \
260"/* 32 | 8 */ signed char *a2;" \
261"/* 40 | 4 */ int a3;" \
262"/* XXX 4-byte padding */" \
263"" \
264" /* total size (bytes): 24 */" \
265" \} f4;" \
266"" \
267" /* total size (bytes): 40 */" \
268" \} fff2;" \
269"/* XXX 32-byte padding */" \
844333e2
TT
270"" \
271" /* total size (bytes): 40 */" \
fbb46296
LS
272" \} f2;" \
273"/* 48 | 2 */ uint16_t f3;" \
274"/* XXX 6-byte hole */" \
275"/* 56 | 56 */ struct pqr \{" \
276"/* 56 | 4 */ int ff1;" \
277"/* XXX 4-byte hole */" \
278"/* 64 | 40 */ struct xyz \{" \
279"/* 64 | 4 */ int f1;" \
280"/* 68 | 1 */ signed char f2;" \
281"/* XXX 3-byte hole */" \
282"/* 72 | 8 */ void *f3;" \
283"/* 80 | 24 */ struct tuv \{" \
284"/* 80 | 4 */ int a1;" \
285"/* XXX 4-byte hole */" \
286"/* 88 | 8 */ signed char *a2;" \
287"/* 96 | 4 */ int a3;" \
288"/* XXX 4-byte padding */" \
844333e2 289"" \
fbb46296
LS
290" /* total size (bytes): 24 */" \
291" \} f4;" \
292"" \
293" /* total size (bytes): 40 */" \
294" \} ff2;" \
295"/* 104 | 1 */ signed char ff3;" \
296"/* XXX 7-byte padding */" \
297"" \
298" /* total size (bytes): 56 */" \
299" \} f4;" \
844333e2 300"" \
fbb46296
LS
301" /* total size (bytes): 112 */" \
302" \}"]]
7c161838
SDJ
303
304# Test printing a struct with several bitfields, laid out in various
305# ways.
306#
307# Because dealing with bitfields and offsets is difficult, it can be
308# tricky to confirm that the output of this command is accurate. A
309# nice way to do that is to use GDB's "x" command and print the actual
310# memory layout of the struct. In order to differentiate between
311# bitfields and non-bitfield variables, one can assign "-1" to every
312# bitfield in the struct. An example of the output of "x" using
313# "struct tyu" is:
314#
315# (gdb) x/24xb &e
316# 0x7fffffffd540: 0xff 0xff 0xff 0x1f 0x00 0x00 0x00 0x00
317# 0x7fffffffd548: 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff
318# 0x7fffffffd550: 0xff 0x00 0x00 0x00 0x00 0x00 0x00 0x00
319gdb_test "ptype /o struct tyu" \
844333e2 320 [string_to_regexp [multi_line \
fbb46296
LS
321"/* offset | size */ type = struct tyu \{" \
322"/* 0: 0 | 4 */ int a1 : 1;" \
323"/* 0: 1 | 4 */ int a2 : 3;" \
324"/* 0: 4 | 4 */ int a3 : 23;" \
325"/* 3: 3 | 1 */ signed char a4 : 2;" \
326"/* XXX 3-bit hole */" \
327"/* XXX 4-byte hole */" \
328"/* 8 | 8 */ int64_t a5;" \
329"/* 16: 0 | 4 */ int a6 : 5;" \
330"/* 16: 5 | 8 */ int64_t a7 : 3;" \
331"/* XXX 7-byte padding */" \
332"" \
333" /* total size (bytes): 24 */" \
334" \}"]]
7c161838
SDJ
335
336gdb_test "ptype /o struct asd" \
844333e2 337 [string_to_regexp [multi_line \
fbb46296
LS
338"/* offset | size */ type = struct asd \{" \
339"/* 0 | 32 */ struct asd::jkl \{" \
340"/* 0 | 8 */ signed char *f1;" \
341"/* 8 | 8 */ union \{" \
342"/* 8 */ void *ff1;" \
844333e2 343"" \
fbb46296
LS
344" /* total size (bytes): 8 */" \
345" \} f2;" \
346"/* 16 | 8 */ union \{" \
347"/* 8 */ signed char *ff2;" \
844333e2 348"" \
fbb46296
LS
349" /* total size (bytes): 8 */" \
350" \} f3;" \
351"/* 24: 0 | 4 */ int f4 : 5;" \
352"/* 24: 5 | 4 */ unsigned int f5 : 1;" \
353"/* XXX 2-bit hole */" \
354"/* XXX 1-byte hole */" \
355"/* 26 | 2 */ short f6;" \
356"/* XXX 4-byte padding */" \
357"" \
358" /* total size (bytes): 32 */" \
359" \} f7;" \
360"/* 32 | 8 */ unsigned long f8;" \
361"/* 40 | 8 */ signed char *f9;" \
362"/* 48: 0 | 4 */ int f10 : 4;" \
363"/* 48: 4 | 4 */ unsigned int f11 : 1;" \
364"/* 48: 5 | 4 */ unsigned int f12 : 1;" \
365"/* 48: 6 | 4 */ unsigned int f13 : 1;" \
366"/* 48: 7 | 4 */ unsigned int f14 : 1;" \
367"/* XXX 7-byte hole */" \
368"/* 56 | 8 */ void *f15;" \
369"/* 64 | 8 */ void *f16;" \
370"" \
371" /* total size (bytes): 72 */" \
372" \}"]]
7c161838
SDJ
373
374# Test that we don't print any header when issuing a "ptype /o" on a
375# non-struct, non-union, non-class type.
376gdb_test "ptype /o int" "int"
377gdb_test "ptype /o uint8_t" "char"
378
379# Test that the "whatis" command doesn't print anything related to the
380# "offsets" feature, even when receiving the "/o" parameter.
381set test "whatis /o asd"
382gdb_test_multiple "$test" "$test" {
383 -re "^$test\r\ntype = asd\r\n$gdb_prompt $" {
384 pass $test
385 }
386}
16ff70dd
SDJ
387
388# Test that printing a struct with a static member of itself doesn't
389# get us into an infinite loop.
390gdb_test "ptype/o static_member" \
844333e2 391 [string_to_regexp [multi_line \
fbb46296
LS
392"/* offset | size */ type = struct static_member \{" \
393" static static_member Empty;" \
394"/* 0 | 4 */ int abc;" \
844333e2 395"" \
fbb46296
LS
396" /* total size (bytes): 4 */" \
397" \}"]]
432ce4cf
TT
398
399# Test that the "no data fields" text is indented properly.
400gdb_test "ptype/o empty_member" \
401 [string_to_regexp [multi_line \
fbb46296
LS
402"/* offset | size */ type = struct empty_member \{" \
403"/* 0 | 1 */ struct {" \
404" <no data fields>" \
432ce4cf 405"" \
fbb46296
LS
406" /* total size (bytes): 1 */" \
407" } empty;" \
408"/* XXX 3-byte hole */" \
409"/* 4 | 4 */ int an_int;" \
432ce4cf 410"" \
fbb46296
LS
411" /* total size (bytes): 8 */" \
412" \}"]]
413
414with_test_prefix "with_hex_default" {
415 # Test setting default display to hex
a3237c7c 416 gdb_test_no_output "set print type hex on"
fbb46296
LS
417 gdb_test "show print type hex" \
418 "Display of struct members offsets and sizes in hexadecimal is on"
419
420 # test "ptype /o" is now equivalent to "ptype /ox"
421 gdb_test "ptype /o struct abc" \
422 [string_to_regexp [multi_line \
423 "/* offset | size */ type = struct abc \{" \
424 " public:" \
425 "/* 0x0008 | 0x0008 */ void *field1;" \
426 "/* 0x0010: 0x0 | 0x0004 */ unsigned int field2 : 1;" \
427 "/* XXX 7-bit hole */" \
428 "/* XXX 3-byte hole */" \
429 "/* 0x0014 | 0x0004 */ int field3;" \
430 "/* 0x0018 | 0x0001 */ signed char field4;" \
431 "/* XXX 7-byte hole */" \
432 "/* 0x0020 | 0x0008 */ uint64_t field5;" \
433 "/* 0x0028 | 0x0008 */ union \{" \
434 "/* 0x0008 */ void *field6;" \
435 "/* 0x0004 */ int field7;" \
436 "" \
437 " /* total size (bytes): 8 */" \
438 " \} field8;" \
439 "/* 0x0030 | 0x0002 */ my_int_type field9;" \
440 "/* XXX 6-byte padding */" \
441 "" \
442 " /* total size (bytes): 56 */" \
443 " \}"]]
444
445 gdb_test "ptype /od struct abc" \
446 [string_to_regexp [multi_line \
447 "/* offset | size */ type = struct abc \{" \
448 " public:" \
449 "/* 8 | 8 */ void *field1;" \
450 "/* 16: 0 | 4 */ unsigned int field2 : 1;" \
451 "/* XXX 7-bit hole */" \
452 "/* XXX 3-byte hole */" \
453 "/* 20 | 4 */ int field3;" \
454 "/* 24 | 1 */ signed char field4;" \
455 "/* XXX 7-byte hole */" \
456 "/* 32 | 8 */ uint64_t field5;" \
457 "/* 40 | 8 */ union \{" \
458 "/* 8 */ void *field6;" \
459 "/* 4 */ int field7;" \
460 "" \
461 " /* total size (bytes): 8 */" \
462 " \} field8;" \
463 "/* 48 | 2 */ my_int_type field9;" \
464 "/* XXX 6-byte padding */" \
465 "" \
466 " /* total size (bytes): 56 */" \
467 " \}"]]
468
469 # restore
a3237c7c 470 gdb_test_no_output "set print type hex off"
fbb46296 471}
This page took 0.968567 seconds and 4 git commands to generate.