1*4b169a6bSchristos #include <ctf-api.h>
2*4b169a6bSchristos #include <stdio.h>
3*4b169a6bSchristos #include <stdlib.h>
4*4b169a6bSchristos
5*4b169a6bSchristos int
main(int argc,char * argv[])6*4b169a6bSchristos main (int argc, char *argv[])
7*4b169a6bSchristos {
8*4b169a6bSchristos ctf_dict_t *fp;
9*4b169a6bSchristos ctf_archive_t *ctf;
10*4b169a6bSchristos ctf_id_t type;
11*4b169a6bSchristos char *type_name;
12*4b169a6bSchristos ctf_membinfo_t mi;
13*4b169a6bSchristos int err;
14*4b169a6bSchristos
15*4b169a6bSchristos if (argc != 2)
16*4b169a6bSchristos {
17*4b169a6bSchristos fprintf (stderr, "Syntax: %s PROGRAM\n", argv[0]);
18*4b169a6bSchristos exit(1);
19*4b169a6bSchristos }
20*4b169a6bSchristos
21*4b169a6bSchristos if ((ctf = ctf_open (argv[1], NULL, &err)) == NULL)
22*4b169a6bSchristos goto open_err;
23*4b169a6bSchristos if ((fp = ctf_dict_open (ctf, NULL, &err)) == NULL)
24*4b169a6bSchristos goto open_err;
25*4b169a6bSchristos
26*4b169a6bSchristos /* Dig out some strucutre members by name. */
27*4b169a6bSchristos
28*4b169a6bSchristos if ((type = ctf_lookup_by_name (fp, "struct foo_t") ) == CTF_ERR)
29*4b169a6bSchristos goto err;
30*4b169a6bSchristos
31*4b169a6bSchristos if (ctf_member_info (fp, type, "baz", &mi) < 0)
32*4b169a6bSchristos goto err;
33*4b169a6bSchristos
34*4b169a6bSchristos type_name = ctf_type_aname (fp, mi.ctm_type);
35*4b169a6bSchristos printf ("baz is of type %s, at offset %lx\n", type_name, mi.ctm_offset);
36*4b169a6bSchristos free (type_name);
37*4b169a6bSchristos
38*4b169a6bSchristos if (ctf_member_info (fp, type, "one_more_level", &mi) < 0)
39*4b169a6bSchristos goto err;
40*4b169a6bSchristos
41*4b169a6bSchristos type_name = ctf_type_aname (fp, mi.ctm_type);
42*4b169a6bSchristos printf ("one_more_level is of type %s, at offset %lx\n", type_name, mi.ctm_offset);
43*4b169a6bSchristos free (type_name);
44*4b169a6bSchristos
45*4b169a6bSchristos if (ctf_member_info (fp, type, "should_not_appear", &mi) >= 0
46*4b169a6bSchristos || ctf_errno (fp) != ECTF_NOMEMBNAM)
47*4b169a6bSchristos fprintf (stderr, "should_not_appear appeared.\n");
48*4b169a6bSchristos
49*4b169a6bSchristos ctf_dict_close (fp);
50*4b169a6bSchristos ctf_close (ctf);
51*4b169a6bSchristos
52*4b169a6bSchristos return 0;
53*4b169a6bSchristos
54*4b169a6bSchristos open_err:
55*4b169a6bSchristos fprintf (stderr, "%s: cannot open: %s\n", argv[0], ctf_errmsg (err));
56*4b169a6bSchristos return 1;
57*4b169a6bSchristos err:
58*4b169a6bSchristos fprintf (stderr, "Lookup failed: %s\n", ctf_errmsg (ctf_errno (fp)));
59*4b169a6bSchristos return 1;
60*4b169a6bSchristos }
61