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