Line data Source code
1 : /* Find source location for PC address in module.
2 : Copyright (C) 2005, 2008, 2014 Red Hat, Inc.
3 : This file is part of elfutils.
4 :
5 : This file is free software; you can redistribute it and/or modify
6 : it under the terms of either
7 :
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
11 :
12 : or
13 :
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
17 :
18 : or both in parallel, as here.
19 :
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.
24 :
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/>. */
28 :
29 : #include "libdwflP.h"
30 : #include "../libdw/libdwP.h"
31 :
32 : Dwfl_Line *
33 233 : dwfl_module_getsrc (Dwfl_Module *mod, Dwarf_Addr addr)
34 : {
35 : Dwarf_Addr bias;
36 233 : if (INTUSE(dwfl_module_getdwarf) (mod, &bias) == NULL)
37 : return NULL;
38 :
39 : struct dwfl_cu *cu;
40 163 : Dwfl_Error error = __libdwfl_addrcu (mod, addr, &cu);
41 163 : if (likely (error == DWFL_E_NOERROR))
42 155 : error = __libdwfl_cu_getsrclines (cu);
43 163 : if (likely (error == DWFL_E_NOERROR))
44 : {
45 155 : Dwarf_Lines *lines = cu->die.cu->lines;
46 155 : size_t nlines = lines->nlines;
47 155 : if (nlines > 0)
48 : {
49 : /* This is guaranteed for us by libdw read_srclines. */
50 155 : assert(lines->info[nlines - 1].end_sequence);
51 :
52 : /* Now we look at the module-relative address. */
53 155 : addr -= bias;
54 :
55 : /* The lines are sorted by address, so we can use binary search. */
56 155 : size_t l = 0, u = nlines - 1;
57 817 : while (l < u)
58 : {
59 507 : size_t idx = u - (u - l) / 2;
60 507 : Dwarf_Line *line = &lines->info[idx];
61 507 : if (addr < line->addr)
62 286 : u = idx - 1;
63 : else
64 : l = idx;
65 : }
66 :
67 : /* The last line which is less than or equal to addr is what
68 : we want, unless it is the end_sequence which is after the
69 : current line sequence. */
70 155 : Dwarf_Line *line = &lines->info[l];
71 155 : if (! line->end_sequence && line->addr <= addr)
72 155 : return &cu->lines->idx[l];
73 : }
74 :
75 : error = DWFL_E_ADDR_OUTOFRANGE;
76 : }
77 :
78 8 : __libdwfl_seterrno (error);
79 8 : return NULL;
80 : }
81 : INTDEF (dwfl_module_getsrc)
|