Fix: filter bytecode and string memory leak on error
[lttng-tools.git] / src / bin / lttng-sessiond / trace-ust.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <inttypes.h>
24
25 #include <common/common.h>
26 #include <common/defaults.h>
27
28 #include "buffer-registry.h"
29 #include "trace-ust.h"
30 #include "utils.h"
31
32 /*
33 * Match function for the events hash table lookup.
34 *
35 * Matches by name only. Used by the disable command.
36 */
37 int trace_ust_ht_match_event_by_name(struct cds_lfht_node *node,
38 const void *_key)
39 {
40 struct ltt_ust_event *event;
41 const char *name;
42
43 assert(node);
44 assert(_key);
45
46 event = caa_container_of(node, struct ltt_ust_event, node.node);
47 name = _key;
48
49 /* Event name */
50 if (strncmp(event->attr.name, name, sizeof(event->attr.name)) != 0) {
51 goto no_match;
52 }
53
54 /* Match */
55 return 1;
56
57 no_match:
58 return 0;
59 }
60
61 /*
62 * Match function for the hash table lookup.
63 *
64 * It matches an ust event based on three attributes which are the event name,
65 * the filter bytecode and the loglevel.
66 */
67 int trace_ust_ht_match_event(struct cds_lfht_node *node, const void *_key)
68 {
69 struct ltt_ust_event *event;
70 const struct ltt_ust_ht_key *key;
71
72 assert(node);
73 assert(_key);
74
75 event = caa_container_of(node, struct ltt_ust_event, node.node);
76 key = _key;
77
78 /* Match the 4 elements of the key: name, filter, loglevel, exclusions. */
79
80 /* Event name */
81 if (strncmp(event->attr.name, key->name, sizeof(event->attr.name)) != 0) {
82 goto no_match;
83 }
84
85 /* Event loglevel. */
86 if (event->attr.loglevel != key->loglevel) {
87 if (event->attr.loglevel_type == LTTNG_UST_LOGLEVEL_ALL
88 && key->loglevel == 0 && event->attr.loglevel == -1) {
89 /*
90 * Match is accepted. This is because on event creation, the
91 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
92 * -1 are accepted for this loglevel type since 0 is the one set by
93 * the API when receiving an enable event.
94 */
95 } else {
96 goto no_match;
97 }
98 }
99
100 /* Only one of the filters is NULL, fail. */
101 if ((key->filter && !event->filter) || (!key->filter && event->filter)) {
102 goto no_match;
103 }
104
105 if (key->filter && event->filter) {
106 /* Both filters exists, check length followed by the bytecode. */
107 if (event->filter->len != key->filter->len ||
108 memcmp(event->filter->data, key->filter->data,
109 event->filter->len) != 0) {
110 goto no_match;
111 }
112 }
113
114 /* If only one of the exclusions is NULL, fail. */
115 if ((key->exclusion && !event->exclusion) || (!key->exclusion && event->exclusion)) {
116 goto no_match;
117 }
118
119 if (key->exclusion && event->exclusion) {
120 /* Both exclusions exist; check count followed by names. */
121 if (event->exclusion->count != key->exclusion->count ||
122 memcmp(event->exclusion->names, key->exclusion->names,
123 event->exclusion->count * LTTNG_SYMBOL_NAME_LEN) != 0) {
124 goto no_match;
125 }
126 }
127 /* Match. */
128 return 1;
129
130 no_match:
131 return 0;
132 }
133
134 /*
135 * Find the channel in the hashtable and return channel pointer. RCU read side
136 * lock MUST be acquired before calling this.
137 */
138 struct ltt_ust_channel *trace_ust_find_channel_by_name(struct lttng_ht *ht,
139 char *name)
140 {
141 struct lttng_ht_node_str *node;
142 struct lttng_ht_iter iter;
143
144 /*
145 * If we receive an empty string for channel name, it means the
146 * default channel name is requested.
147 */
148 if (name[0] == '\0')
149 name = DEFAULT_CHANNEL_NAME;
150
151 lttng_ht_lookup(ht, (void *)name, &iter);
152 node = lttng_ht_iter_get_node_str(&iter);
153 if (node == NULL) {
154 goto error;
155 }
156
157 DBG2("Trace UST channel %s found by name", name);
158
159 return caa_container_of(node, struct ltt_ust_channel, node);
160
161 error:
162 DBG2("Trace UST channel %s not found by name", name);
163 return NULL;
164 }
165
166 /*
167 * Find the event in the hashtable and return event pointer. RCU read side lock
168 * MUST be acquired before calling this.
169 */
170 struct ltt_ust_event *trace_ust_find_event(struct lttng_ht *ht,
171 char *name, struct lttng_filter_bytecode *filter, int loglevel,
172 struct lttng_event_exclusion *exclusion)
173 {
174 struct lttng_ht_node_str *node;
175 struct lttng_ht_iter iter;
176 struct ltt_ust_ht_key key;
177
178 assert(name);
179 assert(ht);
180
181 key.name = name;
182 key.filter = filter;
183 key.loglevel = loglevel;
184 key.exclusion = exclusion;
185
186 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
187 trace_ust_ht_match_event, &key, &iter.iter);
188 node = lttng_ht_iter_get_node_str(&iter);
189 if (node == NULL) {
190 goto error;
191 }
192
193 DBG2("Trace UST event %s found", key.name);
194
195 return caa_container_of(node, struct ltt_ust_event, node);
196
197 error:
198 DBG2("Trace UST event %s NOT found", key.name);
199 return NULL;
200 }
201
202 /*
203 * Allocate and initialize a ust session data structure.
204 *
205 * Return pointer to structure or NULL.
206 */
207 struct ltt_ust_session *trace_ust_create_session(uint64_t session_id)
208 {
209 int ret;
210 struct ltt_ust_session *lus;
211
212 /* Allocate a new ltt ust session */
213 lus = zmalloc(sizeof(struct ltt_ust_session));
214 if (lus == NULL) {
215 PERROR("create ust session zmalloc");
216 goto error;
217 }
218
219 /* Init data structure */
220 lus->id = session_id;
221 lus->active = 0;
222
223 /* Set default metadata channel attribute. */
224 lus->metadata_attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
225 lus->metadata_attr.subbuf_size = default_get_metadata_subbuf_size();
226 lus->metadata_attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
227 lus->metadata_attr.switch_timer_interval = DEFAULT_METADATA_SWITCH_TIMER;
228 lus->metadata_attr.read_timer_interval = DEFAULT_METADATA_READ_TIMER;
229 lus->metadata_attr.output = LTTNG_UST_MMAP;
230
231 /*
232 * Default buffer type. This can be changed through an enable channel
233 * requesting a different type. Note that this can only be changed once
234 * during the session lifetime which is at the first enable channel and
235 * only before start. The flag buffer_type_changed indicates the status.
236 */
237 lus->buffer_type = LTTNG_BUFFER_PER_UID;
238 /* Once set to 1, the buffer_type is immutable for the session. */
239 lus->buffer_type_changed = 0;
240 /* Init it in case it get used after allocation. */
241 CDS_INIT_LIST_HEAD(&lus->buffer_reg_uid_list);
242
243 /* Alloc UST global domain channels' HT */
244 lus->domain_global.channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
245 ret = jul_init_domain(&lus->domain_jul);
246 if (ret < 0) {
247 goto error_consumer;
248 }
249
250 lus->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
251 if (lus->consumer == NULL) {
252 goto error_consumer;
253 }
254
255 /*
256 * The tmp_consumer stays NULL until a set_consumer_uri command is
257 * executed. At this point, the consumer should be nullify until an
258 * enable_consumer command. This assignment is symbolic since we've zmalloc
259 * the struct.
260 */
261 lus->tmp_consumer = NULL;
262
263 DBG2("UST trace session create successful");
264
265 return lus;
266
267 error_consumer:
268 ht_cleanup_push(lus->domain_global.channels);
269 jul_destroy_domain(&lus->domain_jul);
270 free(lus);
271 error:
272 return NULL;
273 }
274
275 /*
276 * Allocate and initialize a ust channel data structure.
277 *
278 * Return pointer to structure or NULL.
279 */
280 struct ltt_ust_channel *trace_ust_create_channel(struct lttng_channel *chan)
281 {
282 struct ltt_ust_channel *luc;
283
284 assert(chan);
285
286 luc = zmalloc(sizeof(struct ltt_ust_channel));
287 if (luc == NULL) {
288 PERROR("ltt_ust_channel zmalloc");
289 goto error;
290 }
291
292 /* Copy UST channel attributes */
293 luc->attr.overwrite = chan->attr.overwrite;
294 luc->attr.subbuf_size = chan->attr.subbuf_size;
295 luc->attr.num_subbuf = chan->attr.num_subbuf;
296 luc->attr.switch_timer_interval = chan->attr.switch_timer_interval;
297 luc->attr.read_timer_interval = chan->attr.read_timer_interval;
298 luc->attr.output = (enum lttng_ust_output) chan->attr.output;
299
300 /* Translate to UST output enum */
301 switch (luc->attr.output) {
302 default:
303 luc->attr.output = LTTNG_UST_MMAP;
304 break;
305 }
306
307 /*
308 * If we receive an empty string for channel name, it means the
309 * default channel name is requested.
310 */
311 if (chan->name[0] == '\0') {
312 strncpy(luc->name, DEFAULT_CHANNEL_NAME, sizeof(luc->name));
313 } else {
314 /* Copy channel name */
315 strncpy(luc->name, chan->name, sizeof(luc->name));
316 }
317 luc->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
318
319 /* Init node */
320 lttng_ht_node_init_str(&luc->node, luc->name);
321 CDS_INIT_LIST_HEAD(&luc->ctx_list);
322
323 /* Alloc hash tables */
324 luc->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
325 luc->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
326
327 /* On-disk circular buffer parameters */
328 luc->tracefile_size = chan->attr.tracefile_size;
329 luc->tracefile_count = chan->attr.tracefile_count;
330
331 DBG2("Trace UST channel %s created", luc->name);
332
333 error:
334 return luc;
335 }
336
337 /*
338 * Allocate and initialize a ust event. Set name and event type.
339 * We own filter_expression, filter, and exclusion.
340 *
341 * Return pointer to structure or NULL.
342 */
343 struct ltt_ust_event *trace_ust_create_event(struct lttng_event *ev,
344 char *filter_expression,
345 struct lttng_filter_bytecode *filter,
346 struct lttng_event_exclusion *exclusion)
347 {
348 struct ltt_ust_event *lue;
349
350 assert(ev);
351
352 lue = zmalloc(sizeof(struct ltt_ust_event));
353 if (lue == NULL) {
354 PERROR("ust event zmalloc");
355 goto error;
356 }
357
358 switch (ev->type) {
359 case LTTNG_EVENT_PROBE:
360 lue->attr.instrumentation = LTTNG_UST_PROBE;
361 break;
362 case LTTNG_EVENT_FUNCTION:
363 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
364 break;
365 case LTTNG_EVENT_FUNCTION_ENTRY:
366 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
367 break;
368 case LTTNG_EVENT_TRACEPOINT:
369 lue->attr.instrumentation = LTTNG_UST_TRACEPOINT;
370 break;
371 default:
372 ERR("Unknown ust instrumentation type (%d)", ev->type);
373 goto error_free_event;
374 }
375
376 /* Copy event name */
377 strncpy(lue->attr.name, ev->name, LTTNG_UST_SYM_NAME_LEN);
378 lue->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
379
380 switch (ev->loglevel_type) {
381 case LTTNG_EVENT_LOGLEVEL_ALL:
382 lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_ALL;
383 lue->attr.loglevel = -1; /* Force to -1 */
384 break;
385 case LTTNG_EVENT_LOGLEVEL_RANGE:
386 lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_RANGE;
387 lue->attr.loglevel = ev->loglevel;
388 break;
389 case LTTNG_EVENT_LOGLEVEL_SINGLE:
390 lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_SINGLE;
391 lue->attr.loglevel = ev->loglevel;
392 break;
393 default:
394 ERR("Unknown ust loglevel type (%d)", ev->loglevel_type);
395 goto error_free_event;
396 }
397 /*
398 * Fix for enabler race. Enable is now done explicitly by
399 * sessiond after setting filter.
400 */
401 lue->attr.disabled = 1;
402
403 /* Same layout. */
404 lue->filter_expression = filter_expression;
405 lue->filter = (struct lttng_ust_filter_bytecode *) filter;
406 lue->exclusion = (struct lttng_event_exclusion *) exclusion;
407
408 /* Init node */
409 lttng_ht_node_init_str(&lue->node, lue->attr.name);
410
411 DBG2("Trace UST event %s, loglevel (%d,%d) created",
412 lue->attr.name, lue->attr.loglevel_type,
413 lue->attr.loglevel);
414
415 return lue;
416
417 error_free_event:
418 free(lue);
419 error:
420 free(filter_expression);
421 free(filter);
422 free(exclusion);
423 return NULL;
424 }
425
426 static
427 int trace_ust_context_type_event_to_ust(enum lttng_event_context_type type)
428 {
429 int utype;
430
431 switch (type) {
432 case LTTNG_EVENT_CONTEXT_VTID:
433 utype = LTTNG_UST_CONTEXT_VTID;
434 break;
435 case LTTNG_EVENT_CONTEXT_VPID:
436 utype = LTTNG_UST_CONTEXT_VPID;
437 break;
438 case LTTNG_EVENT_CONTEXT_PTHREAD_ID:
439 utype = LTTNG_UST_CONTEXT_PTHREAD_ID;
440 break;
441 case LTTNG_EVENT_CONTEXT_PROCNAME:
442 utype = LTTNG_UST_CONTEXT_PROCNAME;
443 break;
444 case LTTNG_EVENT_CONTEXT_IP:
445 utype = LTTNG_UST_CONTEXT_IP;
446 break;
447 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
448 utype = LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER;
449 break;
450 default:
451 ERR("Invalid UST context");
452 utype = -1;
453 break;
454 }
455 return utype;
456 }
457
458 /*
459 * Return 1 if contexts match, 0 otherwise.
460 */
461 int trace_ust_match_context(struct ltt_ust_context *uctx,
462 struct lttng_event_context *ctx)
463 {
464 int utype;
465
466 utype = trace_ust_context_type_event_to_ust(ctx->ctx);
467 if (utype < 0) {
468 return 0;
469 }
470 if (uctx->ctx.ctx != utype) {
471 return 0;
472 }
473 switch (utype) {
474 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER:
475 if (uctx->ctx.u.perf_counter.type
476 != ctx->u.perf_counter.type) {
477 return 0;
478 }
479 if (uctx->ctx.u.perf_counter.config
480 != ctx->u.perf_counter.config) {
481 return 0;
482 }
483 if (strncmp(uctx->ctx.u.perf_counter.name,
484 ctx->u.perf_counter.name,
485 LTTNG_UST_SYM_NAME_LEN)) {
486 return 0;
487 }
488 break;
489 default:
490 break;
491
492 }
493 return 1;
494 }
495
496 /*
497 * Allocate and initialize an UST context.
498 *
499 * Return pointer to structure or NULL.
500 */
501 struct ltt_ust_context *trace_ust_create_context(
502 struct lttng_event_context *ctx)
503 {
504 struct ltt_ust_context *uctx;
505 int utype;
506
507 assert(ctx);
508
509 utype = trace_ust_context_type_event_to_ust(ctx->ctx);
510 if (utype < 0) {
511 return NULL;
512 }
513
514 uctx = zmalloc(sizeof(struct ltt_ust_context));
515 if (uctx == NULL) {
516 PERROR("zmalloc ltt_ust_context");
517 goto error;
518 }
519
520 uctx->ctx.ctx = (enum lttng_ust_context_type) utype;
521 switch (utype) {
522 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER:
523 uctx->ctx.u.perf_counter.type = ctx->u.perf_counter.type;
524 uctx->ctx.u.perf_counter.config = ctx->u.perf_counter.config;
525 strncpy(uctx->ctx.u.perf_counter.name, ctx->u.perf_counter.name,
526 LTTNG_UST_SYM_NAME_LEN);
527 uctx->ctx.u.perf_counter.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
528 break;
529 default:
530 break;
531 }
532 lttng_ht_node_init_ulong(&uctx->node, (unsigned long) uctx->ctx.ctx);
533
534 return uctx;
535
536 error:
537 return NULL;
538 }
539
540 /*
541 * RCU safe free context structure.
542 */
543 static void destroy_context_rcu(struct rcu_head *head)
544 {
545 struct lttng_ht_node_ulong *node =
546 caa_container_of(head, struct lttng_ht_node_ulong, head);
547 struct ltt_ust_context *ctx =
548 caa_container_of(node, struct ltt_ust_context, node);
549
550 free(ctx);
551 }
552
553 /*
554 * Cleanup UST context hash table.
555 */
556 static void destroy_contexts(struct lttng_ht *ht)
557 {
558 int ret;
559 struct lttng_ht_node_ulong *node;
560 struct lttng_ht_iter iter;
561 struct ltt_ust_context *ctx;
562
563 assert(ht);
564
565 rcu_read_lock();
566 cds_lfht_for_each_entry(ht->ht, &iter.iter, node, node) {
567 /* Remove from ordered list. */
568 ctx = caa_container_of(node, struct ltt_ust_context, node);
569 cds_list_del(&ctx->list);
570 /* Remove from channel's hash table. */
571 ret = lttng_ht_del(ht, &iter);
572 if (!ret) {
573 call_rcu(&node->head, destroy_context_rcu);
574 }
575 }
576 rcu_read_unlock();
577
578 ht_cleanup_push(ht);
579 }
580
581 /*
582 * Cleanup ust event structure.
583 */
584 void trace_ust_destroy_event(struct ltt_ust_event *event)
585 {
586 assert(event);
587
588 DBG2("Trace destroy UST event %s", event->attr.name);
589 free(event->filter_expression);
590 free(event->filter);
591 free(event->exclusion);
592 free(event);
593 }
594
595 /*
596 * URCU intermediate call to complete destroy event.
597 */
598 static void destroy_event_rcu(struct rcu_head *head)
599 {
600 struct lttng_ht_node_str *node =
601 caa_container_of(head, struct lttng_ht_node_str, head);
602 struct ltt_ust_event *event =
603 caa_container_of(node, struct ltt_ust_event, node);
604
605 trace_ust_destroy_event(event);
606 }
607
608 /*
609 * Cleanup UST events hashtable.
610 */
611 static void destroy_events(struct lttng_ht *events)
612 {
613 int ret;
614 struct lttng_ht_node_str *node;
615 struct lttng_ht_iter iter;
616
617 assert(events);
618
619 rcu_read_lock();
620 cds_lfht_for_each_entry(events->ht, &iter.iter, node, node) {
621 ret = lttng_ht_del(events, &iter);
622 assert(!ret);
623 call_rcu(&node->head, destroy_event_rcu);
624 }
625 rcu_read_unlock();
626
627 ht_cleanup_push(events);
628 }
629
630 /*
631 * Cleanup ust channel structure.
632 *
633 * Should _NOT_ be called with RCU read lock held.
634 */
635 static void _trace_ust_destroy_channel(struct ltt_ust_channel *channel)
636 {
637 assert(channel);
638
639 DBG2("Trace destroy UST channel %s", channel->name);
640
641 /* Destroying all events of the channel */
642 destroy_events(channel->events);
643 /* Destroying all context of the channel */
644 destroy_contexts(channel->ctx);
645
646 free(channel);
647 }
648
649 /*
650 * URCU intermediate call to complete destroy channel.
651 */
652 static void destroy_channel_rcu(struct rcu_head *head)
653 {
654 struct lttng_ht_node_str *node =
655 caa_container_of(head, struct lttng_ht_node_str, head);
656 struct ltt_ust_channel *channel =
657 caa_container_of(node, struct ltt_ust_channel, node);
658
659 _trace_ust_destroy_channel(channel);
660 }
661
662 void trace_ust_destroy_channel(struct ltt_ust_channel *channel)
663 {
664 call_rcu(&channel->node.head, destroy_channel_rcu);
665 }
666
667 /*
668 * Remove an UST channel from a channel HT.
669 */
670 void trace_ust_delete_channel(struct lttng_ht *ht,
671 struct ltt_ust_channel *channel)
672 {
673 int ret;
674 struct lttng_ht_iter iter;
675
676 assert(ht);
677 assert(channel);
678
679 iter.iter.node = &channel->node.node;
680 ret = lttng_ht_del(ht, &iter);
681 assert(!ret);
682 }
683
684 /*
685 * Iterate over a hash table containing channels and cleanup safely.
686 */
687 static void destroy_channels(struct lttng_ht *channels)
688 {
689 int ret;
690 struct lttng_ht_node_str *node;
691 struct lttng_ht_iter iter;
692
693 assert(channels);
694
695 rcu_read_lock();
696
697 cds_lfht_for_each_entry(channels->ht, &iter.iter, node, node) {
698 ret = lttng_ht_del(channels, &iter);
699 assert(!ret);
700 call_rcu(&node->head, destroy_channel_rcu);
701 }
702 rcu_read_unlock();
703
704 ht_cleanup_push(channels);
705 }
706
707 /*
708 * Cleanup UST global domain.
709 */
710 static void destroy_domain_global(struct ltt_ust_domain_global *dom)
711 {
712 assert(dom);
713
714 destroy_channels(dom->channels);
715 }
716
717 /*
718 * Cleanup ust session structure
719 *
720 * Should *NOT* be called with RCU read-side lock held.
721 */
722 void trace_ust_destroy_session(struct ltt_ust_session *session)
723 {
724 struct buffer_reg_uid *reg, *sreg;
725
726 assert(session);
727
728 DBG2("Trace UST destroy session %" PRIu64, session->id);
729
730 /* Cleaning up UST domain */
731 destroy_domain_global(&session->domain_global);
732 jul_destroy_domain(&session->domain_jul);
733
734 /* Cleanup UID buffer registry object(s). */
735 cds_list_for_each_entry_safe(reg, sreg, &session->buffer_reg_uid_list,
736 lnode) {
737 cds_list_del(&reg->lnode);
738 buffer_reg_uid_remove(reg);
739 buffer_reg_uid_destroy(reg, session->consumer);
740 }
741
742 consumer_destroy_output(session->consumer);
743 consumer_destroy_output(session->tmp_consumer);
744
745 free(session);
746 }
This page took 0.043466 seconds and 4 git commands to generate.