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