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