Brian Silverman | 8649792 | 2018-02-10 19:28:39 -0500 | [diff] [blame] | 1 | /* Iterate through modules. |
| 2 | Copyright (C) 2005, 2008 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 | #ifdef HAVE_CONFIG_H |
| 30 | # include <config.h> |
| 31 | #endif |
| 32 | |
| 33 | #include "libdwflP.h" |
| 34 | |
| 35 | ptrdiff_t |
| 36 | dwfl_getmodules (Dwfl *dwfl, |
| 37 | int (*callback) (Dwfl_Module *, void **, |
| 38 | const char *, Dwarf_Addr, void *), |
| 39 | void *arg, |
| 40 | ptrdiff_t offset) |
| 41 | { |
| 42 | if (dwfl == NULL) |
| 43 | return -1; |
| 44 | |
| 45 | /* We iterate through the linked list when it's all we have. |
| 46 | But continuing from an offset is slow that way. So when |
| 47 | DWFL->lookup_module is populated, we can instead keep our |
| 48 | place by jumping directly into the array. Since the actions |
| 49 | of a callback could cause it to get populated, we must |
| 50 | choose the style of place-holder when we return an offset, |
| 51 | and we encode the choice in the low bits of that value. */ |
| 52 | |
| 53 | Dwfl_Module *m = dwfl->modulelist; |
| 54 | |
| 55 | if ((offset & 3) == 1) |
| 56 | { |
| 57 | offset >>= 2; |
| 58 | for (ptrdiff_t pos = 0; pos < offset; ++pos) |
| 59 | if (m == NULL) |
| 60 | return -1; |
| 61 | else |
| 62 | m = m->next; |
| 63 | } |
| 64 | else if (((offset & 3) == 2) && likely (dwfl->lookup_module != NULL)) |
| 65 | { |
| 66 | offset >>= 2; |
| 67 | |
| 68 | if ((size_t) offset - 1 == dwfl->lookup_elts) |
| 69 | return 0; |
| 70 | |
| 71 | if (unlikely ((size_t) offset - 1 > dwfl->lookup_elts)) |
| 72 | return -1; |
| 73 | |
| 74 | m = dwfl->lookup_module[offset - 1]; |
| 75 | if (unlikely (m == NULL)) |
| 76 | return -1; |
| 77 | } |
| 78 | else if (offset != 0) |
| 79 | { |
| 80 | __libdwfl_seterrno (DWFL_E_BADSTROFF); |
| 81 | return -1; |
| 82 | } |
| 83 | |
| 84 | while (m != NULL) |
| 85 | { |
| 86 | int ok = (*callback) (MODCB_ARGS (m), arg); |
| 87 | ++offset; |
| 88 | m = m->next; |
| 89 | if (ok != DWARF_CB_OK) |
| 90 | return ((dwfl->lookup_module == NULL) ? ((offset << 2) | 1) |
| 91 | : (((m == NULL ? (ptrdiff_t) dwfl->lookup_elts + 1 |
| 92 | : m->segment + 1) << 2) | 2)); |
| 93 | } |
| 94 | return 0; |
| 95 | } |
| 96 | INTDEF (dwfl_getmodules) |