xref: /netbsd-src/external/gpl3/gdb/dist/libctf/testsuite/libctf-lookup/unnamed-field-info.c (revision c9055873d0546e63388f027d3d7f85381cde0545)
1*c9055873Schristos /* Make sure unnamed field offsets are relative to the containing struct.  */
2*c9055873Schristos 
3*c9055873Schristos #include <ctf-api.h>
4*c9055873Schristos #include <stddef.h>
5*c9055873Schristos #include <stdio.h>
6*c9055873Schristos #include <stdlib.h>
7*c9055873Schristos 
8*c9055873Schristos #include "unnamed-field-info-ctf.c"
9*c9055873Schristos 
10*c9055873Schristos static void
11*c9055873Schristos verify_offsetof_matching (ctf_dict_t *fp, ctf_id_t type, const char *name, size_t offset)
12*c9055873Schristos {
13*c9055873Schristos   ctf_membinfo_t mi;
14*c9055873Schristos 
15*c9055873Schristos   if (ctf_member_info (fp, type, name, &mi) < 0)
16*c9055873Schristos     goto err;
17*c9055873Schristos 
18*c9055873Schristos   if (mi.ctm_offset != offset * 8)
19*c9055873Schristos     fprintf (stderr, "field %s inconsistency: offsetof() says %zi bits, CTF says %zi\n",
20*c9055873Schristos 	     name, offset * 8, mi.ctm_offset);
21*c9055873Schristos 
22*c9055873Schristos   return;
23*c9055873Schristos 
24*c9055873Schristos  err:
25*c9055873Schristos   fprintf (stderr, "Cannot look up field %s: %s\n", name,
26*c9055873Schristos 	   ctf_errmsg (ctf_errno (fp)));
27*c9055873Schristos   return;
28*c9055873Schristos }
29*c9055873Schristos 
30*c9055873Schristos int
31*c9055873Schristos main (int argc, char *argv[])
32*c9055873Schristos {
33*c9055873Schristos   ctf_dict_t *fp;
34*c9055873Schristos   ctf_archive_t *ctf;
35*c9055873Schristos   ctf_id_t type;
36*c9055873Schristos   int err;
37*c9055873Schristos 
38*c9055873Schristos   if (argc != 2)
39*c9055873Schristos     {
40*c9055873Schristos       fprintf (stderr, "Syntax: %s PROGRAM\n", argv[0]);
41*c9055873Schristos       exit(1);
42*c9055873Schristos     }
43*c9055873Schristos 
44*c9055873Schristos   if ((ctf = ctf_open (argv[1], NULL, &err)) == NULL)
45*c9055873Schristos     goto open_err;
46*c9055873Schristos   if ((fp = ctf_dict_open (ctf, NULL, &err)) == NULL)
47*c9055873Schristos     goto open_err;
48*c9055873Schristos 
49*c9055873Schristos   /* Dig out some structure members by name.  */
50*c9055873Schristos 
51*c9055873Schristos   if ((type = ctf_lookup_by_name (fp, "struct A") ) == CTF_ERR)
52*c9055873Schristos     goto err;
53*c9055873Schristos 
54*c9055873Schristos   verify_offsetof_matching (fp, type, "a", offsetof (struct A, a));
55*c9055873Schristos   verify_offsetof_matching (fp, type, "b", offsetof (struct A, b));
56*c9055873Schristos   verify_offsetof_matching (fp, type, "one", offsetof (struct A, one));
57*c9055873Schristos   verify_offsetof_matching (fp, type, "two", offsetof (struct A, two));
58*c9055873Schristos   verify_offsetof_matching (fp, type, "three", offsetof (struct A, three));
59*c9055873Schristos   verify_offsetof_matching (fp, type, "four", offsetof (struct A, four));
60*c9055873Schristos   verify_offsetof_matching (fp, type, "x", offsetof (struct A, x));
61*c9055873Schristos   verify_offsetof_matching (fp, type, "y", offsetof (struct A, y));
62*c9055873Schristos   verify_offsetof_matching (fp, type, "z", offsetof (struct A, z));
63*c9055873Schristos   verify_offsetof_matching (fp, type, "aleph", offsetof (struct A, aleph));
64*c9055873Schristos 
65*c9055873Schristos   ctf_dict_close (fp);
66*c9055873Schristos   ctf_arc_close (ctf);
67*c9055873Schristos 
68*c9055873Schristos   printf ("Offset validation complete.\n");
69*c9055873Schristos 
70*c9055873Schristos   return 0;
71*c9055873Schristos 
72*c9055873Schristos  open_err:
73*c9055873Schristos   fprintf (stderr, "%s: cannot open: %s\n", argv[0], ctf_errmsg (err));
74*c9055873Schristos   return 1;
75*c9055873Schristos 
76*c9055873Schristos  err:
77*c9055873Schristos   fprintf (stderr, "Cannot look up type: %s\n", ctf_errmsg (ctf_errno (fp)));
78*c9055873Schristos   return 1;
79*c9055873Schristos }
80