1 /*-
2 * Copyright (c) 2010,2014 Kai Wang
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $Id: die_traverse2.c 3076 2014-06-23 23:54:01Z kaiwang27 $
27 */
28
29 #include <assert.h>
30 #include <dwarf.h>
31 #include <libdwarf.h>
32
33 #include "driver.h"
34
35 static int die_cnt;
36
37 /*
38 * DIE traverse function shared by test cases. (another version with
39 * .debug_types support)
40 */
41
42 static void
_die_traverse_recursive2(Dwarf_Debug dbg,Dwarf_Die die,Dwarf_Bool is_info,void (* die_callback)(Dwarf_Die die))43 _die_traverse_recursive2(Dwarf_Debug dbg, Dwarf_Die die,
44 Dwarf_Bool is_info, void (*die_callback)(Dwarf_Die die))
45 {
46 Dwarf_Die die0;
47 Dwarf_Off offset;
48 Dwarf_Half tag;
49 Dwarf_Error de;
50 const char *tagname;
51 int r;
52
53 assert(dbg != NULL && die != NULL && die_callback != NULL);
54
55 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
56 tet_printf("dwarf_tag failed: %s\n", dwarf_errmsg(de));
57 result = TET_FAIL;
58 }
59 tagname = NULL;
60 if (dwarf_get_TAG_name(tag, &tagname) != DW_DLV_OK) {
61 tet_infoline("dwarf_get_TAG_name failed");
62 result = TET_FAIL;
63 }
64 offset = 0;
65 if (dwarf_dieoffset(die, &offset, &de) != DW_DLV_OK) {
66 tet_printf("dwarf_dieoffset failed: %s\n", dwarf_errmsg(de));
67 result = TET_FAIL;
68 }
69 tet_printf("DIE #%d (%s) [%#x]\n", die_cnt++, tagname, offset);
70
71 die_callback(die);
72
73 /* Search children. */
74 r = dwarf_child(die, &die0, &de);
75 if (r == DW_DLV_ERROR)
76 tet_printf("%s: dwarf_child failed: %s", __func__,
77 dwarf_errmsg(de));
78 else if (r == DW_DLV_OK)
79 _die_traverse_recursive2(dbg, die0, is_info, die_callback);
80
81 /* Search sibling. */
82 r = dwarf_siblingof_b(dbg, die, &die0, is_info, &de);
83 if (r == DW_DLV_ERROR)
84 tet_printf("%s: dwarf_siblingof failed: %s", __func__,
85 dwarf_errmsg(de));
86 else if (r == DW_DLV_OK)
87 _die_traverse_recursive2(dbg, die0, is_info, die_callback);
88 }
89
90 static void
_die_traverse2(Dwarf_Debug dbg,Dwarf_Bool is_info,void (* die_callback)(Dwarf_Die die))91 _die_traverse2(Dwarf_Debug dbg, Dwarf_Bool is_info,
92 void (*die_callback)(Dwarf_Die die))
93 {
94 Dwarf_Die die;
95 Dwarf_Error de;
96 Dwarf_Unsigned cu_next_offset;
97
98 assert(dbg != NULL && die_callback != NULL);
99
100 die_cnt = 0;
101
102 if (is_info) {
103 TS_DWARF_CU_FOREACH2(dbg, 1, cu_next_offset, de) {
104 if (dwarf_siblingof_b(dbg, NULL, &die, 1, &de) !=
105 DW_DLV_OK)
106 break;
107 _die_traverse_recursive2(dbg, die, 1, die_callback);
108 }
109 } else {
110 do {
111 TS_DWARF_CU_FOREACH2(dbg, 0, cu_next_offset, de) {
112 if (dwarf_siblingof_b(dbg, NULL, &die, 0,
113 &de) != DW_DLV_OK)
114 break;
115 _die_traverse_recursive2(dbg, die, 0,
116 die_callback);
117 }
118 } while (dwarf_next_types_section(dbg, &de) == DW_DLV_OK);
119 }
120 }
121