LTTng logger ABI
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Sat, 15 Feb 2014 21:59:09 +0000 (16:59 -0500)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Tue, 11 Mar 2014 01:23:39 +0000 (21:23 -0400)
Add a user-space ABI (new file /proc/lttng-logger) to lttng-modules
which can be written into by any user on the system. The content is
saved into the kernel trace stream into the "lttng_logger" kernel event.

The content of a single write is written into an lttng_logger event.
The write count is truncated to 1024 bytes (if larger), which is much
smaller than the smallest subbuffer size available (4096 bytes). This
ensures all written data makes it into the active tracing buffers.

Example use:

lttng-sessiond -d # (as root)
lttng create
lttng enable-event -k lttng_logger
lttng start

echo -n "this is a test" > /proc/lttng-logger

lttng stop
lttng view

Example result:

[17:00:04.251970425] (+179.750663203) thinkos lttng_logger: { cpu_id = 3 }, { _msg_length = 14, msg = "this is a test" }

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
CC: Karim Yaghmour <karim.yaghmour@opersys.com>
Makefile
instrumentation/events/lttng-module/lttng.h [new file with mode: 0644]
lttng-events.c
lttng-events.h
probes/lttng.c [new file with mode: 0644]

index a9d1cb15911ba8d16c4d9e2af301246b8e492c3b..a4c602fdb971277a7f17f9f3a3a666a883315894 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -20,7 +20,8 @@ lttng-tracer-objs :=  lttng-events.o lttng-abi.o \
                        lttng-context-vpid.o lttng-context-tid.o \
                        lttng-context-vtid.o lttng-context-ppid.o \
                        lttng-context-vppid.o lttng-calibrate.o \
-                       lttng-context-hostname.o wrapper/random.o
+                       lttng-context-hostname.o wrapper/random.o \
+                       probes/lttng.o
 
 obj-m += lttng-statedump.o
 lttng-statedump-objs := lttng-statedump-impl.o wrapper/irqdesc.o \
diff --git a/instrumentation/events/lttng-module/lttng.h b/instrumentation/events/lttng-module/lttng.h
new file mode 100644 (file)
index 0000000..c2ddf8d
--- /dev/null
@@ -0,0 +1,24 @@
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM lttng
+
+#if !defined(_TRACE_LTTNG_H) || defined(TRACE_HEADER_MULTI_READ)
+#define _TRACE_LTTNG_H
+
+#include <linux/tracepoint.h>
+
+TRACE_EVENT(lttng_logger,
+       TP_PROTO(const char __user *text, size_t len),
+       TP_ARGS(text, len),
+       TP_STRUCT__entry(
+               __dynamic_array_text(char, msg, len)
+       ),
+       TP_fast_assign(
+               tp_memcpy_dyn_from_user(msg, text)
+       ),
+       TP_printk("")
+)
+
+#endif /* _TRACE_LTTNG_H */
+
+/* This part must be outside protection */
+#include "../../../probes/define_trace.h"
index 0512a3f3ddfda66a72cf468f8f8c4730e2e4d4a1..ff28c9af559aecff2d5952a6f5fe9a43e2e343fe 100644 (file)
@@ -1261,7 +1261,13 @@ static int __init lttng_events_init(void)
        ret = lttng_abi_init();
        if (ret)
                goto error_abi;
+       ret = lttng_logger_init();
+       if (ret)
+               goto error_logger;
        return 0;
+
+error_logger:
+       lttng_abi_exit();
 error_abi:
        kmem_cache_destroy(event_cache);
        return ret;
