1*4b169a6bSchristos #include <ctf-api.h>
2*4b169a6bSchristos #include <stdio.h>
3*4b169a6bSchristos #include <stdlib.h>
4*4b169a6bSchristos #include <string.h>
5*4b169a6bSchristos
6*4b169a6bSchristos int
main(int argc,char * argv[])7*4b169a6bSchristos main (int argc, char *argv[])
8*4b169a6bSchristos {
9*4b169a6bSchristos ctf_dict_t *fp;
10*4b169a6bSchristos ctf_archive_t *ctf;
11*4b169a6bSchristos ctf_id_t foo_type, bar_type, sym_type;
12*4b169a6bSchristos int found_foo = 0, found_bar = 0;
13*4b169a6bSchristos ctf_next_t *i = NULL;
14*4b169a6bSchristos const char *name;
15*4b169a6bSchristos int err;
16*4b169a6bSchristos
17*4b169a6bSchristos if (argc != 2)
18*4b169a6bSchristos {
19*4b169a6bSchristos fprintf (stderr, "Syntax: %s PROGRAM\n", argv[0]);
20*4b169a6bSchristos exit(1);
21*4b169a6bSchristos }
22*4b169a6bSchristos
23*4b169a6bSchristos if ((ctf = ctf_open (argv[1], NULL, &err)) == NULL)
24*4b169a6bSchristos goto open_err;
25*4b169a6bSchristos
26*4b169a6bSchristos if ((fp = ctf_dict_open (ctf, NULL, &err)) == NULL)
27*4b169a6bSchristos goto open_err;
28*4b169a6bSchristos
29*4b169a6bSchristos /* Make sure we can look up only 'foo' as a variable: bar, being nonstatic,
30*4b169a6bSchristos should have been erased. */
31*4b169a6bSchristos
32*4b169a6bSchristos if ((foo_type = ctf_lookup_variable (fp, "foo")) == CTF_ERR)
33*4b169a6bSchristos printf ("Cannot look up foo: %s\n", ctf_errmsg (ctf_errno (fp)));
34*4b169a6bSchristos else
35*4b169a6bSchristos printf ("foo is of type %lx\n", foo_type);
36*4b169a6bSchristos
37*4b169a6bSchristos if ((bar_type = ctf_lookup_variable (fp, "bar")) == CTF_ERR)
38*4b169a6bSchristos printf ("Cannot look up bar: %s\n", ctf_errmsg (ctf_errno (fp)));
39*4b169a6bSchristos else
40*4b169a6bSchristos printf ("bar is of type %lx\n", bar_type);
41*4b169a6bSchristos
42*4b169a6bSchristos /* Traverse the entire data object section and make sure it contains an entry
43*4b169a6bSchristos for bar alone. (This is pure laziness: the section is small and
44*4b169a6bSchristos ctf_lookup_by_symbol_name does not yet exist.) */
45*4b169a6bSchristos while ((sym_type = ctf_symbol_next (fp, &i, &name, 0)) != CTF_ERR)
46*4b169a6bSchristos {
47*4b169a6bSchristos if (!name)
48*4b169a6bSchristos continue;
49*4b169a6bSchristos
50*4b169a6bSchristos if (strcmp (name, "foo") == 0)
51*4b169a6bSchristos {
52*4b169a6bSchristos found_foo = 1;
53*4b169a6bSchristos printf ("Found foo in data object section with type %lx, "
54*4b169a6bSchristos "but it is static\n", sym_type);
55*4b169a6bSchristos }
56*4b169a6bSchristos if (strcmp (name, "bar") == 0)
57*4b169a6bSchristos found_bar = 1;
58*4b169a6bSchristos }
59*4b169a6bSchristos if (ctf_errno (fp) != ECTF_NEXT_END)
60*4b169a6bSchristos fprintf (stderr, "Unexpected error iterating over symbols: %s\n",
61*4b169a6bSchristos ctf_errmsg (ctf_errno (fp)));
62*4b169a6bSchristos
63*4b169a6bSchristos if (!found_foo)
64*4b169a6bSchristos printf ("foo missing from the data object section\n");
65*4b169a6bSchristos if (!found_bar)
66*4b169a6bSchristos printf ("bar missing from the data object section\n");
67*4b169a6bSchristos
68*4b169a6bSchristos ctf_dict_close (fp);
69*4b169a6bSchristos ctf_close (ctf);
70*4b169a6bSchristos
71*4b169a6bSchristos return 0;
72*4b169a6bSchristos
73*4b169a6bSchristos open_err:
74*4b169a6bSchristos fprintf (stderr, "%s: cannot open: %s\n", argv[0], ctf_errmsg (err));
75*4b169a6bSchristos return 1;
76*4b169a6bSchristos }
77