1 /* Test program for dwarf_getscopes.
2 Copyright (C) 2005 Red Hat, Inc.
3 This file is part of Red Hat elfutils.
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.
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.
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.
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>. */
29 #include ELFUTILS_HEADER(dwfl)
33 #include <stdio_ext.h>
41 paddr (const char *prefix, Dwarf_Addr addr, Dwfl_Line *line)
46 && (src = dwfl_lineinfo (line, &addr, &lineno, &linecol,
50 printf ("%s%#" PRIx64 " (%s:%d:%d)",
51 prefix, addr, src, lineno, linecol);
53 printf ("%s%#" PRIx64 " (%s:%d)",
54 prefix, addr, src, lineno);
57 printf ("%s%#" PRIx64, prefix, addr);
61 print_vars (unsigned int indent, Dwarf_Die *die)
64 if (dwarf_child (die, &child) == 0)
66 switch (dwarf_tag (&child))
69 case DW_TAG_formal_parameter:
70 printf ("%*s%-30s[%6" PRIx64 "]\n", indent, "",
71 dwarf_diename (&child),
72 (uint64_t) dwarf_dieoffset (&child));
77 while (dwarf_siblingof (&child, &child) == 0);
79 Dwarf_Attribute attr_mem;
81 if (dwarf_hasattr (die, DW_AT_abstract_origin)
82 && dwarf_formref_die (dwarf_attr (die, DW_AT_abstract_origin, &attr_mem),
84 && dwarf_child (&origin, &child) == 0)
86 switch (dwarf_tag (&child))
89 case DW_TAG_formal_parameter:
90 printf ("%*s%s (abstract)\n", indent, "",
91 dwarf_diename (&child));
96 while (dwarf_siblingof (&child, &child) == 0);
103 handle_address (GElf_Addr pc, Dwfl *dwfl)
106 Dwarf_Die *cudie = dwfl_addrdie (dwfl, pc, &cubias);
108 error (EXIT_FAILURE, 0, "dwfl_addrdie: %s", dwfl_errmsg (-1));
111 int n = dwarf_getscopes (cudie, pc - cubias, &scopes);
113 error (EXIT_FAILURE, 0, "dwarf_getscopes: %s", dwarf_errmsg (-1));
115 printf ("%#" PRIx64 ": not in any scope\n", pc);
118 printf ("%#" PRIx64 ":\n", pc);
119 unsigned int indent = 0;
122 Dwarf_Die *const die = &scopes[n];
125 printf ("%*s%s (%#x)", indent, "",
126 dwarf_diename (die) ?: "<unnamed>",
129 Dwarf_Addr lowpc, highpc;
130 if (dwarf_lowpc (die, &lowpc) == 0
131 && dwarf_highpc (die, &highpc) == 0)
135 Dwfl_Line *loline = dwfl_getsrc (dwfl, lowpc);
136 Dwfl_Line *hiline = dwfl_getsrc (dwfl, highpc);
137 paddr (": ", lowpc, loline);
139 paddr (" .. ", lowpc, hiline == loline ? NULL : hiline);
143 print_vars (indent + INDENT, die);
149 main (int argc, char *argv[])
154 (void) setlocale (LC_ALL, "");
157 (void) argp_parse (dwfl_standard_argp (), argc, argv, 0, &remaining, &dwfl);
158 assert (dwfl != NULL);
162 /* Now handle the addresses. In case none are given on the command
163 line, read from stdin. */
164 if (remaining == argc)
166 /* We use no threads here which can interfere with handling a stream. */
167 (void) __fsetlocking (stdin, FSETLOCKING_BYCALLER);
171 while (!feof_unlocked (stdin))
173 if (getline (&buf, &len, stdin) < 0)
177 uintmax_t addr = strtoumax (buf, &endp, 0);
179 handle_address (addr, dwfl);
191 uintmax_t addr = strtoumax (argv[remaining], &endp, 0);
192 if (endp != argv[remaining])
193 handle_address (addr, dwfl);
197 while (++remaining < argc);