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>
42 paddr (const char *prefix, Dwarf_Addr addr, Dwfl_Line *line)
47 && (src = dwfl_lineinfo (line, &addr, &lineno, &linecol,
51 printf ("%s%#" PRIx64 " (%s:%d:%d)",
52 prefix, addr, src, lineno, linecol);
54 printf ("%s%#" PRIx64 " (%s:%d)",
55 prefix, addr, src, lineno);
58 printf ("%s%#" PRIx64, prefix, addr);
63 print_vars (unsigned int indent, Dwarf_Die *die)
66 if (dwarf_child (die, &child) == 0)
68 switch (dwarf_tag (&child))
71 case DW_TAG_formal_parameter:
72 printf ("%*s%-30s[%6" PRIx64 "]\n", indent, "",
73 dwarf_diename (&child),
74 (uint64_t) dwarf_dieoffset (&child));
79 while (dwarf_siblingof (&child, &child) == 0);
81 Dwarf_Attribute attr_mem;
83 if (dwarf_hasattr (die, DW_AT_abstract_origin)
84 && dwarf_formref_die (dwarf_attr (die, DW_AT_abstract_origin, &attr_mem),
86 && dwarf_child (&origin, &child) == 0)
88 switch (dwarf_tag (&child))
91 case DW_TAG_formal_parameter:
92 printf ("%*s%s (abstract)\n", indent, "",
93 dwarf_diename (&child));
98 while (dwarf_siblingof (&child, &child) == 0);
113 handle_function (Dwarf_Die *funcdie, void *arg)
115 struct args *a = arg;
117 const char *name = dwarf_diename (funcdie);
118 char **argv = a->argv;
123 match = fnmatch (*argv, name, 0) == 0;
124 while (!match && *++argv);
130 int n = dwarf_getscopes_die (funcdie, &scopes);
132 error (EXIT_FAILURE, 0, "dwarf_getscopes_die: %s", dwarf_errmsg (-1));
135 Dwarf_Addr start, end;
137 const char *modname = dwfl_module_info (dwfl_cumodule (a->cu), NULL,
142 error (EXIT_FAILURE, 0, "dwfl_module_info: %s", dwarf_errmsg (-1));
143 if (modname[0] == '\0')
145 printf ("%s: %#" PRIx64 " .. %#" PRIx64 "\n", modname, start, end);
147 unsigned int indent = 0;
150 Dwarf_Die *const die = &scopes[n];
153 printf ("%*s%s (%#x)", indent, "",
154 dwarf_diename (die) ?: "<unnamed>",
157 Dwarf_Addr lowpc, highpc;
158 if (dwarf_lowpc (die, &lowpc) == 0
159 && dwarf_highpc (die, &highpc) == 0)
163 Dwfl_Line *loline = dwfl_getsrc (a->dwfl, lowpc);
164 Dwfl_Line *hiline = dwfl_getsrc (a->dwfl, highpc);
165 paddr (": ", lowpc, loline);
167 paddr (" .. ", lowpc, hiline == loline ? NULL : hiline);
171 print_vars (indent + INDENT, die);
180 main (int argc, char *argv[])
185 (void) setlocale (LC_ALL, "");
187 struct args a = { .dwfl = NULL, .cu = NULL };
189 (void) argp_parse (dwfl_standard_argp (), argc, argv, 0, &remaining,
191 assert (a.dwfl != NULL);
192 a.argv = &argv[remaining];
196 while ((a.cu = dwfl_nextcu (a.dwfl, a.cu, &a.dwbias)) != NULL)
197 dwarf_getfuncs (a.cu, &handle_function, &a, 0);