Make only libust and libustconsumer use a signal safe usterr.h
authorNils Carlson <nils.carlson@ericsson.com>
Mon, 4 Apr 2011 10:49:56 +0000 (12:49 +0200)
committerNils Carlson <nils.carlson@ericsson.com>
Mon, 4 Apr 2011 15:13:22 +0000 (17:13 +0200)
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@ericsson.com>
Acked-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
18 files changed:
TODO
include/usterr.h
include/usterr_signal_safe.h [new file with mode: 0644]
libust/buffers.c
libust/buffers.h
libust/channels.c
libust/marker-control.c
libust/marker.c
libust/serialize.c
libust/trace_event.c
libust/tracectl.c
libust/tracepoint.c
libust/tracer.c
libustconsumer/libustconsumer.c
libustconsumer/lowlevel.c
tests/hello/Makefile.am
tests/hello/hello.c
tests/register_test/register_test.c

diff --git a/TODO b/TODO
index 0d0758967466898592b559c1372ed3d0ca028a36..536502f2ff07626f0d086ddff38924e3e5348bc4 100644 (file)
--- 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
index b97ad6ba3559621d3cd9b4162bbae298926b4c26..dc51d840b42879d6e8f11e1f7567a9f05dc2edad 100644 (file)
@@ -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
 
 #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 (file)
index 0000000..f12c317
--- /dev/null
@@ -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 */
index 9dcec2a825d953ab51a07adfdaf69e244c51e4b9..b2a949db454a97247ec25e90a6f2ad607d917131 100644 (file)
@@ -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;
index ddacdffe68ac5c1c4ae0745b4f31b15f773cdeba..4017964c7ea7410967ef610e7d6477163d7f608f 100644 (file)
@@ -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"
index 893070570f715196000ba6fbcec35a7f20a0eac6..13178e620c4c6eac9bf7c70e2378049b706ed3ca 100644 (file)
@@ -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.
index 3ad2e6ae2ce845feb62d2f47b644ead5c33dd2b0..3d509525508a0446e828e1b3fecc165bcf75a560 100644 (file)
@@ -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"
index 96d140996890c40c5b4a43da45a63a4180aad7d3..a64b46fc688f42bbaaa132a0b9984f6e4df2bfa3 100644 (file)
@@ -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"
index 8aa3f4b73fac66330c139e6572dd1bfb407b728d..c637786ed9563cde1a63f22e0f88ff9d30abbc6c 100644 (file)
@@ -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"
 
 /*
index 26157e245e032b8c9618b63ba2e437ab926756e4..728140fcba420cf33c9d5de43e30e2ae4a9d4b1e 100644 (file)
@@ -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>
index 58b567f14265d57cea8405b9652cf49dfa66ba69..96053b7b523773000560b70a86a2b717056005a5 100644 (file)
@@ -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"
index f593306a83a8f7a784be2b7ca08393e5ee5294ee..a1aac8264e255d85beeefe7a3472adc378bc4c6f 100644 (file)
@@ -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>
index 3b4fae4c17f51bf17fa8e3a33ccce051e8899cbd..e2be0ae5882084d66a6949133abd54686952e82c 100644 (file)
@@ -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] = {
index eaee1fa61f358b6ec3145818ae5347994fcb6249..c5acffa030296eda5b775fed81e13ec8b7134b7d 100644 (file)
@@ -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
index ec1ef05ca4d18dba3c4f07b216187c7cb5a8c235..a54a8db0c85e8fec893ca0bae4a2c195b7185cc6 100644 (file)
@@ -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) \
index 27ff78df09065c368b68120e9d7dfb0b8e5e5131..bf7b47180ee3be604602811783479a2dd3f63fcc 100644 (file)
@@ -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
index 7eecf69e83f0bbaf23949a71e191806bf884687a..c0b541fd33d23b6c5dbecdac1933b4c0d480b40f 100644 (file)
@@ -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)
index d5cd352afeaadd3362ab338d4e93d4479440c3c8..4d1f0fe7e80a6f2df1b525bd8a20743462bc6cb6 100644 (file)
@@ -27,7 +27,6 @@
 
 #include <ust/marker.h>
 #include "usterr.h"
-#include "tracer.h"
 #include "tp.h"
 
 DEFINE_TRACE(hello_tptest);
This page took 0.031426 seconds and 4 git commands to generate.