Refactoring: introduce lttng_tracker_ids data structure
[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>
82b69413 28#include <common/trace-chunk.h>
97ee3a89 29
7972aab2 30#include "buffer-registry.h"
97ee3a89 31#include "trace-ust.h"
0b2dc8df 32#include "utils.h"
a9ad0c8f 33#include "ust-app.h"
7c1d2758 34#include "agent.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,
df4f5a87 174 const 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");
55c9e7ca 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
55c9e7ca
JR
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) {
55c9e7ca 330 goto error;
00e2e675
DG
331 }
332
44d3bd01
DG
333 DBG2("UST trace session create successful");
334
97ee3a89
DG
335 return lus;
336
55c9e7ca
JR
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);
55c9e7ca 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;
82b4ebce
JG
375 luc->monitor_timer_interval = ((struct lttng_channel_extended *)
376 chan->attr.extended.ptr)->monitor_timer_interval;
491d1539
MD
377 luc->attr.u.s.blocking_timeout = ((struct lttng_channel_extended *)
378 chan->attr.extended.ptr)->blocking_timeout;
44d3bd01
DG
379
380 /* Translate to UST output enum */
381 switch (luc->attr.output) {
382 default:
383 luc->attr.output = LTTNG_UST_MMAP;
384 break;
97ee3a89 385 }
97ee3a89 386
85076754
MD
387 /*
388 * If we receive an empty string for channel name, it means the
389 * default channel name is requested.
390 */
391 if (chan->name[0] == '\0') {
392 strncpy(luc->name, DEFAULT_CHANNEL_NAME, sizeof(luc->name));
393 } else {
394 /* Copy channel name */
395 strncpy(luc->name, chan->name, sizeof(luc->name));
396 }
97ee3a89
DG
397 luc->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
398
f6a9efaa 399 /* Init node */
bec39940 400 lttng_ht_node_init_str(&luc->node, luc->name);
31746f93
DG
401 CDS_INIT_LIST_HEAD(&luc->ctx_list);
402
f6a9efaa 403 /* Alloc hash tables */
bec39940
DG
404 luc->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
405 luc->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
f6a9efaa 406
1624d5b7
JD
407 /* On-disk circular buffer parameters */
408 luc->tracefile_size = chan->attr.tracefile_size;
409 luc->tracefile_count = chan->attr.tracefile_count;
410
f6a9efaa
DG
411 DBG2("Trace UST channel %s created", luc->name);
412
97ee3a89 413error:
7972aab2 414 return luc;
97ee3a89
DG
415}
416
ad11a1d3
PP
417/*
418 * Validates an exclusion list.
419 *
420 * Returns 0 if valid, negative value if invalid.
421 */
422static int validate_exclusion(struct lttng_event_exclusion *exclusion)
423{
424 size_t i;
425 int ret = 0;
426
427 assert(exclusion);
428
429 for (i = 0; i < exclusion->count; ++i) {
430 size_t j;
431 const char *name_a =
432 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, i);
433
434 for (j = 0; j < i; ++j) {
435 const char *name_b =
436 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, j);
437
438 if (!strncmp(name_a, name_b, LTTNG_SYMBOL_NAME_LEN)) {
439 /* Match! */
440 ret = -1;
441 goto end;
442 }
443 }
444 }
445
446end:
447 return ret;
448}
449
97ee3a89
DG
450/*
451 * Allocate and initialize a ust event. Set name and event type.
49d21f93 452 * We own filter_expression, filter, and exclusion.
97ee3a89 453 *
39687410 454 * Return an lttng_error_code
97ee3a89 455 */
39687410 456enum lttng_error_code trace_ust_create_event(struct lttng_event *ev,
6b453b5e 457 char *filter_expression,
561c6897 458 struct lttng_filter_bytecode *filter,
88f06f15 459 struct lttng_event_exclusion *exclusion,
39687410
FD
460 bool internal_event,
461 struct ltt_ust_event **ust_event)
97ee3a89 462{
39687410
FD
463 struct ltt_ust_event *local_ust_event;
464 enum lttng_error_code ret = LTTNG_OK;
97ee3a89 465
0525e9ae
DG
466 assert(ev);
467
ad11a1d3 468 if (exclusion && validate_exclusion(exclusion)) {
39687410 469 ret = LTTNG_ERR_INVALID;
ad11a1d3
PP
470 goto error;
471 }
472
39687410
FD
473 local_ust_event = zmalloc(sizeof(struct ltt_ust_event));
474 if (local_ust_event == NULL) {
ba7f0ae5 475 PERROR("ust event zmalloc");
39687410 476 ret = LTTNG_ERR_NOMEM;
97ee3a89
DG
477 goto error;
478 }
479
39687410 480 local_ust_event->internal = internal_event;
88f06f15 481
97ee3a89
DG
482 switch (ev->type) {
483 case LTTNG_EVENT_PROBE:
39687410 484 local_ust_event->attr.instrumentation = LTTNG_UST_PROBE;
97ee3a89
DG
485 break;
486 case LTTNG_EVENT_FUNCTION:
39687410 487 local_ust_event->attr.instrumentation = LTTNG_UST_FUNCTION;
97ee3a89
DG
488 break;
489 case LTTNG_EVENT_FUNCTION_ENTRY:
39687410 490 local_ust_event->attr.instrumentation = LTTNG_UST_FUNCTION;
97ee3a89
DG
491 break;
492 case LTTNG_EVENT_TRACEPOINT:
39687410 493 local_ust_event->attr.instrumentation = LTTNG_UST_TRACEPOINT;
97ee3a89
DG
494 break;
495 default:
496 ERR("Unknown ust instrumentation type (%d)", ev->type);
39687410 497 ret = LTTNG_ERR_INVALID;
44844c29 498 goto error_free_event;
97ee3a89
DG
499 }
500
501 /* Copy event name */
39687410
FD
502 strncpy(local_ust_event->attr.name, ev->name, LTTNG_UST_SYM_NAME_LEN);
503 local_ust_event->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
97ee3a89 504
0cda4f28 505 switch (ev->loglevel_type) {
8005f29a 506 case LTTNG_EVENT_LOGLEVEL_ALL:
39687410
FD
507 local_ust_event->attr.loglevel_type = LTTNG_UST_LOGLEVEL_ALL;
508 local_ust_event->attr.loglevel = -1; /* Force to -1 */
0cda4f28 509 break;
8005f29a 510 case LTTNG_EVENT_LOGLEVEL_RANGE:
39687410
FD
511 local_ust_event->attr.loglevel_type = LTTNG_UST_LOGLEVEL_RANGE;
512 local_ust_event->attr.loglevel = ev->loglevel;
8005f29a
MD
513 break;
514 case LTTNG_EVENT_LOGLEVEL_SINGLE:
39687410
FD
515 local_ust_event->attr.loglevel_type = LTTNG_UST_LOGLEVEL_SINGLE;
516 local_ust_event->attr.loglevel = ev->loglevel;
0cda4f28
MD
517 break;
518 default:
4431494b 519 ERR("Unknown ust loglevel type (%d)", ev->loglevel_type);
39687410 520 ret = LTTNG_ERR_INVALID;
0cda4f28
MD
521 goto error_free_event;
522 }
523
025faf73 524 /* Same layout. */
39687410
FD
525 local_ust_event->filter_expression = filter_expression;
526 local_ust_event->filter = filter;
527 local_ust_event->exclusion = exclusion;
0cda4f28 528
f6a9efaa 529 /* Init node */
39687410 530 lttng_ht_node_init_str(&local_ust_event->node, local_ust_event->attr.name);
97ee3a89 531
300b8fd5 532 DBG2("Trace UST event %s, loglevel (%d,%d) created",
39687410
FD
533 local_ust_event->attr.name, local_ust_event->attr.loglevel_type,
534 local_ust_event->attr.loglevel);
535
536 *ust_event = local_ust_event;
284d8f55 537
39687410 538 return ret;
97ee3a89 539
44844c29 540error_free_event:
39687410 541 free(local_ust_event);
97ee3a89 542error:
49d21f93
MD
543 free(filter_expression);
544 free(filter);
545 free(exclusion);
39687410 546 return ret;
97ee3a89
DG
547}
548
aa3514e9 549static
bdf64013
JG
550int trace_ust_context_type_event_to_ust(
551 enum lttng_event_context_type type)
55cc08a6 552{
aa3514e9 553 int utype;
0525e9ae 554
aa3514e9 555 switch (type) {
9197c5c4
MD
556 case LTTNG_EVENT_CONTEXT_VTID:
557 utype = LTTNG_UST_CONTEXT_VTID;
558 break;
559 case LTTNG_EVENT_CONTEXT_VPID:
560 utype = LTTNG_UST_CONTEXT_VPID;
561 break;
562 case LTTNG_EVENT_CONTEXT_PTHREAD_ID:
563 utype = LTTNG_UST_CONTEXT_PTHREAD_ID;
564 break;
565 case LTTNG_EVENT_CONTEXT_PROCNAME:
566 utype = LTTNG_UST_CONTEXT_PROCNAME;
567 break;
7c612c2e
WP
568 case LTTNG_EVENT_CONTEXT_IP:
569 utype = LTTNG_UST_CONTEXT_IP;
570 break;
aa3514e9 571 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
354e561b
MD
572 if (!ustctl_has_perf_counters()) {
573 utype = -1;
574 WARN("Perf counters not implemented in UST");
575 } else {
576 utype = LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER;
577 }
aa3514e9 578 break;
bdf64013
JG
579 case LTTNG_EVENT_CONTEXT_APP_CONTEXT:
580 utype = LTTNG_UST_CONTEXT_APP_CONTEXT;
581 break;
f17b8732
MJ
582 case LTTNG_EVENT_CONTEXT_CGROUP_NS:
583 utype = LTTNG_UST_CONTEXT_CGROUP_NS;
584 break;
585 case LTTNG_EVENT_CONTEXT_IPC_NS:
586 utype = LTTNG_UST_CONTEXT_IPC_NS;
587 break;
588 case LTTNG_EVENT_CONTEXT_MNT_NS:
589 utype = LTTNG_UST_CONTEXT_MNT_NS;
590 break;
591 case LTTNG_EVENT_CONTEXT_NET_NS:
592 utype = LTTNG_UST_CONTEXT_NET_NS;
593 break;
594 case LTTNG_EVENT_CONTEXT_PID_NS:
595 utype = LTTNG_UST_CONTEXT_PID_NS;
596 break;
597 case LTTNG_EVENT_CONTEXT_USER_NS:
598 utype = LTTNG_UST_CONTEXT_USER_NS;
599 break;
600 case LTTNG_EVENT_CONTEXT_UTS_NS:
601 utype = LTTNG_UST_CONTEXT_UTS_NS;
602 break;
4fc59cb8
MJ
603 case LTTNG_EVENT_CONTEXT_VUID:
604 utype = LTTNG_UST_CONTEXT_VUID;
605 break;
606 case LTTNG_EVENT_CONTEXT_VEUID:
607 utype = LTTNG_UST_CONTEXT_VEUID;
608 break;
609 case LTTNG_EVENT_CONTEXT_VSUID:
610 utype = LTTNG_UST_CONTEXT_VSUID;
611 break;
612 case LTTNG_EVENT_CONTEXT_VGID:
613 utype = LTTNG_UST_CONTEXT_VGID;
614 break;
615 case LTTNG_EVENT_CONTEXT_VEGID:
616 utype = LTTNG_UST_CONTEXT_VEGID;
617 break;
618 case LTTNG_EVENT_CONTEXT_VSGID:
619 utype = LTTNG_UST_CONTEXT_VSGID;
620 break;
9197c5c4 621 default:
aa3514e9
MD
622 utype = -1;
623 break;
624 }
625 return utype;
626}
627
628/*
629 * Return 1 if contexts match, 0 otherwise.
630 */
df4f5a87
JG
631int trace_ust_match_context(const struct ltt_ust_context *uctx,
632 const struct lttng_event_context *ctx)
aa3514e9
MD
633{
634 int utype;
635
636 utype = trace_ust_context_type_event_to_ust(ctx->ctx);
637 if (utype < 0) {
638 return 0;
639 }
640 if (uctx->ctx.ctx != utype) {
641 return 0;
642 }
643 switch (utype) {
644 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER:
645 if (uctx->ctx.u.perf_counter.type
646 != ctx->u.perf_counter.type) {
647 return 0;
648 }
649 if (uctx->ctx.u.perf_counter.config
650 != ctx->u.perf_counter.config) {
651 return 0;
652 }
653 if (strncmp(uctx->ctx.u.perf_counter.name,
654 ctx->u.perf_counter.name,
655 LTTNG_UST_SYM_NAME_LEN)) {
656 return 0;
657 }
658 break;
54fb33cf
JG
659 case LTTNG_UST_CONTEXT_APP_CONTEXT:
660 assert(uctx->ctx.u.app_ctx.provider_name);
661 assert(uctx->ctx.u.app_ctx.ctx_name);
662 if (strcmp(uctx->ctx.u.app_ctx.provider_name,
663 ctx->u.app_ctx.provider_name) ||
664 strcmp(uctx->ctx.u.app_ctx.ctx_name,
665 ctx->u.app_ctx.ctx_name)) {
666 return 0;
667 }
aa3514e9
MD
668 default:
669 break;
670
671 }
672 return 1;
673}
674
675/*
676 * Allocate and initialize an UST context.
677 *
678 * Return pointer to structure or NULL.
679 */
680struct ltt_ust_context *trace_ust_create_context(
df4f5a87 681 const struct lttng_event_context *ctx)
aa3514e9 682{
bdf64013 683 struct ltt_ust_context *uctx = NULL;
aa3514e9
MD
684 int utype;
685
686 assert(ctx);
687
688 utype = trace_ust_context_type_event_to_ust(ctx->ctx);
689 if (utype < 0) {
1cf992b8 690 ERR("Invalid UST context");
bdf64013 691 goto end;
9197c5c4 692 }
55cc08a6
DG
693
694 uctx = zmalloc(sizeof(struct ltt_ust_context));
bdf64013 695 if (!uctx) {
55cc08a6 696 PERROR("zmalloc ltt_ust_context");
bdf64013 697 goto end;
55cc08a6
DG
698 }
699
aa3514e9
MD
700 uctx->ctx.ctx = (enum lttng_ust_context_type) utype;
701 switch (utype) {
702 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER:
703 uctx->ctx.u.perf_counter.type = ctx->u.perf_counter.type;
704 uctx->ctx.u.perf_counter.config = ctx->u.perf_counter.config;
705 strncpy(uctx->ctx.u.perf_counter.name, ctx->u.perf_counter.name,
706 LTTNG_UST_SYM_NAME_LEN);
707 uctx->ctx.u.perf_counter.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
708 break;
bdf64013
JG
709 case LTTNG_UST_CONTEXT_APP_CONTEXT:
710 {
711 char *provider_name = NULL, *ctx_name = NULL;
712
713 provider_name = strdup(ctx->u.app_ctx.provider_name);
714 if (!provider_name) {
715 goto error;
716 }
717 uctx->ctx.u.app_ctx.provider_name = provider_name;
718
719 ctx_name = strdup(ctx->u.app_ctx.ctx_name);
720 if (!ctx_name) {
721 goto error;
722 }
723 uctx->ctx.u.app_ctx.ctx_name = ctx_name;
724 break;
725 }
aa3514e9
MD
726 default:
727 break;
728 }
bec39940 729 lttng_ht_node_init_ulong(&uctx->node, (unsigned long) uctx->ctx.ctx);
bdf64013 730end:
55cc08a6 731 return uctx;
55cc08a6 732error:
bdf64013 733 trace_ust_destroy_context(uctx);
55cc08a6
DG
734 return NULL;
735}
736
55c9e7ca 737static void destroy_id_tracker_node_rcu(struct rcu_head *head)
a9ad0c8f 738{
55c9e7ca
JR
739 struct ust_id_tracker_node *tracker_node = caa_container_of(
740 head, struct ust_id_tracker_node, node.head);
a9ad0c8f
MD
741 free(tracker_node);
742}
743
55c9e7ca 744static void destroy_id_tracker_node(struct ust_id_tracker_node *tracker_node)
a9ad0c8f 745{
55c9e7ca 746 call_rcu(&tracker_node->node.head, destroy_id_tracker_node_rcu);
a9ad0c8f
MD
747}
748
55c9e7ca 749static int init_id_tracker(struct ust_id_tracker *id_tracker)
a9ad0c8f 750{
55c9e7ca 751 int ret = LTTNG_OK;
a9ad0c8f 752
55c9e7ca
JR
753 id_tracker->ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
754 if (!id_tracker->ht) {
755 ret = LTTNG_ERR_NOMEM;
a9ad0c8f
MD
756 goto end;
757 }
758
759end:
760 return ret;
761}
762
763/*
55c9e7ca 764 * Teardown id tracker content, but don't free id_tracker object.
a9ad0c8f 765 */
55c9e7ca 766static void fini_id_tracker(struct ust_id_tracker *id_tracker)
a9ad0c8f 767{
55c9e7ca 768 struct ust_id_tracker_node *tracker_node;
a9ad0c8f
MD
769 struct lttng_ht_iter iter;
770
55c9e7ca 771 if (!id_tracker->ht) {
a9ad0c8f
MD
772 return;
773 }
774 rcu_read_lock();
55c9e7ca
JR
775 cds_lfht_for_each_entry (id_tracker->ht->ht, &iter.iter, tracker_node,
776 node.node) {
777 int ret = lttng_ht_del(id_tracker->ht, &iter);
a9ad0c8f
MD
778
779 assert(!ret);
55c9e7ca 780 destroy_id_tracker_node(tracker_node);
a9ad0c8f
MD
781 }
782 rcu_read_unlock();
55c9e7ca
JR
783 ht_cleanup_push(id_tracker->ht);
784 id_tracker->ht = NULL;
a9ad0c8f
MD
785}
786
55c9e7ca
JR
787static struct ust_id_tracker_node *id_tracker_lookup(
788 struct ust_id_tracker *id_tracker,
789 int id,
a9ad0c8f
MD
790 struct lttng_ht_iter *iter)
791{
55c9e7ca 792 unsigned long _id = (unsigned long) id;
a9ad0c8f
MD
793 struct lttng_ht_node_ulong *node;
794
55c9e7ca 795 lttng_ht_lookup(id_tracker->ht, (void *) _id, iter);
a9ad0c8f
MD
796 node = lttng_ht_iter_get_node_ulong(iter);
797 if (node) {
55c9e7ca 798 return caa_container_of(node, struct ust_id_tracker_node, node);
a9ad0c8f
MD
799 } else {
800 return NULL;
801 }
802}
803
55c9e7ca 804static int id_tracker_add_id(struct ust_id_tracker *id_tracker, int id)
a9ad0c8f
MD
805{
806 int retval = LTTNG_OK;
55c9e7ca 807 struct ust_id_tracker_node *tracker_node;
a9ad0c8f
MD
808 struct lttng_ht_iter iter;
809
55c9e7ca 810 if (id < 0) {
a9ad0c8f
MD
811 retval = LTTNG_ERR_INVALID;
812 goto end;
813 }
55c9e7ca 814 tracker_node = id_tracker_lookup(id_tracker, id, &iter);
a9ad0c8f
MD
815 if (tracker_node) {
816 /* Already exists. */
55c9e7ca 817 retval = LTTNG_ERR_ID_TRACKED;
a9ad0c8f
MD
818 goto end;
819 }
820 tracker_node = zmalloc(sizeof(*tracker_node));
821 if (!tracker_node) {
822 retval = LTTNG_ERR_NOMEM;
823 goto end;
824 }
55c9e7ca
JR
825 lttng_ht_node_init_ulong(&tracker_node->node, (unsigned long) id);
826 lttng_ht_add_unique_ulong(id_tracker->ht, &tracker_node->node);
a9ad0c8f
MD
827end:
828 return retval;
829}
830
55c9e7ca 831static int id_tracker_del_id(struct ust_id_tracker *id_tracker, int id)
a9ad0c8f
MD
832{
833 int retval = LTTNG_OK, ret;
55c9e7ca 834 struct ust_id_tracker_node *tracker_node;
a9ad0c8f
MD
835 struct lttng_ht_iter iter;
836
55c9e7ca 837 if (id < 0) {
a9ad0c8f
MD
838 retval = LTTNG_ERR_INVALID;
839 goto end;
840 }
55c9e7ca 841 tracker_node = id_tracker_lookup(id_tracker, id, &iter);
a9ad0c8f
MD
842 if (!tracker_node) {
843 /* Not found */
55c9e7ca 844 retval = LTTNG_ERR_ID_NOT_TRACKED;
a9ad0c8f
MD
845 goto end;
846 }
55c9e7ca 847 ret = lttng_ht_del(id_tracker->ht, &iter);
a9ad0c8f
MD
848 assert(!ret);
849
55c9e7ca 850 destroy_id_tracker_node(tracker_node);
a9ad0c8f
MD
851end:
852 return retval;
853}
854
55c9e7ca
JR
855static struct ust_id_tracker *get_id_tracker(struct ltt_ust_session *session,
856 enum lttng_tracker_type tracker_type)
857{
858 switch (tracker_type) {
859 case LTTNG_TRACKER_VPID:
860 return &session->vpid_tracker;
861 case LTTNG_TRACKER_VUID:
862 return &session->vuid_tracker;
863 case LTTNG_TRACKER_VGID:
864 return &session->vgid_tracker;
865 default:
866 return NULL;
867 }
868}
869
870static struct lttng_tracker_list *get_id_tracker_list(
871 struct ltt_ust_session *session,
872 enum lttng_tracker_type tracker_type)
873{
874 switch (tracker_type) {
875 case LTTNG_TRACKER_VPID:
876 return session->tracker_list_vpid;
877 case LTTNG_TRACKER_VUID:
878 return session->tracker_list_vuid;
879 case LTTNG_TRACKER_VGID:
880 return session->tracker_list_vgid;
881 default:
882 return NULL;
883 }
884}
885
a9ad0c8f
MD
886/*
887 * The session lock is held when calling this function.
888 */
55c9e7ca
JR
889int trace_ust_id_tracker_lookup(enum lttng_tracker_type tracker_type,
890 struct ltt_ust_session *session,
891 int id)
a9ad0c8f
MD
892{
893 struct lttng_ht_iter iter;
55c9e7ca 894 struct ust_id_tracker *id_tracker;
a9ad0c8f 895
55c9e7ca
JR
896 id_tracker = get_id_tracker(session, tracker_type);
897 if (!id_tracker) {
898 abort();
899 }
900 if (!id_tracker->ht) {
a9ad0c8f
MD
901 return 1;
902 }
55c9e7ca 903 if (id_tracker_lookup(id_tracker, id, &iter)) {
a9ad0c8f
MD
904 return 1;
905 }
906 return 0;
907}
908
909/*
910 * Called with the session lock held.
911 */
55c9e7ca
JR
912int trace_ust_track_id(enum lttng_tracker_type tracker_type,
913 struct ltt_ust_session *session,
914 const struct lttng_tracker_id *id)
a9ad0c8f
MD
915{
916 int retval = LTTNG_OK;
88e3c2f5 917 bool should_update_apps = false;
55c9e7ca
JR
918 struct ust_id_tracker *id_tracker;
919 struct lttng_tracker_list *tracker_list;
920 int value;
a7a533cd 921 struct lttng_tracker_ids *saved_ids;
55c9e7ca
JR
922
923 if (tracker_type == LTTNG_TRACKER_PID) {
924 DBG("Backward compatible behavior: translate PID tracker to VPID tracker for UST domain.");
925 tracker_type = LTTNG_TRACKER_VPID;
926 }
a9ad0c8f 927
55c9e7ca
JR
928 retval = lttng_tracker_id_lookup_string(tracker_type, id, &value);
929 if (retval != LTTNG_OK) {
930 return retval;
931 }
932 tracker_list = get_id_tracker_list(session, tracker_type);
933 if (!tracker_list) {
934 return LTTNG_ERR_INVALID;
935 }
936 /* Save list for restore on error. */
a7a533cd
JR
937 retval = lttng_tracker_id_get_list(tracker_list, &saved_ids);
938 if (retval != LTTNG_OK) {
55c9e7ca
JR
939 return LTTNG_ERR_INVALID;
940 }
941 /* Add to list. */
942 retval = lttng_tracker_list_add(tracker_list, id);
943 if (retval != LTTNG_OK) {
944 goto end;
945 }
946
947 id_tracker = get_id_tracker(session, tracker_type);
948 if (!id_tracker) {
949 abort();
950 }
951 if (value == -1) {
952 /* Track all ids: destroy tracker if exists. */
953 if (id_tracker->ht) {
954 fini_id_tracker(id_tracker);
a9ad0c8f 955 /* Ensure all apps have session. */
88e3c2f5 956 should_update_apps = true;
a9ad0c8f
MD
957 }
958 } else {
55c9e7ca 959 if (!id_tracker->ht) {
a9ad0c8f 960 /* Create tracker. */
55c9e7ca
JR
961 retval = init_id_tracker(id_tracker);
962 if (retval != LTTNG_OK) {
963 ERR("Error initializing ID tracker");
964 goto end_restore;
a9ad0c8f 965 }
55c9e7ca
JR
966 retval = id_tracker_add_id(id_tracker, value);
967 if (retval != LTTNG_OK) {
968 fini_id_tracker(id_tracker);
969 goto end_restore;
a9ad0c8f
MD
970 }
971 /* Remove all apps from session except pid. */
88e3c2f5 972 should_update_apps = true;
a9ad0c8f
MD
973 } else {
974 struct ust_app *app;
975
55c9e7ca
JR
976 retval = id_tracker_add_id(id_tracker, value);
977 if (retval != LTTNG_OK) {
978 goto end_restore;
a9ad0c8f
MD
979 }
980 /* Add session to application */
55c9e7ca
JR
981 switch (tracker_type) {
982 case LTTNG_TRACKER_VPID:
983 app = ust_app_find_by_pid(value);
984 if (app) {
985 should_update_apps = true;
986 }
987 break;
988 default:
88e3c2f5 989 should_update_apps = true;
a9ad0c8f
MD
990 }
991 }
992 }
88e3c2f5
JG
993 if (should_update_apps && session->active) {
994 ust_app_global_update_all(session);
995 }
55c9e7ca
JR
996 goto end;
997
998end_restore:
a7a533cd 999 if (lttng_tracker_id_set_list(tracker_list, saved_ids) != LTTNG_OK) {
55c9e7ca
JR
1000 ERR("Error on tracker add error handling.\n");
1001 }
a9ad0c8f 1002end:
a7a533cd 1003 lttng_tracker_ids_destroy(saved_ids);
a9ad0c8f
MD
1004 return retval;
1005}
1006
1007/*
1008 * Called with the session lock held.
1009 */
55c9e7ca
JR
1010int trace_ust_untrack_id(enum lttng_tracker_type tracker_type,
1011 struct ltt_ust_session *session,
1012 const struct lttng_tracker_id *id)
a9ad0c8f
MD
1013{
1014 int retval = LTTNG_OK;
a5a30920 1015 bool should_update_apps = false;
55c9e7ca
JR
1016 struct ust_id_tracker *id_tracker;
1017 struct lttng_tracker_list *tracker_list;
1018 int value;
a7a533cd 1019 struct lttng_tracker_ids *saved_ids;
55c9e7ca
JR
1020
1021 if (tracker_type == LTTNG_TRACKER_PID) {
1022 DBG("Backward compatible behavior: translate PID tracker to VPID tracker for UST domain.");
1023 tracker_type = LTTNG_TRACKER_VPID;
1024 }
1025
1026 retval = lttng_tracker_id_lookup_string(tracker_type, id, &value);
1027 if (retval != LTTNG_OK) {
1028 return retval;
1029 }
1030
1031 tracker_list = get_id_tracker_list(session, tracker_type);
1032 if (!tracker_list) {
1033 return LTTNG_ERR_INVALID;
1034 }
1035 /* Save list for restore on error. */
a7a533cd
JR
1036 retval = lttng_tracker_id_get_list(tracker_list, &saved_ids);
1037 if (retval != LTTNG_OK) {
55c9e7ca
JR
1038 return LTTNG_ERR_INVALID;
1039 }
1040 /* Remove from list. */
1041 retval = lttng_tracker_list_remove(tracker_list, id);
1042 if (retval != LTTNG_OK) {
1043 goto end;
1044 }
a9ad0c8f 1045
55c9e7ca
JR
1046 id_tracker = get_id_tracker(session, tracker_type);
1047 if (!id_tracker) {
1048 abort();
1049 }
1050
1051 if (value == -1) {
a9ad0c8f 1052 /* Create empty tracker, replace old tracker. */
55c9e7ca 1053 struct ust_id_tracker tmp_tracker;
a9ad0c8f 1054
55c9e7ca
JR
1055 tmp_tracker = *id_tracker;
1056 retval = init_id_tracker(id_tracker);
1057 if (retval != LTTNG_OK) {
1058 ERR("Error initializing ID tracker");
a9ad0c8f 1059 /* Rollback operation. */
55c9e7ca
JR
1060 *id_tracker = tmp_tracker;
1061 goto end_restore;
a9ad0c8f 1062 }
55c9e7ca 1063 fini_id_tracker(&tmp_tracker);
a9ad0c8f
MD
1064
1065 /* Remove session from all applications */
a5a30920 1066 should_update_apps = true;
a9ad0c8f 1067 } else {
a9ad0c8f
MD
1068 struct ust_app *app;
1069
55c9e7ca
JR
1070 if (!id_tracker->ht) {
1071 /* No ID being tracked. */
1072 retval = LTTNG_ERR_ID_NOT_TRACKED;
1073 goto end_restore;
a9ad0c8f 1074 }
55c9e7ca
JR
1075 /* Remove ID from tracker */
1076 retval = id_tracker_del_id(id_tracker, value);
1077 if (retval != LTTNG_OK) {
1078 goto end_restore;
a9ad0c8f 1079 }
55c9e7ca
JR
1080 switch (tracker_type) {
1081 case LTTNG_TRACKER_VPID:
1082 /* Remove session from application. */
1083 app = ust_app_find_by_pid(value);
1084 if (app) {
1085 should_update_apps = true;
1086 }
1087 break;
1088 default:
1089 /* Keep only apps matching ID. */
a5a30920 1090 should_update_apps = true;
a9ad0c8f
MD
1091 }
1092 }
a5a30920
JR
1093 if (should_update_apps && session->active) {
1094 ust_app_global_update_all(session);
1095 }
55c9e7ca
JR
1096 goto end;
1097
1098end_restore:
a7a533cd 1099 if (lttng_tracker_id_set_list(tracker_list, saved_ids) != LTTNG_OK) {
55c9e7ca
JR
1100 ERR("Error on tracker remove error handling.\n");
1101 }
a9ad0c8f 1102end:
a7a533cd 1103 lttng_tracker_ids_destroy(saved_ids);
a9ad0c8f
MD
1104 return retval;
1105}
1106
a5dfbb9d
MD
1107/*
1108 * Called with session lock held.
1109 */
a7a533cd 1110int trace_ust_list_tracker_ids(enum lttng_tracker_type tracker_type,
55c9e7ca 1111 struct ltt_ust_session *session,
a7a533cd 1112 struct lttng_tracker_ids **_ids)
a5dfbb9d 1113{
a7a533cd 1114 int ret = LTTNG_OK;
55c9e7ca 1115 struct lttng_tracker_list *tracker_list;
a5dfbb9d 1116
55c9e7ca
JR
1117 if (tracker_type == LTTNG_TRACKER_PID) {
1118 DBG("Backward compatible behavior: translate PID tracker to VPID tracker for UST domain.");
1119 tracker_type = LTTNG_TRACKER_VPID;
a5dfbb9d
MD
1120 }
1121
55c9e7ca
JR
1122 tracker_list = get_id_tracker_list(session, tracker_type);
1123 if (!tracker_list) {
a7a533cd
JR
1124 ret = -LTTNG_ERR_INVALID;
1125 goto end;
a5dfbb9d 1126 }
a7a533cd
JR
1127
1128 ret = lttng_tracker_id_get_list(tracker_list, _ids);
1129 if (ret != LTTNG_OK) {
1130 ret = -LTTNG_ERR_INVALID;
1131 goto end;
1132 }
1133end:
1134 return ret;
a5dfbb9d
MD
1135}
1136
f6a9efaa
DG
1137/*
1138 * RCU safe free context structure.
1139 */
1140static void destroy_context_rcu(struct rcu_head *head)
1141{
bec39940
DG
1142 struct lttng_ht_node_ulong *node =
1143 caa_container_of(head, struct lttng_ht_node_ulong, head);
f6a9efaa
DG
1144 struct ltt_ust_context *ctx =
1145 caa_container_of(node, struct ltt_ust_context, node);
1146
bdf64013 1147 trace_ust_destroy_context(ctx);
f6a9efaa
DG
1148}
1149
1150/*
1151 * Cleanup UST context hash table.
1152 */
7bd39047 1153static void destroy_contexts(struct lttng_ht *ht)
f6a9efaa
DG
1154{
1155 int ret;
bec39940
DG
1156 struct lttng_ht_node_ulong *node;
1157 struct lttng_ht_iter iter;
31746f93 1158 struct ltt_ust_context *ctx;
f6a9efaa 1159
0525e9ae
DG
1160 assert(ht);
1161
36b588ed 1162 rcu_read_lock();
bec39940 1163 cds_lfht_for_each_entry(ht->ht, &iter.iter, node, node) {
31746f93
DG
1164 /* Remove from ordered list. */
1165 ctx = caa_container_of(node, struct ltt_ust_context, node);
1166 cds_list_del(&ctx->list);
1167 /* Remove from channel's hash table. */
bec39940 1168 ret = lttng_ht_del(ht, &iter);
f6a9efaa
DG
1169 if (!ret) {
1170 call_rcu(&node->head, destroy_context_rcu);
1171 }
f6a9efaa 1172 }
36b588ed 1173 rcu_read_unlock();
ed52805d 1174
0b2dc8df 1175 ht_cleanup_push(ht);
f6a9efaa
DG
1176}
1177
97ee3a89
DG
1178/*
1179 * Cleanup ust event structure.
1180 */
1181void trace_ust_destroy_event(struct ltt_ust_event *event)
1182{
0525e9ae
DG
1183 assert(event);
1184
f6a9efaa 1185 DBG2("Trace destroy UST event %s", event->attr.name);
6b453b5e 1186 free(event->filter_expression);
53a80697 1187 free(event->filter);
40024f8a 1188 free(event->exclusion);
97ee3a89
DG
1189 free(event);
1190}
1191
bdf64013
JG
1192/*
1193 * Cleanup ust context structure.
1194 */
1195void trace_ust_destroy_context(struct ltt_ust_context *ctx)
1196{
1197 assert(ctx);
1198
1199 if (ctx->ctx.ctx == LTTNG_UST_CONTEXT_APP_CONTEXT) {
1200 free(ctx->ctx.u.app_ctx.provider_name);
1201 free(ctx->ctx.u.app_ctx.ctx_name);
1202 }
1203 free(ctx);
1204}
1205
f6a9efaa
DG
1206/*
1207 * URCU intermediate call to complete destroy event.
1208 */
1209static void destroy_event_rcu(struct rcu_head *head)
1210{
bec39940
DG
1211 struct lttng_ht_node_str *node =
1212 caa_container_of(head, struct lttng_ht_node_str, head);
f6a9efaa
DG
1213 struct ltt_ust_event *event =
1214 caa_container_of(node, struct ltt_ust_event, node);
1215
1216 trace_ust_destroy_event(event);
1217}
1218
284d8f55
DG
1219/*
1220 * Cleanup UST events hashtable.
1221 */
7bd39047 1222static void destroy_events(struct lttng_ht *events)
ed52805d
DG
1223{
1224 int ret;
bec39940
DG
1225 struct lttng_ht_node_str *node;
1226 struct lttng_ht_iter iter;
ed52805d 1227
0525e9ae
DG
1228 assert(events);
1229
36b588ed 1230 rcu_read_lock();
bec39940
DG
1231 cds_lfht_for_each_entry(events->ht, &iter.iter, node, node) {
1232 ret = lttng_ht_del(events, &iter);
7bd39047
DG
1233 assert(!ret);
1234 call_rcu(&node->head, destroy_event_rcu);
ed52805d 1235 }
36b588ed 1236 rcu_read_unlock();
ed52805d 1237
0b2dc8df 1238 ht_cleanup_push(events);
ed52805d
DG
1239}
1240
97ee3a89
DG
1241/*
1242 * Cleanup ust channel structure.
36b588ed
MD
1243 *
1244 * Should _NOT_ be called with RCU read lock held.
97ee3a89 1245 */
36b588ed 1246static void _trace_ust_destroy_channel(struct ltt_ust_channel *channel)
97ee3a89 1247{
0525e9ae
DG
1248 assert(channel);
1249
f6a9efaa 1250 DBG2("Trace destroy UST channel %s", channel->name);
97ee3a89 1251
97ee3a89 1252 free(channel);
f6a9efaa
DG
1253}
1254
1255/*
1256 * URCU intermediate call to complete destroy channel.
1257 */
1258static void destroy_channel_rcu(struct rcu_head *head)
1259{
bec39940
DG
1260 struct lttng_ht_node_str *node =
1261 caa_container_of(head, struct lttng_ht_node_str, head);
f6a9efaa
DG
1262 struct ltt_ust_channel *channel =
1263 caa_container_of(node, struct ltt_ust_channel, node);
1264
36b588ed
MD
1265 _trace_ust_destroy_channel(channel);
1266}
1267
1268void trace_ust_destroy_channel(struct ltt_ust_channel *channel)
1269{
627cbbe7
MD
1270 /* Destroying all events of the channel */
1271 destroy_events(channel->events);
1272 /* Destroying all context of the channel */
1273 destroy_contexts(channel->ctx);
1274
36b588ed 1275 call_rcu(&channel->node.head, destroy_channel_rcu);
97ee3a89
DG
1276}
1277
d5979e4a
DG
1278/*
1279 * Remove an UST channel from a channel HT.
1280 */
1281void trace_ust_delete_channel(struct lttng_ht *ht,
1282 struct ltt_ust_channel *channel)
1283{
1284 int ret;
1285 struct lttng_ht_iter iter;
1286
1287 assert(ht);
1288 assert(channel);
1289
1290 iter.iter.node = &channel->node.node;
1291 ret = lttng_ht_del(ht, &iter);
1292 assert(!ret);
1293}
1294
97ee3a89 1295/*
f6a9efaa 1296 * Iterate over a hash table containing channels and cleanup safely.
97ee3a89 1297 */
bec39940 1298static void destroy_channels(struct lttng_ht *channels)
f6a9efaa 1299{
bec39940
DG
1300 struct lttng_ht_node_str *node;
1301 struct lttng_ht_iter iter;
f6a9efaa 1302
0525e9ae
DG
1303 assert(channels);
1304
24d1723f 1305 rcu_read_lock();
bec39940 1306 cds_lfht_for_each_entry(channels->ht, &iter.iter, node, node) {
627cbbe7
MD
1307 struct ltt_ust_channel *chan =
1308 caa_container_of(node, struct ltt_ust_channel, node);
1309
1310 trace_ust_delete_channel(channels, chan);
1311 trace_ust_destroy_channel(chan);
f6a9efaa 1312 }
36b588ed 1313 rcu_read_unlock();
ed52805d 1314
0b2dc8df 1315 ht_cleanup_push(channels);
f6a9efaa
DG
1316}
1317
f6a9efaa
DG
1318/*
1319 * Cleanup UST global domain.
1320 */
1321static void destroy_domain_global(struct ltt_ust_domain_global *dom)
1322{
0525e9ae
DG
1323 assert(dom);
1324
f6a9efaa
DG
1325 destroy_channels(dom->channels);
1326}
97ee3a89 1327
f6a9efaa 1328/*
d070c424
MD
1329 * Cleanup ust session structure, keeping data required by
1330 * destroy notifier.
36b588ed
MD
1331 *
1332 * Should *NOT* be called with RCU read-side lock held.
f6a9efaa
DG
1333 */
1334void trace_ust_destroy_session(struct ltt_ust_session *session)
1335{
fefd409b 1336 struct agent *agt;
7972aab2 1337 struct buffer_reg_uid *reg, *sreg;
fefd409b 1338 struct lttng_ht_iter iter;
7972aab2 1339
0525e9ae 1340 assert(session);
97ee3a89 1341
d9bf3ca4 1342 DBG2("Trace UST destroy session %" PRIu64, session->id);
f6a9efaa 1343
f6a9efaa
DG
1344 /* Cleaning up UST domain */
1345 destroy_domain_global(&session->domain_global);
fefd409b 1346
0c0fcd77 1347 rcu_read_lock();
fefd409b 1348 cds_lfht_for_each_entry(session->agents->ht, &iter.iter, agt, node.node) {
d0edf546
JG
1349 int ret = lttng_ht_del(session->agents, &iter);
1350
1351 assert(!ret);
fefd409b
DG
1352 agent_destroy(agt);
1353 }
0c0fcd77 1354 rcu_read_unlock();
7972aab2 1355
8ada2175
MD
1356 ht_cleanup_push(session->agents);
1357
7972aab2
DG
1358 /* Cleanup UID buffer registry object(s). */
1359 cds_list_for_each_entry_safe(reg, sreg, &session->buffer_reg_uid_list,
1360 lnode) {
1361 cds_list_del(&reg->lnode);
1362 buffer_reg_uid_remove(reg);
1363 buffer_reg_uid_destroy(reg, session->consumer);
1364 }
44d3bd01 1365
55c9e7ca
JR
1366 lttng_tracker_list_destroy(session->tracker_list_vpid);
1367 lttng_tracker_list_destroy(session->tracker_list_vuid);
1368 lttng_tracker_list_destroy(session->tracker_list_vgid);
1369
1370 fini_id_tracker(&session->vpid_tracker);
1371 fini_id_tracker(&session->vuid_tracker);
1372 fini_id_tracker(&session->vgid_tracker);
82b69413 1373 lttng_trace_chunk_put(session->current_trace_chunk);
d070c424
MD
1374}
1375
1376/* Free elements needed by destroy notifiers. */
1377void trace_ust_free_session(struct ltt_ust_session *session)
1378{
1379 consumer_output_put(session->consumer);
97ee3a89
DG
1380 free(session);
1381}
This page took 0.129088 seconds and 4 git commands to generate.