Commit | Line | Data |
---|---|---|
5e96a467 | 1 | /* |
c0c0989a | 2 | * SPDX-License-Identifier: LGPL-2.1-only |
30ffe279 | 3 | * |
c0c0989a MJ |
4 | * Copyright (C) 2009 Pierre-Marc Fournier |
5 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
30ffe279 NC |
6 | */ |
7 | ||
c0c0989a MJ |
8 | #ifndef _USTERR_SIGNAL_SAFE_H |
9 | #define _USTERR_SIGNAL_SAFE_H | |
10 | ||
30ffe279 NC |
11 | #include <string.h> |
12 | #include <sys/types.h> | |
30ffe279 NC |
13 | #include <errno.h> |
14 | #include <stdarg.h> | |
2b6f4374 | 15 | #include <stdbool.h> |
30ffe279 | 16 | #include <stdio.h> |
864a1eda | 17 | #include <ust-share.h> |
ae4b659d | 18 | #include "ust-tid.h" |
622671fb | 19 | #include "ust-snprintf.h" |
30ffe279 | 20 | |
2b6f4374 MJ |
21 | enum ust_err_loglevel { |
22 | UST_ERR_LOGLEVEL_UNKNOWN = 0, | |
23 | UST_ERR_LOGLEVEL_NORMAL, | |
24 | UST_ERR_LOGLEVEL_DEBUG, | |
5e96a467 MD |
25 | }; |
26 | ||
1d18d519 MJ |
27 | extern volatile enum ust_err_loglevel ust_err_loglevel |
28 | __attribute__((visibility("hidden"))); | |
ddabe860 | 29 | |
1d18d519 MJ |
30 | void ust_err_init(void) |
31 | __attribute__((visibility("hidden"))); | |
5e96a467 | 32 | |
7bdd21a4 | 33 | #ifdef LTTNG_UST_DEBUG |
2b6f4374 | 34 | static inline bool ust_err_debug_enabled(void) |
7bdd21a4 | 35 | { |
2b6f4374 | 36 | return true; |
7bdd21a4 MD |
37 | } |
38 | #else /* #ifdef LTTNG_UST_DEBUG */ | |
2b6f4374 | 39 | static inline bool ust_err_debug_enabled(void) |
5e96a467 | 40 | { |
2b6f4374 | 41 | return ust_err_loglevel == UST_ERR_LOGLEVEL_DEBUG; |
5e96a467 | 42 | } |
7bdd21a4 | 43 | #endif /* #else #ifdef LTTNG_UST_DEBUG */ |
5e96a467 | 44 | |
2b6f4374 MJ |
45 | /* |
46 | * The default component for error messages. | |
47 | */ | |
30ffe279 | 48 | #ifndef UST_COMPONENT |
30ffe279 NC |
49 | #define UST_COMPONENT libust |
50 | #endif | |
51 | ||
52 | /* To stringify the expansion of a define */ | |
1dbfff0c MD |
53 | #define UST_XSTR(d) UST_STR(d) |
54 | #define UST_STR(s) #s | |
30ffe279 | 55 | |
2b6f4374 | 56 | #define UST_ERR_MAX_LEN 512 |
37ed587a | 57 | |
2b6f4374 MJ |
58 | /* |
59 | * We sometimes print in the tracing path, and tracing can occur in | |
30ffe279 NC |
60 | * signal handlers, so we must use a print method which is signal safe. |
61 | */ | |
2b6f4374 | 62 | /* Can't use dynamic allocation. Limit ourselves to UST_ERR_MAX_LEN chars. */ |
648bb724 | 63 | /* Add end of string in case of buffer overflow. */ |
5e96a467 | 64 | #define sigsafe_print_err(fmt, args...) \ |
648bb724 | 65 | do { \ |
2b6f4374 MJ |
66 | if (ust_err_debug_enabled()) { \ |
67 | char ____buf[UST_ERR_MAX_LEN]; \ | |
7bdd21a4 MD |
68 | int ____saved_errno; \ |
69 | \ | |
70 | ____saved_errno = errno; /* signal-safety */ \ | |
71 | ust_safe_snprintf(____buf, sizeof(____buf), fmt, ## args); \ | |
72 | ____buf[sizeof(____buf) - 1] = 0; \ | |
516d12da | 73 | ust_patient_write(STDERR_FILENO, ____buf, strlen(____buf)); \ |
7bdd21a4 MD |
74 | errno = ____saved_errno; /* signal-safety */ \ |
75 | fflush(stderr); \ | |
76 | } \ | |
648bb724 | 77 | } while (0) |
30ffe279 | 78 | |
1dbfff0c | 79 | #define UST_STR_COMPONENT UST_XSTR(UST_COMPONENT) |
30ffe279 | 80 | |
5e96a467 MD |
81 | #define ERRMSG(fmt, args...) \ |
82 | do { \ | |
7bdd21a4 | 83 | sigsafe_print_err(UST_STR_COMPONENT "[%ld/%ld]: " fmt " (in %s() at " __FILE__ ":" UST_XSTR(__LINE__) ")\n", \ |
5e96a467 | 84 | (long) getpid(), \ |
0f029713 | 85 | (long) lttng_gettid(), \ |
5e96a467 | 86 | ## args, __func__); \ |
5e96a467 | 87 | } while(0) |
30ffe279 | 88 | |
7bdd21a4 MD |
89 | |
90 | #define DBG(fmt, args...) ERRMSG(fmt, ## args) | |
91 | #define DBG_raw(fmt, args...) sigsafe_print_err(fmt, ## args) | |
92 | #define WARN(fmt, args...) ERRMSG("Warning: " fmt, ## args) | |
93 | #define ERR(fmt, args...) ERRMSG("Error: " fmt, ## args) | |
94 | #define BUG(fmt, args...) ERRMSG("BUG: " fmt, ## args) | |
30ffe279 | 95 | |
b52af467 | 96 | #if !defined(__GLIBC__) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE)) |
9fd26814 MD |
97 | /* |
98 | * Version using XSI strerror_r. | |
99 | */ | |
7bdd21a4 MD |
100 | #define PERROR(call, args...) \ |
101 | do { \ | |
2b6f4374 | 102 | if (ust_err_debug_enabled()) { \ |
4041a8a7 MJ |
103 | char perror_buf[200] = "Error in strerror_r()"; \ |
104 | strerror_r(errno, perror_buf, \ | |
105 | sizeof(perror_buf)); \ | |
106 | ERRMSG("Error: " call ": %s", ## args, \ | |
107 | perror_buf); \ | |
7bdd21a4 MD |
108 | } \ |
109 | } while(0) | |
30ffe279 | 110 | #else |
9fd26814 MD |
111 | /* |
112 | * Version using GNU strerror_r, for linux with appropriate defines. | |
113 | */ | |
7bdd21a4 MD |
114 | #define PERROR(call, args...) \ |
115 | do { \ | |
2b6f4374 | 116 | if (ust_err_debug_enabled()) { \ |
4041a8a7 MJ |
117 | char *perror_buf; \ |
118 | char perror_tmp[200]; \ | |
119 | perror_buf = strerror_r(errno, perror_tmp, \ | |
120 | sizeof(perror_tmp)); \ | |
121 | ERRMSG("Error: " call ": %s", ## args, \ | |
122 | perror_buf); \ | |
7bdd21a4 MD |
123 | } \ |
124 | } while(0) | |
30ffe279 NC |
125 | #endif |
126 | ||
5e96a467 MD |
127 | #define BUG_ON(condition) \ |
128 | do { \ | |
b5a3dfa5 | 129 | if (caa_unlikely(condition)) \ |
5e96a467 MD |
130 | ERR("condition not respected (BUG) on line %s:%d", __FILE__, __LINE__); \ |
131 | } while(0) | |
132 | #define WARN_ON(condition) \ | |
133 | do { \ | |
b5a3dfa5 | 134 | if (caa_unlikely(condition)) \ |
5e96a467 MD |
135 | WARN("condition not respected on line %s:%d", __FILE__, __LINE__); \ |
136 | } while(0) | |
30ffe279 NC |
137 | #define WARN_ON_ONCE(condition) WARN_ON(condition) |
138 | ||
139 | #endif /* _USTERR_SIGNAL_SAFE_H */ |