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