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"
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;
21b4a71c
MD
880 struct lttng_tracker_id *saved_ids;
881 ssize_t saved_ids_count, i;
a24248b3
MD
882
883 if (tracker_type == LTTNG_TRACKER_PID) {
884 DBG("Backward compatible behavior: translate PID tracker to VPID tracker for UST domain.");
885 tracker_type = LTTNG_TRACKER_VPID;
886 }
887
888 retval = lttng_tracker_id_lookup_string(tracker_type,
889 id, &value);
890 if (retval != LTTNG_OK) {
891 return retval;
892 }
a24248b3
MD
893 tracker_list = get_id_tracker_list(session, tracker_type);
894 if (!tracker_list) {
895 return LTTNG_ERR_INVALID;
896 }
21b4a71c
MD
897 /* Save list for restore on error. */
898 saved_ids_count = lttng_tracker_id_get_list(tracker_list, &saved_ids);
899 if (saved_ids_count < 0) {
900 return LTTNG_ERR_INVALID;
901 }
902 /* Add to list. */
a24248b3
MD
903 retval = lttng_tracker_list_add(tracker_list, id);
904 if (retval != LTTNG_OK) {
21b4a71c 905 goto end;
a24248b3 906 }
a9ad0c8f 907
a24248b3
MD
908 id_tracker = get_id_tracker(session, tracker_type);
909 if (!id_tracker) {
910 abort();
911 }
912 if (value == -1) {
913 /* Track all ids: destroy tracker if exists. */
914 if (id_tracker->ht) {
915 fini_id_tracker(id_tracker);
a9ad0c8f
MD
916 /* Ensure all apps have session. */
917 ust_app_global_update_all(session);
918 }
919 } else {
a24248b3 920 if (!id_tracker->ht) {
a9ad0c8f 921 /* Create tracker. */
a24248b3
MD
922 retval = init_id_tracker(id_tracker);
923 if (retval != LTTNG_OK) {
924 ERR("Error initializing ID tracker");
21b4a71c 925 goto end_restore;
a9ad0c8f 926 }
a24248b3
MD
927 retval = id_tracker_add_id(id_tracker, value);
928 if (retval != LTTNG_OK) {
929 fini_id_tracker(id_tracker);
21b4a71c 930 goto end_restore;
a9ad0c8f 931 }
a24248b3 932 /* Keep only apps matching ID. */
a9ad0c8f
MD
933 ust_app_global_update_all(session);
934 } else {
935 struct ust_app *app;
936
a24248b3
MD
937 retval = id_tracker_add_id(id_tracker, value);
938 if (retval != LTTNG_OK) {
21b4a71c 939 goto end_restore;
a9ad0c8f
MD
940 }
941 /* Add session to application */
a24248b3
MD
942 switch (tracker_type) {
943 case LTTNG_TRACKER_VPID:
944 app = ust_app_find_by_pid(value);
945 if (app) {
946 ust_app_global_update(session, app);
947 }
948 break;
949 default:
950 /* Keep only apps matching ID. */
951 ust_app_global_update_all(session);
a9ad0c8f
MD
952 }
953 }
954 }
21b4a71c
MD
955 goto end;
956
957end_restore:
958 if (lttng_tracker_id_set_list(tracker_list, saved_ids, saved_ids_count) != LTTNG_OK) {
959 ERR("Error on tracker add error handling.\n");
960 }
a9ad0c8f 961end:
21b4a71c
MD
962 for (i = 0; i < saved_ids_count; i++) {
963 free(saved_ids[i].string);
964 }
965 free(saved_ids);
a9ad0c8f
MD
966 return retval;
967}
968
969/*
970 * Called with the session lock held.
971 */
a24248b3
MD
972int trace_ust_untrack_id(enum lttng_tracker_type tracker_type,
973 struct ltt_ust_session *session, struct lttng_tracker_id *id)
a9ad0c8f
MD
974{
975 int retval = LTTNG_OK;
a24248b3
MD
976 struct ust_id_tracker *id_tracker;
977 struct lttng_tracker_list *tracker_list;
978 int value;
21b4a71c
MD
979 struct lttng_tracker_id *saved_ids;
980 ssize_t saved_ids_count, i;
a24248b3
MD
981
982 if (tracker_type == LTTNG_TRACKER_PID) {
983 DBG("Backward compatible behavior: translate PID tracker to VPID tracker for UST domain.");
984 tracker_type = LTTNG_TRACKER_VPID;
985 }
a9ad0c8f 986
a24248b3
MD
987 retval = lttng_tracker_id_lookup_string(tracker_type,
988 id, &value);
989 if (retval != LTTNG_OK) {
990 return retval;
991 }
992
a24248b3
MD
993 tracker_list = get_id_tracker_list(session, tracker_type);
994 if (!tracker_list) {
995 return LTTNG_ERR_INVALID;
996 }
21b4a71c
MD
997 /* Save list for restore on error. */
998 saved_ids_count = lttng_tracker_id_get_list(tracker_list, &saved_ids);
999 if (saved_ids_count < 0) {
1000 return LTTNG_ERR_INVALID;
1001 }
1002 /* Remove from list. */
a24248b3
MD
1003 retval = lttng_tracker_list_remove(tracker_list, id);
1004 if (retval != LTTNG_OK) {
21b4a71c 1005 goto end;
a24248b3
MD
1006 }
1007
1008 id_tracker = get_id_tracker(session, tracker_type);
1009 if (!id_tracker) {
1010 abort();
1011 }
1012
1013 if (value == -1) {
a9ad0c8f 1014 /* Create empty tracker, replace old tracker. */
a24248b3 1015 struct ust_id_tracker tmp_tracker;
a9ad0c8f 1016
a24248b3
MD
1017 tmp_tracker = *id_tracker;
1018 retval = init_id_tracker(id_tracker);
1019 if (retval != LTTNG_OK) {
1020 ERR("Error initializing ID tracker");
a9ad0c8f 1021 /* Rollback operation. */
a24248b3 1022 *id_tracker = tmp_tracker;
21b4a71c 1023 goto end_restore;
a9ad0c8f 1024 }
a24248b3 1025 fini_id_tracker(&tmp_tracker);
a9ad0c8f 1026
a24248b3 1027 /* Keep only apps matching ID. */
a9ad0c8f
MD
1028 ust_app_global_update_all(session);
1029 } else {
a9ad0c8f
MD
1030 struct ust_app *app;
1031
a24248b3
MD
1032 if (!id_tracker->ht) {
1033 /* No ID being tracked. */
1034 retval = LTTNG_ERR_ID_NOT_TRACKED;
21b4a71c 1035 goto end_restore;
a9ad0c8f 1036 }
a24248b3
MD
1037 /* Remove ID from tracker */
1038 retval = id_tracker_del_id(id_tracker, value);
1039 if (retval != LTTNG_OK) {
21b4a71c 1040 goto end_restore;
a9ad0c8f 1041 }
a24248b3
MD
1042 switch (tracker_type) {
1043 case LTTNG_TRACKER_VPID:
1044 /* Remove session from application. */
1045 app = ust_app_find_by_pid(value);
1046 if (app) {
1047 ust_app_global_update(session, app);
1048 }
1049 break;
1050 default:
1051 /* Keep only apps matching ID. */
1052 ust_app_global_update_all(session);
a9ad0c8f
MD
1053 }
1054 }
21b4a71c
MD
1055 goto end;
1056
1057end_restore:
1058 if (lttng_tracker_id_set_list(tracker_list, saved_ids, saved_ids_count) != LTTNG_OK) {
1059 ERR("Error on tracker remove error handling.\n");
1060 }
a9ad0c8f 1061end:
21b4a71c
MD
1062 for (i = 0; i < saved_ids_count; i++) {
1063 free(saved_ids[i].string);
1064 }
1065 free(saved_ids);
a9ad0c8f
MD
1066 return retval;
1067}
1068
a5dfbb9d
MD
1069/*
1070 * Called with session lock held.
1071 */
a24248b3
MD
1072ssize_t trace_ust_list_tracker_ids(enum lttng_tracker_type tracker_type,
1073 struct ltt_ust_session *session,
1074 struct lttng_tracker_id **_ids)
a5dfbb9d 1075{
a24248b3 1076 struct lttng_tracker_list *tracker_list;
a5dfbb9d 1077
a24248b3
MD
1078 if (tracker_type == LTTNG_TRACKER_PID) {
1079 DBG("Backward compatible behavior: translate PID tracker to VPID tracker for UST domain.");
1080 tracker_type = LTTNG_TRACKER_VPID;
a5dfbb9d
MD
1081 }
1082
a24248b3
MD
1083 tracker_list = get_id_tracker_list(session, tracker_type);
1084 if (!tracker_list) {
1085 return -LTTNG_ERR_INVALID;
a5dfbb9d 1086 }
a24248b3 1087 return lttng_tracker_id_get_list(tracker_list, _ids);
a5dfbb9d
MD
1088}
1089
f6a9efaa
DG
1090/*
1091 * RCU safe free context structure.
1092 */
1093static void destroy_context_rcu(struct rcu_head *head)
1094{
bec39940
DG
1095 struct lttng_ht_node_ulong *node =
1096 caa_container_of(head, struct lttng_ht_node_ulong, head);
f6a9efaa
DG
1097 struct ltt_ust_context *ctx =
1098 caa_container_of(node, struct ltt_ust_context, node);
1099
bdf64013 1100 trace_ust_destroy_context(ctx);
f6a9efaa
DG
1101}
1102
1103/*
1104 * Cleanup UST context hash table.
1105 */
7bd39047 1106static void destroy_contexts(struct lttng_ht *ht)
f6a9efaa
DG
1107{
1108 int ret;
bec39940
DG
1109 struct lttng_ht_node_ulong *node;
1110 struct lttng_ht_iter iter;
31746f93 1111 struct ltt_ust_context *ctx;
f6a9efaa 1112
0525e9ae
DG
1113 assert(ht);
1114
36b588ed 1115 rcu_read_lock();
bec39940 1116 cds_lfht_for_each_entry(ht->ht, &iter.iter, node, node) {
31746f93
DG
1117 /* Remove from ordered list. */
1118 ctx = caa_container_of(node, struct ltt_ust_context, node);
1119 cds_list_del(&ctx->list);
1120 /* Remove from channel's hash table. */
bec39940 1121 ret = lttng_ht_del(ht, &iter);
f6a9efaa
DG
1122 if (!ret) {
1123 call_rcu(&node->head, destroy_context_rcu);
1124 }
f6a9efaa 1125 }
36b588ed 1126 rcu_read_unlock();
ed52805d 1127
0b2dc8df 1128 ht_cleanup_push(ht);
f6a9efaa
DG
1129}
1130
97ee3a89
DG
1131/*
1132 * Cleanup ust event structure.
1133 */
1134void trace_ust_destroy_event(struct ltt_ust_event *event)
1135{
0525e9ae
DG
1136 assert(event);
1137
f6a9efaa 1138 DBG2("Trace destroy UST event %s", event->attr.name);
6b453b5e 1139 free(event->filter_expression);
53a80697 1140 free(event->filter);
40024f8a 1141 free(event->exclusion);
97ee3a89
DG
1142 free(event);
1143}
1144
bdf64013
JG
1145/*
1146 * Cleanup ust context structure.
1147 */
1148void trace_ust_destroy_context(struct ltt_ust_context *ctx)
1149{
1150 assert(ctx);
1151
1152 if (ctx->ctx.ctx == LTTNG_UST_CONTEXT_APP_CONTEXT) {
1153 free(ctx->ctx.u.app_ctx.provider_name);
1154 free(ctx->ctx.u.app_ctx.ctx_name);
1155 }
1156 free(ctx);
1157}
1158
f6a9efaa
DG
1159/*
1160 * URCU intermediate call to complete destroy event.
1161 */
1162static void destroy_event_rcu(struct rcu_head *head)
1163{
bec39940
DG
1164 struct lttng_ht_node_str *node =
1165 caa_container_of(head, struct lttng_ht_node_str, head);
f6a9efaa
DG
1166 struct ltt_ust_event *event =
1167 caa_container_of(node, struct ltt_ust_event, node);
1168
1169 trace_ust_destroy_event(event);
1170}
1171
284d8f55
DG
1172/*
1173 * Cleanup UST events hashtable.
1174 */
7bd39047 1175static void destroy_events(struct lttng_ht *events)
ed52805d
DG
1176{
1177 int ret;
bec39940
DG
1178 struct lttng_ht_node_str *node;
1179 struct lttng_ht_iter iter;
ed52805d 1180
0525e9ae
DG
1181 assert(events);
1182
36b588ed 1183 rcu_read_lock();
bec39940
DG
1184 cds_lfht_for_each_entry(events->ht, &iter.iter, node, node) {
1185 ret = lttng_ht_del(events, &iter);
7bd39047
DG
1186 assert(!ret);
1187 call_rcu(&node->head, destroy_event_rcu);
ed52805d 1188 }
36b588ed 1189 rcu_read_unlock();
ed52805d 1190
0b2dc8df 1191 ht_cleanup_push(events);
ed52805d
DG
1192}
1193
97ee3a89
DG
1194/*
1195 * Cleanup ust channel structure.
36b588ed
MD
1196 *
1197 * Should _NOT_ be called with RCU read lock held.
97ee3a89 1198 */
36b588ed 1199static void _trace_ust_destroy_channel(struct ltt_ust_channel *channel)
97ee3a89 1200{
0525e9ae
DG
1201 assert(channel);
1202
f6a9efaa 1203 DBG2("Trace destroy UST channel %s", channel->name);
97ee3a89 1204
97ee3a89 1205 free(channel);
f6a9efaa
DG
1206}
1207
1208/*
1209 * URCU intermediate call to complete destroy channel.
1210 */
1211static void destroy_channel_rcu(struct rcu_head *head)
1212{
bec39940
DG
1213 struct lttng_ht_node_str *node =
1214 caa_container_of(head, struct lttng_ht_node_str, head);
f6a9efaa
DG
1215 struct ltt_ust_channel *channel =
1216 caa_container_of(node, struct ltt_ust_channel, node);
1217
36b588ed
MD
1218 _trace_ust_destroy_channel(channel);
1219}
1220
1221void trace_ust_destroy_channel(struct ltt_ust_channel *channel)
1222{
627cbbe7
MD
1223 /* Destroying all events of the channel */
1224 destroy_events(channel->events);
1225 /* Destroying all context of the channel */
1226 destroy_contexts(channel->ctx);
1227
36b588ed 1228 call_rcu(&channel->node.head, destroy_channel_rcu);
97ee3a89
DG
1229}
1230
d5979e4a
DG
1231/*
1232 * Remove an UST channel from a channel HT.
1233 */
1234void trace_ust_delete_channel(struct lttng_ht *ht,
1235 struct ltt_ust_channel *channel)
1236{
1237 int ret;
1238 struct lttng_ht_iter iter;
1239
1240 assert(ht);
1241 assert(channel);
1242
1243 iter.iter.node = &channel->node.node;
1244 ret = lttng_ht_del(ht, &iter);
1245 assert(!ret);
1246}
1247
97ee3a89 1248/*
f6a9efaa 1249 * Iterate over a hash table containing channels and cleanup safely.
97ee3a89 1250 */
bec39940 1251static void destroy_channels(struct lttng_ht *channels)
f6a9efaa 1252{
bec39940
DG
1253 struct lttng_ht_node_str *node;
1254 struct lttng_ht_iter iter;
f6a9efaa 1255
0525e9ae
DG
1256 assert(channels);
1257
24d1723f 1258 rcu_read_lock();
bec39940 1259 cds_lfht_for_each_entry(channels->ht, &iter.iter, node, node) {
627cbbe7
MD
1260 struct ltt_ust_channel *chan =
1261 caa_container_of(node, struct ltt_ust_channel, node);
1262
1263 trace_ust_delete_channel(channels, chan);
1264 trace_ust_destroy_channel(chan);
f6a9efaa 1265 }
36b588ed 1266 rcu_read_unlock();
ed52805d 1267
0b2dc8df 1268 ht_cleanup_push(channels);
f6a9efaa
DG
1269}
1270
f6a9efaa
DG
1271/*
1272 * Cleanup UST global domain.
1273 */
1274static void destroy_domain_global(struct ltt_ust_domain_global *dom)
1275{
0525e9ae
DG
1276 assert(dom);
1277
f6a9efaa
DG
1278 destroy_channels(dom->channels);
1279}
97ee3a89 1280
f6a9efaa
DG
1281/*
1282 * Cleanup ust session structure
36b588ed
MD
1283 *
1284 * Should *NOT* be called with RCU read-side lock held.
f6a9efaa
DG
1285 */
1286void trace_ust_destroy_session(struct ltt_ust_session *session)
1287{
fefd409b 1288 struct agent *agt;
7972aab2 1289 struct buffer_reg_uid *reg, *sreg;
fefd409b 1290 struct lttng_ht_iter iter;
7972aab2 1291
0525e9ae 1292 assert(session);
97ee3a89 1293
d9bf3ca4 1294 DBG2("Trace UST destroy session %" PRIu64, session->id);
f6a9efaa 1295
f6a9efaa
DG
1296 /* Cleaning up UST domain */
1297 destroy_domain_global(&session->domain_global);
fefd409b 1298
0c0fcd77 1299 rcu_read_lock();
fefd409b 1300 cds_lfht_for_each_entry(session->agents->ht, &iter.iter, agt, node.node) {
d0edf546
JG
1301 int ret = lttng_ht_del(session->agents, &iter);
1302
1303 assert(!ret);
fefd409b
DG
1304 agent_destroy(agt);
1305 }
0c0fcd77 1306 rcu_read_unlock();
7972aab2 1307
8ada2175
MD
1308 ht_cleanup_push(session->agents);
1309
7972aab2
DG
1310 /* Cleanup UID buffer registry object(s). */
1311 cds_list_for_each_entry_safe(reg, sreg, &session->buffer_reg_uid_list,
1312 lnode) {
1313 cds_list_del(&reg->lnode);
1314 buffer_reg_uid_remove(reg);
1315 buffer_reg_uid_destroy(reg, session->consumer);
1316 }
44d3bd01 1317
6addfa37 1318 consumer_output_put(session->consumer);
3f8e211f 1319
a24248b3
MD
1320 lttng_tracker_list_destroy(session->tracker_list_vpid);
1321 lttng_tracker_list_destroy(session->tracker_list_vuid);
1322 lttng_tracker_list_destroy(session->tracker_list_vgid);
1323
1324 fini_id_tracker(&session->vpid_tracker);
1325 fini_id_tracker(&session->vuid_tracker);
1326 fini_id_tracker(&session->vgid_tracker);
a9ad0c8f 1327
97ee3a89
DG
1328 free(session);
1329}
This page took 0.134413 seconds and 5 git commands to generate.