125b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* Find line information for given file/line/column triple. 225b3c049e70834cf33790a28643ab058b507b35cBen Cheng Copyright (C) 2005-2009 Red Hat, Inc. 325b3c049e70834cf33790a28643ab058b507b35cBen Cheng This file is part of Red Hat elfutils. 425b3c049e70834cf33790a28643ab058b507b35cBen Cheng Written by Ulrich Drepper <drepper@redhat.com>, 2005. 525b3c049e70834cf33790a28643ab058b507b35cBen Cheng 625b3c049e70834cf33790a28643ab058b507b35cBen Cheng Red Hat elfutils is free software; you can redistribute it and/or modify 725b3c049e70834cf33790a28643ab058b507b35cBen Cheng it under the terms of the GNU General Public License as published by the 825b3c049e70834cf33790a28643ab058b507b35cBen Cheng Free Software Foundation; version 2 of the License. 925b3c049e70834cf33790a28643ab058b507b35cBen Cheng 1025b3c049e70834cf33790a28643ab058b507b35cBen Cheng Red Hat elfutils is distributed in the hope that it will be useful, but 1125b3c049e70834cf33790a28643ab058b507b35cBen Cheng WITHOUT ANY WARRANTY; without even the implied warranty of 1225b3c049e70834cf33790a28643ab058b507b35cBen Cheng MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1325b3c049e70834cf33790a28643ab058b507b35cBen Cheng General Public License for more details. 1425b3c049e70834cf33790a28643ab058b507b35cBen Cheng 1525b3c049e70834cf33790a28643ab058b507b35cBen Cheng You should have received a copy of the GNU General Public License along 1625b3c049e70834cf33790a28643ab058b507b35cBen Cheng with Red Hat elfutils; if not, write to the Free Software Foundation, 1725b3c049e70834cf33790a28643ab058b507b35cBen Cheng Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. 1825b3c049e70834cf33790a28643ab058b507b35cBen Cheng 1925b3c049e70834cf33790a28643ab058b507b35cBen Cheng In addition, as a special exception, Red Hat, Inc. gives You the 2025b3c049e70834cf33790a28643ab058b507b35cBen Cheng additional right to link the code of Red Hat elfutils with code licensed 2125b3c049e70834cf33790a28643ab058b507b35cBen Cheng under any Open Source Initiative certified open source license 2225b3c049e70834cf33790a28643ab058b507b35cBen Cheng (http://www.opensource.org/licenses/index.php) which requires the 2325b3c049e70834cf33790a28643ab058b507b35cBen Cheng distribution of source code with any binary distribution and to 2425b3c049e70834cf33790a28643ab058b507b35cBen Cheng distribute linked combinations of the two. Non-GPL Code permitted under 2525b3c049e70834cf33790a28643ab058b507b35cBen Cheng this exception must only link to the code of Red Hat elfutils through 2625b3c049e70834cf33790a28643ab058b507b35cBen Cheng those well defined interfaces identified in the file named EXCEPTION 2725b3c049e70834cf33790a28643ab058b507b35cBen Cheng found in the source code files (the "Approved Interfaces"). The files 2825b3c049e70834cf33790a28643ab058b507b35cBen Cheng of Non-GPL Code may instantiate templates or use macros or inline 2925b3c049e70834cf33790a28643ab058b507b35cBen Cheng functions from the Approved Interfaces without causing the resulting 3025b3c049e70834cf33790a28643ab058b507b35cBen Cheng work to be covered by the GNU General Public License. Only Red Hat, 3125b3c049e70834cf33790a28643ab058b507b35cBen Cheng Inc. may make changes or additions to the list of Approved Interfaces. 3225b3c049e70834cf33790a28643ab058b507b35cBen Cheng Red Hat's grant of this exception is conditioned upon your not adding 3325b3c049e70834cf33790a28643ab058b507b35cBen Cheng any new exceptions. If you wish to add a new Approved Interface or 3425b3c049e70834cf33790a28643ab058b507b35cBen Cheng exception, please contact Red Hat. You must obey the GNU General Public 3525b3c049e70834cf33790a28643ab058b507b35cBen Cheng License in all respects for all of the Red Hat elfutils code and other 3625b3c049e70834cf33790a28643ab058b507b35cBen Cheng code used in conjunction with Red Hat elfutils except the Non-GPL Code 3725b3c049e70834cf33790a28643ab058b507b35cBen Cheng covered by this exception. If you modify this file, you may extend this 3825b3c049e70834cf33790a28643ab058b507b35cBen Cheng exception to your version of the file, but you are not obligated to do 3925b3c049e70834cf33790a28643ab058b507b35cBen Cheng so. If you do not wish to provide this exception without modification, 4025b3c049e70834cf33790a28643ab058b507b35cBen Cheng you must delete this exception statement from your version and license 4125b3c049e70834cf33790a28643ab058b507b35cBen Cheng this file solely under the GPL without exception. 4225b3c049e70834cf33790a28643ab058b507b35cBen Cheng 4325b3c049e70834cf33790a28643ab058b507b35cBen Cheng Red Hat elfutils is an included package of the Open Invention Network. 4425b3c049e70834cf33790a28643ab058b507b35cBen Cheng An included package of the Open Invention Network is a package for which 4525b3c049e70834cf33790a28643ab058b507b35cBen Cheng Open Invention Network licensees cross-license their patents. No patent 4625b3c049e70834cf33790a28643ab058b507b35cBen Cheng license is granted, either expressly or impliedly, by designation as an 4725b3c049e70834cf33790a28643ab058b507b35cBen Cheng included package. Should you wish to participate in the Open Invention 4825b3c049e70834cf33790a28643ab058b507b35cBen Cheng Network licensing program, please visit www.openinventionnetwork.com 4925b3c049e70834cf33790a28643ab058b507b35cBen Cheng <http://www.openinventionnetwork.com>. */ 5025b3c049e70834cf33790a28643ab058b507b35cBen Cheng 5125b3c049e70834cf33790a28643ab058b507b35cBen Cheng#ifdef HAVE_CONFIG_H 5225b3c049e70834cf33790a28643ab058b507b35cBen Cheng# include <config.h> 5325b3c049e70834cf33790a28643ab058b507b35cBen Cheng#endif 5425b3c049e70834cf33790a28643ab058b507b35cBen Cheng 5525b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include <assert.h> 5625b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include <limits.h> 5725b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include <stdlib.h> 5825b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include <string.h> 5925b3c049e70834cf33790a28643ab058b507b35cBen Cheng 6025b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include "libdwP.h" 6125b3c049e70834cf33790a28643ab058b507b35cBen Cheng 6225b3c049e70834cf33790a28643ab058b507b35cBen Cheng 6325b3c049e70834cf33790a28643ab058b507b35cBen Chengint 6425b3c049e70834cf33790a28643ab058b507b35cBen Chengdwarf_getsrc_file (Dwarf *dbg, const char *fname, int lineno, int column, 6525b3c049e70834cf33790a28643ab058b507b35cBen Cheng Dwarf_Line ***srcsp, size_t *nsrcs) 6625b3c049e70834cf33790a28643ab058b507b35cBen Cheng{ 6725b3c049e70834cf33790a28643ab058b507b35cBen Cheng if (dbg == NULL) 6825b3c049e70834cf33790a28643ab058b507b35cBen Cheng return -1; 6925b3c049e70834cf33790a28643ab058b507b35cBen Cheng 7025b3c049e70834cf33790a28643ab058b507b35cBen Cheng bool is_basename = strchr (fname, '/') == NULL; 7125b3c049e70834cf33790a28643ab058b507b35cBen Cheng 7225b3c049e70834cf33790a28643ab058b507b35cBen Cheng size_t max_match = *nsrcs ?: ~0u; 7325b3c049e70834cf33790a28643ab058b507b35cBen Cheng size_t act_match = *nsrcs; 7425b3c049e70834cf33790a28643ab058b507b35cBen Cheng size_t cur_match = 0; 7525b3c049e70834cf33790a28643ab058b507b35cBen Cheng Dwarf_Line **match = *nsrcs == 0 ? NULL : *srcsp; 7625b3c049e70834cf33790a28643ab058b507b35cBen Cheng 7725b3c049e70834cf33790a28643ab058b507b35cBen Cheng size_t cuhl; 7825b3c049e70834cf33790a28643ab058b507b35cBen Cheng Dwarf_Off noff; 7925b3c049e70834cf33790a28643ab058b507b35cBen Cheng for (Dwarf_Off off = 0; 8025b3c049e70834cf33790a28643ab058b507b35cBen Cheng INTUSE(dwarf_nextcu) (dbg, off, &noff, &cuhl, NULL, NULL, NULL) == 0; 8125b3c049e70834cf33790a28643ab058b507b35cBen Cheng off = noff) 8225b3c049e70834cf33790a28643ab058b507b35cBen Cheng { 8325b3c049e70834cf33790a28643ab058b507b35cBen Cheng Dwarf_Die cudie_mem; 8425b3c049e70834cf33790a28643ab058b507b35cBen Cheng Dwarf_Die *cudie = INTUSE(dwarf_offdie) (dbg, off + cuhl, &cudie_mem); 8525b3c049e70834cf33790a28643ab058b507b35cBen Cheng if (cudie == NULL) 8625b3c049e70834cf33790a28643ab058b507b35cBen Cheng continue; 8725b3c049e70834cf33790a28643ab058b507b35cBen Cheng 8825b3c049e70834cf33790a28643ab058b507b35cBen Cheng /* Get the line number information for this file. */ 8925b3c049e70834cf33790a28643ab058b507b35cBen Cheng Dwarf_Lines *lines; 9025b3c049e70834cf33790a28643ab058b507b35cBen Cheng size_t nlines; 9125b3c049e70834cf33790a28643ab058b507b35cBen Cheng if (INTUSE(dwarf_getsrclines) (cudie, &lines, &nlines) != 0) 9225b3c049e70834cf33790a28643ab058b507b35cBen Cheng { 9325b3c049e70834cf33790a28643ab058b507b35cBen Cheng /* Ignore a CU that just has no DW_AT_stmt_list at all. */ 9425b3c049e70834cf33790a28643ab058b507b35cBen Cheng int error = INTUSE(dwarf_errno) (); 9525b3c049e70834cf33790a28643ab058b507b35cBen Cheng if (error == 0) 9625b3c049e70834cf33790a28643ab058b507b35cBen Cheng continue; 9725b3c049e70834cf33790a28643ab058b507b35cBen Cheng __libdw_seterrno (error); 9825b3c049e70834cf33790a28643ab058b507b35cBen Cheng return -1; 9925b3c049e70834cf33790a28643ab058b507b35cBen Cheng } 10025b3c049e70834cf33790a28643ab058b507b35cBen Cheng 10125b3c049e70834cf33790a28643ab058b507b35cBen Cheng /* Search through all the line number records for a matching 10225b3c049e70834cf33790a28643ab058b507b35cBen Cheng file and line/column number. If any of the numbers is zero, 10325b3c049e70834cf33790a28643ab058b507b35cBen Cheng no match is performed. */ 10425b3c049e70834cf33790a28643ab058b507b35cBen Cheng unsigned int lastfile = UINT_MAX; 10525b3c049e70834cf33790a28643ab058b507b35cBen Cheng bool lastmatch = false; 10625b3c049e70834cf33790a28643ab058b507b35cBen Cheng for (size_t cnt = 0; cnt < nlines; ++cnt) 10725b3c049e70834cf33790a28643ab058b507b35cBen Cheng { 10825b3c049e70834cf33790a28643ab058b507b35cBen Cheng Dwarf_Line *line = &lines->info[cnt]; 10925b3c049e70834cf33790a28643ab058b507b35cBen Cheng 11025b3c049e70834cf33790a28643ab058b507b35cBen Cheng if (lastfile != line->file) 11125b3c049e70834cf33790a28643ab058b507b35cBen Cheng { 11225b3c049e70834cf33790a28643ab058b507b35cBen Cheng lastfile = line->file; 11325b3c049e70834cf33790a28643ab058b507b35cBen Cheng if (lastfile >= line->files->nfiles) 11425b3c049e70834cf33790a28643ab058b507b35cBen Cheng { 11525b3c049e70834cf33790a28643ab058b507b35cBen Cheng __libdw_seterrno (DWARF_E_INVALID_DWARF); 11625b3c049e70834cf33790a28643ab058b507b35cBen Cheng return -1; 11725b3c049e70834cf33790a28643ab058b507b35cBen Cheng } 11825b3c049e70834cf33790a28643ab058b507b35cBen Cheng 11925b3c049e70834cf33790a28643ab058b507b35cBen Cheng /* Match the name with the name the user provided. */ 12025b3c049e70834cf33790a28643ab058b507b35cBen Cheng const char *fname2 = line->files->info[lastfile].name; 12125b3c049e70834cf33790a28643ab058b507b35cBen Cheng if (is_basename) 12225b3c049e70834cf33790a28643ab058b507b35cBen Cheng lastmatch = strcmp (basename (fname2), fname) == 0; 12325b3c049e70834cf33790a28643ab058b507b35cBen Cheng else 12425b3c049e70834cf33790a28643ab058b507b35cBen Cheng lastmatch = strcmp (fname2, fname) == 0; 12525b3c049e70834cf33790a28643ab058b507b35cBen Cheng } 12625b3c049e70834cf33790a28643ab058b507b35cBen Cheng if (!lastmatch) 12725b3c049e70834cf33790a28643ab058b507b35cBen Cheng continue; 12825b3c049e70834cf33790a28643ab058b507b35cBen Cheng 12925b3c049e70834cf33790a28643ab058b507b35cBen Cheng /* See whether line and possibly column match. */ 13025b3c049e70834cf33790a28643ab058b507b35cBen Cheng if (lineno != 0 13125b3c049e70834cf33790a28643ab058b507b35cBen Cheng && (lineno > line->line 13225b3c049e70834cf33790a28643ab058b507b35cBen Cheng || (column != 0 && column > line->column))) 13325b3c049e70834cf33790a28643ab058b507b35cBen Cheng /* Cannot match. */ 13425b3c049e70834cf33790a28643ab058b507b35cBen Cheng continue; 13525b3c049e70834cf33790a28643ab058b507b35cBen Cheng 13625b3c049e70834cf33790a28643ab058b507b35cBen Cheng /* Determine whether this is the best match so far. */ 13725b3c049e70834cf33790a28643ab058b507b35cBen Cheng size_t inner; 13825b3c049e70834cf33790a28643ab058b507b35cBen Cheng for (inner = 0; inner < cur_match; ++inner) 13925b3c049e70834cf33790a28643ab058b507b35cBen Cheng if (match[inner]->files == line->files 14025b3c049e70834cf33790a28643ab058b507b35cBen Cheng && match[inner]->file == line->file) 14125b3c049e70834cf33790a28643ab058b507b35cBen Cheng break; 14225b3c049e70834cf33790a28643ab058b507b35cBen Cheng if (inner < cur_match 14325b3c049e70834cf33790a28643ab058b507b35cBen Cheng && (match[inner]->line != line->line 14425b3c049e70834cf33790a28643ab058b507b35cBen Cheng || match[inner]->line != lineno 14525b3c049e70834cf33790a28643ab058b507b35cBen Cheng || (column != 0 14625b3c049e70834cf33790a28643ab058b507b35cBen Cheng && (match[inner]->column != line->column 14725b3c049e70834cf33790a28643ab058b507b35cBen Cheng || match[inner]->column != column)))) 14825b3c049e70834cf33790a28643ab058b507b35cBen Cheng { 14925b3c049e70834cf33790a28643ab058b507b35cBen Cheng /* We know about this file already. If this is a better 15025b3c049e70834cf33790a28643ab058b507b35cBen Cheng match for the line number, use it. */ 15125b3c049e70834cf33790a28643ab058b507b35cBen Cheng if (match[inner]->line >= line->line 15225b3c049e70834cf33790a28643ab058b507b35cBen Cheng && (match[inner]->line != line->line 15325b3c049e70834cf33790a28643ab058b507b35cBen Cheng || match[inner]->column >= line->column)) 15425b3c049e70834cf33790a28643ab058b507b35cBen Cheng /* Use the new line. Otherwise the old one. */ 15525b3c049e70834cf33790a28643ab058b507b35cBen Cheng match[inner] = line; 15625b3c049e70834cf33790a28643ab058b507b35cBen Cheng continue; 15725b3c049e70834cf33790a28643ab058b507b35cBen Cheng } 15825b3c049e70834cf33790a28643ab058b507b35cBen Cheng 15925b3c049e70834cf33790a28643ab058b507b35cBen Cheng if (cur_match < max_match) 16025b3c049e70834cf33790a28643ab058b507b35cBen Cheng { 16125b3c049e70834cf33790a28643ab058b507b35cBen Cheng if (cur_match == act_match) 16225b3c049e70834cf33790a28643ab058b507b35cBen Cheng { 16325b3c049e70834cf33790a28643ab058b507b35cBen Cheng /* Enlarge the array for the results. */ 16425b3c049e70834cf33790a28643ab058b507b35cBen Cheng act_match += 10; 16525b3c049e70834cf33790a28643ab058b507b35cBen Cheng Dwarf_Line **newp = realloc (match, 16625b3c049e70834cf33790a28643ab058b507b35cBen Cheng act_match 16725b3c049e70834cf33790a28643ab058b507b35cBen Cheng * sizeof (Dwarf_Line *)); 16825b3c049e70834cf33790a28643ab058b507b35cBen Cheng if (newp == NULL) 16925b3c049e70834cf33790a28643ab058b507b35cBen Cheng { 17025b3c049e70834cf33790a28643ab058b507b35cBen Cheng free (match); 17125b3c049e70834cf33790a28643ab058b507b35cBen Cheng __libdw_seterrno (DWARF_E_NOMEM); 17225b3c049e70834cf33790a28643ab058b507b35cBen Cheng return -1; 17325b3c049e70834cf33790a28643ab058b507b35cBen Cheng } 17425b3c049e70834cf33790a28643ab058b507b35cBen Cheng match = newp; 17525b3c049e70834cf33790a28643ab058b507b35cBen Cheng } 17625b3c049e70834cf33790a28643ab058b507b35cBen Cheng 17725b3c049e70834cf33790a28643ab058b507b35cBen Cheng match[cur_match++] = line; 17825b3c049e70834cf33790a28643ab058b507b35cBen Cheng } 17925b3c049e70834cf33790a28643ab058b507b35cBen Cheng } 18025b3c049e70834cf33790a28643ab058b507b35cBen Cheng 18125b3c049e70834cf33790a28643ab058b507b35cBen Cheng /* If we managed to find as many matches as the user requested 18225b3c049e70834cf33790a28643ab058b507b35cBen Cheng already, there is no need to go on to the next CU. */ 18325b3c049e70834cf33790a28643ab058b507b35cBen Cheng if (cur_match == max_match) 18425b3c049e70834cf33790a28643ab058b507b35cBen Cheng break; 18525b3c049e70834cf33790a28643ab058b507b35cBen Cheng } 18625b3c049e70834cf33790a28643ab058b507b35cBen Cheng 18725b3c049e70834cf33790a28643ab058b507b35cBen Cheng if (cur_match > 0) 18825b3c049e70834cf33790a28643ab058b507b35cBen Cheng { 18925b3c049e70834cf33790a28643ab058b507b35cBen Cheng assert (*nsrcs == 0 || *srcsp == match); 19025b3c049e70834cf33790a28643ab058b507b35cBen Cheng 19125b3c049e70834cf33790a28643ab058b507b35cBen Cheng *nsrcs = cur_match; 19225b3c049e70834cf33790a28643ab058b507b35cBen Cheng *srcsp = match; 19325b3c049e70834cf33790a28643ab058b507b35cBen Cheng 19425b3c049e70834cf33790a28643ab058b507b35cBen Cheng return 0; 19525b3c049e70834cf33790a28643ab058b507b35cBen Cheng } 19625b3c049e70834cf33790a28643ab058b507b35cBen Cheng 19725b3c049e70834cf33790a28643ab058b507b35cBen Cheng __libdw_seterrno (DWARF_E_NO_MATCH); 19825b3c049e70834cf33790a28643ab058b507b35cBen Cheng return -1; 19925b3c049e70834cf33790a28643ab058b507b35cBen Cheng} 200