Move include/ust/ to include/lttng/
[lttng-ust.git] / libust / tracepoint.c
index ca40902494f61b9785a1e0b863852dca4694aff3..8bf875281a97769ed26f2d96974343427f7aa314 100644 (file)
@@ -1,11 +1,11 @@
 /*
- * Copyright (C) 2008 Mathieu Desnoyers
+ * Copyright (C) 2008-2011 Mathieu Desnoyers
  * 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.
+ * 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
  * Ported to userspace by Pierre-Marc Fournier.
  */
 
-#include <errno.h>
-#include <ust/tracepoint.h>
-#include <ust/core.h>
-#include <ust/kcompat/kcompat.h>
-#include "usterr.h"
-
 #define _LGPL_SOURCE
+#include <errno.h>
+#include <lttng/tracepoint.h>
+#include <lttng/tracepoint-internal.h>
+#include <lttng/core.h>
+#include <lttng/kcompat/kcompat.h>
 #include <urcu-bp.h>
 #include <urcu/hlist.h>
+#include <urcu/uatomic.h>
 
-//extern struct tracepoint __start___tracepoints[] __attribute__((visibility("hidden")));
-//extern struct tracepoint __stop___tracepoints[] __attribute__((visibility("hidden")));
+#include <lttng/usterr-signal-safe.h>
+#include "ltt-tracer-core.h"
 
 /* Set to 1 to enable tracepoint debug output */
 static const int tracepoint_debug;
+static int initialized;
+static void (*new_tracepoint_cb)(struct tracepoint *);
 
 /* libraries that contain tracepoints (struct tracepoint_lib) */
 static CDS_LIST_HEAD(libs);
 
 /*
- * tracepoints_mutex nests inside module_mutex. Tracepoints mutex protects the
- * builtin and module tracepoints and the hash table.
+ * The UST lock protects the library tracepoints, the hash table, and
+ * the library list.
+ * All calls to the tracepoint API must be protected by the UST lock,
+ * excepts calls to tracepoint_register_lib and
+ * tracepoint_unregister_lib, which take the UST lock themselves.
  */
