Commit | Line | Data |
---|---|---|
ce5aef0b | 1 | /* |
7dd08bec | 2 | * lttng-probes.c |
ce5aef0b | 3 | * |
ce5aef0b MD |
4 | * Holds LTTng probes registry. |
5 | * | |
e92f3e28 MD |
6 | * Copyright 2010-2012 (c) - 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 | |
ce5aef0b MD |
21 | */ |
22 | ||
8d8a24c8 MD |
23 | #include <string.h> |
24 | #include <errno.h> | |
25 | #include <urcu/list.h> | |
1f18504e | 26 | #include <urcu/hlist.h> |
4318ae1b | 27 | #include <lttng/ust-events.h> |
902e1379 | 28 | #include <lttng/tracepoint.h> |
d8de1354 | 29 | #include "tracepoint-internal.h" |
a3bb4b27 | 30 | #include <assert.h> |
c8fcf224 | 31 | #include <helper.h> |
48740cab | 32 | #include <ctype.h> |
ce5aef0b | 33 | |
7dd08bec | 34 | #include "lttng-tracer-core.h" |
1f18504e MD |
35 | #include "jhash.h" |
36 | #include "error.h" | |
8165c8da MD |
37 | |
38 | /* | |
17dfb34b | 39 | * probe list is protected by ust_lock()/ust_unlock(). |
8165c8da | 40 | */ |
ac69b35b | 41 | static CDS_LIST_HEAD(_probe_list); |
e58095ef | 42 | |
6715d7d1 MD |
43 | /* |
44 | * List of probes registered by not yet processed. | |
45 | */ | |
46 | static CDS_LIST_HEAD(lazy_probe_init); | |
df854e41 | 47 | |
6715d7d1 MD |
48 | /* |
49 | * lazy_nesting counter ensures we don't trigger lazy probe registration | |
50 | * fixup while we are performing the fixup. It is protected by the ust | |
51 | * mutex. | |
52 | */ | |
53 | static int lazy_nesting; | |
df854e41 | 54 | |
6715d7d1 MD |
55 | /* |
56 | * Called under ust lock. | |
57 | */ | |
ce5aef0b | 58 | static |
48205d60 | 59 | int check_event_provider(struct lttng_probe_desc *desc) |
ce5aef0b | 60 | { |
ce5aef0b | 61 | int i; |
48205d60 | 62 | size_t provider_name_len; |
ce5aef0b | 63 | |
48205d60 MD |
64 | provider_name_len = strnlen(desc->provider, |
65 | LTTNG_UST_SYM_NAME_LEN - 1); | |
66 | for (i = 0; i < desc->nr_events; i++) { | |
67 | if (strncmp(desc->event_desc[i]->name, | |
68 | desc->provider, | |
69 | provider_name_len)) | |
70 | return 0; /* provider mismatch */ | |
ce5aef0b | 71 | } |
48205d60 | 72 | return 1; |
ce5aef0b MD |
73 | } |
74 | ||
6715d7d1 MD |
75 | /* |
76 | * Called under ust lock. | |
77 | */ | |
78 | static | |
79 | void lttng_lazy_probe_register(struct lttng_probe_desc *desc) | |
ce5aef0b | 80 | { |
df854e41 | 81 | struct lttng_probe_desc *iter; |
ac69b35b | 82 | struct cds_list_head *probe_list; |
48205d60 | 83 | |
ce5aef0b | 84 | /* |
48205d60 MD |
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 | assert(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. | |
ce5aef0b | 96 | */ |
df854e41 MD |
97 | |
98 | /* | |
99 | * We sort the providers by struct lttng_probe_desc pointer | |
100 | * address. | |
101 | */ | |
6715d7d1 | 102 | probe_list = &_probe_list; |
ac69b35b | 103 | cds_list_for_each_entry_reverse(iter, probe_list, head) { |
df854e41 MD |
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 | cds_list_add(&desc->head, &iter->head); | |
108 | goto desc_added; | |
109 | } | |
110 | } | |
111 | /* We should be added at the head of the list */ | |
ac69b35b | 112 | cds_list_add(&desc->head, probe_list); |
df854e41 | 113 | desc_added: |
e81a53af MD |
114 | DBG("just registered probe %s containing %u events", |
115 | desc->provider, desc->nr_events); | |
6715d7d1 MD |
116 | } |
117 | ||
118 | /* | |
119 | * Called under ust lock. | |
120 | */ | |
121 | static | |
122 | void fixup_lazy_probes(void) | |
123 | { | |
124 | struct lttng_probe_desc *iter, *tmp; | |
5f733922 | 125 | int ret; |
6715d7d1 MD |
126 | |
127 | lazy_nesting++; | |
128 | cds_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 | cds_list_del(&iter->lazy_init_head); | |
133 | } | |
5f733922 MD |
134 | ret = lttng_fix_pending_events(); |
135 | assert(!ret); | |
6715d7d1 MD |
136 | lazy_nesting--; |
137 | } | |
138 | ||
139 | /* | |
140 | * Called under ust lock. | |
141 | */ | |
142 | struct cds_list_head *lttng_get_probe_list_head(void) | |
143 | { | |
144 | if (!lazy_nesting && !cds_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 cds_list_head *probe_list; | |
154 | ||
155 | probe_list = lttng_get_probe_list_head(); | |
156 | cds_list_for_each_entry(iter, probe_list, head) { | |
157 | if (!strcmp(iter->provider, provider)) | |
158 | return iter; | |
159 | } | |
160 | return NULL; | |
161 | } | |
162 | ||
71d31690 MD |
163 | static |
164 | int check_provider_version(struct lttng_probe_desc *desc) | |
165 | { | |
166 | /* | |
167 | * Check tracepoint provider version compatibility. | |
168 | */ | |
169 | if (desc->major <= LTTNG_UST_PROVIDER_MAJOR) { | |
170 | DBG("Provider \"%s\" accepted, version %u.%u is compatible " | |
171 | "with LTTng UST provider version %u.%u.", | |
172 | desc->provider, desc->major, desc->minor, | |
173 | LTTNG_UST_PROVIDER_MAJOR, | |
174 | LTTNG_UST_PROVIDER_MINOR); | |
175 | if (desc->major < LTTNG_UST_PROVIDER_MAJOR) { | |
176 | DBG("However, some LTTng UST features might not be " | |
177 | "available for this provider unless it is " | |
178 | "recompiled against a more recent LTTng UST."); | |
179 | } | |
180 | return 1; /* accept */ | |
181 | } else { | |
182 | ERR("Provider \"%s\" rejected, version %u.%u is incompatible " | |
183 | "with LTTng UST provider version %u.%u. Please upgrade " | |
184 | "LTTng UST.", | |
185 | desc->provider, desc->major, desc->minor, | |
186 | LTTNG_UST_PROVIDER_MAJOR, | |
187 | LTTNG_UST_PROVIDER_MINOR); | |
188 | return 0; /* reject */ | |
189 | } | |
190 | } | |
191 | ||
192 | ||
6715d7d1 MD |
193 | int lttng_probe_register(struct lttng_probe_desc *desc) |
194 | { | |
195 | int ret = 0; | |
196 | ||
71d31690 MD |
197 | /* |
198 | * If version mismatch, don't register, but don't trigger assert | |
199 | * on caller. The version check just prints an error. | |
200 | */ | |
201 | if (!check_provider_version(desc)) | |
202 | return 0; | |
203 | ||
6715d7d1 MD |
204 | ust_lock(); |
205 | ||
206 | /* | |
207 | * Check if the provider has already been registered. | |
208 | */ | |
209 | if (find_provider(desc->provider)) { | |
210 | ret = -EEXIST; | |
211 | goto end; | |
212 | } | |
213 | cds_list_add(&desc->lazy_init_head, &lazy_probe_init); | |
214 | desc->lazy = 1; | |
215 | DBG("adding probe %s containing %u events to lazy registration list", | |
216 | desc->provider, desc->nr_events); | |
217 | /* | |
218 | * If there is at least one active session, we need to register | |
219 | * the probe immediately, since we cannot delay event | |
220 | * registration because they are needed ASAP. | |
221 | */ | |
222 | if (lttng_session_active()) | |
223 | fixup_lazy_probes(); | |
ce5aef0b | 224 | end: |
17dfb34b | 225 | ust_unlock(); |
ce5aef0b MD |
226 | return ret; |
227 | } | |
ce5aef0b | 228 | |
7dd08bec MD |
229 | /* Backward compatibility with UST 2.0 */ |
230 | int ltt_probe_register(struct lttng_probe_desc *desc) | |
231 | { | |
232 | return lttng_probe_register(desc); | |
233 | } | |
234 | ||
235 | void lttng_probe_unregister(struct lttng_probe_desc *desc) | |
ce5aef0b | 236 | { |
71d31690 MD |
237 | if (!check_provider_version(desc)) |
238 | return; | |
239 | ||
17dfb34b | 240 | ust_lock(); |
6715d7d1 MD |
241 | if (!desc->lazy) |
242 | cds_list_del(&desc->head); | |
243 | else | |
244 | cds_list_del(&desc->lazy_init_head); | |
e81a53af | 245 | DBG("just unregistered probe %s", desc->provider); |
17dfb34b | 246 | ust_unlock(); |
ce5aef0b | 247 | } |
ce5aef0b | 248 | |
7dd08bec MD |
249 | /* Backward compatibility with UST 2.0 */ |
250 | void ltt_probe_unregister(struct lttng_probe_desc *desc) | |
251 | { | |
252 | lttng_probe_unregister(desc); | |
253 | } | |
254 | ||
7dd08bec | 255 | void lttng_probes_prune_event_list(struct lttng_ust_tracepoint_list *list) |
c8fcf224 MD |
256 | { |
257 | struct tp_list_entry *list_entry, *tmp; | |
258 | ||
259 | cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) { | |
260 | cds_list_del(&list_entry->head); | |
261 | free(list_entry); | |
262 | } | |
263 | } | |
264 | ||
265 | /* | |
266 | * called with UST lock held. | |
267 | */ | |
7dd08bec | 268 | int lttng_probes_get_event_list(struct lttng_ust_tracepoint_list *list) |
c8fcf224 MD |
269 | { |
270 | struct lttng_probe_desc *probe_desc; | |
271 | int i; | |
ac69b35b | 272 | struct cds_list_head *probe_list; |
c8fcf224 | 273 | |
ac69b35b | 274 | probe_list = lttng_get_probe_list_head(); |
c8fcf224 | 275 | CDS_INIT_LIST_HEAD(&list->head); |
ac69b35b | 276 | cds_list_for_each_entry(probe_desc, probe_list, head) { |
c8fcf224 MD |
277 | for (i = 0; i < probe_desc->nr_events; i++) { |
278 | struct tp_list_entry *list_entry; | |
279 | ||
280 | list_entry = zmalloc(sizeof(*list_entry)); | |
281 | if (!list_entry) | |
282 | goto err_nomem; | |
283 | cds_list_add(&list_entry->head, &list->head); | |
284 | strncpy(list_entry->tp.name, | |
285 | probe_desc->event_desc[i]->name, | |
286 | LTTNG_UST_SYM_NAME_LEN); | |
287 | list_entry->tp.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; | |
288 | if (!probe_desc->event_desc[i]->loglevel) { | |
882a56d7 | 289 | list_entry->tp.loglevel = TRACE_DEFAULT; |
c8fcf224 | 290 | } else { |
882a56d7 | 291 | list_entry->tp.loglevel = *(*probe_desc->event_desc[i]->loglevel); |
c8fcf224 MD |
292 | } |
293 | } | |
294 | } | |
295 | if (cds_list_empty(&list->head)) | |
296 | list->iter = NULL; | |
297 | else | |
298 | list->iter = | |
299 | cds_list_first_entry(&list->head, struct tp_list_entry, head); | |
300 | return 0; | |
301 | ||
302 | err_nomem: | |
7dd08bec | 303 | lttng_probes_prune_event_list(list); |
c8fcf224 MD |
304 | return -ENOMEM; |
305 | } | |
306 | ||
307 | /* | |
308 | * Return current iteration position, advance internal iterator to next. | |
309 | * Return NULL if end of list. | |
310 | */ | |
311 | struct lttng_ust_tracepoint_iter * | |
312 | lttng_ust_tracepoint_list_get_iter_next(struct lttng_ust_tracepoint_list *list) | |
313 | { | |
314 | struct tp_list_entry *entry; | |
315 | ||
316 | if (!list->iter) | |
317 | return NULL; | |
318 | entry = list->iter; | |
319 | if (entry->head.next == &list->head) | |
320 | list->iter = NULL; | |
321 | else | |
322 | list->iter = cds_list_entry(entry->head.next, | |
323 | struct tp_list_entry, head); | |
324 | return &entry->tp; | |
325 | } | |
1f18504e | 326 | |
7dd08bec | 327 | void lttng_probes_prune_field_list(struct lttng_ust_field_list *list) |
06d4f27e MD |
328 | { |
329 | struct tp_field_list_entry *list_entry, *tmp; | |
330 | ||
331 | cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) { | |
332 | cds_list_del(&list_entry->head); | |
333 | free(list_entry); | |
334 | } | |
335 | } | |
336 | ||
337 | /* | |
338 | * called with UST lock held. | |
339 | */ | |
7dd08bec | 340 | int lttng_probes_get_field_list(struct lttng_ust_field_list *list) |
06d4f27e MD |
341 | { |
342 | struct lttng_probe_desc *probe_desc; | |
343 | int i; | |
ac69b35b | 344 | struct cds_list_head *probe_list; |
06d4f27e | 345 | |
ac69b35b | 346 | probe_list = lttng_get_probe_list_head(); |
06d4f27e | 347 | CDS_INIT_LIST_HEAD(&list->head); |
ac69b35b | 348 | cds_list_for_each_entry(probe_desc, probe_list, head) { |
06d4f27e MD |
349 | for (i = 0; i < probe_desc->nr_events; i++) { |
350 | const struct lttng_event_desc *event_desc = | |
351 | probe_desc->event_desc[i]; | |
352 | int j; | |
353 | ||
26329f26 MD |
354 | if (event_desc->nr_fields == 0) { |
355 | /* Events without fields. */ | |
356 | struct tp_field_list_entry *list_entry; | |
357 | ||
358 | list_entry = zmalloc(sizeof(*list_entry)); | |
359 | if (!list_entry) | |
360 | goto err_nomem; | |
361 | cds_list_add(&list_entry->head, &list->head); | |
362 | strncpy(list_entry->field.event_name, | |
363 | event_desc->name, | |
364 | LTTNG_UST_SYM_NAME_LEN); | |
365 | list_entry->field.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; | |
366 | list_entry->field.field_name[0] = '\0'; | |
367 | list_entry->field.type = LTTNG_UST_FIELD_OTHER; | |
368 | if (!event_desc->loglevel) { | |
369 | list_entry->field.loglevel = TRACE_DEFAULT; | |
370 | } else { | |
371 | list_entry->field.loglevel = *(*event_desc->loglevel); | |
372 | } | |
373 | list_entry->field.nowrite = 1; | |
374 | } | |
375 | ||
06d4f27e MD |
376 | for (j = 0; j < event_desc->nr_fields; j++) { |
377 | const struct lttng_event_field *event_field = | |
378 | &event_desc->fields[j]; | |
379 | struct tp_field_list_entry *list_entry; | |
380 | ||
381 | list_entry = zmalloc(sizeof(*list_entry)); | |
382 | if (!list_entry) | |
383 | goto err_nomem; | |
384 | cds_list_add(&list_entry->head, &list->head); | |
385 | strncpy(list_entry->field.event_name, | |
386 | event_desc->name, | |
387 | LTTNG_UST_SYM_NAME_LEN); | |
388 | list_entry->field.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; | |
389 | strncpy(list_entry->field.field_name, | |
390 | event_field->name, | |
391 | LTTNG_UST_SYM_NAME_LEN); | |
392 | list_entry->field.field_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; | |
40003310 MD |
393 | switch (event_field->type.atype) { |
394 | case atype_integer: | |
395 | list_entry->field.type = LTTNG_UST_FIELD_INTEGER; | |
396 | break; | |
397 | case atype_string: | |
398 | list_entry->field.type = LTTNG_UST_FIELD_STRING; | |
399 | break; | |
400 | case atype_array: | |
401 | if (event_field->type.u.array.elem_type.atype != atype_integer | |
402 | || event_field->type.u.array.elem_type.u.basic.integer.encoding == lttng_encode_none) | |
403 | list_entry->field.type = LTTNG_UST_FIELD_OTHER; | |
404 | else | |
405 | list_entry->field.type = LTTNG_UST_FIELD_STRING; | |
406 | break; | |
407 | case atype_sequence: | |
408 | if (event_field->type.u.sequence.elem_type.atype != atype_integer | |
409 | || event_field->type.u.sequence.elem_type.u.basic.integer.encoding == lttng_encode_none) | |
410 | list_entry->field.type = LTTNG_UST_FIELD_OTHER; | |
411 | else | |
412 | list_entry->field.type = LTTNG_UST_FIELD_STRING; | |
413 | break; | |
414 | case atype_float: | |
415 | list_entry->field.type = LTTNG_UST_FIELD_FLOAT; | |
416 | break; | |
417 | case atype_enum: | |
418 | list_entry->field.type = LTTNG_UST_FIELD_ENUM; | |
419 | break; | |
420 | default: | |
421 | list_entry->field.type = LTTNG_UST_FIELD_OTHER; | |
422 | } | |
06d4f27e MD |
423 | if (!event_desc->loglevel) { |
424 | list_entry->field.loglevel = TRACE_DEFAULT; | |
425 | } else { | |
426 | list_entry->field.loglevel = *(*event_desc->loglevel); | |
427 | } | |
180901e6 | 428 | list_entry->field.nowrite = event_field->nowrite; |
06d4f27e MD |
429 | } |
430 | } | |
431 | } | |
432 | if (cds_list_empty(&list->head)) | |
433 | list->iter = NULL; | |
434 | else | |
435 | list->iter = | |
436 | cds_list_first_entry(&list->head, | |
437 | struct tp_field_list_entry, head); | |
438 | return 0; | |
439 | ||
440 | err_nomem: | |
7dd08bec | 441 | lttng_probes_prune_field_list(list); |
06d4f27e MD |
442 | return -ENOMEM; |
443 | } | |
444 | ||
445 | /* | |
446 | * Return current iteration position, advance internal iterator to next. | |
447 | * Return NULL if end of list. | |
448 | */ | |
449 | struct lttng_ust_field_iter * | |
450 | lttng_ust_field_list_get_iter_next(struct lttng_ust_field_list *list) | |
451 | { | |
452 | struct tp_field_list_entry *entry; | |
453 | ||
454 | if (!list->iter) | |
455 | return NULL; | |
456 | entry = list->iter; | |
457 | if (entry->head.next == &list->head) | |
458 | list->iter = NULL; | |
459 | else | |
460 | list->iter = cds_list_entry(entry->head.next, | |
461 | struct tp_field_list_entry, head); | |
462 | return &entry->field; | |
463 | } |