2 Copyright (C) 2019 Free Software Foundation, Inc.
4 This file is part of libctf.
6 libctf is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; see the file COPYING. If not see
18 <http://www.gnu.org/licenses/>. */
24 #include <sys/types.h>
29 static size_t _PAGESIZE _libctf_unused_;
30 int _libctf_version = CTF_VERSION; /* Library client version. */
31 int _libctf_debug = 0; /* Debugging messages enabled. */
33 _libctf_malloc_ void *
34 ctf_data_alloc (size_t size)
40 _PAGESIZE = sysconf(_SC_PAGESIZE);
44 ret = mmap (NULL, size, PROT_READ | PROT_WRITE,
45 MAP_PRIVATE | MAP_ANON, -1, 0);
46 if (ret == MAP_FAILED)
50 ret = calloc (1, size);
52 ret = calloc (1, size);
58 ctf_data_free (void *buf, size_t size _libctf_unused_)
61 /* Must be the same as the check in ctf_data_alloc(). */
64 (void) munmap (buf, size);
72 /* Private, read-only mmap from a file, with fallback to copying.
74 No handling of page-offset issues at all: the caller must allow for that. */
76 _libctf_malloc_ void *
77 ctf_mmap (size_t length, size_t offset, int fd)
82 data = mmap (NULL, length, PROT_READ, MAP_PRIVATE, fd, offset);
83 if (data == MAP_FAILED)
86 if ((data = malloc (length)) != NULL)
88 if (ctf_pread (fd, data, length, offset) <= 0)
99 ctf_munmap (void *buf, size_t length _libctf_unused_)
102 (void) munmap (buf, length);
109 ctf_data_protect (void *buf, size_t size)
112 /* Must be the same as the check in ctf_data_alloc(). */
114 if (size > _PAGESIZE)
115 (void) mprotect (buf, size, PROT_READ);
119 _libctf_malloc_ void *
120 ctf_alloc (size_t size)
122 return (malloc (size));
132 ctf_pread (int fd, void *buf, ssize_t count, off_t offset)
136 char *data = (char *) buf;
142 if (((len = pread (fd, data, count, offset)) < 0) &&
149 if (len == 0) /* EOF. */
160 if ((orig_off = lseek (fd, 0, SEEK_CUR)) < 0)
162 if ((lseek (fd, offset, SEEK_SET)) < 0)
168 if (((len = read (fd, data, count)) < 0) &&
175 if (len == 0) /* EOF. */
181 if ((lseek (fd, orig_off, SEEK_SET)) < 0)
182 return -1; /* offset is smashed. */
189 ctf_strerror (int err)
191 return (const char *) (strerror (err));
194 /* Set the CTF library client version to the specified version. If version is
195 zero, we just return the default library version number. */
197 ctf_version (int version)
207 /* Dynamic version switching is not presently supported. */
208 if (version != CTF_VERSION)
213 ctf_dprintf ("ctf_version: client using version %d\n", version);
214 _libctf_version = version;
217 return _libctf_version;
221 libctf_init_debug (void)
226 _libctf_debug = getenv ("LIBCTF_DEBUG") != NULL;
231 void ctf_setdebug (int debug)
233 /* Ensure that libctf_init_debug() has been called, so that we don't get our
234 debugging-on-or-off smashed by the next call. */
237 _libctf_debug = debug;
238 ctf_dprintf ("CTF debugging set to %i\n", debug);
241 int ctf_getdebug (void)
243 return _libctf_debug;
246 _libctf_printflike_ (1, 2)
247 void ctf_dprintf (const char *format, ...)
253 va_start (alist, format);
255 (void) fputs ("libctf DEBUG: ", stderr);
256 (void) vfprintf (stderr, format, alist);