1 /* Test program for dwfl_report_offline_memory.
2 Copyright (C) 2022 Google LLC
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
20 #include <assert.h>
21 #include <fcntl.h>
22 #include <locale.h>
23 #include <stdio.h>
24 #include <stdio_ext.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include ELFUTILS_HEADER(dwfl)
28 #include "system.h"
29
30
31 static const Dwfl_Callbacks offline_callbacks =
32 {
33 .find_debuginfo = INTUSE(dwfl_standard_find_debuginfo),
34 .section_address = INTUSE(dwfl_offline_section_address),
35 };
36
37 static int
count_modules(Dwfl_Module *mod __attribute__ ((unused)), void **userdata __attribute__ ((unused)), const char *name __attribute__ ((unused)), Dwarf_Addr base __attribute__ ((unused)), void *arg)38 count_modules (Dwfl_Module *mod __attribute__ ((unused)),
39 void **userdata __attribute__ ((unused)),
40 const char *name __attribute__ ((unused)),
41 Dwarf_Addr base __attribute__ ((unused)), void *arg)
42 {
43 unsigned long long *counter = arg;
44 ++(*counter);
45 return DWARF_CB_OK;
46 }
47
48 int
main(int argc, char **argv)49 main (int argc, char **argv)
50 {
51 /* We use no threads here which can interfere with handling a stream. */
52 (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
53
54 /* Set locale. */
55 (void) setlocale (LC_ALL, "");
56
57 if (argc != 3)
58 error (-1, 0,
59 "usage: dwfl_report_offline_memory [filename] "
60 "[expected number of modules]");
61
62 const char *fname = argv[1];
63 int fd = open (fname, O_RDONLY);
64 if (fd < 0)
65 error (-1, 0, "can't open file %s: %s", fname, strerror (errno));
66 off_t size = lseek (fd, 0, SEEK_END);
67 if (size < 0)
68 error (-1, 0, "can't lseek file %s: %s", fname, strerror (errno));
69 lseek (fd, 0, SEEK_SET);
70 char *data = malloc (size);
71 if (data == NULL)
72 error (-1, 0, "can't malloc: %s", strerror (errno));
73 ssize_t bytes_read = read (fd, data, size);
74 assert (bytes_read == size);
75 close (fd);
76
77 Dwfl *dwfl = dwfl_begin (&offline_callbacks);
78 assert (dwfl != NULL);
79
80 Dwfl_Module *mod =
81 dwfl_report_offline_memory (dwfl, argv[1], argv[1], data, size);
82 assert (mod != NULL);
83 dwfl_report_end (dwfl, NULL, NULL);
84
85 unsigned long long number_of_modules = 0;
86 ptrdiff_t offset =
87 dwfl_getmodules (dwfl, &count_modules, &number_of_modules, 0);
88 if (offset < 0)
89 error (1, 0, "dwfl_getmodules: %s", dwfl_errmsg (-1));
90 assert (offset == 0);
91
92 char *endptr;
93 unsigned long long expected_number_of_modules =
94 strtoull (argv[2], &endptr, 0);
95 assert (endptr && !*endptr);
96 assert (number_of_modules == expected_number_of_modules);
97
98 dwfl_end (dwfl);
99 free (data);
100
101 return 0;
102 }
103