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