3d353c4f88bfe0bad41b062f019fdf096e5d73a6
[platform/upstream/gdb.git] / gdb / testsuite / gdb.mi / mi-var-child.c
1 /* Copyright 1999-2015 Free Software Foundation, Inc.
2
3    This file is part of GDB.
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 <stdlib.h>
19 #include <string.h>
20
21 struct _simple_struct {
22   int integer;
23   unsigned int unsigned_integer;
24   char character;
25   signed char signed_character;
26   char *char_ptr;
27   int array_of_10[10];
28 };
29
30 typedef struct _simple_struct simpleton;
31
32 simpleton global_simple;
33
34 enum foo {
35   bar = 1,
36   baz
37 };
38
39 typedef enum foo efoo;
40
41 union named_union
42 {
43   int integer;
44   char *char_ptr;
45 };
46
47 typedef struct _struct_decl {
48   int   integer;
49   char  character;
50   char *char_ptr;
51   long  long_int;
52   int  **int_ptr_ptr;
53   long  long_array[12];
54
55   void (*func_ptr) (void);
56   struct _struct_decl (*func_ptr_struct) (int, char *, long);
57   struct _struct_decl *(*func_ptr_ptr) (int, char *, long);
58   union {
59     int   a;
60     char *b;
61     long  c;
62     enum foo d;
63   } u1;
64
65   struct {
66     union {
67       struct {
68         int d;
69         char e[10];
70         int *(*func) (void);
71         efoo foo;
72       } u1s1;
73
74       long f;
75       struct {
76         char array_ptr[2];
77         int (*func) (int, char *);
78       } u1s2;
79     } u2;
80
81     int g;
82     char h;
83     long i[10];
84   } s2;
85 } weird_struct;
86
87 struct _struct_n_pointer {
88   char ****char_ptr;
89   long ****long_ptr;
90   struct _struct_n_pointer *ptrs[3];
91   struct _struct_n_pointer *next;
92 };
93
94 void do_locals_tests (void);
95 void do_block_tests (void);
96 void subroutine1 (int, long *);
97 void nothing (void);
98 void do_children_tests (void);
99 void do_special_tests (void);
100 void incr_a (char);
101
102 void incr_a (char a)
103 {
104   int b;
105   b = a;
106 }
107
108 void
109 do_locals_tests ()
110 {
111   int linteger;
112   int *lpinteger;
113   char lcharacter;
114   char *lpcharacter;
115   long llong;
116   long *lplong;
117   float lfloat;
118   float *lpfloat;
119   double ldouble;
120   double *lpdouble;
121   struct _simple_struct lsimple;
122   struct _simple_struct *lpsimple;
123   void (*func) (void);
124
125   /* Simple assignments */
126   linteger = 1234;
127   lpinteger = &linteger;
128   lcharacter = 'a';
129   lpcharacter = &lcharacter;
130   llong = 2121L;
131   lplong = &llong;
132   lfloat = 2.1;
133   lpfloat = &lfloat;
134   ldouble = 2.718281828459045;
135   lpdouble = &ldouble;
136   lsimple.integer = 1234;
137   lsimple.unsigned_integer = 255;
138   lsimple.character = 'a';
139   lsimple.signed_character = 21;
140   lsimple.char_ptr = &lcharacter;
141   lpsimple = &lsimple;
142   func = nothing;
143
144   /* Check pointers */
145   linteger = 4321;
146   lcharacter = 'b';
147   llong = 1212L;
148   lfloat = 1.2;
149   ldouble = 5.498548281828172;
150   lsimple.integer = 255;
151   lsimple.unsigned_integer = 4321;
152   lsimple.character = 'b';
153   lsimple.signed_character = 0;
154
155   subroutine1 (linteger, &llong);
156 }
157
158 void
159 nothing ()
160 {
161 }
162
163 struct _struct_decl
164 nothing1 (int a, char *b, long c)
165 {
166   struct _struct_decl foo;
167
168   return foo;
169 }
170
171 struct _struct_decl *
172 nothing2 (int a, char *b, long c)
173 {
174   return (struct _struct_decl *) 0;
175 }
176
177 void
178 subroutine1 (int i, long *l)
179 {
180   global_simple.integer = i + 3;
181   i = 212;
182   *l = 12;
183 }
184
185 void
186 do_block_tests ()
187 {
188   int cb = 12;
189
190   {
191     int foo;
192     foo = 123;
193     {
194       int foo2;
195       foo2 = 123;
196       {
197         int foo;
198         foo = 321;
199       }
200       foo2 = 0;
201     }
202     foo = 0;
203   }
204
205   cb = 21;
206 }
207
208 void
209 do_children_tests (void)
210 {
211   weird_struct *weird;
212   struct _struct_n_pointer *psnp;
213   struct _struct_n_pointer snp0, snp1, snp2;
214   char a0[2] = {}, *a1, **a2, ***a3;
215   char b0[2] = {}, *b1, **b2, ***b3;
216   char c0[2] = {}, *c1, **c2, ***c3;
217   long z0, *z1, **z2, ***z3;
218   long y0, *y1, **y2, ***y3;
219   long x0, *x1, **x2, ***x3;
220   int *foo;
221   int bar;
222
223   /* Avoid pointing into NULL, as that is editable on some
224      systems.  */
225   int dummy;
226   int *dummy_ptr = &dummy;
227
228   struct _struct_decl struct_declarations = { 0, 0, NULL, 0, &dummy_ptr };
229   weird = &struct_declarations;
230
231   struct_declarations.integer = 123;
232   weird->char_ptr = "hello";
233   bar = 2121;
234   foo = &bar;
235   struct_declarations.int_ptr_ptr = &foo;
236   weird->long_array[0] = 1234;
237   struct_declarations.long_array[1] = 2345;
238   weird->long_array[2] = 3456;
239   struct_declarations.long_array[3] = 4567;
240   weird->long_array[4] = 5678;
241   struct_declarations.long_array[5] = 6789;
242   weird->long_array[6] = 7890;
243   struct_declarations.long_array[7] = 8901;
244   weird->long_array[8] = 9012;
245   struct_declarations.long_array[9] = 1234;
246
247   weird->func_ptr = nothing;
248   weird->func_ptr_struct = nothing1;
249   weird->func_ptr_ptr = nothing2;
250   struct_declarations.long_array[10] = 3456;
251   struct_declarations.long_array[11] = 5678;
252
253   /* Struct/pointer/array tests */
254   a0[0] = '0';  
255   a1 = a0;
256   a2 = &a1;
257   a3 = &a2;
258   b0[0] = '1';
259   b1 = b0;
260   b2 = &b1;
261   b3 = &b2;
262   c0[1] = '2';
263   c1 = c0;
264   c2 = &c1;
265   c3 = &c2;
266   z0 = 0xdead + 0;
267   z1 = &z0;
268   z2 = &z1;
269   z3 = &z2;
270   y0 = 0xdead + 1;
271   y1 = &y0;
272   y2 = &y1;
273   y3 = &y2;
274   x0 = 0xdead + 2;
275   x1 = &x0;
276   x2 = &x1;
277   x3 = &x2;
278   snp0.char_ptr = &a3;
279   snp0.long_ptr = &z3;
280   snp0.ptrs[0] = &snp0;
281   snp0.ptrs[1] = &snp1;
282   snp0.ptrs[2] = &snp2;
283   snp0.next = &snp1;
284   snp1.char_ptr = &b3;
285   snp1.long_ptr = &y3;
286   snp1.ptrs[0] = &snp0;
287   snp1.ptrs[1] = &snp1;
288   snp1.ptrs[2] = &snp2;
289   snp1.next = &snp2;
290   snp2.char_ptr = &c3;
291   snp2.long_ptr = &x3;
292   snp2.ptrs[0] = &snp0;
293   snp2.ptrs[1] = &snp1;
294   snp2.ptrs[2] = &snp2;
295   snp2.next = 0x0;
296   psnp = &snp0;
297   snp0.char_ptr = &b3;
298   snp1.char_ptr = &c3;
299   snp2.char_ptr = &a3;
300   snp0.long_ptr = &y3;
301   snp1.long_ptr = &x3;
302   snp2.long_ptr = &z3;
303 }
304
305 void
306 do_special_tests (void)
307 {
308   union named_union u;
309   union {
310     int a;
311     char b;
312     long c;
313   } anonu;
314   struct _simple_struct s;
315   struct {
316     int a;
317     char b;
318     long c;
319   } anons;
320   enum foo e;
321   enum { A, B, C } anone;
322   int array[21];
323   int a;
324
325   a = 1;   
326   incr_a(2);
327 }
328
329 struct very_simple_struct
330 {
331   int a;
332   int b;
333 };
334
335 int
336 do_child_deletion (void)
337 {
338   /*: BEGIN: child_deletion :*/
339   struct very_simple_struct s = {1, 2};
340   /*:
341     mi_create_varobj S s "create varobj for s"
342     mi_list_varobj_children S {{S.a a 0 int} {S.b b 0 int}}     \
343        "list children of S"
344     mi_delete_varobj S.a "delete S.a"    
345     mi_delete_varobj S.b "delete S.b"
346     mi_delete_varobj S "delete S"
347     :*/
348   return 99;
349   /*: END: child_deletion :*/  
350 }
351
352 int
353 main (int argc, char *argv [])
354 {
355   do_locals_tests ();
356   do_block_tests ();
357   do_children_tests ();
358   do_special_tests ();
359   do_child_deletion ();
360   exit (0);
361 }
362
363