Imported Upstream version 0.155
[platform/upstream/elfutils.git] / tests / dwfl-bug-getmodules.c
1 /* Test program for dwfl_getmodules bug.
2    Copyright (C) 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 the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    elfutils is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 #include <config.h>
19 #include ELFUTILS_HEADER(dwfl)
20
21 #include <error.h>
22
23 static const Dwfl_Callbacks callbacks =
24   {
25     .find_elf = dwfl_linux_proc_find_elf,
26     .find_debuginfo = dwfl_standard_find_debuginfo,
27   };
28
29 static int
30 iterate (Dwfl_Module *mod __attribute__ ((unused)),
31          void **userdata __attribute__ ((unused)),
32          const char *name __attribute__ ((unused)),
33          Dwarf_Addr base, void *arg)
34 {
35   if (base != 0x2000)
36     return DWARF_CB_OK;
37
38   if (dwfl_addrmodule (arg, 0x2100) == NULL)
39     error (1, 0, "dwfl_addrmodule: %s", dwfl_errmsg (-1));
40
41   return DWARF_CB_ABORT;
42 }
43
44 int
45 main (void)
46 {
47   Dwfl *dwfl = dwfl_begin (&callbacks);
48
49   dwfl_report_module (dwfl, "m1", 0, 0x1000);
50   dwfl_report_module (dwfl, "m2", 0x2000, 0x3000);
51   dwfl_report_module (dwfl, "m3", 0x4000, 0x5000);
52
53   dwfl_report_end (dwfl, NULL, NULL);
54
55   ptrdiff_t offset = dwfl_getmodules (dwfl, &iterate, dwfl, 0);
56   if (offset <= 0)
57     error (1, 0, "dwfl_getmodules: %s", dwfl_errmsg (-1));
58
59   offset = dwfl_getmodules (dwfl, &iterate, NULL, offset);
60   if (offset != 0)
61     error (1, 0, "dwfl_getmodules (%d): %s", (int) offset, dwfl_errmsg (-1));
62
63   dwfl_end (dwfl);
64
65   return 0;
66 }