]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.python/py-prettyprint.c
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.python / py-prettyprint.c
1 /* This testcase is part of GDB, the GNU debugger.
2
3 Copyright 2008-2024 Free Software Foundation, Inc.
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 #include <string.h>
19
20 struct s
21 {
22 int a;
23 int *b;
24 };
25
26 struct ss
27 {
28 struct s a;
29 struct s b;
30 };
31
32 struct arraystruct
33 {
34 int y;
35 struct s x[2];
36 };
37
38 struct ns {
39 const char *null_str;
40 int length;
41 };
42
43 struct lazystring {
44 const char *lazy_str;
45 /* If -1, don't pass length to gdb.lazy_string(). */
46 int len;
47 };
48
49 struct hint_error {
50 int x;
51 };
52
53 struct children_as_list {
54 int x;
55 };
56
57 #ifdef __cplusplus
58 struct S : public s {
59 int zs;
60 };
61
62 struct SS {
63 int zss;
64 S s;
65 };
66
67 struct SSS
68 {
69 SSS (int x, const S& r);
70 int a;
71 const S &b;
72 };
73 SSS::SSS (int x, const S& r) : a(x), b(r) { }
74
75 class VirtualTest
76 {
77 private:
78 int value;
79
80 public:
81 VirtualTest ()
82 {
83 value = 1;
84 }
85 };
86
87 class Vbase1 : public virtual VirtualTest { };
88 class Vbase2 : public virtual VirtualTest { };
89 class Vbase3 : public virtual VirtualTest { };
90
91 class Derived : public Vbase1, public Vbase2, public Vbase3
92 {
93 private:
94 int value;
95
96 public:
97 Derived ()
98 {
99 value = 2;
100 }
101 };
102
103 class Fake
104 {
105 int sname;
106
107 public:
108 Fake (const int name = 0):
109 sname (name)
110 {
111 }
112 };
113
114 struct has_static_member
115 {
116 static s global;
117 };
118
119 s has_static_member::global;
120
121 #endif
122
123 struct to_string_returns_value_inner
124 {
125 int val;
126 };
127
128 struct to_string_returns_value_wrapper
129 {
130 struct to_string_returns_value_inner inner;
131 };
132
133 struct substruct {
134 int a;
135 int b;
136 };
137
138 struct outerstruct {
139 struct substruct s;
140 int x;
141 };
142
143 struct outerstruct
144 substruct_test (void)
145 {
146 struct outerstruct outer;
147 outer.s.a = 0;
148 outer.s.b = 0;
149 outer.x = 0;
150
151 outer.s.a = 3; /* MI outer breakpoint here */
152
153 return outer;
154 }
155
156 typedef struct string_repr
157 {
158 struct whybother
159 {
160 const char *contents;
161 } whybother;
162 } string;
163
164 /* This lets us avoid malloc. */
165 int array[100];
166 int narray[10];
167
168 struct justchildren
169 {
170 int len;
171 int *elements;
172 };
173
174 typedef struct justchildren nostring_type;
175
176 struct memory_error
177 {
178 const char *s;
179 };
180
181 struct container
182 {
183 string name;
184 int len;
185 int *elements;
186 int is_map_p;
187 int is_array_p;
188 };
189
190 typedef struct container zzz_type;
191
192 string
193 make_string (const char *s)
194 {
195 string result;
196 result.whybother.contents = s;
197 return result;
198 }
199
200 zzz_type
201 make_container (const char *s)
202 {
203 zzz_type result;
204
205 result.name = make_string (s);
206 result.len = 0;
207 result.elements = 0;
208 result.is_map_p = 0;
209 result.is_array_p = 0;
210
211 return result;
212 }
213
214 void
215 add_item (zzz_type *c, int val)
216 {
217 if (c->len == 0)
218 c->elements = array;
219 c->elements[c->len] = val;
220 ++c->len;
221 }
222
223 void
224 set_item(zzz_type *c, int i, int val)
225 {
226 if (i < c->len)
227 c->elements[i] = val;
228 }
229
230 void init_s(struct s *s, int a)
231 {
232 s->a = a;
233 s->b = &s->a;
234 }
235
236 void init_ss(struct ss *s, int a, int b)
237 {
238 init_s(&s->a, a);
239 init_s(&s->b, b);
240 }
241
242 void do_nothing(void)
243 {
244 int c;
245
246 c = 23; /* Another MI breakpoint */
247 }
248
249 struct nullstr
250 {
251 char *s;
252 };
253
254 struct string_repr string_1 = { { "one" } };
255 struct string_repr string_2 = { { "two" } };
256
257 int
258 eval_func (int p1, int p2, int p3, int p4, int p5, int p6, int p7, int p8)
259 {
260 return p1;
261 }
262
263 static void
264 eval_sub (void)
265 {
266 struct eval_type_s { int x; } eval1 = { 1 }, eval2 = { 2 }, eval3 = { 3 },
267 eval4 = { 4 }, eval5 = { 5 }, eval6 = { 6 },
268 eval7 = { 7 }, eval8 = { 8 }, eval9 = { 9 };
269
270 eval1.x++; /* eval-break */
271 }
272
273 static void
274 bug_14741()
275 {
276 zzz_type c = make_container ("bug_14741");
277 add_item (&c, 71);
278 set_item(&c, 0, 42); /* breakpoint bug 14741 */
279 set_item(&c, 0, 5);
280 }
281
282 /* Some typedefs/variables for checking that GDB doesn't lose typedefs
283 when looking for a printer. */
284 typedef int int_type;
285 typedef int_type int_type2;
286 typedef int_type int_type3;
287
288 int an_int = -1;
289 int_type an_int_type = 1;
290 int_type2 an_int_type2 = 2;
291 int_type3 an_int_type3 = 3;
292
293 int
294 main ()
295 {
296 struct ss ss;
297 struct ss ssa[2];
298 struct arraystruct arraystruct;
299 string x = make_string ("this is x");
300 zzz_type c = make_container ("container");
301 zzz_type c2 = make_container ("container2");
302 const struct string_repr cstring = { { "const string" } };
303 /* Clearing by being `static' could invoke an other GDB C++ bug. */
304 struct nullstr nullstr;
305 nostring_type nstype, nstype2;
306 struct memory_error me;
307 struct ns ns, ns2;
308 struct lazystring estring, estring2, estring3;
309 struct hint_error hint_error;
310 struct children_as_list children_as_list;
311 struct to_string_returns_value_wrapper tsrvw = { { 1989 } };
312
313 nstype.elements = narray;
314 nstype.len = 0;
315
316 me.s = "blah";
317
318 init_ss(&ss, 1, 2);
319 init_ss(ssa+0, 3, 4);
320 init_ss(ssa+1, 5, 6);
321 memset (&nullstr, 0, sizeof nullstr);
322
323 arraystruct.y = 7;
324 init_s (&arraystruct.x[0], 23);
325 init_s (&arraystruct.x[1], 24);
326
327 ns.null_str = "embedded\0null\0string";
328 ns.length = 20;
329
330 /* Make a "corrupted" string. */
331 ns2.null_str = NULL;
332 ns2.length = 20;
333
334 estring.lazy_str = "embedded x\201\202\203\204";
335 estring.len = -1;
336
337 /* Incomplete UTF-8, but ok Latin-1. */
338 estring2.lazy_str = "embedded x\302";
339 estring2.len = -1;
340
341 estring3.lazy_str = NULL;
342 estring3.len = 42;
343
344 #ifdef __cplusplus
345 S cps;
346
347 cps.zs = 7;
348 init_s(&cps, 8);
349
350 SS cpss;
351 cpss.zss = 9;
352 init_s(&cpss.s, 10);
353
354 SS cpssa[2];
355 cpssa[0].zss = 11;
356 init_s(&cpssa[0].s, 12);
357 cpssa[1].zss = 13;
358 init_s(&cpssa[1].s, 14);
359
360 SSS sss(15, cps);
361
362 SSS& ref (sss);
363
364 Derived derived;
365
366 Fake fake (42);
367
368 init_s (&has_static_member::global, 23);
369 has_static_member has_member;
370 #endif
371
372 add_item (&c, 23); /* MI breakpoint here */
373 add_item (&c, 72);
374
375 #ifdef MI
376 add_item (&c, 1011);
377 c.elements[0] = 1023;
378 c.elements[0] = 2323;
379
380 add_item (&c2, 2222);
381 add_item (&c2, 3333);
382
383 substruct_test ();
384 do_nothing ();
385 #endif
386
387 nstype.elements[0] = 7;
388 nstype.elements[1] = 42;
389 nstype.len = 2;
390
391 nstype2 = nstype;
392
393 eval_sub ();
394
395 bug_14741(); /* break to inspect struct and union */
396 return 0;
397 }