1/* Helper functions to descend DWARF scope trees. 2 Copyright (C) 2005,2006,2007 Red Hat, Inc. 3 This file is part of Red Hat elfutils. 4 5 Red Hat elfutils is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by the 7 Free Software Foundation; version 2 of the License. 8 9 Red Hat elfutils is distributed in the hope that it will be useful, but 10 WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 General Public License for more details. 13 14 You should have received a copy of the GNU General Public License along 15 with Red Hat elfutils; if not, write to the Free Software Foundation, 16 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. 17 18 In addition, as a special exception, Red Hat, Inc. gives You the 19 additional right to link the code of Red Hat elfutils with code licensed 20 under an Open Source Initiative certified open source license 21 (http://www.opensource.org/licenses/index.php) and to distribute linked 22 combinations including the two. Non-GPL Code permitted under this 23 exception must only link to the code of Red Hat elfutils through those 24 well defined interfaces identified in the file named EXCEPTION found in 25 the source code files (the "Approved Interfaces"). The files of Non-GPL 26 Code may instantiate templates or use macros or inline functions from 27 the Approved Interfaces without causing the resulting work to be covered 28 by the GNU General Public License. Only Red Hat, Inc. may make changes 29 or additions to the list of Approved Interfaces. Red Hat's grant of 30 this exception is conditioned upon your not adding any new exceptions. 31 If you wish to add a new Approved Interface or exception, please contact 32 Red Hat. You must obey the GNU General Public License in all respects 33 for all of the Red Hat elfutils code and other code used in conjunction 34 with Red Hat elfutils except the Non-GPL Code covered by this exception. 35 If you modify this file, you may extend this exception to your version 36 of the file, but you are not obligated to do so. If you do not wish to 37 provide this exception without modification, you must delete this 38 exception statement from your version and license this file solely under 39 the GPL without exception. 40 41 Red Hat elfutils is an included package of the Open Invention Network. 42 An included package of the Open Invention Network is a package for which 43 Open Invention Network licensees cross-license their patents. No patent 44 license is granted, either expressly or impliedly, by designation as an 45 included package. Should you wish to participate in the Open Invention 46 Network licensing program, please visit www.openinventionnetwork.com 47 <http://www.openinventionnetwork.com>. */ 48 49#ifdef HAVE_CONFIG_H 50# include <config.h> 51#endif 52 53#include "libdwP.h" 54#include <dwarf.h> 55 56enum die_class { ignore, match, match_inline, walk, imported }; 57 58static enum die_class 59classify_die (Dwarf_Die *die) 60{ 61 switch (INTUSE(dwarf_tag) (die)) 62 { 63 /* DIEs with addresses we can try to match. */ 64 case DW_TAG_compile_unit: 65 case DW_TAG_module: 66 case DW_TAG_lexical_block: 67 case DW_TAG_with_stmt: 68 case DW_TAG_catch_block: 69 case DW_TAG_try_block: 70 case DW_TAG_entry_point: 71 return match; 72 case DW_TAG_inlined_subroutine: 73 return match_inline; 74 case DW_TAG_subprogram: 75 /* This might be a concrete out-of-line instance of an inline, in 76 which case it is not guaranteed to be owned by the right scope and 77 we will search for its origin as for DW_TAG_inlined_subroutine. */ 78 return (INTUSE(dwarf_hasattr) (die, DW_AT_abstract_origin) 79 ? match_inline : match); 80 81 /* DIEs without addresses that can own DIEs with addresses. */ 82 case DW_TAG_namespace: 83 case DW_TAG_class_type: 84 case DW_TAG_structure_type: 85 return walk; 86 87 /* Special indirection required. */ 88 case DW_TAG_imported_unit: 89 return imported; 90 91 /* Other DIEs we have no reason to descend. */ 92 default: 93 break; 94 } 95 return ignore; 96} 97 98int 99__libdw_visit_scopes (depth, root, previsit, postvisit, arg) 100 unsigned int depth; 101 struct Dwarf_Die_Chain *root; 102 int (*previsit) (unsigned int depth, struct Dwarf_Die_Chain *, void *); 103 int (*postvisit) (unsigned int depth, struct Dwarf_Die_Chain *, void *); 104 void *arg; 105{ 106 struct Dwarf_Die_Chain child; 107 108 child.parent = root; 109 if (INTUSE(dwarf_child) (&root->die, &child.die) != 0) 110 return -1; 111 112 inline int recurse (void) 113 { 114 return __libdw_visit_scopes (depth + 1, &child, 115 previsit, postvisit, arg); 116 } 117 118 do 119 { 120 child.prune = false; 121 122 if (previsit != NULL) 123 { 124 int result = (*previsit) (depth + 1, &child, arg); 125 if (result != DWARF_CB_OK) 126 return result; 127 } 128 129 if (!child.prune) 130 switch (classify_die (&child.die)) 131 { 132 case match: 133 case match_inline: 134 case walk: 135 if (INTUSE(dwarf_haschildren) (&child.die)) 136 { 137 int result = recurse (); 138 if (result != DWARF_CB_OK) 139 return result; 140 } 141 break; 142 143 case imported: 144 { 145 /* This imports another compilation unit to appear 146 as part of this one, inside the current scope. 147 Recurse to search the referenced unit, but without 148 recording it as an inner scoping level. */ 149 150 Dwarf_Attribute attr_mem; 151 Dwarf_Attribute *attr = INTUSE(dwarf_attr) (&child.die, 152 DW_AT_import, 153 &attr_mem); 154 if (INTUSE(dwarf_formref_die) (attr, &child.die) != NULL) 155 { 156 int result = recurse (); 157 if (result != DWARF_CB_OK) 158 return result; 159 } 160 } 161 break; 162 163 default: 164 break; 165 } 166 167 if (postvisit != NULL) 168 { 169 int result = (*postvisit) (depth + 1, &child, arg); 170 if (result != DWARF_CB_OK) 171 return result; 172 } 173 } 174 while (INTUSE(dwarf_siblingof) (&child.die, &child.die) == 0); 175 176 return 0; 177} 178