Branch data Line data Source code
1 : : /* Find a named variable or parameter within given scopes.
2 : : Copyright (C) 2005-2009 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 <stdbool.h>
34 : : #include <string.h>
35 : : #include "libdwP.h"
36 : : #include <dwarf.h>
37 : :
38 : :
39 : : /* Find the containing CU's files. */
40 : : static int
41 : 0 : getfiles (Dwarf_Die *die, Dwarf_Files **files)
42 : : {
43 : 0 : return INTUSE(dwarf_getsrcfiles) (&CUDIE (die->cu), files, NULL);
44 : : }
45 : :
46 : : /* Fetch an attribute that should have a constant integer form. */
47 : : static int
48 : 0 : getattr (Dwarf_Die *die, int search_name, Dwarf_Word *value)
49 : : {
50 : 0 : Dwarf_Attribute attr_mem;
51 : 0 : return INTUSE(dwarf_formudata) (INTUSE(dwarf_attr) (die, search_name,
52 : : &attr_mem), value);
53 : : }
54 : :
55 : : static inline int
56 : 0 : file_matches (const char *lastfile,
57 : : size_t match_file_len, const char *match_file,
58 : : Dwarf_Files *files, size_t idx,
59 : : bool *lastfile_matches)
60 : : {
61 [ # # ]: 0 : if (idx >= files->nfiles)
62 : : return false;
63 : 0 : const char *file = files->info[idx].name;
64 [ # # ]: 0 : if (file != lastfile)
65 : : {
66 : 0 : size_t len = strlen (file);
67 : 0 : *lastfile_matches = (len >= match_file_len
68 [ # # ]: 0 : && !memcmp (match_file, file, match_file_len)
69 [ # # # # ]: 0 : && (len == match_file_len
70 [ # # ]: 0 : || file[len - match_file_len - 1] == '/'));
71 : : }
72 : 0 : return *lastfile_matches;
73 : : }
74 : :
75 : : /* Search SCOPES[0..NSCOPES-1] for a variable called NAME.
76 : : Ignore the first SKIP_SHADOWS scopes that match the name.
77 : : If MATCH_FILE is not null, accept only declaration in that source file;
78 : : if MATCH_LINENO or MATCH_LINECOL are also nonzero, accept only declaration
79 : : at that line and column.
80 : :
81 : : If successful, fill in *RESULT with the DIE of the variable found,
82 : : and return N where SCOPES[N] is the scope defining the variable.
83 : : Return -1 for errors or -2 for no matching variable found. */
84 : :
85 : : int
86 : 0 : dwarf_getscopevar (Dwarf_Die *scopes, int nscopes,
87 : : const char *name, int skip_shadows,
88 : : const char *match_file, int match_lineno, int match_linecol,
89 : : Dwarf_Die *result)
90 : : {
91 : : /* Match against the given file name. */
92 [ # # ]: 0 : size_t match_file_len = match_file == NULL ? 0 : strlen (match_file);
93 : 0 : bool lastfile_matches = false;
94 : 0 : const char *lastfile = NULL;
95 : :
96 : : /* Start with the innermost scope and move out. */
97 [ # # ]: 0 : for (int out = 0; out < nscopes; ++out)
98 [ # # ]: 0 : if (INTUSE(dwarf_haschildren) (&scopes[out]))
99 : : {
100 [ # # ]: 0 : if (INTUSE(dwarf_child) (&scopes[out], result) != 0)
101 : : return -1;
102 : 0 : do
103 : : {
104 [ # # ]: 0 : switch (INTUSE(dwarf_tag) (result))
105 : : {
106 : : case DW_TAG_variable:
107 : : case DW_TAG_formal_parameter:
108 : 0 : break;
109 : :
110 : 0 : default:
111 : 0 : continue;
112 : : }
113 : :
114 : : /* Only get here for a variable or parameter. Check the name. */
115 : 0 : const char *diename = INTUSE(dwarf_diename) (result);
116 [ # # # # ]: 0 : if (diename != NULL && !strcmp (name, diename))
117 : : {
118 : : /* We have a matching name. */
119 : :
120 [ # # ]: 0 : if (skip_shadows > 0)
121 : : {
122 : : /* Punt this scope for the one it shadows. */
123 : 0 : --skip_shadows;
124 : 0 : break;
125 : : }
126 : :
127 [ # # ]: 0 : if (match_file != NULL)
128 : : {
129 : : /* Check its decl_file. */
130 : :
131 : 0 : Dwarf_Word i;
132 : 0 : Dwarf_Files *files;
133 [ # # ]: 0 : if (getattr (result, DW_AT_decl_file, &i) != 0
134 [ # # ]: 0 : || getfiles (&scopes[out], &files) != 0)
135 : : break;
136 : :
137 [ # # ]: 0 : if (!file_matches (lastfile, match_file_len, match_file,
138 : : files, i, &lastfile_matches))
139 : : break;
140 : :
141 [ # # ]: 0 : if (match_lineno > 0
142 [ # # ]: 0 : && (getattr (result, DW_AT_decl_line, &i) != 0
143 [ # # ]: 0 : || (int) i != match_lineno))
144 : : break;
145 [ # # ]: 0 : if (match_linecol > 0
146 [ # # ]: 0 : && (getattr (result, DW_AT_decl_column, &i) != 0
147 [ # # ]: 0 : || (int) i != match_linecol))
148 : : break;
149 : : }
150 : :
151 : : /* We have a winner! */
152 : 0 : return out;
153 : : }
154 : : }
155 [ # # ]: 0 : while (INTUSE(dwarf_siblingof) (result, result) == 0);
156 : : }
157 : :
158 : : return -2;
159 : : }
|