Backport: Fix: tracker: ensure consistency of tracker states
[lttng-tools.git] / src / bin / lttng-sessiond / trace-ust.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #define _LGPL_SOURCE
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <inttypes.h>
25
26 #include <common/common.h>
27 #include <common/defaults.h>
28
29 #include "buffer-registry.h"
30 #include "trace-ust.h"
31 #include "utils.h"
32 #include "ust-app.h"
33 #include "agent.h"
34 #include "tracker.h"
35
36 /*
37 * Match function for the events hash table lookup.
38 *
39 * Matches by name only. Used by the disable command.
40 */
41 int trace_ust_ht_match_event_by_name(struct cds_lfht_node *node,
42 const void *_key)
43 {
44 struct ltt_ust_event *event;
45 const char *name;
46
47 assert(node);
48 assert(_key);
49
50 event = caa_container_of(node, struct ltt_ust_event, node.node);
51 name = _key;
52
53 /* Event name */
54 if (strncmp(event->attr.name, name, sizeof(event->attr.name)) != 0) {
55 goto no_match;
56 }
57
58 /* Match */
59 return 1;
60
61 no_match:
62 return 0;
63 }
64
65 /*
66 * Match function for the hash table lookup.
67 *
68 * It matches an ust event based on three attributes which are the event name,
69 * the filter bytecode and the loglevel.
70 */
71 int trace_ust_ht_match_event(struct cds_lfht_node *node, const void *_key)
72 {
73 struct ltt_ust_event *event;
74 const struct ltt_ust_ht_key *key;
75 int ev_loglevel_value;
76 int ll_match;
77
78 assert(node);
79 assert(_key);
80
81 event = caa_container_of(node, struct ltt_ust_event, node.node);
82 key = _key;
83 ev_loglevel_value = event->attr.loglevel;
84
85 /* Match the 4 elements of the key: name, filter, loglevel, exclusions. */
86
87 /* Event name */
88 if (strncmp(event->attr.name, key->name, sizeof(event->attr.name)) != 0) {
89 goto no_match;
90 }
91
92 /* Event loglevel value and type. */
93 ll_match = loglevels_match(event->attr.loglevel_type,
94 ev_loglevel_value, key->loglevel_type,
95 key->loglevel_value, LTTNG_UST_LOGLEVEL_ALL);
96
97 if (!ll_match) {
98 goto no_match;
99 }
100
101 /* Only one of the filters is NULL, fail. */
102 if ((key->filter && !event->filter) || (!key->filter && event->filter)) {
103 goto no_match;
104 }
105
106 if (key->filter && event->filter) {
107 /* Both filters exists, check length followed by the bytecode. */
108 if (event->filter->len != key->filter->len ||
109 memcmp(event->filter->data, key->filter->data,
110 event->filter->len) != 0) {
111 goto no_match;
112 }
113 }
114
115 /* If only one of the exclusions is NULL, fail. */
116 if ((key->exclusion && !event->exclusion) || (!key->exclusion && event->exclusion)) {
117 goto no_match;
118 }
119
120 if (key->exclusion && event->exclusion) {
121 size_t i;
122
123 /* Check exclusion counts first. */
124 if (event->exclusion->count != key->exclusion->count) {
125 goto no_match;
126 }
127
128 /* Compare names individually. */
129 for (i = 0; i < event->exclusion->count; ++i) {
130 size_t j;
131 bool found = false;
132 const char *name_ev =
133 LTTNG_EVENT_EXCLUSION_NAME_AT(
134 event->exclusion, i);
135
136 /*
137 * Compare this exclusion name to all the key's
138 * exclusion names.
139 */
140 for (j = 0; j < key->exclusion->count; ++j) {
141 const char *name_key =
142 LTTNG_EVENT_EXCLUSION_NAME_AT(
143 key->exclusion, j);
144
145 if (!strncmp(name_ev, name_key,
146 LTTNG_SYMBOL_NAME_LEN)) {
147 /* Names match! */
148 found = true;
149 break;
150 }
151 }
152
153 /*
154 * If the current exclusion name was not found amongst
155 * the key's exclusion names, then there's no match.
156 */
157 if (!found) {
158 goto no_match;
159 }
160 }
161 }
162 /* Match. */
163 return 1;
164
165 no_match:
166 return 0;
167 }
168
169 /*
170 * Find the channel in the hashtable and return channel pointer. RCU read side
171 * lock MUST be acquired before calling this.
172 */
173 struct ltt_ust_channel *trace_ust_find_channel_by_name(struct lttng_ht *ht,
174 char *name)
175 {
176 struct lttng_ht_node_str *node;
177 struct lttng_ht_iter iter;
178
179 /*
180 * If we receive an empty string for channel name, it means the
181 * default channel name is requested.
182 */
183 if (name[0] == '\0')
184 name = DEFAULT_CHANNEL_NAME;
185
186 lttng_ht_lookup(ht, (void *)name, &iter);
187 node = lttng_ht_iter_get_node_str(&iter);
188 if (node == NULL) {
189 goto error;
190 }
191
192 DBG2("Trace UST channel %s found by name", name);
193
194 return caa_container_of(node, struct ltt_ust_channel, node);
195
196 error:
197 DBG2("Trace UST channel %s not found by name", name);
198 return NULL;
199 }
200
201 /*
202 * Find the event in the hashtable and return event pointer. RCU read side lock
203 * MUST be acquired before calling this.
204 */
205 struct ltt_ust_event *trace_ust_find_event(struct lttng_ht *ht,
206 char *name, struct lttng_filter_bytecode *filter,
207 enum lttng_ust_loglevel_type loglevel_type, int loglevel_value,
208 struct lttng_event_exclusion *exclusion)
209 {
210 struct lttng_ht_node_str *node;
211 struct lttng_ht_iter iter;
212 struct ltt_ust_ht_key key;
213
214 assert(name);
215 assert(ht);
216
217 key.name = name;
218 key.filter = filter;
219 key.loglevel_type = loglevel_type;
220 key.loglevel_value = loglevel_value;
221 key.exclusion = exclusion;
222
223 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
224 trace_ust_ht_match_event, &key, &iter.iter);
225 node = lttng_ht_iter_get_node_str(&iter);
226 if (node == NULL) {
227 goto error;
228 }
229
230 DBG2("Trace UST event %s found", key.name);
231
232 return caa_container_of(node, struct ltt_ust_event, node);
233
234 error:
235 DBG2("Trace UST event %s NOT found", key.name);
236 return NULL;
237 }
238
239 /*
240 * Lookup an agent in the session agents hash table by domain type and return
241 * the object if found else NULL.
242 *
243 * RCU read side lock must be acquired before calling and only released
244 * once the agent is no longer in scope or being used.
245 */
246 struct agent *trace_ust_find_agent(struct ltt_ust_session *session,
247 enum lttng_domain_type domain_type)
248 {
249 struct agent *agt = NULL;
250 struct lttng_ht_node_u64 *node;
251 struct lttng_ht_iter iter;
252 uint64_t key;
253
254 assert(session);
255
256 DBG3("Trace ust agent lookup for domain %d", domain_type);
257
258 key = domain_type;
259
260 lttng_ht_lookup(session->agents, &key, &iter);
261 node = lttng_ht_iter_get_node_u64(&iter);
262 if (!node) {
263 goto end;
264 }
265 agt = caa_container_of(node, struct agent, node);
266
267 end:
268 return agt;
269 }
270
271 /*
272 * Allocate and initialize a ust session data structure.
273 *
274 * Return pointer to structure or NULL.
275 */
276 struct ltt_ust_session *trace_ust_create_session(uint64_t session_id)
277 {
278 struct ltt_ust_session *lus;
279
280 /* Allocate a new ltt ust session */
281 lus = zmalloc(sizeof(struct ltt_ust_session));
282 if (lus == NULL) {
283 PERROR("create ust session zmalloc");
284 goto error_alloc;
285 }
286
287 /* Init data structure */
288 lus->id = session_id;
289 lus->active = 0;
290
291 /* Set default metadata channel attribute. */
292 lus->metadata_attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
293 lus->metadata_attr.subbuf_size = default_get_metadata_subbuf_size();
294 lus->metadata_attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
295 lus->metadata_attr.switch_timer_interval = DEFAULT_METADATA_SWITCH_TIMER;
296 lus->metadata_attr.read_timer_interval = DEFAULT_METADATA_READ_TIMER;
297 lus->metadata_attr.output = LTTNG_UST_MMAP;
298
299 /*
300 * Default buffer type. This can be changed through an enable channel
301 * requesting a different type. Note that this can only be changed once
302 * during the session lifetime which is at the first enable channel and
303 * only before start. The flag buffer_type_changed indicates the status.
304 */
305 lus->buffer_type = LTTNG_BUFFER_PER_UID;
306 /* Once set to 1, the buffer_type is immutable for the session. */
307 lus->buffer_type_changed = 0;
308 /* Init it in case it get used after allocation. */
309 CDS_INIT_LIST_HEAD(&lus->buffer_reg_uid_list);
310
311 /* Alloc UST global domain channels' HT */
312 lus->domain_global.channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
313 /* Alloc agent hash table. */
314 lus->agents = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
315
316 lus->tracker_list_vpid = lttng_tracker_list_create();
317 if (!lus->tracker_list_vpid) {
318 goto error;
319 }
320 lus->tracker_list_vuid = lttng_tracker_list_create();
321 if (!lus->tracker_list_vuid) {
322 goto error;
323 }
324 lus->tracker_list_vgid = lttng_tracker_list_create();
325 if (!lus->tracker_list_vgid) {
326 goto error;
327 }
328 lus->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
329 if (lus->consumer == NULL) {
330 goto error;
331 }
332
333 DBG2("UST trace session create successful");
334
335 return lus;
336
337 error:
338 lttng_tracker_list_destroy(lus->tracker_list_vpid);
339 lttng_tracker_list_destroy(lus->tracker_list_vuid);
340 lttng_tracker_list_destroy(lus->tracker_list_vgid);
341 ht_cleanup_push(lus->domain_global.channels);
342 ht_cleanup_push(lus->agents);
343 free(lus);
344 error_alloc:
345 return NULL;
346 }
347
348 /*
349 * Allocate and initialize a ust channel data structure.
350 *
351 * Return pointer to structure or NULL.
352 */
353 struct ltt_ust_channel *trace_ust_create_channel(struct lttng_channel *chan,
354 enum lttng_domain_type domain)
355 {
356 struct ltt_ust_channel *luc;
357
358 assert(chan);
359
360 luc = zmalloc(sizeof(struct ltt_ust_channel));
361 if (luc == NULL) {
362 PERROR("ltt_ust_channel zmalloc");
363 goto error;
364 }
365
366 luc->domain = domain;
367
368 /* Copy UST channel attributes */
369 luc->attr.overwrite = chan->attr.overwrite;
370 luc->attr.subbuf_size = chan->attr.subbuf_size;
371 luc->attr.num_subbuf = chan->attr.num_subbuf;
372 luc->attr.switch_timer_interval = chan->attr.switch_timer_interval;
373 luc->attr.read_timer_interval = chan->attr.read_timer_interval;
374 luc->attr.output = (enum lttng_ust_output) chan->attr.output;
375
376 /* Translate to UST output enum */
377 switch (luc->attr.output) {
378 default:
379 luc->attr.output = LTTNG_UST_MMAP;
380 break;
381 }
382
383 /*
384 * If we receive an empty string for channel name, it means the
385 * default channel name is requested.
386 */
387 if (chan->name[0] == '\0') {
388 strncpy(luc->name, DEFAULT_CHANNEL_NAME, sizeof(luc->name));
389 } else {
390 /* Copy channel name */
391 strncpy(luc->name, chan->name, sizeof(luc->name));
392 }
393 luc->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
394
395 /* Init node */
396 lttng_ht_node_init_str(&luc->node, luc->name);
397 CDS_INIT_LIST_HEAD(&luc->ctx_list);
398
399 /* Alloc hash tables */
400 luc->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
401 luc->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
402
403 /* On-disk circular buffer parameters */
404 luc->tracefile_size = chan->attr.tracefile_size;
405 luc->tracefile_count = chan->attr.tracefile_count;
406
407 DBG2("Trace UST channel %s created", luc->name);
408
409 error:
410 return luc;
411 }
412
413 /*
414 * Validates an exclusion list.
415 *
416 * Returns 0 if valid, negative value if invalid.
417 */
418 static int validate_exclusion(struct lttng_event_exclusion *exclusion)
419 {
420 size_t i;
421 int ret = 0;
422
423 assert(exclusion);
424
425 for (i = 0; i < exclusion->count; ++i) {
426 size_t j;
427 const char *name_a =
428 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, i);
429
430 for (j = 0; j < i; ++j) {
431 const char *name_b =
432 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, j);
433
434 if (!strncmp(name_a, name_b, LTTNG_SYMBOL_NAME_LEN)) {
435 /* Match! */
436 ret = -1;
437 goto end;
438 }
439 }
440 }
441
442 end:
443 return ret;
444 }
445
446 /*
447 * Allocate and initialize a ust event. Set name and event type.
448 * We own filter_expression, filter, and exclusion.
449 *
450 * Return pointer to structure or NULL.
451 */
452 struct ltt_ust_event *trace_ust_create_event(struct lttng_event *ev,
453 char *filter_expression,
454 struct lttng_filter_bytecode *filter,
455 struct lttng_event_exclusion *exclusion,
456 bool internal_event)
457 {
458 struct ltt_ust_event *lue;
459
460 assert(ev);
461
462 if (exclusion && validate_exclusion(exclusion)) {
463 goto error;
464 }
465
466 lue = zmalloc(sizeof(struct ltt_ust_event));
467 if (lue == NULL) {
468 PERROR("ust event zmalloc");
469 goto error;
470 }
471
472 lue->internal = internal_event;
473
474 switch (ev->type) {
475 case LTTNG_EVENT_PROBE:
476 lue->attr.instrumentation = LTTNG_UST_PROBE;
477 break;
478 case LTTNG_EVENT_FUNCTION:
479 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
480 break;
481 case LTTNG_EVENT_FUNCTION_ENTRY:
482 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
483 break;
484 case LTTNG_EVENT_TRACEPOINT:
485 lue->attr.instrumentation = LTTNG_UST_TRACEPOINT;
486 break;
487 default:
488 ERR("Unknown ust instrumentation type (%d)", ev->type);
489 goto error_free_event;
490 }
491
492 /* Copy event name */
493 strncpy(lue->attr.name, ev->name, LTTNG_UST_SYM_NAME_LEN);
494 lue->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
495
496 switch (ev->loglevel_type) {
497 case LTTNG_EVENT_LOGLEVEL_ALL:
498 lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_ALL;
499 lue->attr.loglevel = -1; /* Force to -1 */
500 break;
501 case LTTNG_EVENT_LOGLEVEL_RANGE:
502 lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_RANGE;
503 lue->attr.loglevel = ev->loglevel;
504 break;
505 case LTTNG_EVENT_LOGLEVEL_SINGLE:
506 lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_SINGLE;
507 lue->attr.loglevel = ev->loglevel;
508 break;
509 default:
510 ERR("Unknown ust loglevel type (%d)", ev->loglevel_type);
511 goto error_free_event;
512 }
513
514 /* Same layout. */
515 lue->filter_expression = filter_expression;
516 lue->filter = filter;
517 lue->exclusion = exclusion;
518
519 /* Init node */
520 lttng_ht_node_init_str(&lue->node, lue->attr.name);
521
522 DBG2("Trace UST event %s, loglevel (%d,%d) created",
523 lue->attr.name, lue->attr.loglevel_type,
524 lue->attr.loglevel);
525
526 return lue;
527
528 error_free_event:
529 free(lue);
530 error:
531 free(filter_expression);
532 free(filter);
533 free(exclusion);
534 return NULL;
535 }
536
537 static
538 int trace_ust_context_type_event_to_ust(
539 enum lttng_event_context_type type)
540 {
541 int utype;
542
543 switch (type) {
544 case LTTNG_EVENT_CONTEXT_VTID:
545 utype = LTTNG_UST_CONTEXT_VTID;
546 break;
547 case LTTNG_EVENT_CONTEXT_VPID:
548 utype = LTTNG_UST_CONTEXT_VPID;
549 break;
550 case LTTNG_EVENT_CONTEXT_PTHREAD_ID:
551 utype = LTTNG_UST_CONTEXT_PTHREAD_ID;
552 break;
553 case LTTNG_EVENT_CONTEXT_PROCNAME:
554 utype = LTTNG_UST_CONTEXT_PROCNAME;
555 break;
556 case LTTNG_EVENT_CONTEXT_IP:
557 utype = LTTNG_UST_CONTEXT_IP;
558 break;
559 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
560 if (!ustctl_has_perf_counters()) {
561 utype = -1;
562 WARN("Perf counters not implemented in UST");
563 } else {
564 utype = LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER;
565 }
566 break;
567 case LTTNG_EVENT_CONTEXT_APP_CONTEXT:
568 utype = LTTNG_UST_CONTEXT_APP_CONTEXT;
569 break;
570 default:
571 utype = -1;
572 break;
573 }
574 return utype;
575 }
576
577 /*
578 * Return 1 if contexts match, 0 otherwise.
579 */
580 int trace_ust_match_context(struct ltt_ust_context *uctx,
581 struct lttng_event_context *ctx)
582 {
583 int utype;
584
585 utype = trace_ust_context_type_event_to_ust(ctx->ctx);
586 if (utype < 0) {
587 return 0;
588 }
589 if (uctx->ctx.ctx != utype) {
590 return 0;
591 }
592 switch (utype) {
593 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER:
594 if (uctx->ctx.u.perf_counter.type
595 != ctx->u.perf_counter.type) {
596 return 0;
597 }
598 if (uctx->ctx.u.perf_counter.config
599 != ctx->u.perf_counter.config) {
600 return 0;
601 }
602 if (strncmp(uctx->ctx.u.perf_counter.name,
603 ctx->u.perf_counter.name,
604 LTTNG_UST_SYM_NAME_LEN)) {
605 return 0;
606 }
607 break;
608 case LTTNG_UST_CONTEXT_APP_CONTEXT:
609 assert(uctx->ctx.u.app_ctx.provider_name);
610 assert(uctx->ctx.u.app_ctx.ctx_name);
611 if (strcmp(uctx->ctx.u.app_ctx.provider_name,
612 ctx->u.app_ctx.provider_name) ||
613 strcmp(uctx->ctx.u.app_ctx.ctx_name,
614 ctx->u.app_ctx.ctx_name)) {
615 return 0;
616 }
617 default:
618 break;
619
620 }
621 return 1;
622 }
623
624 /*
625 * Allocate and initialize an UST context.
626 *
627 * Return pointer to structure or NULL.
628 */
629 struct ltt_ust_context *trace_ust_create_context(
630 struct lttng_event_context *ctx)
631 {
632 struct ltt_ust_context *uctx = NULL;
633 int utype;
634
635 assert(ctx);
636
637 utype = trace_ust_context_type_event_to_ust(ctx->ctx);
638 if (utype < 0) {
639 ERR("Invalid UST context");
640 goto end;
641 }
642
643 uctx = zmalloc(sizeof(struct ltt_ust_context));
644 if (!uctx) {
645 PERROR("zmalloc ltt_ust_context");
646 goto end;
647 }
648
649 uctx->ctx.ctx = (enum lttng_ust_context_type) utype;
650 switch (utype) {
651 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER:
652 uctx->ctx.u.perf_counter.type = ctx->u.perf_counter.type;
653 uctx->ctx.u.perf_counter.config = ctx->u.perf_counter.config;
654 strncpy(uctx->ctx.u.perf_counter.name, ctx->u.perf_counter.name,
655 LTTNG_UST_SYM_NAME_LEN);
656 uctx->ctx.u.perf_counter.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
657 break;
658 case LTTNG_UST_CONTEXT_APP_CONTEXT:
659 {
660 char *provider_name = NULL, *ctx_name = NULL;
661
662 provider_name = strdup(ctx->u.app_ctx.provider_name);
663 if (!provider_name) {
664 goto error;
665 }
666 uctx->ctx.u.app_ctx.provider_name = provider_name;
667
668 ctx_name = strdup(ctx->u.app_ctx.ctx_name);
669 if (!ctx_name) {
670 goto error;
671 }
672 uctx->ctx.u.app_ctx.ctx_name = ctx_name;
673 break;
674 }
675 default:
676 break;
677 }
678 lttng_ht_node_init_ulong(&uctx->node, (unsigned long) uctx->ctx.ctx);
679 end:
680 return uctx;
681 error:
682 trace_ust_destroy_context(uctx);
683 return NULL;
684 }
685
686 static
687 void destroy_id_tracker_node_rcu(struct rcu_head *head)
688 {
689 struct ust_id_tracker_node *tracker_node =
690 caa_container_of(head, struct ust_id_tracker_node, node.head);
691 free(tracker_node);
692 }
693
694 static
695 void destroy_id_tracker_node(struct ust_id_tracker_node *tracker_node)
696 {
697
698 call_rcu(&tracker_node->node.head, destroy_id_tracker_node_rcu);
699 }
700
701 static
702 int init_id_tracker(struct ust_id_tracker *id_tracker)
703 {
704 int ret = LTTNG_OK;
705
706 id_tracker->ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
707 if (!id_tracker->ht) {
708 ret = LTTNG_ERR_NOMEM;
709 goto end;
710 }
711
712 end:
713 return ret;
714 }
715
716 /*
717 * Teardown id tracker content, but don't free id_tracker object.
718 */
719 static
720 void fini_id_tracker(struct ust_id_tracker *id_tracker)
721 {
722 struct ust_id_tracker_node *tracker_node;
723 struct lttng_ht_iter iter;
724
725 if (!id_tracker->ht) {
726 return;
727 }
728 rcu_read_lock();
729 cds_lfht_for_each_entry(id_tracker->ht->ht,
730 &iter.iter, tracker_node, node.node) {
731 int ret = lttng_ht_del(id_tracker->ht, &iter);
732
733 assert(!ret);
734 destroy_id_tracker_node(tracker_node);
735 }
736 rcu_read_unlock();
737 ht_cleanup_push(id_tracker->ht);
738 id_tracker->ht = NULL;
739 }
740
741 static
742 struct ust_id_tracker_node *id_tracker_lookup(
743 struct ust_id_tracker *id_tracker, int id,
744 struct lttng_ht_iter *iter)
745 {
746 unsigned long _id = (unsigned long) id;
747 struct lttng_ht_node_ulong *node;
748
749 lttng_ht_lookup(id_tracker->ht, (void *) _id, iter);
750 node = lttng_ht_iter_get_node_ulong(iter);
751 if (node) {
752 return caa_container_of(node, struct ust_id_tracker_node,
753 node);
754 } else {
755 return NULL;
756 }
757 }
758
759 static
760 int id_tracker_add_id(struct ust_id_tracker *id_tracker, int id)
761 {
762 int retval = LTTNG_OK;
763 struct ust_id_tracker_node *tracker_node;
764 struct lttng_ht_iter iter;
765
766 if (id < 0) {
767 retval = LTTNG_ERR_INVALID;
768 goto end;
769 }
770 tracker_node = id_tracker_lookup(id_tracker, id, &iter);
771 if (tracker_node) {
772 /* Already exists. */
773 retval = LTTNG_ERR_ID_TRACKED;
774 goto end;
775 }
776 tracker_node = zmalloc(sizeof(*tracker_node));
777 if (!tracker_node) {
778 retval = LTTNG_ERR_NOMEM;
779 goto end;
780 }
781 lttng_ht_node_init_ulong(&tracker_node->node, (unsigned long) id);
782 lttng_ht_add_unique_ulong(id_tracker->ht, &tracker_node->node);
783 end:
784 return retval;
785 }
786
787 static
788 int id_tracker_del_id(struct ust_id_tracker *id_tracker, int id)
789 {
790 int retval = LTTNG_OK, ret;
791 struct ust_id_tracker_node *tracker_node;
792 struct lttng_ht_iter iter;
793
794 if (id < 0) {
795 retval = LTTNG_ERR_INVALID;
796 goto end;
797 }
798 tracker_node = id_tracker_lookup(id_tracker, id, &iter);
799 if (!tracker_node) {
800 /* Not found */
801 retval = LTTNG_ERR_ID_NOT_TRACKED;
802 goto end;
803 }
804 ret = lttng_ht_del(id_tracker->ht, &iter);
805 assert(!ret);
806
807 destroy_id_tracker_node(tracker_node);
808 end:
809 return retval;
810 }
811
812 static
813 struct ust_id_tracker *get_id_tracker(struct ltt_ust_session *session,
814 enum lttng_tracker_type tracker_type)
815 {
816 switch (tracker_type) {
817 case LTTNG_TRACKER_VPID:
818 return &session->vpid_tracker;
819 case LTTNG_TRACKER_VUID:
820 return &session->vuid_tracker;
821 case LTTNG_TRACKER_VGID:
822 return &session->vgid_tracker;
823 default:
824 return NULL;
825 }
826 }
827
828 static
829 struct lttng_tracker_list *get_id_tracker_list(struct ltt_ust_session *session,
830 enum lttng_tracker_type tracker_type)
831 {
832 switch (tracker_type) {
833 case LTTNG_TRACKER_VPID:
834 return session->tracker_list_vpid;
835 case LTTNG_TRACKER_VUID:
836 return session->tracker_list_vuid;
837 case LTTNG_TRACKER_VGID:
838 return session->tracker_list_vgid;
839 default:
840 return NULL;
841 }
842 }
843
844 /*
845 * The session lock is held when calling this function.
846 */
847 int trace_ust_id_tracker_lookup(enum lttng_tracker_type tracker_type,
848 struct ltt_ust_session *session, int id)
849 {
850 struct lttng_ht_iter iter;
851 struct ust_id_tracker *id_tracker;
852
853 id_tracker = get_id_tracker(session, tracker_type);
854 if (!id_tracker) {
855 abort();
856 }
857 if (!id_tracker->ht) {
858 return 1;
859 }
860 if (id_tracker_lookup(id_tracker, id, &iter)) {
861 return 1;
862 }
863 return 0;
864 }
865
866 /*
867 * Called with the session lock held.
868 */
869 int trace_ust_track_id(enum lttng_tracker_type tracker_type,
870 struct ltt_ust_session *session,
871 struct lttng_tracker_id *id)
872 {
873 int retval = LTTNG_OK;
874 struct ust_id_tracker *id_tracker;
875 struct lttng_tracker_list *tracker_list;
876 int value;
877 struct lttng_tracker_id *saved_ids;
878 ssize_t saved_ids_count, i;
879
880 if (tracker_type == LTTNG_TRACKER_PID) {
881 DBG("Backward compatible behavior: translate PID tracker to VPID tracker for UST domain.");
882 tracker_type = LTTNG_TRACKER_VPID;
883 }
884
885 retval = lttng_tracker_id_lookup_string(tracker_type,
886 id, &value);
887 if (retval != LTTNG_OK) {
888 return retval;
889 }
890 tracker_list = get_id_tracker_list(session, tracker_type);
891 if (!tracker_list) {
892 return LTTNG_ERR_INVALID;
893 }
894 /* Save list for restore on error. */
895 saved_ids_count = lttng_tracker_id_get_list(tracker_list, &saved_ids);
896 if (saved_ids_count < 0) {
897 return LTTNG_ERR_INVALID;
898 }
899 /* Add to list. */
900 retval = lttng_tracker_list_add(tracker_list, id);
901 if (retval != LTTNG_OK) {
902 goto end;
903 }
904
905 id_tracker = get_id_tracker(session, tracker_type);
906 if (!id_tracker) {
907 abort();
908 }
909 if (value == -1) {
910 /* Track all ids: destroy tracker if exists. */
911 if (id_tracker->ht) {
912 fini_id_tracker(id_tracker);
913 /* Ensure all apps have session. */
914 ust_app_global_update_all(session);
915 }
916 } else {
917 if (!id_tracker->ht) {
918 /* Create tracker. */
919 retval = init_id_tracker(id_tracker);
920 if (retval != LTTNG_OK) {
921 ERR("Error initializing ID tracker");
922 goto end_restore;
923 }
924 retval = id_tracker_add_id(id_tracker, value);
925 if (retval != LTTNG_OK) {
926 fini_id_tracker(id_tracker);
927 goto end_restore;
928 }
929 /* Keep only apps matching ID. */
930 ust_app_global_update_all(session);
931 } else {
932 struct ust_app *app;
933
934 retval = id_tracker_add_id(id_tracker, value);
935 if (retval != LTTNG_OK) {
936 goto end_restore;
937 }
938 /* Add session to application */
939 switch (tracker_type) {
940 case LTTNG_TRACKER_VPID:
941 app = ust_app_find_by_pid(value);
942 if (app) {
943 ust_app_global_update(session, app);
944 }
945 break;
946 default:
947 /* Keep only apps matching ID. */
948 ust_app_global_update_all(session);
949 }
950 }
951 }
952 goto end;
953
954 end_restore:
955 if (lttng_tracker_id_set_list(tracker_list, saved_ids, saved_ids_count) != LTTNG_OK) {
956 ERR("Error on tracker add error handling.\n");
957 }
958 end:
959 for (i = 0; i < saved_ids_count; i++) {
960 free(saved_ids[i].string);
961 }
962 free(saved_ids);
963 return retval;
964 }
965
966 /*
967 * Called with the session lock held.
968 */
969 int trace_ust_untrack_id(enum lttng_tracker_type tracker_type,
970 struct ltt_ust_session *session, struct lttng_tracker_id *id)
971 {
972 int retval = LTTNG_OK;
973 struct ust_id_tracker *id_tracker;
974 struct lttng_tracker_list *tracker_list;
975 int value;
976 struct lttng_tracker_id *saved_ids;
977 ssize_t saved_ids_count, i;
978
979 if (tracker_type == LTTNG_TRACKER_PID) {
980 DBG("Backward compatible behavior: translate PID tracker to VPID tracker for UST domain.");
981 tracker_type = LTTNG_TRACKER_VPID;
982 }
983
984 retval = lttng_tracker_id_lookup_string(tracker_type,
985 id, &value);
986 if (retval != LTTNG_OK) {
987 return retval;
988 }
989
990 tracker_list = get_id_tracker_list(session, tracker_type);
991 if (!tracker_list) {
992 return LTTNG_ERR_INVALID;
993 }
994 /* Save list for restore on error. */
995 saved_ids_count = lttng_tracker_id_get_list(tracker_list, &saved_ids);
996 if (saved_ids_count < 0) {
997 return LTTNG_ERR_INVALID;
998 }
999 /* Remove from list. */
1000 retval = lttng_tracker_list_remove(tracker_list, id);
1001 if (retval != LTTNG_OK) {
1002 goto end;
1003 }
1004
1005 id_tracker = get_id_tracker(session, tracker_type);
1006 if (!id_tracker) {
1007 abort();
1008 }
1009
1010 if (value == -1) {
1011 /* Create empty tracker, replace old tracker. */
1012 struct ust_id_tracker tmp_tracker;
1013
1014 tmp_tracker = *id_tracker;
1015 retval = init_id_tracker(id_tracker);
1016 if (retval != LTTNG_OK) {
1017 ERR("Error initializing ID tracker");
1018 /* Rollback operation. */
1019 *id_tracker = tmp_tracker;
1020 goto end_restore;
1021 }
1022 fini_id_tracker(&tmp_tracker);
1023
1024 /* Keep only apps matching ID. */
1025 ust_app_global_update_all(session);
1026 } else {
1027 struct ust_app *app;
1028
1029 if (!id_tracker->ht) {
1030 /* No ID being tracked. */
1031 retval = LTTNG_ERR_ID_NOT_TRACKED;
1032 goto end_restore;
1033 }
1034 /* Remove ID from tracker */
1035 retval = id_tracker_del_id(id_tracker, value);
1036 if (retval != LTTNG_OK) {
1037 goto end_restore;
1038 }
1039 switch (tracker_type) {
1040 case LTTNG_TRACKER_VPID:
1041 /* Remove session from application. */
1042 app = ust_app_find_by_pid(value);
1043 if (app) {
1044 ust_app_global_update(session, app);
1045 }
1046 break;
1047 default:
1048 /* Keep only apps matching ID. */
1049 ust_app_global_update_all(session);
1050 }
1051 }
1052 goto end;
1053
1054 end_restore:
1055 if (lttng_tracker_id_set_list(tracker_list, saved_ids, saved_ids_count) != LTTNG_OK) {
1056 ERR("Error on tracker remove error handling.\n");
1057 }
1058 end:
1059 for (i = 0; i < saved_ids_count; i++) {
1060 free(saved_ids[i].string);
1061 }
1062 free(saved_ids);
1063 return retval;
1064 }
1065
1066 /*
1067 * Called with session lock held.
1068 */
1069 ssize_t trace_ust_list_tracker_ids(enum lttng_tracker_type tracker_type,
1070 struct ltt_ust_session *session,
1071 struct lttng_tracker_id **_ids)
1072 {
1073 struct lttng_tracker_list *tracker_list;
1074
1075 if (tracker_type == LTTNG_TRACKER_PID) {
1076 DBG("Backward compatible behavior: translate PID tracker to VPID tracker for UST domain.");
1077 tracker_type = LTTNG_TRACKER_VPID;
1078 }
1079
1080 tracker_list = get_id_tracker_list(session, tracker_type);
1081 if (!tracker_list) {
1082 return -LTTNG_ERR_INVALID;
1083 }
1084 return lttng_tracker_id_get_list(tracker_list, _ids);
1085 }
1086
1087 /*
1088 * RCU safe free context structure.
1089 */
1090 static void destroy_context_rcu(struct rcu_head *head)
1091 {
1092 struct lttng_ht_node_ulong *node =
1093 caa_container_of(head, struct lttng_ht_node_ulong, head);
1094 struct ltt_ust_context *ctx =
1095 caa_container_of(node, struct ltt_ust_context, node);
1096
1097 trace_ust_destroy_context(ctx);
1098 }
1099
1100 /*
1101 * Cleanup UST context hash table.
1102 */
1103 static void destroy_contexts(struct lttng_ht *ht)
1104 {
1105 int ret;
1106 struct lttng_ht_node_ulong *node;
1107 struct lttng_ht_iter iter;
1108 struct ltt_ust_context *ctx;
1109
1110 assert(ht);
1111
1112 rcu_read_lock();
1113 cds_lfht_for_each_entry(ht->ht, &iter.iter, node, node) {
1114 /* Remove from ordered list. */
1115 ctx = caa_container_of(node, struct ltt_ust_context, node);
1116 cds_list_del(&ctx->list);
1117 /* Remove from channel's hash table. */
1118 ret = lttng_ht_del(ht, &iter);
1119 if (!ret) {
1120 call_rcu(&node->head, destroy_context_rcu);
1121 }
1122 }
1123 rcu_read_unlock();
1124
1125 ht_cleanup_push(ht);
1126 }
1127
1128 /*
1129 * Cleanup ust event structure.
1130 */
1131 void trace_ust_destroy_event(struct ltt_ust_event *event)
1132 {
1133 assert(event);
1134
1135 DBG2("Trace destroy UST event %s", event->attr.name);
1136 free(event->filter_expression);
1137 free(event->filter);
1138 free(event->exclusion);
1139 free(event);
1140 }
1141
1142 /*
1143 * Cleanup ust context structure.
1144 */
1145 void trace_ust_destroy_context(struct ltt_ust_context *ctx)
1146 {
1147 assert(ctx);
1148
1149 if (ctx->ctx.ctx == LTTNG_UST_CONTEXT_APP_CONTEXT) {
1150 free(ctx->ctx.u.app_ctx.provider_name);
1151 free(ctx->ctx.u.app_ctx.ctx_name);
1152 }
1153 free(ctx);
1154 }
1155
1156 /*
1157 * URCU intermediate call to complete destroy event.
1158 */
1159 static void destroy_event_rcu(struct rcu_head *head)
1160 {
1161 struct lttng_ht_node_str *node =
1162 caa_container_of(head, struct lttng_ht_node_str, head);
1163 struct ltt_ust_event *event =
1164 caa_container_of(node, struct ltt_ust_event, node);
1165
1166 trace_ust_destroy_event(event);
1167 }
1168
1169 /*
1170 * Cleanup UST events hashtable.
1171 */
1172 static void destroy_events(struct lttng_ht *events)
1173 {
1174 int ret;
1175 struct lttng_ht_node_str *node;
1176 struct lttng_ht_iter iter;
1177
1178 assert(events);
1179
1180 rcu_read_lock();
1181 cds_lfht_for_each_entry(events->ht, &iter.iter, node, node) {
1182 ret = lttng_ht_del(events, &iter);
1183 assert(!ret);
1184 call_rcu(&node->head, destroy_event_rcu);
1185 }
1186 rcu_read_unlock();
1187
1188 ht_cleanup_push(events);
1189 }
1190
1191 /*
1192 * Cleanup ust channel structure.
1193 *
1194 * Should _NOT_ be called with RCU read lock held.
1195 */
1196 static void _trace_ust_destroy_channel(struct ltt_ust_channel *channel)
1197 {
1198 assert(channel);
1199
1200 DBG2("Trace destroy UST channel %s", channel->name);
1201
1202 free(channel);
1203 }
1204
1205 /*
1206 * URCU intermediate call to complete destroy channel.
1207 */
1208 static void destroy_channel_rcu(struct rcu_head *head)
1209 {
1210 struct lttng_ht_node_str *node =
1211 caa_container_of(head, struct lttng_ht_node_str, head);
1212 struct ltt_ust_channel *channel =
1213 caa_container_of(node, struct ltt_ust_channel, node);
1214
1215 _trace_ust_destroy_channel(channel);
1216 }
1217
1218 void trace_ust_destroy_channel(struct ltt_ust_channel *channel)
1219 {
1220 /* Destroying all events of the channel */
1221 destroy_events(channel->events);
1222 /* Destroying all context of the channel */
1223 destroy_contexts(channel->ctx);
1224
1225 call_rcu(&channel->node.head, destroy_channel_rcu);
1226 }
1227
1228 /*
1229 * Remove an UST channel from a channel HT.
1230 */
1231 void trace_ust_delete_channel(struct lttng_ht *ht,
1232 struct ltt_ust_channel *channel)
1233 {
1234 int ret;
1235 struct lttng_ht_iter iter;
1236
1237 assert(ht);
1238 assert(channel);
1239
1240 iter.iter.node = &channel->node.node;
1241 ret = lttng_ht_del(ht, &iter);
1242 assert(!ret);
1243 }
1244
1245 /*
1246 * Iterate over a hash table containing channels and cleanup safely.
1247 */
1248 static void destroy_channels(struct lttng_ht *channels)
1249 {
1250 struct lttng_ht_node_str *node;
1251 struct lttng_ht_iter iter;
1252
1253 assert(channels);
1254
1255 rcu_read_lock();
1256 cds_lfht_for_each_entry(channels->ht, &iter.iter, node, node) {
1257 struct ltt_ust_channel *chan =
1258 caa_container_of(node, struct ltt_ust_channel, node);
1259
1260 trace_ust_delete_channel(channels, chan);
1261 trace_ust_destroy_channel(chan);
1262 }
1263 rcu_read_unlock();
1264
1265 ht_cleanup_push(channels);
1266 }
1267
1268 /*
1269 * Cleanup UST global domain.
1270 */
1271 static void destroy_domain_global(struct ltt_ust_domain_global *dom)
1272 {
1273 assert(dom);
1274
1275 destroy_channels(dom->channels);
1276 }
1277
1278 /*
1279 * Cleanup ust session structure
1280 *
1281 * Should *NOT* be called with RCU read-side lock held.
1282 */
1283 void trace_ust_destroy_session(struct ltt_ust_session *session)
1284 {
1285 struct agent *agt;
1286 struct buffer_reg_uid *reg, *sreg;
1287 struct lttng_ht_iter iter;
1288
1289 assert(session);
1290
1291 DBG2("Trace UST destroy session %" PRIu64, session->id);
1292
1293 /* Cleaning up UST domain */
1294 destroy_domain_global(&session->domain_global);
1295
1296 rcu_read_lock();
1297 cds_lfht_for_each_entry(session->agents->ht, &iter.iter, agt, node.node) {
1298 int ret = lttng_ht_del(session->agents, &iter);
1299
1300 assert(!ret);
1301 agent_destroy(agt);
1302 }
1303 rcu_read_unlock();
1304
1305 ht_cleanup_push(session->agents);
1306
1307 /* Cleanup UID buffer registry object(s). */
1308 cds_list_for_each_entry_safe(reg, sreg, &session->buffer_reg_uid_list,
1309 lnode) {
1310 cds_list_del(&reg->lnode);
1311 buffer_reg_uid_remove(reg);
1312 buffer_reg_uid_destroy(reg, session->consumer);
1313 }
1314
1315 consumer_output_put(session->consumer);
1316
1317 lttng_tracker_list_destroy(session->tracker_list_vpid);
1318 lttng_tracker_list_destroy(session->tracker_list_vuid);
1319 lttng_tracker_list_destroy(session->tracker_list_vgid);
1320
1321 fini_id_tracker(&session->vpid_tracker);
1322 fini_id_tracker(&session->vuid_tracker);
1323 fini_id_tracker(&session->vgid_tracker);
1324
1325 free(session);
1326 }
This page took 0.095161 seconds and 5 git commands to generate.