1*7304104dSAndroid Build Coastguard Worker /* Test program for dwarf_ranges
2*7304104dSAndroid Build Coastguard Worker Copyright (C) 2015, 2018 Red Hat, Inc.
3*7304104dSAndroid Build Coastguard Worker This file is part of elfutils.
4*7304104dSAndroid Build Coastguard Worker
5*7304104dSAndroid Build Coastguard Worker This file is free software; you can redistribute it and/or modify
6*7304104dSAndroid Build Coastguard Worker it under the terms of the GNU General Public License as published by
7*7304104dSAndroid Build Coastguard Worker the Free Software Foundation; either version 3 of the License, or
8*7304104dSAndroid Build Coastguard Worker (at your option) any later version.
9*7304104dSAndroid Build Coastguard Worker
10*7304104dSAndroid Build Coastguard Worker elfutils is distributed in the hope that it will be useful, but
11*7304104dSAndroid Build Coastguard Worker WITHOUT ANY WARRANTY; without even the implied warranty of
12*7304104dSAndroid Build Coastguard Worker MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*7304104dSAndroid Build Coastguard Worker GNU General Public License for more details.
14*7304104dSAndroid Build Coastguard Worker
15*7304104dSAndroid Build Coastguard Worker You should have received a copy of the GNU General Public License
16*7304104dSAndroid Build Coastguard Worker along with this program. If not, see <http://www.gnu.org/licenses/>. */
17*7304104dSAndroid Build Coastguard Worker
18*7304104dSAndroid Build Coastguard Worker #include <config.h>
19*7304104dSAndroid Build Coastguard Worker #include ELFUTILS_HEADER(dw)
20*7304104dSAndroid Build Coastguard Worker #include <dwarf.h>
21*7304104dSAndroid Build Coastguard Worker #include <sys/types.h>
22*7304104dSAndroid Build Coastguard Worker #include <sys/stat.h>
23*7304104dSAndroid Build Coastguard Worker #include <fcntl.h>
24*7304104dSAndroid Build Coastguard Worker #include <stdio.h>
25*7304104dSAndroid Build Coastguard Worker #include <stdint.h>
26*7304104dSAndroid Build Coastguard Worker #include <stdlib.h>
27*7304104dSAndroid Build Coastguard Worker #include <assert.h>
28*7304104dSAndroid Build Coastguard Worker #include <inttypes.h>
29*7304104dSAndroid Build Coastguard Worker
30*7304104dSAndroid Build Coastguard Worker static void
ranges_die(Dwarf_Die * die)31*7304104dSAndroid Build Coastguard Worker ranges_die (Dwarf_Die *die)
32*7304104dSAndroid Build Coastguard Worker {
33*7304104dSAndroid Build Coastguard Worker Dwarf_Addr base, start, end;
34*7304104dSAndroid Build Coastguard Worker int ranges = dwarf_ranges (die, 0, &base, &start, &end);
35*7304104dSAndroid Build Coastguard Worker if (ranges < 0)
36*7304104dSAndroid Build Coastguard Worker puts (dwarf_errmsg (-1));
37*7304104dSAndroid Build Coastguard Worker else if (ranges > 0)
38*7304104dSAndroid Build Coastguard Worker {
39*7304104dSAndroid Build Coastguard Worker printf ("die: %s (%x)\n", dwarf_diename (die) ?: "<unknown>",
40*7304104dSAndroid Build Coastguard Worker dwarf_tag (die));
41*7304104dSAndroid Build Coastguard Worker for (ptrdiff_t off = 0;
42*7304104dSAndroid Build Coastguard Worker (off = dwarf_ranges (die, off, &base, &start, &end)); )
43*7304104dSAndroid Build Coastguard Worker if (off == -1)
44*7304104dSAndroid Build Coastguard Worker {
45*7304104dSAndroid Build Coastguard Worker puts (dwarf_errmsg (-1));
46*7304104dSAndroid Build Coastguard Worker break;
47*7304104dSAndroid Build Coastguard Worker }
48*7304104dSAndroid Build Coastguard Worker else
49*7304104dSAndroid Build Coastguard Worker printf (" %"PRIx64"..%"PRIx64"\n", start, end);
50*7304104dSAndroid Build Coastguard Worker printf ("\n");
51*7304104dSAndroid Build Coastguard Worker }
52*7304104dSAndroid Build Coastguard Worker }
53*7304104dSAndroid Build Coastguard Worker
54*7304104dSAndroid Build Coastguard Worker static void
walk_tree(Dwarf_Die * dwarf_die)55*7304104dSAndroid Build Coastguard Worker walk_tree (Dwarf_Die *dwarf_die)
56*7304104dSAndroid Build Coastguard Worker {
57*7304104dSAndroid Build Coastguard Worker Dwarf_Die die = *dwarf_die;
58*7304104dSAndroid Build Coastguard Worker do
59*7304104dSAndroid Build Coastguard Worker {
60*7304104dSAndroid Build Coastguard Worker Dwarf_Die child;
61*7304104dSAndroid Build Coastguard Worker ranges_die (&die);
62*7304104dSAndroid Build Coastguard Worker if (dwarf_child (&die, &child) == 0)
63*7304104dSAndroid Build Coastguard Worker walk_tree (&child);
64*7304104dSAndroid Build Coastguard Worker }
65*7304104dSAndroid Build Coastguard Worker while (dwarf_siblingof (&die, &die) == 0);
66*7304104dSAndroid Build Coastguard Worker }
67*7304104dSAndroid Build Coastguard Worker
68*7304104dSAndroid Build Coastguard Worker int
main(int argc,char * argv[])69*7304104dSAndroid Build Coastguard Worker main (int argc, char *argv[])
70*7304104dSAndroid Build Coastguard Worker {
71*7304104dSAndroid Build Coastguard Worker assert (argc >= 2);
72*7304104dSAndroid Build Coastguard Worker const char *name = argv[1];
73*7304104dSAndroid Build Coastguard Worker
74*7304104dSAndroid Build Coastguard Worker int fd = open (name, O_RDONLY);
75*7304104dSAndroid Build Coastguard Worker Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
76*7304104dSAndroid Build Coastguard Worker
77*7304104dSAndroid Build Coastguard Worker Dwarf_CU *cu = NULL;
78*7304104dSAndroid Build Coastguard Worker Dwarf_Die cudie, subdie;
79*7304104dSAndroid Build Coastguard Worker uint8_t unit_type;
80*7304104dSAndroid Build Coastguard Worker while (dwarf_get_units (dbg, cu, &cu, NULL,
81*7304104dSAndroid Build Coastguard Worker &unit_type, &cudie, &subdie) == 0)
82*7304104dSAndroid Build Coastguard Worker {
83*7304104dSAndroid Build Coastguard Worker Dwarf_Die die = (unit_type == DW_UT_skeleton
84*7304104dSAndroid Build Coastguard Worker ? subdie : cudie);
85*7304104dSAndroid Build Coastguard Worker walk_tree (&die);
86*7304104dSAndroid Build Coastguard Worker }
87*7304104dSAndroid Build Coastguard Worker dwarf_end (dbg);
88*7304104dSAndroid Build Coastguard Worker
89*7304104dSAndroid Build Coastguard Worker return 0;
90*7304104dSAndroid Build Coastguard Worker }
91