]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - libctf/testsuite/libctf-lookup/add-to-opened.c
libctf: support addition of types to dicts read via ctf_open()
[thirdparty/binutils-gdb.git] / libctf / testsuite / libctf-lookup / add-to-opened.c
CommitLineData
8a60c930
NA
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
8int
9main (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, "Writeout unexpectedly succeeded: %s\n", ctf_errmsg (ctf_errno (fp)));
123
124 if (ctf_errno (fp) != ECTF_RDONLY)
125 fprintf (stderr, "unexpected error %s trying to write out previously serialized dict\n", ctf_errmsg (ctf_errno (fp)));
126
127 /* Finally, make sure we can add new types, and look them up again. */
128
129 if ((type = ctf_lookup_by_name (fp, "struct a_struct")) == CTF_ERR)
130 fprintf (stderr, "Lookup of struct a_struct failed: %s\n", ctf_errmsg (ctf_errno (fp)));
131
132 if ((ptrtype = ctf_add_pointer (fp, CTF_ADD_ROOT, type)) == CTF_ERR)
133 fprintf (stderr, "Cannot add pointer to ctf_opened dict: %s\n", ctf_errmsg (ctf_errno (fp)));
134
135 if (ctf_type_reference (fp, ptrtype) == CTF_ERR)
136 fprintf (stderr, "Lookup of pointer preserved across writeout failed: %s\n", ctf_errmsg (ctf_errno (fp)));
137
138 if (ctf_type_reference (fp, ptrtype) != type)
139 fprintf (stderr, "Look up of newly-added type in serialized dict yields ID %lx, expected %lx\n", ctf_type_reference (fp, ptrtype), type);
140
141 printf ("All done.\n");
142 return 0;
143
144 open_err:
145 fprintf (stderr, "%s: cannot open: %s\n", argv[0], ctf_errmsg (err));
146 return 1;
147}