1 /* Test program for dwarf_decl_file
2 Copyright (c) 2024 Meta Platforms, Inc. and affiliates.
3 This file is part of elfutils.
4
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.
9
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.
14
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/>. */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21 #include <fcntl.h>
22 #include <stdio.h>
23 #include <unistd.h>
24
25 #include <dwarf.h>
26 #include ELFUTILS_HEADER(dw)
27
28 static void
walk_tree(Dwarf_Die * dwarf_die,int indent)29 walk_tree (Dwarf_Die *dwarf_die, int indent)
30 {
31 Dwarf_Die die = *dwarf_die;
32 do
33 {
34 int child_indent = indent;
35 const char *file = dwarf_decl_file (&die);
36 if (file != NULL)
37 {
38 printf("%*s", indent, "");
39 const char *name = dwarf_diename (&die) ?: "???";
40 int line, column;
41 if (dwarf_decl_line (&die, &line) == 0)
42 {
43 if (dwarf_decl_column (&die, &column) == 0)
44 printf ("%s@%s:%d:%d\n", name, file, line, column);
45 else
46 printf ("%s@%s:%d\n", name, file, line);
47 }
48 else
49 printf ("%s@%s\n", name, file);
50 child_indent++;
51 }
52
53 Dwarf_Die child;
54 if (dwarf_child (&die, &child) == 0)
55 walk_tree (&child, child_indent);
56 }
57 while (dwarf_siblingof (&die, &die) == 0);
58 }
59
60 int
main(int argc,char * argv[])61 main (int argc, char *argv[])
62 {
63 for (int i = 1; i < argc; i++)
64 {
65 printf ("file: %s\n", argv[i]);
66 int fd = open (argv[i], O_RDONLY);
67 Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
68 if (dbg == NULL)
69 {
70 printf ("%s not usable: %s\n", argv[i], dwarf_errmsg (-1));
71 return -1;
72 }
73
74 Dwarf_CU *cu = NULL;
75 Dwarf_Die cudie, subdie;
76 uint8_t unit_type;
77 while (dwarf_get_units (dbg, cu, &cu, NULL, &unit_type, &cudie, &subdie)
78 == 0)
79 {
80 Dwarf_Die *die = unit_type == DW_UT_skeleton ? &subdie : &cudie;
81 printf (" cu: %s\n", dwarf_diename (die) ?: "???");
82 walk_tree (die, 2);
83 }
84
85 dwarf_end (dbg);
86 close (fd);
87 }
88
89 return 0;
90 }
91