xref: /netbsd-src/external/gpl3/gdb/dist/libctf/testsuite/libctf-lookup/enum-symbol.c (revision 4b169a6ba595ae283ca507b26b15fdff40495b1c)
1*4b169a6bSchristos #include "config.h"
2*4b169a6bSchristos #include <ctf-api.h>
3*4b169a6bSchristos #include <stdio.h>
4*4b169a6bSchristos #include <stdlib.h>
5*4b169a6bSchristos #include <string.h>
6*4b169a6bSchristos 
7*4b169a6bSchristos int
main(int argc,char * argv[])8*4b169a6bSchristos main (int argc, char *argv[])
9*4b169a6bSchristos {
10*4b169a6bSchristos   ctf_archive_t *ctf;
11*4b169a6bSchristos   ctf_dict_t *fp, *tmp_fp;
12*4b169a6bSchristos   int err;
13*4b169a6bSchristos   ctf_id_t type, tmp;
14*4b169a6bSchristos   ctf_next_t *i = NULL;
15*4b169a6bSchristos   const char *name;
16*4b169a6bSchristos   int val;
17*4b169a6bSchristos 
18*4b169a6bSchristos   if (argc != 2)
19*4b169a6bSchristos     {
20*4b169a6bSchristos       fprintf (stderr, "Syntax: %s PROGRAM\n", argv[0]);
21*4b169a6bSchristos       exit(1);
22*4b169a6bSchristos     }
23*4b169a6bSchristos 
24*4b169a6bSchristos   if ((ctf = ctf_open (argv[1], NULL, &err)) == NULL)
25*4b169a6bSchristos     goto open_err;
26*4b169a6bSchristos 
27*4b169a6bSchristos   /* Fish out the enumerator, then fish out all its enumerand/value pairs.  */
28*4b169a6bSchristos 
29*4b169a6bSchristos   if ((fp = ctf_arc_lookup_symbol_name (ctf, "primary1", &type, &err)) == NULL)
30*4b169a6bSchristos     goto sym_err;
31*4b169a6bSchristos 
32*4b169a6bSchristos   while ((name = ctf_enum_next (fp, type, &i, &val)) != NULL)
33*4b169a6bSchristos     {
34*4b169a6bSchristos       printf ("%s has value %i\n", name, val);
35*4b169a6bSchristos     }
36*4b169a6bSchristos   if (ctf_errno (fp) != ECTF_NEXT_END)
37*4b169a6bSchristos     goto nerr;
38*4b169a6bSchristos 
39*4b169a6bSchristos   /* Fish it out again to check the caching layer.  */
40*4b169a6bSchristos   if (((tmp_fp = ctf_arc_lookup_symbol_name (ctf, "primary1", &tmp, &err)) != fp)
41*4b169a6bSchristos       || (tmp != type))
42*4b169a6bSchristos     goto sym_cache_err;
43*4b169a6bSchristos 
44*4b169a6bSchristos   ctf_dict_close (tmp_fp);
45*4b169a6bSchristos   ctf_dict_close (fp);
46*4b169a6bSchristos   ctf_close (ctf);
47*4b169a6bSchristos 
48*4b169a6bSchristos   return 0;
49*4b169a6bSchristos 
50*4b169a6bSchristos  open_err:
51*4b169a6bSchristos   fprintf (stderr, "%s: cannot open: %s\n", argv[0], ctf_errmsg (err));
52*4b169a6bSchristos   return 1;
53*4b169a6bSchristos  sym_err:
54*4b169a6bSchristos   fprintf (stderr, "%s: Symbol lookup error: %s\n", argv[0], ctf_errmsg (err));
55*4b169a6bSchristos   return 1;
56*4b169a6bSchristos  sym_cache_err:
57*4b169a6bSchristos   fprintf (stderr, "%s: Symbol re-lookup error (caching bug): %s\n", argv[0],
58*4b169a6bSchristos 	   ctf_errmsg (err));
59*4b169a6bSchristos   return 1;
60*4b169a6bSchristos  nerr:
61*4b169a6bSchristos   fprintf (stderr, "iteration failed: %s\n", ctf_errmsg (ctf_errno (fp)));
62*4b169a6bSchristos   return 1;
63*4b169a6bSchristos }
64