Introduce vtracef
authorMaxime Roussin-Belanger <maxime.roussinbelanger@gmail.com>
Tue, 3 Mar 2020 23:10:30 +0000 (18:10 -0500)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Wed, 4 Mar 2020 15:24:32 +0000 (10:24 -0500)
vtracef accepts a va_list argument to simplify
tracing functions which use a va_list

Here's an example from wpa_supplicant that I wanted to
trace:

void wpa_debug(int level, const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);

...
// The call I want to easily trace with vtracef
vprintf(fmt, ap);

...
va_end(ap);
}

wpa_debug is used a fair amount and it would be annoying to
replace all the wpa_debug calls with tracef.

With vtracef, it simplifies the find and replace effort by
only changing it at one place.

Signed-off-by: Maxime Roussin-Belanger <maxime.roussinbelanger@gmail.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
.gitignore
doc/examples/demo-tracef/Makefile
doc/examples/demo-tracef/demo-vtracef.c [new file with mode: 0644]
doc/man/tracef-tracelog-limitations.txt
doc/man/tracef.3.txt
include/lttng/tracef.h
liblttng-ust/tracef.c

index 78646d74599456205c44d69e61846de48e2c51c6..6574f44eb45901e56efb805d2bff772898a54f38 100644 (file)
@@ -54,6 +54,7 @@ doc/examples/hello-static-lib/hello
 doc/examples/gen-tp/sample
 doc/examples/gen-tp/sample_tracepoint.h
 doc/examples/demo-tracef/demo-tracef
 doc/examples/gen-tp/sample
 doc/examples/gen-tp/sample_tracepoint.h
 doc/examples/demo-tracef/demo-tracef
+doc/examples/demo-tracef/demo-vtracef
 doc/examples/demo-tracelog/demo-tracelog
 doc/examples/cmake-multiple-shared-libraries/build/
 
 doc/examples/demo-tracelog/demo-tracelog
 doc/examples/cmake-multiple-shared-libraries/build/
 
index ee20784335ac00c486fb5d83519e6e5fb0e93d3e..809aac196f80519f7199aae6e28066f6a2392dbe 100644 (file)
@@ -20,7 +20,7 @@ LIBS = -ldl -llttng-ust       # On Linux
 LOCAL_CPPFLAGS += -I.
 AM_V_P := :
 
 LOCAL_CPPFLAGS += -I.
 AM_V_P := :
 
-all: demo-tracef
+all: demo-tracef demo-vtracef
 
 demo-tracef.o: demo-tracef.c
        @if $(AM_V_P); then set -x; else echo "  CC       $@"; fi; \
 
 demo-tracef.o: demo-tracef.c
        @if $(AM_V_P); then set -x; else echo "  CC       $@"; fi; \
@@ -32,6 +32,16 @@ demo-tracef: demo-tracef.o
                $(CC) $(LDFLAGS) $(AM_CFLAGS) $(AM_LDFLAGS) $(CFLAGS) \
                -o $@ $< $(LIBS)
 
                $(CC) $(LDFLAGS) $(AM_CFLAGS) $(AM_LDFLAGS) $(CFLAGS) \
                -o $@ $< $(LIBS)
 
+demo-vtracef.o: demo-vtracef.c
+       @if $(AM_V_P); then set -x; else echo "  CC       $@"; fi; \
+               $(CC) $(CPPFLAGS) $(LOCAL_CPPFLAGS) $(AM_CFLAGS) $(AM_CPPFLAGS) \
+               $(CFLAGS) -c -o $@ $<
+
+demo-vtracef: demo-vtracef.o
+       @if $(AM_V_P); then set -x; else echo "  CCLD     $@"; fi; \
+               $(CC) $(LDFLAGS) $(AM_CFLAGS) $(AM_LDFLAGS) $(CFLAGS) \
+               -o $@ $< $(LIBS)
+
 .PHONY: clean
 clean:
 .PHONY: clean
 clean:
