Add event and context files
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 16 Jun 2011 19:36:27 +0000 (15:36 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 16 Jun 2011 19:36:27 +0000 (15:36 -0400)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
libust/ltt-context.c [new file with mode: 0644]
libust/ltt-events.c [new file with mode: 0644]
libust/ltt-events.h [new file with mode: 0644]

diff --git a/libust/ltt-context.c b/libust/ltt-context.c
new file mode 100644 (file)
index 0000000..634694b
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ * ltt-context.c
+ *
+ * Copyright 2011 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ *
+ * LTTng trace/channel/event context management.
+ *
+ * Dual LGPL v2.1/GPL v2 license.
+ */
+
+#include <linux/module.h>
+#include <linux/list.h>
+#include <linux/mutex.h>
+#include <linux/slab.h>
+#include "wrapper/vmalloc.h"   /* for wrapper_vmalloc_sync_all() */
+#include "ltt-events.h"
+#include "ltt-tracer.h"
+
+struct lttng_ctx_field *lttng_append_context(struct lttng_ctx **ctx_p)
+{
+       struct lttng_ctx_field *field;
+       struct lttng_ctx *ctx;
+
+       if (!*ctx_p) {
+               *ctx_p = kzalloc(sizeof(struct lttng_ctx), GFP_KERNEL);
+               if (!*ctx_p)
+                       return NULL;
+       }
+       ctx = *ctx_p;
+       if (ctx->nr_fields + 1 > ctx->allocated_fields) {
+               struct lttng_ctx_field *new_fields;
+
+               ctx->allocated_fields = max_t(size_t, 1, 2 * ctx->allocated_fields);
+               new_fields = kzalloc(ctx->allocated_fields * sizeof(struct lttng_ctx_field), GFP_KERNEL);
+               if (!new_fields)
+                       return NULL;
+               if (ctx->fields)
+                       memcpy(new_fields, ctx->fields, sizeof(*ctx->fields) * ctx->nr_fields);
+               kfree(ctx->fields);
+               ctx->fields = new_fields;
+       }
+       field = &ctx->fields[ctx->nr_fields];
+       ctx->nr_fields++;
+       return field;
+}
+EXPORT_SYMBOL_GPL(lttng_append_context);
+
+void lttng_remove_context_field(struct lttng_ctx **ctx_p,
+                               struct lttng_ctx_field *field)
+{
+       struct lttng_ctx *ctx;
+
+       ctx = *ctx_p;
+       ctx->nr_fields--;
+       memset(&ctx->fields[ctx->nr_fields], 0, sizeof(struct lttng_ctx_field));
+}
+EXPORT_SYMBOL_GPL(lttng_remove_context_field);
+
+void lttng_destroy_context(struct lttng_ctx *ctx)
+{
+       int i;
+
+       if (!ctx)
+               return;
+       for (i = 0; i < ctx->nr_fields; i++) {
+               if (ctx->fields[i].destroy)
+                       ctx->fields[i].destroy(&ctx->fields[i]);
+       }
+       kfree(ctx->fields);
+       kfree(ctx);
+}
diff --git a/libust/ltt-events.c b/libust/ltt-events.c
new file mode 100644 (file)
index 0000000..40e8e45
--- /dev/null
@@ -0,0 +1,894 @@
+/*
+ * ltt-events.c
+ *
+ * Copyright 2010 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ *
+ * Holds LTTng per-session event registry.
+ *
+ * Dual LGPL v2.1/GPL v2 license.
+ */
+
+#include <linux/module.h>
+#include <linux/list.h>
+#include <linux/mutex.h>
+#include <linux/sched.h>
+#include <linux/slab.h>
+#include <linux/jiffies.h>
+#include <linux/uuid.h>
+#include "wrapper/vmalloc.h"   /* for wrapper_vmalloc_sync_all() */
+#include "ltt-events.h"
+#include "ltt-tracer.h"
+
+static LIST_HEAD(sessions);
+static LIST_HEAD(ltt_transport_list);
+static DEFINE_MUTEX(sessions_mutex);
+static struct kmem_cache *event_cache;
+
+static void _ltt_event_destroy(struct ltt_event *event);
+static void _ltt_channel_destroy(struct ltt_channel *chan);
+static int _ltt_event_unregister(struct ltt_event *event);
+static
+int _ltt_event_metadata_statedump(struct ltt_session *session,
+                                 struct ltt_channel *chan,
+                                 struct ltt_event *event);
+static
+int _ltt_session_metadata_statedump(struct ltt_session *session);
+
+
+static
+void synchronize_trace(void)
+{
+       synchronize_sched();
+#ifdef CONFIG_PREEMPT_RT
+       synchronize_rcu();
+#endif
+}
+
+struct ltt_session *ltt_session_create(void)
+{
+       struct ltt_session *session;
+
+       mutex_lock(&sessions_mutex);
+       session = kzalloc(sizeof(struct ltt_session), GFP_KERNEL);
+       if (!session)
+               return NULL;
+       INIT_LIST_HEAD(&session->chan);
+       INIT_LIST_HEAD(&session->events);
+       uuid_le_gen(&session->uuid);
+       list_add(&session->list, &sessions);
+       mutex_unlock(&sessions_mutex);
+       return session;
+}
+
+void ltt_session_destroy(struct ltt_session *session)
+{
+       struct ltt_channel *chan, *tmpchan;
+       struct ltt_event *event, *tmpevent;
+       int ret;
+
+       mutex_lock(&sessions_mutex);
+       ACCESS_ONCE(session->active) = 0;
+       list_for_each_entry(event, &session->events, list) {
+               ret = _ltt_event_unregister(event);
+               WARN_ON(ret);
+       }
+       synchronize_trace();    /* Wait for in-flight events to complete */
+       list_for_each_entry_safe(event, tmpevent, &session->events, list)
+               _ltt_event_destroy(event);
+       list_for_each_entry_safe(chan, tmpchan, &session->chan, list)
+               _ltt_channel_destroy(chan);
+       list_del(&session->list);
+       mutex_unlock(&sessions_mutex);
+       kfree(session);
+}
+
+int ltt_session_start(struct ltt_session *session)
+{
+       int ret = 0;
+       struct ltt_channel *chan;
+
+       mutex_lock(&sessions_mutex);
+       if (session->active) {
+               ret = -EBUSY;
+               goto end;
+       }
+
+       /*
+        * Snapshot the number of events per channel to know the type of header
+        * we need to use.
+        */
+       list_for_each_entry(chan, &session->chan, list) {
+               if (chan->header_type)
+                       continue;               /* don't change it if session stop/restart */
+               if (chan->free_event_id < 31)
+                       chan->header_type = 1;  /* compact */
+               else
+                       chan->header_type = 2;  /* large */
+       }
+
+       ACCESS_ONCE(session->active) = 1;
+       ACCESS_ONCE(session->been_active) = 1;
+       synchronize_trace();    /* Wait for in-flight events to complete */
+       ret = _ltt_session_metadata_statedump(session);
+       if (ret) {
+               ACCESS_ONCE(session->active) = 0;
+               synchronize_trace();    /* Wait for in-flight events to complete */
+       }
+end:
+       mutex_unlock(&sessions_mutex);
+       return ret;
+}
+
+int ltt_session_stop(struct ltt_session *session)
+{
+       int ret = 0;
+
+       mutex_lock(&sessions_mutex);
+       if (!session->active) {
+               ret = -EBUSY;
+               goto end;
+       }
+       ACCESS_ONCE(session->active) = 0;
+       synchronize_trace();    /* Wait for in-flight events to complete */
+end:
+       mutex_unlock(&sessions_mutex);
+       return ret;
+}
+
+static struct ltt_transport *ltt_transport_find(const char *name)
+{
+       struct ltt_transport *transport;
+
+       list_for_each_entry(transport, &ltt_transport_list, node) {
+               if (!strcmp(transport->name, name))
+                       return transport;
+       }
+       return NULL;
+}
+
+struct ltt_channel *ltt_channel_create(struct ltt_session *session,
+                                      const char *transport_name,
+                                      void *buf_addr,
+                                      size_t subbuf_size, size_t num_subbuf,
+                                      unsigned int switch_timer_interval,
+                                      unsigned int read_timer_interval)
+{
+       struct ltt_channel *chan;
+       struct ltt_transport *transport;
+
+       mutex_lock(&sessions_mutex);
+       if (session->been_active)
+               goto active;    /* Refuse to add channel to active session */
+       transport = ltt_transport_find(transport_name);
+       if (!transport) {
+               printk(KERN_WARNING "LTTng transport %s not found\n",
+                      transport_name);
+               goto notransport;
+       }
+       chan = kzalloc(sizeof(struct ltt_channel), GFP_KERNEL);
+       if (!chan)
+               goto nomem;
+       chan->session = session;
+       chan->id = session->free_chan_id++;
+       /*
+        * Note: the channel creation op already writes into the packet
+        * headers. Therefore the "chan" information used as input
+        * should be already accessible.
+        */
+       chan->chan = transport->ops.channel_create("[lttng]", chan, buf_addr,
+                       subbuf_size, num_subbuf, switch_timer_interval,
+                       read_timer_interval);
+       if (!chan->chan)
+               goto create_error;
+       chan->ops = &transport->ops;
+       list_add(&chan->list, &session->chan);
+       mutex_unlock(&sessions_mutex);
+       return chan;
+
+create_error:
+       kfree(chan);
+nomem:
+notransport:
+active:
+       mutex_unlock(&sessions_mutex);
+       return NULL;
+}
+
+/*
+ * Only used internally at session destruction.
+ */
+static
+void _ltt_channel_destroy(struct ltt_channel *chan)
+{
+       chan->ops->channel_destroy(chan->chan);
+       list_del(&chan->list);
+       lttng_destroy_context(chan->ctx);
+       kfree(chan);
+}
+
+/*
+ * Supports event creation while tracing session is active.
+ */
+struct ltt_event *ltt_event_create(struct ltt_channel *chan,
+                                  struct lttng_kernel_event *event_param,
+                                  void *filter)
+{
+       struct ltt_event *event;
+       int ret;
+
+       mutex_lock(&sessions_mutex);
+       if (chan->free_event_id == -1UL)
+               goto full;
+       /*
+        * This is O(n^2) (for each event, the loop is called at event
+        * creation). Might require a hash if we have lots of events.
+        */
+       list_for_each_entry(event, &chan->session->events, list)
+               if (!strcmp(event->desc->name, event_param->name))
+                       goto exist;
+       event = kmem_cache_zalloc(event_cache, GFP_KERNEL);
+       if (!event)
+               goto cache_error;
+       event->chan = chan;
+       event->filter = filter;
+       event->id = chan->free_event_id++;
+       event->instrumentation = event_param->instrumentation;
+       /* Populate ltt_event structure before tracepoint registration. */
+       smp_wmb();
+       switch (event_param->instrumentation) {
+       case LTTNG_KERNEL_TRACEPOINT:
+               event->desc = ltt_event_get(event_param->name);
+               if (!event->desc)
+                       goto register_error;
+               ret = tracepoint_probe_register(event_param->name,
+                               event->desc->probe_callback,
+                               event);
+               if (ret)
+                       goto register_error;
+               break;
+       case LTTNG_KERNEL_KPROBE:
+               ret = lttng_kprobes_register(event_param->name,
+                               event_param->u.kprobe.symbol_name,
+                               event_param->u.kprobe.offset,
+                               event_param->u.kprobe.addr,
+                               event);
+               if (ret)
+                       goto register_error;
+               ret = try_module_get(event->desc->owner);
+               WARN_ON_ONCE(!ret);
+               break;
+       case LTTNG_KERNEL_FUNCTION:
+               ret = lttng_ftrace_register(event_param->name,
+                               event_param->u.ftrace.symbol_name,
+                               event);
+               if (ret)
+                       goto register_error;
+               ret = try_module_get(event->desc->owner);
+               WARN_ON_ONCE(!ret);
+               break;
+       default:
+               WARN_ON_ONCE(1);
+       }
+       ret = _ltt_event_metadata_statedump(chan->session, chan, event);
+       if (ret)
+               goto statedump_error;
+       list_add(&event->list, &chan->session->events);
+       mutex_unlock(&sessions_mutex);
+       return event;
+
+statedump_error:
+       WARN_ON_ONCE(tracepoint_probe_unregister(event_param->name,
+                               event->desc->probe_callback,
+                               event));
+       ltt_event_put(event->desc);
+register_error:
+       kmem_cache_free(event_cache, event);
+cache_error:
+exist:
+full:
+       mutex_unlock(&sessions_mutex);
+       return NULL;
+}
+
+/*
+ * Only used internally at session destruction.
+ */
+int _ltt_event_unregister(struct ltt_event *event)
+{
+       int ret = -EINVAL;
+
+       switch (event->instrumentation) {
+       case LTTNG_KERNEL_TRACEPOINT:
+               ret = tracepoint_probe_unregister(event->desc->name,
+                                                 event->desc->probe_callback,
+                                                 event);
+               if (ret)
+                       return ret;
+               break;
+       case LTTNG_KERNEL_KPROBE:
+               lttng_kprobes_unregister(event);
+               ret = 0;
+               break;
+       case LTTNG_KERNEL_FUNCTION:
+               lttng_ftrace_unregister(event);
+               ret = 0;
+               break;
+       default:
+               WARN_ON_ONCE(1);
+       }
+       return ret;
+}
+
+/*
+ * Only used internally at session destruction.
+ */
+static
+void _ltt_event_destroy(struct ltt_event *event)
+{
+       switch (event->instrumentation) {
+       case LTTNG_KERNEL_TRACEPOINT:
+               ltt_event_put(event->desc);
+               break;
+       case LTTNG_KERNEL_KPROBE:
+               module_put(event->desc->owner);
+               lttng_kprobes_destroy_private(event);
+               break;
+       case LTTNG_KERNEL_FUNCTION:
+               module_put(event->desc->owner);
+               lttng_ftrace_destroy_private(event);
+               break;
+       default:
+               WARN_ON_ONCE(1);
+       }
+       list_del(&event->list);
+       lttng_destroy_context(event->ctx);
+       kmem_cache_free(event_cache, event);
+}
+
+/*
+ * We have exclusive access to our metadata buffer (protected by the
+ * sessions_mutex), so we can do racy operations such as looking for
+ * remaining space left in packet and write, since mutual exclusion
+ * protects us from concurrent writes.
+ */
+int lttng_metadata_printf(struct ltt_session *session,
+                         const char *fmt, ...)
+{
+       struct lib_ring_buffer_ctx ctx;
+       struct ltt_channel *chan = session->metadata;
+       char *str;
+       int ret = 0, waitret;
+       size_t len, reserve_len, pos;
+       va_list ap;
+
+       WARN_ON_ONCE(!ACCESS_ONCE(session->active));
+
+       va_start(ap, fmt);
+       str = kvasprintf(GFP_KERNEL, fmt, ap);
+       va_end(ap);
+       if (!str)
+               return -ENOMEM;
+
+       len = strlen(str);
+       pos = 0;
+
+       for (pos = 0; pos < len; pos += reserve_len) {
+               reserve_len = min_t(size_t,
+                               chan->ops->packet_avail_size(chan->chan),
+                               len - pos);
+               lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len,
+                                        sizeof(char), -1);
+               /*
+                * We don't care about metadata buffer's records lost
+                * count, because we always retry here. Report error if
+                * we need to bail out after timeout or being
+                * interrupted.
+                */
+               waitret = wait_event_interruptible_timeout(*chan->ops->get_reader_wait_queue(chan->chan),
+                       ({
+                               ret = chan->ops->event_reserve(&ctx, 0);
+                               ret != -ENOBUFS || !ret;
+                       }),
+                       msecs_to_jiffies(LTTNG_METADATA_TIMEOUT_MSEC));
+               if (!waitret || waitret == -ERESTARTSYS || ret) {
+                       printk(KERN_WARNING "LTTng: Failure to write metadata to buffers (%s)\n",
+                               waitret == -ERESTARTSYS ? "interrupted" :
+                                       (ret == -ENOBUFS ? "timeout" : "I/O error"));
+                       if (waitret == -ERESTARTSYS)
+                               ret = waitret;
+                       goto end;
+               }
+               chan->ops->event_write(&ctx, &str[pos], reserve_len);
+               chan->ops->event_commit(&ctx);
+       }
+end:
+       kfree(str);
+       return ret;
+}
+
+static
+int _ltt_field_statedump(struct ltt_session *session,
+                        const struct lttng_event_field *field)
+{
+       int ret = 0;
+
+       switch (field->type.atype) {
+       case atype_integer:
+               ret = lttng_metadata_printf(session,
+                       "               integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } %s;\n",
+                       field->type.u.basic.integer.size,
+                       field->type.u.basic.integer.alignment,
+                       field->type.u.basic.integer.signedness,
+                       (field->type.u.basic.integer.encoding == lttng_encode_none)
+                               ? "none"
+                               : (field->type.u.basic.integer.encoding == lttng_encode_UTF8)
+                                       ? "UTF8"
+                                       : "ASCII",
+                       field->type.u.basic.integer.base,
+#ifdef __BIG_ENDIAN
+                       field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
+#else
+                       field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
+#endif
+                       field->name);
+               break;
+       case atype_enum:
+               ret = lttng_metadata_printf(session,
+                       "               %s %s;\n",
+                       field->type.u.basic.enumeration.name,
+                       field->name);
+               break;
+       case atype_array:
+       {
+               const struct lttng_basic_type *elem_type;
+
+               elem_type = &field->type.u.array.elem_type;
+               ret = lttng_metadata_printf(session,
+                       "               integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } %s[%u];\n",
+                       elem_type->u.basic.integer.size,
+                       elem_type->u.basic.integer.alignment,
+                       elem_type->u.basic.integer.signedness,
+                       (elem_type->u.basic.integer.encoding == lttng_encode_none)
+                               ? "none"
+                               : (elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
+                                       ? "UTF8"
+                                       : "ASCII",
+                       elem_type->u.basic.integer.base,
+#ifdef __BIG_ENDIAN
+                       elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
+#else
+                       elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
+#endif
+                       field->name, field->type.u.array.length);
+               break;
+       }
+       case atype_sequence:
+       {
+               const struct lttng_basic_type *elem_type;
+               const struct lttng_basic_type *length_type;
+
+               elem_type = &field->type.u.sequence.elem_type;
+               length_type = &field->type.u.sequence.length_type;
+               ret = lttng_metadata_printf(session,
+                       "               integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
+                       length_type->u.basic.integer.size,
+                       (unsigned int) length_type->u.basic.integer.alignment,
+                       length_type->u.basic.integer.signedness,
+                       (length_type->u.basic.integer.encoding == lttng_encode_none)
+                               ? "none"
+                               : ((length_type->u.basic.integer.encoding == lttng_encode_UTF8)
+                                       ? "UTF8"
+                                       : "ASCII"),
+                       length_type->u.basic.integer.base,
+#ifdef __BIG_ENDIAN
+                       length_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
+#else
+                       length_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
+#endif
+                       field->name);
+               if (ret)
+                       return ret;
+
+               ret = lttng_metadata_printf(session,
+                       "               integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } %s[ __%s_length ];\n",
+                       elem_type->u.basic.integer.size,
+                       (unsigned int) elem_type->u.basic.integer.alignment,
+                       elem_type->u.basic.integer.signedness,
+                       (elem_type->u.basic.integer.encoding == lttng_encode_none)
+                               ? "none"
+                               : ((elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
+                                       ? "UTF8"
+                                       : "ASCII"),
+                       elem_type->u.basic.integer.base,
+#ifdef __BIG_ENDIAN
+                       elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
+#else
+                       elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
+#endif
+                       field->name,
+                       field->name);
+               break;
+       }
+
+       case atype_string:
+               /* Default encoding is UTF8 */
+               ret = lttng_metadata_printf(session,
+                       "               string%s %s;\n",
+                       field->type.u.basic.string.encoding == lttng_encode_ASCII ?
+                               " { encoding = ASCII; }" : "",
+                       field->name);
+               break;
+       default:
+               WARN_ON_ONCE(1);
+               return -EINVAL;
+       }
+       return ret;
+}
+
+static
+int _ltt_context_metadata_statedump(struct ltt_session *session,
+                                   struct lttng_ctx *ctx)
+{
+       int ret = 0;
+       int i;
+
+       if (!ctx)
+               return 0;
+       for (i = 0; i < ctx->nr_fields; i++) {
+               const struct lttng_ctx_field *field = &ctx->fields[i];
+
+               ret = _ltt_field_statedump(session, &field->event_field);
+               if (ret)
+                       return ret;
+       }
+       return ret;
+}
+
+static
+int _ltt_fields_metadata_statedump(struct ltt_session *session,
+                                  struct ltt_event *event)
+{
+       const struct lttng_event_desc *desc = event->desc;
+       int ret = 0;
+       int i;
+
+       for (i = 0; i < desc->nr_fields; i++) {
+               const struct lttng_event_field *field = &desc->fields[i];
+
+               ret = _ltt_field_statedump(session, field);
+               if (ret)
+                       return ret;
+       }
+       return ret;
+}
+
+static
+int _ltt_event_metadata_statedump(struct ltt_session *session,
+                                 struct ltt_channel *chan,
+                                 struct ltt_event *event)
+{
+       int ret = 0;
+
+       if (event->metadata_dumped || !ACCESS_ONCE(session->active))
+               return 0;
+       if (chan == session->metadata)
+               return 0;
+
+       ret = lttng_metadata_printf(session,
+               "event {\n"
+               "       name = %s;\n"
+               "       id = %u;\n"
+               "       stream_id = %u;\n",
+               event->desc->name,
+               event->id,
+               event->chan->id);
+       if (ret)
+               goto end;
+
+       if (event->ctx) {
+               ret = lttng_metadata_printf(session,
+                       "       context := struct {\n");
+               if (ret)
+                       goto end;
+       }
+       ret = _ltt_context_metadata_statedump(session, event->ctx);
+       if (ret)
+               goto end;
+       if (event->ctx) {
+               ret = lttng_metadata_printf(session,
+                       "       };\n");
+               if (ret)
+                       goto end;
+       }
+
+       ret = lttng_metadata_printf(session,
+               "       fields := struct {\n"
+               );
+       if (ret)
+               goto end;
+
+       ret = _ltt_fields_metadata_statedump(session, event);
+       if (ret)
+               goto end;
+
+       /*
+        * LTTng space reservation can only reserve multiples of the
+        * byte size.
+        */
+       ret = lttng_metadata_printf(session,
+               "       };\n"
+               "};\n\n");
+       if (ret)
+               goto end;
+
+       event->metadata_dumped = 1;
+end:
+       return ret;
+
+}
+
+static
+int _ltt_channel_metadata_statedump(struct ltt_session *session,
+                                   struct ltt_channel *chan)
+{
+       int ret = 0;
+
+       if (chan->metadata_dumped || !ACCESS_ONCE(session->active))
+               return 0;
+       if (chan == session->metadata)
+               return 0;
+
+       WARN_ON_ONCE(!chan->header_type);
+       ret = lttng_metadata_printf(session,
+               "stream {\n"
+               "       id = %u;\n"
+               "       event.header := %s;\n"
+               "       packet.context := struct packet_context;\n",
+               chan->id,
+               chan->header_type == 1 ? "struct event_header_compact" :
+                       "struct event_header_large");
+       if (ret)
+               goto end;
+
+       if (chan->ctx) {
+               ret = lttng_metadata_printf(session,
+                       "       event.context := struct {\n");
+               if (ret)
+                       goto end;
+       }
+       ret = _ltt_context_metadata_statedump(session, chan->ctx);
+       if (ret)
+               goto end;
+       if (chan->ctx) {
+               ret = lttng_metadata_printf(session,
+                       "       };\n");
+               if (ret)
+                       goto end;
+       }
+
+       ret = lttng_metadata_printf(session,
+               "};\n\n");
+
+       chan->metadata_dumped = 1;
+end:
+       return ret;
+}
+
+static
+int _ltt_stream_packet_context_declare(struct ltt_session *session)
+{
+       return lttng_metadata_printf(session,
+               "struct packet_context {\n"
+               "       uint64_t timestamp_begin;\n"
+               "       uint64_t timestamp_end;\n"
+               "       uint32_t events_discarded;\n"
+               "       uint32_t content_size;\n"
+               "       uint32_t packet_size;\n"
+               "       uint32_t cpu_id;\n"
+               "};\n\n"
+               );
+}
+
+/*
+ * Compact header:
+ * id: range: 0 - 30.
+ * id 31 is reserved to indicate an extended header.
+ *
+ * Large header:
+ * id: range: 0 - 65534.
+ * id 65535 is reserved to indicate an extended header.
+ */
+static
+int _ltt_event_header_declare(struct ltt_session *session)
+{
+       return lttng_metadata_printf(session,
+       "struct event_header_compact {\n"
+       "       enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
+       "       variant <id> {\n"
+       "               struct {\n"
+       "                       uint27_t timestamp;\n"
+       "               } compact;\n"
+       "               struct {\n"
+       "                       uint32_t id;\n"
+       "                       uint64_t timestamp;\n"
+       "               } extended;\n"
+       "       } v;\n"
+       "} align(%u);\n"
+       "\n"
+       "struct event_header_large {\n"
+       "       enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
+       "       variant <id> {\n"
+       "               struct {\n"
+       "                       uint32_t timestamp;\n"
+       "               } compact;\n"
+       "               struct {\n"
+       "                       uint32_t id;\n"
+       "                       uint64_t timestamp;\n"
+       "               } extended;\n"
+       "       } v;\n"
+       "} align(%u);\n\n",
+       ltt_alignof(uint32_t) * CHAR_BIT,
+       ltt_alignof(uint16_t) * CHAR_BIT
+       );
+}
+
+/*
+ * Output metadata into this session's metadata buffers.
+ */
+static
+int _ltt_session_metadata_statedump(struct ltt_session *session)
+{
+       unsigned char *uuid_c = session->uuid.b;
+       unsigned char uuid_s[37];
+       struct ltt_channel *chan;
+       struct ltt_event *event;
+       int ret = 0;
+
+       if (!ACCESS_ONCE(session->active))
+               return 0;
+       if (session->metadata_dumped)
+               goto skip_session;
+       if (!session->metadata) {
+               printk(KERN_WARNING "LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
+               return -EPERM;
+       }
+
+       snprintf(uuid_s, sizeof(uuid_s),
+               "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
+               uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3],
+               uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7],
+               uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11],
+               uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]);
+
+       ret = lttng_metadata_printf(session,
+               "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
+               "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
+               "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
+               "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
+               "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
+               "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
+               "\n"
+               "trace {\n"
+               "       major = %u;\n"
+               "       minor = %u;\n"
+               "       uuid = \"%s\";\n"
+               "       byte_order = %s;\n"
+               "       packet.header := struct {\n"
+               "               uint32_t magic;\n"
+               "               uint8_t  uuid[16];\n"
+               "               uint32_t stream_id;\n"
+               "       };\n"
+               "};\n\n",
+               ltt_alignof(uint8_t) * CHAR_BIT,
+               ltt_alignof(uint16_t) * CHAR_BIT,
+               ltt_alignof(uint32_t) * CHAR_BIT,
+               ltt_alignof(uint64_t) * CHAR_BIT,
+               CTF_VERSION_MAJOR,
+               CTF_VERSION_MINOR,
+               uuid_s,
+#ifdef __BIG_ENDIAN
+               "be"
+#else
+               "le"
+#endif
+               );
+       if (ret)
+               goto end;
+
+       ret = _ltt_stream_packet_context_declare(session);
+       if (ret)
+               goto end;
+
+       ret = _ltt_event_header_declare(session);
+       if (ret)
+               goto end;
+
+skip_session:
+       list_for_each_entry(chan, &session->chan, list) {
+               ret = _ltt_channel_metadata_statedump(session, chan);
+               if (ret)
+                       goto end;
+       }
+
+       list_for_each_entry(event, &session->events, list) {
+               ret = _ltt_event_metadata_statedump(session, event->chan, event);
+               if (ret)
+                       goto end;
+       }
+       session->metadata_dumped = 1;
+end:
+       return ret;
+}
+
+/**
+ * ltt_transport_register - LTT transport registration
+ * @transport: transport structure
+ *
+ * Registers a transport which can be used as output to extract the data out of
+ * LTTng. The module calling this registration function must ensure that no
+ * trap-inducing code will be executed by the transport functions. E.g.
+ * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
+ * is made visible to the transport function. This registration acts as a
+ * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
+ * after its registration must it synchronize the TLBs.
+ */
+void ltt_transport_register(struct ltt_transport *transport)
+{
+       /*
+        * Make sure no page fault can be triggered by the module about to be
+        * registered. We deal with this here so we don't have to call
+        * vmalloc_sync_all() in each module's init.
+        */
+       wrapper_vmalloc_sync_all();
+
+       mutex_lock(&sessions_mutex);
+       list_add_tail(&transport->node, &ltt_transport_list);
+       mutex_unlock(&sessions_mutex);
+}
+EXPORT_SYMBOL_GPL(ltt_transport_register);
+
+/**
+ * ltt_transport_unregister - LTT transport unregistration
+ * @transport: transport structure
+ */
+void ltt_transport_unregister(struct ltt_transport *transport)
+{
+       mutex_lock(&sessions_mutex);
+       list_del(&transport->node);
+       mutex_unlock(&sessions_mutex);
+}
+EXPORT_SYMBOL_GPL(ltt_transport_unregister);
+
+static int __init ltt_events_init(void)
+{
+       int ret;
+
+       event_cache = KMEM_CACHE(ltt_event, 0);
+       if (!event_cache)
+               return -ENOMEM;
+       ret = ltt_debugfs_abi_init();
+       if (ret)
+               goto error_abi;
+       return 0;
+error_abi:
+       kmem_cache_destroy(event_cache);
+       return ret;
+}
+
+module_init(ltt_events_init);
+
+static void __exit ltt_events_exit(void)
+{
+       struct ltt_session *session, *tmpsession;
+
+       ltt_debugfs_abi_exit();
+       list_for_each_entry_safe(session, tmpsession, &sessions, list)
+               ltt_session_destroy(session);
+       kmem_cache_destroy(event_cache);
+}
+
+module_exit(ltt_events_exit);
+
+MODULE_LICENSE("GPL and additional rights");
+MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
+MODULE_DESCRIPTION("LTTng Events");
diff --git a/libust/ltt-events.h b/libust/ltt-events.h
new file mode 100644 (file)
index 0000000..05eff54
--- /dev/null
@@ -0,0 +1,349 @@
+#ifndef _LTT_EVENTS_H
+#define _LTT_EVENTS_H
+
+/*
+ * ltt-events.h
+ *
+ * Copyright 2010 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ *
+ * Holds LTTng per-session event registry.
+ *
+ * Dual LGPL v2.1/GPL v2 license.
+ */
+
+#include <linux/list.h>
+#include <linux/uuid.h>
+#include <linux/kprobes.h>
+#include "ltt-debugfs-abi.h"
+
+#undef is_signed_type
+#define is_signed_type(type)           (((type)(-1)) < 0)
+
+struct ltt_channel;
+struct ltt_session;
+struct lib_ring_buffer_ctx;
+struct perf_event;
+struct perf_event_attr;
+
+/* Type description */
+
+/* Update the astract_types name table in lttng-types.c along with this enum */
+enum abstract_types {
+       atype_integer,
+       atype_enum,
+       atype_array,
+       atype_sequence,
+       atype_string,
+       NR_ABSTRACT_TYPES,
+};
+
+/* Update the string_encodings name table in lttng-types.c along with this enum */
+enum lttng_string_encodings {
+       lttng_encode_none = 0,
+       lttng_encode_UTF8 = 1,
+       lttng_encode_ASCII = 2,
+       NR_STRING_ENCODINGS,
+};
+
+struct lttng_enum_entry {
+       unsigned long long start, end;  /* start and end are inclusive */
+       const char *string;
+};
+
+#define __type_integer(_type, _byte_order, _base, _encoding)   \
+       {                                                       \
+           .atype = atype_integer,                             \
+           .u.basic.integer =                                  \
+               {                                               \
+                 .size = sizeof(_type) * CHAR_BIT,             \
+                 .alignment = ltt_alignof(_type) * CHAR_BIT,   \
+                 .signedness = is_signed_type(_type),          \
+                 .reverse_byte_order = _byte_order != __BYTE_ORDER,    \
+                 .base = _base,                                \
+                 .encoding = lttng_encode_##_encoding,         \
+               },                                              \
+       }                                                       \
+
+struct lttng_integer_type {
+       unsigned int size;              /* in bits */
+       unsigned short alignment;       /* in bits */
+       unsigned int signedness:1;
+       unsigned int reverse_byte_order:1;
+       unsigned int base;              /* 2, 8, 10, 16, for pretty print */
+       enum lttng_string_encodings encoding;
+};
+
+union _lttng_basic_type {
+       struct lttng_integer_type integer;
+       struct {
+               const char *name;
+       } enumeration;
+       struct {
+               enum lttng_string_encodings encoding;
+       } string;
+};
+
+struct lttng_basic_type {
+       enum abstract_types atype;
+       union {
+               union _lttng_basic_type basic;
+       } u;
+};
+
+struct lttng_type {
+       enum abstract_types atype;
+       union {
+               union _lttng_basic_type basic;
+               struct {
+                       struct lttng_basic_type elem_type;
+                       unsigned int length;            /* num. elems. */
+               } array;
+               struct {
+                       struct lttng_basic_type length_type;
+                       struct lttng_basic_type elem_type;
+               } sequence;
+       } u;
+};
+
+struct lttng_enum {
+       const char *name;
+       struct lttng_type container_type;
+       const struct lttng_enum_entry *entries;
+       unsigned int len;
+};
+
+/* Event field description */
+
+struct lttng_event_field {
+       const char *name;
+       struct lttng_type type;
+};
+
+struct lttng_ctx_field {
+       struct lttng_event_field event_field;
+       size_t (*get_size)(size_t offset);
+       void (*record)(struct lttng_ctx_field *field,
+                      struct lib_ring_buffer_ctx *ctx,
+                      struct ltt_channel *chan);
+       union {
+               struct {
+                       struct perf_event **e;  /* per-cpu array */
+                       struct notifier_block nb;
+                       int hp_enable;
+                       struct perf_event_attr *attr;
+               } perf_counter;
+       } u;
+       void (*destroy)(struct lttng_ctx_field *field);
+};
+
+struct lttng_ctx {
+       struct lttng_ctx_field *fields;
+       unsigned int nr_fields;
+       unsigned int allocated_fields;
+};
+
+struct lttng_event_desc {
+       const char *name;
+       void *probe_callback;
+       const struct lttng_event_ctx *ctx;      /* context */
+       const struct lttng_event_field *fields; /* event payload */
+       unsigned int nr_fields;
+       struct module *owner;
+};
+
+struct lttng_probe_desc {
+       const struct lttng_event_desc *event_desc;
+       unsigned int nr_events;
+       struct list_head head;                  /* chain registered probes */
+};
+
+/*
+ * ltt_event structure is referred to by the tracing fast path. It must be
+ * kept small.
+ */
+struct ltt_event {
+       unsigned int id;
+       struct ltt_channel *chan;
+       const struct lttng_event_desc *desc;
+       void *filter;
+       struct lttng_ctx *ctx;
+       enum lttng_kernel_instrumentation instrumentation;
+       union {
+               struct {
+                       struct kprobe kp;
+                       char *symbol_name;
+               } kprobe;
+               struct {
+                       char *symbol_name;
+               } ftrace;
+       } u;
+       struct list_head list;          /* Event list */
+       int metadata_dumped:1;
+};
+
+struct ltt_channel_ops {
+       struct channel *(*channel_create)(const char *name,
+                               struct ltt_channel *ltt_chan,
+                               void *buf_addr,
+                               size_t subbuf_size, size_t num_subbuf,
+                               unsigned int switch_timer_interval,
+                               unsigned int read_timer_interval);
+       void (*channel_destroy)(struct channel *chan);
+       struct lib_ring_buffer *(*buffer_read_open)(struct channel *chan);
+       void (*buffer_read_close)(struct lib_ring_buffer *buf);
+       int (*event_reserve)(struct lib_ring_buffer_ctx *ctx,
+                            uint32_t event_id);
+       void (*event_commit)(struct lib_ring_buffer_ctx *ctx);
+       void (*event_write)(struct lib_ring_buffer_ctx *ctx, const void *src,
+                           size_t len);
+       /*
+        * packet_avail_size returns the available size in the current
+        * packet. Note that the size returned is only a hint, since it
+        * may change due to concurrent writes.
+        */
+       size_t (*packet_avail_size)(struct channel *chan);
+       wait_queue_head_t *(*get_reader_wait_queue)(struct channel *chan);
+       wait_queue_head_t *(*get_hp_wait_queue)(struct channel *chan);
+       int (*is_finalized)(struct channel *chan);
+       int (*is_disabled)(struct channel *chan);
+};
+
+struct ltt_channel {
+       unsigned int id;
+       struct channel *chan;           /* Channel buffers */
+       struct lttng_ctx *ctx;
+       /* Event ID management */
+       struct ltt_session *session;
+       struct file *file;              /* File associated to channel */
+       unsigned int free_event_id;     /* Next event ID to allocate */
+       struct list_head list;          /* Channel list */
+       struct ltt_channel_ops *ops;
+       int header_type;                /* 0: unset, 1: compact, 2: large */
+       int metadata_dumped:1;
+};
+
+struct ltt_session {
+       int active;                     /* Is trace session active ? */
+       int been_active;                /* Has trace session been active ? */
+       struct file *file;              /* File associated to session */
+       struct ltt_channel *metadata;   /* Metadata channel */
+       struct list_head chan;          /* Channel list head */
+       struct list_head events;        /* Event list head */
+       struct list_head list;          /* Session list */
+       unsigned int free_chan_id;      /* Next chan ID to allocate */
+       uuid_le uuid;                   /* Trace session unique ID */
+       int metadata_dumped:1;
+};
+
+struct ltt_transport {
+       char *name;
+       struct module *owner;
+       struct list_head node;
+       struct ltt_channel_ops ops;
+};
+
+struct ltt_session *ltt_session_create(void);
+int ltt_session_start(struct ltt_session *session);
+int ltt_session_stop(struct ltt_session *session);
+void ltt_session_destroy(struct ltt_session *session);
+
+struct ltt_channel *ltt_channel_create(struct ltt_session *session,
+                                      const char *transport_name,
+                                      void *buf_addr,
+                                      size_t subbuf_size, size_t num_subbuf,
+                                      unsigned int switch_timer_interval,
+                                      unsigned int read_timer_interval);
+struct ltt_channel *ltt_global_channel_create(struct ltt_session *session,
+                                      int overwrite, void *buf_addr,
+                                      size_t subbuf_size, size_t num_subbuf,
+                                      unsigned int switch_timer_interval,
+                                      unsigned int read_timer_interval);
+
+struct ltt_event *ltt_event_create(struct ltt_channel *chan,
+                                  struct lttng_kernel_event *event_param,
+                                  void *filter);
+
+void ltt_transport_register(struct ltt_transport *transport);
+void ltt_transport_unregister(struct ltt_transport *transport);
+
+int ltt_debugfs_abi_init(void);
+void ltt_debugfs_abi_exit(void);
+
+int ltt_probe_register(struct lttng_probe_desc *desc);
+void ltt_probe_unregister(struct lttng_probe_desc *desc);
+const struct lttng_event_desc *ltt_event_get(const char *name);
+void ltt_event_put(const struct lttng_event_desc *desc);
+int ltt_probes_init(void);
+void ltt_probes_exit(void);
+struct lttng_ctx_field *lttng_append_context(struct lttng_ctx **ctx);
+void lttng_remove_context_field(struct lttng_ctx **ctx,
+                               struct lttng_ctx_field *field);
+void lttng_destroy_context(struct lttng_ctx *ctx);
+int lttng_add_pid_to_ctx(struct lttng_ctx **ctx);
+int lttng_add_comm_to_ctx(struct lttng_ctx **ctx);
+int lttng_add_prio_to_ctx(struct lttng_ctx **ctx);
+int lttng_add_nice_to_ctx(struct lttng_ctx **ctx);
+int lttng_add_perf_counter_to_ctx(uint32_t type,
+                                 uint64_t config,
+                                 const char *name,
+                                 struct lttng_ctx **ctx);
+
+#ifdef CONFIG_KPROBES
+int lttng_kprobes_register(const char *name,
+               const char *symbol_name,
+               uint64_t offset,
+               uint64_t addr,
+               struct ltt_event *event);
+void lttng_kprobes_unregister(struct ltt_event *event);
+void lttng_kprobes_destroy_private(struct ltt_event *event);
+#else
+static inline
+int lttng_kprobes_register(const char *name,
+               const char *symbol_name,
+               uint64_t offset,
+               uint64_t addr,
+               struct ltt_event *event)
+{
+       return -ENOSYS;
+}
+
+static inline
+void lttng_kprobes_unregister(struct ltt_event *event)
+{
+}
+
+static inline
+void lttng_kprobes_destroy_private(struct ltt_event *event)
+{
+}
+#endif
+
+#ifdef CONFIG_DYNAMIC_FTRACE
+int lttng_ftrace_register(const char *name,
+                         const char *symbol_name,
+                         struct ltt_event *event);
+void lttng_ftrace_unregister(struct ltt_event *event);
+void lttng_ftrace_destroy_private(struct ltt_event *event);
+#else
+static inline
+int lttng_ftrace_register(const char *name,
+                         const char *symbol_name,
+                         struct ltt_event *event)
+{
+       return -ENOSYS;
+}
+
+static inline
+void lttng_ftrace_unregister(struct ltt_event *event)
+{
+}
+
+static inline
+void lttng_ftrace_destroy_private(struct ltt_event *event)
+{
+}
+#endif
+
+extern const struct file_operations lttng_tracepoint_list_fops;
+
+#endif /* _LTT_EVENTS_H */
This page took 0.038548 seconds and 4 git commands to generate.