]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - libctf/testsuite/libctf-lookup/enum-symbol.c
libctf, include: find types of symbols by name
[thirdparty/binutils-gdb.git] / libctf / testsuite / libctf-lookup / enum-symbol.c
1 #include "config.h"
2 #include <ctf-api.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 int
8 main (int argc, char *argv[])
9 {
10 ctf_archive_t *ctf;
11 ctf_dict_t *fp, *tmp_fp;
12 int err;
13 ctf_id_t type, tmp;
14 ctf_next_t *i = NULL;
15 const char *name;
16 int val;
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 /* Fish out the enumerator, then fish out all its enumerand/value pairs. */
28
29 if ((fp = ctf_arc_lookup_symbol_name (ctf, "primary1", &type, &err)) == NULL)
30 goto sym_err;
31
32 while ((name = ctf_enum_next (fp, type, &i, &val)) != NULL)
33 {
34 printf ("%s has value %i\n", name, val);
35 }
36 if (ctf_errno (fp) != ECTF_NEXT_END)
37 goto nerr;
38
39 /* Fish it out again to check the caching layer. */
40 if (((tmp_fp = ctf_arc_lookup_symbol_name (ctf, "primary1", &tmp, &err)) != fp)
41 || (tmp != type))
42 goto sym_cache_err;
43
44 ctf_dict_close (tmp_fp);
45 ctf_dict_close (fp);
46 ctf_close (ctf);
47
48 return 0;
49
50 open_err:
51 fprintf (stderr, "%s: cannot open: %s\n", argv[0], ctf_errmsg (err));
52 return 1;
53 sym_err:
54 fprintf (stderr, "%s: Symbol lookup error: %s\n", argv[0], ctf_errmsg (err));
55 return 1;
56 sym_cache_err:
57 fprintf (stderr, "%s: Symbol re-lookup error (caching bug): %s\n", argv[0],
58 ctf_errmsg (err));
59 return 1;
60 err:
61 fprintf (stderr, "Lookup failed: %s\n", ctf_errmsg (ctf_errno (fp)));
62 return 1;
63 nerr:
64 fprintf (stderr, "iteration failed: %s\n", ctf_errmsg (ctf_errno (fp)));
65 return 1;
66 }