usterr-signal-safe.h: ensure all macros are trivial (10 lines max)
[lttng-ust.git] / include / lttng / 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
4318ae1b
MD
30#include <lttng/core.h>
31#include <lttng/share.h>
30ffe279 32
5e96a467
MD
33enum ust_loglevel {
34 UST_LOGLEVEL_UNKNOWN = 0,
35 UST_LOGLEVEL_NORMAL,
36 UST_LOGLEVEL_DEBUG,
37};
38
39extern volatile enum ust_loglevel ust_loglevel;
40void init_usterr(void);
41
42static inline int ust_debug(void)
43{
44 return ust_loglevel == UST_LOGLEVEL_DEBUG;
45}
46
30ffe279
NC
47#ifndef UST_COMPONENT
48//#error UST_COMPONENT is undefined
49#define UST_COMPONENT libust
50#endif
51
52/* To stringify the expansion of a define */
1dbfff0c
MD
53#define UST_XSTR(d) UST_STR(d)
54#define UST_STR(s) #s
30ffe279 55
37ed587a
MD
56#define USTERR_MAX_LEN 512
57
30ffe279
NC
58/* We sometimes print in the tracing path, and tracing can occur in
59 * signal handlers, so we must use a print method which is signal safe.
60 */
61
62extern int ust_safe_snprintf(char *str, size_t n, const char *fmt, ...)
63 __attribute__ ((format (printf, 3, 4)));
64
65static inline void __attribute__ ((format (printf, 1, 2)))
66 __check_ust_safe_fmt(const char *fmt, ...)
67{
68}
69
648bb724
MD
70/* Can't use dynamic allocation. Limit ourselves to USTERR_MAX_LEN chars. */
71/* Add end of string in case of buffer overflow. */
5e96a467 72#define sigsafe_print_err(fmt, args...) \
648bb724 73do { \
37ed587a 74 char ____buf[USTERR_MAX_LEN]; \
5e96a467 75 int ____saved_errno; \
648bb724 76 ____saved_errno = errno; /* signal-safety */ \
5e96a467 77 ust_safe_snprintf(____buf, sizeof(____buf), fmt, ## args); \
5e96a467 78 ____buf[sizeof(____buf) - 1] = 0; \
5e96a467 79 patient_write(STDERR_FILENO, ____buf, strlen(____buf)); \
648bb724
MD
80 errno = ____saved_errno; /* signal-safety */ \
81} while (0)
30ffe279 82
1dbfff0c 83#define UST_STR_COMPONENT UST_XSTR(UST_COMPONENT)
30ffe279 84
5e96a467
MD
85#define ERRMSG(fmt, args...) \
86 do { \
1dbfff0c 87 sigsafe_print_err(UST_STR_COMPONENT "[%ld/%ld]: " fmt " (in %s() at " __FILE__ ":" UST_XSTR(__LINE__) ")\n", \
5e96a467
MD
88 (long) getpid(), \
89 (long) syscall(SYS_gettid), \
90 ## args, __func__); \
91 fflush(stderr); \
92 } while(0)
30ffe279 93
69400ac4 94#ifdef LTTNG_UST_DEBUG
5e96a467
MD
95# define DBG(fmt, args...) ERRMSG(fmt, ## args)
96# define DBG_raw(fmt, args...) \
97 do { \
98 sigsafe_print_err(fmt, ## args); \
99 fflush(stderr); \
100 } while(0)
30ffe279 101#else
5e96a467
MD
102# define DBG(fmt, args...) \
103 do { \
104 if (ust_debug()) \
105 ERRMSG(fmt, ## args); \
106 } while (0)
107# define DBG_raw(fmt, args...) \
108 do { \
109 if (ust_debug()) { \
110 sigsafe_print_err(fmt, ## args); \
111 fflush(stderr); \
112 } \
113 } while(0)
30ffe279
NC
114#endif
115#define WARN(fmt, args...) ERRMSG("Warning: " fmt, ## args)
116#define ERR(fmt, args...) ERRMSG("Error: " fmt, ## args)
117#define BUG(fmt, args...) ERRMSG("BUG: " fmt, ## args)
118
119#if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE)
120#define PERROR(call, args...)\
121 do { \
122 char buf[200] = "Error in strerror_r()"; \
123 strerror_r(errno, buf, sizeof(buf)); \
124 ERRMSG("Error: " call ": %s", ## args, buf); \
125 } while(0);
126#else
127#define PERROR(call, args...)\
128 do { \
129 char *buf; \
130 char tmp[200]; \
131 buf = strerror_r(errno, tmp, sizeof(tmp)); \
132 ERRMSG("Error: " call ": %s", ## args, buf); \
133 } while(0);
134#endif
135
5e96a467
MD
136#define BUG_ON(condition) \
137 do { \
b5a3dfa5 138 if (caa_unlikely(condition)) \
5e96a467
MD
139 ERR("condition not respected (BUG) on line %s:%d", __FILE__, __LINE__); \
140 } while(0)
141#define WARN_ON(condition) \
142 do { \
b5a3dfa5 143 if (caa_unlikely(condition)) \
5e96a467
MD
144 WARN("condition not respected on line %s:%d", __FILE__, __LINE__); \
145 } while(0)
30ffe279
NC
146#define WARN_ON_ONCE(condition) WARN_ON(condition)
147
148#endif /* _USTERR_SIGNAL_SAFE_H */
This page took 0.029667 seconds and 4 git commands to generate.