75d507ac10825825ec931a00f561d625192d1690
[ust.git] / include / usterr.h
1 #ifndef USTERR_H
2 #define USTERR_H
3
4 #include <string.h>
5 #include <sys/types.h>
6 #include <sys/syscall.h>
7 #include <errno.h>
8 #include <stdarg.h>
9 #include <stdio.h>
10
11 #include "share.h"
12
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
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
26 #define sigsafe_print_err(fmt, args...) \
27 { \
28 /* Can't use dynamic allocation. Limit ourselves to 250 chars. */ \
29 char ____buf[250]; \
30 int ____saved_errno; \
31 \
32 /* Save the errno. */ \
33 ____saved_errno = errno; \
34 \
35 snprintf(____buf, sizeof(____buf), fmt, ## args); \
36 \
37 /* Add end of string in case of buffer overflow. */ \
38 ____buf[sizeof(____buf)-1] = 0; \
39 \
40 patient_write(STDERR_FILENO, ____buf, strlen(____buf)); \
41 /* Can't print errors because we are in the error printing code path. */ \
42 \
43 /* Restore errno, in order to be async-signal safe. */ \
44 errno = ____saved_errno; \
45 }
46
47 #define UST_STR_COMPONENT XSTR(UST_COMPONENT)
48
49 #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)
50
51 #ifdef UST_DEBUG
52 # define DBG(fmt, args...) ERRMSG(fmt, ## args)
53 # define DBG_raw(fmt, args...) do { sigsafe_print_err(fmt, ## args); fflush(stderr); } while(0)
54 #else
55 # define DBG(fmt, args...) do {} while(0)
56 # define DBG_raw(fmt, args...) do {} while(0)
57 #endif
58 #define WARN(fmt, args...) ERRMSG("Warning: " fmt, ## args)
59 #define ERR(fmt, args...) ERRMSG("Error: " fmt, ## args)
60 #define BUG(fmt, args...) ERRMSG("BUG: " fmt, ## args)
61
62 #if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE)
63 #define PERROR(call, args...)\
64 do { \
65 char buf[200] = "Error in strerror_r()"; \
66 strerror_r(errno, buf, sizeof(buf)); \
67 ERRMSG("Error: " call ": %s", ## args, buf); \
68 } while(0);
69 #else
70 #define PERROR(call, args...)\
71 do { \
72 char *buf; \
73 char tmp[200]; \
74 buf = strerror_r(errno, tmp, sizeof(tmp)); \
75 ERRMSG("Error: " call ": %s", ## args, buf); \
76 } while(0);
77 #endif
78
79 #define BUG_ON(condition) do { if (unlikely(condition)) ERR("condition not respected (BUG)"); } while(0)
80 #define WARN_ON(condition) do { if (unlikely(condition)) WARN("condition not respected on line %s:%d", __FILE__, __LINE__); } while(0)
81
82 #endif /* USTERR_H */
This page took 0.030615 seconds and 3 git commands to generate.