File: type-add-unnamed-struct.c

package info (click to toggle)
binutils 2.45-5
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 408,464 kB
  • sloc: ansic: 1,465,044; asm: 816,463; cpp: 87,135; exp: 79,015; makefile: 67,947; sh: 20,839; yacc: 14,149; lisp: 13,640; perl: 13,404; lex: 1,714; ada: 1,681; pascal: 1,446; cs: 879; python: 630; java: 478; sed: 191; xml: 95; awk: 25
file content (70 lines) | stat: -rw-r--r-- 1,725 bytes parent folder | download | duplicates (18)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <ctf-api.h>
#include <stdio.h>
#include <stdlib.h>

int
main (int argc, char *argv[])
{
  ctf_dict_t *fp;
  ctf_archive_t *ctf;
  ctf_dict_t *dyn;
  ctf_id_t type;
  ctf_id_t newtype;
  ctf_membinfo_t mi;
  const char *membs[] = { "bar", "baz", "foo", NULL };
  const char **walk;
  int err;

  if (argc != 2)
    {
      fprintf (stderr, "Syntax: %s PROGRAM\n", argv[0]);
      exit(1);
    }

  if ((ctf = ctf_open (argv[1], NULL, &err)) == NULL)
    goto open_err;

  if ((fp = ctf_dict_open (ctf, NULL, &err)) == NULL)
    goto open_err;

  if ((dyn = ctf_create (&err)) == NULL)
    goto create_err;

  /* Copy 'struct foo' into the dynamic dict, then make sure we can look up a
     member situated inside an unnamed struct.  */

  if ((type = ctf_lookup_by_name (fp, "struct foo")) == CTF_ERR)
    {
      fprintf (stderr, "Cannot look up struct foo: %s\n", ctf_errmsg (ctf_errno (dyn)));
      return 1;
    }

  if ((newtype = ctf_add_type (dyn, fp, type)) == CTF_ERR)
    goto copy_err;

  for (walk = membs; *walk != NULL; walk++)
    {
      if (ctf_member_info (dyn, newtype, *walk, &mi) < 0)
	goto lookup_err;
      printf ("Looked up %s, type %lx, offset %lx\n", *walk, (long) mi.ctm_type, mi.ctm_offset);
    }

  ctf_dict_close (dyn);
  ctf_dict_close (fp);
  ctf_close (ctf);

  return 0;

 open_err:
  fprintf (stderr, "%s: cannot open: %s\n", argv[0], ctf_errmsg (err));
  return 1;
 create_err:
  fprintf (stderr, "%s: cannot create: %s\n", argv[0], ctf_errmsg (err));
  return 1;
 copy_err:
  fprintf (stderr, "Type addition failed: %s\n", ctf_errmsg (ctf_errno (dyn)));
  return 1;
 lookup_err:
  fprintf (stderr, "Cannot look up %s: %s\n", *walk, ctf_errmsg (ctf_errno (dyn)));
  return 1;
}