xref: /netbsd-src/external/gpl3/gdb/dist/libctf/testsuite/libctf-regression/zrewrite.c (revision c9055873d0546e63388f027d3d7f85381cde0545)
1*c9055873Schristos /* Make sure that you can modify then ctf_compress_write() a dict
2*c9055873Schristos    and it changes after modification.  */
3*c9055873Schristos 
4*c9055873Schristos #include <ctf-api.h>
5*c9055873Schristos #include <errno.h>
6*c9055873Schristos #include <fcntl.h>
7*c9055873Schristos #include <stdio.h>
8*c9055873Schristos #include <stdlib.h>
9*c9055873Schristos #include <string.h>
10*c9055873Schristos #include <unistd.h>
11*c9055873Schristos 
12*c9055873Schristos char *read_file(const char *path, size_t *len)
13*c9055873Schristos {
14*c9055873Schristos   char *in = NULL;
15*c9055873Schristos   char buf[4096];
16*c9055873Schristos   int foo;
17*c9055873Schristos   size_t ret;
18*c9055873Schristos 
19*c9055873Schristos   if ((foo = open (path, O_RDONLY)) < 0)
20*c9055873Schristos     {
21*c9055873Schristos       fprintf (stderr, "error opening %s: %s\n", path, strerror(errno));
22*c9055873Schristos       exit (1);
23*c9055873Schristos     }
24*c9055873Schristos 
25*c9055873Schristos   *len = 0;
26*c9055873Schristos   while ((ret = read (foo, buf, 4096)) > 0)
27*c9055873Schristos     {
28*c9055873Schristos       if ((in = realloc (in, *len + ret)) == NULL)
29*c9055873Schristos 	{
30*c9055873Schristos 	  fprintf (stderr, "Out of memory\n");
31*c9055873Schristos 	  exit (1);
32*c9055873Schristos 	}
33*c9055873Schristos 
34*c9055873Schristos       memcpy (&in[*len], buf, ret);
35*c9055873Schristos       *len += ret;
36*c9055873Schristos     }
37*c9055873Schristos 
38*c9055873Schristos   if (ret < 0)
39*c9055873Schristos     {
40*c9055873Schristos       fprintf (stderr, "error reading %s: %s\n", path, strerror(errno));
41*c9055873Schristos       exit (1);
42*c9055873Schristos     }
43*c9055873Schristos   close (foo);
44*c9055873Schristos   return in;
45*c9055873Schristos }
46*c9055873Schristos 
47*c9055873Schristos int
48*c9055873Schristos main (int argc, char *argv[])
49*c9055873Schristos {
50*c9055873Schristos   ctf_dict_t *fp, *fp_b;
51*c9055873Schristos   ctf_archive_t *ctf, *ctf_b;
52*c9055873Schristos   int foo;
53*c9055873Schristos   char *a, *b;
54*c9055873Schristos   size_t a_len, b_len;
55*c9055873Schristos   ctf_id_t type, ptrtype;
56*c9055873Schristos   int err;
57*c9055873Schristos 
58*c9055873Schristos   if (argc != 2)
59*c9055873Schristos     {
60*c9055873Schristos       fprintf (stderr, "Syntax: %s PROGRAM\n", argv[0]);
61*c9055873Schristos       exit(1);
62*c9055873Schristos     }
63*c9055873Schristos 
64*c9055873Schristos   if ((ctf = ctf_open (argv[1], NULL, &err)) == NULL)
65*c9055873Schristos     goto open_err;
66*c9055873Schristos   if ((fp = ctf_dict_open (ctf, NULL, &err)) == NULL)
67*c9055873Schristos     goto open_err;
68*c9055873Schristos 
69*c9055873Schristos   if ((foo = open ("tmpdir/one", O_CREAT | O_TRUNC | O_WRONLY, 0666)) < 0)
70*c9055873Schristos     goto write_stderr;
71*c9055873Schristos   if (ctf_compress_write (fp, foo) < 0)
72*c9055873Schristos     goto write_err;
73*c9055873Schristos   close (foo);
74*c9055873Schristos 
75*c9055873Schristos   if ((foo = open ("tmpdir/two", O_CREAT | O_TRUNC | O_WRONLY, 0666)) < 0)
76*c9055873Schristos     goto write_stderr;
77*c9055873Schristos   if (ctf_compress_write (fp, foo) < 0)
78*c9055873Schristos     goto write_err;
79*c9055873Schristos   close (foo);
80*c9055873Schristos 
81*c9055873Schristos   a = read_file ("tmpdir/one", &a_len);
82*c9055873Schristos   b = read_file ("tmpdir/two", &b_len);
83*c9055873Schristos 
84*c9055873Schristos   if (a_len != b_len || memcmp (a, b, a_len) != 0)
85*c9055873Schristos     {
86*c9055873Schristos       fprintf (stderr, "consecutive compress_writes are different: lengths %zu and %zu\n", a_len, b_len);
87*c9055873Schristos       return 1;
88*c9055873Schristos     }
89*c9055873Schristos 
90*c9055873Schristos   free (b);
91*c9055873Schristos 
92*c9055873Schristos   /* Add some new types to the dict and write it out, then read it back in and
93*c9055873Schristos      make sure they're still there, and that at least some of the
94*c9055873Schristos      originally-present data objects are still there too.  */
95*c9055873Schristos 
96*c9055873Schristos   if ((type = ctf_lookup_by_name (fp, "struct a_struct")) == CTF_ERR)
97*c9055873Schristos     fprintf (stderr, "Lookup of struct a_struct failed: %s\n", ctf_errmsg (ctf_errno (fp)));
98*c9055873Schristos 
99*c9055873Schristos   if ((ptrtype = ctf_add_pointer (fp, CTF_ADD_ROOT, type)) == CTF_ERR)
100*c9055873Schristos     fprintf (stderr, "Cannot add pointer to ctf_opened dict: %s\n", ctf_errmsg (ctf_errno (fp)));
101*c9055873Schristos 
102*c9055873Schristos   unlink ("tmpdir/two");
103*c9055873Schristos 
104*c9055873Schristos   if ((foo = open ("tmpdir/two", O_CREAT | O_TRUNC | O_WRONLY, 0666)) < 0)
105*c9055873Schristos     goto write_stderr;
106*c9055873Schristos   if (ctf_compress_write (fp, foo) < 0)
107*c9055873Schristos     goto write_err;
108*c9055873Schristos   close (foo);
109*c9055873Schristos 
110*c9055873Schristos   b = read_file ("tmpdir/two", &b_len);
111*c9055873Schristos 
112*c9055873Schristos   if (a_len == b_len && memcmp (a, b, b_len) == 0)
113*c9055873Schristos     {
114*c9055873Schristos       fprintf (stderr, "compress_writes after adding types does not change the dict\n");
115*c9055873Schristos       return 1;
116*c9055873Schristos     }
117*c9055873Schristos 
118*c9055873Schristos   free (a);
119*c9055873Schristos   free (b);
120*c9055873Schristos 
121*c9055873Schristos   if ((ctf_b = ctf_open ("tmpdir/two", NULL, &err)) == NULL)
122*c9055873Schristos     goto open_err;
123*c9055873Schristos   if ((fp_b = ctf_dict_open (ctf_b, NULL, &err)) == NULL)
124*c9055873Schristos     goto open_err;
125*c9055873Schristos 
126*c9055873Schristos   if (ctf_type_reference (fp_b, ptrtype) == CTF_ERR)
127*c9055873Schristos     fprintf (stderr, "Lookup of pointer preserved across writeout failed: %s\n", ctf_errmsg (ctf_errno (fp_b)));
128*c9055873Schristos 
129*c9055873Schristos   if (ctf_type_reference (fp_b, ptrtype) != type)
130*c9055873Schristos     fprintf (stderr, "Look up of newly-added type in serialized dict yields ID %lx, expected %lx\n", ctf_type_reference (fp_b, ptrtype), type);
131*c9055873Schristos 
132*c9055873Schristos   if (ctf_lookup_by_symbol_name (fp_b, "an_int") == CTF_ERR)
133*c9055873Schristos     fprintf (stderr, "Lookup of symbol an_int failed: %s\n", ctf_errmsg (ctf_errno (fp_b)));
134*c9055873Schristos 
135*c9055873Schristos   ctf_dict_close (fp);
136*c9055873Schristos   ctf_close (ctf);
137*c9055873Schristos 
138*c9055873Schristos   ctf_dict_close (fp_b);
139*c9055873Schristos   ctf_close (ctf_b);
140*c9055873Schristos 
141*c9055873Schristos   printf ("All done.\n");
142*c9055873Schristos   return 0;
143*c9055873Schristos 
144*c9055873Schristos  open_err:
145*c9055873Schristos   fprintf (stderr, "%s: cannot open: %s\n", argv[0], ctf_errmsg (err));
146*c9055873Schristos   return 1;
147*c9055873Schristos  write_err:
148*c9055873Schristos   fprintf (stderr, "%s: cannot write: %s\n", argv[0], ctf_errmsg (ctf_errno (fp)));
149*c9055873Schristos   return 1;
150*c9055873Schristos  write_stderr:
151*c9055873Schristos   fprintf (stderr, "%s: cannot open for writing: %s\n", argv[0], strerror (errno));
152*c9055873Schristos   return 1;
153*c9055873Schristos  read_err:
154*c9055873Schristos   fprintf (stderr, "%s: cannot read\n", argv[0]);
155*c9055873Schristos   return 1;
156*c9055873Schristos }
157