Update copyright year range in header of all files managed by GDB
[binutils-gdb.git] / gdb / testsuite / gdb.mi / var-cmd.c
blob9dbb43c60a27fb70a4e713a8a4470d27c5ad34b7
1 /* Copyright 1999-2023 Free Software Foundation, Inc.
3 This file is part of GDB.
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.
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.
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/>. */
18 #include <stdlib.h>
19 #include <string.h>
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];
30 typedef struct _simple_struct simpleton;
32 simpleton global_simple;
34 enum foo {
35 bar = 1,
36 baz
39 typedef enum foo efoo;
41 union named_union
43 int integer;
44 char *char_ptr;
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];
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;
65 struct {
66 union {
67 struct {
68 int d;
69 char e[10];
70 int *(*func) (void);
71 efoo foo;
72 } u1s1;
74 long f;
75 struct {
76 char array_ptr[2];
77 int (*func) (int, char *);
78 } u1s2;
79 } u2;
81 int g;
82 char h;
83 long i[10];
84 } s2;
85 } weird_struct;
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;
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;
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);
120 void incr_a (char a)
122 int b;
123 b = a;
126 int array[] = {1,2,3};
127 int array2[] = {4,5,6};
128 int *array_ptr = array;
130 void
131 do_locals_tests ()
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;
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;
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;
177 subroutine1 (linteger, &llong);
180 void
181 nothing ()
185 struct _struct_decl
186 nothing1 (int a, char *b, long c)
188 struct _struct_decl foo;
190 return foo;
193 struct _struct_decl *
194 nothing2 (int a, char *b, long c)
196 return (struct _struct_decl *) 0;
199 void
200 subroutine1 (int i, long *l)
202 global_simple.integer = i + 3;
203 i = 212;
204 *l = 12;
207 void
208 do_block_tests ()
210 int cb = 0;
211 cb = 12;
214 int foo;
215 foo = 123;
217 int foo2;
218 foo2 = 123;
220 int foo;
221 foo = 321;
223 foo2 = 0;
225 foo = 0;
228 cb = 21;
231 void
232 do_children_tests (void)
234 weird_struct *weird;
235 struct _struct_n_pointer *psnp;
236 struct _struct_n_pointer snp0, snp1, snp2;
237 char a0[2] = {}, *a1, **a2, ***a3;
238 char b0[2] = {}, *b1, **b2, ***b3;
239 char c0[2] = {}, *c1, **c2, ***c3;
240 long z0, *z1, **z2, ***z3;
241 long y0, *y1, **y2, ***y3;
242 long x0, *x1, **x2, ***x3;
243 int *foo;
244 int bar;
246 /* Avoid pointing into NULL, as that is editable on some
247 systems. */
248 int dummy;
249 int *dummy_ptr = &dummy;
251 struct _struct_decl struct_declarations = { 0, 0, NULL, 0, &dummy_ptr };
252 weird = &struct_declarations;
254 struct_declarations.integer = 123;
255 weird->char_ptr = "hello";
256 bar = 2121;
257 foo = &bar;
258 struct_declarations.int_ptr_ptr = &foo;
259 weird->long_array[0] = 1234;
260 struct_declarations.long_array[1] = 2345;
261 weird->long_array[2] = 3456;
262 struct_declarations.long_array[3] = 4567;
263 weird->long_array[4] = 5678;
264 struct_declarations.long_array[5] = 6789;
265 weird->long_array[6] = 7890;
266 struct_declarations.long_array[7] = 8901;
267 weird->long_array[8] = 9012;
268 struct_declarations.long_array[9] = 1234;
270 weird->func_ptr = nothing;
271 weird->func_ptr_struct = nothing1;
272 weird->func_ptr_ptr = nothing2;
274 /* Struct/pointer/array tests */
275 a0[0] = '0';
276 a1 = a0;
277 a2 = &a1;
278 a3 = &a2;
279 b0[0] = '1';
280 b1 = b0;
281 b2 = &b1;
282 b3 = &b2;
283 c0[0] = '2';
284 c1 = c0;
285 c2 = &c1;
286 c3 = &c2;
287 z0 = 0xdead + 0;
288 z1 = &z0;
289 z2 = &z1;
290 z3 = &z2;
291 y0 = 0xdead + 1;
292 y1 = &y0;
293 y2 = &y1;
294 y3 = &y2;
295 x0 = 0xdead + 2;
296 x1 = &x0;
297 x2 = &x1;
298 x3 = &x2;
299 snp0.char_ptr = &a3;
300 snp0.long_ptr = &z3;
301 snp0.ptrs[0] = &snp0;
302 snp0.ptrs[1] = &snp1;
303 snp0.ptrs[2] = &snp2;
304 snp0.next = &snp1;
305 snp1.char_ptr = &b3;
306 snp1.long_ptr = &y3;
307 snp1.ptrs[0] = &snp0;
308 snp1.ptrs[1] = &snp1;
309 snp1.ptrs[2] = &snp2;
310 snp1.next = &snp2;
311 snp2.char_ptr = &c3;
312 snp2.long_ptr = &x3;
313 snp2.ptrs[0] = &snp0;
314 snp2.ptrs[1] = &snp1;
315 snp2.ptrs[2] = &snp2;
316 snp2.next = 0x0;
317 psnp = &snp0;
318 snp0.char_ptr = &b3;
319 snp1.char_ptr = &c3;
320 snp2.char_ptr = &a3;
321 snp0.long_ptr = &y3;
322 snp1.long_ptr = &x3;
323 snp2.long_ptr = &z3;
324 {int a = 0;}
327 void
328 do_special_tests (void)
330 union named_union u;
331 union {
332 int a;
333 char b;
334 long c;
335 } anonu;
336 struct _simple_struct s;
337 struct {
338 int a;
339 char b;
340 long c;
341 } anons;
342 enum foo e;
343 enum { A, B, C } anone;
344 int array[21];
345 int a;
347 a = 1;
348 u.integer = a;
349 anonu.a = a;
350 s.integer = a;
351 anons.a = a;
352 e = bar;
353 anone = A;
354 incr_a(2);
357 void do_frozen_tests ()
359 /*: BEGIN: frozen :*/
360 struct {
361 int i;
362 struct {
363 int j;
364 int k;
365 } nested;
366 } v1 = {1, {2, 3}};
368 int v2 = 4;
369 /*:
370 with_test_prefix "create varobj V1 and V2" {
371 mi_create_varobj V1 v1 "create varobj for v1"
372 mi_create_varobj V2 v2 "create varobj for v2"
374 mi_list_varobj_children "V1" {
375 {"V1.i" "i" "0" "int"}
376 {"V1.nested" "nested" "2" "struct {...}"}
377 } "list children of v1"
379 mi_list_varobj_children "V1.nested" {
380 {"V1.nested.j" "j" "0" "int"}
381 {"V1.nested.k" "k" "0" "int"}
382 } "list children of v1.nested"
384 mi_check_varobj_value V1.i 1 "check V1.i: 1"
385 mi_check_varobj_value V1.nested.j 2 "check V1.nested.j: 2"
386 mi_check_varobj_value V1.nested.k 3 "check V1.nested.k: 3"
387 mi_check_varobj_value V2 4 "check V2: 4"
390 v2 = 5;
391 /*:
392 mi_varobj_update * {V2} "update varobjs: V2 changed"
393 set_frozen V2 1
395 v2 = 6;
396 /*:
397 mi_varobj_update * {} "update varobjs: nothing changed"
398 mi_check_varobj_value V2 5 "check V2: 5"
399 mi_varobj_update V2 {V2} "update V2 explicitly"
400 mi_check_varobj_value V2 6 "check V2: 6"
402 v1.i = 7;
403 v1.nested.j = 8;
404 v1.nested.k = 9;
406 with_test_prefix "frozen V1" {
407 set_frozen V1 1
408 mi_varobj_update * {} "update varobjs: nothing changed"
409 mi_check_varobj_value V1.i 1 "check V1.i: 1"
410 mi_check_varobj_value V1.nested.j 2 "check V1.nested.j: 2"
411 mi_check_varobj_value V1.nested.k 3 "check V1.nested.k: 3"
413 # Check that explicit update for elements of structures
414 # works.
415 with_test_prefix "update v1.j" {
416 # Update v1.j
417 mi_varobj_update V1.nested.j {V1.nested.j} "update V1.nested.j"
418 mi_check_varobj_value V1.i 1 "check V1.i: 1"
419 mi_check_varobj_value V1.nested.j 8 "check V1.nested.j: 8"
420 mi_check_varobj_value V1.nested.k 3 "check V1.nested.k: 3"
422 with_test_prefix "update v1.nested" {
423 # Update v1.nested, check that children is updated.
424 mi_varobj_update V1.nested {V1.nested.k} "update V1.nested"
425 mi_check_varobj_value V1.i 1 "check V1.i: 1"
426 mi_check_varobj_value V1.nested.j 8 "check V1.nested.j: 8"
427 mi_check_varobj_value V1.nested.k 9 "check V1.nested.k: 9"
429 with_test_prefix "update v1.i" {
430 # Update v1.i
431 mi_varobj_update V1.i {V1.i} "update V1.i"
432 mi_check_varobj_value V1.i 7 "check V1.i: 7"
435 v1.i = 10;
436 v1.nested.j = 11;
437 v1.nested.k = 12;
439 # Check that unfreeze itself does not updates the values.
440 with_test_prefix "unfrozen V1" {
441 set_frozen V1 0
442 mi_check_varobj_value V1.i 7 "check V1.i: 7"
443 mi_check_varobj_value V1.nested.j 8 "check V1.nested.j: 8"
444 mi_check_varobj_value V1.nested.k 9 "check V1.nested.k: 9"
445 mi_varobj_update V1 {V1.i V1.nested.j V1.nested.k} "update V1"
446 mi_check_varobj_value V1.i 10 "check V1.i: 10"
447 mi_check_varobj_value V1.nested.j 11 "check V1.nested.j: 11"
448 mi_check_varobj_value V1.nested.k 12 "check V1.nested.k: 12"
450 :*/
452 /*: END: frozen :*/
455 void do_at_tests_callee ()
457 /* This is a test of wrong DWARF data being assigned to expression.
458 The DWARF location expression is bound to symbol when expression
459 is parsed. So, if we create floating varobj in one function,
460 and then try to reevaluate it in other frame without reparsing
461 the expression, we will access local variables using DWARF
462 location expression from the original frame, and are likely
463 to grab wrong symbol. To reliably reproduce this bug, we need
464 to wrap our variable with a bunch of buffers, so that those
465 buffers are accessed instead of the real one. */
466 int buffer1 = 10;
467 int buffer2 = 11;
468 int buffer3 = 12;
469 int i = 7;
470 int buffer4 = 13;
471 int buffer5 = 14;
472 int buffer6 = 15;
473 i++; /* breakpoint inside callee */
474 i++;
477 void do_at_tests ()
479 int x;
480 /*: BEGIN: floating :*/
481 int i = 10;
482 int y = 15;
484 mi_create_floating_varobj F i "create floating varobj"
486 i++;
488 mi_varobj_update F {F} "update F (1)"
489 mi_check_varobj_value F 11 "check F (1)"
491 i++;
493 double i = 15;
495 mi_varobj_update_with_type_change F "double" "0" "update F (2)"
496 mi_check_varobj_value F 15 "check F (2)"
498 i += 2.0;
501 float i = 19;
503 mi_gdb_test "-var-update --all-values F" {.*value="19".*} "update F (--all-values)"
505 i += 2.0;
507 i++;
509 mi_varobj_update_with_type_change F "int" "0" "update F (3)"
510 mi_check_varobj_value F 13 "check F (3)"
512 i++;
513 do_at_tests_callee ();
514 i++;
515 /*: END: floating :*/
518 /* Some header appear to define uint already, so apply some
519 uglification. Note that without uglification, the compile
520 does not fail, rather, we don't test what we want because
521 something else calls check_typedef on 'uint' already. */
522 typedef unsigned int uint_for_mi_testing;
524 struct Data {
525 int alloc;
526 uint_for_mi_testing sharable : 4;
529 /* Accessing a value of a bitfield whose type is a typed used to
530 result in division by zero. See:
532 http://sourceware.org/bugzilla/show_bug.cgi?id=10884
534 This tests for this bug. */
536 void do_bitfield_tests ()
538 /*: BEGIN: bitfield :*/
539 struct Data d = {0, 3};
541 mi_create_varobj V d "create varobj for Data"
542 mi_list_varobj_children "V" {
543 {"V.alloc" "alloc" "0" "int"}
544 {"V.sharable" "sharable" "0" "uint_for_mi_testing"}
545 } "list children of Data"
546 mi_check_varobj_value V.sharable 3 "access bitfield"
548 return;
549 /*: END: bitfield :*/
552 void
553 do_anonymous_type_tests (void)
555 struct anonymous *anon;
556 struct anonymous **ptr;
557 struct
559 int x;
560 struct
562 int a;
564 struct
566 int b;
568 } v = {1, {2}, {3}};
570 anon = (struct anonymous *) malloc (sizeof (struct anonymous));
571 anon->a = 1;
572 anon->b = 2;
573 anon->c = (char *) 3;
574 anon->d = 4;
575 anon->g = '5';
576 anon->h = (const char **) 6;
577 anon->simple = (simpleton ***) 7;
579 ptr = &anon;
580 free (anon);
581 return; /* anonymous type tests breakpoint */
584 void
585 do_nested_struct_union_tests (void)
587 struct s_a
589 int a;
591 struct s_b
593 int b;
595 union u_ab
597 struct s_a a;
598 struct s_b b;
600 struct ss
602 struct s_a a1;
603 struct s_b b1;
604 union u_ab u1;
606 /* Anonymous union. */
607 union
609 struct s_a a2;
610 struct s_b b2;
613 union
615 struct s_a a3;
616 struct s_b b3;
617 } u2;
620 typedef struct
622 int a;
623 } td_s_a;
625 typedef struct
627 int b;
628 } td_s_b;
630 typedef union
632 td_s_a a;
633 td_s_b b;
634 } td_u_ab;
636 struct ss var;
637 struct
639 td_u_ab ab;
640 } var2;
642 struct ss *ss_ptr;
644 memset (&var, 0, sizeof (var));
645 memset (&var2, 0, sizeof (var2));
646 ss_ptr = &var;
648 return; /* nested struct union tests breakpoint */
652 main (int argc, char *argv [])
654 do_locals_tests ();
655 do_block_tests ();
656 do_children_tests ();
657 do_special_tests ();
658 do_frozen_tests ();
659 do_at_tests ();
660 do_bitfield_tests ();
661 do_anonymous_type_tests ();
662 do_nested_struct_union_tests ();
663 exit (0);