Update loglevel selection ABI
[lttng-ust.git] / liblttng-ust / ltt-probes.c
1 /*
2 * ltt-probes.c
3 *
4 * Copyright 2010 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * Holds LTTng probes registry.
7 *
8 * Dual LGPL v2.1/GPL v2 license.
9 */
10
11 #include <string.h>
12 #include <errno.h>
13 #include <urcu/list.h>
14 #include <urcu/hlist.h>
15 #include <lttng/ust-events.h>
16 #include <assert.h>
17 #include <helper.h>
18 #include <ctype.h>
19
20 #include "ltt-tracer-core.h"
21 #include "jhash.h"
22 #include "error.h"
23
24 /*
25 * probe list is protected by ust_lock()/ust_unlock().
26 */
27 static CDS_LIST_HEAD(probe_list);
28
29 /*
30 * Loglevel hash table, containing the active loglevels.
31 * Protected by ust lock.
32 */
33 #define LOGLEVEL_HASH_BITS 6
34 #define LOGLEVEL_TABLE_SIZE (1 << LOGLEVEL_HASH_BITS)
35 static struct cds_hlist_head loglevel_table[LOGLEVEL_TABLE_SIZE];
36
37 /*
38 * Wildcard list, containing the active wildcards.
39 * Protected by ust lock.
40 */
41 static CDS_LIST_HEAD(wildcard_list);
42
43 static
44 const struct lttng_probe_desc *find_provider(const char *provider)
45 {
46 struct lttng_probe_desc *iter;
47
48 cds_list_for_each_entry(iter, &probe_list, head) {
49 if (!strcmp(iter->provider, provider))
50 return iter;
51 }
52 return NULL;
53 }
54
55 static
56 const struct lttng_event_desc *find_event(const char *name)
57 {
58 struct lttng_probe_desc *probe_desc;
59 int i;
60
61 cds_list_for_each_entry(probe_desc, &probe_list, head) {
62 for (i = 0; i < probe_desc->nr_events; i++) {
63 if (!strncmp(probe_desc->event_desc[i]->name, name,
64 LTTNG_UST_SYM_NAME_LEN - 1))
65 return probe_desc->event_desc[i];
66 }
67 }
68 return NULL;
69 }
70
71 int ltt_probe_register(struct lttng_probe_desc *desc)
72 {
73 struct lttng_probe_desc *iter;
74 int ret = 0;
75 int i;
76
77 ust_lock();
78 if (find_provider(desc->provider)) {
79 ret = -EEXIST;
80 goto end;
81 }
82 /*
83 * TODO: This is O(N^2). Turn into a hash table when probe registration
84 * overhead becomes an issue.
85 */
86 for (i = 0; i < desc->nr_events; i++) {
87 if (find_event(desc->event_desc[i]->name)) {
88 ret = -EEXIST;
89 goto end;
90 }
91 }
92
93 /*
94 * We sort the providers by struct lttng_probe_desc pointer
95 * address.
96 */
97 cds_list_for_each_entry_reverse(iter, &probe_list, head) {
98 BUG_ON(iter == desc); /* Should never be in the list twice */
99 if (iter < desc) {
100 /* We belong to the location right after iter. */
101 cds_list_add(&desc->head, &iter->head);
102 goto desc_added;
103 }
104 }
105 /* We should be added at the head of the list */
106 cds_list_add(&desc->head, &probe_list);
107 desc_added:
108 DBG("just registered probe %s containing %u events",
109 desc->provider, desc->nr_events);
110 /*
111 * fix the events awaiting probe load.
112 */
113 for (i = 0; i < desc->nr_events; i++) {
114 ret = pending_probe_fix_events(desc->event_desc[i]);
115 assert(!ret);
116 }
117 end:
118 ust_unlock();
119 return ret;
120 }
121
122 void ltt_probe_unregister(struct lttng_probe_desc *desc)
123 {
124 ust_lock();
125 cds_list_del(&desc->head);
126 DBG("just unregistered probe %s", desc->provider);
127 ust_unlock();
128 }
129
130 /*
131 * called with UST lock held.
132 */
133 const struct lttng_event_desc *ltt_event_get(const char *name)
134 {
135 const struct lttng_event_desc *event;
136
137 event = find_event(name);
138 if (!event)
139 return NULL;
140 return event;
141 }
142
143 void ltt_event_put(const struct lttng_event_desc *event)
144 {
145 }
146
147 void ltt_probes_prune_event_list(struct lttng_ust_tracepoint_list *list)
148 {
149 struct tp_list_entry *list_entry, *tmp;
150
151 cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) {
152 cds_list_del(&list_entry->head);
153 free(list_entry);
154 }
155 }
156
157 /*
158 * called with UST lock held.
159 */
160 int ltt_probes_get_event_list(struct lttng_ust_tracepoint_list *list)
161 {
162 struct lttng_probe_desc *probe_desc;
163 int i;
164
165 CDS_INIT_LIST_HEAD(&list->head);
166 cds_list_for_each_entry(probe_desc, &probe_list, head) {
167 for (i = 0; i < probe_desc->nr_events; i++) {
168 struct tp_list_entry *list_entry;
169
170 list_entry = zmalloc(sizeof(*list_entry));
171 if (!list_entry)
172 goto err_nomem;
173 cds_list_add(&list_entry->head, &list->head);
174 strncpy(list_entry->tp.name,
175 probe_desc->event_desc[i]->name,
176 LTTNG_UST_SYM_NAME_LEN);
177 list_entry->tp.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
178 if (!probe_desc->event_desc[i]->loglevel) {
179 list_entry->tp.loglevel[0] = '\0';
180 list_entry->tp.loglevel_value = 0;
181 } else {
182 strncpy(list_entry->tp.loglevel,
183 (*probe_desc->event_desc[i]->loglevel)->identifier,
184 LTTNG_UST_SYM_NAME_LEN);
185 list_entry->tp.loglevel[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
186 list_entry->tp.loglevel_value =
187 (*probe_desc->event_desc[i]->loglevel)->value;
188 }
189 }
190 }
191 if (cds_list_empty(&list->head))
192 list->iter = NULL;
193 else
194 list->iter =
195 cds_list_first_entry(&list->head, struct tp_list_entry, head);
196 return 0;
197
198 err_nomem:
199 ltt_probes_prune_event_list(list);
200 return -ENOMEM;
201 }
202
203 /*
204 * Return current iteration position, advance internal iterator to next.
205 * Return NULL if end of list.
206 */
207 struct lttng_ust_tracepoint_iter *
208 lttng_ust_tracepoint_list_get_iter_next(struct lttng_ust_tracepoint_list *list)
209 {
210 struct tp_list_entry *entry;
211
212 if (!list->iter)
213 return NULL;
214 entry = list->iter;
215 if (entry->head.next == &list->head)
216 list->iter = NULL;
217 else
218 list->iter = cds_list_entry(entry->head.next,
219 struct tp_list_entry, head);
220 return &entry->tp;
221 }
222
223 /*
224 * Get loglevel if the loglevel is present in the loglevel hash table.
225 * Must be called with ust lock held.
226 * Returns NULL if not present.
227 */
228 struct loglevel_entry *get_loglevel(const char *name)
229 {
230 struct cds_hlist_head *head;
231 struct cds_hlist_node *node;
232 struct loglevel_entry *e;
233 size_t name_len = strlen(name);
234 uint32_t hash;
235
236 if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) {
237 WARN("Truncating loglevel name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1);
238 name_len = LTTNG_UST_SYM_NAME_LEN - 1;
239 }
240 hash = jhash(name, name_len, 0);
241 head = &loglevel_table[hash & (LOGLEVEL_TABLE_SIZE - 1)];
242 cds_hlist_for_each_entry(e, node, head, hlist) {
243 if (!strncmp(name, e->name, LTTNG_UST_SYM_NAME_LEN - 1))
244 return e;
245 }
246 return NULL;
247 }
248
249 struct loglevel_entry *get_loglevel_value(int64_t value)
250 {
251 char name[LTTNG_UST_SYM_NAME_LEN];
252 int ret;
253
254 ret = snprintf(name, LTTNG_UST_SYM_NAME_LEN, "%lld", (long long) value);
255 if (ret < 0)
256 return NULL;
257 return get_loglevel(name);
258 }
259
260 /*
261 * marshall all probes/all events and create those that fit the
262 * loglevel. Add them to the events list as created.
263 */
264 static
265 void _probes_create_loglevel_events(struct loglevel_entry *entry,
266 struct session_loglevel *loglevel)
267 {
268 struct lttng_probe_desc *probe_desc;
269 struct lttng_ust_event event_param;
270 int i;
271
272 cds_list_for_each_entry(probe_desc, &probe_list, head) {
273 for (i = 0; i < probe_desc->nr_events; i++) {
274 const struct tracepoint_loglevel_entry *ev_ll;
275 const struct lttng_event_desc *event_desc;
276 int match = 0;
277
278 event_desc = probe_desc->event_desc[i];
279 if (!(event_desc->loglevel))
280 continue;
281 ev_ll = *event_desc->loglevel;
282 if (isdigit(entry->name[0])) {
283 if (atoll(entry->name) == ev_ll->value) {
284 match = 1;
285 }
286 } else if (!strncmp(ev_ll->identifier, entry->name,
287 LTTNG_UST_SYM_NAME_LEN - 1)) {
288 match = 1;
289 }
290
291 if (match) {
292 struct ltt_event *ev;
293 int ret;
294
295 memcpy(&event_param, &loglevel->event_param,
296 sizeof(event_param));
297 memcpy(event_param.name,
298 event_desc->name,
299 sizeof(event_param.name));
300 /* create event */
301 ret = ltt_event_create(loglevel->chan,
302 &event_param, NULL,
303 &ev);
304 if (ret) {
305 DBG("Error creating event");
306 continue;
307 }
308 cds_list_add(&ev->loglevel_list,
309 &loglevel->events);
310 }
311 }
312 }
313 }
314
315 /*
316 * Add the loglevel to the loglevel hash table. Must be called with
317 * ust lock held.
318 * TODO: should be integrated with events and wildcards.
319 */
320 struct session_loglevel *add_loglevel(const char *name,
321 struct ltt_channel *chan,
322 struct lttng_ust_event *event_param)
323 {
324 struct cds_hlist_head *head;
325 struct cds_hlist_node *node;
326 struct loglevel_entry *e;
327 struct session_loglevel *sl;
328 int found = 0;
329 size_t name_len = strlen(name);
330 uint32_t hash;
331
332 if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) {
333 WARN("Truncating loglevel name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1);
334 name_len = LTTNG_UST_SYM_NAME_LEN - 1;
335 }
336 hash = jhash(name, name_len, 0);
337 /* loglevel entry */
338 head = &loglevel_table[hash & (LOGLEVEL_TABLE_SIZE - 1)];
339 cds_hlist_for_each_entry(e, node, head, hlist) {
340 if (!strncmp(name, e->name, LTTNG_UST_SYM_NAME_LEN - 1)) {
341 found = 1;
342 break;
343 }
344 }
345
346 if (!found) {
347 /*
348 * Using zmalloc here to allocate a variable length element. Could
349 * cause some memory fragmentation if overused.
350 */
351 e = zmalloc(sizeof(struct loglevel_entry) + name_len + 1);
352 if (!e)
353 return ERR_PTR(-ENOMEM);
354 memcpy(&e->name[0], name, name_len + 1);
355 e->name[name_len] = '\0';
356 cds_hlist_add_head(&e->hlist, head);
357 CDS_INIT_LIST_HEAD(&e->session_list);
358 }
359
360 /* session loglevel */
361 cds_list_for_each_entry(sl, &e->session_list, session_list) {
362 if (chan == sl->chan) {
363 DBG("loglevel %s busy for this channel", name);
364 return ERR_PTR(-EEXIST); /* Already there */
365 }
366 }
367 sl = zmalloc(sizeof(struct session_loglevel));
368 if (!sl)
369 return ERR_PTR(-ENOMEM);
370 sl->chan = chan;
371 sl->enabled = 1;
372 memcpy(&sl->event_param, event_param, sizeof(sl->event_param));
373 sl->event_param.instrumentation = LTTNG_UST_TRACEPOINT;
374 CDS_INIT_LIST_HEAD(&sl->events);
375 cds_list_add(&sl->list, &chan->session->loglevels);
376 cds_list_add(&sl->session_list, &e->session_list);
377 sl->entry = e;
378 _probes_create_loglevel_events(e, sl);
379 return sl;
380 }
381
382 /*
383 * Remove the loglevel from the loglevel hash table. Must be called with
384 * ust_lock held. Only called at session teardown.
385 */
386 void _remove_loglevel(struct session_loglevel *loglevel)
387 {
388 struct ltt_event *ev, *tmp;
389
390 /*
391 * Just remove the events owned (for enable/disable) by this
392 * loglevel from the list. The session teardown will take care
393 * of freeing the event memory.
394 */
395 cds_list_for_each_entry_safe(ev, tmp, &loglevel->events,
396 loglevel_list) {
397 cds_list_del(&ev->loglevel_list);
398 }
399 cds_list_del(&loglevel->session_list);
400 cds_list_del(&loglevel->list);
401 if (cds_list_empty(&loglevel->entry->session_list)) {
402 cds_hlist_del(&loglevel->entry->hlist);
403 free(loglevel->entry);
404 }
405 free(loglevel);
406 }
407
408 int ltt_loglevel_enable(struct session_loglevel *loglevel)
409 {
410 struct ltt_event *ev;
411 int ret;
412
413 if (loglevel->enabled)
414 return -EEXIST;
415 cds_list_for_each_entry(ev, &loglevel->events, loglevel_list) {
416 ret = ltt_event_enable(ev);
417 if (ret) {
418 DBG("Error: enable error.\n");
419 return ret;
420 }
421 }
422 loglevel->enabled = 1;
423 return 0;
424 }
425
426 int ltt_loglevel_disable(struct session_loglevel *loglevel)
427 {
428 struct ltt_event *ev;
429 int ret;
430
431 if (!loglevel->enabled)
432 return -EEXIST;
433 cds_list_for_each_entry(ev, &loglevel->events, loglevel_list) {
434 ret = ltt_event_disable(ev);
435 if (ret) {
436 DBG("Error: disable error.\n");
437 return ret;
438 }
439 }
440 loglevel->enabled = 0;
441 return 0;
442 }
443
444 /* WILDCARDS */
445
446 /*
447 * Return wildcard for a given event name if the event name match the
448 * one of the wildcards.
449 * Must be called with ust lock held.
450 * Returns NULL if not present.
451 */
452 struct wildcard_entry *match_wildcard(const char *name)
453 {
454 struct wildcard_entry *e;
455
456 cds_list_for_each_entry(e, &wildcard_list, list) {
457 /* If only contain '*' */
458 if (strlen(e->name) == 1)
459 return e;
460 /* Compare excluding final '*' */
461 if (!strncmp(name, e->name, strlen(e->name) - 1))
462 return e;
463 }
464 return NULL;
465 }
466
467 /*
468 * marshall all probes/all events and create those that fit the
469 * wildcard. Add them to the events list as created.
470 */
471 static
472 void _probes_create_wildcard_events(struct wildcard_entry *entry,
473 struct session_wildcard *wildcard)
474 {
475 struct lttng_probe_desc *probe_desc;
476 struct lttng_ust_event event_param;
477 int i;
478
479 cds_list_for_each_entry(probe_desc, &probe_list, head) {
480 for (i = 0; i < probe_desc->nr_events; i++) {
481 const struct lttng_event_desc *event_desc;
482 int match = 0;
483
484 event_desc = probe_desc->event_desc[i];
485 /* compare excluding final '*' */
486 assert(strlen(entry->name) > 0);
487 if (strcmp(event_desc->name, "lttng_ust:metadata")
488 && (strlen(entry->name) == 1
489 || !strncmp(event_desc->name, entry->name,
490 strlen(entry->name) - 1))) {
491 match = 1;
492 }
493 if (match) {
494 struct ltt_event *ev;
495 int ret;
496
497 memcpy(&event_param, &wildcard->event_param,
498 sizeof(event_param));
499 memcpy(event_param.name,
500 event_desc->name,
501 sizeof(event_param.name));
502 /* create event */
503 ret = ltt_event_create(wildcard->chan,
504 &event_param, NULL,
505 &ev);
506 if (ret) {
507 DBG("Error creating event");
508 continue;
509 }
510 cds_list_add(&ev->wildcard_list,
511 &wildcard->events);
512 }
513 }
514 }
515 }
516
517 /*
518 * Add the wildcard to the wildcard hash table. Must be called with
519 * ust lock held.
520 */
521 struct session_wildcard *add_wildcard(const char *name,
522 struct ltt_channel *chan,
523 struct lttng_ust_event *event_param)
524 {
525 struct wildcard_entry *e;
526 struct session_wildcard *sw;
527 size_t name_len = strlen(name) + 1;
528 int found = 0;
529
530 /* wildcard entry */
531 cds_list_for_each_entry(e, &wildcard_list, list) {
532 if (!strncmp(name, e->name, LTTNG_UST_SYM_NAME_LEN - 1)) {
533 found = 1;
534 break;
535 }
536 }
537
538 if (!found) {
539 /*
540 * Using zmalloc here to allocate a variable length element. Could
541 * cause some memory fragmentation if overused.
542 */
543 e = zmalloc(sizeof(struct wildcard_entry) + name_len);
544 if (!e)
545 return ERR_PTR(-ENOMEM);
546 memcpy(&e->name[0], name, name_len);
547 cds_list_add(&e->list, &wildcard_list);
548 CDS_INIT_LIST_HEAD(&e->session_list);
549 }
550
551 /* session wildcard */
552 cds_list_for_each_entry(sw, &e->session_list, session_list) {
553 if (chan == sw->chan) {
554 DBG("wildcard %s busy for this channel", name);
555 return ERR_PTR(-EEXIST); /* Already there */
556 }
557 }
558 sw = zmalloc(sizeof(struct session_wildcard));
559 if (!sw)
560 return ERR_PTR(-ENOMEM);
561 sw->chan = chan;
562 sw->enabled = 1;
563 memcpy(&sw->event_param, event_param, sizeof(sw->event_param));
564 sw->event_param.instrumentation = LTTNG_UST_TRACEPOINT;
565 CDS_INIT_LIST_HEAD(&sw->events);
566 cds_list_add(&sw->list, &chan->session->wildcards);
567 cds_list_add(&sw->session_list, &e->session_list);
568 sw->entry = e;
569 _probes_create_wildcard_events(e, sw);
570 return sw;
571 }
572
573 /*
574 * Remove the wildcard from the wildcard hash table. Must be called with
575 * ust_lock held. Only called at session teardown.
576 */
577 void _remove_wildcard(struct session_wildcard *wildcard)
578 {
579 struct ltt_event *ev, *tmp;
580
581 /*
582 * Just remove the events owned (for enable/disable) by this
583 * wildcard from the list. The session teardown will take care
584 * of freeing the event memory.
585 */
586 cds_list_for_each_entry_safe(ev, tmp, &wildcard->events,
587 wildcard_list) {
588 cds_list_del(&ev->wildcard_list);
589 }
590 cds_list_del(&wildcard->session_list);
591 cds_list_del(&wildcard->list);
592 if (cds_list_empty(&wildcard->entry->session_list)) {
593 cds_list_del(&wildcard->entry->list);
594 free(wildcard->entry);
595 }
596 free(wildcard);
597 }
598
599 int ltt_wildcard_enable(struct session_wildcard *wildcard)
600 {
601 struct ltt_event *ev;
602 int ret;
603
604 if (wildcard->enabled)
605 return -EEXIST;
606 cds_list_for_each_entry(ev, &wildcard->events, wildcard_list) {
607 ret = ltt_event_enable(ev);
608 if (ret) {
609 DBG("Error: enable error.\n");
610 return ret;
611 }
612 }
613 wildcard->enabled = 1;
614 return 0;
615 }
616
617 int ltt_wildcard_disable(struct session_wildcard *wildcard)
618 {
619 struct ltt_event *ev;
620 int ret;
621
622 if (!wildcard->enabled)
623 return -EEXIST;
624 cds_list_for_each_entry(ev, &wildcard->events, wildcard_list) {
625 ret = ltt_event_disable(ev);
626 if (ret) {
627 DBG("Error: disable error.\n");
628 return ret;
629 }
630 }
631 wildcard->enabled = 0;
632 return 0;
633 }
This page took 0.041834 seconds and 4 git commands to generate.