Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1 | /* Find a named variable or parameter within given scopes. |
Roland McGrath | 08277ba | 2009-08-10 15:08:08 -0700 | [diff] [blame] | 2 | Copyright (C) 2005-2009 Red Hat, Inc. |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame^] | 3 | This file is part of elfutils. |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 4 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame^] | 5 | This file is free software; you can redistribute it and/or modify |
| 6 | it under the terms of either |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 7 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame^] | 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 |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 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 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame^] | 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/>. */ |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 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 | getfiles (Dwarf_Die *die, Dwarf_Files **files) |
| 42 | { |
Roland McGrath | 6724c90 | 2005-10-28 07:07:19 +0000 | [diff] [blame] | 43 | return INTUSE(dwarf_getsrcfiles) (&CUDIE (die->cu), files, NULL); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /* Fetch an attribute that should have a constant integer form. */ |
| 47 | static int |
| 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 | /* Search SCOPES[0..NSCOPES-1] for a variable called NAME. |
| 56 | Ignore the first SKIP_SHADOWS scopes that match the name. |
| 57 | If MATCH_FILE is not null, accept only declaration in that source file; |
| 58 | if MATCH_LINENO or MATCH_LINECOL are also nonzero, accept only declaration |
| 59 | at that line and column. |
| 60 | |
| 61 | If successful, fill in *RESULT with the DIE of the variable found, |
| 62 | and return N where SCOPES[N] is the scope defining the variable. |
| 63 | Return -1 for errors or -2 for no matching variable found. */ |
| 64 | |
| 65 | int |
| 66 | dwarf_getscopevar (Dwarf_Die *scopes, int nscopes, |
| 67 | const char *name, int skip_shadows, |
| 68 | const char *match_file, int match_lineno, int match_linecol, |
| 69 | Dwarf_Die *result) |
| 70 | { |
| 71 | /* Match against the given file name. */ |
| 72 | size_t match_file_len = match_file == NULL ? 0 : strlen (match_file); |
| 73 | bool lastfile_matches = false; |
| 74 | const char *lastfile = NULL; |
| 75 | inline bool file_matches (Dwarf_Files *files, size_t idx) |
| 76 | { |
| 77 | if (idx >= files->nfiles) |
| 78 | return false; |
| 79 | |
| 80 | const char *file = files->info[idx].name; |
| 81 | if (file != lastfile) |
| 82 | { |
| 83 | size_t len = strlen (file); |
| 84 | lastfile_matches = (len >= match_file_len |
| 85 | && !memcmp (match_file, file, match_file_len) |
| 86 | && (len == match_file_len |
| 87 | || file[len - match_file_len - 1] == '/')); |
| 88 | } |
| 89 | return lastfile_matches; |
| 90 | } |
| 91 | |
| 92 | /* Start with the innermost scope and move out. */ |
| 93 | for (int out = 0; out < nscopes; ++out) |
| 94 | if (INTUSE(dwarf_haschildren) (&scopes[out])) |
| 95 | { |
| 96 | if (INTUSE(dwarf_child) (&scopes[out], result) != 0) |
| 97 | return -1; |
| 98 | do |
| 99 | { |
| 100 | switch (INTUSE(dwarf_tag) (result)) |
| 101 | { |
| 102 | case DW_TAG_variable: |
| 103 | case DW_TAG_formal_parameter: |
| 104 | break; |
| 105 | |
| 106 | default: |
| 107 | continue; |
| 108 | } |
| 109 | |
| 110 | /* Only get here for a variable or parameter. Check the name. */ |
Roland McGrath | 08277ba | 2009-08-10 15:08:08 -0700 | [diff] [blame] | 111 | const char *diename = INTUSE(dwarf_diename) (result); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 112 | if (diename != NULL && !strcmp (name, diename)) |
| 113 | { |
| 114 | /* We have a matching name. */ |
| 115 | |
| 116 | if (skip_shadows > 0) |
| 117 | { |
| 118 | /* Punt this scope for the one it shadows. */ |
| 119 | --skip_shadows; |
| 120 | break; |
| 121 | } |
| 122 | |
| 123 | if (match_file != NULL) |
| 124 | { |
| 125 | /* Check its decl_file. */ |
| 126 | |
| 127 | Dwarf_Word i; |
| 128 | Dwarf_Files *files; |
| 129 | if (getattr (result, DW_AT_decl_file, &i) != 0 |
| 130 | || getfiles (&scopes[out], &files) != 0) |
| 131 | break; |
| 132 | |
| 133 | if (!file_matches (files, i)) |
| 134 | break; |
| 135 | |
| 136 | if (match_lineno > 0 |
| 137 | && (getattr (result, DW_AT_decl_line, &i) != 0 |
| 138 | || (int) i != match_lineno)) |
| 139 | break; |
| 140 | if (match_linecol > 0 |
| 141 | && (getattr (result, DW_AT_decl_column, &i) != 0 |
| 142 | || (int) i != match_linecol)) |
| 143 | break; |
| 144 | } |
| 145 | |
| 146 | /* We have a winner! */ |
| 147 | return out; |
| 148 | } |
| 149 | } |
| 150 | while (INTUSE(dwarf_siblingof) (result, result) == 0); |
| 151 | } |
| 152 | |
| 153 | return -2; |
| 154 | } |