PageRenderTime 23ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/external/elfutils/libdw/dwarf_getsrc_file.c

https://gitlab.com/brian0218/rk3188_r-box_android4.2.2_sdk
C | 193 lines | 108 code | 22 blank | 63 comment | 55 complexity | 03867cd136a9ce5879b94a63964c8884 MD5 | raw file
  1. /* Find line information for given file/line/column triple.
  2. Copyright (C) 2005 Red Hat, Inc.
  3. This file is part of Red Hat elfutils.
  4. Written by Ulrich Drepper <drepper@redhat.com>, 2005.
  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. Red Hat elfutils is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with Red Hat elfutils; if not, write to the Free Software Foundation,
  14. Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
  15. In addition, as a special exception, Red Hat, Inc. gives You the
  16. additional right to link the code of Red Hat elfutils with code licensed
  17. under any Open Source Initiative certified open source license
  18. (http://www.opensource.org/licenses/index.php) which requires the
  19. distribution of source code with any binary distribution and to
  20. distribute linked combinations of the two. Non-GPL Code permitted under
  21. this exception must only link to the code of Red Hat elfutils through
  22. those well defined interfaces identified in the file named EXCEPTION
  23. found in the source code files (the "Approved Interfaces"). The files
  24. of Non-GPL Code may instantiate templates or use macros or inline
  25. functions from the Approved Interfaces without causing the resulting
  26. work to be covered by the GNU General Public License. Only Red Hat,
  27. Inc. may make changes or additions to the list of Approved Interfaces.
  28. Red Hat's grant of this exception is conditioned upon your not adding
  29. any new exceptions. If you wish to add a new Approved Interface or
  30. exception, please contact Red Hat. You must obey the GNU General Public
  31. License in all respects for all of the Red Hat elfutils code and other
  32. code used in conjunction with Red Hat elfutils except the Non-GPL Code
  33. covered by this exception. If you modify this file, you may extend this
  34. exception to your version of the file, but you are not obligated to do
  35. so. If you do not wish to provide this exception without modification,
  36. you must delete this exception statement from your version and license
  37. this file solely under the GPL without exception.
  38. Red Hat elfutils is an included package of the Open Invention Network.
  39. An included package of the Open Invention Network is a package for which
  40. Open Invention Network licensees cross-license their patents. No patent
  41. license is granted, either expressly or impliedly, by designation as an
  42. included package. Should you wish to participate in the Open Invention
  43. Network licensing program, please visit www.openinventionnetwork.com
  44. <http://www.openinventionnetwork.com>. */
  45. #ifdef HAVE_CONFIG_H
  46. # include <config.h>
  47. #endif
  48. #include <assert.h>
  49. #include <limits.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #include "libdwP.h"
  53. int
  54. dwarf_getsrc_file (Dwarf *dbg, const char *fname, int lineno, int column,
  55. Dwarf_Line ***srcsp, size_t *nsrcs)
  56. {
  57. if (dbg == NULL)
  58. return -1;
  59. bool is_basename = strchr (fname, '/') == NULL;
  60. size_t max_match = *nsrcs ?: ~0u;
  61. size_t act_match = *nsrcs;
  62. size_t cur_match = 0;
  63. Dwarf_Line **match = *nsrcs == 0 ? NULL : *srcsp;
  64. Dwarf_Off off = 0;
  65. size_t cuhl;
  66. Dwarf_Off noff;
  67. while (INTUSE(dwarf_nextcu) (dbg, off, &noff, &cuhl, NULL, NULL, NULL) == 0)
  68. {
  69. Dwarf_Die cudie_mem;
  70. Dwarf_Die *cudie = INTUSE(dwarf_offdie) (dbg, off + cuhl, &cudie_mem);
  71. if (cudie == NULL)
  72. continue;
  73. /* Get the line number information for this file. */
  74. Dwarf_Lines *lines;
  75. size_t nlines;
  76. if (INTUSE(dwarf_getsrclines) (cudie, &lines, &nlines) != 0)
  77. return -1;
  78. /* Search through all the line number records for a matching
  79. file and line/column number. If any of the numbers is zero,
  80. no match is performed. */
  81. unsigned int lastfile = UINT_MAX;
  82. bool lastmatch = false;
  83. for (size_t cnt = 0; cnt < nlines; ++cnt)
  84. {
  85. Dwarf_Line *line = &lines->info[cnt];
  86. if (lastfile != line->file)
  87. {
  88. lastfile = line->file;
  89. if (lastfile >= line->files->nfiles)
  90. {
  91. __libdw_seterrno (DWARF_E_INVALID_DWARF);
  92. return -1;
  93. }
  94. /* Match the name with the name the user provided. */
  95. const char *fname2 = line->files->info[lastfile].name;
  96. if (is_basename)
  97. lastmatch = strcmp (basename (fname2), fname) == 0;
  98. else
  99. lastmatch = strcmp (fname2, fname) == 0;
  100. }
  101. if (!lastmatch)
  102. continue;
  103. /* See whether line and possibly column match. */
  104. if (lineno != 0
  105. && (lineno > line->line
  106. || (column != 0 && column > line->column)))
  107. /* Cannot match. */
  108. continue;
  109. /* Determine whether this is the best match so far. */
  110. size_t inner;
  111. for (inner = 0; inner < cur_match; ++inner)
  112. if (match[inner]->files == line->files
  113. && match[inner]->file == line->file)
  114. break;
  115. if (inner < cur_match
  116. && (match[inner]->line != line->line
  117. || match[inner]->line != lineno
  118. || (column != 0
  119. && (match[inner]->column != line->column
  120. || match[inner]->column != column))))
  121. {
  122. /* We know about this file already. If this is a better
  123. match for the line number, use it. */
  124. if (match[inner]->line >= line->line
  125. && (match[inner]->line != line->line
  126. || match[inner]->column >= line->column))
  127. /* Use the new line. Otherwise the old one. */
  128. match[inner] = line;
  129. continue;
  130. }
  131. if (cur_match < max_match)
  132. {
  133. if (cur_match == act_match)
  134. {
  135. /* Enlarge the array for the results. */
  136. act_match += 10;
  137. Dwarf_Line **newp = realloc (match,
  138. act_match
  139. * sizeof (Dwarf_Line *));
  140. if (newp == NULL)
  141. {
  142. free (match);
  143. __libdw_seterrno (DWARF_E_NOMEM);
  144. return -1;
  145. }
  146. match = newp;
  147. }
  148. match[cur_match++] = line;
  149. }
  150. }
  151. /* If we managed to find as many matches as the user requested
  152. already, there is no need to go on to the next CU. */
  153. if (cur_match == max_match)
  154. break;
  155. off = noff;
  156. }
  157. if (cur_match > 0)
  158. {
  159. assert (*nsrcs == 0 || *srcsp == match);
  160. *nsrcs = cur_match;
  161. *srcsp = match;
  162. return 0;
  163. }
  164. __libdw_seterrno (DWARF_E_NO_MATCH);
  165. return -1;
  166. }