Add UST_DEBUG env. var. support
[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>
25#include <sys/syscall.h>
26#include <errno.h>
27#include <stdarg.h>
28#include <stdio.h>
29
30#include <ust/core.h>
31
32#include "share.h"
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 */
54#define XSTR(d) STR(d)
55#define STR(s) #s
56
57/* We sometimes print in the tracing path, and tracing can occur in
58 * signal handlers, so we must use a print method which is signal safe.
59 */
60
61extern int ust_safe_snprintf(char *str, size_t n, const char *fmt, ...)
62 __attribute__ ((format (printf, 3, 4)));
63
64static inline void __attribute__ ((format (printf, 1, 2)))
65 __check_ust_safe_fmt(const char *fmt, ...)
66{
67}
68
5e96a467
MD
69#define sigsafe_print_err(fmt, args...) \
70{ \
30ffe279 71 /* Can't use dynamic allocation. Limit ourselves to 250 chars. */ \
5e96a467
MD
72 char ____buf[250]; \
73 int ____saved_errno; \
74 \
75 /* Save the errno. */ \
76 ____saved_errno = errno; \
77 \
78 ust_safe_snprintf(____buf, sizeof(____buf), fmt, ## args); \
79 \
80 /* Add end of string in case of buffer overflow. */ \
81 ____buf[sizeof(____buf) - 1] = 0; \
82 \
83 patient_write(STDERR_FILENO, ____buf, strlen(____buf)); \
84 /* \
85 * Can't print errors because we are in the error printing code \
86 * path. \
87 */ \
88 \
89 /* Restore errno, in order to be async-signal safe. */ \
90 errno = ____saved_errno; \
30ffe279
NC
91}
92
93#define UST_STR_COMPONENT XSTR(UST_COMPONENT)
94
5e96a467
MD
95#define ERRMSG(fmt, args...) \
96 do { \
97 sigsafe_print_err(UST_STR_COMPONENT "[%ld/%ld]: " fmt " (in %s() at " __FILE__ ":" XSTR(__LINE__) ")\n", \
98 (long) getpid(), \
99 (long) syscall(SYS_gettid), \
100 ## args, __func__); \
101 fflush(stderr); \
102 } while(0)
30ffe279
NC
103
104#ifdef UST_DEBUG
5e96a467
MD
105# define DBG(fmt, args...) ERRMSG(fmt, ## args)
106# define DBG_raw(fmt, args...) \
107 do { \
108 sigsafe_print_err(fmt, ## args); \
109 fflush(stderr); \
110 } while(0)
30ffe279 111#else
5e96a467
MD
112# define DBG(fmt, args...) \
113 do { \
114 if (ust_debug()) \
115 ERRMSG(fmt, ## args); \
116 } while (0)
117# define DBG_raw(fmt, args...) \
118 do { \
119 if (ust_debug()) { \
120 sigsafe_print_err(fmt, ## args); \
121 fflush(stderr); \
122 } \
123 } while(0)
30ffe279
NC
124#endif
125#define WARN(fmt, args...) ERRMSG("Warning: " fmt, ## args)
126#define ERR(fmt, args...) ERRMSG("Error: " fmt, ## args)
127#define BUG(fmt, args...) ERRMSG("BUG: " fmt, ## args)
128
129#if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE)
130#define PERROR(call, args...)\
131 do { \
132 char buf[200] = "Error in strerror_r()"; \
133 strerror_r(errno, buf, sizeof(buf)); \
134 ERRMSG("Error: " call ": %s", ## args, buf); \
135 } while(0);
136#else
137#define PERROR(call, args...)\
138 do { \
139 char *buf; \
140 char tmp[200]; \
141 buf = strerror_r(errno, tmp, sizeof(tmp)); \
142 ERRMSG("Error: " call ": %s", ## args, buf); \
143 } while(0);
144#endif
145
5e96a467
MD
146#define BUG_ON(condition) \
147 do { \
148 if (unlikely(condition)) \
149 ERR("condition not respected (BUG) on line %s:%d", __FILE__, __LINE__); \
150 } while(0)
151#define WARN_ON(condition) \
152 do { \
153 if (unlikely(condition)) \
154 WARN("condition not respected on line %s:%d", __FILE__, __LINE__); \
155 } while(0)
30ffe279
NC
156#define WARN_ON_ONCE(condition) WARN_ON(condition)
157
5e96a467 158
30ffe279 159#endif /* _USTERR_SIGNAL_SAFE_H */
This page took 0.030965 seconds and 4 git commands to generate.