487375dcc4a6c03aa2d0617c314a67c809332f60
[platform/upstream/elfutils.git] / libdw / libdw_visit_scopes.c
1 /* Helper functions to descend DWARF scope trees.
2    Copyright (C) 2005,2006,2007 Red Hat, Inc.
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 either
7
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
11
12    or
13
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
17
18    or both in parallel, as here.
19
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.
24
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/>.  */
28
29 #ifdef HAVE_CONFIG_H
30 # include <config.h>
31 #endif
32
33 #include "libdwP.h"
34 #include <dwarf.h>
35
36
37 static bool
38 may_have_scopes (Dwarf_Die *die)
39 {
40   switch (INTUSE(dwarf_tag) (die))
41     {
42       /* DIEs with addresses we can try to match.  */
43     case DW_TAG_compile_unit:
44     case DW_TAG_module:
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:
52       return true;
53
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:
58       return true;
59
60       /* Other DIEs we have no reason to descend.  */
61     default:
62       break;
63     }
64   return false;
65 }
66
67 int
68 __libdw_visit_scopes (depth, root, previsit, postvisit, arg)
69      unsigned int depth;
70      struct Dwarf_Die_Chain *root;
71      int (*previsit) (unsigned int depth, struct Dwarf_Die_Chain *, void *);
72      int (*postvisit) (unsigned int depth, struct Dwarf_Die_Chain *, void *);
73      void *arg;
74 {
75   struct Dwarf_Die_Chain child;
76   int ret;
77
78   child.parent = root;
79   if ((ret = INTUSE(dwarf_child) (&root->die, &child.die)) != 0)
80     return ret < 0 ? -1 : 0; // Having zero children is legal.
81
82   inline int recurse (void)
83     {
84       return __libdw_visit_scopes (depth + 1, &child,
85                                    previsit, postvisit, arg);
86     }
87
88   inline int walk_children ()
89   {
90     do
91       {
92         /* For an imported unit, it is logically as if the children of
93            that unit are siblings of the other children.  So don't do
94            a full recursion into the imported unit, but just walk the
95            children in place before moving to the next real child.  */
96         while (INTUSE(dwarf_tag) (&child.die) == DW_TAG_imported_unit)
97           {
98             Dwarf_Die orig_child_die = child.die;
99             Dwarf_Attribute attr_mem;
100             Dwarf_Attribute *attr = INTUSE(dwarf_attr) (&child.die,
101                                                         DW_AT_import,
102                                                         &attr_mem);
103             if (INTUSE(dwarf_formref_die) (attr, &child.die) != NULL
104                 && INTUSE(dwarf_child) (&child.die, &child.die) == 0)
105               {
106                 int result = walk_children ();
107                 if (result != DWARF_CB_OK)
108                   return result;
109               }
110
111             /* Any "real" children left?  */
112             if ((ret = INTUSE(dwarf_siblingof) (&orig_child_die,
113                                                 &child.die)) != 0)
114               return ret < 0 ? -1 : 0;
115           };
116
117         child.prune = false;
118
119         if (previsit != NULL)
120           {
121             int result = (*previsit) (depth + 1, &child, arg);
122             if (result != DWARF_CB_OK)
123               return result;
124           }
125
126         if (!child.prune && may_have_scopes (&child.die)
127             && INTUSE(dwarf_haschildren) (&child.die))
128           {
129             int result = recurse ();
130             if (result != DWARF_CB_OK)
131               return result;
132           }
133
134         if (postvisit != NULL)
135           {
136             int result = (*postvisit) (depth + 1, &child, arg);
137             if (result != DWARF_CB_OK)
138               return result;
139           }
140       }
141     while ((ret = INTUSE(dwarf_siblingof) (&child.die, &child.die)) == 0);
142
143     return ret < 0 ? -1 : 0;
144   }
145
146   return walk_children ();
147 }