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