2 Copyright (C) 2019-2024 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>
30 #define ENOTSUP ENOSYS
33 int _libctf_version
= CTF_VERSION
; /* Library client version. */
34 int _libctf_debug
= 0; /* Debugging messages enabled. */
36 /* Private, read-only mmap from a file, with fallback to copying.
38 No handling of page-offset issues at all: the caller must allow for that. */
40 _libctf_malloc_
void *
41 ctf_mmap (size_t length
, size_t offset
, int fd
)
46 data
= mmap (NULL
, length
, PROT_READ
, MAP_PRIVATE
, fd
, offset
);
47 if (data
== MAP_FAILED
)
50 if ((data
= malloc (length
)) != NULL
)
52 if (ctf_pread (fd
, data
, length
, offset
) <= 0)
63 ctf_munmap (void *buf
, size_t length _libctf_unused_
)
66 (void) munmap (buf
, length
);
73 ctf_pread (int fd
, void *buf
, ssize_t count
, off_t offset
)
77 char *data
= (char *) buf
;
83 if (((len
= pread (fd
, data
, count
, offset
)) < 0) &&
90 if (len
== 0) /* EOF. */
101 if ((orig_off
= lseek (fd
, 0, SEEK_CUR
)) < 0)
103 if ((lseek (fd
, offset
, SEEK_SET
)) < 0)
109 if (((len
= read (fd
, data
, count
)) < 0) &&
116 if (len
== 0) /* EOF. */
122 if ((lseek (fd
, orig_off
, SEEK_SET
)) < 0)
123 return -1; /* offset is smashed. */
129 /* Set the CTF library client version to the specified version. If version is
130 zero, we just return the default library version number. */
132 ctf_version (int version
)
142 /* Dynamic version switching is not presently supported. */
143 if (version
!= CTF_VERSION
)
148 ctf_dprintf ("ctf_version: client using version %d\n", version
);
149 _libctf_version
= version
;
152 return _libctf_version
;
155 /* Get and set CTF dict-wide flags. We are fairly strict about returning
156 errors here, to make it easier to determine programmatically which flags are
160 ctf_dict_set_flag (ctf_dict_t
*fp
, uint64_t flag
, int set
)
162 if (set
< 0 || set
> 1)
163 return (ctf_set_errno (fp
, ECTF_BADFLAG
));
167 case CTF_STRICT_NO_DUP_ENUMERATORS
:
169 fp
->ctf_flags
|= LCTF_STRICT_NO_DUP_ENUMERATORS
;
171 fp
->ctf_flags
&= ~LCTF_STRICT_NO_DUP_ENUMERATORS
;
174 return (ctf_set_errno (fp
, ECTF_BADFLAG
));
180 ctf_dict_get_flag (ctf_dict_t
*fp
, uint64_t flag
)
184 case CTF_STRICT_NO_DUP_ENUMERATORS
:
185 return (fp
->ctf_flags
& LCTF_STRICT_NO_DUP_ENUMERATORS
) != 0;
187 return (ctf_set_errno (fp
, ECTF_BADFLAG
));
193 libctf_init_debug (void)
198 _libctf_debug
= getenv ("LIBCTF_DEBUG") != NULL
;
203 void ctf_setdebug (int debug
)
205 /* Ensure that libctf_init_debug() has been called, so that we don't get our
206 debugging-on-or-off smashed by the next call. */
209 _libctf_debug
= debug
;
210 ctf_dprintf ("CTF debugging set to %i\n", debug
);
213 int ctf_getdebug (void)
215 return _libctf_debug
;
218 _libctf_printflike_ (1, 2)
219 void ctf_dprintf (const char *format
, ...)
221 if (_libctf_unlikely_ (_libctf_debug
))
225 va_start (alist
, format
);
227 (void) fputs ("libctf DEBUG: ", stderr
);
228 (void) vfprintf (stderr
, format
, alist
);
233 /* This needs more attention to thread-safety later on. */
234 static ctf_list_t open_errors
;
236 /* Errors and warnings. Report the warning or error to the list in FP (or the
237 open errors list if NULL): if ERR is nonzero it is the errno to report to the
238 debug stream instead of that recorded on fp. */
239 _libctf_printflike_ (4, 5)
241 ctf_err_warn (ctf_dict_t
*fp
, int is_warning
, int err
,
242 const char *format
, ...)
245 ctf_err_warning_t
*cew
;
247 /* Don't bother reporting errors here: we can't do much about them if they
248 happen. If we're so short of memory that a tiny malloc doesn't work, a
249 vfprintf isn't going to work either and the caller will have to rely on the
250 ENOMEM return they'll be getting in short order anyway. */
252 if ((cew
= malloc (sizeof (ctf_err_warning_t
))) == NULL
)
255 cew
->cew_is_warning
= is_warning
;
256 va_start (alist
, format
);
257 if (vasprintf (&cew
->cew_text
, format
, alist
) < 0)
265 /* Include the error code only if there is one; if this is a warning,
266 only use the error code if it was explicitly passed and is nonzero.
267 (Warnings may not have a meaningful error code, since the warning may not
268 lead to unwinding up to the user.) */
269 if ((!is_warning
&& (err
!= 0 || (fp
&& ctf_errno (fp
) != 0)))
270 || (is_warning
&& err
!= 0))
271 ctf_dprintf ("%s: %s (%s)\n", is_warning
? _("warning") : _("error"),
272 cew
->cew_text
, err
!= 0 ? ctf_errmsg (err
)
273 : ctf_errmsg (ctf_errno (fp
)));
275 ctf_dprintf ("%s: %s\n", is_warning
? _("warning") : _("error"),
279 ctf_list_append (&fp
->ctf_errs_warnings
, cew
);
281 ctf_list_append (&open_errors
, cew
);
284 /* Move all the errors/warnings from an fp into the open_errors. */
286 ctf_err_warn_to_open (ctf_dict_t
*fp
)
288 ctf_list_splice (&open_errors
, &fp
->ctf_errs_warnings
);
291 /* Error-warning reporting: an 'iterator' that returns errors and warnings from
292 the error/warning list, in order of emission. Errors and warnings are popped
293 after return: the caller must free the returned error-text pointer.
295 An fp of NULL returns CTF-open-time errors from the open_errors variable
298 The treatment of errors from this function itself is somewhat unusual: it
299 will often be called on an error path, so we don't want to overwrite the
300 ctf_errno unless we have no choice. So, like ctf_bufopen et al, this
301 function takes an errp pointer where errors are reported. The pointer is
302 optional: if not set, errors are reported via the fp (if non-NULL). Calls
303 with neither fp nor errp set are mildly problematic because there is no clear
304 way to report end-of-iteration: you just have to assume that a NULL return
305 means the end, and not an iterator error. */
308 ctf_errwarning_next (ctf_dict_t
*fp
, ctf_next_t
**it
, int *is_warning
,
314 ctf_err_warning_t
*cew
;
317 errlist
= &fp
->ctf_errs_warnings
;
319 errlist
= &open_errors
;
323 if ((i
= ctf_next_create ()) == NULL
)
328 ctf_set_errno (fp
, ENOMEM
);
333 i
->ctn_iter_fun
= (void (*) (void)) ctf_errwarning_next
;
337 if ((void (*) (void)) ctf_errwarning_next
!= i
->ctn_iter_fun
)
340 *errp
= ECTF_NEXT_WRONGFUN
;
342 ctf_set_errno (fp
, ECTF_NEXT_WRONGFUN
);
346 if (fp
!= i
->cu
.ctn_fp
)
349 *errp
= ECTF_NEXT_WRONGFP
;
351 ctf_set_errno (fp
, ECTF_NEXT_WRONGFP
);
355 cew
= ctf_list_next (errlist
);
359 ctf_next_destroy (i
);
362 *errp
= ECTF_NEXT_END
;
364 ctf_set_errno (fp
, ECTF_NEXT_END
);
369 *is_warning
= cew
->cew_is_warning
;
371 ctf_list_delete (errlist
, cew
);
377 ctf_assert_fail_internal (ctf_dict_t
*fp
, const char *file
, size_t line
,
380 ctf_set_errno (fp
, ECTF_INTERNAL
);
381 ctf_err_warn (fp
, 0, 0, _("%s: %lu: libctf assertion failed: %s"),
382 file
, (long unsigned int) line
, exprstr
);