2005-11-16 Roland McGrath <roland@redhat.com>
[platform/upstream/elfutils.git] / tests / get-aranges.c
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 #ifdef HAVE_CONFIG_H
15 # include <config.h>
16 #endif
17
18 #include <fcntl.h>
19 #include <libelf.h>
20 #include ELFUTILS_HEADER(dw)
21 #include <stdio.h>
22 #include <unistd.h>
23
24
25 static const Dwarf_Addr testaddr[] =
26 {
27   0x804842b, 0x804842c, 0x804843c, 0x8048459, 0x804845a,
28   0x804845b, 0x804845c, 0x8048460, 0x8048465, 0x8048466,
29   0x8048467, 0x8048468, 0x8048470, 0x8048471, 0x8048472
30 };
31 #define ntestaddr (sizeof (testaddr) / sizeof (testaddr[0]))
32
33
34 int
35 main (int argc, char *argv[])
36 {
37   int result = 0;
38   int cnt;
39
40   for (cnt = 1; cnt < argc; ++cnt)
41     {
42       int fd = open (argv[cnt], O_RDONLY);
43
44       Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
45       if (dbg == NULL)
46         {
47           printf ("%s not usable\n", argv[cnt]);
48           result = 1;
49           close (fd);
50           continue;
51         }
52
53       Dwarf_Aranges *aranges;
54       size_t naranges;
55       if (dwarf_getaranges (dbg, &aranges, &naranges) != 0)
56         printf ("%s: cannot get aranges\n", argv[cnt]);
57       else
58         {
59           for (size_t i = 0; i < ntestaddr; ++i)
60             {
61               Dwarf_Arange *found;
62
63               found = dwarf_getarange_addr (aranges, testaddr[i]);
64               if (found != NULL)
65                 {
66                   Dwarf_Off cu_offset;
67
68                   if (dwarf_getarangeinfo (found, NULL, NULL, &cu_offset) != 0)
69                     {
70                       puts ("failed to get CU die offset");
71                       result = 1;
72                     }
73                   else
74                     {
75                       const char *cuname;
76                       Dwarf_Die cu_die;
77
78                       if (dwarf_offdie (dbg, cu_offset, &cu_die) == NULL
79                           || (cuname = dwarf_diename (&cu_die)) == NULL)
80                         {
81                           puts ("failed to get CU die");
82                           result = 1;
83                         }
84                       else
85                         printf ("CU name: \"%s\"\n", cuname);
86                     }
87                 }
88               else
89                 printf ("%#llx: not in range\n",
90                         (unsigned long long int) testaddr[i]);
91             }
92
93           for (size_t i = 0; i < naranges; ++i)
94             {
95               Dwarf_Arange *arange = dwarf_onearange (aranges, i);
96               if (arange == NULL)
97                 {
98                   printf ("cannot get arange %zu: %s\n", i, dwarf_errmsg (-1));
99                   break;
100                 }
101
102               Dwarf_Addr start;
103               Dwarf_Word length;
104               Dwarf_Off cu_offset;
105
106               if (dwarf_getarangeinfo (arange, &start, &length, &cu_offset)
107                   != 0)
108                 {
109                   printf ("cannot get info from aranges[%zu]\n", i);
110                   result = 1;
111                 }
112               else
113                 {
114                   printf (" [%2zu] start: %#llx, length: %llu, cu: %llu\n",
115                           i, (unsigned long long int) start,
116                           (unsigned long long int) length,
117                           (unsigned long long int) cu_offset);
118
119                   const char *cuname;
120                   Dwarf_Die cu_die;
121                   if (dwarf_offdie (dbg, cu_offset, &cu_die) == NULL
122                       || (cuname = dwarf_diename (&cu_die)) == NULL)
123                     {
124                       puts ("failed to get CU die");
125                       result = 1;
126                     }
127                   else
128                     printf ("CU name: \"%s\"\n", cuname);
129                 }
130             }
131         }
132
133       dwarf_end (dbg);
134       close (fd);
135     }
136
137   return result;
138 }