3d6756872fd6943a18ca83149bde06349fb084c8
[lttng-ust.git] / include / usterr-signal-safe.h
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2009 Pierre-Marc Fournier
5 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 */
7
8 #ifndef _USTERR_SIGNAL_SAFE_H
9 #define _USTERR_SIGNAL_SAFE_H
10
11 #include <string.h>
12 #include <sys/types.h>
13 #include <errno.h>
14 #include <stdarg.h>
15 #include <stdio.h>
16 #include <ust-share.h>
17 #include "ust-tid.h"
18 #include "ust-snprintf.h"
19
20 enum ust_loglevel {
21 UST_LOGLEVEL_UNKNOWN = 0,
22 UST_LOGLEVEL_NORMAL,
23 UST_LOGLEVEL_DEBUG,
24 };
25
26 extern volatile enum ust_loglevel ust_loglevel;
27 void init_usterr(void);
28
29 #ifdef LTTNG_UST_DEBUG
30 static inline int ust_debug(void)
31 {
32 return 1;
33 }
34 #else /* #ifdef LTTNG_UST_DEBUG */
35 static inline int ust_debug(void)
36 {
37 return ust_loglevel == UST_LOGLEVEL_DEBUG;
38 }
39 #endif /* #else #ifdef LTTNG_UST_DEBUG */
40
41 #ifndef UST_COMPONENT
42 //#error UST_COMPONENT is undefined
43 #define UST_COMPONENT libust
44 #endif
45
46 /* To stringify the expansion of a define */
47 #define UST_XSTR(d) UST_STR(d)
48 #define UST_STR(s) #s
49
50 #define USTERR_MAX_LEN 512
51
52 /* We sometimes print in the tracing path, and tracing can occur in
53 * signal handlers, so we must use a print method which is signal safe.
54 */
55 /* Can't use dynamic allocation. Limit ourselves to USTERR_MAX_LEN chars. */
56 /* Add end of string in case of buffer overflow. */
57 #define sigsafe_print_err(fmt, args...) \
58 do { \
59 if (ust_debug()) { \
60 char ____buf[USTERR_MAX_LEN]; \
61 int ____saved_errno; \
62 \
63 ____saved_errno = errno; /* signal-safety */ \
64 ust_safe_snprintf(____buf, sizeof(____buf), fmt, ## args); \
65 ____buf[sizeof(____buf) - 1] = 0; \
66 ust_patient_write(STDERR_FILENO, ____buf, strlen(____buf)); \
67 errno = ____saved_errno; /* signal-safety */ \
68 fflush(stderr); \
69 } \
70 } while (0)
71
72 #define UST_STR_COMPONENT UST_XSTR(UST_COMPONENT)
73
74 #define ERRMSG(fmt, args...) \
75 do { \
76 sigsafe_print_err(UST_STR_COMPONENT "[%ld/%ld]: " fmt " (in %s() at " __FILE__ ":" UST_XSTR(__LINE__) ")\n", \
77 (long) getpid(), \
78 (long) lttng_gettid(), \
79 ## args, __func__); \
80 } while(0)
81
82
83 #define DBG(fmt, args...) ERRMSG(fmt, ## args)
84 #define DBG_raw(fmt, args...) sigsafe_print_err(fmt, ## args)
85 #define WARN(fmt, args...) ERRMSG("Warning: " fmt, ## args)
86 #define ERR(fmt, args...) ERRMSG("Error: " fmt, ## args)
87 #define BUG(fmt, args...) ERRMSG("BUG: " fmt, ## args)
88
89 #if !defined(__GLIBC__) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE))
90 /*
91 * Version using XSI strerror_r.
92 */
93 #define PERROR(call, args...) \
94 do { \
95 if (ust_debug()) { \
96 char buf[200] = "Error in strerror_r()"; \
97 strerror_r(errno, buf, sizeof(buf)); \
98 ERRMSG("Error: " call ": %s", ## args, buf); \
99 } \
100 } while(0)
101 #else
102 /*
103 * Version using GNU strerror_r, for linux with appropriate defines.
104 */
105 #define PERROR(call, args...) \
106 do { \
107 if (ust_debug()) { \
108 char *buf; \
109 char tmp[200]; \
110 buf = strerror_r(errno, tmp, sizeof(tmp)); \
111 ERRMSG("Error: " call ": %s", ## args, buf); \
112 } \
113 } while(0)
114 #endif
115
116 #define BUG_ON(condition) \
117 do { \
118 if (caa_unlikely(condition)) \
119 ERR("condition not respected (BUG) on line %s:%d", __FILE__, __LINE__); \
120 } while(0)
121 #define WARN_ON(condition) \
122 do { \
123 if (caa_unlikely(condition)) \
124 WARN("condition not respected on line %s:%d", __FILE__, __LINE__); \
125 } while(0)
126 #define WARN_ON_ONCE(condition) WARN_ON(condition)
127
128 #endif /* _USTERR_SIGNAL_SAFE_H */
This page took 0.03146 seconds and 3 git commands to generate.