| 1 | /* |
| 2 | * Copyright (C) 2011 EfficiOS Inc. |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #ifndef _ERROR_H |
| 9 | #define _ERROR_H |
| 10 | |
| 11 | #include <common/compat/errno.h> |
| 12 | #include <stdio.h> |
| 13 | #include <stdint.h> |
| 14 | #include <string.h> |
| 15 | #include <stdbool.h> |
| 16 | #include <urcu/tls-compat.h> |
| 17 | #include <common/compat/time.h> |
| 18 | #include <common/string-utils/format.h> |
| 19 | #include <common/macros.h> |
| 20 | |
| 21 | #ifndef _GNU_SOURCE |
| 22 | #error "lttng-tools error.h needs _GNU_SOURCE" |
| 23 | #endif |
| 24 | |
| 25 | #include <lttng/lttng-error.h> |
| 26 | #include <common/compat/tid.h> |
| 27 | |
| 28 | /* Avoid conflict with Solaris <sys/regset.h> */ |
| 29 | #if defined(ERR) && defined(__sun__) |
| 30 | #undef ERR |
| 31 | #endif |
| 32 | |
| 33 | /* Stringify the expansion of a define */ |
| 34 | #define XSTR(d) STR(d) |
| 35 | #define STR(s) #s |
| 36 | |
| 37 | /* |
| 38 | * Contains the string of the log entry time. This is used as a thread local |
| 39 | * storage so we don't race between thread and also avoid memory allocation |
| 40 | * every time a log is fired. |
| 41 | */ |
| 42 | struct log_time { |
| 43 | /* Format: 00:00:00.000000000 plus NULL byte. */ |
| 44 | char str[19]; |
| 45 | }; |
| 46 | extern LTTNG_EXPORT DECLARE_URCU_TLS(struct log_time, error_log_time); |
| 47 | extern DECLARE_URCU_TLS(const char *, logger_thread_name); |
| 48 | |
| 49 | extern int lttng_opt_quiet; |
| 50 | extern int lttng_opt_verbose; |
| 51 | extern int lttng_opt_mi; |
| 52 | |
| 53 | /* Error type. */ |
| 54 | enum lttng_error_level { |
| 55 | PRINT_ERR = 0, |
| 56 | PRINT_BUG = 1, |
| 57 | PRINT_WARN = 2, |
| 58 | PRINT_MSG = 3, |
| 59 | PRINT_DBG = 4, |
| 60 | PRINT_DBG2 = 5, |
| 61 | PRINT_DBG3 = 6, |
| 62 | }; |
| 63 | |
| 64 | static inline bool __lttng_print_check_opt(enum lttng_error_level type) |
| 65 | { |
| 66 | /* lttng_opt_mi and lttng_opt_quiet. */ |
| 67 | switch (type) { |
| 68 | case PRINT_DBG3: |
| 69 | case PRINT_DBG2: |
| 70 | case PRINT_DBG: |
| 71 | case PRINT_MSG: |
| 72 | if (lttng_opt_mi) { |
| 73 | return false; |
| 74 | } |
| 75 | /* Fall-through. */ |
| 76 | case PRINT_WARN: |
| 77 | case PRINT_BUG: |
| 78 | case PRINT_ERR: |
| 79 | if (lttng_opt_quiet) { |
| 80 | return false; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /* lttng_opt_verbose */ |
| 85 | switch (type) { |
| 86 | case PRINT_DBG3: |
| 87 | if (lttng_opt_verbose < 3) { |
| 88 | return false; |
| 89 | } |
| 90 | break; |
| 91 | case PRINT_DBG2: |
| 92 | if (lttng_opt_verbose < 2) { |
| 93 | return false; |
| 94 | } |
| 95 | break; |
| 96 | case PRINT_DBG: |
| 97 | if (lttng_opt_verbose < 1) { |
| 98 | return false; |
| 99 | } |
| 100 | break; |
| 101 | case PRINT_MSG: |
| 102 | case PRINT_WARN: |
| 103 | case PRINT_BUG: |
| 104 | case PRINT_ERR: |
| 105 | break; |
| 106 | } |
| 107 | |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | C_LINKAGE void lttng_abort_on_error(void); |
| 112 | |
| 113 | static inline void __lttng_print_check_abort(enum lttng_error_level type) |
| 114 | { |
| 115 | switch (type) { |
| 116 | case PRINT_DBG3: |
| 117 | case PRINT_DBG2: |
| 118 | case PRINT_DBG: |
| 119 | case PRINT_MSG: |
| 120 | case PRINT_WARN: |
| 121 | break; |
| 122 | case PRINT_BUG: |
| 123 | case PRINT_ERR: |
| 124 | lttng_abort_on_error(); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * Macro for printing message depending on command line option and verbosity. |
| 130 | * |
| 131 | * Machine interface: |
| 132 | * We use lttng_opt_mi to suppress all normal msg to stdout. We don't |
| 133 | * want any nested msg to show up when printing mi to stdout(if it's the case). |
| 134 | * All warnings and errors should be printed to stderr as normal. |
| 135 | */ |
| 136 | #define __lttng_print(type, fmt, args...) \ |
| 137 | do { \ |
| 138 | if (__lttng_print_check_opt(type)) { \ |
| 139 | fprintf((type) == PRINT_MSG ? stdout : stderr, fmt, ## args); \ |
| 140 | } \ |
| 141 | __lttng_print_check_abort(type); \ |
| 142 | } while (0) |
| 143 | |
| 144 | /* Three level of debug. Use -v, -vv or -vvv for the levels */ |
| 145 | #define _ERRMSG(msg, type, fmt, args...) \ |
| 146 | do { \ |
| 147 | if (caa_unlikely(__lttng_print_check_opt(type))) { \ |
| 148 | char generic_name[MAX_INT_DEC_LEN(long) + \ |
| 149 | MAX_INT_DEC_LEN(long)]; \ |
| 150 | \ |
| 151 | snprintf(generic_name, sizeof(generic_name), \ |
| 152 | "%ld/%ld", (long) getpid(), \ |
| 153 | (long) lttng_gettid()); \ |
| 154 | \ |
| 155 | __lttng_print(type, \ |
| 156 | msg " - %s [%s]: " fmt \ |
| 157 | " (in %s() at " __FILE__ \ |
| 158 | ":" XSTR(__LINE__) ")\n", \ |
| 159 | log_add_time(), \ |
| 160 | URCU_TLS(logger_thread_name) ?: \ |
| 161 | generic_name, \ |
| 162 | ##args, __func__); \ |
| 163 | } \ |
| 164 | } while (0) |
| 165 | |
| 166 | #define _ERRMSG_NO_LOC(msg, type, fmt, args...) \ |
| 167 | do { \ |
| 168 | if (caa_unlikely(__lttng_print_check_opt(type))) { \ |
| 169 | char generic_name[MAX_INT_DEC_LEN(long) + \ |
| 170 | MAX_INT_DEC_LEN(long)]; \ |
| 171 | \ |
| 172 | snprintf(generic_name, sizeof(generic_name), \ |
| 173 | "%ld/%ld", (long) getpid(), \ |
| 174 | (long) lttng_gettid()); \ |
| 175 | \ |
| 176 | __lttng_print(type, msg " - %s [%s]: " fmt "\n", \ |
| 177 | log_add_time(), \ |
| 178 | URCU_TLS(logger_thread_name) ?: \ |
| 179 | generic_name, \ |
| 180 | ##args); \ |
| 181 | } \ |
| 182 | } while (0) |
| 183 | |
| 184 | #define MSG(fmt, args...) \ |
| 185 | __lttng_print(PRINT_MSG, fmt "\n", ## args) |
| 186 | #define _MSG(fmt, args...) \ |
| 187 | __lttng_print(PRINT_MSG, fmt, ## args) |
| 188 | #define ERR(fmt, args...) \ |
| 189 | __lttng_print(PRINT_ERR, "Error: " fmt "\n", ## args) |
| 190 | #define WARN(fmt, args...) \ |
| 191 | __lttng_print(PRINT_WARN, "Warning: " fmt "\n", ## args) |
| 192 | |
| 193 | #define BUG(fmt, args...) _ERRMSG("BUG", PRINT_BUG, fmt, ## args) |
| 194 | |
| 195 | #define DBG(fmt, args...) _ERRMSG("DBG1", PRINT_DBG, fmt, ## args) |
| 196 | #define DBG_NO_LOC(fmt, args...) _ERRMSG_NO_LOC("DBG1", PRINT_DBG, fmt, ## args) |
| 197 | #define DBG2(fmt, args...) _ERRMSG("DBG2", PRINT_DBG2, fmt, ## args) |
| 198 | #define DBG3(fmt, args...) _ERRMSG("DBG3", PRINT_DBG3, fmt, ## args) |
| 199 | #define LOG(type, fmt, args...) \ |
| 200 | do { \ |
| 201 | switch (type) { \ |
| 202 | case PRINT_ERR: \ |
| 203 | ERR(fmt, ## args); \ |
| 204 | break; \ |
| 205 | case PRINT_WARN: \ |
| 206 | WARN(fmt, ## args); \ |
| 207 | break; \ |
| 208 | case PRINT_BUG: \ |
| 209 | BUG(fmt, ## args); \ |
| 210 | break; \ |
| 211 | case PRINT_MSG: \ |
| 212 | MSG(fmt, ## args); \ |
| 213 | break; \ |
| 214 | case PRINT_DBG: \ |
| 215 | DBG(fmt, ## args); \ |
| 216 | break; \ |
| 217 | case PRINT_DBG2: \ |
| 218 | DBG2(fmt, ## args); \ |
| 219 | break; \ |
| 220 | case PRINT_DBG3: \ |
| 221 | DBG3(fmt, ## args); \ |
| 222 | break; \ |
| 223 | default: \ |
| 224 | abort(); \ |
| 225 | } \ |
| 226 | } while(0); |
| 227 | |
| 228 | #define _PERROR(fmt, args...) _ERRMSG("PERROR", PRINT_ERR, fmt, ## args) |
| 229 | |
| 230 | #if !defined(__GLIBC__) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE)) |
| 231 | |
| 232 | /* |
| 233 | * Version using XSI strerror_r. |
| 234 | */ |
| 235 | #define PERROR(call, args...) \ |
| 236 | do { \ |
| 237 | char _perror_buf[200]; \ |
| 238 | strerror_r(errno, _perror_buf, sizeof(_perror_buf)); \ |
| 239 | _PERROR(call ": %s", ##args, _perror_buf); \ |
| 240 | } while (0); |
| 241 | #else |
| 242 | /* |
| 243 | * Version using GNU strerror_r, for linux with appropriate defines. |
| 244 | */ |
| 245 | #define PERROR(call, args...) \ |
| 246 | do { \ |
| 247 | char *_perror_buf; \ |
| 248 | char _perror_tmp[200]; \ |
| 249 | _perror_buf = strerror_r( \ |
| 250 | errno, _perror_tmp, sizeof(_perror_tmp)); \ |
| 251 | _PERROR(call ": %s", ##args, _perror_buf); \ |
| 252 | } while (0); |
| 253 | #endif |
| 254 | |
| 255 | const char *error_get_str(int32_t code); |
| 256 | |
| 257 | /* |
| 258 | * Function that format the time and return the reference of log_time.str to |
| 259 | * the caller. On error, an empty string is returned thus no time will be |
| 260 | * printed in the log. |
| 261 | */ |
| 262 | const char *log_add_time(void); |
| 263 | |
| 264 | /* Name must be a statically-allocated string. */ |
| 265 | void logger_set_thread_name(const char *name, bool set_pthread_name); |
| 266 | |
| 267 | #endif /* _ERROR_H */ |