[ltt-dev] [UST PATCH] Make only libust and libustconsumer use a signal safe usterr.h

Nils Carlson nils.carlson at ericsson.com
Mon Apr 4 07:35:54 EDT 2011


Copy usterr.h to usterr_signal_safe.h and rewrite those parts of
usterr.h that depended on libustsnprintf. This removes the dependency
on libustsnprintf from all parts of ust except libust.

Signed-off-by: Nils Carlson <nils.carlson at ericsson.com>
---
 TODO                                |    1 +
 include/usterr.h                    |   40 ++-----------
 include/usterr_signal_safe.h        |  110 +++++++++++++++++++++++++++++++++++
 libust/buffers.c                    |    2 +-
 libust/buffers.h                    |    2 +-
 libust/channels.c                   |    2 +-
 libust/marker-control.c             |    2 +-
 libust/marker.c                     |    2 +-
 libust/serialize.c                  |    2 +-
 libust/trace_event.c                |    2 +-
 libust/tracectl.c                   |    2 +-
 libust/tracepoint.c                 |    2 +-
 libust/tracer.c                     |    2 +-
 libustconsumer/libustconsumer.c     |    2 +-
 libustconsumer/lowlevel.c           |    2 +-
 tests/hello/Makefile.am             |    4 +-
 tests/hello/hello.c                 |    6 +-
 tests/register_test/register_test.c |    1 -
 18 files changed, 135 insertions(+), 51 deletions(-)
 create mode 100644 include/usterr_signal_safe.h

diff --git a/TODO b/TODO
index 0d07589..536502f 100644
--- a/TODO
+++ b/TODO
@@ -1,3 +1,4 @@
+- remove libustconsumers dependency on libustsnprintf (usterr_signal_safe.h)
 - correctly destroy buffers at trace destroy
 - add multi-threaded test program
 - add dlopen() based test program
diff --git a/include/usterr.h b/include/usterr.h
index b97ad6b..dc51d84 100644
--- a/include/usterr.h
+++ b/include/usterr.h
@@ -15,8 +15,8 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
-#ifndef USTERR_H
-#define USTERR_H
+#ifndef _USTERR_H
+#define _USTERR_H
 
 #include <string.h>
 #include <sys/types.h>
@@ -30,7 +30,6 @@
 #include "share.h"
 
 #ifndef UST_COMPONENT
-//#error UST_COMPONENT is undefined
 #define UST_COMPONENT libust
 #endif
 
@@ -38,46 +37,19 @@
 #define XSTR(d) STR(d)
 #define STR(s) #s
 
-/* We sometimes print in the tracing path, and tracing can occur in
- * signal handlers, so we must use a print method which is signal safe.
- */
-
-extern int ust_safe_snprintf(char *str, size_t n, const char *fmt, ...)
-	__attribute__ ((format (printf, 3, 4)));
-
+/* A dummy function to force format checking */
 static inline void __attribute__ ((format (printf, 1, 2)))
 	__check_ust_safe_fmt(const char *fmt, ...)
 {
 }
 
