Blame include/usterr-signal-safe.h

Packit c04fcb
#ifndef _USTERR_SIGNAL_SAFE_H
Packit c04fcb
#define _USTERR_SIGNAL_SAFE_H
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * Copyright (C) 2009  Pierre-Marc Fournier
Packit c04fcb
 * Copyright (C) 2011  Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Packit c04fcb
 *
Packit c04fcb
 * This library is free software; you can redistribute it and/or
Packit c04fcb
 * modify it under the terms of the GNU Lesser General Public
Packit c04fcb
 * License as published by the Free Software Foundation; version 2.1 of
Packit c04fcb
 * the License.
Packit c04fcb
 *
Packit c04fcb
 * This library is distributed in the hope that it will be useful,
Packit c04fcb
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit c04fcb
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit c04fcb
 * Lesser General Public License for more details.
Packit c04fcb
 *
Packit c04fcb
 * You should have received a copy of the GNU Lesser General Public
Packit c04fcb
 * License along with this library; if not, write to the Free Software
Packit c04fcb
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
Packit c04fcb
 */
Packit c04fcb
Packit c04fcb
#include <string.h>
Packit c04fcb
#include <sys/types.h>
Packit c04fcb
#include <errno.h>
Packit c04fcb
#include <stdarg.h>
Packit c04fcb
#include <stdio.h>
Packit c04fcb
#include <share.h>
Packit c04fcb
#include "lttng/ust-tid.h"
Packit c04fcb
Packit c04fcb
enum ust_loglevel {
Packit c04fcb
	UST_LOGLEVEL_UNKNOWN = 0,
Packit c04fcb
	UST_LOGLEVEL_NORMAL,
Packit c04fcb
	UST_LOGLEVEL_DEBUG,
Packit c04fcb
};
Packit c04fcb
Packit c04fcb
extern volatile enum ust_loglevel ust_loglevel;
Packit c04fcb
void init_usterr(void);
Packit c04fcb
Packit c04fcb
#ifdef LTTNG_UST_DEBUG
Packit c04fcb
static inline int ust_debug(void)
Packit c04fcb
{
Packit c04fcb
	return 1;
Packit c04fcb
}
Packit c04fcb
#else /* #ifdef LTTNG_UST_DEBUG */
Packit c04fcb
static inline int ust_debug(void)
Packit c04fcb
{
Packit c04fcb
	return ust_loglevel == UST_LOGLEVEL_DEBUG;
Packit c04fcb
}
Packit c04fcb
#endif /* #else #ifdef LTTNG_UST_DEBUG */
Packit c04fcb
Packit c04fcb
#ifndef UST_COMPONENT
Packit c04fcb
//#error UST_COMPONENT is undefined
Packit c04fcb
#define UST_COMPONENT libust
Packit c04fcb
#endif
Packit c04fcb
Packit c04fcb
/* To stringify the expansion of a define */
Packit c04fcb
#define UST_XSTR(d) UST_STR(d)
Packit c04fcb
#define UST_STR(s) #s
Packit c04fcb
Packit c04fcb
#define USTERR_MAX_LEN	512
Packit c04fcb
Packit c04fcb
/* We sometimes print in the tracing path, and tracing can occur in
Packit c04fcb
 * signal handlers, so we must use a print method which is signal safe.
Packit c04fcb
 */
Packit c04fcb
Packit c04fcb
extern int ust_safe_snprintf(char *str, size_t n, const char *fmt, ...)
Packit c04fcb
	__attribute__ ((format (printf, 3, 4)));
Packit c04fcb
Packit c04fcb
static inline void __attribute__ ((format (printf, 1, 2)))
Packit c04fcb
	__check_ust_safe_fmt(const char *fmt, ...)
