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>
25 #include <sys/syscall.h>
32 #include <ust/share.h>
35 UST_LOGLEVEL_UNKNOWN
= 0,
40 extern volatile enum ust_loglevel ust_loglevel
;
41 void init_usterr(void);
43 static inline int ust_debug(void)
45 return ust_loglevel
== UST_LOGLEVEL_DEBUG
;
49 //#error UST_COMPONENT is undefined
50 #define UST_COMPONENT libust
53 /* To stringify the expansion of a define */
54 #define UST_XSTR(d) UST_STR(d)
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.
61 extern int ust_safe_snprintf(char *str
, size_t n
, const char *fmt
, ...)
62 __attribute__ ((format (printf
, 3, 4)));
64 static inline void __attribute__ ((format (printf
, 1, 2)))
65 __check_ust_safe_fmt(const char *fmt
, ...)
69 #define sigsafe_print_err(fmt, args...) \
71 /* Can't use dynamic allocation. Limit ourselves to 250 chars. */ \
73 int ____saved_errno; \
75 /* Save the errno. */ \
76 ____saved_errno = errno; \
78 ust_safe_snprintf(____buf, sizeof(____buf), fmt, ## args); \
80 /* Add end of string in case of buffer overflow. */ \
81 ____buf[sizeof(____buf) - 1] = 0; \
83 patient_write(STDERR_FILENO, ____buf, strlen(____buf)); \
85 * Can't print errors because we are in the error printing code \
89 /* Restore errno, in order to be async-signal safe. */ \
90 errno = ____saved_errno; \
93 #define UST_STR_COMPONENT UST_XSTR(UST_COMPONENT)
95 #define ERRMSG(fmt, args...) \
97 sigsafe_print_err(UST_STR_COMPONENT "[%ld/%ld]: " fmt " (in %s() at " __FILE__ ":" UST_XSTR(__LINE__) ")\n", \
99 (long) syscall(SYS_gettid), \
100 ## args, __func__); \
105 # define DBG(fmt, args...) ERRMSG(fmt, ## args)
106 # define DBG_raw(fmt, args...) \
108 sigsafe_print_err(fmt, ## args); \
112 # define DBG(fmt, args...) \
115 ERRMSG(fmt, ## args); \
117 # define DBG_raw(fmt, args...) \
120 sigsafe_print_err(fmt, ## args); \
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)
129 #if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE)
130 #define PERROR(call, args...)\
132 char buf[200] = "Error in strerror_r()"; \
133 strerror_r(errno, buf, sizeof(buf)); \
134 ERRMSG("Error: " call ": %s", ## args, buf); \
137 #define PERROR(call, args...)\
141 buf = strerror_r(errno, tmp, sizeof(tmp)); \
142 ERRMSG("Error: " call ": %s", ## args, buf); \
146 #define BUG_ON(condition) \
148 if (unlikely(condition)) \
149 ERR("condition not respected (BUG) on line %s:%d", __FILE__, __LINE__); \
151 #define WARN_ON(condition) \
153 if (unlikely(condition)) \
154 WARN("condition not respected on line %s:%d", __FILE__, __LINE__); \
156 #define WARN_ON_ONCE(condition) WARN_ON(condition)
158 #endif /* _USTERR_SIGNAL_SAFE_H */