• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 2002, 2004 Red Hat, Inc.
2    This file is part of Red Hat elfutils.
3    Written by Ulrich Drepper <drepper@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 const Dwarf_Addr testaddr[] =
38 {
39   0x804842b, 0x804842c, 0x804843c, 0x8048459, 0x804845a,
40   0x804845b, 0x804845c, 0x8048460, 0x8048465, 0x8048466,
41   0x8048467, 0x8048468, 0x8048470, 0x8048471, 0x8048472
42 };
43 #define ntestaddr (sizeof (testaddr) / sizeof (testaddr[0]))
44 
45 
46 int
main(int argc,char * argv[])47 main (int argc, char *argv[])
48 {
49   int result = 0;
50   int cnt;
51 
52   for (cnt = 1; cnt < argc; ++cnt)
53     {
54       int fd = open (argv[cnt], O_RDONLY);
55 
56       Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
57       if (dbg == NULL)
58 	{
59 	  printf ("%s not usable\n", argv[cnt]);
60 	  result = 1;
61 	  close (fd);
62 	  continue;
63 	}
64 
65       Dwarf_Aranges *aranges;
66       size_t naranges;
67       if (dwarf_getaranges (dbg, &aranges, &naranges) != 0)
68 	printf ("%s: cannot get aranges\n", argv[cnt]);
69       else
70 	{
71 	  for (size_t i = 0; i < ntestaddr; ++i)
72 	    {
73 	      Dwarf_Arange *found;
74 
75 	      found = dwarf_getarange_addr (aranges, testaddr[i]);
76 	      if (found != NULL)
77 		{
78 		  Dwarf_Off cu_offset;
79 
80 		  if (dwarf_getarangeinfo (found, NULL, NULL, &cu_offset) != 0)
81 		    {
82 		      puts ("failed to get CU die offset");
83 		      result = 1;
84 		    }
85 		  else
86 		    {
87 		      const char *cuname;
88 		      Dwarf_Die cu_die;
89 
90 		      if (dwarf_offdie (dbg, cu_offset, &cu_die) == NULL
91 			  || (cuname = dwarf_diename (&cu_die)) == NULL)
92 			{
93 			  puts ("failed to get CU die");
94 			  result = 1;
95 			}
96 		      else
97 			printf ("CU name: \"%s\"\n", cuname);
98 		    }
99 		}
100 	      else
101 		printf ("%#llx: not in range\n",
102 			(unsigned long long int) testaddr[i]);
103 	    }
104 
105 	  for (size_t i = 0; i < naranges; ++i)
106 	    {
107 	      Dwarf_Arange *arange = dwarf_onearange (aranges, i);
108 	      if (arange == NULL)
109 		{
110 		  printf ("cannot get arange %zu: %s\n", i, dwarf_errmsg (-1));
111 		  break;
112 		}
113 
114 	      Dwarf_Addr start;
115 	      Dwarf_Word length;
116 	      Dwarf_Off cu_offset;
117 
118 	      if (dwarf_getarangeinfo (arange, &start, &length, &cu_offset)
119 		  != 0)
120 		{
121 		  printf ("cannot get info from aranges[%zu]\n", i);
122 		  result = 1;
123 		}
124 	      else
125 		{
126 		  printf (" [%2zu] start: %#llx, length: %llu, cu: %llu\n",
127 			  i, (unsigned long long int) start,
128 			  (unsigned long long int) length,
129 			  (unsigned long long int) cu_offset);
130 
131 		  const char *cuname;
132 		  Dwarf_Die cu_die;
133 		  if (dwarf_offdie (dbg, cu_offset, &cu_die) == NULL
134 		      || (cuname = dwarf_diename (&cu_die)) == NULL)
135 		    {
136 		      puts ("failed to get CU die");
137 		      result = 1;
138 		    }
139 		  else
140 		    printf ("CU name: \"%s\"\n", cuname);
141 		}
142 	    }
143 	}
144 
145       dwarf_end (dbg);
146       close (fd);
147     }
148 
149   return result;
150 }
151