(root)/
binutils-2.41/
libctf/
testsuite/
libctf-regression/
type-add-unnamed-struct.c
       1  #include <ctf-api.h>
       2  #include <stdio.h>
       3  #include <stdlib.h>
       4  
       5  int
       6  main (int argc, char *argv[])
       7  {
       8    ctf_dict_t *fp;
       9    ctf_archive_t *ctf;
      10    ctf_dict_t *dyn;
      11    ctf_id_t type;
      12    ctf_id_t newtype;
      13    ctf_membinfo_t mi;
      14    const char *membs[] = { "bar", "baz", "foo", NULL };
      15    const char **walk;
      16    int err;
      17  
      18    if (argc != 2)
      19      {
      20        fprintf (stderr, "Syntax: %s PROGRAM\n", argv[0]);
      21        exit(1);
      22      }
      23  
      24    if ((ctf = ctf_open (argv[1], NULL, &err)) == NULL)
      25      goto open_err;
      26  
      27    if ((fp = ctf_dict_open (ctf, NULL, &err)) == NULL)
      28      goto open_err;
      29  
      30    if ((dyn = ctf_create (&err)) == NULL)
      31      goto create_err;
      32  
      33    /* Copy 'struct foo' into the dynamic dict, then make sure we can look up a
      34       member situated inside an unnamed struct.  */
      35  
      36    if ((type = ctf_lookup_by_name (fp, "struct foo")) == CTF_ERR)
      37      {
      38        fprintf (stderr, "Cannot look up struct foo: %s\n", ctf_errmsg (ctf_errno (dyn)));
      39        return 1;
      40      }
      41  
      42    if ((newtype = ctf_add_type (dyn, fp, type)) == CTF_ERR)
      43      goto copy_err;
      44  
      45    for (walk = membs; *walk != NULL; walk++)
      46      {
      47        if (ctf_member_info (dyn, newtype, *walk, &mi) < 0)
      48  	goto lookup_err;
      49        printf ("Looked up %s, type %lx, offset %lx\n", *walk, (long) mi.ctm_type, mi.ctm_offset);
      50      }
      51  
      52    ctf_dict_close (dyn);
      53    ctf_dict_close (fp);
      54    ctf_close (ctf);
      55  
      56    return 0;
      57  
      58   open_err:
      59    fprintf (stderr, "%s: cannot open: %s\n", argv[0], ctf_errmsg (err));
      60    return 1;
      61   create_err:
      62    fprintf (stderr, "%s: cannot create: %s\n", argv[0], ctf_errmsg (err));
      63    return 1;
      64   copy_err:
      65    fprintf (stderr, "Type addition failed: %s\n", ctf_errmsg (ctf_errno (dyn)));
      66    return 1;
      67   lookup_err:
      68    fprintf (stderr, "Cannot look up %s: %s\n", *walk, ctf_errmsg (ctf_errno (dyn)));
      69    return 1;
      70  }