-       rm -f *.o *.a demo-tracef
+       rm -f *.o *.a demo-tracef demo-vtracef
diff --git a/doc/examples/demo-tracef/demo-vtracef.c b/doc/examples/demo-tracef/demo-vtracef.c
new file mode 100644 (file)
index 0000000..cfe885b
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2020  Maxime Roussin-Belanger <maxime.roussinbelanger@gmail.com>
+ *
+ * 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; version 2.1 of
+ * the License.
+ *
+ * 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
+ */
+
+#include <stdarg.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#include <lttng/tracef.h>
+
+void print_debug(const char* msg, ...)
+{
+       va_list ap;
+
+       va_start(ap, msg);
+       vtracef(msg, ap);
+       va_end(ap);
+}
+
+int main(int argc, char **argv)
+{
+       int i;
+       int delay = 0;
+       const char *str = "mystring test";
+       long l = 0x42;
+
+       if (argc > 2)
+               delay = atoi(argv[1]);
+
+       fprintf(stderr, "Demo program starting.\n");
+
+       sleep(delay);
+
+       fprintf(stderr, "Tracing... ");
+
+       for (i = 0; i < 5; i++) {
+               print_debug("This is a \"%s\" formatted %d event %lx", str, i, l);
+       }
+
+       fprintf(stderr, " done.\n");
+       return 0;
+}
index a94919030ec71ce18f46ec9b49a824404d51534d..ad4130dd4c03312f7d331c830d54ab2601b435bd 100644 (file)
@@ -21,4 +21,7 @@ Thus, +{macro-name}()+ is useful for quick prototyping and debugging, but
 should not be considered for any permanent/serious application
 instrumentation.
 
 should not be considered for any permanent/serious application
 instrumentation.
 
++v{macro-name}()+ does not have a `STAP_PROBEV()` call, because `STAP_PROBEV()`
+does not support `va_list`. If you need it, you should emit this call yourself.
+
 See man:lttng-ust(3) to learn more about custom tracepoint providers.
 See man:lttng-ust(3) to learn more about custom tracepoint providers.
index 1068afadfef56b6e7f0256adfe196d885a280246..2894ca455af39e99c5d587dfd88272aab45a6cf0 100644 (file)
@@ -5,7 +5,7 @@ tracef(3)
 
 NAME
 ----
 
 NAME
 ----
-tracef - LTTng-UST printf(3)-like interface
+tracef, vtracef - LTTng-UST printf(3)-like interface
 
 
 SYNOPSIS
 
 
 SYNOPSIS
@@ -15,6 +15,7 @@ SYNOPSIS
 
 [verse]
 #define *tracef*('fmt', ...)
 
 [verse]
 #define *tracef*('fmt', ...)
+#define *vtracef*('fmt', 'va_list' ap)
 
 Link with `-llttng-ust`.
 
 
 Link with `-llttng-ust`.
 
@@ -26,11 +27,11 @@ the help of a simple man:printf(3)-like macro. The 'fmt' argument is
 passed directly to the 'fmt' parameter of man:vasprintf(3), as well as
 the optional parameters following 'fmt'.
 
 passed directly to the 'fmt' parameter of man:vasprintf(3), as well as
 the optional parameters following 'fmt'.
 
-To use `tracef()`, include `<lttng/tracef.h>` where you need it, and
-link your application with `liblttng-ust`. See the <<example,EXAMPLE>>
+To use `tracef()` or `vtracef()`, include `<lttng/tracef.h>` where you need it,
+and link your application with `liblttng-ust`. See the <<example,EXAMPLE>>
 section below for a complete usage example.
 
 section below for a complete usage example.
 
