• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
getfiles(Dwarf_Die * die,Dwarf_Files ** files)41 getfiles (Dwarf_Die *die, Dwarf_Files **files)
42 {
43   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
getattr(Dwarf_Die * die,int search_name,Dwarf_Word * value)48 getattr (Dwarf_Die *die, int search_name, Dwarf_Word *value)
49 {
50   Dwarf_Attribute attr_mem;
51   return INTUSE(dwarf_formudata) (INTUSE(dwarf_attr) (die, search_name,
52 						      &attr_mem), value);
53 }
54 
55 static inline int
file_matches(const char * lastfile,size_t match_file_len,const char * match_file,Dwarf_Files * files,size_t idx,bool * lastfile_matches)56 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   if (idx >= files->nfiles)
62     return false;
63   const char *file = files->info[idx].name;
64   if (file != lastfile)
65     {
66       size_t len = strlen (file);
67       *lastfile_matches = (len >= match_file_len
68                           && !memcmp (match_file, file, match_file_len)
69                           && (len == match_file_len
70                               || file[len - match_file_len - 1] == '/'));
71     }
72   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
dwarf_getscopevar(Dwarf_Die * scopes,int nscopes,const char * name,int skip_shadows,const char * match_file,int match_lineno,int match_linecol,Dwarf_Die * result)86 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   size_t match_file_len = match_file == NULL ? 0 : strlen (match_file);
93   bool lastfile_matches = false;
94   const char *lastfile = NULL;
95 
96   /* Start with the innermost scope and move out.  */
97   for (int out = 0; out < nscopes; ++out)
98     if (INTUSE(dwarf_haschildren) (&scopes[out]))
99       {
100 	if (INTUSE(dwarf_child) (&scopes[out], result) != 0)
101 	  return -1;
102 	do
103 	  {
104 	    switch (INTUSE(dwarf_tag) (result))
105 	      {
106 	      case DW_TAG_variable:
107 	      case DW_TAG_formal_parameter:
108 		break;
109 
110 	      default:
111 		continue;
112 	      }
113 
114 	    /* Only get here for a variable or parameter.  Check the name.  */
115 	    const char *diename = INTUSE(dwarf_diename) (result);
116 	    if (diename != NULL && !strcmp (name, diename))
117 	      {
118 		/* We have a matching name.  */
119 
120 		if (skip_shadows > 0)
121 		  {
122 		    /* Punt this scope for the one it shadows.  */
123 		    --skip_shadows;
124 		    break;
125 		  }
126 
127 		if (match_file != NULL)
128 		  {
129 		    /* Check its decl_file.  */
130 
131 		    Dwarf_Word i;
132 		    Dwarf_Files *files;
133 		    if (getattr (result, DW_AT_decl_file, &i) != 0
134 			|| getfiles (&scopes[out], &files) != 0)
135 		      break;
136 
137 		    if (!file_matches (lastfile, match_file_len, match_file,
138 		                       files, i, &lastfile_matches))
139 		      break;
140 
141 		    if (match_lineno > 0
142 			&& (getattr (result, DW_AT_decl_line, &i) != 0
143 			    || (int) i != match_lineno))
144 		      break;
145 		    if (match_linecol > 0
146 			&& (getattr (result, DW_AT_decl_column, &i) != 0
147 			    || (int) i != match_linecol))
148 		      break;
149 		  }
150 
151 		/* We have a winner!  */
152 		return out;
153 	      }
154 	  }
155 	while (INTUSE(dwarf_siblingof) (result, result) == 0);
156       }
157 
158   return -2;
159 }
160