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