-Once your application is instrumented with `tracef()` calls and
+Once your application is instrumented with `tracef()` or `vtracef()` calls and
 ready to run, use man:lttng-enable-event(1) to enable the
 `lttng_ust_tracef:*` event.
 
 ready to run, use man:lttng-enable-event(1) to enable the
 `lttng_ust_tracef:*` event.
 
@@ -41,7 +42,7 @@ If you need to attach a specific log level to a `tracef()` call, use
 man:tracelog(3) instead.
 
 See also the <<limitations,LIMITATIONS>> section below for important
 man:tracelog(3) instead.
 
 See also the <<limitations,LIMITATIONS>> section below for important
-limitations to consider when using `tracef()`.
+limitations to consider when using `tracef()` or `vtracef()`.
 
 
 [[example]]
 
 
 [[example]]
index 0c59c9ae17c6b4b1cc80a8c93fb01e4bb4e29f0b..854ccdce52b48c20f7098b49af8cb20ebc4cb5e9 100644 (file)
@@ -32,6 +32,9 @@ extern "C" {
 extern
 void _lttng_ust_tracef(const char *fmt, ...);
 
 extern
 void _lttng_ust_tracef(const char *fmt, ...);
 
+extern
+void _lttng_ust_vtracef(const char *fmt, va_list ap);
+
 #define tracef(fmt, ...)                                               \
        do {                                                            \
                LTTNG_STAP_PROBEV(tracepoint_lttng_ust_tracef, event, ## __VA_ARGS__); \
 #define tracef(fmt, ...)                                               \
        do {                                                            \
                LTTNG_STAP_PROBEV(tracepoint_lttng_ust_tracef, event, ## __VA_ARGS__); \
@@ -39,6 +42,11 @@ void _lttng_ust_tracef(const char *fmt, ...);
                        _lttng_ust_tracef(fmt, ## __VA_ARGS__);         \
        } while (0)
 
                        _lttng_ust_tracef(fmt, ## __VA_ARGS__);         \
        } while (0)
 
+#define vtracef(fmt, ap)                                               \
+       do {                                                            \
+               if (caa_unlikely(__tracepoint_lttng_ust_tracef___event.state)) \
+                       _lttng_ust_vtracef(fmt, ap);            \
+       } while (0)
 #ifdef __cplusplus
 }
 #endif
 #ifdef __cplusplus
 }
 #endif
index ea98e43eeed198a3588ddb21d3668954383da810..fa001ea28c7245b3b5dde95a482b8935bbc4ddcc 100644 (file)
 #define TRACEPOINT_DEFINE
 #include "lttng-ust-tracef-provider.h"
 
 #define TRACEPOINT_DEFINE
 #include "lttng-ust-tracef-provider.h"
 
-void _lttng_ust_tracef(const char *fmt, ...)
+static inline __attribute__((always_inline))
+void __lttng_ust_vtracef(const char *fmt, va_list ap)
 {
 {
-       va_list ap;
        char *msg;
        char *msg;
-       int len;
+       const int len = vasprintf(&msg, fmt, ap);
 
 
-       va_start(ap, fmt);
-       len = vasprintf(&msg, fmt, ap);
        /* len does not include the final \0 */
        if (len < 0)
                goto end;
        /* len does not include the final \0 */
        if (len < 0)
                goto end;
@@ -44,5 +42,19 @@ void _lttng_ust_tracef(const char *fmt, ...)
                LTTNG_UST_CALLER_IP());
        free(msg);
 end:
                LTTNG_UST_CALLER_IP());
        free(msg);
 end:
+       return;
+}
+
+void _lttng_ust_vtracef(const char *fmt, va_list ap)
+{
+       __lttng_ust_vtracef(fmt, ap);
+}
+
+void _lttng_ust_tracef(const char *fmt, ...)
+{
+       va_list ap;
+
+       va_start(ap, fmt);
+       __lttng_ust_vtracef(fmt, ap);
        va_end(ap);
 }
        va_end(ap);
 }
This page took 0.029053 seconds and 4 git commands to generate.