Home | History | Annotate | Download | only in tests
      1 /* Copyright (C) 2002, 2004 Red Hat, Inc.
      2    This file is part of elfutils.
      3    Written by Ulrich Drepper <drepper (at) redhat.com>, 2002.
      4 
      5    This file is free software; you can redistribute it and/or modify
      6    it under the terms of the GNU General Public License as published by
      7    the Free Software Foundation; either version 3 of the License, or
      8    (at your option) any later version.
      9 
     10    elfutils is distributed in the hope that it will be useful, but
     11    WITHOUT ANY WARRANTY; without even the implied warranty of
     12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13    GNU General Public License for more details.
     14 
     15    You should have received a copy of the GNU General Public License
     16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     17 
     18 #ifdef HAVE_CONFIG_H
     19 # include <config.h>
     20 #endif
     21 
     22 #include <fcntl.h>
     23 #include <libelf.h>
     24 #include ELFUTILS_HEADER(dw)
     25 #include <stdio.h>
     26 #include <unistd.h>
     27 
     28 
     29 static const Dwarf_Addr testaddr[] =
     30 {
     31   0x804842b, 0x804842c, 0x804843c, 0x8048459, 0x804845a,
     32   0x804845b, 0x804845c, 0x8048460, 0x8048465, 0x8048466,
     33   0x8048467, 0x8048468, 0x8048470, 0x8048471, 0x8048472
     34 };
     35 #define ntestaddr (sizeof (testaddr) / sizeof (testaddr[0]))
     36 
     37 
     38 int
     39 main (int argc, char *argv[])
     40 {
     41   int result = 0;
     42   int cnt;
     43 
     44   for (cnt = 1; cnt < argc; ++cnt)
     45     {
     46       int fd = open (argv[cnt], O_RDONLY);
     47 
     48       Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
     49       if (dbg == NULL)
     50 	{
     51 	  printf ("%s not usable\n", argv[cnt]);
     52 	  result = 1;
     53 	  close (fd);
     54 	  continue;
     55 	}
     56 
     57       Dwarf_Aranges *aranges;
     58       size_t naranges;
     59       if (dwarf_getaranges (dbg, &aranges, &naranges) != 0)
     60 	printf ("%s: cannot get aranges\n", argv[cnt]);
     61       else
     62 	{
     63 	  for (size_t i = 0; i < ntestaddr; ++i)
     64 	    {
     65 	      Dwarf_Arange *found;
     66 
     67 	      found = dwarf_getarange_addr (aranges, testaddr[i]);
     68 	      if (found != NULL)
     69 		{
     70 		  Dwarf_Off cu_offset;
     71 
     72 		  if (dwarf_getarangeinfo (found, NULL, NULL, &cu_offset) != 0)
     73 		    {
     74 		      puts ("failed to get CU die offset");
     75 		      result = 1;
     76 		    }
     77 		  else
     78 		    {
     79 		      const char *cuname;
     80 		      Dwarf_Die cu_die;
     81 
     82 		      if (dwarf_offdie (dbg, cu_offset, &cu_die) == NULL
     83 			  || (cuname = dwarf_diename (&cu_die)) == NULL)
     84 			{
     85 			  puts ("failed to get CU die");
     86 			  result = 1;
     87 			}
     88 		      else
     89 			printf ("CU name: \"%s\"\n", cuname);
     90 		    }
     91 		}
     92 	      else
     93 		printf ("%#llx: not in range\n",
     94 			(unsigned long long int) testaddr[i]);
     95 	    }
     96 
     97 	  for (size_t i = 0; i < naranges; ++i)
     98 	    {
     99 	      Dwarf_Arange *arange = dwarf_onearange (aranges, i);
    100 	      if (arange == NULL)
    101 		{
    102 		  printf ("cannot get arange %zu: %s\n", i, dwarf_errmsg (-1));
    103 		  break;
    104 		}
    105 
    106 	      Dwarf_Addr start;
    107 	      Dwarf_Word length;
    108 	      Dwarf_Off cu_offset;
    109 
    110 	      if (dwarf_getarangeinfo (arange, &start, &length, &cu_offset)
    111 		  != 0)
    112 		{
    113 		  printf ("cannot get info from aranges[%zu]\n", i);
    114 		  result = 1;
    115 		}
    116 	      else
    117 		{
    118 		  printf (" [%2zu] start: %#llx, length: %llu, cu: %llu\n",
    119 			  i, (unsigned long long int) start,
    120 			  (unsigned long long int) length,
    121 			  (unsigned long long int) cu_offset);
    122 
    123 		  const char *cuname;
    124 		  Dwarf_Die cu_die;
    125 		  if (dwarf_offdie (dbg, cu_offset, &cu_die) == NULL
    126 		      || (cuname = dwarf_diename (&cu_die)) == NULL)
    127 		    {
    128 		      puts ("failed to get CU die");
    129 		      result = 1;
    130 		    }
    131 		  else
    132 		    printf ("CU name: \"%s\"\n", cuname);
    133 		}
    134 	    }
    135 	}
    136 
    137       dwarf_end (dbg);
    138       close (fd);
    139     }
    140 
    141   return result;
    142 }
    143