Commit | Line | Data |
---|---|---|
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 | |
a90917c3 | 28 | #include "lttng-events.h" |
02119ee5 | 29 | |
3c997079 MD |
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); | |
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 | */ | |
45 | static int lazy_nesting; | |
46 | ||
47 | /* | |
48 | * Called under sessions lock. | |
49 | */ | |
85a9ca7f | 50 | static |
3c997079 | 51 | int 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 | */ | |
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; | |
85a9ca7f | 109 | } |
02119ee5 | 110 | } |
3c997079 MD |
111 | /* We should be added at the head of the list */ |
112 | list_add(&desc->head, probe_list); | |
113 | desc_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 | */ | |
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 | } | |
02119ee5 MD |
160 | return NULL; |
161 | } | |
162 | ||
a90917c3 | 163 | int 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 | 187 | end: |
3c997079 | 188 | lttng_unlock_sessions(); |
02119ee5 MD |
189 | return ret; |
190 | } | |
a90917c3 | 191 | EXPORT_SYMBOL_GPL(lttng_probe_register); |
02119ee5 | 192 | |
a90917c3 | 193 | void 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 | 203 | EXPORT_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 | */ | |
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 | */ | |
a90917c3 | 227 | const 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 | 239 | EXPORT_SYMBOL_GPL(lttng_event_get); |
02119ee5 | 240 | |
3c997079 MD |
241 | /* |
242 | * Called with sessions lock held. | |
243 | */ | |
a90917c3 | 244 | void lttng_event_put(const struct lttng_event_desc *event) |
02119ee5 | 245 | { |
dc7f600a | 246 | module_put(event->owner); |
02119ee5 | 247 | } |
a90917c3 | 248 | EXPORT_SYMBOL_GPL(lttng_event_put); |
271b6681 MD |
249 | |
250 | static | |
251 | void *tp_list_start(struct seq_file *m, loff_t *pos) | |
252 | { | |
253 | struct lttng_probe_desc *probe_desc; | |
254 | int iter = 0, i; | |
255 | ||
3c997079 MD |
256 | lttng_lock_sessions(); |
257 | list_for_each_entry(probe_desc, &_probe_list, head) { | |
271b6681 MD |
258 | for (i = 0; i < probe_desc->nr_events; i++) { |
259 | if (iter++ >= *pos) | |
f7bdf4db | 260 | return (void *) probe_desc->event_desc[i]; |
271b6681 MD |
261 | } |
262 | } | |
263 | /* End of list */ | |
264 | return NULL; | |
265 | } | |
266 | ||
267 | static | |
268 | void *tp_list_next(struct seq_file *m, void *p, loff_t *ppos) | |
269 | { | |
270 | struct lttng_probe_desc *probe_desc; | |
271 | int iter = 0, i; | |
272 | ||
273 | (*ppos)++; | |
3c997079 | 274 | list_for_each_entry(probe_desc, &_probe_list, head) { |
271b6681 MD |
275 | for (i = 0; i < probe_desc->nr_events; i++) { |
276 | if (iter++ >= *ppos) | |
f7bdf4db | 277 | return (void *) probe_desc->event_desc[i]; |
271b6681 MD |
278 | } |
279 | } | |
280 | /* End of list */ | |
281 | return NULL; | |
282 | } | |
283 | ||
284 | static | |
285 | void tp_list_stop(struct seq_file *m, void *p) | |
286 | { | |
3c997079 | 287 | lttng_unlock_sessions(); |
271b6681 MD |
288 | } |
289 | ||
290 | static | |
291 | int tp_list_show(struct seq_file *m, void *p) | |
292 | { | |
293 | const struct lttng_event_desc *probe_desc = p; | |
294 | ||
295 | seq_printf(m, "event { name = %s; };\n", | |
296 | probe_desc->name); | |
297 | return 0; | |
298 | } | |
299 | ||
300 | static | |
301 | const struct seq_operations lttng_tracepoint_list_seq_ops = { | |
302 | .start = tp_list_start, | |
303 | .next = tp_list_next, | |
304 | .stop = tp_list_stop, | |
305 | .show = tp_list_show, | |
306 | }; | |
307 | ||
308 | static | |
309 | int lttng_tracepoint_list_open(struct inode *inode, struct file *file) | |
310 | { | |
311 | return seq_open(file, <tng_tracepoint_list_seq_ops); | |
312 | } | |
313 | ||
314 | const struct file_operations lttng_tracepoint_list_fops = { | |
2e54c205 | 315 | .owner = THIS_MODULE, |
271b6681 MD |
316 | .open = lttng_tracepoint_list_open, |
317 | .read = seq_read, | |
318 | .llseek = seq_lseek, | |
a51729c7 | 319 | .release = seq_release, |
271b6681 | 320 | }; |