-static DEFINE_MUTEX(tracepoints_mutex);
 
 /*
  * Tracepoint hash table, containing the active tracepoints.
@@ -52,6 +56,9 @@ static DEFINE_MUTEX(tracepoints_mutex);
 #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
 static struct cds_hlist_head tracepoint_table[TRACEPOINT_TABLE_SIZE];
 
+static CDS_LIST_HEAD(old_probes);
+static int need_update;
+
 /*
  * Note about RCU :
  * It is used to to delay the free of multiple probes array until a quiescent
@@ -60,37 +67,30 @@ static struct cds_hlist_head tracepoint_table[TRACEPOINT_TABLE_SIZE];
  */
 struct tracepoint_entry {
        struct cds_hlist_node hlist;
-       struct probe *probes;
+       struct tracepoint_probe *probes;
        int refcount;   /* Number of times armed. 0 if disarmed. */
        char name[0];
 };
 
 struct tp_probes {
        union {
-//ust//                struct rcu_head rcu;
                struct cds_list_head list;
        } u;
-       struct probe probes[0];
+       struct tracepoint_probe probes[0];
 };
 
 static inline void *allocate_probes(int count)
 {
-       struct tp_probes *p  = zmalloc(count * sizeof(struct probe)
+       struct tp_probes *p  = zmalloc(count * sizeof(struct tracepoint_probe)
                        + sizeof(struct tp_probes));
        return p == NULL ? NULL : p->probes;
 }
 
-//ust// static void rcu_free_old_probes(struct rcu_head *head)
-//ust// {
-//ust//        kfree(container_of(head, struct tp_probes, u.rcu));
-//ust// }
-
 static inline void release_probes(void *old)
 {
        if (old) {
                struct tp_probes *tp_probes = _ust_container_of(old,
                        struct tp_probes, probes[0]);
-//ust//                call_rcu_sched(&tp_probes->u.rcu, rcu_free_old_probes);
                synchronize_rcu();
                free(tp_probes);
        }
@@ -112,7 +112,7 @@ tracepoint_entry_add_probe(struct tracepoint_entry *entry,
                           void *probe, void *data)
 {
        int nr_probes = 0;
-       struct probe *old, *new;
+       struct tracepoint_probe *old, *new;
 
        WARN_ON(!probe);
 
@@ -130,7 +130,7 @@ tracepoint_entry_add_probe(struct tracepoint_entry *entry,
        if (new == NULL)
                return ERR_PTR(-ENOMEM);
        if (old)
-               memcpy(new, old, nr_probes * sizeof(struct probe));
+               memcpy(new, old, nr_probes * sizeof(struct tracepoint_probe));
        new[nr_probes].func = probe;
        new[nr_probes].data = data;
        new[nr_probes + 1].func = NULL;
@@ -145,7 +145,7 @@ tracepoint_entry_remove_probe(struct tracepoint_entry *entry, void *probe,
                              void *data)
 {
        int nr_probes = 0, nr_del = 0, i;
-       struct probe *old, *new;
+       struct tracepoint_probe *old, *new;
 
        old = entry->probes;
 
@@ -241,7 +241,7 @@ static struct tracepoint_entry *add_tracepoint(const char *name)
 
 /*
  * Remove the tracepoint from the tracepoint hash table. Must be called with
- * mutex_lock held.
+ * ust_lock held.
  */
 static inline void remove_tracepoint(struct tracepoint_entry *e)
 {
@@ -265,7 +265,7 @@ static void set_tracepoint(struct tracepoint_entry **entry,
         * is used.
         */
        rcu_assign_pointer(elem->probes, (*entry)->probes);
-       elem->state__imv = active;
+       elem->state = active;
 }
 
 /*
@@ -276,7 +276,7 @@ static void set_tracepoint(struct tracepoint_entry **entry,
  */
 static void disable_tracepoint(struct tracepoint *elem)
 {
-       elem->state__imv = 0;
+       elem->state = 0;
        rcu_assign_pointer(elem->probes, NULL);
 }
 
@@ -287,14 +287,16 @@ static void disable_tracepoint(struct tracepoint *elem)
  *
  * Updates the probe callback corresponding to a range of tracepoints.
  */
+static
 void tracepoint_update_probe_range(struct tracepoint * const *begin,
                                   struct tracepoint * const *end)
 {
        struct tracepoint * const *iter;
        struct tracepoint_entry *mark_entry;
 
-       pthread_mutex_lock(&tracepoints_mutex);
        for (iter = begin; iter < end; iter++) {
+               if (!*iter)
+                       continue;       /* skip dummy */
                if (!(*iter)->name) {
                        disable_tracepoint(*iter);
                        continue;
@@ -307,18 +309,16 @@ void tracepoint_update_probe_range(struct tracepoint * const *begin,
                        disable_tracepoint(*iter);
                }
        }
-       pthread_mutex_unlock(&tracepoints_mutex);
 }
 
 static void lib_update_tracepoints(void)
 {
        struct tracepoint_lib *lib;
 
-//ust//        pthread_mutex_lock(&module_mutex);
-       cds_list_for_each_entry(lib, &libs, list)
+       cds_list_for_each_entry(lib, &libs, list) {
                tracepoint_update_probe_range(lib->tracepoints_start,
                                lib->tracepoints_start + lib->tracepoints_count);
-//ust//        pthread_mutex_unlock(&module_mutex);
+       }
 }
 
 /*
@@ -326,27 +326,21 @@ static void lib_update_tracepoints(void)
  */
 static void tracepoint_update_probes(void)
 {
-       /* Core kernel tracepoints */
-//ust//        tracepoint_update_probe_range(__start___tracepoints,
-//ust//                __stop___tracepoints);
-       /* tracepoints in modules. */
+       /* tracepoints registered from libraries and executable. */
        lib_update_tracepoints();
-       /* Update immediate values */
-       core_imv_update();
-//ust//        module_imv_update();
 }
 
-static struct probe *
+static struct tracepoint_probe *
 tracepoint_add_probe(const char *name, void *probe, void *data)
 {
        struct tracepoint_entry *entry;
-       struct probe *old;
+       struct tracepoint_probe *old;
 
        entry = get_tracepoint(name);
        if (!entry) {
                entry = add_tracepoint(name);
                if (IS_ERR(entry))
-                       return (struct probe *)entry;
+                       return (struct tracepoint_probe *)entry;
        }
        old = tracepoint_entry_add_probe(entry, probe, data);
        if (IS_ERR(old) && !entry->refcount)
@@ -355,20 +349,19 @@ tracepoint_add_probe(const char *name, void *probe, void *data)
 }
 
 /**
- * tracepoint_probe_register -  Connect a probe to a tracepoint
+ * __tracepoint_probe_register -  Connect a probe to a tracepoint
  * @name: tracepoint name
  * @probe: probe handler
  *
  * Returns 0 if ok, error value on error.
  * The probe address must at least be aligned on the architecture pointer size.
+ * Called with the UST lock held.
  */
-int tracepoint_probe_register(const char *name, void *probe, void *data)
+int __tracepoint_probe_register(const char *name, void *probe, void *data)
 {
        void *old;
 
-       pthread_mutex_lock(&tracepoints_mutex);
        old = tracepoint_add_probe(name, probe, data);
-       pthread_mutex_unlock(&tracepoints_mutex);
        if (IS_ERR(old))
                return PTR_ERR(old);
 
@@ -376,7 +369,6 @@ int tracepoint_probe_register(const char *name, void *probe, void *data)
        release_probes(old);
        return 0;
 }
-//ust// EXPORT_SYMBOL_GPL(tracepoint_probe_register);
 
 static void *tracepoint_remove_probe(const char *name, void *probe, void *data)
 {
@@ -400,18 +392,13 @@ static void *tracepoint_remove_probe(const char *name, void *probe, void *data)
  * @probe: probe function pointer
  * @probe: probe data pointer
  *
- * We do not need to call a synchronize_sched to make sure the probes have
- * finished running before doing a module unload, because the module unload
- * itself uses stop_machine(), which insures that every preempt disabled section
- * have finished.
+ * Called with the UST lock held.
  */
-int tracepoint_probe_unregister(const char *name, void *probe, void *data)
+int __tracepoint_probe_unregister(const char *name, void *probe, void *data)
 {
        void *old;
 
-       pthread_mutex_lock(&tracepoints_mutex);
        old = tracepoint_remove_probe(name, probe, data);
-       pthread_mutex_unlock(&tracepoints_mutex);
        if (IS_ERR(old))
                return PTR_ERR(old);
 
@@ -419,10 +406,6 @@ int tracepoint_probe_unregister(const char *name, void *probe, void *data)
        release_probes(old);
        return 0;
 }
-//ust// EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
-
-static CDS_LIST_HEAD(old_probes);
-static int need_update;
 
 static void tracepoint_add_old_probes(void *old)
 {
@@ -440,23 +423,20 @@ static void tracepoint_add_old_probes(void *old)
  * @probe: probe handler
  *
  * caller must call tracepoint_probe_update_all()
+ * Called with the UST lock held.
  */
 int tracepoint_probe_register_noupdate(const char *name, void *probe,
                                       void *data)
 {
        void *old;
 
-       pthread_mutex_lock(&tracepoints_mutex);
        old = tracepoint_add_probe(name, probe, data);
        if (IS_ERR(old)) {
-               pthread_mutex_unlock(&tracepoints_mutex);
                return PTR_ERR(old);
        }
        tracepoint_add_old_probes(old);
-       pthread_mutex_unlock(&tracepoints_mutex);
        return 0;
 }
-//ust// EXPORT_SYMBOL_GPL(tracepoint_probe_register_noupdate);
 
 /**
  * tracepoint_probe_unregister_noupdate -  remove a probe but not disconnect
@@ -464,62 +444,56 @@ int tracepoint_probe_register_noupdate(const char *name, void *probe,
  * @probe: probe function pointer
  *
  * caller must call tracepoint_probe_update_all()
+ * Called with the UST lock held.
  */
 int tracepoint_probe_unregister_noupdate(const char *name, void *probe,
                                         void *data)
 {
        void *old;
 
-       pthread_mutex_lock(&tracepoints_mutex);
        old = tracepoint_remove_probe(name, probe, data);
        if (IS_ERR(old)) {
-               pthread_mutex_unlock(&tracepoints_mutex);
                return PTR_ERR(old);
        }
        tracepoint_add_old_probes(old);
-       pthread_mutex_unlock(&tracepoints_mutex);
        return 0;
 }
-//ust// EXPORT_SYMBOL_GPL(tracepoint_probe_unregister_noupdate);
 
 /**
  * tracepoint_probe_update_all -  update tracepoints
+ * Called with the UST lock held.
  */
 void tracepoint_probe_update_all(void)
 {
        CDS_LIST_HEAD(release_probes);
        struct tp_probes *pos, *next;
 
-       pthread_mutex_lock(&tracepoints_mutex);
        if (!need_update) {
-               pthread_mutex_unlock(&tracepoints_mutex);
                return;
        }
        if (!cds_list_empty(&old_probes))
                cds_list_replace_init(&old_probes, &release_probes);
        need_update = 0;
-       pthread_mutex_unlock(&tracepoints_mutex);
 
        tracepoint_update_probes();
        cds_list_for_each_entry_safe(pos, next, &release_probes, u.list) {
                cds_list_del(&pos->u.list);
-//ust//                call_rcu_sched(&pos->u.rcu, rcu_free_old_probes);
                synchronize_rcu();
                free(pos);
        }
 }
-//ust// EXPORT_SYMBOL_GPL(tracepoint_probe_update_all);
 
 /*
  * Returns 0 if current not found.
  * Returns 1 if current found.
+ *
+ * Called with tracepoint mutex held
  */
 int lib_get_iter_tracepoints(struct tracepoint_iter *iter)
 {
        struct tracepoint_lib *iter_lib;
        int found = 0;
 
-//ust//        pthread_mutex_lock(&module_mutex);
        cds_list_for_each_entry(iter_lib, &libs, list) {
                if (iter_lib < iter->lib)
                        continue;
@@ -533,7 +507,6 @@ int lib_get_iter_tracepoints(struct tracepoint_iter *iter)
                        break;
                }
        }
-//ust//        pthread_mutex_unlock(&module_mutex);
        return found;
 }
 
@@ -546,44 +519,46 @@ int lib_get_iter_tracepoints(struct tracepoint_iter *iter)
  * Returns whether a next tracepoint has been found (1) or not (0).
  * Will return the first tracepoint in the range if the input tracepoint is
  * NULL.
+ * Called with tracepoint mutex held.
  */
 int tracepoint_get_iter_range(struct tracepoint * const **tracepoint,
        struct tracepoint * const *begin, struct tracepoint * const *end)
 {
-       if (!*tracepoint && begin != end) {
+       if (!*tracepoint && begin != end)
                *tracepoint = begin;
-               return 1;
+       while (*tracepoint >= begin && *tracepoint < end) {
+               if (!**tracepoint)
+                       (*tracepoint)++;        /* skip dummy */
+               else
+                       return 1;
        }
-       if (*tracepoint >= begin && *tracepoint < end)
-               return 1;
        return 0;
 }
-//ust// EXPORT_SYMBOL_GPL(tracepoint_get_iter_range);
 
+/*
+ * Called with tracepoint mutex held.
+ */
 static void tracepoint_get_iter(struct tracepoint_iter *iter)
 {
        int found = 0;
 
-//ust//        /* Core kernel tracepoints */
-//ust//        if (!iter->module) {
-//ust//                found = tracepoint_get_iter_range(&iter->tracepoint,
-//ust//                                __start___tracepoints, __stop___tracepoints);
-//ust//                if (found)
-//ust//                        goto end;
-//ust//        }
        /* tracepoints in libs. */
        found = lib_get_iter_tracepoints(iter);
-//ust// end:
        if (!found)
                tracepoint_iter_reset(iter);
 }
 
+/*
+ * Called with UST lock held.
+ */
 void tracepoint_iter_start(struct tracepoint_iter *iter)
 {
        tracepoint_get_iter(iter);
 }
-//ust// EXPORT_SYMBOL_GPL(tracepoint_iter_start);
 
+/*
+ * Called with UST lock held.
+ */
 void tracepoint_iter_next(struct tracepoint_iter *iter)
 {
        iter->tracepoint++;
@@ -594,54 +569,18 @@ void tracepoint_iter_next(struct tracepoint_iter *iter)
         */
        tracepoint_get_iter(iter);
 }
-//ust// EXPORT_SYMBOL_GPL(tracepoint_iter_next);
 
+/*
+ * Called with UST lock held.
+ */
 void tracepoint_iter_stop(struct tracepoint_iter *iter)
 {
 }
-//ust// EXPORT_SYMBOL_GPL(tracepoint_iter_stop);
 
 void tracepoint_iter_reset(struct tracepoint_iter *iter)
 {
-//ust//        iter->module = NULL;
        iter->tracepoint = NULL;
 }
-//ust// EXPORT_SYMBOL_GPL(tracepoint_iter_reset);
-
-//ust// #ifdef CONFIG_MODULES
-
-//ust// int tracepoint_module_notify(struct notifier_block *self,
-//ust//                             unsigned long val, void *data)
-//ust// {
-//ust//        struct module *mod = data;
-//ust// 
-//ust//        switch (val) {
-//ust//        case MODULE_STATE_COMING:
-//ust//                tracepoint_update_probe_range(mod->tracepoints,
-//ust//                        mod->tracepoints + mod->num_tracepoints);
-//ust//                break;
-//ust//        case MODULE_STATE_GOING:
-//ust//                tracepoint_update_probe_range(mod->tracepoints,
-//ust//                        mod->tracepoints + mod->num_tracepoints);
-//ust//                break;
-//ust//        }
-//ust//        return 0;
-//ust// }
-
-//ust// struct notifier_block tracepoint_module_nb = {
-//ust//        .notifier_call = tracepoint_module_notify,
-//ust//        .priority = 0,
-//ust// };
-
-//ust// static int init_tracepoints(void)
-//ust// {
-//ust//        return register_module_notifier(&tracepoint_module_nb);
-//ust// }
-//ust// __initcall(init_tracepoints);
-
-//ust// #endif /* CONFIG_MODULES */
-
-static void (*new_tracepoint_cb)(struct tracepoint *) = NULL;
 
 void tracepoint_set_new_tracepoint_cb(void (*cb)(struct tracepoint *))
 {
@@ -652,32 +591,49 @@ static void new_tracepoints(struct tracepoint * const *start, struct tracepoint
 {
        if (new_tracepoint_cb) {
                struct tracepoint * const *t;
-               for(t=start; t < end; t++) {
-                       new_tracepoint_cb(*t);
+
+               for (t = start; t < end; t++) {
+                       if (*t)
+                               new_tracepoint_cb(*t);
                }
        }
 }
 
-int tracepoint_register_lib(struct tracepoint * const *tracepoints_start, int tracepoints_count)
+int tracepoint_register_lib(struct tracepoint * const *tracepoints_start,
+                           int tracepoints_count)
 {
-       struct tracepoint_lib *pl;
+       struct tracepoint_lib *pl, *iter;
+
+       init_tracepoint();
 
        pl = (struct tracepoint_lib *) zmalloc(sizeof(struct tracepoint_lib));
 
        pl->tracepoints_start = tracepoints_start;
        pl->tracepoints_count = tracepoints_count;
 
-       /* FIXME: maybe protect this with its own mutex? */
-       pthread_mutex_lock(&tracepoints_mutex);
+       ust_lock();
+       /*
+        * We sort the libs by struct lib pointer address.
+        */
+       cds_list_for_each_entry_reverse(iter, &libs, list) {
+               BUG_ON(iter == pl);    /* Should never be in the list twice */
+               if (iter < pl) {
+                       /* We belong to the location right after iter. */
+                       cds_list_add(&pl->list, &iter->list);
+                       goto lib_added;
+               }
+       }
+       /* We should be added at the head of the list */
        cds_list_add(&pl->list, &libs);
-       pthread_mutex_unlock(&tracepoints_mutex);
-
+lib_added:
        new_tracepoints(tracepoints_start, tracepoints_start + tracepoints_count);
 
-       /* FIXME: update just the loaded lib */
+       /* TODO: update just the loaded lib */
        lib_update_tracepoints();
+       ust_unlock();
 
-       DBG("just registered a tracepoints section from %p and having %d tracepoints", tracepoints_start, tracepoints_count);
+       DBG("just registered a tracepoints section from %p and having %d tracepoints",
+               tracepoints_start, tracepoints_count);
 
        return 0;
 }
@@ -686,8 +642,7 @@ int tracepoint_unregister_lib(struct tracepoint * const *tracepoints_start)
 {
        struct tracepoint_lib *lib;
 
-       pthread_mutex_lock(&tracepoints_mutex);
-
+       ust_lock();
        cds_list_for_each_entry(lib, &libs, list) {
                if (lib->tracepoints_start == tracepoints_start) {
                        struct tracepoint_lib *lib2free = lib;
@@ -696,8 +651,19 @@ int tracepoint_unregister_lib(struct tracepoint * const *tracepoints_start)
                        break;
                }
        }
-
-       pthread_mutex_unlock(&tracepoints_mutex);
+       ust_unlock();
 
        return 0;
 }
+
+void init_tracepoint(void)
+{
+       if (uatomic_xchg(&initialized, 1) == 1)
+               return;
+       init_usterr();
+}
+
+void exit_tracepoint(void)
+{
+       initialized = 0;
+}
This page took 0.036261 seconds and 4 git commands to generate.