Rename struct lib_ring_buffer* to struct lttng_ust_lib_ring_buffer*
[lttng-ust.git] / include / ust / usterr-signal-safe.h
CommitLineData
5e96a467
MD
1#ifndef _USTERR_SIGNAL_SAFE_H
2#define _USTERR_SIGNAL_SAFE_H
3
4/*
5 * Copyright (C) 2009 Pierre-Marc Fournier
6 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
30ffe279
NC
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
5e96a467
MD
10 * License as published by the Free Software Foundation; version 2.1 of
11 * the License.
30ffe279
NC
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
30ffe279
NC
23#include <string.h>
24#include <sys/types.h>
25#include <sys/syscall.h>
26#include <errno.h>
27#include <stdarg.h>
28#include <stdio.h>
29
30#include <ust/core.h>
31
1dbfff0c 32#include <ust/share.h>
30ffe279 33
5e96a467
MD
34enum ust_loglevel {
35 UST_LOGLEVEL_UNKNOWN = 0,
36 UST_LOGLEVEL_NORMAL,
37 UST_LOGLEVEL_DEBUG,
38};
39
40extern volatile enum ust_loglevel ust_loglevel;
41void init_usterr(void);
42
43static inline int ust_debug(void)
44{
45 return ust_loglevel == UST_LOGLEVEL_DEBUG;
46}
47
30ffe279
NC
48#ifndef UST_COMPONENT
49//#error UST_COMPONENT is undefined
50#define UST_COMPONENT libust
51#endif
52
53/* To stringify the expansion of a define */
1dbfff0c
MD
54#define UST_XSTR(d) UST_STR(d)
55#define UST_STR(s) #s
30ffe279 56
37ed587a
MD
57#define USTERR_MAX_LEN 512
58
30ffe279
NC
59/* We sometimes print in the tracing path, and tracing can occur in
60 * signal handlers, so we must use a print method which is signal safe.
61 */
62
63extern int ust_safe_snprintf(char *str, size_t n, const char *fmt, ...)
64 __attribute__ ((format (printf, 3, 4)));
65
66static inline void __attribute__ ((format (printf, 1, 2)))
67 __check_ust_safe_fmt(const char *fmt, ...)
68{
69}
70
5e96a467
MD
71#define sigsafe_print_err(fmt, args...) \
72{ \
37ed587a
MD
73 /* Can't use dynamic allocation. Limit ourselves to USTERR_MAX_LEN chars. */ \
74 char ____buf[USTERR_MAX_LEN]; \
5e96a467
MD
75 int ____saved_errno; \
76 \
77 /* Save the errno. */ \
78 ____saved_errno = errno; \
79 \
80 ust_safe_snprintf(____buf, sizeof(____buf), fmt, ## args); \
81 \
82 /* Add end of string in case of buffer overflow. */ \
83 ____buf[sizeof(____buf) - 1] = 0; \
84 \
85 patient_write(STDERR_FILENO, ____buf, strlen(____buf)); \
86 /* \
87 * Can't print errors because we are in the error printing code \
88 * path. \
89 */ \
90 \
91 /* Restore errno, in order to be async-signal safe. */ \
92 errno = ____saved_errno; \
30ffe279
NC
93}
94
1dbfff0c 95#define UST_STR_COMPONENT UST_XSTR(UST_COMPONENT)
30ffe279 96
5e96a467
MD
97#define ERRMSG(fmt, args...) \
98 do { \
1dbfff0c 99 sigsafe_print_err(UST_STR_COMPONENT "[%ld/%ld]: " fmt " (in %s() at " __FILE__ ":" UST_XSTR(__LINE__) ")\n", \
5e96a467
MD
100 (long) getpid(), \
101 (long) syscall(SYS_gettid), \
102 ## args, __func__); \
103 fflush(stderr); \
104 } while(0)
30ffe279
NC
105
106#ifdef UST_DEBUG
5e96a467
MD
107# define DBG(fmt, args...) ERRMSG(fmt, ## args)
108# define DBG_raw(fmt, args...) \
109 do { \
110 sigsafe_print_err(fmt, ## args); \
111 fflush(stderr); \
112 } while(0)
30ffe279 113#else
5e96a467
MD
114# define DBG(fmt, args...) \
115 do { \
116 if (ust_debug()) \
117 ERRMSG(fmt, ## args); \
118 } while (0)
119# define DBG_raw(fmt, args...) \
120 do { \
121 if (ust_debug()) { \
122 sigsafe_print_err(fmt, ## args); \
123 fflush(stderr); \
124 } \
125 } while(0)
30ffe279
NC
126#endif
127#define WARN(fmt, args...) ERRMSG("Warning: " fmt, ## args)
128#define ERR(fmt, args...) ERRMSG("Error: " fmt, ## args)
129#define BUG(fmt, args...) ERRMSG("BUG: " fmt, ## args)
130
131#if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE)
132#define PERROR(call, args...)\
133 do { \
134 char buf[200] = "Error in strerror_r()"; \
135 strerror_r(errno, buf, sizeof(buf)); \
136 ERRMSG("Error: " call ": %s", ## args, buf); \
137 } while(0);
138#else
139#define PERROR(call, args...)\
140 do { \
141 char *buf; \
142 char tmp[200]; \
143 buf = strerror_r(errno, tmp, sizeof(tmp)); \
144 ERRMSG("Error: " call ": %s", ## args, buf); \
145 } while(0);
146#endif
147
5e96a467
MD
148#define BUG_ON(condition) \
149 do { \
150 if (unlikely(condition)) \
151 ERR("condition not respected (BUG) on line %s:%d", __FILE__, __LINE__); \
152 } while(0)
153#define WARN_ON(condition) \
154 do { \
155 if (unlikely(condition)) \
156 WARN("condition not respected on line %s:%d", __FILE__, __LINE__); \
157 } while(0)
30ffe279
NC
158#define WARN_ON_ONCE(condition) WARN_ON(condition)
159
160#endif /* _USTERR_SIGNAL_SAFE_H */
This page took 0.028772 seconds and 4 git commands to generate.