use signal safe ust_safe_snprintf()
[ust.git] / include / usterr.h
CommitLineData
59b161cd
PMF
1#ifndef USTERR_H
2#define USTERR_H
3
d61bfa72 4#include <string.h>
dd80aafe
PMF
5#include <sys/types.h>
6#include <sys/syscall.h>
7#include <errno.h>
ff1fedb9 8#include <stdarg.h>
c1f20530 9#include <stdio.h>
ff1fedb9
PMF
10
11#include "share.h"
d61bfa72 12
2028e7fd
PMF
13#ifndef UST_COMPONENT
14//#error UST_COMPONENT is undefined
15#define UST_COMPONENT libust
16#endif
17
18/* To stringify the expansion of a define */
19#define XSTR(d) STR(d)
20#define STR(s) #s
21
ff1fedb9
PMF
22/* We sometimes print in the tracing path, and tracing can occur in
23 * signal handlers, so we must use a print method which is signal safe.
24 */
25
bf0d695d
PMF
26extern int ust_safe_snprintf(char *str, size_t n, const char *fmt, ...);
27
ff1fedb9
PMF
28#define sigsafe_print_err(fmt, args...) \
29{ \
30 /* Can't use dynamic allocation. Limit ourselves to 250 chars. */ \
31 char ____buf[250]; \
32 int ____saved_errno; \
33\
34 /* Save the errno. */ \
35 ____saved_errno = errno; \
36\
bf0d695d 37 ust_safe_snprintf(____buf, sizeof(____buf), fmt, ## args); \
ff1fedb9
PMF
38\
39 /* Add end of string in case of buffer overflow. */ \
40 ____buf[sizeof(____buf)-1] = 0; \
41\
42 patient_write(STDERR_FILENO, ____buf, strlen(____buf)); \
43 /* Can't print errors because we are in the error printing code path. */ \
44\
45 /* Restore errno, in order to be async-signal safe. */ \
46 errno = ____saved_errno; \
47}
48
2028e7fd
PMF
49#define UST_STR_COMPONENT XSTR(UST_COMPONENT)
50
38f015df 51#define ERRMSG(fmt, args...) do { sigsafe_print_err(UST_STR_COMPONENT "[%ld/%ld]: " fmt " (in %s() at " __FILE__ ":" XSTR(__LINE__) ")\n", (long) getpid(), (long) syscall(SYS_gettid), ## args, __func__); fflush(stderr); } while(0)
dd80aafe 52
02549014 53#ifdef UST_DEBUG
dd80aafe 54# define DBG(fmt, args...) ERRMSG(fmt, ## args)
106e716d 55# define DBG_raw(fmt, args...) do { sigsafe_print_err(fmt, ## args); fflush(stderr); } while(0)
17674885
PMF
56#else
57# define DBG(fmt, args...) do {} while(0)
106e716d 58# define DBG_raw(fmt, args...) do {} while(0)
17674885 59#endif
dd80aafe
PMF
60#define WARN(fmt, args...) ERRMSG("Warning: " fmt, ## args)
61#define ERR(fmt, args...) ERRMSG("Error: " fmt, ## args)
62#define BUG(fmt, args...) ERRMSG("BUG: " fmt, ## args)
d61bfa72
PMF
63
64#if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE)
65#define PERROR(call, args...)\
66 do { \
67 char buf[200] = "Error in strerror_r()"; \
68 strerror_r(errno, buf, sizeof(buf)); \
dd80aafe 69 ERRMSG("Error: " call ": %s", ## args, buf); \
d61bfa72
PMF
70 } while(0);
71#else
72#define PERROR(call, args...)\
73 do { \
74 char *buf; \
75 char tmp[200]; \
76 buf = strerror_r(errno, tmp, sizeof(tmp)); \
dd80aafe 77 ERRMSG("Error: " call ": %s", ## args, buf); \
d61bfa72
PMF
78 } while(0);
79#endif
c9b64079
PMF
80
81#define BUG_ON(condition) do { if (unlikely(condition)) ERR("condition not respected (BUG)"); } while(0)
5f54827b 82#define WARN_ON(condition) do { if (unlikely(condition)) WARN("condition not respected on line %s:%d", __FILE__, __LINE__); } while(0)
c9b64079 83
59b161cd 84#endif /* USTERR_H */
This page took 0.028016 seconds and 4 git commands to generate.