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>
34 paddr (const char *prefix, Dwarf_Addr addr, Dwfl_Line *line)
39 && (src = dwfl_lineinfo (line, &addr, &lineno, &linecol,
43 printf ("%s%#" PRIx64 " (%s:%d:%d)",
44 prefix, addr, src, lineno, linecol);
46 printf ("%s%#" PRIx64 " (%s:%d)",
47 prefix, addr, src, lineno);
50 printf ("%s%#" PRIx64, prefix, addr);
55 print_vars (unsigned int indent, Dwarf_Die *die)
58 if (dwarf_child (die, &child) == 0)
60 switch (dwarf_tag (&child))
63 case DW_TAG_formal_parameter:
64 printf ("%*s%-30s[%6" PRIx64 "]\n", indent, "",
65 dwarf_diename (&child),
66 (uint64_t) dwarf_dieoffset (&child));
71 while (dwarf_siblingof (&child, &child) == 0);
73 Dwarf_Attribute attr_mem;
75 if (dwarf_hasattr (die, DW_AT_abstract_origin)
76 && dwarf_formref_die (dwarf_attr (die, DW_AT_abstract_origin, &attr_mem),
78 && dwarf_child (&origin, &child) == 0)
80 switch (dwarf_tag (&child))
83 case DW_TAG_formal_parameter:
84 printf ("%*s%s (abstract)\n", indent, "",
85 dwarf_diename (&child));
90 while (dwarf_siblingof (&child, &child) == 0);
105 handle_function (Dwarf_Die *funcdie, void *arg)
107 struct args *a = arg;
109 const char *name = dwarf_diename (funcdie);
110 char **argv = a->argv;
115 match = fnmatch (*argv, name, 0) == 0;
116 while (!match && *++argv);
122 int n = dwarf_getscopes_die (funcdie, &scopes);
124 error (EXIT_FAILURE, 0, "dwarf_getscopes_die: %s", dwarf_errmsg (-1));
127 Dwarf_Addr start, end;
129 const char *modname = dwfl_module_info (dwfl_cumodule (a->cu), NULL,
134 error (EXIT_FAILURE, 0, "dwfl_module_info: %s", dwarf_errmsg (-1));
135 if (modname[0] == '\0')
137 printf ("%s: %#" PRIx64 " .. %#" PRIx64 "\n", modname, start, end);
139 unsigned int indent = 0;
142 Dwarf_Die *const die = &scopes[n];
145 printf ("%*s%s (%#x)", indent, "",
146 dwarf_diename (die) ?: "<unnamed>",
149 Dwarf_Addr lowpc, highpc;
150 if (dwarf_lowpc (die, &lowpc) == 0
151 && dwarf_highpc (die, &highpc) == 0)
155 Dwfl_Line *loline = dwfl_getsrc (a->dwfl, lowpc);
156 Dwfl_Line *hiline = dwfl_getsrc (a->dwfl, highpc);
157 paddr (": ", lowpc, loline);
159 paddr (" .. ", lowpc, hiline == loline ? NULL : hiline);
163 print_vars (indent + INDENT, die);
172 main (int argc, char *argv[])
177 (void) setlocale (LC_ALL, "");
179 struct args a = { .dwfl = NULL, .cu = NULL };
181 (void) argp_parse (dwfl_standard_argp (), argc, argv, 0, &remaining,
183 assert (a.dwfl != NULL);
184 a.argv = &argv[remaining];
188 while ((a.cu = dwfl_nextcu (a.dwfl, a.cu, &a.dwbias)) != NULL)
189 dwarf_getfuncs (a.cu, &handle_function, &a, 0);