Add a data pointer to all tracepoint probe callbacks
[ust.git] / include / usterr.h
... / ...
CommitLineData
1/* Copyright (C) 2009 Pierre-Marc Fournier
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18#ifndef USTERR_H
19#define USTERR_H
20
21#include <string.h>
22#include <sys/types.h>
23#include <sys/syscall.h>
24#include <errno.h>
25#include <stdarg.h>
26#include <stdio.h>
27
28#include <ust/core.h>
29
30#include "share.h"
31
32#ifndef UST_COMPONENT
33//#error UST_COMPONENT is undefined
34#define UST_COMPONENT libust
35#endif
36
37/* To stringify the expansion of a define */
38#define XSTR(d) STR(d)
39#define STR(s) #s
40
41/* We sometimes print in the tracing path, and tracing can occur in
42 * signal handlers, so we must use a print method which is signal safe.
43 */
44
45extern int ust_safe_snprintf(char *str, size_t n, const char *fmt, ...);
46
47#define sigsafe_print_err(fmt, args...) \
48{ \
49 /* Can't use dynamic allocation. Limit ourselves to 250 chars. */ \
50 char ____buf[250]; \
51 int ____saved_errno; \
52\
53 /* Save the errno. */ \
54 ____saved_errno = errno; \
55\
56 ust_safe_snprintf(____buf, sizeof(____buf), fmt, ## args); \
57\
58 /* Add end of string in case of buffer overflow. */ \
59 ____buf[sizeof(____buf)-1] = 0; \
60\
61 patient_write(STDERR_FILENO, ____buf, strlen(____buf)); \
62 /* Can't print errors because we are in the error printing code path. */ \
63\
64 /* Restore errno, in order to be async-signal safe. */ \
65 errno = ____saved_errno; \
66}
67
68#define UST_STR_COMPONENT XSTR(UST_COMPONENT)
69
70#define ERRMSG(fmt, args...) do { sigsafe_print_err(UST_STR_COMPONENT "[%ld/%ld]: " fmt " (in %s() at " __FILE__ ":" XSTR(__LINE__) ")\n", (long) getpid(), (long) syscall(SYS_gettid), ## args, __func__); fflush(stderr); } while(0)
71
72#ifdef UST_DEBUG
73# define DBG(fmt, args...) ERRMSG(fmt, ## args)
74# define DBG_raw(fmt, args...) do { sigsafe_print_err(fmt, ## args); fflush(stderr); } while(0)
75#else
76# define DBG(fmt, args...) do {} while(0)
77# define DBG_raw(fmt, args...) do {} while(0)
78#endif
79#define WARN(fmt, args...) ERRMSG("Warning: " fmt, ## args)
80#define ERR(fmt, args...) ERRMSG("Error: " fmt, ## args)
81#define BUG(fmt, args...) ERRMSG("BUG: " fmt, ## args)
82
83#if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE)
84#define PERROR(call, args...)\
85 do { \
86 char buf[200] = "Error in strerror_r()"; \
87 strerror_r(errno, buf, sizeof(buf)); \
88 ERRMSG("Error: " call ": %s", ## args, buf); \
89 } while(0);
90#else
91#define PERROR(call, args...)\
92 do { \
93 char *buf; \
94 char tmp[200]; \
95 buf = strerror_r(errno, tmp, sizeof(tmp)); \
96 ERRMSG("Error: " call ": %s", ## args, buf); \
97 } while(0);
98#endif
99
100#define BUG_ON(condition) do { if (unlikely(condition)) ERR("condition not respected (BUG)"); } while(0)
101#define WARN_ON(condition) do { if (unlikely(condition)) WARN("condition not respected on line %s:%d", __FILE__, __LINE__); } while(0)
102#define WARN_ON_ONCE(condition) WARN_ON(condition)
103
104#endif /* USTERR_H */
This page took 0.021637 seconds and 4 git commands to generate.