1 #ifndef _USTERR_SIGNAL_SAFE_H
2 #define _USTERR_SIGNAL_SAFE_H
5 * Copyright (C) 2009 Pierre-Marc Fournier
6 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; version 2.1 of
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.
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
24 #include <sys/types.h>
29 #include "lttng/ust-tid.h"
32 UST_LOGLEVEL_UNKNOWN
= 0,
37 extern volatile enum ust_loglevel ust_loglevel
;
38 void init_usterr(void);
40 #ifdef LTTNG_UST_DEBUG
41 static inline int ust_debug(void)
45 #else /* #ifdef LTTNG_UST_DEBUG */
46 static inline int ust_debug(void)
48 return ust_loglevel
== UST_LOGLEVEL_DEBUG
;
50 #endif /* #else #ifdef LTTNG_UST_DEBUG */
53 //#error UST_COMPONENT is undefined
54 #define UST_COMPONENT libust
57 /* To stringify the expansion of a define */
58 #define UST_XSTR(d) UST_STR(d)
61 #define USTERR_MAX_LEN 512
63 /* We sometimes print in the tracing path, and tracing can occur in
64 * signal handlers, so we must use a print method which is signal safe.
67 extern int ust_safe_snprintf(char *str
, size_t n
, const char *fmt
, ...)
68 __attribute__ ((format (printf
, 3, 4)));
70 /* Can't use dynamic allocation. Limit ourselves to USTERR_MAX_LEN chars. */
71 /* Add end of string in case of buffer overflow. */
72 #define sigsafe_print_err(fmt, args...) \
75 char ____buf[USTERR_MAX_LEN]; \
76 int ____saved_errno; \
78 ____saved_errno = errno; /* signal-safety */ \
79 ust_safe_snprintf(____buf, sizeof(____buf), fmt, ## args); \
80 ____buf[sizeof(____buf) - 1] = 0; \
81 patient_write(STDERR_FILENO, ____buf, strlen(____buf)); \
82 errno = ____saved_errno; /* signal-safety */ \
87 #define UST_STR_COMPONENT UST_XSTR(UST_COMPONENT)
89 #define ERRMSG(fmt, args...) \
91 sigsafe_print_err(UST_STR_COMPONENT "[%ld/%ld]: " fmt " (in %s() at " __FILE__ ":" UST_XSTR(__LINE__) ")\n", \
93 (long) lttng_gettid(), \
98 #define DBG(fmt, args...) ERRMSG(fmt, ## args)
99 #define DBG_raw(fmt, args...) sigsafe_print_err(fmt, ## args)
100 #define WARN(fmt, args...) ERRMSG("Warning: " fmt, ## args)
101 #define ERR(fmt, args...) ERRMSG("Error: " fmt, ## args)
102 #define BUG(fmt, args...) ERRMSG("BUG: " fmt, ## args)
104 #if !defined(__GLIBC__) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE))
106 * Version using XSI strerror_r.
108 #define PERROR(call, args...) \
111 char buf[200] = "Error in strerror_r()"; \
112 strerror_r(errno, buf, sizeof(buf)); \
113 ERRMSG("Error: " call ": %s", ## args, buf); \
118 * Version using GNU strerror_r, for linux with appropriate defines.
120 #define PERROR(call, args...) \
125 buf = strerror_r(errno, tmp, sizeof(tmp)); \
126 ERRMSG("Error: " call ": %s", ## args, buf); \
131 #define BUG_ON(condition) \
133 if (caa_unlikely(condition)) \
134 ERR("condition not respected (BUG) on line %s:%d", __FILE__, __LINE__); \
136 #define WARN_ON(condition) \
138 if (caa_unlikely(condition)) \
139 WARN("condition not respected on line %s:%d", __FILE__, __LINE__); \
141 #define WARN_ON_ONCE(condition) WARN_ON(condition)
143 #endif /* _USTERR_SIGNAL_SAFE_H */