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