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