-#define sigsafe_print_err(fmt, args...) \
-{ \
-	/* Can't use dynamic allocation. Limit ourselves to 250 chars. */ \
-	char ____buf[250]; \
-	int ____saved_errno; \
-\
-	/* Save the errno. */ \
-	____saved_errno = errno; \
-\
-	ust_safe_snprintf(____buf, sizeof(____buf), fmt, ## args); \
-\
-	/* Add end of string in case of buffer overflow. */ \
-	____buf[sizeof(____buf)-1] = 0; \
-\
-	patient_write(STDERR_FILENO, ____buf, strlen(____buf)); \
-	/* Can't print errors because we are in the error printing code path. */ \
-\
-	/* Restore errno, in order to be async-signal safe. */ \
-	errno = ____saved_errno; \
-}
-
 #define UST_STR_COMPONENT XSTR(UST_COMPONENT)
 
-#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)
+#define ERRMSG(fmt, args...) do { fprintf(stderr, UST_STR_COMPONENT "[%ld/%ld]: " fmt " (in %s() at " __FILE__ ":" XSTR(__LINE__) ")\n", (long) getpid(), (long) syscall(SYS_gettid), ## args, __func__); } while(0)
 
 #ifdef UST_DEBUG
 # define DBG(fmt, args...) ERRMSG(fmt, ## args)
-# define DBG_raw(fmt, args...) do { sigsafe_print_err(fmt, ## args); fflush(stderr); } while(0)
+# define DBG_raw(fmt, args...) do { fprintf(stderr, fmt, ## args); } while(0)
 #else
 # define DBG(fmt, args...) __check_ust_safe_fmt(fmt, ## args)
 # define DBG_raw(fmt, args...) __check_ust_safe_fmt(fmt, ## args)
@@ -107,4 +79,4 @@ static inline void __attribute__ ((format (printf, 1, 2)))
 #define WARN_ON(condition) do { if (unlikely(condition)) WARN("condition not respected on line %s:%d", __FILE__, __LINE__); } while(0)
 #define WARN_ON_ONCE(condition) WARN_ON(condition)
 
-#endif /* USTERR_H */
+#endif /* _USTERR_H */
diff --git a/include/usterr_signal_safe.h b/include/usterr_signal_safe.h
new file mode 100644
index 0000000..f12c317
--- /dev/null
+++ b/include/usterr_signal_safe.h
@@ -0,0 +1,110 @@
+/* Copyright (C) 2009  Pierre-Marc Fournier
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
+ */
+
+#ifndef _USTERR_SIGNAL_SAFE_H
+#define _USTERR_SIGNAL_SAFE_H
+
+#include <string.h>
+#include <sys/types.h>
+#include <sys/syscall.h>
+#include <errno.h>
+#include <stdarg.h>
+#include <stdio.h>
+
+#include <ust/core.h>
+
+#include "share.h"
+
+#ifndef UST_COMPONENT
+//#error UST_COMPONENT is undefined
+#define UST_COMPONENT libust
+#endif
+
+/* To stringify the expansion of a define */
+#define XSTR(d) STR(d)
+#define STR(s) #s
+
+/* We sometimes print in the tracing path, and tracing can occur in
+ * signal handlers, so we must use a print method which is signal safe.
+ */
+
+extern int ust_safe_snprintf(char *str, size_t n, const char *fmt, ...)
+	__attribute__ ((format (printf, 3, 4)));
+
+static inline void __attribute__ ((format (printf, 1, 2)))
+	__check_ust_safe_fmt(const char *fmt, ...)
+{
+}
+
+#define sigsafe_print_err(fmt, args...) \
+{ \
+	/* Can't use dynamic allocation. Limit ourselves to 250 chars. */ \
+	char ____buf[250]; \
+	int ____saved_errno; \
+\
+	/* Save the errno. */ \
+	____saved_errno = errno; \
+\
+	ust_safe_snprintf(____buf, sizeof(____buf), fmt, ## args); \
+\
+	/* Add end of string in case of buffer overflow. */ \
+	____buf[sizeof(____buf)-1] = 0; \
+\
+	patient_write(STDERR_FILENO, ____buf, strlen(____buf)); \
+	/* Can't print errors because we are in the error printing code path. */ \
+\
+	/* Restore errno, in order to be async-signal safe. */ \
+	errno = ____saved_errno; \
+}
+
+#define UST_STR_COMPONENT XSTR(UST_COMPONENT)
+
+#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)
+
+#ifdef UST_DEBUG
+# define DBG(fmt, args...) ERRMSG(fmt, ## args)
+# define DBG_raw(fmt, args...) do { sigsafe_print_err(fmt, ## args); fflush(stderr); } while(0)
+#else
+# define DBG(fmt, args...) __check_ust_safe_fmt(fmt, ## args)
+# define DBG_raw(fmt, args...) __check_ust_safe_fmt(fmt, ## args)
+#endif
+#define WARN(fmt, args...) ERRMSG("Warning: " fmt, ## args)
+#define ERR(fmt, args...) ERRMSG("Error: " fmt, ## args)
+#define BUG(fmt, args...) ERRMSG("BUG: " fmt, ## args)
+
+#if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE)
+#define PERROR(call, args...)\
+	do { \
+		char buf[200] = "Error in strerror_r()"; \
+		strerror_r(errno, buf, sizeof(buf)); \
+		ERRMSG("Error: " call ": %s", ## args, buf); \
+	} while(0);
+#else
+#define PERROR(call, args...)\
+	do { \
+		char *buf; \
+		char tmp[200]; \
+		buf = strerror_r(errno, tmp, sizeof(tmp)); \
+		ERRMSG("Error: " call ": %s", ## args, buf); \
+	} while(0);
+#endif
+
+#define BUG_ON(condition) do { if (unlikely(condition)) ERR("condition not respected (BUG)"); } while(0)
+#define WARN_ON(condition) do { if (unlikely(condition)) WARN("condition not respected on line %s:%d", __FILE__, __LINE__); } while(0)
+#define WARN_ON_ONCE(condition) WARN_ON(condition)
+
+#endif /* _USTERR_SIGNAL_SAFE_H */
diff --git a/libust/buffers.c b/libust/buffers.c
index 9dcec2a..b2a949d 100644
--- a/libust/buffers.c
+++ b/libust/buffers.c
@@ -33,7 +33,7 @@
 #include "channels.h"
 #include "tracer.h"
 #include "tracercore.h"
-#include "usterr.h"
+#include "usterr_signal_safe.h"
 
 struct ltt_reserve_switch_offsets {
 	long begin, end, old;
diff --git a/libust/buffers.h b/libust/buffers.h
index ddacdff..4017964 100644
--- a/libust/buffers.h
+++ b/libust/buffers.h
@@ -28,7 +28,7 @@
 #include <ust/core.h>
 #include <ust/clock.h>
 
-#include "usterr.h"
+#include "usterr_signal_safe.h"
 #include "channels.h"
 #include "tracerconst.h"
 #include "tracercore.h"
diff --git a/libust/channels.c b/libust/channels.c
index 8930705..13178e6 100644
--- a/libust/channels.c
+++ b/libust/channels.c
@@ -26,7 +26,7 @@
 #include <stdlib.h>
 #include <ust/marker.h>
 #include "channels.h"
-#include "usterr.h"
+#include "usterr_signal_safe.h"
 
 /*
  * ltt_channel_mutex may be nested inside the LTT trace mutex.
diff --git a/libust/marker-control.c b/libust/marker-control.c
index 3ad2e6a..3d50952 100644
--- a/libust/marker-control.c
+++ b/libust/marker-control.c
@@ -26,7 +26,7 @@
 #include <stdlib.h>
 
 #include "tracer.h"
-#include "usterr.h"
+#include "usterr_signal_safe.h"
 
 #define DEFAULT_CHANNEL "cpu"
 #define DEFAULT_PROBE "default"
diff --git a/libust/marker.c b/libust/marker.c
index 96d1409..a64b46f 100644
--- a/libust/marker.c
+++ b/libust/marker.c
@@ -27,7 +27,7 @@
 #include <ust/marker.h>
 #include <ust/tracepoint.h>
 
-#include "usterr.h"
+#include "usterr_signal_safe.h"
 #include "channels.h"
 #include "tracercore.h"
 #include "tracer.h"
diff --git a/libust/serialize.c b/libust/serialize.c
index 8aa3f4b..c637786 100644
--- a/libust/serialize.c
+++ b/libust/serialize.c
@@ -40,7 +40,7 @@
 #include <ust/clock.h>
 #include "buffers.h"
 #include "tracer.h"
-#include "usterr.h"
+#include "usterr_signal_safe.h"
 #include "ust_snprintf.h"
 
 /*
diff --git a/libust/trace_event.c b/libust/trace_event.c
index 26157e2..728140f 100644
--- a/libust/trace_event.c
+++ b/libust/trace_event.c
@@ -21,7 +21,7 @@
 #include <ust/tracepoint.h>
 #include <ust/core.h>
 #include <ust/kcompat/kcompat.h>
-#include "usterr.h"
+#include "usterr_signal_safe.h"
 
 #define _LGPL_SOURCE
 #include <urcu-bp.h>
diff --git a/libust/tracectl.c b/libust/tracectl.c
index 58b567f..96053b7 100644
--- a/libust/tracectl.c
+++ b/libust/tracectl.c
@@ -41,7 +41,7 @@
 #include <ust/tracectl.h>
 #include <ust/clock.h>
 #include "tracer.h"
-#include "usterr.h"
+#include "usterr_signal_safe.h"
 #include "ustcomm.h"
 #include "buffers.h"
 #include "marker-control.h"
diff --git a/libust/tracepoint.c b/libust/tracepoint.c
index f593306..a1aac82 100644
--- a/libust/tracepoint.c
+++ b/libust/tracepoint.c
@@ -23,7 +23,7 @@
 #include <ust/tracepoint.h>
 #include <ust/core.h>
 #include <ust/kcompat/kcompat.h>
-#include "usterr.h"
+#include "usterr_signal_safe.h"
 
 #define _LGPL_SOURCE
 #include <urcu-bp.h>
diff --git a/libust/tracer.c b/libust/tracer.c
index 3b4fae4..e2be0ae 100644
--- a/libust/tracer.c
+++ b/libust/tracer.c
@@ -38,7 +38,7 @@
 
 #include "tracercore.h"
 #include "tracer.h"
-#include "usterr.h"
+#include "usterr_signal_safe.h"
 
 struct chan_info_struct chan_infos[] = {
 	[LTT_CHANNEL_METADATA] = {
diff --git a/libustconsumer/libustconsumer.c b/libustconsumer/libustconsumer.c
index eaee1fa..c5acffa 100644
--- a/libustconsumer/libustconsumer.c
+++ b/libustconsumer/libustconsumer.c
@@ -34,7 +34,7 @@
 
 #include <ust/ustconsumer.h>
 #include "lowlevel.h"
-#include "usterr.h"
+#include "usterr_signal_safe.h"
 #include "ustcomm.h"
 
 #define GET_SUBBUF_OK 1
diff --git a/libustconsumer/lowlevel.c b/libustconsumer/lowlevel.c
index ec1ef05..a54a8db 100644
--- a/libustconsumer/lowlevel.c
+++ b/libustconsumer/lowlevel.c
@@ -22,7 +22,7 @@
 #include "ust/ustconsumer.h"
 #include "buffers.h"
 #include "tracer.h"
-#include "usterr.h"
+#include "usterr_signal_safe.h"
 
 /* This truncates to an offset in the buffer. */
 #define USTD_BUFFER_TRUNC(offset, bufinfo) \
diff --git a/tests/hello/Makefile.am b/tests/hello/Makefile.am
index 27ff78d..bf7b471 100644
--- a/tests/hello/Makefile.am
+++ b/tests/hello/Makefile.am
@@ -2,7 +2,9 @@ AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/libust
 
 noinst_PROGRAMS = hello
 hello_SOURCES = hello.c tp.c tp.h
-hello_LDADD = $(top_builddir)/libust/libust.la $(top_builddir)/libust-initializer.o
+hello_LDADD = $(top_builddir)/libust/libust.la \
+	$(top_builddir)/libustctl/libustctl.la \
+	$(top_builddir)/libust-initializer.o
 
 noinst_SCRIPTS = run
 EXTRA_DIST = run
diff --git a/tests/hello/hello.c b/tests/hello/hello.c
index 7eecf69..c0b541f 100644
--- a/tests/hello/hello.c
+++ b/tests/hello/hello.c
@@ -25,8 +25,8 @@
 #include <signal.h>
 
 #include <ust/marker.h>
+#include <ust/ustctl.h>
 #include "usterr.h"
-#include "tracer.h"
 #include "tp.h"
 
 void inthandler(int sig)
@@ -80,8 +80,8 @@ int main()
 	if (scanf("%*s") == EOF)
 		PERROR("scanf failed");
 
-	ltt_trace_stop("auto");
-	ltt_trace_destroy("auto", 0);
+	ustctl_stop_trace(getpid(), "auto");
+	ustctl_destroy_trace(getpid(), "auto");
 
 	DBG("TRACE STOPPED");
 	if (scanf("%*s") == EOF)
diff --git a/tests/register_test/register_test.c b/tests/register_test/register_test.c
index d5cd352..4d1f0fe 100644
--- a/tests/register_test/register_test.c
+++ b/tests/register_test/register_test.c
@@ -27,7 +27,6 @@
 
 #include <ust/marker.h>
 #include "usterr.h"
-#include "tracer.h"
 #include "tp.h"
 
 DEFINE_TRACE(hello_tptest);
-- 
1.7.1





More information about the lttng-dev mailing list