1 /* Helper functions to descend DWARF scope trees.
2 Copyright (C) 2005,2006,2007,2015 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 either
8 * the GNU Lesser General Public License as published by the Free
9 Software Foundation; either version 3 of the License, or (at
10 your option) any later version
14 * the GNU General Public License as published by the Free
15 Software Foundation; either version 2 of the License, or (at
16 your option) any later version
18 or both in parallel, as here.
20 elfutils is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received copies of the GNU General Public License and
26 the GNU Lesser General Public License along with this program. If
27 not, see <http://www.gnu.org/licenses/>. */
38 may_have_scopes (Dwarf_Die *die)
40 switch (INTUSE(dwarf_tag) (die))
42 /* DIEs with addresses we can try to match. */
43 case DW_TAG_compile_unit:
45 case DW_TAG_lexical_block:
46 case DW_TAG_with_stmt:
47 case DW_TAG_catch_block:
48 case DW_TAG_try_block:
49 case DW_TAG_entry_point:
50 case DW_TAG_inlined_subroutine:
51 case DW_TAG_subprogram:
54 /* DIEs without addresses that can own DIEs with addresses. */
55 case DW_TAG_namespace:
56 case DW_TAG_class_type:
57 case DW_TAG_structure_type:
60 /* Other DIEs we have no reason to descend. */
67 struct walk_children_state
69 /* Parameters of __libdw_visit_scopes. */
71 struct Dwarf_Die_Chain *imports;
72 int (*previsit) (unsigned int depth, struct Dwarf_Die_Chain *, void *);
73 int (*postvisit) (unsigned int depth, struct Dwarf_Die_Chain *, void *);
75 /* Extra local variables for the walker. */
76 struct Dwarf_Die_Chain child;
80 walk_children (struct walk_children_state *state);
84 __libdw_visit_scopes (unsigned int depth, struct Dwarf_Die_Chain *root,
85 struct Dwarf_Die_Chain *imports,
86 int (*previsit) (unsigned int,
87 struct Dwarf_Die_Chain *,
89 int (*postvisit) (unsigned int,
90 struct Dwarf_Die_Chain *,
94 struct walk_children_state state =
99 .postvisit = postvisit,
103 state.child.parent = root;
105 if ((ret = INTUSE(dwarf_child) (&root->die, &state.child.die)) != 0)
106 return ret < 0 ? -1 : 0; // Having zero children is legal.
108 return walk_children (&state);
112 walk_children (struct walk_children_state *state)
117 /* For an imported unit, it is logically as if the children of
118 that unit are siblings of the other children. So don't do
119 a full recursion into the imported unit, but just walk the
120 children in place before moving to the next real child. */
121 while (INTUSE(dwarf_tag) (&state->child.die) == DW_TAG_imported_unit)
123 Dwarf_Die orig_child_die = state->child.die;
124 Dwarf_Attribute attr_mem;
125 Dwarf_Attribute *attr = INTUSE(dwarf_attr) (&state->child.die,
128 if (INTUSE(dwarf_formref_die) (attr, &state->child.die) != NULL
129 && INTUSE(dwarf_child) (&state->child.die, &state->child.die) == 0)
131 /* Checks the given DIE hasn't been imported yet
132 to prevent cycles. */
133 bool imported = false;
134 for (struct Dwarf_Die_Chain *import = state->imports; import != NULL;
135 import = import->parent)
136 if (import->die.addr == orig_child_die.addr)
143 __libdw_seterrno (DWARF_E_INVALID_DWARF);
146 struct Dwarf_Die_Chain *orig_imports = state->imports;
147 struct Dwarf_Die_Chain import = { .die = orig_child_die,
148 .parent = orig_imports };
149 state->imports = &import;
150 int result = walk_children (state);
151 state->imports = orig_imports;
152 if (result != DWARF_CB_OK)
156 /* Any "real" children left? */
157 if ((ret = INTUSE(dwarf_siblingof) (&orig_child_die,
158 &state->child.die)) != 0)
159 return ret < 0 ? -1 : 0;
162 state->child.prune = false;
164 /* previsit is declared NN */
165 int result = (*state->previsit) (state->depth + 1, &state->child, state->arg);
166 if (result != DWARF_CB_OK)
169 if (!state->child.prune && may_have_scopes (&state->child.die)
170 && INTUSE(dwarf_haschildren) (&state->child.die))
172 result = __libdw_visit_scopes (state->depth + 1, &state->child, state->imports,
173 state->previsit, state->postvisit, state->arg);
174 if (result != DWARF_CB_OK)
178 if (state->postvisit != NULL)
180 result = (*state->postvisit) (state->depth + 1, &state->child, state->arg);
181 if (result != DWARF_CB_OK)
185 while ((ret = INTUSE(dwarf_siblingof) (&state->child.die, &state->child.die)) == 0);
187 return ret < 0 ? -1 : 0;