Fix: Add kernel configuration for lttng clock plugin
[lttng-modules.git] / lttng-probes.c
CommitLineData
02119ee5 1/*
a90917c3 2 * lttng-probes.c
02119ee5 3 *
02119ee5 4 * Holds LTTng probes registry.
17baffe2 5 *
886d51a3
MD
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
02119ee5
MD
21 */
22
23#include <linux/module.h>
24#include <linux/list.h>
25#include <linux/mutex.h>
271b6681 26#include <linux/seq_file.h>
02119ee5 27
241ae9a8 28#include <lttng-events.h>
02119ee5 29
3c997079
MD
30/*
31 * probe list is protected by sessions lock.
32 */
33static LIST_HEAD(_probe_list);
34
35/*
36 * List of probes registered by not yet processed.
37 */
38static LIST_HEAD(lazy_probe_init);
02119ee5 39
3c997079
MD
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 */
45static int lazy_nesting;
46
47/*
48 * Called under sessions lock.
49 */
85a9ca7f 50static
3c997079 51int check_event_provider(struct lttng_probe_desc *desc)
02119ee5 52{
85a9ca7f 53 int i;
3c997079 54 size_t provider_name_len;
02119ee5 55
3c997079
MD
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 */
021153c0
MD
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 */
3c997079
MD
71 }
72 return 1;
73}
74
75/*
76 * Called under sessions lock.
77 */
78static
79void 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;
85a9ca7f 109 }
02119ee5 110 }
3c997079
MD
111 /* We should be added at the head of the list */
112 list_add(&desc->head, probe_list);
113desc_added:
33a39a3c 114 pr_debug("LTTng: just registered probe %s containing %u events\n",
3c997079
MD
115 desc->provider, desc->nr_events);
116}
117
118/*
119 * Called under sessions lock.
120 */
121static
122void 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 */
142struct 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
149static
150const 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 }
02119ee5
MD
160 return NULL;
161}
162
a90917c3 163int lttng_probe_register(struct lttng_probe_desc *desc)
02119ee5 164{
02119ee5 165 int ret = 0;
02119ee5 166
3c997079
MD
167 lttng_lock_sessions();
168
85a9ca7f 169 /*
3c997079 170 * Check if the provider has already been registered.
85a9ca7f 171 */
3c997079
MD
172 if (find_provider(desc->provider)) {
173 ret = -EEXIST;
174 goto end;
02119ee5 175 }
3c997079
MD
176 list_add(&desc->lazy_init_head, &lazy_probe_init);
177 desc->lazy = 1;
33a39a3c 178 pr_debug("LTTng: adding probe %s containing %u events to lazy registration list\n",
3c997079
MD
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();
02119ee5 187end:
3c997079 188 lttng_unlock_sessions();
02119ee5
MD
189 return ret;
190}
a90917c3 191EXPORT_SYMBOL_GPL(lttng_probe_register);
02119ee5 192
a90917c3 193void lttng_probe_unregister(struct lttng_probe_desc *desc)
02119ee5 194{
3c997079
MD
195 lttng_lock_sessions();
196 if (!desc->lazy)
197 list_del(&desc->head);
198 else
199 list_del(&desc->lazy_init_head);
33a39a3c 200 pr_debug("LTTng: just unregistered probe %s\n", desc->provider);
3c997079 201 lttng_unlock_sessions();
02119ee5 202}
a90917c3 203EXPORT_SYMBOL_GPL(lttng_probe_unregister);
02119ee5 204
3c997079
MD
205/*
206 * TODO: this is O(nr_probes * nb_events), could be faster.
207 * Called with sessions lock held.
208 */
209static
210const 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 */
a90917c3 227const struct lttng_event_desc *lttng_event_get(const char *name)
02119ee5 228{
85a9ca7f 229 const struct lttng_event_desc *event;
02119ee5
MD
230 int ret;
231
85a9ca7f
MD
232 event = find_event(name);
233 if (!event)
c099397a 234 return NULL;
dc7f600a 235 ret = try_module_get(event->owner);
02119ee5 236 WARN_ON_ONCE(!ret);
85a9ca7f 237 return event;
02119ee5 238}
a90917c3 239EXPORT_SYMBOL_GPL(lttng_event_get);
02119ee5 240
3c997079
MD
241/*
242 * Called with sessions lock held.
243 */
a90917c3 244void lttng_event_put(const struct lttng_event_desc *event)
02119ee5 245{
dc7f600a 246 module_put(event->owner);
02119ee5 247}
a90917c3 248EXPORT_SYMBOL_GPL(lttng_event_put);
271b6681
MD
249
250static
251void *tp_list_start(struct seq_file *m, loff_t *pos)
252{
253 struct lttng_probe_desc *probe_desc;
a1031097 254 struct list_head *probe_list;
271b6681
MD
255 int iter = 0, i;
256
3c997079 257 lttng_lock_sessions();
a1031097
MD
258 probe_list = lttng_get_probe_list_head();
259 list_for_each_entry(probe_desc, probe_list, head) {
271b6681
MD
260 for (i = 0; i < probe_desc->nr_events; i++) {
261 if (iter++ >= *pos)
f7bdf4db 262 return (void *) probe_desc->event_desc[i];
271b6681
MD
263 }
264 }
265 /* End of list */
266 return NULL;
267}
268
269static
270void *tp_list_next(struct seq_file *m, void *p, loff_t *ppos)
271{
272 struct lttng_probe_desc *probe_desc;
a1031097 273 struct list_head *probe_list;
271b6681
MD
274 int iter = 0, i;
275
276 (*ppos)++;
a1031097
MD
277 probe_list = lttng_get_probe_list_head();
278 list_for_each_entry(probe_desc, probe_list, head) {
271b6681
MD
279 for (i = 0; i < probe_desc->nr_events; i++) {
280 if (iter++ >= *ppos)
f7bdf4db 281 return (void *) probe_desc->event_desc[i];
271b6681
MD
282 }
283 }
284 /* End of list */
285 return NULL;
286}
287
288static
289void tp_list_stop(struct seq_file *m, void *p)
290{
3c997079 291 lttng_unlock_sessions();
271b6681
MD
292}
293
294static
295int 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
304static
305const 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
312static
313int lttng_tracepoint_list_open(struct inode *inode, struct file *file)
314{
315 return seq_open(file, &lttng_tracepoint_list_seq_ops);
316}
317
318const struct file_operations lttng_tracepoint_list_fops = {
2e54c205 319 .owner = THIS_MODULE,
271b6681
MD
320 .open = lttng_tracepoint_list_open,
321 .read = seq_read,
322 .llseek = seq_lseek,
a51729c7 323 .release = seq_release,
271b6681 324};
This page took 0.047001 seconds and 4 git commands to generate.