1 /* Find matching source locations in a module. 2 Copyright (C) 2005 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 #include "libdwflP.h" 30 #include "../libdw/libdwP.h" 31 32 33 static inline const char * 34 dwfl_dwarf_line_file (const Dwarf_Line *line) 35 { 36 return line->files->info[line->file].name; 37 } 38 39 static inline Dwarf_Line * 40 dwfl_line (const Dwfl_Line *line) 41 { 42 return &dwfl_linecu (line)->die.cu->lines->info[line->idx]; 43 } 44 45 static inline const char * 46 dwfl_line_file (const Dwfl_Line *line) 47 { 48 return dwfl_dwarf_line_file (dwfl_line (line)); 49 } 50 51 int 52 dwfl_module_getsrc_file (Dwfl_Module *mod, 53 const char *fname, int lineno, int column, 54 Dwfl_Line ***srcsp, size_t *nsrcs) 55 { 56 if (mod == NULL) 57 return -1; 58 59 if (mod->dw == NULL) 60 { 61 Dwarf_Addr bias; 62 if (INTUSE(dwfl_module_getdwarf) (mod, &bias) == NULL) 63 return -1; 64 } 65 66 bool is_basename = strchr (fname, '/') == NULL; 67 68 size_t max_match = *nsrcs ?: ~0u; 69 size_t act_match = *nsrcs; 70 size_t cur_match = 0; 71 Dwfl_Line **match = *nsrcs == 0 ? NULL : *srcsp; 72 73 struct dwfl_cu *cu = NULL; 74 Dwfl_Error error; 75 while ((error = __libdwfl_nextcu (mod, cu, &cu)) == DWFL_E_NOERROR 76 && cu != NULL 77 && (error = __libdwfl_cu_getsrclines (cu)) == DWFL_E_NOERROR) 78 { 79 /* Search through all the line number records for a matching 80 file and line/column number. If any of the numbers is zero, 81 no match is performed. */ 82 const char *lastfile = NULL; 83 bool lastmatch = false; 84 for (size_t cnt = 0; cnt < cu->die.cu->lines->nlines; ++cnt) 85 { 86 Dwarf_Line *line = &cu->die.cu->lines->info[cnt]; 87 88 if (unlikely (line->file >= line->files->nfiles)) 89 { 90 __libdwfl_seterrno (DWFL_E (LIBDW, DWARF_E_INVALID_DWARF)); 91 return -1; 92 } 93 else 94 { 95 const char *file = dwfl_dwarf_line_file (line); 96 if (file != lastfile) 97 { 98 /* Match the name with the name the user provided. */ 99 lastfile = file; 100 lastmatch = !strcmp (is_basename ? basename (file) : file, 101 fname); 102 } 103 } 104 if (!lastmatch) 105 continue; 106 107 /* See whether line and possibly column match. */ 108 if (lineno != 0 109 && (lineno > line->line 110 || (column != 0 && column > line->column))) 111 /* Cannot match. */ 112 continue; 113 114 /* Determine whether this is the best match so far. */ 115 size_t inner; 116 for (inner = 0; inner < cur_match; ++inner) 117 if (dwfl_line_file (match[inner]) 118 == dwfl_dwarf_line_file (line)) 119 break; 120 if (inner < cur_match 121 && (dwfl_line (match[inner])->line != line->line 122 || dwfl_line (match[inner])->line != lineno 123 || (column != 0 124 && (dwfl_line (match[inner])->column != line->column 125 || dwfl_line (match[inner])->column != column)))) 126 { 127 /* We know about this file already. If this is a better 128 match for the line number, use it. */ 129 if (dwfl_line (match[inner])->line >= line->line 130 && (dwfl_line (match[inner])->line != line->line 131 || dwfl_line (match[inner])->column >= line->column)) 132 /* Use the new line. Otherwise the old one. */ 133 match[inner] = &cu->lines->idx[cnt]; 134 continue; 135 } 136 137 if (cur_match < max_match) 138 { 139 if (cur_match == act_match) 140 { 141 /* Enlarge the array for the results. */ 142 act_match += 10; 143 Dwfl_Line **newp = realloc (match, 144 act_match 145 * sizeof (Dwfl_Line *)); 146 if (newp == NULL) 147 { 148 free (match); 149 __libdwfl_seterrno (DWFL_E_NOMEM); 150 return -1; 151 } 152 match = newp; 153 } 154 155 match[cur_match++] = &cu->lines->idx[cnt]; 156 } 157 } 158 } 159 160 if (cur_match > 0) 161 { 162 assert (*nsrcs == 0 || *srcsp == match); 163 164 *nsrcs = cur_match; 165 *srcsp = match; 166 167 return 0; 168 } 169 170 __libdwfl_seterrno (DWFL_E_NO_MATCH); 171 return -1; 172 } 173