Home | History | Annotate | Download | only in tests
      1 /* Copyright (C) 2002, 2005 Red Hat, Inc.
      2    This file is part of Red Hat elfutils.
      3    Written by Ulrich Drepper <drepper (at) redhat.com>, 2002.
      4 
      5    Red Hat elfutils is free software; you can redistribute it and/or modify
      6    it under the terms of the GNU General Public License as published by the
      7    Free Software Foundation; version 2 of the License.
      8 
      9    Red Hat elfutils is distributed in the hope that it will be useful, but
     10    WITHOUT ANY WARRANTY; without even the implied warranty of
     11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12    General Public License for more details.
     13 
     14    You should have received a copy of the GNU General Public License along
     15    with Red Hat elfutils; if not, write to the Free Software Foundation,
     16    Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
     17 
     18    Red Hat elfutils is an included package of the Open Invention Network.
     19    An included package of the Open Invention Network is a package for which
     20    Open Invention Network licensees cross-license their patents.  No patent
     21    license is granted, either expressly or impliedly, by designation as an
     22    included package.  Should you wish to participate in the Open Invention
     23    Network licensing program, please visit www.openinventionnetwork.com
     24    <http://www.openinventionnetwork.com>.  */
     25 
     26 #ifdef HAVE_CONFIG_H
     27 # include <config.h>
     28 #endif
     29 
     30 #include <fcntl.h>
     31 #include <libelf.h>
     32 #include ELFUTILS_HEADER(dw)
     33 #include <stdio.h>
     34 #include <unistd.h>
     35 
     36 
     37 static int globcnt;
     38 
     39 static int
     40 callback (Dwarf *dbg, Dwarf_Global *gl, void *arg __attribute__ ((unused)))
     41 {
     42   int result = DWARF_CB_OK;
     43 
     44   printf (" [%2d] \"%s\", die: %llu, cu: %llu\n",
     45 	  globcnt++, gl->name, (unsigned long long int) gl->die_offset,
     46 	  (unsigned long long int) gl->cu_offset);
     47 
     48   Dwarf_Die cu_die;
     49   const char *cuname;
     50   if (dwarf_offdie (dbg, gl->cu_offset, &cu_die) == NULL
     51       || (cuname = dwarf_diename (&cu_die)) == NULL)
     52     {
     53       puts ("failed to get CU die");
     54       result = DWARF_CB_ABORT;
     55     }
     56   else
     57     printf ("CU name: \"%s\"\n", cuname);
     58 
     59   const char *diename;
     60   Dwarf_Die die;
     61   if (dwarf_offdie (dbg, gl->die_offset, &die) == NULL
     62       || (diename = dwarf_diename (&die)) == NULL)
     63     {
     64       puts ("failed to get object die");
     65       result = DWARF_CB_ABORT;
     66     }
     67   else
     68     printf ("object name: \"%s\"\n", diename);
     69 
     70   return result;
     71 }
     72 
     73 
     74 int
     75 main (int argc, char *argv[])
     76 {
     77   int result = 0;
     78   int cnt;
     79 
     80   for (cnt = 1; cnt < argc; ++cnt)
     81     {
     82       int fd = open (argv[cnt], O_RDONLY);
     83       Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
     84       if (dbg == NULL)
     85 	{
     86 	  printf ("%s not usable: %s\n", argv[cnt], dwarf_errmsg (-1));
     87 	  result = 1;
     88 	  close (fd);
     89 	  continue;
     90 	}
     91 
     92       globcnt = 0;
     93 
     94       if (dwarf_getpubnames (dbg, callback, NULL, 0) != 0)
     95 	{
     96 	  printf ("dwarf_get_pubnames didn't return zero: %s\n",
     97 		  dwarf_errmsg (-1));
     98 	  result = 1;
     99 	}
    100 
    101       dwarf_end (dbg);
    102       close (fd);
    103     }
    104 
    105   return result;
    106 }
    107