xref: /netbsd-src/external/gpl3/gdb/dist/libctf/testsuite/libctf-writable/libctf-errors.c (revision c9055873d0546e63388f027d3d7f85381cde0545)
1*c9055873Schristos /* Make sure that error returns are correct.  Usually this is trivially
2*c9055873Schristos    true, but on platforms with unusual type sizes all the casting might
3*c9055873Schristos    cause problems with unexpected sign-extension and truncation.  */
4*c9055873Schristos 
5*c9055873Schristos #include <ctf-api.h>
6*c9055873Schristos #include <stdio.h>
7*c9055873Schristos #include <stdlib.h>
8*c9055873Schristos 
9*c9055873Schristos int
10*c9055873Schristos main (int argc, char *argv[])
11*c9055873Schristos {
12*c9055873Schristos   ctf_dict_t *fp;
13*c9055873Schristos   ctf_next_t *i = NULL;
14*c9055873Schristos   size_t boom = 0;
15*c9055873Schristos   ctf_id_t itype = 0, stype = 0;
16*c9055873Schristos   ctf_encoding_t encoding = {0};
17*c9055873Schristos   ctf_membinfo_t mi;
18*c9055873Schristos   ssize_t ret;
19*c9055873Schristos   int err;
20*c9055873Schristos 
21*c9055873Schristos   if ((fp = ctf_create (&err)) == NULL)
22*c9055873Schristos     {
23*c9055873Schristos       fprintf (stderr, "%s: cannot create: %s\n", argv[0], ctf_errmsg (err));
24*c9055873Schristos       return 1;
25*c9055873Schristos     }
26*c9055873Schristos 
27*c9055873Schristos   /* First error class: int return.  */
28*c9055873Schristos 
29*c9055873Schristos   if (ctf_member_count (fp, 1024) >= 0)
30*c9055873Schristos     fprintf (stderr, "int return: non-error return: %i\n",
31*c9055873Schristos              ctf_member_count(fp, 1024));
32*c9055873Schristos 
33*c9055873Schristos   /* Second error class: type ID return.  */
34*c9055873Schristos 
35*c9055873Schristos   if (ctf_type_reference (fp, 1024) != CTF_ERR)
36*c9055873Schristos     fprintf (stderr, "ctf_id_t return: non-error return: %li\n",
37*c9055873Schristos              ctf_type_reference (fp, 1024));
38*c9055873Schristos 
39*c9055873Schristos   /* Third error class: ssize_t return.  Create a type to iterate over first.  */
40*c9055873Schristos 
41*c9055873Schristos   if ((itype = ctf_add_integer (fp, CTF_ADD_ROOT, "int", &encoding)) == CTF_ERR)
42*c9055873Schristos     fprintf (stderr, "cannot add int: %s\n", ctf_errmsg (ctf_errno (fp)));
43*c9055873Schristos   else if ((stype = ctf_add_struct (fp, CTF_ADD_ROOT, "foo")) == CTF_ERR)
44*c9055873Schristos     fprintf (stderr, "cannot add struct: %s\n", ctf_errmsg (ctf_errno (fp)));
45*c9055873Schristos   else if (ctf_add_member (fp, stype, "bar", itype) < 0)
46*c9055873Schristos     fprintf (stderr, "cannot add member: %s\n", ctf_errmsg (ctf_errno (fp)));
47*c9055873Schristos 
48*c9055873Schristos   if (ctf_member_info (fp, stype, "bar", &mi) < 0)
49*c9055873Schristos     fprintf (stderr, "cannot get member info: %s\n", ctf_errmsg (ctf_errno (fp)));
50*c9055873Schristos 
51*c9055873Schristos   /* Iteration should never produce an offset bigger than the offset just returned,
52*c9055873Schristos      and should quickly terminate.  */
53*c9055873Schristos 
54*c9055873Schristos   while ((ret = ctf_member_next (fp, stype, &i, NULL, NULL, 0)) >= 0) {
55*c9055873Schristos     if (ret > mi.ctm_offset)
56*c9055873Schristos       fprintf (stderr, "ssize_t return: unexpected offset: %zi\n", ret);
57*c9055873Schristos     if (boom++ > 1000)
58*c9055873Schristos       {
59*c9055873Schristos         fprintf (stderr, "member iteration went on way too long\n");
60*c9055873Schristos         exit (1);
61*c9055873Schristos       }
62*c9055873Schristos   }
63*c9055873Schristos 
64*c9055873Schristos   /* Fourth error class (trivial): pointer return.  */
65*c9055873Schristos   if (ctf_type_aname (fp, 1024) != NULL)
66*c9055873Schristos     fprintf (stderr, "pointer return: non-error return: %p\n",
67*c9055873Schristos              ctf_type_aname (fp, 1024));
68*c9055873Schristos 
69*c9055873Schristos   ctf_file_close (fp);
70*c9055873Schristos 
71*c9055873Schristos   printf("All done.\n");
72*c9055873Schristos 
73*c9055873Schristos   return 0;
74*c9055873Schristos }
75