Packit c04fcb
{
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
/* Can't use dynamic allocation. Limit ourselves to USTERR_MAX_LEN chars. */
Packit c04fcb
/* Add end of string in case of buffer overflow. */
Packit c04fcb
#define sigsafe_print_err(fmt, args...)					\
Packit c04fcb
do {									\
Packit c04fcb
	if (ust_debug()) {						\
Packit c04fcb
		char ____buf[USTERR_MAX_LEN];				\
Packit c04fcb
		int ____saved_errno;					\
Packit c04fcb
									\
Packit c04fcb
		____saved_errno = errno;	/* signal-safety */	\
Packit c04fcb
		ust_safe_snprintf(____buf, sizeof(____buf), fmt, ## args); \
Packit c04fcb
		____buf[sizeof(____buf) - 1] = 0;			\
Packit c04fcb
		patient_write(STDERR_FILENO, ____buf, strlen(____buf));	\
Packit c04fcb
		errno = ____saved_errno;	/* signal-safety */	\
Packit c04fcb
		fflush(stderr);						\
Packit c04fcb
	}								\
Packit c04fcb
} while (0)
Packit c04fcb
Packit c04fcb
#define UST_STR_COMPONENT UST_XSTR(UST_COMPONENT)
Packit c04fcb
Packit c04fcb
#define ERRMSG(fmt, args...)			\
Packit c04fcb
	do {					\
Packit c04fcb
		sigsafe_print_err(UST_STR_COMPONENT "[%ld/%ld]: " fmt " (in %s() at " __FILE__ ":" UST_XSTR(__LINE__) ")\n", \
Packit c04fcb
		(long) getpid(),		\
Packit c04fcb
		(long) gettid(),		\
Packit c04fcb
		## args, __func__);		\
Packit c04fcb
	} while(0)
Packit c04fcb
Packit c04fcb
Packit c04fcb
#define DBG(fmt, args...)	ERRMSG(fmt, ## args)
Packit c04fcb
#define DBG_raw(fmt, args...)	sigsafe_print_err(fmt, ## args)
Packit c04fcb
#define WARN(fmt, args...)	ERRMSG("Warning: " fmt, ## args)
Packit c04fcb
#define ERR(fmt, args...)	ERRMSG("Error: " fmt, ## args)
Packit c04fcb
#define BUG(fmt, args...)	ERRMSG("BUG: " fmt, ## args)
Packit c04fcb
Packit c04fcb
#if !defined(__GLIBC__) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE))
Packit c04fcb
/*
Packit c04fcb
 * Version using XSI strerror_r.
Packit c04fcb
 */
Packit c04fcb
#define PERROR(call, args...)						\
Packit c04fcb
	do {								\
Packit c04fcb
		if (ust_debug()) {					\
Packit c04fcb
			char buf[200] = "Error in strerror_r()";	\
Packit c04fcb
			strerror_r(errno, buf, sizeof(buf));		\
Packit c04fcb
			ERRMSG("Error: " call ": %s", ## args, buf);	\
Packit c04fcb
		}							\
Packit c04fcb
	} while(0)
Packit c04fcb
#else
Packit c04fcb
/*
Packit c04fcb
 * Version using GNU strerror_r, for linux with appropriate defines.
Packit c04fcb
 */
Packit c04fcb
#define PERROR(call, args...)						\
Packit c04fcb
	do {								\
Packit c04fcb
		if (ust_debug()) {					\
Packit c04fcb
			char *buf;					\
Packit c04fcb
			char tmp[200];					\
Packit c04fcb
			buf = strerror_r(errno, tmp, sizeof(tmp));	\
Packit c04fcb
			ERRMSG("Error: " call ": %s", ## args, buf);	\
Packit c04fcb
		}							\
Packit c04fcb
	} while(0)
Packit c04fcb
#endif
Packit c04fcb
Packit c04fcb
#define BUG_ON(condition)					\
Packit c04fcb
	do {							\
Packit c04fcb
		if (caa_unlikely(condition))			\
Packit c04fcb
			ERR("condition not respected (BUG) on line %s:%d", __FILE__, __LINE__);	\
Packit c04fcb
	} while(0)
Packit c04fcb
#define WARN_ON(condition)					\
Packit c04fcb
	do {							\
Packit c04fcb
		if (caa_unlikely(condition))			\
Packit c04fcb
			WARN("condition not respected on line %s:%d", __FILE__, __LINE__); \
Packit c04fcb
	} while(0)
Packit c04fcb
#define WARN_ON_ONCE(condition) WARN_ON(condition)
Packit c04fcb
Packit c04fcb
#endif /* _USTERR_SIGNAL_SAFE_H */