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