trackers: update lttng-sessiond
[lttng-tools.git] / src / bin / lttng-sessiond / trace-ust.c
CommitLineData
97ee3a89
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
bdf64013 3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
97ee3a89 4 *
d14d33bf
AM
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.
97ee3a89 8 *
d14d33bf
AM
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.
97ee3a89 13 *
d14d33bf
AM
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.
97ee3a89
DG
17 */
18
6c1c0768 19#define _LGPL_SOURCE
97ee3a89
DG
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <unistd.h>
d9bf3ca4 24#include <inttypes.h>
97ee3a89 25
990570ed
DG
26#include <common/common.h>
27#include <common/defaults.h>
97ee3a89 28
7972aab2 29#include "buffer-registry.h"
97ee3a89 30#include "trace-ust.h"
0b2dc8df 31#include "utils.h"
a9ad0c8f 32#include "ust-app.h"
7c1d2758 33#include "agent.h"
97ee3a89 34
025faf73
DG
35/*
36 * Match function for the events hash table lookup.
37 *
38 * Matches by name only. Used by the disable command.
39 */
18eace3b
DG
40int 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
025faf73 57 /* Match */
18eace3b
DG
58 return 1;
59
60no_match:
61 return 0;
62}
63
025faf73
DG
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 */
18eace3b
DG
70int 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;
2106efa0 74 int ev_loglevel_value;
19a97244 75 int ll_match;
18eace3b
DG
76
77 assert(node);
78 assert(_key);
79
80 event = caa_container_of(node, struct ltt_ust_event, node.node);
81 key = _key;
2106efa0 82 ev_loglevel_value = event->attr.loglevel;
18eace3b 83
f19e9f8b 84 /* Match the 4 elements of the key: name, filter, loglevel, exclusions. */
18eace3b
DG
85
86 /* Event name */
87 if (strncmp(event->attr.name, key->name, sizeof(event->attr.name)) != 0) {
18eace3b
DG
88 goto no_match;
89 }
90
b953b8cd 91 /* Event loglevel value and type. */
19a97244
PP
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) {
b953b8cd 97 goto no_match;
18eace3b
DG
98 }
99
100 /* Only one of the filters is NULL, fail. */
101 if ((key->filter && !event->filter) || (!key->filter && event->filter)) {
18eace3b
DG
102 goto no_match;
103 }
104
025faf73
DG
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 }
18eace3b
DG
112 }
113
f19e9f8b
JI
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) {
a5b7e00c
PP
120 size_t i;
121
122 /* Check exclusion counts first. */
123 if (event->exclusion->count != key->exclusion->count) {
f19e9f8b
JI
124 goto no_match;
125 }
a5b7e00c
PP
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 }
f19e9f8b 160 }
025faf73
DG
161 /* Match. */
162 return 1;
18eace3b
DG
163
164no_match:
165 return 0;
18eace3b
DG
166}
167
0177d773 168/*
2223c96f
DG
169 * Find the channel in the hashtable and return channel pointer. RCU read side
170 * lock MUST be acquired before calling this.
0177d773 171 */
bec39940 172struct ltt_ust_channel *trace_ust_find_channel_by_name(struct lttng_ht *ht,
f6a9efaa 173 char *name)
0177d773 174{
bec39940
DG
175 struct lttng_ht_node_str *node;
176 struct lttng_ht_iter iter;
0177d773 177
85076754
MD
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
bec39940
DG
185 lttng_ht_lookup(ht, (void *)name, &iter);
186 node = lttng_ht_iter_get_node_str(&iter);
f6a9efaa 187 if (node == NULL) {
44d3bd01
DG
188 goto error;
189 }
190
f6a9efaa 191 DBG2("Trace UST channel %s found by name", name);
0177d773 192
f6a9efaa 193 return caa_container_of(node, struct ltt_ust_channel, node);
97ee3a89
DG
194
195error:
f6a9efaa 196 DBG2("Trace UST channel %s not found by name", name);
97ee3a89
DG
197 return NULL;
198}
199
200/*
2223c96f
DG
201 * Find the event in the hashtable and return event pointer. RCU read side lock
202 * MUST be acquired before calling this.
97ee3a89 203 */
18eace3b 204struct ltt_ust_event *trace_ust_find_event(struct lttng_ht *ht,
2106efa0 205 char *name, struct lttng_filter_bytecode *filter,
b953b8cd
PP
206 enum lttng_ust_loglevel_type loglevel_type, int loglevel_value,
207 struct lttng_event_exclusion *exclusion)
97ee3a89 208{
bec39940
DG
209 struct lttng_ht_node_str *node;
210 struct lttng_ht_iter iter;
18eace3b 211 struct ltt_ust_ht_key key;
97ee3a89 212
18eace3b
DG
213 assert(name);
214 assert(ht);
215
216 key.name = name;
217 key.filter = filter;
b953b8cd
PP
218 key.loglevel_type = loglevel_type;
219 key.loglevel_value = loglevel_value;
10646003 220 key.exclusion = exclusion;
18eace3b 221
18eace3b
DG
222 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
223 trace_ust_ht_match_event, &key, &iter.iter);
bec39940 224 node = lttng_ht_iter_get_node_str(&iter);
f6a9efaa 225 if (node == NULL) {
97ee3a89
DG
226 goto error;
227 }
228
18eace3b 229 DBG2("Trace UST event %s found", key.name);
f6a9efaa
DG
230
231 return caa_container_of(node, struct ltt_ust_event, node);
97ee3a89
DG
232
233error:
18eace3b 234 DBG2("Trace UST event %s NOT found", key.name);
97ee3a89
DG
235 return NULL;
236}
237
fefd409b
DG
238/*
239 * Lookup an agent in the session agents hash table by domain type and return
240 * the object if found else NULL.
4da703ad
JG
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.
fefd409b
DG
244 */
245struct 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
266end:
267 return agt;
268}
269
97ee3a89
DG
270/*
271 * Allocate and initialize a ust session data structure.
272 *
273 * Return pointer to structure or NULL.
274 */
d9bf3ca4 275struct ltt_ust_session *trace_ust_create_session(uint64_t session_id)
97ee3a89
DG
276{
277 struct ltt_ust_session *lus;
278
279 /* Allocate a new ltt ust session */
ba7f0ae5 280 lus = zmalloc(sizeof(struct ltt_ust_session));
97ee3a89 281 if (lus == NULL) {
ba7f0ae5 282 PERROR("create ust session zmalloc");
a24248b3 283 goto error_alloc;
97ee3a89
DG
284 }
285
286 /* Init data structure */
a991f516 287 lus->id = session_id;
14fb1ebe 288 lus->active = 0;
97ee3a89 289
84ad93e8
DG
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
7972aab2
DG
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 */
8692d4e5 304 lus->buffer_type = LTTNG_BUFFER_PER_UID;
7972aab2
DG
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);
f6a9efaa
DG
309
310 /* Alloc UST global domain channels' HT */
bec39940 311 lus->domain_global.channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
fefd409b
DG
312 /* Alloc agent hash table. */
313 lus->agents = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
44d3bd01 314
a24248b3
MD
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 }
00e2e675
DG
327 lus->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
328 if (lus->consumer == NULL) {
a24248b3 329 goto error;
00e2e675
DG
330 }
331
44d3bd01
DG
332 DBG2("UST trace session create successful");
333
97ee3a89
DG
334 return lus;
335
a24248b3
MD
336error:
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);
0b2dc8df 340 ht_cleanup_push(lus->domain_global.channels);
fefd409b 341 ht_cleanup_push(lus->agents);
44844c29 342 free(lus);
a24248b3 343error_alloc:
97ee3a89
DG
344 return NULL;
345}
346
347/*
348 * Allocate and initialize a ust channel data structure.
349 *
350 * Return pointer to structure or NULL.
351 */
51755dc8
JG
352struct ltt_ust_channel *trace_ust_create_channel(struct lttng_channel *chan,
353 enum lttng_domain_type domain)
97ee3a89 354{
97ee3a89
DG
355 struct ltt_ust_channel *luc;
356
0525e9ae 357 assert(chan);
0525e9ae 358
37357452 359 luc = zmalloc(sizeof(struct ltt_ust_channel));
97ee3a89 360 if (luc == NULL) {
df0f840b 361 PERROR("ltt_ust_channel zmalloc");
97ee3a89
DG
362 goto error;
363 }
364
51755dc8
JG
365 luc->domain = domain;
366
44d3bd01 367 /* Copy UST channel attributes */
48842b30
DG
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;
6775595e 373 luc->attr.output = (enum lttng_ust_output) chan->attr.output;
82b4ebce
JG
374 luc->monitor_timer_interval = ((struct lttng_channel_extended *)
375 chan->attr.extended.ptr)->monitor_timer_interval;
491d1539
MD
376 luc->attr.u.s.blocking_timeout = ((struct lttng_channel_extended *)
377 chan->attr.extended.ptr)->blocking_timeout;
44d3bd01
DG
378
379 /* Translate to UST output enum */
380 switch (luc->attr.output) {
381 default:
382 luc->attr.output = LTTNG_UST_MMAP;
383 break;
97ee3a89 384 }
97ee3a89 385
85076754
MD
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 }
97ee3a89
DG
396 luc->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
397
f6a9efaa 398 /* Init node */
bec39940 399 lttng_ht_node_init_str(&luc->node, luc->name);
31746f93
DG
400 CDS_INIT_LIST_HEAD(&luc->ctx_list);
401
f6a9efaa 402 /* Alloc hash tables */
bec39940
DG
403 luc->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
404 luc->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
f6a9efaa 405
1624d5b7
JD
406 /* On-disk circular buffer parameters */
407 luc->tracefile_size = chan->attr.tracefile_size;
408 luc->tracefile_count = chan->attr.tracefile_count;
409
f6a9efaa
DG
410 DBG2("Trace UST channel %s created", luc->name);
411
97ee3a89 412error:
7972aab2 413 return luc;
97ee3a89
DG
414}
415
ad11a1d3
PP
416/*
417 * Validates an exclusion list.
418 *
419 * Returns 0 if valid, negative value if invalid.
420 */
421static 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
445end:
446 return ret;
447}
448
97ee3a89
DG
449/*
450 * Allocate and initialize a ust event. Set name and event type.
49d21f93 451 * We own filter_expression, filter, and exclusion.
97ee3a89
DG
452 *
453 * Return pointer to structure or NULL.
454 */
025faf73 455struct ltt_ust_event *trace_ust_create_event(struct lttng_event *ev,
6b453b5e 456 char *filter_expression,
561c6897 457 struct lttng_filter_bytecode *filter,
88f06f15
JG
458 struct lttng_event_exclusion *exclusion,
459 bool internal_event)
97ee3a89
DG
460{
461 struct ltt_ust_event *lue;
97ee3a89 462
0525e9ae
DG
463 assert(ev);
464
ad11a1d3
PP
465 if (exclusion && validate_exclusion(exclusion)) {
466 goto error;
467 }
468
37357452 469 lue = zmalloc(sizeof(struct ltt_ust_event));
44d3bd01 470 if (lue == NULL) {
ba7f0ae5 471 PERROR("ust event zmalloc");
97ee3a89
DG
472 goto error;
473 }
474
88f06f15
JG
475 lue->internal = internal_event;
476
97ee3a89
DG
477 switch (ev->type) {
478 case LTTNG_EVENT_PROBE:
44d3bd01 479 lue->attr.instrumentation = LTTNG_UST_PROBE;
97ee3a89
DG
480 break;
481 case LTTNG_EVENT_FUNCTION:
44d3bd01 482 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
97ee3a89
DG
483 break;
484 case LTTNG_EVENT_FUNCTION_ENTRY:
44d3bd01 485 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
97ee3a89
DG
486 break;
487 case LTTNG_EVENT_TRACEPOINT:
44d3bd01 488 lue->attr.instrumentation = LTTNG_UST_TRACEPOINT;
97ee3a89
DG
489 break;
490 default:
491 ERR("Unknown ust instrumentation type (%d)", ev->type);
44844c29 492 goto error_free_event;
97ee3a89
DG
493 }
494
495 /* Copy event name */
44d3bd01
DG
496 strncpy(lue->attr.name, ev->name, LTTNG_UST_SYM_NAME_LEN);
497 lue->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
97ee3a89 498
0cda4f28 499 switch (ev->loglevel_type) {
8005f29a
MD
500 case LTTNG_EVENT_LOGLEVEL_ALL:
501 lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_ALL;
22e25b71 502 lue->attr.loglevel = -1; /* Force to -1 */
0cda4f28 503 break;
8005f29a
MD
504 case LTTNG_EVENT_LOGLEVEL_RANGE:
505 lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_RANGE;
22e25b71 506 lue->attr.loglevel = ev->loglevel;
8005f29a
MD
507 break;
508 case LTTNG_EVENT_LOGLEVEL_SINGLE:
509 lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_SINGLE;
22e25b71 510 lue->attr.loglevel = ev->loglevel;
0cda4f28
MD
511 break;
512 default:
4431494b 513 ERR("Unknown ust loglevel type (%d)", ev->loglevel_type);
0cda4f28
MD
514 goto error_free_event;
515 }
516
025faf73 517 /* Same layout. */
6b453b5e 518 lue->filter_expression = filter_expression;
51755dc8
JG
519 lue->filter = filter;
520 lue->exclusion = exclusion;
0cda4f28 521
f6a9efaa 522 /* Init node */
bec39940 523 lttng_ht_node_init_str(&lue->node, lue->attr.name);
97ee3a89 524
300b8fd5
MD
525 DBG2("Trace UST event %s, loglevel (%d,%d) created",
526 lue->attr.name, lue->attr.loglevel_type,
527 lue->attr.loglevel);
284d8f55 528
97ee3a89
DG
529 return lue;
530
44844c29
MD
531error_free_event:
532 free(lue);
97ee3a89 533error:
49d21f93
MD
534 free(filter_expression);
535 free(filter);
536 free(exclusion);
97ee3a89
DG
537 return NULL;
538}
539
aa3514e9 540static
bdf64013
JG
541int trace_ust_context_type_event_to_ust(
542 enum lttng_event_context_type type)
55cc08a6 543{
aa3514e9 544 int utype;
0525e9ae 545
aa3514e9 546 switch (type) {
9197c5c4
MD
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;
7c612c2e
WP
559 case LTTNG_EVENT_CONTEXT_IP:
560 utype = LTTNG_UST_CONTEXT_IP;
561 break;
aa3514e9 562 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
354e561b
MD
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 }
aa3514e9 569 break;
bdf64013
JG
570 case LTTNG_EVENT_CONTEXT_APP_CONTEXT:
571 utype = LTTNG_UST_CONTEXT_APP_CONTEXT;
572 break;
9197c5c4 573 default:
aa3514e9
MD
574 utype = -1;
575 break;
576 }
577 return utype;
578}
579
580/*
581 * Return 1 if contexts match, 0 otherwise.
582 */
583int 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;
54fb33cf
JG
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 }
aa3514e9
MD
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 */
632struct ltt_ust_context *trace_ust_create_context(
633 struct lttng_event_context *ctx)
634{
bdf64013 635 struct ltt_ust_context *uctx = NULL;
aa3514e9
MD
636 int utype;
637
638 assert(ctx);
639
640 utype = trace_ust_context_type_event_to_ust(ctx->ctx);
641 if (utype < 0) {
1cf992b8 642 ERR("Invalid UST context");
bdf64013 643 goto end;
9197c5c4 644 }
55cc08a6
DG
645
646 uctx = zmalloc(sizeof(struct ltt_ust_context));
bdf64013 647 if (!uctx) {
55cc08a6 648 PERROR("zmalloc ltt_ust_context");
bdf64013 649 goto end;
55cc08a6
DG
650 }
651
aa3514e9
MD
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;
bdf64013
JG
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 }
aa3514e9
MD
678 default:
679 break;
680 }
bec39940 681 lttng_ht_node_init_ulong(&uctx->node, (unsigned long) uctx->ctx.ctx);
bdf64013 682end:
55cc08a6 683 return uctx;
55cc08a6 684error:
bdf64013 685 trace_ust_destroy_context(uctx);
55cc08a6
DG
686 return NULL;
687}
688
a9ad0c8f 689static
a24248b3 690void destroy_id_tracker_node_rcu(struct rcu_head *head)
a9ad0c8f 691{
a24248b3
MD
692 struct ust_id_tracker_node *tracker_node =
693 caa_container_of(head, struct ust_id_tracker_node, node.head);
a9ad0c8f
MD
694 free(tracker_node);
695}
696
697static
a24248b3 698void destroy_id_tracker_node(struct ust_id_tracker_node *tracker_node)
a9ad0c8f
MD
699{
700
a24248b3 701 call_rcu(&tracker_node->node.head, destroy_id_tracker_node_rcu);
a9ad0c8f
MD
702}
703
704static
a24248b3 705int init_id_tracker(struct ust_id_tracker *id_tracker)
a9ad0c8f 706{
a24248b3 707 int ret = LTTNG_OK;
a9ad0c8f 708
a24248b3
MD
709 id_tracker->ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
710 if (!id_tracker->ht) {
711 ret = LTTNG_ERR_NOMEM;
a9ad0c8f
MD
712 goto end;
713 }
714
715end:
716 return ret;
717}
718
719/*
a24248b3 720 * Teardown id tracker content, but don't free id_tracker object.
a9ad0c8f
MD
721 */
722static
a24248b3 723void fini_id_tracker(struct ust_id_tracker *id_tracker)
a9ad0c8f 724{
a24248b3 725 struct ust_id_tracker_node *tracker_node;
a9ad0c8f
MD
726 struct lttng_ht_iter iter;
727
a24248b3 728 if (!id_tracker->ht) {
a9ad0c8f
MD
729 return;
730 }
731 rcu_read_lock();
a24248b3 732 cds_lfht_for_each_entry(id_tracker->ht->ht,
a9ad0c8f 733 &iter.iter, tracker_node, node.node) {
a24248b3 734 int ret = lttng_ht_del(id_tracker->ht, &iter);
a9ad0c8f
MD
735
736 assert(!ret);
a24248b3 737 destroy_id_tracker_node(tracker_node);
a9ad0c8f
MD
738 }
739 rcu_read_unlock();
a24248b3
MD
740 ht_cleanup_push(id_tracker->ht);
741 id_tracker->ht = NULL;
a9ad0c8f
MD
742}
743
744static
a24248b3
MD
745struct ust_id_tracker_node *id_tracker_lookup(
746 struct ust_id_tracker *id_tracker, int id,
a9ad0c8f
MD
747 struct lttng_ht_iter *iter)
748{
a24248b3 749 unsigned long _id = (unsigned long) id;
a9ad0c8f
MD
750 struct lttng_ht_node_ulong *node;
751
a24248b3 752 lttng_ht_lookup(id_tracker->ht, (void *) _id, iter);
a9ad0c8f
MD
753 node = lttng_ht_iter_get_node_ulong(iter);
754 if (node) {
a24248b3 755 return caa_container_of(node, struct ust_id_tracker_node,
a9ad0c8f
MD
756 node);
757 } else {
758 return NULL;
759 }
760}
761
762static
a24248b3 763int id_tracker_add_id(struct ust_id_tracker *id_tracker, int id)
a9ad0c8f
MD
764{
765 int retval = LTTNG_OK;
a24248b3 766 struct ust_id_tracker_node *tracker_node;
a9ad0c8f
MD
767 struct lttng_ht_iter iter;
768
a24248b3 769 if (id < 0) {
a9ad0c8f
MD
770 retval = LTTNG_ERR_INVALID;
771 goto end;
772 }
a24248b3 773 tracker_node = id_tracker_lookup(id_tracker, id, &iter);
a9ad0c8f
MD
774 if (tracker_node) {
775 /* Already exists. */
a24248b3 776 retval = LTTNG_ERR_ID_TRACKED;
a9ad0c8f
MD
777 goto end;
778 }
779 tracker_node = zmalloc(sizeof(*tracker_node));
780 if (!tracker_node) {
781 retval = LTTNG_ERR_NOMEM;
782 goto end;
783 }
a24248b3
MD
784 lttng_ht_node_init_ulong(&tracker_node->node, (unsigned long) id);
785 lttng_ht_add_unique_ulong(id_tracker->ht, &tracker_node->node);
a9ad0c8f
MD
786end:
787 return retval;
788}
789
790static
a24248b3 791int id_tracker_del_id(struct ust_id_tracker *id_tracker, int id)
a9ad0c8f
MD
792{
793 int retval = LTTNG_OK, ret;
a24248b3 794 struct ust_id_tracker_node *tracker_node;
a9ad0c8f
MD
795 struct lttng_ht_iter iter;
796
a24248b3 797 if (id < 0) {
a9ad0c8f
MD
798 retval = LTTNG_ERR_INVALID;
799 goto end;
800 }
a24248b3 801 tracker_node = id_tracker_lookup(id_tracker, id, &iter);
a9ad0c8f
MD
802 if (!tracker_node) {
803 /* Not found */
a24248b3 804 retval = LTTNG_ERR_ID_NOT_TRACKED;
a9ad0c8f
MD
805 goto end;
806 }
a24248b3 807 ret = lttng_ht_del(id_tracker->ht, &iter);
a9ad0c8f
MD
808 assert(!ret);
809
a24248b3 810 destroy_id_tracker_node(tracker_node);
a9ad0c8f
MD
811end:
812 return retval;
813}
814
a24248b3
MD
815static
816struct 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
831static
832struct 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
a9ad0c8f
MD
847/*
848 * The session lock is held when calling this function.
849 */
a24248b3
MD
850int trace_ust_id_tracker_lookup(enum lttng_tracker_type tracker_type,
851 struct ltt_ust_session *session, int id)
a9ad0c8f
MD
852{
853 struct lttng_ht_iter iter;
a24248b3 854 struct ust_id_tracker *id_tracker;
a9ad0c8f 855
a24248b3
MD
856 id_tracker = get_id_tracker(session, tracker_type);
857 if (!id_tracker) {
858 abort();
859 }
860 if (!id_tracker->ht) {
a9ad0c8f
MD
861 return 1;
862 }
a24248b3 863 if (id_tracker_lookup(id_tracker, id, &iter)) {
a9ad0c8f
MD
864 return 1;
865 }
866 return 0;
867}
868
869/*
870 * Called with the session lock held.
871 */
a24248b3
MD
872int trace_ust_track_id(enum lttng_tracker_type tracker_type,
873 struct ltt_ust_session *session,
874 struct lttng_tracker_id *id)
a9ad0c8f
MD
875{
876 int retval = LTTNG_OK;
a24248b3
MD
877 struct ust_id_tracker *id_tracker;
878 struct lttng_tracker_list *tracker_list;
879 int value;
880
881 if (tracker_type == LTTNG_TRACKER_PID) {
882 DBG("Backward compatible behavior: translate PID tracker to VPID tracker for UST domain.");
883 tracker_type = LTTNG_TRACKER_VPID;
884 }
885
886 retval = lttng_tracker_id_lookup_string(tracker_type,
887 id, &value);
888 if (retval != LTTNG_OK) {
889 return retval;
890 }
891
892 /* Add to list. */
893 tracker_list = get_id_tracker_list(session, tracker_type);
894 if (!tracker_list) {
895 return LTTNG_ERR_INVALID;
896 }
897 retval = lttng_tracker_list_add(tracker_list, id);
898 if (retval != LTTNG_OK) {
899 return retval;
900 }
a9ad0c8f 901
a24248b3
MD
902 id_tracker = get_id_tracker(session, tracker_type);
903 if (!id_tracker) {
904 abort();
905 }
906 if (value == -1) {
907 /* Track all ids: destroy tracker if exists. */
908 if (id_tracker->ht) {
909 fini_id_tracker(id_tracker);
a9ad0c8f
MD
910 /* Ensure all apps have session. */
911 ust_app_global_update_all(session);
912 }
913 } else {
a24248b3 914 if (!id_tracker->ht) {
a9ad0c8f 915 /* Create tracker. */
a24248b3
MD
916 retval = init_id_tracker(id_tracker);
917 if (retval != LTTNG_OK) {
918 ERR("Error initializing ID tracker");
a9ad0c8f
MD
919 goto end;
920 }
a24248b3
MD
921 retval = id_tracker_add_id(id_tracker, value);
922 if (retval != LTTNG_OK) {
923 fini_id_tracker(id_tracker);
a9ad0c8f
MD
924 goto end;
925 }
a24248b3 926 /* Keep only apps matching ID. */
a9ad0c8f
MD
927 ust_app_global_update_all(session);
928 } else {
929 struct ust_app *app;
930
a24248b3
MD
931 retval = id_tracker_add_id(id_tracker, value);
932 if (retval != LTTNG_OK) {
a9ad0c8f
MD
933 goto end;
934 }
935 /* Add session to application */
a24248b3
MD
936 switch (tracker_type) {
937 case LTTNG_TRACKER_VPID:
938 app = ust_app_find_by_pid(value);
939 if (app) {
940 ust_app_global_update(session, app);
941 }
942 break;
943 default:
944 /* Keep only apps matching ID. */
945 ust_app_global_update_all(session);
a9ad0c8f
MD
946 }
947 }
948 }
949end:
950 return retval;
951}
952
953/*
954 * Called with the session lock held.
955 */
a24248b3
MD
956int trace_ust_untrack_id(enum lttng_tracker_type tracker_type,
957 struct ltt_ust_session *session, struct lttng_tracker_id *id)
a9ad0c8f
MD
958{
959 int retval = LTTNG_OK;
a24248b3
MD
960 struct ust_id_tracker *id_tracker;
961 struct lttng_tracker_list *tracker_list;
962 int value;
963
964 if (tracker_type == LTTNG_TRACKER_PID) {
965 DBG("Backward compatible behavior: translate PID tracker to VPID tracker for UST domain.");
966 tracker_type = LTTNG_TRACKER_VPID;
967 }
a9ad0c8f 968
a24248b3
MD
969 retval = lttng_tracker_id_lookup_string(tracker_type,
970 id, &value);
971 if (retval != LTTNG_OK) {
972 return retval;
973 }
974
975 /* Remove from list. */
976 tracker_list = get_id_tracker_list(session, tracker_type);
977 if (!tracker_list) {
978 return LTTNG_ERR_INVALID;
979 }
980 retval = lttng_tracker_list_remove(tracker_list, id);
981 if (retval != LTTNG_OK) {
982 return retval;
983 }
984
985 id_tracker = get_id_tracker(session, tracker_type);
986 if (!id_tracker) {
987 abort();
988 }
989
990 if (value == -1) {
a9ad0c8f 991 /* Create empty tracker, replace old tracker. */
a24248b3 992 struct ust_id_tracker tmp_tracker;
a9ad0c8f 993
a24248b3
MD
994 tmp_tracker = *id_tracker;
995 retval = init_id_tracker(id_tracker);
996 if (retval != LTTNG_OK) {
997 ERR("Error initializing ID tracker");
a9ad0c8f 998 /* Rollback operation. */
a24248b3 999 *id_tracker = tmp_tracker;
a9ad0c8f
MD
1000 goto end;
1001 }
a24248b3 1002 fini_id_tracker(&tmp_tracker);
a9ad0c8f 1003
a24248b3 1004 /* Keep only apps matching ID. */
a9ad0c8f
MD
1005 ust_app_global_update_all(session);
1006 } else {
a9ad0c8f
MD
1007 struct ust_app *app;
1008
a24248b3
MD
1009 if (!id_tracker->ht) {
1010 /* No ID being tracked. */
1011 retval = LTTNG_ERR_ID_NOT_TRACKED;
a9ad0c8f
MD
1012 goto end;
1013 }
a24248b3
MD
1014 /* Remove ID from tracker */
1015 retval = id_tracker_del_id(id_tracker, value);
1016 if (retval != LTTNG_OK) {
a9ad0c8f
MD
1017 goto end;
1018 }
a24248b3
MD
1019 switch (tracker_type) {
1020 case LTTNG_TRACKER_VPID:
1021 /* Remove session from application. */
1022 app = ust_app_find_by_pid(value);
1023 if (app) {
1024 ust_app_global_update(session, app);
1025 }
1026 break;
1027 default:
1028 /* Keep only apps matching ID. */
1029 ust_app_global_update_all(session);
a9ad0c8f
MD
1030 }
1031 }
1032end:
1033 return retval;
1034}
1035
a5dfbb9d
MD
1036/*
1037 * Called with session lock held.
1038 */
a24248b3
MD
1039ssize_t trace_ust_list_tracker_ids(enum lttng_tracker_type tracker_type,
1040 struct ltt_ust_session *session,
1041 struct lttng_tracker_id **_ids)
a5dfbb9d 1042{
a24248b3 1043 struct lttng_tracker_list *tracker_list;
a5dfbb9d 1044
a24248b3
MD
1045 if (tracker_type == LTTNG_TRACKER_PID) {
1046 DBG("Backward compatible behavior: translate PID tracker to VPID tracker for UST domain.");
1047 tracker_type = LTTNG_TRACKER_VPID;
a5dfbb9d
MD
1048 }
1049
a24248b3
MD
1050 tracker_list = get_id_tracker_list(session, tracker_type);
1051 if (!tracker_list) {
1052 return -LTTNG_ERR_INVALID;
a5dfbb9d 1053 }
a24248b3 1054 return lttng_tracker_id_get_list(tracker_list, _ids);
a5dfbb9d
MD
1055}
1056
f6a9efaa
DG
1057/*
1058 * RCU safe free context structure.
1059 */
1060static void destroy_context_rcu(struct rcu_head *head)
1061{
bec39940
DG
1062 struct lttng_ht_node_ulong *node =
1063 caa_container_of(head, struct lttng_ht_node_ulong, head);
f6a9efaa
DG
1064 struct ltt_ust_context *ctx =
1065 caa_container_of(node, struct ltt_ust_context, node);
1066
bdf64013 1067 trace_ust_destroy_context(ctx);
f6a9efaa
DG
1068}
1069
1070/*
1071 * Cleanup UST context hash table.
1072 */
7bd39047 1073static void destroy_contexts(struct lttng_ht *ht)
f6a9efaa
DG
1074{
1075 int ret;
bec39940
DG
1076 struct lttng_ht_node_ulong *node;
1077 struct lttng_ht_iter iter;
31746f93 1078 struct ltt_ust_context *ctx;
f6a9efaa 1079
0525e9ae
DG
1080 assert(ht);
1081
36b588ed 1082 rcu_read_lock();
bec39940 1083 cds_lfht_for_each_entry(ht->ht, &iter.iter, node, node) {
31746f93
DG
1084 /* Remove from ordered list. */
1085 ctx = caa_container_of(node, struct ltt_ust_context, node);
1086 cds_list_del(&ctx->list);
1087 /* Remove from channel's hash table. */
bec39940 1088 ret = lttng_ht_del(ht, &iter);
f6a9efaa
DG
1089 if (!ret) {
1090 call_rcu(&node->head, destroy_context_rcu);
1091 }
f6a9efaa 1092 }
36b588ed 1093 rcu_read_unlock();
ed52805d 1094
0b2dc8df 1095 ht_cleanup_push(ht);
f6a9efaa
DG
1096}
1097
97ee3a89
DG
1098/*
1099 * Cleanup ust event structure.
1100 */
1101void trace_ust_destroy_event(struct ltt_ust_event *event)
1102{
0525e9ae
DG
1103 assert(event);
1104
f6a9efaa 1105 DBG2("Trace destroy UST event %s", event->attr.name);
6b453b5e 1106 free(event->filter_expression);
53a80697 1107 free(event->filter);
40024f8a 1108 free(event->exclusion);
97ee3a89
DG
1109 free(event);
1110}
1111
bdf64013
JG
1112/*
1113 * Cleanup ust context structure.
1114 */
1115void trace_ust_destroy_context(struct ltt_ust_context *ctx)
1116{
1117 assert(ctx);
1118
1119 if (ctx->ctx.ctx == LTTNG_UST_CONTEXT_APP_CONTEXT) {
1120 free(ctx->ctx.u.app_ctx.provider_name);
1121 free(ctx->ctx.u.app_ctx.ctx_name);
1122 }
1123 free(ctx);
1124}
1125
f6a9efaa
DG
1126/*
1127 * URCU intermediate call to complete destroy event.
1128 */
1129static void destroy_event_rcu(struct rcu_head *head)
1130{
bec39940
DG
1131 struct lttng_ht_node_str *node =
1132 caa_container_of(head, struct lttng_ht_node_str, head);
f6a9efaa
DG
1133 struct ltt_ust_event *event =
1134 caa_container_of(node, struct ltt_ust_event, node);
1135
1136 trace_ust_destroy_event(event);
1137}
1138
284d8f55
DG
1139/*
1140 * Cleanup UST events hashtable.
1141 */
7bd39047 1142static void destroy_events(struct lttng_ht *events)
ed52805d
DG
1143{
1144 int ret;
bec39940
DG
1145 struct lttng_ht_node_str *node;
1146 struct lttng_ht_iter iter;
ed52805d 1147
0525e9ae
DG
1148 assert(events);
1149
36b588ed 1150 rcu_read_lock();
bec39940
DG
1151 cds_lfht_for_each_entry(events->ht, &iter.iter, node, node) {
1152 ret = lttng_ht_del(events, &iter);
7bd39047
DG
1153 assert(!ret);
1154 call_rcu(&node->head, destroy_event_rcu);
ed52805d 1155 }
36b588ed 1156 rcu_read_unlock();
ed52805d 1157
0b2dc8df 1158 ht_cleanup_push(events);
ed52805d
DG
1159}
1160
97ee3a89
DG
1161/*
1162 * Cleanup ust channel structure.
36b588ed
MD
1163 *
1164 * Should _NOT_ be called with RCU read lock held.
97ee3a89 1165 */
36b588ed 1166static void _trace_ust_destroy_channel(struct ltt_ust_channel *channel)
97ee3a89 1167{
0525e9ae
DG
1168 assert(channel);
1169
f6a9efaa 1170 DBG2("Trace destroy UST channel %s", channel->name);
97ee3a89 1171
97ee3a89 1172 free(channel);
f6a9efaa
DG
1173}
1174
1175/*
1176 * URCU intermediate call to complete destroy channel.
1177 */
1178static void destroy_channel_rcu(struct rcu_head *head)
1179{
bec39940
DG
1180 struct lttng_ht_node_str *node =
1181 caa_container_of(head, struct lttng_ht_node_str, head);
f6a9efaa
DG
1182 struct ltt_ust_channel *channel =
1183 caa_container_of(node, struct ltt_ust_channel, node);
1184
36b588ed
MD
1185 _trace_ust_destroy_channel(channel);
1186}
1187
1188void trace_ust_destroy_channel(struct ltt_ust_channel *channel)
1189{
627cbbe7
MD
1190 /* Destroying all events of the channel */
1191 destroy_events(channel->events);
1192 /* Destroying all context of the channel */
1193 destroy_contexts(channel->ctx);
1194
36b588ed 1195 call_rcu(&channel->node.head, destroy_channel_rcu);
97ee3a89
DG
1196}
1197
d5979e4a
DG
1198/*
1199 * Remove an UST channel from a channel HT.
1200 */
1201void trace_ust_delete_channel(struct lttng_ht *ht,
1202 struct ltt_ust_channel *channel)
1203{
1204 int ret;
1205 struct lttng_ht_iter iter;
1206
1207 assert(ht);
1208 assert(channel);
1209
1210 iter.iter.node = &channel->node.node;
1211 ret = lttng_ht_del(ht, &iter);
1212 assert(!ret);
1213}
1214
97ee3a89 1215/*
f6a9efaa 1216 * Iterate over a hash table containing channels and cleanup safely.
97ee3a89 1217 */
bec39940 1218static void destroy_channels(struct lttng_ht *channels)
f6a9efaa 1219{
bec39940
DG
1220 struct lttng_ht_node_str *node;
1221 struct lttng_ht_iter iter;
f6a9efaa 1222
0525e9ae
DG
1223 assert(channels);
1224
24d1723f 1225 rcu_read_lock();
bec39940 1226 cds_lfht_for_each_entry(channels->ht, &iter.iter, node, node) {
627cbbe7
MD
1227 struct ltt_ust_channel *chan =
1228 caa_container_of(node, struct ltt_ust_channel, node);
1229
1230 trace_ust_delete_channel(channels, chan);
1231 trace_ust_destroy_channel(chan);
f6a9efaa 1232 }
36b588ed 1233 rcu_read_unlock();
ed52805d 1234
0b2dc8df 1235 ht_cleanup_push(channels);
f6a9efaa
DG
1236}
1237
f6a9efaa
DG
1238/*
1239 * Cleanup UST global domain.
1240 */
1241static void destroy_domain_global(struct ltt_ust_domain_global *dom)
1242{
0525e9ae
DG
1243 assert(dom);
1244
f6a9efaa
DG
1245 destroy_channels(dom->channels);
1246}
97ee3a89 1247
f6a9efaa
DG
1248/*
1249 * Cleanup ust session structure
36b588ed
MD
1250 *
1251 * Should *NOT* be called with RCU read-side lock held.
f6a9efaa
DG
1252 */
1253void trace_ust_destroy_session(struct ltt_ust_session *session)
1254{
fefd409b 1255 struct agent *agt;
7972aab2 1256 struct buffer_reg_uid *reg, *sreg;
fefd409b 1257 struct lttng_ht_iter iter;
7972aab2 1258
0525e9ae 1259 assert(session);
97ee3a89 1260
d9bf3ca4 1261 DBG2("Trace UST destroy session %" PRIu64, session->id);
f6a9efaa 1262
f6a9efaa
DG
1263 /* Cleaning up UST domain */
1264 destroy_domain_global(&session->domain_global);
fefd409b 1265
0c0fcd77 1266 rcu_read_lock();
fefd409b 1267 cds_lfht_for_each_entry(session->agents->ht, &iter.iter, agt, node.node) {
d0edf546
JG
1268 int ret = lttng_ht_del(session->agents, &iter);
1269
1270 assert(!ret);
fefd409b
DG
1271 agent_destroy(agt);
1272 }
0c0fcd77 1273 rcu_read_unlock();
7972aab2 1274
8ada2175
MD
1275 ht_cleanup_push(session->agents);
1276
7972aab2
DG
1277 /* Cleanup UID buffer registry object(s). */
1278 cds_list_for_each_entry_safe(reg, sreg, &session->buffer_reg_uid_list,
1279 lnode) {
1280 cds_list_del(&reg->lnode);
1281 buffer_reg_uid_remove(reg);
1282 buffer_reg_uid_destroy(reg, session->consumer);
1283 }
44d3bd01 1284
6addfa37 1285 consumer_output_put(session->consumer);
3f8e211f 1286
a24248b3
MD
1287 lttng_tracker_list_destroy(session->tracker_list_vpid);
1288 lttng_tracker_list_destroy(session->tracker_list_vuid);
1289 lttng_tracker_list_destroy(session->tracker_list_vgid);
1290
1291 fini_id_tracker(&session->vpid_tracker);
1292 fini_id_tracker(&session->vuid_tracker);
1293 fini_id_tracker(&session->vgid_tracker);
a9ad0c8f 1294
97ee3a89
DG
1295 free(session);
1296}
This page took 0.135963 seconds and 5 git commands to generate.