1 /* Find matching source locations in a module.
2 Copyright (C) 2005 Red Hat, Inc.
3 This file is part of elfutils.
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
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
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
18 or both in parallel, as here.
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.
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/>. */
30 #include "../libdw/libdwP.h"
33 static inline const char *
34 dwfl_dwarf_line_file (const Dwarf_Line *line)
36 return line->files->info[line->file].name;
39 static inline Dwarf_Line *
40 dwfl_line (const Dwfl_Line *line)
42 return &dwfl_linecu (line)->die.cu->lines->info[line->idx];
45 static inline const char *
46 dwfl_line_file (const Dwfl_Line *line)
48 return dwfl_dwarf_line_file (dwfl_line (line));
52 dwfl_module_getsrc_file (Dwfl_Module *mod,
53 const char *fname, int lineno, int column,
54 Dwfl_Line ***srcsp, size_t *nsrcs)
62 if (INTUSE(dwfl_module_getdwarf) (mod, &bias) == NULL)
66 bool is_basename = strchr (fname, '/') == NULL;
68 size_t max_match = *nsrcs ?: ~0u;
69 size_t act_match = *nsrcs;
71 Dwfl_Line **match = *nsrcs == 0 ? NULL : *srcsp;
73 struct dwfl_cu *cu = NULL;
75 while ((error = __libdwfl_nextcu (mod, cu, &cu)) == DWFL_E_NOERROR
77 && (error = __libdwfl_cu_getsrclines (cu)) == DWFL_E_NOERROR)
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)
86 Dwarf_Line *line = &cu->die.cu->lines->info[cnt];
88 if (unlikely (line->file >= line->files->nfiles))
90 __libdwfl_seterrno (DWFL_E (LIBDW, DWARF_E_INVALID_DWARF));
95 const char *file = dwfl_dwarf_line_file (line);
98 /* Match the name with the name the user provided. */
100 lastmatch = !strcmp (is_basename ? basename (file) : file,
107 /* See whether line and possibly column match. */
109 && (lineno > line->line
110 || (column != 0 && column > line->column)))
114 /* Determine whether this is the best match so far. */
116 for (inner = 0; inner < cur_match; ++inner)
117 if (dwfl_line_file (match[inner])
118 == dwfl_dwarf_line_file (line))
120 if (inner < cur_match
121 && (dwfl_line (match[inner])->line != line->line
122 || dwfl_line (match[inner])->line != lineno
124 && (dwfl_line (match[inner])->column != line->column
125 || dwfl_line (match[inner])->column != column))))
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];
137 if (cur_match < max_match)
139 if (cur_match == act_match)
141 /* Enlarge the array for the results. */
143 Dwfl_Line **newp = realloc (match,
145 * sizeof (Dwfl_Line *));
149 __libdwfl_seterrno (DWFL_E_NOMEM);
155 match[cur_match++] = &cu->lines->idx[cnt];
162 assert (*nsrcs == 0 || *srcsp == match);
170 __libdwfl_seterrno (DWFL_E_NO_MATCH);