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"
34 dwfl_module_getsrc_file (Dwfl_Module *mod,
35 const char *fname, int lineno, int column,
36 Dwfl_Line ***srcsp, size_t *nsrcs)
44 if (INTUSE(dwfl_module_getdwarf) (mod, &bias) == NULL)
48 bool is_basename = strchr (fname, '/') == NULL;
50 size_t max_match = *nsrcs ?: ~0u;
51 size_t act_match = *nsrcs;
53 Dwfl_Line **match = *nsrcs == 0 ? NULL : *srcsp;
55 struct dwfl_cu *cu = NULL;
57 while ((error = __libdwfl_nextcu (mod, cu, &cu)) == DWFL_E_NOERROR
59 && (error = __libdwfl_cu_getsrclines (cu)) == DWFL_E_NOERROR)
61 inline const char *INTUSE(dwarf_line_file) (const Dwarf_Line *line)
63 return line->files->info[line->file].name;
65 inline Dwarf_Line *dwfl_line (const Dwfl_Line *line)
67 return &dwfl_linecu (line)->die.cu->lines->info[line->idx];
69 inline const char *dwfl_line_file (const Dwfl_Line *line)
71 return INTUSE(dwarf_line_file) (dwfl_line (line));
74 /* Search through all the line number records for a matching
75 file and line/column number. If any of the numbers is zero,
76 no match is performed. */
77 const char *lastfile = NULL;
78 bool lastmatch = false;
79 for (size_t cnt = 0; cnt < cu->die.cu->lines->nlines; ++cnt)
81 Dwarf_Line *line = &cu->die.cu->lines->info[cnt];
83 if (unlikely (line->file >= line->files->nfiles))
85 __libdwfl_seterrno (DWFL_E (LIBDW, DWARF_E_INVALID_DWARF));
90 const char *file = INTUSE(dwarf_line_file) (line);
93 /* Match the name with the name the user provided. */
95 lastmatch = !strcmp (is_basename ? basename (file) : file,
102 /* See whether line and possibly column match. */
104 && (lineno > line->line
105 || (column != 0 && column > line->column)))
109 /* Determine whether this is the best match so far. */
111 for (inner = 0; inner < cur_match; ++inner)
112 if (dwfl_line_file (match[inner])
113 == INTUSE(dwarf_line_file) (line))
115 if (inner < cur_match
116 && (dwfl_line (match[inner])->line != line->line
117 || dwfl_line (match[inner])->line != lineno
119 && (dwfl_line (match[inner])->column != line->column
120 || dwfl_line (match[inner])->column != column))))
122 /* We know about this file already. If this is a better
123 match for the line number, use it. */
124 if (dwfl_line (match[inner])->line >= line->line
125 && (dwfl_line (match[inner])->line != line->line
126 || dwfl_line (match[inner])->column >= line->column))
127 /* Use the new line. Otherwise the old one. */
128 match[inner] = &cu->lines->idx[cnt];
132 if (cur_match < max_match)
134 if (cur_match == act_match)
136 /* Enlarge the array for the results. */
138 Dwfl_Line **newp = realloc (match,
140 * sizeof (Dwfl_Line *));
144 __libdwfl_seterrno (DWFL_E_NOMEM);
150 match[cur_match++] = &cu->lines->idx[cnt];
157 assert (*nsrcs == 0 || *srcsp == match);
165 __libdwfl_seterrno (DWFL_E_NO_MATCH);