Print compiler warning/runtime warning and truncate too long tracepoint names
[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
109 /*
110 * fix the events awaiting probe load.
111 */
112 for (i = 0; i < desc->nr_events; i++) {
113 ret = pending_probe_fix_events(desc->event_desc[i]);
114 assert(!ret);
115 }
116 end:
117 ust_unlock();
118 return ret;
119 }
120
121 void ltt_probe_unregister(struct lttng_probe_desc *desc)
122 {
123 ust_lock();
124 cds_list_del(&desc->head);
125 ust_unlock();
126 }
127
128 /*
129 * called with UST lock held.
130 */
131 const struct lttng_event_desc *ltt_event_get(const char *name)
132 {
133 const struct lttng_event_desc *event;
134
135 event = find_event(name);
136 if (!event)
137 return NULL;
138 return event;
139 }
140
141 void ltt_event_put(const struct lttng_event_desc *event)
142 {
143 }
144
145 void ltt_probes_prune_event_list(struct lttng_ust_tracepoint_list *list)
146 {
147 struct tp_list_entry *list_entry, *tmp;
148
149 cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) {
150 cds_list_del(&list_entry->head);
151 free(list_entry);
152 }
153 }
154
155 /*
156 * called with UST lock held.
157 */
158 int ltt_probes_get_event_list(struct lttng_ust_tracepoint_list *list)
159 {
160 struct lttng_probe_desc *probe_desc;
161 int i;
162
163 CDS_INIT_LIST_HEAD(&list->head);
164 cds_list_for_each_entry(probe_desc, &probe_list, head) {
165 for (i = 0; i < probe_desc->nr_events; i++) {
166 struct tp_list_entry *list_entry;
167
168 list_entry = zmalloc(sizeof(*list_entry));
169 if (!list_entry)
170 goto err_nomem;
171 cds_list_add(&list_entry->head, &list->head);
172 strncpy(list_entry->tp.name,
173 probe_desc->event_desc[i]->name,
174 LTTNG_UST_SYM_NAME_LEN);
175 list_entry->tp.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
176 if (!probe_desc->event_desc[i]->loglevel) {
177 list_entry->tp.loglevel[0] = '\0';
178 list_entry->tp.loglevel_value = 0;
179 } else {
180 strncpy(list_entry->tp.loglevel,
181 (*probe_desc->event_desc[i]->loglevel)->identifier,
182 LTTNG_UST_SYM_NAME_LEN);
183 list_entry->tp.loglevel[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
184 list_entry->tp.loglevel_value =
185 (*probe_desc->event_desc[i]->loglevel)->value;
186 }
187 }
188 }
189 if (cds_list_empty(&list->head))
190 list->iter = NULL;
191 else
192 list->iter =
193 cds_list_first_entry(&list->head, struct tp_list_entry, head);
194 return 0;
195
196 err_nomem:
197 ltt_probes_prune_event_list(list);
198 return -ENOMEM;
199 }
200
201 /*
202 * Return current iteration position, advance internal iterator to next.
203 * Return NULL if end of list.
204 */
205 struct lttng_ust_tracepoint_iter *
206 lttng_ust_tracepoint_list_get_iter_next(struct lttng_ust_tracepoint_list *list)
207 {
208 struct tp_list_entry *entry;
209
210 if (!list->iter)
211 return NULL;
212 entry = list->iter;
213 if (entry->head.next == &list->head)
214 list->iter = NULL;
215 else
216 list->iter = cds_list_entry(entry->head.next,
217 struct tp_list_entry, head);
218 return &entry->tp;
219 }
220
221 /*
222 * Get loglevel if the loglevel is present in the loglevel hash table.
223 * Must be called with ust lock held.
224 * Returns NULL if not present.
225 */
226 struct loglevel_entry *get_loglevel(const char *name)
227 {
228 struct cds_hlist_head *head;
229 struct cds_hlist_node *node;
230 struct loglevel_entry *e;
231 size_t name_len = strlen(name);
232 uint32_t hash;
233
234 if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) {
235 WARN("Truncating loglevel name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1);
236 name_len = LTTNG_UST_SYM_NAME_LEN - 1;
237 }
238 hash = jhash(name, name_len, 0);
239 head = &loglevel_table[hash & (LOGLEVEL_TABLE_SIZE - 1)];
240 cds_hlist_for_each_entry(e, node, head, hlist) {
241 if (!strncmp(name, e->name, LTTNG_UST_SYM_NAME_LEN - 1))
242 return e;
243 }
244 return NULL;
245 }
246
247 struct loglevel_entry *get_loglevel_value(int64_t value)
248 {
249 char name[LTTNG_UST_SYM_NAME_LEN];
250 int ret;
251
252 ret = snprintf(name, LTTNG_UST_SYM_NAME_LEN, "%lld", (long long) value);
253 if (ret < 0)
254 return NULL;
255 return get_loglevel(name);
256 }
257
258 /*
259 * marshall all probes/all events and create those that fit the
260 * loglevel. Add them to the events list as created.
261 */
262 static
263 void _probes_create_loglevel_events(struct loglevel_entry *entry,
264 struct session_loglevel *loglevel)
265 {
266 struct lttng_probe_desc *probe_desc;
267 struct lttng_ust_event event_param;
268 int i;
269
270 cds_list_for_each_entry(probe_desc, &probe_list, head) {
271 for (i = 0; i < probe_desc->nr_events; i++) {
272 const struct tracepoint_loglevel_entry *ev_ll;
273 const struct lttng_event_desc *event_desc;
274 int match = 0;
275
276 event_desc = probe_desc->event_desc[i];
277 if (!(event_desc->loglevel))
278 continue;
279 ev_ll = *event_desc->loglevel;
280 if (isdigit(entry->name[0])) {
281 if (atoll(entry->name) == ev_ll->value) {
282 match = 1;
283 }
284 } else if (!strncmp(ev_ll->identifier, entry->name,
285 LTTNG_UST_SYM_NAME_LEN - 1)) {
286 match = 1;
287 }
288
289 if (match) {
290 struct ltt_event *ev;
291 int ret;
292
293 memcpy(&event_param, &loglevel->event_param,
294 sizeof(event_param));
295 memcpy(event_param.name,
296 event_desc->name,
297 sizeof(event_param.name));
298 /* create event */
299 ret = ltt_event_create(loglevel->chan,
300 &event_param, NULL,
301 &ev);
302 if (ret) {
303 DBG("Error creating event");
304 continue;
305 }
306 cds_list_add(&ev->loglevel_list,
307 &loglevel->events);
308 }
309 }
310 }
311 }
312
313 /*
314 * Add the loglevel to the loglevel hash table. Must be called with
315 * ust lock held.
316 */
317 struct session_loglevel *add_loglevel(const char *name,
318 struct ltt_channel *chan,
319 struct lttng_ust_event *event_param)
320 {
321 struct cds_hlist_head *head;
322 struct cds_hlist_node *node;
323 struct loglevel_entry *e;
324 struct session_loglevel *sl;
325 int found = 0;
326 size_t name_len = strlen(name);
327 uint32_t hash;
328
329 if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) {
330 WARN("Truncating loglevel name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1);
331 name_len = LTTNG_UST_SYM_NAME_LEN - 1;
332 }
333 hash = jhash(name, name_len, 0);
334 /* loglevel entry */
335 head = &loglevel_table[hash & (LOGLEVEL_TABLE_SIZE - 1)];
336 cds_hlist_for_each_entry(e, node, head, hlist) {
337 if (!strncmp(name, e->name, LTTNG_UST_SYM_NAME_LEN - 1)) {
338 found = 1;
339 break;
340 }
341 }
342
343 if (!found) {
344 /*
345 * Using zmalloc here to allocate a variable length element. Could
346 * cause some memory fragmentation if overused.
347 */
348 e = zmalloc(sizeof(struct loglevel_entry) + name_len + 1);
349 if (!e)
350 return ERR_PTR(-ENOMEM);
351 memcpy(&e->name[0], name, name_len + 1);
352 e->name[name_len] = '\0';
353 cds_hlist_add_head(&e->hlist, head);
354 CDS_INIT_LIST_HEAD(&e->session_list);
355 }
356
357 /* session loglevel */
358 cds_list_for_each_entry(sl, &e->session_list, session_list) {
359 if (chan == sl->chan) {
360 DBG("loglevel %s busy for this channel", name);
361 return ERR_PTR(-EEXIST); /* Already there */
362 }
363 }
364 sl = zmalloc(sizeof(struct session_loglevel));
365 if (!sl)
366 return ERR_PTR(-ENOMEM);
367 sl->chan = chan;
368 sl->enabled = 1;
369 memcpy(&sl->event_param, event_param, sizeof(sl->event_param));
370 sl->event_param.instrumentation = LTTNG_UST_TRACEPOINT;
371 CDS_INIT_LIST_HEAD(&sl->events);
372 cds_list_add(&sl->list, &chan->session->loglevels);
373 cds_list_add(&sl->session_list, &e->session_list);
374 sl->entry = e;
375 _probes_create_loglevel_events(e, sl);
376 return sl;
377 }
378
379 /*
380 * Remove the loglevel from the loglevel hash table. Must be called with
381 * ust_lock held. Only called at session teardown.
382 */
383 void _remove_loglevel(struct session_loglevel *loglevel)
384 {
385 struct ltt_event *ev, *tmp;
386
387 /*
388 * Just remove the events owned (for enable/disable) by this
389 * loglevel from the list. The session teardown will take care
390 * of freeing the event memory.
391 */
392 cds_list_for_each_entry_safe(ev, tmp, &loglevel->events,
393 loglevel_list) {
394 cds_list_del(&ev->loglevel_list);
395 }
396 cds_list_del(&loglevel->session_list);
397 cds_list_del(&loglevel->list);
398 if (cds_list_empty(&loglevel->entry->session_list)) {
399 cds_hlist_del(&loglevel->entry->hlist);
400 free(loglevel->entry);
401 }
402 free(loglevel);
403 }
404
405 int ltt_loglevel_enable(struct session_loglevel *loglevel)
406 {
407 struct ltt_event *ev;
408 int ret;
409
410 if (loglevel->enabled)
411 return -EEXIST;
412 cds_list_for_each_entry(ev, &loglevel->events, loglevel_list) {
413 ret = ltt_event_enable(ev);
414 if (ret) {
415 DBG("Error: enable error.\n");
416 return ret;
417 }
418 }
419 loglevel->enabled = 1;
420 return 0;
421 }
422
423 int ltt_loglevel_disable(struct session_loglevel *loglevel)
424 {
425 struct ltt_event *ev;
426 int ret;
427
428 if (!loglevel->enabled)
429 return -EEXIST;
430 cds_list_for_each_entry(ev, &loglevel->events, loglevel_list) {
431 ret = ltt_event_disable(ev);
432 if (ret) {
433 DBG("Error: disable error.\n");
434 return ret;
435 }
436 }
437 loglevel->enabled = 0;
438 return 0;
439 }
440
441 /* WILDCARDS */
442
443 /*
444 * Return wildcard for a given event name if the event name match the
445 * one of the wildcards.
446 * Must be called with ust lock held.
447 * Returns NULL if not present.
448 */
449 struct wildcard_entry *match_wildcard(const char *name)
450 {
451 struct wildcard_entry *e;
452
453 cds_list_for_each_entry(e, &wildcard_list, list) {
454 /* If only contain '*' */
455 if (strlen(e->name) == 1)
456 return e;
457 /* Compare excluding final '*' */
458 if (!strncmp(name, e->name, strlen(e->name) - 1))
459 return e;
460 }
461 return NULL;
462 }
463
464 /*
465 * marshall all probes/all events and create those that fit the
466 * wildcard. Add them to the events list as created.
467 */
468 static
469 void _probes_create_wildcard_events(struct wildcard_entry *entry,
470 struct session_wildcard *wildcard)
471 {
472 struct lttng_probe_desc *probe_desc;
473 struct lttng_ust_event event_param;
474 int i;
475
476 cds_list_for_each_entry(probe_desc, &probe_list, head) {
477 for (i = 0; i < probe_desc->nr_events; i++) {
478 const struct lttng_event_desc *event_desc;
479 int match = 0;
480
481 event_desc = probe_desc->event_desc[i];
482 /* compare excluding final '*' */
483 assert(strlen(entry->name) > 0);
484 if (strcmp(event_desc->name, "lttng_ust:metadata")
485 && (strlen(entry->name) == 1
486 || !strncmp(event_desc->name, entry->name,
487 strlen(entry->name) - 1))) {
488 match = 1;
489 }
490 if (match) {
491 struct ltt_event *ev;
492 int ret;
493
494 memcpy(&event_param, &wildcard->event_param,
495 sizeof(event_param));
496 memcpy(event_param.name,
497 event_desc->name,
498 sizeof(event_param.name));
499 /* create event */
500 ret = ltt_event_create(wildcard->chan,
501 &event_param, NULL,
502 &ev);
503 if (ret) {
504 DBG("Error creating event");
505 continue;
506 }
507 cds_list_add(&ev->wildcard_list,
508 &wildcard->events);
509 }
510 }
511 }
512 }
513
514 /*
515 * Add the wildcard to the wildcard hash table. Must be called with
516 * ust lock held.
517 */
518 struct session_wildcard *add_wildcard(const char *name,
519 struct ltt_channel *chan,
520 struct lttng_ust_event *event_param)
521 {
522 struct wildcard_entry *e;
523 struct session_wildcard *sw;
524 size_t name_len = strlen(name) + 1;
525 int found = 0;
526
527 /* wildcard entry */
528 cds_list_for_each_entry(e, &wildcard_list, list) {
529 if (!strncmp(name, e->name, LTTNG_UST_SYM_NAME_LEN - 1)) {
530 found = 1;
531 break;
532 }
533 }
534
535 if (!found) {
536 /*
537 * Using zmalloc here to allocate a variable length element. Could
538 * cause some memory fragmentation if overused.
539 */
540 e = zmalloc(sizeof(struct wildcard_entry) + name_len);
541 if (!e)
542 return ERR_PTR(-ENOMEM);
543 memcpy(&e->name[0], name, name_len);
544 cds_list_add(&e->list, &wildcard_list);
545 CDS_INIT_LIST_HEAD(&e->session_list);
546 }
547
548 /* session wildcard */
549 cds_list_for_each_entry(sw, &e->session_list, session_list) {
550 if (chan == sw->chan) {
551 DBG("wildcard %s busy for this channel", name);
552 return ERR_PTR(-EEXIST); /* Already there */
553 }
554 }
555 sw = zmalloc(sizeof(struct session_wildcard));
556 if (!sw)
557 return ERR_PTR(-ENOMEM);
558 sw->chan = chan;
559 sw->enabled = 1;
560 memcpy(&sw->event_param, event_param, sizeof(sw->event_param));
561 sw->event_param.instrumentation = LTTNG_UST_TRACEPOINT;
562 CDS_INIT_LIST_HEAD(&sw->events);
563 cds_list_add(&sw->list, &chan->session->wildcards);
564 cds_list_add(&sw->session_list, &e->session_list);
565 sw->entry = e;
566 _probes_create_wildcard_events(e, sw);
567 return sw;
568 }
569
570 /*
571 * Remove the wildcard from the wildcard hash table. Must be called with
572 * ust_lock held. Only called at session teardown.
573 */
574 void _remove_wildcard(struct session_wildcard *wildcard)
575 {
576 struct ltt_event *ev, *tmp;
577
578 /*
579 * Just remove the events owned (for enable/disable) by this
580 * wildcard from the list. The session teardown will take care
581 * of freeing the event memory.
582 */
583 cds_list_for_each_entry_safe(ev, tmp, &wildcard->events,
584 wildcard_list) {
585 cds_list_del(&ev->wildcard_list);
586 }
587 cds_list_del(&wildcard->session_list);
588 cds_list_del(&wildcard->list);
589 if (cds_list_empty(&wildcard->entry->session_list)) {
590 cds_list_del(&wildcard->entry->list);
591 free(wildcard->entry);
592 }
593 free(wildcard);
594 }
595
596 int ltt_wildcard_enable(struct session_wildcard *wildcard)
597 {
598 struct ltt_event *ev;
599 int ret;
600
601 if (wildcard->enabled)
602 return -EEXIST;
603 cds_list_for_each_entry(ev, &wildcard->events, wildcard_list) {
604 ret = ltt_event_enable(ev);
605 if (ret) {
606 DBG("Error: enable error.\n");
607 return ret;
608 }
609 }
610 wildcard->enabled = 1;
611 return 0;
612 }
613
614 int ltt_wildcard_disable(struct session_wildcard *wildcard)
615 {
616 struct ltt_event *ev;
617 int ret;
618
619 if (!wildcard->enabled)
620 return -EEXIST;
621 cds_list_for_each_entry(ev, &wildcard->events, wildcard_list) {
622 ret = ltt_event_disable(ev);
623 if (ret) {
624 DBG("Error: disable error.\n");
625 return ret;
626 }
627 }
628 wildcard->enabled = 0;
629 return 0;
630 }
This page took 0.042857 seconds and 4 git commands to generate.