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