1 /* Find matching source locations in a module. 2 Copyright (C) 2005 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 any Open Source Initiative certified open source license 21 (http://www.opensource.org/licenses/index.php) which requires the 22 distribution of source code with any binary distribution and to 23 distribute linked combinations of the two. Non-GPL Code permitted under 24 this exception must only link to the code of Red Hat elfutils through 25 those well defined interfaces identified in the file named EXCEPTION 26 found in the source code files (the "Approved Interfaces"). The files 27 of Non-GPL Code may instantiate templates or use macros or inline 28 functions from the Approved Interfaces without causing the resulting 29 work to be covered by the GNU General Public License. Only Red Hat, 30 Inc. may make changes or additions to the list of Approved Interfaces. 31 Red Hat's grant of this exception is conditioned upon your not adding 32 any new exceptions. If you wish to add a new Approved Interface or 33 exception, please contact Red Hat. You must obey the GNU General Public 34 License in all respects for all of the Red Hat elfutils code and other 35 code used in conjunction with Red Hat elfutils except the Non-GPL Code 36 covered by this exception. If you modify this file, you may extend this 37 exception to your version of the file, but you are not obligated to do 38 so. If you do not wish to provide this exception without modification, 39 you must delete this exception statement from your version and license 40 this file solely under the GPL without exception. 41 42 Red Hat elfutils is an included package of the Open Invention Network. 43 An included package of the Open Invention Network is a package for which 44 Open Invention Network licensees cross-license their patents. No patent 45 license is granted, either expressly or impliedly, by designation as an 46 included package. Should you wish to participate in the Open Invention 47 Network licensing program, please visit www.openinventionnetwork.com 48 <http://www.openinventionnetwork.com>. */ 49 50 #include "libdwflP.h" 51 #include "../libdw/libdwP.h" 52 53 int 54 dwfl_module_getsrc_file (Dwfl_Module *mod, 55 const char *fname, int lineno, int column, 56 Dwfl_Line ***srcsp, size_t *nsrcs) 57 { 58 if (mod == NULL) 59 return -1; 60 61 if (mod->dw == NULL) 62 { 63 Dwarf_Addr bias; 64 if (INTUSE(dwfl_module_getdwarf) (mod, &bias) == NULL) 65 return -1; 66 } 67 68 bool is_basename = strchr (fname, '/') == NULL; 69 70 size_t max_match = *nsrcs ?: ~0u; 71 size_t act_match = *nsrcs; 72 size_t cur_match = 0; 73 Dwfl_Line **match = *nsrcs == 0 ? NULL : *srcsp; 74 75 struct dwfl_cu *cu = NULL; 76 Dwfl_Error error; 77 while ((error = __libdwfl_nextcu (mod, cu, &cu)) == DWFL_E_NOERROR 78 && cu != NULL 79 && (error = __libdwfl_cu_getsrclines (cu)) == DWFL_E_NOERROR) 80 { 81 inline const char *INTUSE(dwarf_line_file) (const Dwarf_Line *line) 82 { 83 return line->files->info[line->file].name; 84 } 85 inline Dwarf_Line *dwfl_line (const Dwfl_Line *line) 86 { 87 return &dwfl_linecu (line)->die.cu->lines->info[line->idx]; 88 } 89 inline const char *dwfl_line_file (const Dwfl_Line *line) 90 { 91 return INTUSE(dwarf_line_file) (dwfl_line (line)); 92 } 93 94 /* Search through all the line number records for a matching 95 file and line/column number. If any of the numbers is zero, 96 no match is performed. */ 97 const char *lastfile = NULL; 98 bool lastmatch = false; 99 for (size_t cnt = 0; cnt < cu->die.cu->lines->nlines; ++cnt) 100 { 101 Dwarf_Line *line = &cu->die.cu->lines->info[cnt]; 102 103 if (unlikely (line->file >= line->files->nfiles)) 104 { 105 __libdwfl_seterrno (DWFL_E (LIBDW, DWARF_E_INVALID_DWARF)); 106 return -1; 107 } 108 else 109 { 110 const char *file = INTUSE(dwarf_line_file) (line); 111 if (file != lastfile) 112 { 113 /* Match the name with the name the user provided. */ 114 lastfile = file; 115 lastmatch = !strcmp (is_basename ? basename (file) : file, 116 fname); 117 } 118 } 119 if (!lastmatch) 120 continue; 121 122 /* See whether line and possibly column match. */ 123 if (lineno != 0 124 && (lineno > line->line 125 || (column != 0 && column > line->column))) 126 /* Cannot match. */ 127 continue; 128 129 /* Determine whether this is the best match so far. */ 130 size_t inner; 131 for (inner = 0; inner < cur_match; ++inner) 132 if (dwfl_line_file (match[inner]) 133 == INTUSE(dwarf_line_file) (line)) 134 break; 135 if (inner < cur_match 136 && (dwfl_line (match[inner])->line != line->line 137 || dwfl_line (match[inner])->line != lineno 138 || (column != 0 139 && (dwfl_line (match[inner])->column != line->column 140 || dwfl_line (match[inner])->column != column)))) 141 { 142 /* We know about this file already. If this is a better 143 match for the line number, use it. */ 144 if (dwfl_line (match[inner])->line >= line->line 145 && (dwfl_line (match[inner])->line != line->line 146 || dwfl_line (match[inner])->column >= line->column)) 147 /* Use the new line. Otherwise the old one. */ 148 match[inner] = &cu->lines->idx[cnt]; 149 continue; 150 } 151 152 if (cur_match < max_match) 153 { 154 if (cur_match == act_match) 155 { 156 /* Enlarge the array for the results. */ 157 act_match += 10; 158 Dwfl_Line **newp = realloc (match, 159 act_match 160 * sizeof (Dwfl_Line *)); 161 if (newp == NULL) 162 { 163 free (match); 164 __libdwfl_seterrno (DWFL_E_NOMEM); 165 return -1; 166 } 167 match = newp; 168 } 169 170 match[cur_match++] = &cu->lines->idx[cnt]; 171 } 172 } 173 } 174 175 if (cur_match > 0) 176 { 177 assert (*nsrcs == 0 || *srcsp == match); 178 179 *nsrcs = cur_match; 180 *srcsp = match; 181 182 return 0; 183 } 184 185 __libdwfl_seterrno (DWFL_E_NO_MATCH); 186 return -1; 187 } 188