Implement tracepoint wildcard support
[lttng-modules.git] / lttng-probes.c
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 return 1;
65 }
66
67 /*
68 * Called under sessions lock.
69 */
70 static
71 void lttng_lazy_probe_register(struct lttng_probe_desc *desc)
72 {
73 struct lttng_probe_desc *iter;
74 struct list_head *probe_list;
75
76 /*
77 * Each provider enforce that every event name begins with the
78 * provider name. Check this in an assertion for extra
79 * carefulness. This ensures we cannot have duplicate event
80 * names across providers.
81 */
82 WARN_ON_ONCE(!check_event_provider(desc));
83
84 /*
85 * The provider ensures there are no duplicate event names.
86 * Duplicated TRACEPOINT_EVENT event names would generate a
87 * compile-time error due to duplicated symbol names.
88 */
89
90 /*
91 * We sort the providers by struct lttng_probe_desc pointer
92 * address.
93 */
94 probe_list = &_probe_list;
95 list_for_each_entry_reverse(iter, probe_list, head) {
96 BUG_ON(iter == desc); /* Should never be in the list twice */
97 if (iter < desc) {
98 /* We belong to the location right after iter. */
99 list_add(&desc->head, &iter->head);
100 goto desc_added;
101 }
102 }
103 /* We should be added at the head of the list */
104 list_add(&desc->head, probe_list);
105 desc_added:
106 printk(KERN_DEBUG "just registered probe %s containing %u events\n",
107 desc->provider, desc->nr_events);
108 }
109
110 /*
111 * Called under sessions lock.
112 */
113 static
114 void fixup_lazy_probes(void)
115 {
116 struct lttng_probe_desc *iter, *tmp;
117 int ret;
118
119 lazy_nesting++;
120 list_for_each_entry_safe(iter, tmp,
121 &lazy_probe_init, lazy_init_head) {
122 lttng_lazy_probe_register(iter);
123 iter->lazy = 0;
124 list_del(&iter->lazy_init_head);
125 }
126 ret = lttng_fix_pending_events();
127 WARN_ON_ONCE(ret);
128 lazy_nesting--;
129 }
130
131 /*
132 * Called under sessions lock.
133 */
134 struct list_head *lttng_get_probe_list_head(void)
135 {
136 if (!lazy_nesting && !list_empty(&lazy_probe_init))
137 fixup_lazy_probes();
138 return &_probe_list;
139 }
140
141 static
142 const struct lttng_probe_desc *find_provider(const char *provider)
143 {
144 struct lttng_probe_desc *iter;
145 struct list_head *probe_list;
146
147 probe_list = lttng_get_probe_list_head();
148 list_for_each_entry(iter, probe_list, head) {
149 if (!strcmp(iter->provider, provider))
150 return iter;
151 }
152 return NULL;
153 }
154
155 int lttng_probe_register(struct lttng_probe_desc *desc)
156 {
157 int ret = 0;
158
159 lttng_lock_sessions();
160
161 /*
162 * Check if the provider has already been registered.
163 */
164 if (find_provider(desc->provider)) {
165 ret = -EEXIST;
166 goto end;
167 }
168 list_add(&desc->lazy_init_head, &lazy_probe_init);
169 desc->lazy = 1;
170 printk(KERN_DEBUG "adding probe %s containing %u events to lazy registration list\n",
171 desc->provider, desc->nr_events);
172 /*
173 * If there is at least one active session, we need to register
174 * the probe immediately, since we cannot delay event
175 * registration because they are needed ASAP.
176 */
177 if (lttng_session_active())
178 fixup_lazy_probes();
179 end:
180 lttng_unlock_sessions();
181 return ret;
182 }
183 EXPORT_SYMBOL_GPL(lttng_probe_register);
184
185 void lttng_probe_unregister(struct lttng_probe_desc *desc)
186 {
187 lttng_lock_sessions();
188 if (!desc->lazy)
189 list_del(&desc->head);
190 else
191 list_del(&desc->lazy_init_head);
192 printk(KERN_DEBUG "just unregistered probe %s\n", desc->provider);
193 lttng_unlock_sessions();
194 }
195 EXPORT_SYMBOL_GPL(lttng_probe_unregister);
196
197 /*
198 * TODO: this is O(nr_probes * nb_events), could be faster.
199 * Called with sessions lock held.
200 */
201 static
202 const struct lttng_event_desc *find_event(const char *name)
203 {
204 struct lttng_probe_desc *probe_desc;
205 int i;
206
207 list_for_each_entry(probe_desc, &_probe_list, head) {
208 for (i = 0; i < probe_desc->nr_events; i++) {
209 if (!strcmp(probe_desc->event_desc[i]->name, name))
210 return probe_desc->event_desc[i];
211 }
212 }
213 return NULL;
214 }
215
216 /*
217 * Called with sessions lock held.
218 */
219 const struct lttng_event_desc *lttng_event_get(const char *name)
220 {
221 const struct lttng_event_desc *event;
222 int ret;
223
224 event = find_event(name);
225 if (!event)
226 return NULL;
227 ret = try_module_get(event->owner);
228 WARN_ON_ONCE(!ret);
229 return event;
230 }
231 EXPORT_SYMBOL_GPL(lttng_event_get);
232
233 /*
234 * Called with sessions lock held.
235 */
236 void lttng_event_put(const struct lttng_event_desc *event)
237 {
238 module_put(event->owner);
239 }
240 EXPORT_SYMBOL_GPL(lttng_event_put);
241
242 static
243 void *tp_list_start(struct seq_file *m, loff_t *pos)
244 {
245 struct lttng_probe_desc *probe_desc;
246 int iter = 0, i;
247
248 lttng_lock_sessions();
249 list_for_each_entry(probe_desc, &_probe_list, head) {
250 for (i = 0; i < probe_desc->nr_events; i++) {
251 if (iter++ >= *pos)
252 return (void *) probe_desc->event_desc[i];
253 }
254 }
255 /* End of list */
256 return NULL;
257 }
258
259 static
260 void *tp_list_next(struct seq_file *m, void *p, loff_t *ppos)
261 {
262 struct lttng_probe_desc *probe_desc;
263 int iter = 0, i;
264
265 (*ppos)++;
266 list_for_each_entry(probe_desc, &_probe_list, head) {
267 for (i = 0; i < probe_desc->nr_events; i++) {
268 if (iter++ >= *ppos)
269 return (void *) probe_desc->event_desc[i];
270 }
271 }
272 /* End of list */
273 return NULL;
274 }
275
276 static
277 void tp_list_stop(struct seq_file *m, void *p)
278 {
279 lttng_unlock_sessions();
280 }
281
282 static
283 int tp_list_show(struct seq_file *m, void *p)
284 {
285 const struct lttng_event_desc *probe_desc = p;
286
287 seq_printf(m, "event { name = %s; };\n",
288 probe_desc->name);
289 return 0;
290 }
291
292 static
293 const struct seq_operations lttng_tracepoint_list_seq_ops = {
294 .start = tp_list_start,
295 .next = tp_list_next,
296 .stop = tp_list_stop,
297 .show = tp_list_show,
298 };
299
300 static
301 int lttng_tracepoint_list_open(struct inode *inode, struct file *file)
302 {
303 return seq_open(file, &lttng_tracepoint_list_seq_ops);
304 }
305
306 const struct file_operations lttng_tracepoint_list_fops = {
307 .owner = THIS_MODULE,
308 .open = lttng_tracepoint_list_open,
309 .read = seq_read,
310 .llseek = seq_lseek,
311 .release = seq_release,
312 };
This page took 0.036087 seconds and 4 git commands to generate.