| 1 | /* |
| 2 | * ltt-probes.c |
| 3 | * |
| 4 | * Copyright 2010 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * |
| 6 | * Holds LTTng probes registry. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/list.h> |
| 11 | #include <linux/mutex.h> |
| 12 | #include <linux/seq_file.h> |
| 13 | |
| 14 | #include "ltt-events.h" |
| 15 | |
| 16 | static LIST_HEAD(probe_list); |
| 17 | static DEFINE_MUTEX(probe_mutex); |
| 18 | |
| 19 | static |
| 20 | const struct lttng_event_desc *find_event(const char *name) |
| 21 | { |
| 22 | struct lttng_probe_desc *probe_desc; |
| 23 | int i; |
| 24 | |
| 25 | list_for_each_entry(probe_desc, &probe_list, head) { |
| 26 | for (i = 0; i < probe_desc->nr_events; i++) { |
| 27 | if (!strcmp(probe_desc->event_desc[i].name, name)) |
| 28 | return &probe_desc->event_desc[i]; |
| 29 | } |
| 30 | } |
| 31 | return NULL; |
| 32 | } |
| 33 | |
| 34 | int ltt_probe_register(struct lttng_probe_desc *desc) |
| 35 | { |
| 36 | int ret = 0; |
| 37 | int i; |
| 38 | |
| 39 | mutex_lock(&probe_mutex); |
| 40 | /* |
| 41 | * TODO: This is O(N^2). Turn into a hash table when probe registration |
| 42 | * overhead becomes an issue. |
| 43 | */ |
| 44 | for (i = 0; i < desc->nr_events; i++) { |
| 45 | if (find_event(desc->event_desc[i].name)) { |
| 46 | ret = -EEXIST; |
| 47 | goto end; |
| 48 | } |
| 49 | } |
| 50 | list_add(&desc->head, &probe_list); |
| 51 | end: |
| 52 | mutex_unlock(&probe_mutex); |
| 53 | return ret; |
| 54 | } |
| 55 | EXPORT_SYMBOL_GPL(ltt_probe_register); |
| 56 | |
| 57 | void ltt_probe_unregister(struct lttng_probe_desc *desc) |
| 58 | { |
| 59 | mutex_lock(&probe_mutex); |
| 60 | list_del(&desc->head); |
| 61 | mutex_unlock(&probe_mutex); |
| 62 | } |
| 63 | EXPORT_SYMBOL_GPL(ltt_probe_unregister); |
| 64 | |
| 65 | const struct lttng_event_desc *ltt_event_get(const char *name) |
| 66 | { |
| 67 | const struct lttng_event_desc *event; |
| 68 | int ret; |
| 69 | |
| 70 | mutex_lock(&probe_mutex); |
| 71 | event = find_event(name); |
| 72 | mutex_unlock(&probe_mutex); |
| 73 | if (!event) |
| 74 | return NULL; |
| 75 | ret = try_module_get(event->owner); |
| 76 | WARN_ON_ONCE(!ret); |
| 77 | return event; |
| 78 | } |
| 79 | EXPORT_SYMBOL_GPL(ltt_event_get); |
| 80 | |
| 81 | void ltt_event_put(const struct lttng_event_desc *event) |
| 82 | { |
| 83 | module_put(event->owner); |
| 84 | } |
| 85 | EXPORT_SYMBOL_GPL(ltt_event_put); |
| 86 | |
| 87 | static |
| 88 | void *tp_list_start(struct seq_file *m, loff_t *pos) |
| 89 | { |
| 90 | struct lttng_probe_desc *probe_desc; |
| 91 | int iter = 0, i; |
| 92 | |
| 93 | mutex_lock(&probe_mutex); |
| 94 | list_for_each_entry(probe_desc, &probe_list, head) { |
| 95 | for (i = 0; i < probe_desc->nr_events; i++) { |
| 96 | if (iter++ >= *pos) |
| 97 | return (void *) &probe_desc->event_desc[i]; |
| 98 | } |
| 99 | } |
| 100 | /* End of list */ |
| 101 | return NULL; |
| 102 | } |
| 103 | |
| 104 | static |
| 105 | void *tp_list_next(struct seq_file *m, void *p, loff_t *ppos) |
| 106 | { |
| 107 | struct lttng_probe_desc *probe_desc; |
| 108 | int iter = 0, i; |
| 109 | |
| 110 | (*ppos)++; |
| 111 | list_for_each_entry(probe_desc, &probe_list, head) { |
| 112 | for (i = 0; i < probe_desc->nr_events; i++) { |
| 113 | if (iter++ >= *ppos) |
| 114 | return (void *) &probe_desc->event_desc[i]; |
| 115 | } |
| 116 | } |
| 117 | /* End of list */ |
| 118 | return NULL; |
| 119 | } |
| 120 | |
| 121 | static |
| 122 | void tp_list_stop(struct seq_file *m, void *p) |
| 123 | { |
| 124 | mutex_unlock(&probe_mutex); |
| 125 | } |
| 126 | |
| 127 | static |
| 128 | int tp_list_show(struct seq_file *m, void *p) |
| 129 | { |
| 130 | const struct lttng_event_desc *probe_desc = p; |
| 131 | |
| 132 | /* |
| 133 | * Don't export lttng internal events (metadata). |
| 134 | */ |
| 135 | if (!strncmp(probe_desc->name, "lttng_", sizeof("lttng_") - 1)) |
| 136 | return 0; |
| 137 | seq_printf(m, "event { name = %s; };\n", |
| 138 | probe_desc->name); |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | static |
| 143 | const struct seq_operations lttng_tracepoint_list_seq_ops = { |
| 144 | .start = tp_list_start, |
| 145 | .next = tp_list_next, |
| 146 | .stop = tp_list_stop, |
| 147 | .show = tp_list_show, |
| 148 | }; |
| 149 | |
| 150 | static |
| 151 | int lttng_tracepoint_list_open(struct inode *inode, struct file *file) |
| 152 | { |
| 153 | return seq_open(file, <tng_tracepoint_list_seq_ops); |
| 154 | } |
| 155 | |
| 156 | const struct file_operations lttng_tracepoint_list_fops = { |
| 157 | .open = lttng_tracepoint_list_open, |
| 158 | .read = seq_read, |
| 159 | .llseek = seq_lseek, |
| 160 | .release = seq_release, |
| 161 | }; |