Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1 | /* Find source location for PC address in module. |
Roland McGrath | d11f9cb | 2008-03-26 20:51:59 +0000 | [diff] [blame] | 2 | Copyright (C) 2005, 2008 Red Hat, Inc. |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 3 | This file is part of elfutils. |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 4 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 5 | This file is free software; you can redistribute it and/or modify |
| 6 | it under the terms of either |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 7 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 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 |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 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 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 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/>. */ |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 28 | |
| 29 | #include "libdwflP.h" |
| 30 | #include "../libdw/libdwP.h" |
| 31 | |
| 32 | Dwfl_Line * |
| 33 | dwfl_module_getsrc (Dwfl_Module *mod, Dwarf_Addr addr) |
| 34 | { |
| 35 | Dwarf_Addr bias; |
| 36 | if (INTUSE(dwfl_module_getdwarf) (mod, &bias) == NULL) |
| 37 | return NULL; |
| 38 | |
| 39 | struct dwfl_cu *cu; |
| 40 | Dwfl_Error error = __libdwfl_addrcu (mod, addr, &cu); |
| 41 | if (likely (error == DWFL_E_NOERROR)) |
| 42 | error = __libdwfl_cu_getsrclines (cu); |
| 43 | if (likely (error == DWFL_E_NOERROR)) |
| 44 | { |
Roland McGrath | d11f9cb | 2008-03-26 20:51:59 +0000 | [diff] [blame] | 45 | /* Now we look at the module-relative address. */ |
| 46 | addr -= bias; |
| 47 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 48 | /* The lines are sorted by address, so we can use binary search. */ |
| 49 | size_t l = 0, u = cu->die.cu->lines->nlines; |
| 50 | while (l < u) |
| 51 | { |
| 52 | size_t idx = (l + u) / 2; |
| 53 | if (addr < cu->die.cu->lines->info[idx].addr) |
| 54 | u = idx; |
| 55 | else if (addr > cu->die.cu->lines->info[idx].addr) |
| 56 | l = idx + 1; |
| 57 | else |
| 58 | return &cu->lines->idx[idx]; |
| 59 | } |
| 60 | |
| 61 | if (cu->die.cu->lines->nlines > 0) |
| 62 | assert (cu->die.cu->lines->info |
| 63 | [cu->die.cu->lines->nlines - 1].end_sequence); |
| 64 | |
| 65 | /* If none were equal, the closest one below is what we want. |
| 66 | We never want the last one, because it's the end-sequence |
| 67 | marker with an address at the high bound of the CU's code. */ |
| 68 | if (u > 0 && u < cu->die.cu->lines->nlines |
| 69 | && addr > cu->die.cu->lines->info[u - 1].addr) |
| 70 | return &cu->lines->idx[u - 1]; |
| 71 | |
| 72 | error = DWFL_E_ADDR_OUTOFRANGE; |
| 73 | } |
| 74 | |
| 75 | __libdwfl_seterrno (error); |
| 76 | return NULL; |
| 77 | } |
| 78 | INTDEF (dwfl_module_getsrc) |