2 Copyright (C) 2019-2020 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 int _libctf_version = CTF_VERSION; /* Library client version. */
30 int _libctf_debug = 0; /* Debugging messages enabled. */
32 /* Private, read-only mmap from a file, with fallback to copying.
34 No handling of page-offset issues at all: the caller must allow for that. */
36 _libctf_malloc_ void *
37 ctf_mmap (size_t length, size_t offset, int fd)
42 data = mmap (NULL, length, PROT_READ, MAP_PRIVATE, fd, offset);
43 if (data == MAP_FAILED)
46 if ((data = malloc (length)) != NULL)
48 if (ctf_pread (fd, data, length, offset) <= 0)
59 ctf_munmap (void *buf, size_t length _libctf_unused_)
62 (void) munmap (buf, length);
69 ctf_pread (int fd, void *buf, ssize_t count, off_t offset)
73 char *data = (char *) buf;
79 if (((len = pread (fd, data, count, offset)) < 0) &&
86 if (len == 0) /* EOF. */
97 if ((orig_off = lseek (fd, 0, SEEK_CUR)) < 0)
99 if ((lseek (fd, offset, SEEK_SET)) < 0)
105 if (((len = read (fd, data, count)) < 0) &&
112 if (len == 0) /* EOF. */
118 if ((lseek (fd, orig_off, SEEK_SET)) < 0)
119 return -1; /* offset is smashed. */
126 ctf_strerror (int err)
128 return (const char *) (strerror (err));
131 /* Set the CTF library client version to the specified version. If version is
132 zero, we just return the default library version number. */
134 ctf_version (int version)
144 /* Dynamic version switching is not presently supported. */
145 if (version != CTF_VERSION)
150 ctf_dprintf ("ctf_version: client using version %d\n", version);
151 _libctf_version = version;
154 return _libctf_version;
158 libctf_init_debug (void)
163 _libctf_debug = getenv ("LIBCTF_DEBUG") != NULL;
168 void ctf_setdebug (int debug)
170 /* Ensure that libctf_init_debug() has been called, so that we don't get our
171 debugging-on-or-off smashed by the next call. */
174 _libctf_debug = debug;
175 ctf_dprintf ("CTF debugging set to %i\n", debug);
178 int ctf_getdebug (void)
180 return _libctf_debug;
183 _libctf_printflike_ (1, 2)
184 void ctf_dprintf (const char *format, ...)
186 if (_libctf_unlikely_ (_libctf_debug))
190 va_start (alist, format);
192 (void) fputs ("libctf DEBUG: ", stderr);
193 (void) vfprintf (stderr, format, alist);
198 /* Errors and warnings. */
199 _libctf_printflike_ (3, 4)
201 ctf_err_warn (ctf_file_t *fp, int is_warning, const char *format, ...)
204 ctf_err_warning_t *cew;
206 /* Don't bother reporting errors here: we can't do much about them if they
207 happen. If we're so short of memory that a tiny malloc doesn't work, a
208 vfprintf isn't going to work either and the caller will have to rely on the
209 ENOMEM return they'll be getting in short order anyway. */
211 if ((cew = malloc (sizeof (ctf_err_warning_t))) == NULL)
214 cew->cew_is_warning = is_warning;
215 va_start (alist, format);
216 if (vasprintf (&cew->cew_text, format, alist) < 0)
224 ctf_dprintf ("%s: %s\n", is_warning ? "error" : "warning", cew->cew_text);
226 ctf_list_append (&fp->ctf_errs_warnings, cew);
229 /* Error-warning reporting: an 'iterator' that returns errors and warnings from
230 the error/warning list, in order of emission. Errors and warnings are popped
231 after return: the caller must free the returned error-text pointer. */
233 ctf_errwarning_next (ctf_file_t *fp, ctf_next_t **it, int *is_warning)
237 ctf_err_warning_t *cew;
241 if ((i = ctf_next_create ()) == NULL)
243 ctf_set_errno (fp, ENOMEM);
248 i->ctn_iter_fun = (void (*) (void)) ctf_errwarning_next;
252 if ((void (*) (void)) ctf_errwarning_next != i->ctn_iter_fun)
254 ctf_set_errno (fp, ECTF_NEXT_WRONGFUN);
258 if (fp != i->cu.ctn_fp)
260 ctf_set_errno (fp, ECTF_NEXT_WRONGFP);
264 cew = ctf_list_next (&fp->ctf_errs_warnings);
268 ctf_next_destroy (i);
270 ctf_set_errno (fp, ECTF_NEXT_END);
275 *is_warning = cew->cew_is_warning;
277 ctf_list_delete (&fp->ctf_errs_warnings, cew);
283 ctf_assert_fail_internal (ctf_file_t *fp, const char *file, size_t line,
286 ctf_err_warn (fp, 0, "%s: %lu: libctf assertion failed: %s", file,
287 (long unsigned int) line, exprstr);
288 ctf_set_errno (fp, ECTF_INTERNAL);