xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.python/py-prettyprint.c (revision 3117ece4fc4a4ca4489ba793710b60b0d26bab6c)
1 /* This testcase is part of GDB, the GNU debugger.
2 
3    Copyright 2008-2023 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 #endif
114 
115 struct to_string_returns_value_inner
116 {
117   int val;
118 };
119 
120 struct to_string_returns_value_wrapper
121 {
122   struct to_string_returns_value_inner inner;
123 };
124 
125 struct substruct {
126   int a;
127   int b;
128 };
129 
130 struct outerstruct {
131   struct substruct s;
132   int x;
133 };
134 
135 struct outerstruct
136 substruct_test (void)
137 {
138   struct outerstruct outer;
139   outer.s.a = 0;
140   outer.s.b = 0;
141   outer.x = 0;
142 
143   outer.s.a = 3;		/* MI outer breakpoint here */
144 
145   return outer;
146 }
147 
148 typedef struct string_repr
149 {
150   struct whybother
151   {
152     const char *contents;
153   } whybother;
154 } string;
155 
156 /* This lets us avoid malloc.  */
157 int array[100];
158 int narray[10];
159 
160 struct justchildren
161 {
162   int len;
163   int *elements;
164 };
165 
166 typedef struct justchildren nostring_type;
167 
168 struct memory_error
169 {
170   const char *s;
171 };
172 
173 struct container
174 {
175   string name;
176   int len;
177   int *elements;
178   int is_map_p;
179   int is_array_p;
180 };
181 
182 typedef struct container zzz_type;
183 
184 string
185 make_string (const char *s)
186 {
187   string result;
188   result.whybother.contents = s;
189   return result;
190 }
191 
192 zzz_type
193 make_container (const char *s)
194 {
195   zzz_type result;
196 
197   result.name = make_string (s);
198   result.len = 0;
199   result.elements = 0;
200   result.is_map_p = 0;
201   result.is_array_p = 0;
202 
203   return result;
204 }
205 
206 void
207 add_item (zzz_type *c, int val)
208 {
209   if (c->len == 0)
210     c->elements = array;
211   c->elements[c->len] = val;
212   ++c->len;
213 }
214 
215 void
216 set_item(zzz_type *c, int i, int val)
217 {
218   if (i < c->len)
219     c->elements[i] = val;
220 }
221 
222 void init_s(struct s *s, int a)
223 {
224   s->a = a;
225   s->b = &s->a;
226 }
227 
228 void init_ss(struct ss *s, int a, int b)
229 {
230   init_s(&s->a, a);
231   init_s(&s->b, b);
232 }
233 
234 void do_nothing(void)
235 {
236   int c;
237 
238   c = 23;			/* Another MI breakpoint */
239 }
240 
241 struct nullstr
242 {
243   char *s;
244 };
245 
246 struct string_repr string_1 = { { "one" } };
247 struct string_repr string_2 = { { "two" } };
248 
249 int
250 eval_func (int p1, int p2, int p3, int p4, int p5, int p6, int p7, int p8)
251 {
252   return p1;
253 }
254 
255 static void
256 eval_sub (void)
257 {
258   struct eval_type_s { int x; } eval1 = { 1 }, eval2 = { 2 }, eval3 = { 3 },
259 				eval4 = { 4 }, eval5 = { 5 }, eval6 = { 6 },
260 				eval7 = { 7 }, eval8 = { 8 }, eval9 = { 9 };
261 
262   eval1.x++; /* eval-break */
263 }
264 
265 static void
266 bug_14741()
267 {
268   zzz_type c = make_container ("bug_14741");
269   add_item (&c, 71);
270   set_item(&c, 0, 42); /* breakpoint bug 14741 */
271   set_item(&c, 0, 5);
272 }
273 
274 /* Some typedefs/variables for checking that GDB doesn't lose typedefs
275    when looking for a printer.  */
276 typedef int int_type;
277 typedef int_type int_type2;
278 typedef int_type int_type3;
279 
280 int an_int = -1;
281 int_type an_int_type = 1;
282 int_type2 an_int_type2 = 2;
283 int_type3 an_int_type3 = 3;
284 
285 int
286 main ()
287 {
288   struct ss  ss;
289   struct ss  ssa[2];
290   struct arraystruct arraystruct;
291   string x = make_string ("this is x");
292   zzz_type c = make_container ("container");
293   zzz_type c2 = make_container ("container2");
294   const struct string_repr cstring = { { "const string" } };
295   /* Clearing by being `static' could invoke an other GDB C++ bug.  */
296   struct nullstr nullstr;
297   nostring_type nstype, nstype2;
298   struct memory_error me;
299   struct ns ns, ns2;
300   struct lazystring estring, estring2, estring3;
301   struct hint_error hint_error;
302   struct children_as_list children_as_list;
303   struct to_string_returns_value_wrapper tsrvw = { { 1989 } };
304 
305   nstype.elements = narray;
306   nstype.len = 0;
307 
308   me.s = "blah";
309 
310   init_ss(&ss, 1, 2);
311   init_ss(ssa+0, 3, 4);
312   init_ss(ssa+1, 5, 6);
313   memset (&nullstr, 0, sizeof nullstr);
314 
315   arraystruct.y = 7;
316   init_s (&arraystruct.x[0], 23);
317   init_s (&arraystruct.x[1], 24);
318 
319   ns.null_str = "embedded\0null\0string";
320   ns.length = 20;
321 
322   /* Make a "corrupted" string.  */
323   ns2.null_str = NULL;
324   ns2.length = 20;
325 
326   estring.lazy_str = "embedded x\201\202\203\204";
327   estring.len = -1;
328 
329   /* Incomplete UTF-8, but ok Latin-1.  */
330   estring2.lazy_str = "embedded x\302";
331   estring2.len = -1;
332 
333   estring3.lazy_str = NULL;
334   estring3.len = 42;
335 
336 #ifdef __cplusplus
337   S cps;
338 
339   cps.zs = 7;
340   init_s(&cps, 8);
341 
342   SS cpss;
343   cpss.zss = 9;
344   init_s(&cpss.s, 10);
345 
346   SS cpssa[2];
347   cpssa[0].zss = 11;
348   init_s(&cpssa[0].s, 12);
349   cpssa[1].zss = 13;
350   init_s(&cpssa[1].s, 14);
351 
352   SSS sss(15, cps);
353 
354   SSS& ref (sss);
355 
356   Derived derived;
357 
358   Fake fake (42);
359 #endif
360 
361   add_item (&c, 23);		/* MI breakpoint here */
362   add_item (&c, 72);
363 
364 #ifdef MI
365   add_item (&c, 1011);
366   c.elements[0] = 1023;
367   c.elements[0] = 2323;
368 
369   add_item (&c2, 2222);
370   add_item (&c2, 3333);
371 
372   substruct_test ();
373   do_nothing ();
374 #endif
375 
376   nstype.elements[0] = 7;
377   nstype.elements[1] = 42;
378   nstype.len = 2;
379 
380   nstype2 = nstype;
381 
382   eval_sub ();
383 
384   bug_14741();      /* break to inspect struct and union */
385   return 0;
386 }
387