* printcmd.c (print_address_demangle): Remove special case for 0.
[platform/upstream/binutils.git] / gdb / testsuite / gdb.mi / var-cmd.c
1 /* Copyright 1999, 2004, 2007-2012 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[10];
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 struct anonymous {
95   int a;
96   struct {
97     int b;
98     char *c;
99     union {
100       int d;
101       void *e;
102       char f;
103       struct {
104         char g;
105         const char **h;
106         simpleton ***simple;
107       };
108     };
109   };
110 };
111
112 void do_locals_tests (void);
113 void do_block_tests (void);
114 void subroutine1 (int, long *);
115 void nothing (void);
116 void do_children_tests (void);
117 void do_special_tests (void);
118 void incr_a (char);
119
120 void incr_a (char a)
121 {
122   int b;
123   b = a;
124 }
125
126 int array[] = {1,2,3};
127 int array2[] = {4,5,6};
128 int *array_ptr = array;
129
130 void
131 do_locals_tests ()
132 {
133   int linteger = 0;
134   int *lpinteger = 0;
135   char lcharacter[2] = { 0, 0 };
136   char *lpcharacter = 0;
137   long llong = 0;
138   long *lplong = 0;
139   float lfloat = 0;
140   float *lpfloat = 0;
141   double ldouble = 0;
142   double *lpdouble = 0;
143   struct _simple_struct lsimple = { 0 };
144   struct _simple_struct *lpsimple = 0;
145   void (*func) (void) = 0;
146
147   /* Simple assignments */
148   linteger = 1234;
149   lpinteger = &linteger;
150   lcharacter[0] = 'a';
151   lpcharacter = lcharacter;
152   llong = 2121L;
153   lplong = &llong;
154   lfloat = 2.1;
155   lpfloat = &lfloat;
156   ldouble = 2.718281828459045;
157   lpdouble = &ldouble;
158   lsimple.integer = 1234;
159   lsimple.unsigned_integer = 255;
160   lsimple.character = 'a';
161   lsimple.signed_character = 21;
162   lsimple.char_ptr = lcharacter;
163   lpsimple = &lsimple;
164   func = nothing;
165
166   /* Check pointers */
167   linteger = 4321;
168   lcharacter[0] = 'b';
169   llong = 1212L;
170   lfloat = 1.2;
171   ldouble = 5.498548281828172;
172   lsimple.integer = 255;
173   lsimple.unsigned_integer = 4321;
174   lsimple.character = 'b';
175   lsimple.signed_character = 0;
176
177   subroutine1 (linteger, &llong);
178 }
179
180 void
181 nothing ()
182 {
183 }
184
185 void
186 subroutine1 (int i, long *l)
187 {
188   global_simple.integer = i + 3;
189   i = 212;
190   *l = 12;
191 }
192
193 void
194 do_block_tests ()
195 {
196   int cb = 12;
197
198   {
199     int foo;
200     foo = 123;
201     {
202       int foo2;
203       foo2 = 123;
204       {
205         int foo;
206         foo = 321;
207       }
208       foo2 = 0;
209     }
210     foo = 0;
211   }
212
213   cb = 21;
214 }
215
216 void
217 do_children_tests (void)
218 {
219   weird_struct *weird;
220   struct _struct_n_pointer *psnp;
221   struct _struct_n_pointer snp0, snp1, snp2;
222   char a0[2] = {}, *a1, **a2, ***a3;
223   char b0[2] = {}, *b1, **b2, ***b3;
224   char c0[2] = {}, *c1, **c2, ***c3;
225   long z0, *z1, **z2, ***z3;
226   long y0, *y1, **y2, ***y3;
227   long x0, *x1, **x2, ***x3;
228   int *foo;
229   int bar;
230
231   /* Avoid pointing into NULL, as that is editable on some
232      systems.  */
233   int dummy;
234   int *dummy_ptr = &dummy;
235
236   struct _struct_decl struct_declarations = { 0, 0, NULL, 0, &dummy_ptr };
237   weird = &struct_declarations;
238
239   struct_declarations.integer = 123;
240   weird->char_ptr = "hello";
241   bar = 2121;
242   foo = &bar;
243   struct_declarations.int_ptr_ptr = &foo;
244   weird->long_array[0] = 1234;
245   struct_declarations.long_array[1] = 2345;
246   weird->long_array[2] = 3456;
247   struct_declarations.long_array[3] = 4567;
248   weird->long_array[4] = 5678;
249   struct_declarations.long_array[5] = 6789;
250   weird->long_array[6] = 7890;
251   struct_declarations.long_array[7] = 8901;
252   weird->long_array[8] = 9012;
253   struct_declarations.long_array[9] = 1234;
254
255   weird->func_ptr = nothing;
256
257   /* Struct/pointer/array tests */
258   a0[0] = '0';
259   a1 = a0;
260   a2 = &a1;
261   a3 = &a2;
262   b0[0] = '1';
263   b1 = b0;
264   b2 = &b1;
265   b3 = &b2;
266   c0[0] = '2';
267   c1 = c0;
268   c2 = &c1;
269   c3 = &c2;
270   z0 = 0xdead + 0;
271   z1 = &z0;
272   z2 = &z1;
273   z3 = &z2;
274   y0 = 0xdead + 1;
275   y1 = &y0;
276   y2 = &y1;
277   y3 = &y2;
278   x0 = 0xdead + 2;
279   x1 = &x0;
280   x2 = &x1;
281   x3 = &x2;
282   snp0.char_ptr = &a3;
283   snp0.long_ptr = &z3;
284   snp0.ptrs[0] = &snp0;
285   snp0.ptrs[1] = &snp1;
286   snp0.ptrs[2] = &snp2;
287   snp0.next = &snp1;
288   snp1.char_ptr = &b3;
289   snp1.long_ptr = &y3;
290   snp1.ptrs[0] = &snp0;
291   snp1.ptrs[1] = &snp1;
292   snp1.ptrs[2] = &snp2;
293   snp1.next = &snp2;
294   snp2.char_ptr = &c3;
295   snp2.long_ptr = &x3;
296   snp2.ptrs[0] = &snp0;
297   snp2.ptrs[1] = &snp1;
298   snp2.ptrs[2] = &snp2;
299   snp2.next = 0x0;
300   psnp = &snp0;
301   snp0.char_ptr = &b3;
302   snp1.char_ptr = &c3;
303   snp2.char_ptr = &a3;
304   snp0.long_ptr = &y3;
305   snp1.long_ptr = &x3;
306   snp2.long_ptr = &z3;
307   {int a = 0;}
308 }
309
310 void
311 do_special_tests (void)
312 {
313   union named_union u;
314   union {
315     int a;
316     char b;
317     long c;
318   } anonu;
319   struct _simple_struct s;
320   struct {
321     int a;
322     char b;
323     long c;
324   } anons;
325   enum foo e;
326   enum { A, B, C } anone;
327   int array[21];
328   int a;
329
330   a = 1;   
331   u.integer = a;
332   anonu.a = a;
333   s.integer = a;
334   anons.a = a;
335   e = bar;
336   anone = A;
337   incr_a(2);
338 }
339
340 void do_frozen_tests ()
341 {
342   /*: BEGIN: frozen :*/
343   struct {
344     int i;
345     struct {
346       int j;
347       int k;
348     } nested;
349   } v1 = {1, {2, 3}};
350
351   int v2 = 4;
352   /*: 
353     mi_create_varobj V1 v1 "create varobj for v1" 
354     mi_create_varobj V2 v2 "create varobj for v2"
355
356     mi_list_varobj_children "V1" {
357         {"V1.i" "i" "0" "int"}
358         {"V1.nested" "nested" "2" "struct {...}"}
359     } "list children of v1"
360
361     mi_list_varobj_children "V1.nested" {
362         {"V1.nested.j" "j" "0" "int"}
363         {"V1.nested.k" "k" "0" "int"}
364     } "list children of v1.nested"
365
366     mi_check_varobj_value V1.i 1 "check V1.i: 1"
367     mi_check_varobj_value V1.nested.j 2 "check V1.nested.j: 2"
368     mi_check_varobj_value V1.nested.k 3 "check V1.nested.k: 3"
369     mi_check_varobj_value V2 4 "check V2: 4"
370   :*/
371   v2 = 5;
372   /*: 
373     mi_varobj_update * {V2} "update varobjs: V2 changed"
374     set_frozen V2 1
375   :*/
376   v2 = 6;
377   /*: 
378     mi_varobj_update * {} "update varobjs: nothing changed"
379     mi_check_varobj_value V2 5 "check V2: 5"
380     mi_varobj_update V2 {V2} "update V2 explicitly"
381     mi_check_varobj_value V2 6 "check V2: 6"
382   :*/
383   v1.i = 7;
384   v1.nested.j = 8;
385   v1.nested.k = 9;
386   /*:
387     set_frozen V1 1
388     mi_varobj_update * {} "update varobjs: nothing changed"
389     mi_check_varobj_value V1.i 1 "check V1.i: 1"
390     mi_check_varobj_value V1.nested.j 2 "check V1.nested.j: 2"
391     mi_check_varobj_value V1.nested.k 3 "check V1.nested.k: 3"    
392     # Check that explicit update for elements of structures
393     # works.
394     # Update v1.j
395     mi_varobj_update V1.nested.j {V1.nested.j} "update V1.nested.j"
396     mi_check_varobj_value V1.i 1 "check V1.i: 1"
397     mi_check_varobj_value V1.nested.j 8 "check V1.nested.j: 8"
398     mi_check_varobj_value V1.nested.k 3 "check V1.nested.k: 3"    
399     # Update v1.nested, check that children is updated.
400     mi_varobj_update V1.nested {V1.nested.k} "update V1.nested"
401     mi_check_varobj_value V1.i 1 "check V1.i: 1"
402     mi_check_varobj_value V1.nested.j 8 "check V1.nested.j: 8"
403     mi_check_varobj_value V1.nested.k 9 "check V1.nested.k: 9"    
404     # Update v1.i
405     mi_varobj_update V1.i {V1.i} "update V1.i"
406     mi_check_varobj_value V1.i 7 "check V1.i: 7"
407   :*/
408   v1.i = 10;
409   v1.nested.j = 11;
410   v1.nested.k = 12;
411   /*:
412     # Check that unfreeze itself does not updates the values.
413     set_frozen V1 0
414     mi_check_varobj_value V1.i 7 "check V1.i: 7"
415     mi_check_varobj_value V1.nested.j 8 "check V1.nested.j: 8"
416     mi_check_varobj_value V1.nested.k 9 "check V1.nested.k: 9"    
417     mi_varobj_update V1 {V1.i V1.nested.j V1.nested.k} "update V1"
418     mi_check_varobj_value V1.i 10 "check V1.i: 10"
419     mi_check_varobj_value V1.nested.j 11 "check V1.nested.j: 11"
420     mi_check_varobj_value V1.nested.k 12 "check V1.nested.k: 12"    
421   :*/    
422   
423   /*: END: frozen :*/
424 }
425
426 void do_at_tests_callee ()
427 {
428   /* This is a test of wrong DWARF data being assigned to expression.
429      The DWARF location expression is bound to symbol when expression
430      is parsed.  So, if we create floating varobj in one function,
431      and then try to reevaluate it in other frame without reparsing
432      the expression, we will access local variables using DWARF
433      location expression from the original frame, and are likely
434      to grab wrong symbol.  To reliably reproduce this bug, we need 
435      to wrap our variable with a bunch of buffers, so that those
436      buffers are accessed instead of the real one.  */
437   int buffer1 = 10;
438   int buffer2 = 11;
439   int buffer3 = 12;
440   int i = 7;
441   int buffer4 = 13;
442   int buffer5 = 14;
443   int buffer6 = 15;
444   i++;  /* breakpoint inside callee */
445   i++;
446 }
447
448 void do_at_tests ()
449 {
450   int x;
451   /*: BEGIN: floating :*/
452   int i = 10;
453   int y = 15;
454   /*:
455     mi_create_floating_varobj F i "create floating varobj"
456     :*/
457   i++;
458   /*:
459     mi_varobj_update F {F} "update F (1)"
460     mi_check_varobj_value F 11 "check F (1)"
461     :*/
462   i++;
463   {
464     double i = 15;
465     /*:
466       mi_varobj_update_with_type_change F "double" "0" "update F (2)"
467       mi_check_varobj_value F 15 "check F (2)"
468       :*/
469     i += 2.0;
470   }
471   {
472     float i = 19;
473     /*:
474       mi_gdb_test "-var-update --all-values F" {.*value="19".*} "update F (--all-values)"
475       :*/
476     i += 2.0;
477   }
478   i++;
479   /*:
480     mi_varobj_update_with_type_change F "int" "0" "update F (3)"
481     mi_check_varobj_value F 13 "check F (3)"
482     :*/
483   i++;
484   do_at_tests_callee ();
485   i++;
486   /*: END: floating :*/
487 }
488
489 /* Some header appear to define uint already, so apply some
490    uglification.  Note that without uglification, the compile
491    does not fail, rather, we don't test what we want because
492    something else calls check_typedef on 'uint' already.  */
493 typedef unsigned int uint_for_mi_testing;
494
495 struct Data {
496   int alloc;
497   uint_for_mi_testing sharable : 4;
498 };
499
500 /* Accessing a value of a bitfield whose type is a typed used to
501    result in division by zero.  See:
502
503          http://sourceware.org/bugzilla/show_bug.cgi?id=10884
504
505    This tests for this bug.  */
506
507 void do_bitfield_tests ()
508 {
509   /*: BEGIN: bitfield :*/
510   struct Data d = {0, 3};
511   /*:
512     mi_create_varobj V d "create varobj for Data"
513     mi_list_varobj_children "V" {
514         {"V.alloc" "alloc" "0" "int"}
515         {"V.sharable" "sharable" "0" "uint_for_mi_testing"}
516     } "list children of Data"
517     mi_check_varobj_value V.sharable 3 "access bitfield"
518     :*/
519   return;
520   /*: END: bitfield :*/  
521 }
522
523 void
524 do_anonymous_type_tests (void)
525 {
526   struct anonymous *anon;
527   struct anonymous **ptr;
528   struct
529   {
530     int x;
531     struct
532     {
533       int a;
534     };
535     struct
536     {
537       int b;
538     };
539   } v = {1, {2}, {3}};
540
541   anon = malloc (sizeof (struct anonymous));
542   anon->a = 1;
543   anon->b = 2;
544   anon->c = (char *) 3;
545   anon->d = 4;
546   anon->g = '5';
547   anon->h = (const char **) 6;
548   anon->simple = (simpleton ***) 7;
549
550   ptr = &anon;
551   free (anon);
552   return; /* anonymous type tests breakpoint */
553 }
554
555 int
556 main (int argc, char *argv [])
557 {
558   do_locals_tests ();
559   do_block_tests ();
560   do_children_tests ();
561   do_special_tests ();
562   do_frozen_tests ();
563   do_at_tests ();
564   do_bitfield_tests ();
565   do_anonymous_type_tests ();
566   exit (0);
567 }
568
569