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