1 /* Test program for dwarf_getscopes.
2 Copyright (C) 2005 Red Hat, Inc.
3 This file is part of elfutils.
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.
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.
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/>. */
21 #include ELFUTILS_HEADER(dwfl)
25 #include <stdio_ext.h>
33 paddr (const char *prefix, Dwarf_Addr addr, Dwfl_Line *line)
38 && (src = dwfl_lineinfo (line, &addr, &lineno, &linecol,
42 printf ("%s%#" PRIx64 " (%s:%d:%d)",
43 prefix, addr, src, lineno, linecol);
45 printf ("%s%#" PRIx64 " (%s:%d)",
46 prefix, addr, src, lineno);
49 printf ("%s%#" PRIx64, prefix, addr);
53 print_vars (unsigned int indent, Dwarf_Die *die)
56 if (dwarf_child (die, &child) == 0)
58 switch (dwarf_tag (&child))
61 case DW_TAG_formal_parameter:
62 printf ("%*s%-30s[%6" PRIx64 "]\n", indent, "",
63 dwarf_diename (&child),
64 (uint64_t) dwarf_dieoffset (&child));
69 while (dwarf_siblingof (&child, &child) == 0);
71 Dwarf_Attribute attr_mem;
73 if (dwarf_hasattr (die, DW_AT_abstract_origin)
74 && dwarf_formref_die (dwarf_attr (die, DW_AT_abstract_origin, &attr_mem),
76 && dwarf_child (&origin, &child) == 0)
78 switch (dwarf_tag (&child))
81 case DW_TAG_formal_parameter:
82 printf ("%*s%s (abstract)\n", indent, "",
83 dwarf_diename (&child));
88 while (dwarf_siblingof (&child, &child) == 0);
95 handle_address (GElf_Addr pc, Dwfl *dwfl)
98 Dwarf_Die *cudie = dwfl_addrdie (dwfl, pc, &cubias);
100 error (EXIT_FAILURE, 0, "dwfl_addrdie: %s", dwfl_errmsg (-1));
103 int n = dwarf_getscopes (cudie, pc - cubias, &scopes);
105 error (EXIT_FAILURE, 0, "dwarf_getscopes: %s", dwarf_errmsg (-1));
107 printf ("%#" PRIx64 ": not in any scope\n", pc);
110 printf ("%#" PRIx64 ":\n", pc);
111 unsigned int indent = 0;
114 Dwarf_Die *const die = &scopes[n];
117 printf ("%*s%s (%#x)", indent, "",
118 dwarf_diename (die) ?: "<unnamed>",
121 Dwarf_Addr lowpc, highpc;
122 if (dwarf_lowpc (die, &lowpc) == 0
123 && dwarf_highpc (die, &highpc) == 0)
127 Dwfl_Line *loline = dwfl_getsrc (dwfl, lowpc);
128 Dwfl_Line *hiline = dwfl_getsrc (dwfl, highpc);
129 paddr (": ", lowpc, loline);
131 paddr (" .. ", lowpc, hiline == loline ? NULL : hiline);
135 print_vars (indent + INDENT, die);
141 main (int argc, char *argv[])
146 (void) setlocale (LC_ALL, "");
149 (void) argp_parse (dwfl_standard_argp (), argc, argv, 0, &remaining, &dwfl);
150 assert (dwfl != NULL);
154 /* Now handle the addresses. In case none are given on the command
155 line, read from stdin. */
156 if (remaining == argc)
158 /* We use no threads here which can interfere with handling a stream. */
159 (void) __fsetlocking (stdin, FSETLOCKING_BYCALLER);
163 while (!feof_unlocked (stdin))
165 if (getline (&buf, &len, stdin) < 0)
169 uintmax_t addr = strtoumax (buf, &endp, 0);
171 handle_address (addr, dwfl);
183 uintmax_t addr = strtoumax (argv[remaining], &endp, 0);
184 if (endp != argv[remaining])
185 handle_address (addr, dwfl);
189 while (++remaining < argc);