1/* Copyright (C) 2002, 2004 Red Hat, Inc.
2   Written by Ulrich Drepper <drepper@redhat.com>, 2002.
3
4   This program is Open Source software; you can redistribute it and/or
5   modify it under the terms of the Open Software License version 1.0 as
6   published by the Open Source Initiative.
7
8   You should have received a copy of the Open Software License along
9   with this program; if not, you may obtain a copy of the Open Software
10   License version 1.0 from http://www.opensource.org/licenses/osl.php or
11   by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
12   3001 King Ranch Road, Ukiah, CA 95482.   */
13
14#include <fcntl.h>
15#include <libelf.h>
16#include <libdw.h>
17#include <stdio.h>
18#include <unistd.h>
19
20
21static const Dwarf_Addr testaddr[] =
22{
23  0x804842b, 0x804842c, 0x804843c, 0x8048459, 0x804845a,
24  0x804845b, 0x804845c, 0x8048460, 0x8048465, 0x8048466,
25  0x8048467, 0x8048468, 0x8048470, 0x8048471, 0x8048472
26};
27#define ntestaddr (sizeof (testaddr) / sizeof (testaddr[0]))
28
29
30int
31main (int argc, char *argv[])
32{
33  int result = 0;
34  int cnt;
35
36  for (cnt = 1; cnt < argc; ++cnt)
37    {
38      int fd = open (argv[cnt], O_RDONLY);
39
40      Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
41      if (dbg == NULL)
42	{
43	  printf ("%s not usable\n", argv[cnt]);
44	  result = 1;
45	  close (fd);
46	  continue;
47	}
48
49      Dwarf_Aranges *aranges;
50      size_t naranges;
51      if (dwarf_getaranges (dbg, &aranges, &naranges) != 0)
52	printf ("%s: cannot get aranges\n", argv[cnt]);
53      else
54	{
55	  for (size_t i = 0; i < ntestaddr; ++i)
56	    {
57	      Dwarf_Arange *found;
58
59	      found = dwarf_getarange_addr (aranges, testaddr[i]);
60	      if (found != NULL)
61		{
62		  Dwarf_Off cu_offset;
63
64		  if (dwarf_getarangeinfo (found, NULL, NULL, &cu_offset) != 0)
65		    {
66		      puts ("failed to get CU die offset");
67		      result = 1;
68		    }
69		  else
70		    {
71		      const char *cuname;
72		      Dwarf_Die cu_die;
73
74		      if (dwarf_offdie (dbg, cu_offset, &cu_die) == NULL
75			  || (cuname = dwarf_diename (&cu_die)) == NULL)
76			{
77			  puts ("failed to get CU die");
78			  result = 1;
79			}
80		      else
81			printf ("CU name: \"%s\"\n", cuname);
82		    }
83		}
84	      else
85		printf ("%#llx: not in range\n",
86			(unsigned long long int) testaddr[i]);
87	    }
88
89	  for (size_t i = 0; i < naranges; ++i)
90	    {
91	      Dwarf_Arange *arange = dwarf_onearange (aranges, i);
92	      if (arange == NULL)
93		{
94		  printf ("cannot get arange %zu: %s\n", i, dwarf_errmsg (-1));
95		  break;
96		}
97
98	      Dwarf_Addr start;
99	      Dwarf_Word length;
100	      Dwarf_Off cu_offset;
101
102	      if (dwarf_getarangeinfo (arange, &start, &length, &cu_offset)
103		  != 0)
104		{
105		  printf ("cannot get info from aranges[%zu]\n", i);
106		  result = 1;
107		}
108	      else
109		{
110		  printf (" [%2zu] start: %#llx, length: %llu, cu: %llu\n",
111			  i, (unsigned long long int) start,
112			  (unsigned long long int) length,
113			  (unsigned long long int) cu_offset);
114
115		  const char *cuname;
116		  Dwarf_Die cu_die;
117		  if (dwarf_offdie (dbg, cu_offset, &cu_die) == NULL
118		      || (cuname = dwarf_diename (&cu_die)) == NULL)
119		    {
120		      puts ("failed to get CU die");
121		      result = 1;
122		    }
123		  else
124		    printf ("CU name: \"%s\"\n", cuname);
125		}
126	    }
127	}
128
129      dwarf_end (dbg);
130      close (fd);
131    }
132
133  return result;
134}
135