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