| 1 | /* |
| 2 | * lttng-probes.c |
| 3 | * |
| 4 | * Holds LTTng probes registry. |
| 5 | * |
| 6 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 7 | * |
| 8 | * This library is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Lesser General Public |
| 10 | * License as published by the Free Software Foundation; only |
| 11 | * version 2.1 of the License. |
| 12 | * |
| 13 | * This library is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * Lesser General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Lesser General Public |
| 19 | * License along with this library; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | */ |
| 22 | |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/list.h> |
| 25 | #include <linux/mutex.h> |
| 26 | #include <linux/seq_file.h> |
| 27 | |
| 28 | #include <lttng-events.h> |
| 29 | |
| 30 | /* |
| 31 | * probe list is protected by sessions lock. |
| 32 | */ |
| 33 | static LIST_HEAD(_probe_list); |
| 34 | |
| 35 | /* |
| 36 | * List of probes registered by not yet processed. |
| 37 | */ |
| 38 | static LIST_HEAD(lazy_probe_init); |
| 39 | |
| 40 | /* |
| 41 | * lazy_nesting counter ensures we don't trigger lazy probe registration |
| 42 | * fixup while we are performing the fixup. It is protected by the |
| 43 | * sessions lock. |
| 44 | */ |
| 45 | static int lazy_nesting; |
| 46 | |
| 47 | /* |
| 48 | * Called under sessions lock. |
| 49 | */ |
| 50 | static |
| 51 | int check_event_provider(struct lttng_probe_desc *desc) |
| 52 | { |
| 53 | int i; |
| 54 | size_t provider_name_len; |
| 55 | |
| 56 | provider_name_len = strnlen(desc->provider, |
| 57 | LTTNG_KERNEL_SYM_NAME_LEN - 1); |
| 58 | for (i = 0; i < desc->nr_events; i++) { |
| 59 | if (strncmp(desc->event_desc[i]->name, |
| 60 | desc->provider, |
| 61 | provider_name_len)) |
| 62 | return 0; /* provider mismatch */ |
| 63 | /* |
| 64 | * The event needs to contain at least provider name + _ + |
| 65 | * one or more letter. |
| 66 | */ |
| 67 | if (strlen(desc->event_desc[i]->name) <= provider_name_len + 1) |
| 68 | return 0; /* provider mismatch */ |
| 69 | if (desc->event_desc[i]->name[provider_name_len] != '_') |
| 70 | return 0; /* provider mismatch */ |
| 71 | } |
| 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * Called under sessions lock. |
| 77 | */ |
| 78 | static |
| 79 | void lttng_lazy_probe_register(struct lttng_probe_desc *desc) |
| 80 | { |
| 81 | struct lttng_probe_desc *iter; |
| 82 | struct list_head *probe_list; |
| 83 | |
| 84 | /* |
| 85 | * Each provider enforce that every event name begins with the |
| 86 | * provider name. Check this in an assertion for extra |
| 87 | * carefulness. This ensures we cannot have duplicate event |
| 88 | * names across providers. |
| 89 | */ |
| 90 | WARN_ON_ONCE(!check_event_provider(desc)); |
| 91 | |
| 92 | /* |
| 93 | * The provider ensures there are no duplicate event names. |
| 94 | * Duplicated TRACEPOINT_EVENT event names would generate a |
| 95 | * compile-time error due to duplicated symbol names. |
| 96 | */ |
| 97 | |
| 98 | /* |
| 99 | * We sort the providers by struct lttng_probe_desc pointer |
| 100 | * address. |
| 101 | */ |
| 102 | probe_list = &_probe_list; |
| 103 | list_for_each_entry_reverse(iter, probe_list, head) { |
| 104 | BUG_ON(iter == desc); /* Should never be in the list twice */ |
| 105 | if (iter < desc) { |
| 106 | /* We belong to the location right after iter. */ |
| 107 | list_add(&desc->head, &iter->head); |
| 108 | goto desc_added; |
| 109 | } |
| 110 | } |
| 111 | /* We should be added at the head of the list */ |
| 112 | list_add(&desc->head, probe_list); |
| 113 | desc_added: |
| 114 | pr_debug("LTTng: just registered probe %s containing %u events\n", |
| 115 | desc->provider, desc->nr_events); |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * Called under sessions lock. |
| 120 | */ |
| 121 | static |
| 122 | void fixup_lazy_probes(void) |
| 123 | { |
| 124 | struct lttng_probe_desc *iter, *tmp; |
| 125 | int ret; |
| 126 | |
| 127 | lazy_nesting++; |
| 128 | list_for_each_entry_safe(iter, tmp, |
| 129 | &lazy_probe_init, lazy_init_head) { |
| 130 | lttng_lazy_probe_register(iter); |
| 131 | iter->lazy = 0; |
| 132 | list_del(&iter->lazy_init_head); |
| 133 | } |
| 134 | ret = lttng_fix_pending_events(); |
| 135 | WARN_ON_ONCE(ret); |
| 136 | lazy_nesting--; |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * Called under sessions lock. |
| 141 | */ |
| 142 | struct list_head *lttng_get_probe_list_head(void) |
| 143 | { |
| 144 | if (!lazy_nesting && !list_empty(&lazy_probe_init)) |
| 145 | fixup_lazy_probes(); |
| 146 | return &_probe_list; |
| 147 | } |
| 148 | |
| 149 | static |
| 150 | const struct lttng_probe_desc *find_provider(const char *provider) |
| 151 | { |
| 152 | struct lttng_probe_desc *iter; |
| 153 | struct list_head *probe_list; |
| 154 | |
| 155 | probe_list = lttng_get_probe_list_head(); |
| 156 | list_for_each_entry(iter, probe_list, head) { |
| 157 | if (!strcmp(iter->provider, provider)) |
| 158 | return iter; |
| 159 | } |
| 160 | return NULL; |
| 161 | } |
| 162 | |
| 163 | int lttng_probe_register(struct lttng_probe_desc *desc) |
| 164 | { |
| 165 | int ret = 0; |
| 166 | |
| 167 | lttng_lock_sessions(); |
| 168 | |
| 169 | /* |
| 170 | * Check if the provider has already been registered. |
| 171 | */ |
| 172 | if (find_provider(desc->provider)) { |
| 173 | ret = -EEXIST; |
| 174 | goto end; |
| 175 | } |
| 176 | list_add(&desc->lazy_init_head, &lazy_probe_init); |
| 177 | desc->lazy = 1; |
| 178 | pr_debug("LTTng: adding probe %s containing %u events to lazy registration list\n", |
| 179 | desc->provider, desc->nr_events); |
| 180 | /* |
| 181 | * If there is at least one active session, we need to register |
| 182 | * the probe immediately, since we cannot delay event |
| 183 | * registration because they are needed ASAP. |
| 184 | */ |
| 185 | if (lttng_session_active()) |
| 186 | fixup_lazy_probes(); |
| 187 | end: |
| 188 | lttng_unlock_sessions(); |
| 189 | return ret; |
| 190 | } |
| 191 | EXPORT_SYMBOL_GPL(lttng_probe_register); |
| 192 | |
| 193 | void lttng_probe_unregister(struct lttng_probe_desc *desc) |
| 194 | { |
| 195 | lttng_lock_sessions(); |
| 196 | if (!desc->lazy) |
| 197 | list_del(&desc->head); |
| 198 | else |
| 199 | list_del(&desc->lazy_init_head); |
| 200 | pr_debug("LTTng: just unregistered probe %s\n", desc->provider); |
| 201 | lttng_unlock_sessions(); |
| 202 | } |
| 203 | EXPORT_SYMBOL_GPL(lttng_probe_unregister); |
| 204 | |
| 205 | /* |
| 206 | * TODO: this is O(nr_probes * nb_events), could be faster. |
| 207 | * Called with sessions lock held. |
| 208 | */ |
| 209 | static |
| 210 | const struct lttng_event_desc *find_event(const char *name) |
| 211 | { |
| 212 | struct lttng_probe_desc *probe_desc; |
| 213 | int i; |
| 214 | |
| 215 | list_for_each_entry(probe_desc, &_probe_list, head) { |
| 216 | for (i = 0; i < probe_desc->nr_events; i++) { |
| 217 | if (!strcmp(probe_desc->event_desc[i]->name, name)) |
| 218 | return probe_desc->event_desc[i]; |
| 219 | } |
| 220 | } |
| 221 | return NULL; |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * Called with sessions lock held. |
| 226 | */ |
| 227 | const struct lttng_event_desc *lttng_event_get(const char *name) |
| 228 | { |
| 229 | const struct lttng_event_desc *event; |
| 230 | int ret; |
| 231 | |
| 232 | event = find_event(name); |
| 233 | if (!event) |
| 234 | return NULL; |
| 235 | ret = try_module_get(event->owner); |
| 236 | WARN_ON_ONCE(!ret); |
| 237 | return event; |
| 238 | } |
| 239 | EXPORT_SYMBOL_GPL(lttng_event_get); |
| 240 | |
| 241 | /* |
| 242 | * Called with sessions lock held. |
| 243 | */ |
| 244 | void lttng_event_put(const struct lttng_event_desc *event) |
| 245 | { |
| 246 | module_put(event->owner); |
| 247 | } |
| 248 | EXPORT_SYMBOL_GPL(lttng_event_put); |
| 249 | |
| 250 | static |
| 251 | void *tp_list_start(struct seq_file *m, loff_t *pos) |
| 252 | { |
| 253 | struct lttng_probe_desc *probe_desc; |
| 254 | struct list_head *probe_list; |
| 255 | int iter = 0, i; |
| 256 | |
| 257 | lttng_lock_sessions(); |
| 258 | probe_list = lttng_get_probe_list_head(); |
| 259 | list_for_each_entry(probe_desc, probe_list, head) { |
| 260 | for (i = 0; i < probe_desc->nr_events; i++) { |
| 261 | if (iter++ >= *pos) |
| 262 | return (void *) probe_desc->event_desc[i]; |
| 263 | } |
| 264 | } |
| 265 | /* End of list */ |
| 266 | return NULL; |
| 267 | } |
| 268 | |
| 269 | static |
| 270 | void *tp_list_next(struct seq_file *m, void *p, loff_t *ppos) |
| 271 | { |
| 272 | struct lttng_probe_desc *probe_desc; |
| 273 | struct list_head *probe_list; |
| 274 | int iter = 0, i; |
| 275 | |
| 276 | (*ppos)++; |
| 277 | probe_list = lttng_get_probe_list_head(); |
| 278 | list_for_each_entry(probe_desc, probe_list, head) { |
| 279 | for (i = 0; i < probe_desc->nr_events; i++) { |
| 280 | if (iter++ >= *ppos) |
| 281 | return (void *) probe_desc->event_desc[i]; |
| 282 | } |
| 283 | } |
| 284 | /* End of list */ |
| 285 | return NULL; |
| 286 | } |
| 287 | |
| 288 | static |
| 289 | void tp_list_stop(struct seq_file *m, void *p) |
| 290 | { |
| 291 | lttng_unlock_sessions(); |
| 292 | } |
| 293 | |
| 294 | static |
| 295 | int tp_list_show(struct seq_file *m, void *p) |
| 296 | { |
| 297 | const struct lttng_event_desc *probe_desc = p; |
| 298 | |
| 299 | seq_printf(m, "event { name = %s; };\n", |
| 300 | probe_desc->name); |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | static |
| 305 | const struct seq_operations lttng_tracepoint_list_seq_ops = { |
| 306 | .start = tp_list_start, |
| 307 | .next = tp_list_next, |
| 308 | .stop = tp_list_stop, |
| 309 | .show = tp_list_show, |
| 310 | }; |
| 311 | |
| 312 | static |
| 313 | int lttng_tracepoint_list_open(struct inode *inode, struct file *file) |
| 314 | { |
| 315 | return seq_open(file, <tng_tracepoint_list_seq_ops); |
| 316 | } |
| 317 | |
| 318 | const struct file_operations lttng_tracepoint_list_fops = { |
| 319 | .owner = THIS_MODULE, |
| 320 | .open = lttng_tracepoint_list_open, |
| 321 | .read = seq_read, |
| 322 | .llseek = seq_lseek, |
| 323 | .release = seq_release, |
| 324 | }; |