blob: 791569f51cbbc604746bb57150df10fa8e2ea583 [file] [log] [blame]
Roland McGrath9a1f3652005-08-18 20:57:04 +00001/* Test program for dwarf_getscopes.
Mark Wielaardf8198f22014-12-27 16:16:29 +01002 Copyright (C) 2005, 2014 Red Hat, Inc.
Mark Wielaardde2ed972012-06-05 17:15:16 +02003 This file is part of elfutils.
Roland McGrath9a1f3652005-08-18 20:57:04 +00004
Mark Wielaardde2ed972012-06-05 17:15:16 +02005 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.
Roland McGrath9a1f3652005-08-18 20:57:04 +00009
Mark Wielaardde2ed972012-06-05 17:15:16 +020010 elfutils is distributed in the hope that it will be useful, but
Ulrich Drepper361df7d2006-04-04 21:38:57 +000011 WITHOUT ANY WARRANTY; without even the implied warranty of
Mark Wielaardde2ed972012-06-05 17:15:16 +020012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
Ulrich Drepper361df7d2006-04-04 21:38:57 +000014
Mark Wielaardde2ed972012-06-05 17:15:16 +020015 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/>. */
Roland McGrath9a1f3652005-08-18 20:57:04 +000017
18#include <config.h>
19#include <assert.h>
20#include <inttypes.h>
Roland McGrathd7f8d0c2005-11-17 02:32:03 +000021#include ELFUTILS_HEADER(dwfl)
Roland McGrath9a1f3652005-08-18 20:57:04 +000022#include <dwarf.h>
23#include <argp.h>
24#include <stdio.h>
25#include <stdio_ext.h>
26#include <locale.h>
27#include <stdlib.h>
28#include <error.h>
29#include <string.h>
30
31
32static void
33paddr (const char *prefix, Dwarf_Addr addr, Dwfl_Line *line)
34{
35 const char *src;
36 int lineno, linecol;
37 if (line != NULL
38 && (src = dwfl_lineinfo (line, &addr, &lineno, &linecol,
39 NULL, NULL)) != NULL)
40 {
41 if (linecol != 0)
42 printf ("%s%#" PRIx64 " (%s:%d:%d)",
43 prefix, addr, src, lineno, linecol);
44 else
45 printf ("%s%#" PRIx64 " (%s:%d)",
46 prefix, addr, src, lineno);
47 }
48 else
49 printf ("%s%#" PRIx64, prefix, addr);
50}
51
Roland McGrath9a1f3652005-08-18 20:57:04 +000052static void
53print_vars (unsigned int indent, Dwarf_Die *die)
54{
55 Dwarf_Die child;
56 if (dwarf_child (die, &child) == 0)
57 do
58 switch (dwarf_tag (&child))
59 {
60 case DW_TAG_variable:
61 case DW_TAG_formal_parameter:
62 printf ("%*s%-30s[%6" PRIx64 "]\n", indent, "",
Roland McGrath71e15a02005-08-27 10:33:26 +000063 dwarf_diename (&child),
Roland McGrath9a1f3652005-08-18 20:57:04 +000064 (uint64_t) dwarf_dieoffset (&child));
65 break;
66 default:
67 break;
68 }
69 while (dwarf_siblingof (&child, &child) == 0);
70
71 Dwarf_Attribute attr_mem;
72 Dwarf_Die origin;
73 if (dwarf_hasattr (die, DW_AT_abstract_origin)
74 && dwarf_formref_die (dwarf_attr (die, DW_AT_abstract_origin, &attr_mem),
75 &origin) != NULL
76 && dwarf_child (&origin, &child) == 0)
77 do
78 switch (dwarf_tag (&child))
79 {
80 case DW_TAG_variable:
81 case DW_TAG_formal_parameter:
82 printf ("%*s%s (abstract)\n", indent, "",
Roland McGrath71e15a02005-08-27 10:33:26 +000083 dwarf_diename (&child));
Roland McGrath9a1f3652005-08-18 20:57:04 +000084 break;
85 default:
86 break;
87 }
88 while (dwarf_siblingof (&child, &child) == 0);
89}
90
91
92#define INDENT 4
93
94static void
95handle_address (GElf_Addr pc, Dwfl *dwfl)
96{
97 Dwarf_Addr cubias;
98 Dwarf_Die *cudie = dwfl_addrdie (dwfl, pc, &cubias);
99 if (cudie == NULL)
100 error (EXIT_FAILURE, 0, "dwfl_addrdie: %s", dwfl_errmsg (-1));
101
102 Dwarf_Die *scopes;
103 int n = dwarf_getscopes (cudie, pc - cubias, &scopes);
104 if (n < 0)
105 error (EXIT_FAILURE, 0, "dwarf_getscopes: %s", dwarf_errmsg (-1));
106 else if (n == 0)
107 printf ("%#" PRIx64 ": not in any scope\n", pc);
108 else
109 {
110 printf ("%#" PRIx64 ":\n", pc);
111 unsigned int indent = 0;
112 while (n-- > 0)
113 {
114 Dwarf_Die *const die = &scopes[n];
115
116 indent += INDENT;
117 printf ("%*s%s (%#x)", indent, "",
Roland McGrath71e15a02005-08-27 10:33:26 +0000118 dwarf_diename (die) ?: "<unnamed>",
Roland McGrath9a1f3652005-08-18 20:57:04 +0000119 dwarf_tag (die));
120
121 Dwarf_Addr lowpc, highpc;
122 if (dwarf_lowpc (die, &lowpc) == 0
123 && dwarf_highpc (die, &highpc) == 0)
124 {
Roland McGrathb0bc2782005-08-27 21:07:16 +0000125 lowpc += cubias;
126 highpc += cubias;
Roland McGrath9a1f3652005-08-18 20:57:04 +0000127 Dwfl_Line *loline = dwfl_getsrc (dwfl, lowpc);
Mark Wielaardf8198f22014-12-27 16:16:29 +0100128 Dwfl_Line *hiline = dwfl_getsrc (dwfl, highpc - 1);
Roland McGrath9a1f3652005-08-18 20:57:04 +0000129 paddr (": ", lowpc, loline);
130 if (highpc != lowpc)
Mark Wielaardf8198f22014-12-27 16:16:29 +0100131 paddr (" .. ", highpc - 1, hiline == loline ? NULL : hiline);
Roland McGrath9a1f3652005-08-18 20:57:04 +0000132 }
133 puts ("");
134
135 print_vars (indent + INDENT, die);
136 }
Mark Wielaarda1372e02015-12-01 15:55:08 +0100137 free (scopes);
Roland McGrath9a1f3652005-08-18 20:57:04 +0000138 }
139}
140
141int
142main (int argc, char *argv[])
143{
144 int remaining;
145
146 /* Set locale. */
147 (void) setlocale (LC_ALL, "");
148
149 Dwfl *dwfl = NULL;
150 (void) argp_parse (dwfl_standard_argp (), argc, argv, 0, &remaining, &dwfl);
151 assert (dwfl != NULL);
152
153 int result = 0;
154
155 /* Now handle the addresses. In case none are given on the command
156 line, read from stdin. */
157 if (remaining == argc)
158 {
159 /* We use no threads here which can interfere with handling a stream. */
160 (void) __fsetlocking (stdin, FSETLOCKING_BYCALLER);
161
162 char *buf = NULL;
163 size_t len = 0;
164 while (!feof_unlocked (stdin))
165 {
166 if (getline (&buf, &len, stdin) < 0)
167 break;
168
169 char *endp;
170 uintmax_t addr = strtoumax (buf, &endp, 0);
171 if (endp != buf)
172 handle_address (addr, dwfl);
173 else
174 result = 1;
175 }
176
177 free (buf);
178 }
179 else
180 {
181 do
182 {
183 char *endp;
184 uintmax_t addr = strtoumax (argv[remaining], &endp, 0);
185 if (endp != argv[remaining])
186 handle_address (addr, dwfl);
187 else
188 result = 1;
189 }
190 while (++remaining < argc);
191 }
192
Roland McGrath994b4892005-12-05 22:46:21 +0000193 dwfl_end (dwfl);
194
Roland McGrath9a1f3652005-08-18 20:57:04 +0000195 return result;
196}