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