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