xref: /netbsd-src/external/gpl3/gdb/dist/libctf/testsuite/libctf-lookup/add-to-opened.c (revision c9055873d0546e63388f027d3d7f85381cde0545)
1*c9055873Schristos /* Make sure you can add to ctf_open()ed CTF dicts, and that you
2*c9055873Schristos    cannot make changes to existing types.  */
3*c9055873Schristos 
4*c9055873Schristos #include <ctf-api.h>
5*c9055873Schristos #include <stdio.h>
6*c9055873Schristos #include <stdlib.h>
7*c9055873Schristos 
8*c9055873Schristos int
9*c9055873Schristos main (int argc, char *argv[])
10*c9055873Schristos {
11*c9055873Schristos   ctf_dict_t *fp;
12*c9055873Schristos   ctf_archive_t *ctf;
13*c9055873Schristos   ctf_id_t type, ptrtype;
14*c9055873Schristos   ctf_arinfo_t ar = {0, 0, 0};
15*c9055873Schristos   ctf_encoding_t en = { CTF_INT_SIGNED, 0, sizeof (int) };
16*c9055873Schristos   unsigned char *ctf_written;
17*c9055873Schristos   size_t size;
18*c9055873Schristos   int err;
19*c9055873Schristos 
20*c9055873Schristos   if (argc != 2)
21*c9055873Schristos     {
22*c9055873Schristos       fprintf (stderr, "Syntax: %s PROGRAM\n", argv[0]);
23*c9055873Schristos       exit(1);
24*c9055873Schristos     }
25*c9055873Schristos 
26*c9055873Schristos   if ((ctf = ctf_open (argv[1], NULL, &err)) == NULL)
27*c9055873Schristos     goto open_err;
28*c9055873Schristos   if ((fp = ctf_dict_open (ctf, NULL, &err)) == NULL)
29*c9055873Schristos     goto open_err;
30*c9055873Schristos 
31*c9055873Schristos   /* Check that various modifications to already-written types
32*c9055873Schristos      are prohibited.  */
33*c9055873Schristos 
34*c9055873Schristos   if (ctf_add_integer (fp, CTF_ADD_ROOT, "int", &en) == 0)
35*c9055873Schristos     fprintf (stderr, "allowed to add integer existing in readonly portion\n");
36*c9055873Schristos 
37*c9055873Schristos   if (ctf_errno (fp) != ECTF_RDONLY)
38*c9055873Schristos     fprintf (stderr, "unexpected error %s attempting to add integer in readonly portion\n", ctf_errmsg (ctf_errno (fp)));
39*c9055873Schristos 
40*c9055873Schristos   if (ctf_add_typedef (fp, CTF_ADD_ROOT, "a_typedef", 0) == 0)
41*c9055873Schristos     fprintf (stderr, "allowed to add typedef existing in readonly portion\n");
42*c9055873Schristos 
43*c9055873Schristos   if (ctf_errno (fp) != ECTF_RDONLY)
44*c9055873Schristos     fprintf (stderr, "unexpected error %s attempting to add typedef in readonly portion\n", ctf_errmsg (ctf_errno (fp)));
45*c9055873Schristos 
46*c9055873Schristos   if (ctf_add_struct (fp, CTF_ADD_ROOT, "a_struct") == 0)
47*c9055873Schristos     fprintf (stderr, "allowed to add struct existing in readonly portion\n");
48*c9055873Schristos 
49*c9055873Schristos   if (ctf_errno (fp) != ECTF_RDONLY)
50*c9055873Schristos     fprintf (stderr, "unexpected error %s attempting to add struct in readonly portion\n", ctf_errmsg (ctf_errno (fp)));
51*c9055873Schristos 
52*c9055873Schristos   if (ctf_add_union (fp, CTF_ADD_ROOT, "a_union") == 0)
53*c9055873Schristos     fprintf (stderr, "allowed to add union existing in readonly portion\n");
54*c9055873Schristos 
55*c9055873Schristos   if (ctf_errno (fp) != ECTF_RDONLY)
56*c9055873Schristos     fprintf (stderr, "unexpected error %s attempting to add union in readonly portion\n", ctf_errmsg (ctf_errno (fp)));
57*c9055873Schristos 
58*c9055873Schristos   if (ctf_add_enum (fp, CTF_ADD_ROOT, "an_enum") == 0)
59*c9055873Schristos     fprintf (stderr, "allowed to add enum existing in readonly portion\n");
60*c9055873Schristos 
61*c9055873Schristos   if (ctf_errno (fp) != ECTF_RDONLY)
62*c9055873Schristos     fprintf (stderr, "unexpected error %s attempting to add enum in readonly portion\n", ctf_errmsg (ctf_errno (fp)));
63*c9055873Schristos 
64*c9055873Schristos   if (ctf_add_struct (fp, CTF_ADD_ROOT, "struct_forward") == 0)
65*c9055873Schristos     fprintf (stderr, "allowed to promote struct forward existing in readonly portion\n");
66*c9055873Schristos 
67*c9055873Schristos   if (ctf_errno (fp) != ECTF_RDONLY)
68*c9055873Schristos     fprintf (stderr, "unexpected error %s attempting to promote struct forward in readonly portion\n", ctf_errmsg (ctf_errno (fp)));
69*c9055873Schristos 
70*c9055873Schristos   if (ctf_add_union (fp, CTF_ADD_ROOT, "union_forward") == 0)
71*c9055873Schristos     fprintf (stderr, "allowed to promote union forward existing in readonly portion\n");
72*c9055873Schristos 
73*c9055873Schristos   if (ctf_errno (fp) != ECTF_RDONLY)
74*c9055873Schristos     fprintf (stderr, "unexpected error %s attempting to promote union forward in readonly portion\n", ctf_errmsg (ctf_errno (fp)));
75*c9055873Schristos 
76*c9055873Schristos   if (ctf_add_enum (fp, CTF_ADD_ROOT, "enum_forward") == 0)
77*c9055873Schristos     fprintf (stderr, "allowed to promote enum forward existing in readonly portion\n");
78*c9055873Schristos 
79*c9055873Schristos   if (ctf_errno (fp) != ECTF_RDONLY)
80*c9055873Schristos     fprintf (stderr, "unexpected error %s attempting to promote enum forward in readonly portion\n", ctf_errmsg (ctf_errno (fp)));
81*c9055873Schristos 
82*c9055873Schristos   if ((type = ctf_lookup_by_name (fp, "struct a_struct")) == CTF_ERR)
83*c9055873Schristos     fprintf (stderr, "Lookup of struct a_struct failed: %s\n", ctf_errmsg (ctf_errno (fp)));
84*c9055873Schristos 
85*c9055873Schristos   if (ctf_add_member (fp, type, "wombat", 0) == 0)
86*c9055873Schristos     fprintf (stderr, "allowed to add member to struct existing in readonly portion\n");
87*c9055873Schristos 
88*c9055873Schristos   if (ctf_errno (fp) != ECTF_RDONLY)
89*c9055873Schristos     fprintf (stderr, "unexpected error %s attempting to add member to struct in readonly portion\n", ctf_errmsg (ctf_errno (fp)));
90*c9055873Schristos 
91*c9055873Schristos   if ((type = ctf_lookup_by_name (fp, "union a_union")) == CTF_ERR)
92*c9055873Schristos     fprintf (stderr, "Lookup of union a_union failed: %s\n", ctf_errmsg (ctf_errno (fp)));
93*c9055873Schristos 
94*c9055873Schristos   if (ctf_add_member (fp, type, "wombat", 0) == 0)
95*c9055873Schristos     fprintf (stderr, "allowed to add member to union existing in readonly portion\n");
96*c9055873Schristos 
97*c9055873Schristos   if (ctf_errno (fp) != ECTF_RDONLY)
98*c9055873Schristos     fprintf (stderr, "unexpected error %s attempting to add member to union in readonly portion\n", ctf_errmsg (ctf_errno (fp)));
99*c9055873Schristos 
100*c9055873Schristos   if ((type = ctf_lookup_by_name (fp, "enum an_enum")) == CTF_ERR)
101*c9055873Schristos     fprintf (stderr, "Lookup of enum an_enum failed: %s\n", ctf_errmsg (ctf_errno (fp)));
102*c9055873Schristos 
103*c9055873Schristos   if (ctf_add_enumerator (fp, type, "wombat", 0) == 0)
104*c9055873Schristos     fprintf (stderr, "allowed to add enumerator to enum existing in readonly portion\n");
105*c9055873Schristos 
106*c9055873Schristos   if (ctf_errno (fp) != ECTF_RDONLY)
107*c9055873Schristos     fprintf (stderr, "unexpected error %s attempting to add enumerator to enum in readonly portion\n", ctf_errmsg (ctf_errno (fp)));
108*c9055873Schristos 
109*c9055873Schristos   if ((type = ctf_lookup_by_name (fp, "an_array")) == CTF_ERR)
110*c9055873Schristos     fprintf (stderr, "Lookup of an_array failed: %s\n", ctf_errmsg (ctf_errno (fp)));
111*c9055873Schristos 
112*c9055873Schristos   if ((type = ctf_type_reference (fp, type)) == CTF_ERR)
113*c9055873Schristos     fprintf (stderr, "Lookup of type reffed by an_array failed: %s\n", ctf_errmsg (ctf_errno (fp)));
114*c9055873Schristos 
115*c9055873Schristos   if (ctf_set_array (fp, type, &ar) == 0)
116*c9055873Schristos     fprintf (stderr, "allowed to set array in readonly portion\n");
117*c9055873Schristos 
118*c9055873Schristos   if (ctf_errno (fp) != ECTF_RDONLY)
119*c9055873Schristos     fprintf (stderr, "unexpected error %s attempting to set array in readonly portion\n", ctf_errmsg (ctf_errno (fp)));
120*c9055873Schristos 
121*c9055873Schristos   if ((ctf_written = ctf_write_mem (fp, &size, 4096)) == NULL)
122*c9055873Schristos     fprintf (stderr, "Re-writeout unexpectedly failed: %s\n", ctf_errmsg (ctf_errno (fp)));
123*c9055873Schristos   free (ctf_written);
124*c9055873Schristos 
125*c9055873Schristos   /* Finally, make sure we can add new types, and look them up again.  */
126*c9055873Schristos 
127*c9055873Schristos   if ((type = ctf_lookup_by_name (fp, "struct a_struct")) == CTF_ERR)
128*c9055873Schristos     fprintf (stderr, "Lookup of struct a_struct failed: %s\n", ctf_errmsg (ctf_errno (fp)));
129*c9055873Schristos 
130*c9055873Schristos   if ((ptrtype = ctf_add_pointer (fp, CTF_ADD_ROOT, type)) == CTF_ERR)
131*c9055873Schristos     fprintf (stderr, "Cannot add pointer to ctf_opened dict: %s\n", ctf_errmsg (ctf_errno (fp)));
132*c9055873Schristos 
133*c9055873Schristos   if (ctf_type_reference (fp, ptrtype) == CTF_ERR)
134*c9055873Schristos     fprintf (stderr, "Lookup of pointer preserved across writeout failed: %s\n", ctf_errmsg (ctf_errno (fp)));
135*c9055873Schristos 
136*c9055873Schristos   if (ctf_type_reference (fp, ptrtype) != type)
137*c9055873Schristos     fprintf (stderr, "Look up of newly-added type in serialized dict yields ID %lx, expected %lx\n", ctf_type_reference (fp, ptrtype), type);
138*c9055873Schristos 
139*c9055873Schristos   ctf_dict_close (fp);
140*c9055873Schristos   ctf_close (ctf);
141*c9055873Schristos 
142*c9055873Schristos   printf ("All done.\n");
143*c9055873Schristos   return 0;
144*c9055873Schristos 
145*c9055873Schristos  open_err:
146*c9055873Schristos   fprintf (stderr, "%s: cannot open: %s\n", argv[0], ctf_errmsg (err));
147*c9055873Schristos   return 1;
148*c9055873Schristos }
149