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 */ | |
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; | |
85a9ca7f | 101 | } |
02119ee5 | 102 | } |
3c997079 MD |
103 | /* We should be added at the head of the list */ |
104 | list_add(&desc->head, probe_list); | |
105 | desc_added: | |
33a39a3c | 106 | pr_debug("LTTng: just registered probe %s containing %u events\n", |
3c997079 MD |
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 | } | |
02119ee5 MD |
152 | return NULL; |
153 | } | |
154 | ||
a90917c3 | 155 | int lttng_probe_register(struct lttng_probe_desc *desc) |
02119ee5 | 156 | { |
02119ee5 | 157 | int ret = 0; |
02119ee5 | 158 | |
3c997079 MD |
159 | lttng_lock_sessions(); |
160 | ||
85a9ca7f | 161 | /* |
3c997079 | 162 | * Check if the provider has already been registered. |
85a9ca7f | 163 | */ |
3c997079 MD |
164 | if (find_provider(desc->provider)) { |
165 | ret = -EEXIST; | |
166 | goto end; | |
02119ee5 | 167 | } |
3c997079 MD |
168 | list_add(&desc->lazy_init_head, &lazy_probe_init); |
169 | desc->lazy = 1; | |
33a39a3c | 170 | pr_debug("LTTng: adding probe %s containing %u events to lazy registration list\n", |
3c997079 MD |
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(); | |
02119ee5 | 179 | end: |
3c997079 | 180 | lttng_unlock_sessions(); |
02119ee5 MD |
181 | return ret; |
182 | } | |
a90917c3 | 183 | EXPORT_SYMBOL_GPL(lttng_probe_register); |
02119ee5 | 184 | |
a90917c3 | 185 | void lttng_probe_unregister(struct lttng_probe_desc *desc) |
02119ee5 | 186 | { |
3c997079 MD |
187 | lttng_lock_sessions(); |
188 | if (!desc->lazy) | |
189 | list_del(&desc->head); | |
190 | else | |
191 | list_del(&desc->lazy_init_head); | |
33a39a3c | 192 | pr_debug("LTTng: just unregistered probe %s\n", desc->provider); |
3c997079 | 193 | lttng_unlock_sessions(); |
02119ee5 | 194 | } |
a90917c3 | 195 | EXPORT_SYMBOL_GPL(lttng_probe_unregister); |
02119ee5 | 196 | |
3c997079 MD |
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 | */ | |
a90917c3 | 219 | const struct lttng_event_desc *lttng_event_get(const char *name) |
02119ee5 | 220 | { |
85a9ca7f | 221 | const struct lttng_event_desc *event; |
02119ee5 MD |
222 | int ret; |
223 | ||
85a9ca7f MD |
224 | event = find_event(name); |
225 | if (!event) | |
c099397a | 226 | return NULL; |
dc7f600a | 227 | ret = try_module_get(event->owner); |
02119ee5 | 228 | WARN_ON_ONCE(!ret); |
85a9ca7f | 229 | return event; |
02119ee5 | 230 | } |
a90917c3 | 231 | EXPORT_SYMBOL_GPL(lttng_event_get); |
02119ee5 | 232 | |
3c997079 MD |
233 | /* |
234 | * Called with sessions lock held. | |
235 | */ | |
a90917c3 | 236 | void lttng_event_put(const struct lttng_event_desc *event) |
02119ee5 | 237 | { |
dc7f600a | 238 | module_put(event->owner); |
02119ee5 | 239 | } |
a90917c3 | 240 | EXPORT_SYMBOL_GPL(lttng_event_put); |
271b6681 MD |
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 | ||
3c997079 MD |
248 | lttng_lock_sessions(); |
249 | list_for_each_entry(probe_desc, &_probe_list, head) { | |
271b6681 MD |
250 | for (i = 0; i < probe_desc->nr_events; i++) { |
251 | if (iter++ >= *pos) | |
f7bdf4db | 252 | return (void *) probe_desc->event_desc[i]; |
271b6681 MD |
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)++; | |
3c997079 | 266 | list_for_each_entry(probe_desc, &_probe_list, head) { |
271b6681 MD |
267 | for (i = 0; i < probe_desc->nr_events; i++) { |
268 | if (iter++ >= *ppos) | |
f7bdf4db | 269 | return (void *) probe_desc->event_desc[i]; |
271b6681 MD |
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 | { | |
3c997079 | 279 | lttng_unlock_sessions(); |
271b6681 MD |
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, <tng_tracepoint_list_seq_ops); | |
304 | } | |
305 | ||
306 | const struct file_operations lttng_tracepoint_list_fops = { | |
2e54c205 | 307 | .owner = THIS_MODULE, |
271b6681 MD |
308 | .open = lttng_tracepoint_list_open, |
309 | .read = seq_read, | |
310 | .llseek = seq_lseek, | |
a51729c7 | 311 | .release = seq_release, |
271b6681 | 312 | }; |