@@ -1273,6 +1279,7 @@ static void __exit lttng_events_exit(void)
 {
        struct lttng_session *session, *tmpsession;
 
+       lttng_logger_exit();
        lttng_abi_exit();
        list_for_each_entry_safe(session, tmpsession, &sessions, list)
                lttng_session_destroy(session);
index 6b39304f8d114e92387308cac22d74c72e0f5a98..aeb2a6b25631242f76b81cf7ffb698a93ae9cee3 100644 (file)
@@ -429,6 +429,9 @@ int lttng_add_perf_counter_to_ctx(uint32_t type,
 }
 #endif
 
+int lttng_logger_init(void);
+void lttng_logger_exit(void);
+
 extern int lttng_statedump_start(struct lttng_session *session);
 
 #ifdef CONFIG_KPROBES
diff --git a/probes/lttng.c b/probes/lttng.c
new file mode 100644 (file)
index 0000000..8025687
--- /dev/null
@@ -0,0 +1,134 @@
+/*
+ * lttng.c
+ *
+ * LTTng logger ABI
+ *
+ * Copyright (C) 2008-2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.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; only
+ * 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 <linux/module.h>
+#include <linux/tracepoint.h>
+#include <linux/uaccess.h>
+#include <linux/gfp.h>
+#include <linux/fs.h>
+#include <linux/proc_fs.h>
+#include <linux/slab.h>
+#include <linux/mm.h>
+#include "../wrapper/vmalloc.h"
+#include "../lttng-events.h"
+
+#define TP_MODULE_NOAUTOLOAD
+#define LTTNG_PACKAGE_BUILD
+#define CREATE_TRACE_POINTS
+#define TRACE_INCLUDE_PATH ../instrumentation/events/lttng-module
+#define TRACE_INCLUDE_FILE lttng
+
+#include "../instrumentation/events/lttng-module/lttng.h"
+
+/* Events written through logger are truncated at 1024 bytes */
+#define LTTNG_LOGGER_COUNT_MAX 1024
+#define LTTNG_LOGGER_FILE      "lttng-logger"
+
+static struct proc_dir_entry *lttng_logger_dentry;
+
+/**
+ * lttng_logger_write - write a userspace string into the trace system
+ * @file: file pointer
+ * @user_buf: user string
+ * @count: length to copy
+ * @ppos: file position
+ *
+ * Copy a userspace string into a trace event named "lttng:logger".
+ * Copies at most @count bytes into the event "msg" dynamic array.
+ * Truncates the count at LTTNG_LOGGER_COUNT_MAX. Returns the number of
+ * bytes copied from the source.
+ * Return -1 on error, with EFAULT errno.
+ */
+static
+ssize_t lttng_logger_write(struct file *file, const char __user *user_buf,
+                   size_t count, loff_t *ppos)
+{
+       unsigned int nr_pages = 1, i;
+       unsigned long uaddr = (unsigned long) user_buf;
+       struct page *pages[2];
+       ssize_t written;
+       int ret;
+
+       /* Truncate count */
+       if (unlikely(count > LTTNG_LOGGER_COUNT_MAX))
+               count = LTTNG_LOGGER_COUNT_MAX;
+
+       /* How many pages are we dealing with ? */
+       if (unlikely((uaddr & PAGE_MASK) != ((uaddr + count) & PAGE_MASK)))
+               nr_pages = 2;
+
+       /* Pin userspace pages */
+       ret = get_user_pages_fast(uaddr, nr_pages, 0, pages);
+       if (unlikely(ret < nr_pages)) {
+               if (ret > 0) {
+                       BUG_ON(ret != 1);
+                       put_page(pages[0]);
+               }
+               written = -EFAULT;
+               goto end;
+       }
+
+       /* Trace the event */
+       trace_lttng_logger(user_buf, count);
+       written = count;
+       *ppos += written;
+
+       for (i = 0; i < nr_pages; i++)
+               put_page(pages[i]);
+end:
+       return written;
+}
+
+static const struct file_operations lttng_logger_operations = {
+       .write = lttng_logger_write,
+};
+
+int __init lttng_logger_init(void)
+{
+       int ret = 0;
+
+       wrapper_vmalloc_sync_all();
+       lttng_logger_dentry = proc_create_data(LTTNG_LOGGER_FILE,
+                               S_IRUGO | S_IWUGO, NULL,
+                               &lttng_logger_operations, NULL);
+       if (!lttng_logger_dentry) {
+               printk(KERN_ERR "Error creating LTTng logger file\n");
+               ret = -ENOMEM;
+               goto error;
+       }
+       ret = __lttng_events_init__lttng();
+       if (ret)
+               goto error_events;
+       return ret;
+
+error_events:
+       remove_proc_entry("lttng-logger", NULL);
+error:
+       return ret;
+}
+
+void __exit lttng_logger_exit(void)
+{
+       __lttng_events_exit__lttng();
+       if (lttng_logger_dentry)
+               remove_proc_entry("lttng-logger", NULL);
+}
This page took 0.031115 seconds and 4 git commands to generate.