Build fix: missing stdio.h include in signal-helper.hpp
[lttng-tools.git] / src / bin / lttng-sessiond / trace-ust.cpp
CommitLineData
97ee3a89 1/*
21cf9b6b 2 * Copyright (C) 2011 EfficiOS Inc.
ab5be9fa 3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
97ee3a89 4 *
ab5be9fa 5 * SPDX-License-Identifier: GPL-2.0-only
97ee3a89 6 *
97ee3a89
DG
7 */
8
6c1c0768 9#define _LGPL_SOURCE
97ee3a89
DG
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <unistd.h>
d9bf3ca4 14#include <inttypes.h>
97ee3a89 15
c9e313bc
SM
16#include <common/common.hpp>
17#include <common/defaults.hpp>
18#include <common/trace-chunk.hpp>
19#include <common/utils.hpp>
97ee3a89 20
c9e313bc
SM
21#include "buffer-registry.hpp"
22#include "trace-ust.hpp"
23#include "utils.hpp"
24#include "ust-app.hpp"
25#include "agent.hpp"
97ee3a89 26
b0f2e8db
JG
27namespace lsu = lttng::sessiond::ust;
28
025faf73
DG
29/*
30 * Match function for the events hash table lookup.
31 *
32 * Matches by name only. Used by the disable command.
33 */
18eace3b
DG
34int trace_ust_ht_match_event_by_name(struct cds_lfht_node *node,
35 const void *_key)
36{
37 struct ltt_ust_event *event;
38 const char *name;
39
a0377dfe
FD
40 LTTNG_ASSERT(node);
41 LTTNG_ASSERT(_key);
18eace3b
DG
42
43 event = caa_container_of(node, struct ltt_ust_event, node.node);
7966af57 44 name = (const char *) _key;
18eace3b
DG
45
46 /* Event name */
47 if (strncmp(event->attr.name, name, sizeof(event->attr.name)) != 0) {
48 goto no_match;
49 }
50
025faf73 51 /* Match */
18eace3b
DG
52 return 1;
53
54no_match:
55 return 0;
56}
57
025faf73
DG
58/*
59 * Match function for the hash table lookup.
60 *
61 * It matches an ust event based on three attributes which are the event name,
62 * the filter bytecode and the loglevel.
63 */
18eace3b
DG
64int trace_ust_ht_match_event(struct cds_lfht_node *node, const void *_key)
65{
66 struct ltt_ust_event *event;
67 const struct ltt_ust_ht_key *key;
2106efa0 68 int ev_loglevel_value;
19a97244 69 int ll_match;
18eace3b 70
a0377dfe
FD
71 LTTNG_ASSERT(node);
72 LTTNG_ASSERT(_key);
18eace3b
DG
73
74 event = caa_container_of(node, struct ltt_ust_event, node.node);
7966af57 75 key = (ltt_ust_ht_key *) _key;
2106efa0 76 ev_loglevel_value = event->attr.loglevel;
18eace3b 77
f19e9f8b 78 /* Match the 4 elements of the key: name, filter, loglevel, exclusions. */
18eace3b
DG
79
80 /* Event name */
81 if (strncmp(event->attr.name, key->name, sizeof(event->attr.name)) != 0) {
18eace3b
DG
82 goto no_match;
83 }
84
b953b8cd 85 /* Event loglevel value and type. */
19a97244
PP
86 ll_match = loglevels_match(event->attr.loglevel_type,
87 ev_loglevel_value, key->loglevel_type,
fc4b93fa 88 key->loglevel_value, LTTNG_UST_ABI_LOGLEVEL_ALL);
19a97244
PP
89
90 if (!ll_match) {
b953b8cd 91 goto no_match;
18eace3b
DG
92 }
93
94 /* Only one of the filters is NULL, fail. */
95 if ((key->filter && !event->filter) || (!key->filter && event->filter)) {
18eace3b
DG
96 goto no_match;
97 }
98
025faf73
DG
99 if (key->filter && event->filter) {
100 /* Both filters exists, check length followed by the bytecode. */
101 if (event->filter->len != key->filter->len ||
102 memcmp(event->filter->data, key->filter->data,
103 event->filter->len) != 0) {
104 goto no_match;
105 }
18eace3b
DG
106 }
107
f19e9f8b
JI
108 /* If only one of the exclusions is NULL, fail. */
109 if ((key->exclusion && !event->exclusion) || (!key->exclusion && event->exclusion)) {
110 goto no_match;
111 }
112
113 if (key->exclusion && event->exclusion) {
a5b7e00c
PP
114 size_t i;
115
116 /* Check exclusion counts first. */
117 if (event->exclusion->count != key->exclusion->count) {
f19e9f8b
JI
118 goto no_match;
119 }
a5b7e00c
PP
120
121 /* Compare names individually. */
122 for (i = 0; i < event->exclusion->count; ++i) {
123 size_t j;
124 bool found = false;
125 const char *name_ev =
126 LTTNG_EVENT_EXCLUSION_NAME_AT(
127 event->exclusion, i);
128
129 /*
130 * Compare this exclusion name to all the key's
131 * exclusion names.
132 */
133 for (j = 0; j < key->exclusion->count; ++j) {
134 const char *name_key =
135 LTTNG_EVENT_EXCLUSION_NAME_AT(
136 key->exclusion, j);
137
138 if (!strncmp(name_ev, name_key,
139 LTTNG_SYMBOL_NAME_LEN)) {
140 /* Names match! */
141 found = true;
142 break;
143 }
144 }
145
146 /*
147 * If the current exclusion name was not found amongst
148 * the key's exclusion names, then there's no match.
149 */
150 if (!found) {
151 goto no_match;
152 }
153 }
f19e9f8b 154 }
025faf73
DG
155 /* Match. */
156 return 1;
18eace3b
DG
157
158no_match:
159 return 0;
18eace3b
DG
160}
161
0177d773 162/*
2223c96f
DG
163 * Find the channel in the hashtable and return channel pointer. RCU read side
164 * lock MUST be acquired before calling this.
0177d773 165 */
bec39940 166struct ltt_ust_channel *trace_ust_find_channel_by_name(struct lttng_ht *ht,
df4f5a87 167 const char *name)
0177d773 168{
bec39940
DG
169 struct lttng_ht_node_str *node;
170 struct lttng_ht_iter iter;
0177d773 171
48b7cdc2 172 ASSERT_RCU_READ_LOCKED();
85076754
MD
173 /*
174 * If we receive an empty string for channel name, it means the
175 * default channel name is requested.
176 */
177 if (name[0] == '\0')
178 name = DEFAULT_CHANNEL_NAME;
179
bec39940
DG
180 lttng_ht_lookup(ht, (void *)name, &iter);
181 node = lttng_ht_iter_get_node_str(&iter);
f6a9efaa 182 if (node == NULL) {
44d3bd01
DG
183 goto error;
184 }
185
f6a9efaa 186 DBG2("Trace UST channel %s found by name", name);
0177d773 187
0114db0e 188 return lttng::utils::container_of(node, &ltt_ust_channel::node);
97ee3a89
DG
189
190error:
f6a9efaa 191 DBG2("Trace UST channel %s not found by name", name);
97ee3a89
DG
192 return NULL;
193}
194
195/*
2223c96f
DG
196 * Find the event in the hashtable and return event pointer. RCU read side lock
197 * MUST be acquired before calling this.
97ee3a89 198 */
18eace3b 199struct ltt_ust_event *trace_ust_find_event(struct lttng_ht *ht,
2b00d462 200 char *name, struct lttng_bytecode *filter,
fc4b93fa 201 enum lttng_ust_abi_loglevel_type loglevel_type, int loglevel_value,
b953b8cd 202 struct lttng_event_exclusion *exclusion)
97ee3a89 203{
bec39940
DG
204 struct lttng_ht_node_str *node;
205 struct lttng_ht_iter iter;
18eace3b 206 struct ltt_ust_ht_key key;
97ee3a89 207
a0377dfe
FD
208 LTTNG_ASSERT(name);
209 LTTNG_ASSERT(ht);
48b7cdc2 210 ASSERT_RCU_READ_LOCKED();
18eace3b
DG
211
212 key.name = name;
213 key.filter = filter;
b953b8cd
PP
214 key.loglevel_type = loglevel_type;
215 key.loglevel_value = loglevel_value;
10646003 216 key.exclusion = exclusion;
18eace3b 217
18eace3b
DG
218 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
219 trace_ust_ht_match_event, &key, &iter.iter);
bec39940 220 node = lttng_ht_iter_get_node_str(&iter);
f6a9efaa 221 if (node == NULL) {
97ee3a89
DG
222 goto error;
223 }
224
18eace3b 225 DBG2("Trace UST event %s found", key.name);
f6a9efaa 226
0114db0e 227 return lttng::utils::container_of(node, &ltt_ust_event::node);
97ee3a89
DG
228
229error:
18eace3b 230 DBG2("Trace UST event %s NOT found", key.name);
97ee3a89
DG
231 return NULL;
232}
233
fefd409b
DG
234/*
235 * Lookup an agent in the session agents hash table by domain type and return
236 * the object if found else NULL.
4da703ad
JG
237 *
238 * RCU read side lock must be acquired before calling and only released
239 * once the agent is no longer in scope or being used.
fefd409b
DG
240 */
241struct agent *trace_ust_find_agent(struct ltt_ust_session *session,
242 enum lttng_domain_type domain_type)
243{
244 struct agent *agt = NULL;
245 struct lttng_ht_node_u64 *node;
246 struct lttng_ht_iter iter;
247 uint64_t key;
248
a0377dfe 249 LTTNG_ASSERT(session);
fefd409b
DG
250
251 DBG3("Trace ust agent lookup for domain %d", domain_type);
252
253 key = domain_type;
254
255 lttng_ht_lookup(session->agents, &key, &iter);
256 node = lttng_ht_iter_get_node_u64(&iter);
257 if (!node) {
258 goto end;
259 }
0114db0e 260 agt = lttng::utils::container_of(node, &agent::node);
fefd409b
DG
261
262end:
263 return agt;
264}
265
97ee3a89
DG
266/*
267 * Allocate and initialize a ust session data structure.
268 *
269 * Return pointer to structure or NULL.
270 */
d9bf3ca4 271struct ltt_ust_session *trace_ust_create_session(uint64_t session_id)
97ee3a89
DG
272{
273 struct ltt_ust_session *lus;
274
275 /* Allocate a new ltt ust session */
64803277 276 lus = zmalloc<ltt_ust_session>();
97ee3a89 277 if (lus == NULL) {
ba7f0ae5 278 PERROR("create ust session zmalloc");
55c9e7ca 279 goto error_alloc;
97ee3a89
DG
280 }
281
282 /* Init data structure */
a991f516 283 lus->id = session_id;
14fb1ebe 284 lus->active = 0;
97ee3a89 285
84ad93e8
DG
286 /* Set default metadata channel attribute. */
287 lus->metadata_attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
288 lus->metadata_attr.subbuf_size = default_get_metadata_subbuf_size();
289 lus->metadata_attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
290 lus->metadata_attr.switch_timer_interval = DEFAULT_METADATA_SWITCH_TIMER;
291 lus->metadata_attr.read_timer_interval = DEFAULT_METADATA_READ_TIMER;
fc4b93fa 292 lus->metadata_attr.output = LTTNG_UST_ABI_MMAP;
84ad93e8 293
7972aab2
DG
294 /*
295 * Default buffer type. This can be changed through an enable channel
296 * requesting a different type. Note that this can only be changed once
297 * during the session lifetime which is at the first enable channel and
298 * only before start. The flag buffer_type_changed indicates the status.
299 */
8692d4e5 300 lus->buffer_type = LTTNG_BUFFER_PER_UID;
7972aab2
DG
301 /* Once set to 1, the buffer_type is immutable for the session. */
302 lus->buffer_type_changed = 0;
303 /* Init it in case it get used after allocation. */
304 CDS_INIT_LIST_HEAD(&lus->buffer_reg_uid_list);
f6a9efaa
DG
305
306 /* Alloc UST global domain channels' HT */
bec39940 307 lus->domain_global.channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
fefd409b
DG
308 /* Alloc agent hash table. */
309 lus->agents = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
44d3bd01 310
159b042f
JG
311 lus->tracker_vpid = process_attr_tracker_create();
312 if (!lus->tracker_vpid) {
55c9e7ca
JR
313 goto error;
314 }
159b042f
JG
315 lus->tracker_vuid = process_attr_tracker_create();
316 if (!lus->tracker_vuid) {
55c9e7ca
JR
317 goto error;
318 }
159b042f
JG
319 lus->tracker_vgid = process_attr_tracker_create();
320 if (!lus->tracker_vgid) {
55c9e7ca
JR
321 goto error;
322 }
00e2e675
DG
323 lus->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
324 if (lus->consumer == NULL) {
55c9e7ca 325 goto error;
00e2e675
DG
326 }
327
44d3bd01
DG
328 DBG2("UST trace session create successful");
329
97ee3a89
DG
330 return lus;
331
55c9e7ca 332error:
159b042f
JG
333 process_attr_tracker_destroy(lus->tracker_vpid);
334 process_attr_tracker_destroy(lus->tracker_vuid);
335 process_attr_tracker_destroy(lus->tracker_vgid);
3c339053
FD
336 lttng_ht_destroy(lus->domain_global.channels);
337 lttng_ht_destroy(lus->agents);
44844c29 338 free(lus);
55c9e7ca 339error_alloc:
97ee3a89
DG
340 return NULL;
341}
342
343/*
344 * Allocate and initialize a ust channel data structure.
345 *
346 * Return pointer to structure or NULL.
347 */
51755dc8
JG
348struct ltt_ust_channel *trace_ust_create_channel(struct lttng_channel *chan,
349 enum lttng_domain_type domain)
97ee3a89 350{
97ee3a89
DG
351 struct ltt_ust_channel *luc;
352
a0377dfe 353 LTTNG_ASSERT(chan);
0525e9ae 354
64803277 355 luc = zmalloc<ltt_ust_channel>();
97ee3a89 356 if (luc == NULL) {
df0f840b 357 PERROR("ltt_ust_channel zmalloc");
97ee3a89
DG
358 goto error;
359 }
360
51755dc8
JG
361 luc->domain = domain;
362
44d3bd01 363 /* Copy UST channel attributes */
48842b30
DG
364 luc->attr.overwrite = chan->attr.overwrite;
365 luc->attr.subbuf_size = chan->attr.subbuf_size;
366 luc->attr.num_subbuf = chan->attr.num_subbuf;
367 luc->attr.switch_timer_interval = chan->attr.switch_timer_interval;
368 luc->attr.read_timer_interval = chan->attr.read_timer_interval;
fc4b93fa 369 luc->attr.output = (enum lttng_ust_abi_output) chan->attr.output;
82b4ebce
JG
370 luc->monitor_timer_interval = ((struct lttng_channel_extended *)
371 chan->attr.extended.ptr)->monitor_timer_interval;
491d1539
MD
372 luc->attr.u.s.blocking_timeout = ((struct lttng_channel_extended *)
373 chan->attr.extended.ptr)->blocking_timeout;
44d3bd01
DG
374
375 /* Translate to UST output enum */
376 switch (luc->attr.output) {
377 default:
fc4b93fa 378 luc->attr.output = LTTNG_UST_ABI_MMAP;
44d3bd01 379 break;
97ee3a89 380 }
97ee3a89 381
85076754
MD
382 /*
383 * If we receive an empty string for channel name, it means the
384 * default channel name is requested.
385 */
386 if (chan->name[0] == '\0') {
387 strncpy(luc->name, DEFAULT_CHANNEL_NAME, sizeof(luc->name));
388 } else {
389 /* Copy channel name */
390 strncpy(luc->name, chan->name, sizeof(luc->name));
391 }
fc4b93fa 392 luc->name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] = '\0';
97ee3a89 393
f6a9efaa 394 /* Init node */
bec39940 395 lttng_ht_node_init_str(&luc->node, luc->name);
31746f93
DG
396 CDS_INIT_LIST_HEAD(&luc->ctx_list);
397
f6a9efaa 398 /* Alloc hash tables */
bec39940
DG
399 luc->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
400 luc->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
f6a9efaa 401
1624d5b7
JD
402 /* On-disk circular buffer parameters */
403 luc->tracefile_size = chan->attr.tracefile_size;
404 luc->tracefile_count = chan->attr.tracefile_count;
405
f6a9efaa
DG
406 DBG2("Trace UST channel %s created", luc->name);
407
97ee3a89 408error:
7972aab2 409 return luc;
97ee3a89
DG
410}
411
ad11a1d3
PP
412/*
413 * Validates an exclusion list.
414 *
415 * Returns 0 if valid, negative value if invalid.
416 */
417static int validate_exclusion(struct lttng_event_exclusion *exclusion)
418{
419 size_t i;
420 int ret = 0;
421
a0377dfe 422 LTTNG_ASSERT(exclusion);
ad11a1d3
PP
423
424 for (i = 0; i < exclusion->count; ++i) {
425 size_t j;
426 const char *name_a =
427 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, i);
428
429 for (j = 0; j < i; ++j) {
430 const char *name_b =
431 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, j);
432
433 if (!strncmp(name_a, name_b, LTTNG_SYMBOL_NAME_LEN)) {
434 /* Match! */
435 ret = -1;
436 goto end;
437 }
438 }
439 }
440
441end:
442 return ret;
443}
444
97ee3a89
DG
445/*
446 * Allocate and initialize a ust event. Set name and event type.
49d21f93 447 * We own filter_expression, filter, and exclusion.
97ee3a89 448 *
39687410 449 * Return an lttng_error_code
97ee3a89 450 */
39687410 451enum lttng_error_code trace_ust_create_event(struct lttng_event *ev,
6b453b5e 452 char *filter_expression,
2b00d462 453 struct lttng_bytecode *filter,
88f06f15 454 struct lttng_event_exclusion *exclusion,
39687410
FD
455 bool internal_event,
456 struct ltt_ust_event **ust_event)
97ee3a89 457{
39687410
FD
458 struct ltt_ust_event *local_ust_event;
459 enum lttng_error_code ret = LTTNG_OK;
97ee3a89 460
a0377dfe 461 LTTNG_ASSERT(ev);
0525e9ae 462
ad11a1d3 463 if (exclusion && validate_exclusion(exclusion)) {
39687410 464 ret = LTTNG_ERR_INVALID;
ad11a1d3
PP
465 goto error;
466 }
467
64803277 468 local_ust_event = zmalloc<ltt_ust_event>();
39687410 469 if (local_ust_event == NULL) {
ba7f0ae5 470 PERROR("ust event zmalloc");
39687410 471 ret = LTTNG_ERR_NOMEM;
97ee3a89
DG
472 goto error;
473 }
474
39687410 475 local_ust_event->internal = internal_event;
88f06f15 476
97ee3a89
DG
477 switch (ev->type) {
478 case LTTNG_EVENT_PROBE:
fc4b93fa 479 local_ust_event->attr.instrumentation = LTTNG_UST_ABI_PROBE;
97ee3a89
DG
480 break;
481 case LTTNG_EVENT_FUNCTION:
fc4b93fa 482 local_ust_event->attr.instrumentation = LTTNG_UST_ABI_FUNCTION;
97ee3a89
DG
483 break;
484 case LTTNG_EVENT_FUNCTION_ENTRY:
fc4b93fa 485 local_ust_event->attr.instrumentation = LTTNG_UST_ABI_FUNCTION;
97ee3a89
DG
486 break;
487 case LTTNG_EVENT_TRACEPOINT:
fc4b93fa 488 local_ust_event->attr.instrumentation = LTTNG_UST_ABI_TRACEPOINT;
97ee3a89
DG
489 break;
490 default:
491 ERR("Unknown ust instrumentation type (%d)", ev->type);
39687410 492 ret = LTTNG_ERR_INVALID;
44844c29 493 goto error_free_event;
97ee3a89
DG
494 }
495
496 /* Copy event name */
fc4b93fa
MD
497 strncpy(local_ust_event->attr.name, ev->name, LTTNG_UST_ABI_SYM_NAME_LEN);
498 local_ust_event->attr.name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] = '\0';
97ee3a89 499
0cda4f28 500 switch (ev->loglevel_type) {
8005f29a 501 case LTTNG_EVENT_LOGLEVEL_ALL:
fc4b93fa 502 local_ust_event->attr.loglevel_type = LTTNG_UST_ABI_LOGLEVEL_ALL;
39687410 503 local_ust_event->attr.loglevel = -1; /* Force to -1 */
0cda4f28 504 break;
8005f29a 505 case LTTNG_EVENT_LOGLEVEL_RANGE:
fc4b93fa 506 local_ust_event->attr.loglevel_type = LTTNG_UST_ABI_LOGLEVEL_RANGE;
39687410 507 local_ust_event->attr.loglevel = ev->loglevel;
8005f29a
MD
508 break;
509 case LTTNG_EVENT_LOGLEVEL_SINGLE:
fc4b93fa 510 local_ust_event->attr.loglevel_type = LTTNG_UST_ABI_LOGLEVEL_SINGLE;
39687410 511 local_ust_event->attr.loglevel = ev->loglevel;
0cda4f28
MD
512 break;
513 default:
4431494b 514 ERR("Unknown ust loglevel type (%d)", ev->loglevel_type);
39687410 515 ret = LTTNG_ERR_INVALID;
0cda4f28
MD
516 goto error_free_event;
517 }
518
025faf73 519 /* Same layout. */
39687410
FD
520 local_ust_event->filter_expression = filter_expression;
521 local_ust_event->filter = filter;
522 local_ust_event->exclusion = exclusion;
0cda4f28 523
f6a9efaa 524 /* Init node */
39687410 525 lttng_ht_node_init_str(&local_ust_event->node, local_ust_event->attr.name);
97ee3a89 526
300b8fd5 527 DBG2("Trace UST event %s, loglevel (%d,%d) created",
39687410
FD
528 local_ust_event->attr.name, local_ust_event->attr.loglevel_type,
529 local_ust_event->attr.loglevel);
530
531 *ust_event = local_ust_event;
284d8f55 532
39687410 533 return ret;
97ee3a89 534
44844c29 535error_free_event:
39687410 536 free(local_ust_event);
97ee3a89 537error:
49d21f93
MD
538 free(filter_expression);
539 free(filter);
540 free(exclusion);
39687410 541 return ret;
97ee3a89
DG
542}
543
aa3514e9 544static
bdf64013
JG
545int trace_ust_context_type_event_to_ust(
546 enum lttng_event_context_type type)
55cc08a6 547{
aa3514e9 548 int utype;
0525e9ae 549
aa3514e9 550 switch (type) {
9197c5c4 551 case LTTNG_EVENT_CONTEXT_VTID:
fc4b93fa 552 utype = LTTNG_UST_ABI_CONTEXT_VTID;
9197c5c4
MD
553 break;
554 case LTTNG_EVENT_CONTEXT_VPID:
fc4b93fa 555 utype = LTTNG_UST_ABI_CONTEXT_VPID;
9197c5c4
MD
556 break;
557 case LTTNG_EVENT_CONTEXT_PTHREAD_ID:
fc4b93fa 558 utype = LTTNG_UST_ABI_CONTEXT_PTHREAD_ID;
9197c5c4
MD
559 break;
560 case LTTNG_EVENT_CONTEXT_PROCNAME:
fc4b93fa 561 utype = LTTNG_UST_ABI_CONTEXT_PROCNAME;
9197c5c4 562 break;
7c612c2e 563 case LTTNG_EVENT_CONTEXT_IP:
fc4b93fa 564 utype = LTTNG_UST_ABI_CONTEXT_IP;
7c612c2e 565 break;
aa3514e9 566 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
b623cb6a 567 if (!lttng_ust_ctl_has_perf_counters()) {
354e561b
MD
568 utype = -1;
569 WARN("Perf counters not implemented in UST");
570 } else {
fc4b93fa 571 utype = LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER;
354e561b 572 }
aa3514e9 573 break;
bdf64013 574 case LTTNG_EVENT_CONTEXT_APP_CONTEXT:
fc4b93fa 575 utype = LTTNG_UST_ABI_CONTEXT_APP_CONTEXT;
bdf64013 576 break;
f17b8732 577 case LTTNG_EVENT_CONTEXT_CGROUP_NS:
fc4b93fa 578 utype = LTTNG_UST_ABI_CONTEXT_CGROUP_NS;
f17b8732
MJ
579 break;
580 case LTTNG_EVENT_CONTEXT_IPC_NS:
fc4b93fa 581 utype = LTTNG_UST_ABI_CONTEXT_IPC_NS;
f17b8732
MJ
582 break;
583 case LTTNG_EVENT_CONTEXT_MNT_NS:
fc4b93fa 584 utype = LTTNG_UST_ABI_CONTEXT_MNT_NS;
f17b8732
MJ
585 break;
586 case LTTNG_EVENT_CONTEXT_NET_NS:
fc4b93fa 587 utype = LTTNG_UST_ABI_CONTEXT_NET_NS;
f17b8732
MJ
588 break;
589 case LTTNG_EVENT_CONTEXT_PID_NS:
fc4b93fa 590 utype = LTTNG_UST_ABI_CONTEXT_PID_NS;
f17b8732 591 break;
d37ac3cd 592 case LTTNG_EVENT_CONTEXT_TIME_NS:
fc4b93fa 593 utype = LTTNG_UST_ABI_CONTEXT_TIME_NS;
d37ac3cd 594 break;
f17b8732 595 case LTTNG_EVENT_CONTEXT_USER_NS:
fc4b93fa 596 utype = LTTNG_UST_ABI_CONTEXT_USER_NS;
f17b8732
MJ
597 break;
598 case LTTNG_EVENT_CONTEXT_UTS_NS:
fc4b93fa 599 utype = LTTNG_UST_ABI_CONTEXT_UTS_NS;
f17b8732 600 break;
4fc59cb8 601 case LTTNG_EVENT_CONTEXT_VUID:
fc4b93fa 602 utype = LTTNG_UST_ABI_CONTEXT_VUID;
4fc59cb8
MJ
603 break;
604 case LTTNG_EVENT_CONTEXT_VEUID:
fc4b93fa 605 utype = LTTNG_UST_ABI_CONTEXT_VEUID;
4fc59cb8
MJ
606 break;
607 case LTTNG_EVENT_CONTEXT_VSUID:
fc4b93fa 608 utype = LTTNG_UST_ABI_CONTEXT_VSUID;
4fc59cb8
MJ
609 break;
610 case LTTNG_EVENT_CONTEXT_VGID:
fc4b93fa 611 utype = LTTNG_UST_ABI_CONTEXT_VGID;
4fc59cb8
MJ
612 break;
613 case LTTNG_EVENT_CONTEXT_VEGID:
fc4b93fa 614 utype = LTTNG_UST_ABI_CONTEXT_VEGID;
4fc59cb8
MJ
615 break;
616 case LTTNG_EVENT_CONTEXT_VSGID:
fc4b93fa 617 utype = LTTNG_UST_ABI_CONTEXT_VSGID;
4fc59cb8 618 break;
9197c5c4 619 default:
aa3514e9
MD
620 utype = -1;
621 break;
622 }
623 return utype;
624}
625
626/*
627 * Return 1 if contexts match, 0 otherwise.
628 */
df4f5a87
JG
629int trace_ust_match_context(const struct ltt_ust_context *uctx,
630 const struct lttng_event_context *ctx)
aa3514e9
MD
631{
632 int utype;
633
634 utype = trace_ust_context_type_event_to_ust(ctx->ctx);
635 if (utype < 0) {
636 return 0;
637 }
638 if (uctx->ctx.ctx != utype) {
639 return 0;
640 }
641 switch (utype) {
fc4b93fa 642 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER:
aa3514e9
MD
643 if (uctx->ctx.u.perf_counter.type
644 != ctx->u.perf_counter.type) {
645 return 0;
646 }
647 if (uctx->ctx.u.perf_counter.config
648 != ctx->u.perf_counter.config) {
649 return 0;
650 }
651 if (strncmp(uctx->ctx.u.perf_counter.name,
652 ctx->u.perf_counter.name,
fc4b93fa 653 LTTNG_UST_ABI_SYM_NAME_LEN)) {
aa3514e9
MD
654 return 0;
655 }
656 break;
fc4b93fa 657 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT:
a0377dfe
FD
658 LTTNG_ASSERT(uctx->ctx.u.app_ctx.provider_name);
659 LTTNG_ASSERT(uctx->ctx.u.app_ctx.ctx_name);
54fb33cf
JG
660 if (strcmp(uctx->ctx.u.app_ctx.provider_name,
661 ctx->u.app_ctx.provider_name) ||
662 strcmp(uctx->ctx.u.app_ctx.ctx_name,
663 ctx->u.app_ctx.ctx_name)) {
664 return 0;
665 }
aa3514e9
MD
666 default:
667 break;
668
669 }
670 return 1;
671}
672
673/*
674 * Allocate and initialize an UST context.
675 *
676 * Return pointer to structure or NULL.
677 */
678struct ltt_ust_context *trace_ust_create_context(
df4f5a87 679 const struct lttng_event_context *ctx)
aa3514e9 680{
bdf64013 681 struct ltt_ust_context *uctx = NULL;
aa3514e9
MD
682 int utype;
683
a0377dfe 684 LTTNG_ASSERT(ctx);
aa3514e9
MD
685
686 utype = trace_ust_context_type_event_to_ust(ctx->ctx);
687 if (utype < 0) {
1cf992b8 688 ERR("Invalid UST context");
bdf64013 689 goto end;
9197c5c4 690 }
55cc08a6 691
64803277 692 uctx = zmalloc<ltt_ust_context>();
bdf64013 693 if (!uctx) {
55cc08a6 694 PERROR("zmalloc ltt_ust_context");
bdf64013 695 goto end;
55cc08a6
DG
696 }
697
fc4b93fa 698 uctx->ctx.ctx = (enum lttng_ust_abi_context_type) utype;
aa3514e9 699 switch (utype) {
fc4b93fa 700 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER:
aa3514e9
MD
701 uctx->ctx.u.perf_counter.type = ctx->u.perf_counter.type;
702 uctx->ctx.u.perf_counter.config = ctx->u.perf_counter.config;
703 strncpy(uctx->ctx.u.perf_counter.name, ctx->u.perf_counter.name,
fc4b93fa
MD
704 LTTNG_UST_ABI_SYM_NAME_LEN);
705 uctx->ctx.u.perf_counter.name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] = '\0';
aa3514e9 706 break;
fc4b93fa 707 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT:
bdf64013
JG
708 {
709 char *provider_name = NULL, *ctx_name = NULL;
710
711 provider_name = strdup(ctx->u.app_ctx.provider_name);
712 if (!provider_name) {
713 goto error;
714 }
715 uctx->ctx.u.app_ctx.provider_name = provider_name;
716
717 ctx_name = strdup(ctx->u.app_ctx.ctx_name);
718 if (!ctx_name) {
719 goto error;
720 }
721 uctx->ctx.u.app_ctx.ctx_name = ctx_name;
722 break;
723 }
aa3514e9
MD
724 default:
725 break;
726 }
bec39940 727 lttng_ht_node_init_ulong(&uctx->node, (unsigned long) uctx->ctx.ctx);
bdf64013 728end:
55cc08a6 729 return uctx;
55cc08a6 730error:
bdf64013 731 trace_ust_destroy_context(uctx);
55cc08a6
DG
732 return NULL;
733}
734
55c9e7ca 735static void destroy_id_tracker_node_rcu(struct rcu_head *head)
a9ad0c8f 736{
55c9e7ca
JR
737 struct ust_id_tracker_node *tracker_node = caa_container_of(
738 head, struct ust_id_tracker_node, node.head);
a9ad0c8f
MD
739 free(tracker_node);
740}
741
55c9e7ca 742static void destroy_id_tracker_node(struct ust_id_tracker_node *tracker_node)
a9ad0c8f 743{
55c9e7ca 744 call_rcu(&tracker_node->node.head, destroy_id_tracker_node_rcu);
a9ad0c8f
MD
745}
746
55c9e7ca 747static int init_id_tracker(struct ust_id_tracker *id_tracker)
a9ad0c8f 748{
55c9e7ca 749 int ret = LTTNG_OK;
a9ad0c8f 750
55c9e7ca
JR
751 id_tracker->ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
752 if (!id_tracker->ht) {
753 ret = LTTNG_ERR_NOMEM;
a9ad0c8f
MD
754 goto end;
755 }
756
757end:
758 return ret;
759}
760
761/*
55c9e7ca 762 * Teardown id tracker content, but don't free id_tracker object.
a9ad0c8f 763 */
55c9e7ca 764static void fini_id_tracker(struct ust_id_tracker *id_tracker)
a9ad0c8f 765{
55c9e7ca 766 struct ust_id_tracker_node *tracker_node;
a9ad0c8f
MD
767 struct lttng_ht_iter iter;
768
55c9e7ca 769 if (!id_tracker->ht) {
a9ad0c8f
MD
770 return;
771 }
772 rcu_read_lock();
55c9e7ca
JR
773 cds_lfht_for_each_entry (id_tracker->ht->ht, &iter.iter, tracker_node,
774 node.node) {
775 int ret = lttng_ht_del(id_tracker->ht, &iter);
a9ad0c8f 776
a0377dfe 777 LTTNG_ASSERT(!ret);
55c9e7ca 778 destroy_id_tracker_node(tracker_node);
a9ad0c8f
MD
779 }
780 rcu_read_unlock();
3c339053 781 lttng_ht_destroy(id_tracker->ht);
55c9e7ca 782 id_tracker->ht = NULL;
a9ad0c8f
MD
783}
784
55c9e7ca
JR
785static struct ust_id_tracker_node *id_tracker_lookup(
786 struct ust_id_tracker *id_tracker,
787 int id,
a9ad0c8f
MD
788 struct lttng_ht_iter *iter)
789{
55c9e7ca 790 unsigned long _id = (unsigned long) id;
a9ad0c8f
MD
791 struct lttng_ht_node_ulong *node;
792
55c9e7ca 793 lttng_ht_lookup(id_tracker->ht, (void *) _id, iter);
a9ad0c8f
MD
794 node = lttng_ht_iter_get_node_ulong(iter);
795 if (node) {
0114db0e 796 return lttng::utils::container_of(node, &ust_id_tracker_node::node);
a9ad0c8f
MD
797 } else {
798 return NULL;
799 }
800}
801
55c9e7ca 802static int id_tracker_add_id(struct ust_id_tracker *id_tracker, int id)
a9ad0c8f
MD
803{
804 int retval = LTTNG_OK;
55c9e7ca 805 struct ust_id_tracker_node *tracker_node;
a9ad0c8f
MD
806 struct lttng_ht_iter iter;
807
55c9e7ca 808 if (id < 0) {
a9ad0c8f
MD
809 retval = LTTNG_ERR_INVALID;
810 goto end;
811 }
55c9e7ca 812 tracker_node = id_tracker_lookup(id_tracker, id, &iter);
a9ad0c8f
MD
813 if (tracker_node) {
814 /* Already exists. */
159b042f 815 retval = LTTNG_ERR_PROCESS_ATTR_EXISTS;
a9ad0c8f
MD
816 goto end;
817 }
64803277 818 tracker_node = zmalloc<ust_id_tracker_node>();
a9ad0c8f
MD
819 if (!tracker_node) {
820 retval = LTTNG_ERR_NOMEM;
821 goto end;
822 }
55c9e7ca
JR
823 lttng_ht_node_init_ulong(&tracker_node->node, (unsigned long) id);
824 lttng_ht_add_unique_ulong(id_tracker->ht, &tracker_node->node);
a9ad0c8f
MD
825end:
826 return retval;
827}
828
55c9e7ca 829static int id_tracker_del_id(struct ust_id_tracker *id_tracker, int id)
a9ad0c8f
MD
830{
831 int retval = LTTNG_OK, ret;
55c9e7ca 832 struct ust_id_tracker_node *tracker_node;
a9ad0c8f
MD
833 struct lttng_ht_iter iter;
834
55c9e7ca 835 if (id < 0) {
a9ad0c8f
MD
836 retval = LTTNG_ERR_INVALID;
837 goto end;
838 }
55c9e7ca 839 tracker_node = id_tracker_lookup(id_tracker, id, &iter);
a9ad0c8f
MD
840 if (!tracker_node) {
841 /* Not found */
159b042f 842 retval = LTTNG_ERR_PROCESS_ATTR_MISSING;
a9ad0c8f
MD
843 goto end;
844 }
55c9e7ca 845 ret = lttng_ht_del(id_tracker->ht, &iter);
a0377dfe 846 LTTNG_ASSERT(!ret);
a9ad0c8f 847
55c9e7ca 848 destroy_id_tracker_node(tracker_node);
a9ad0c8f
MD
849end:
850 return retval;
851}
852
55c9e7ca 853static struct ust_id_tracker *get_id_tracker(struct ltt_ust_session *session,
159b042f 854 enum lttng_process_attr process_attr)
55c9e7ca 855{
159b042f
JG
856 switch (process_attr) {
857 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID:
55c9e7ca 858 return &session->vpid_tracker;
159b042f 859 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID:
55c9e7ca 860 return &session->vuid_tracker;
159b042f 861 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID:
55c9e7ca
JR
862 return &session->vgid_tracker;
863 default:
864 return NULL;
865 }
866}
867
159b042f 868static struct process_attr_tracker *_trace_ust_get_process_attr_tracker(
55c9e7ca 869 struct ltt_ust_session *session,
159b042f 870 enum lttng_process_attr process_attr)
55c9e7ca 871{
159b042f
JG
872 switch (process_attr) {
873 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID:
874 return session->tracker_vpid;
875 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID:
876 return session->tracker_vuid;
877 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID:
878 return session->tracker_vgid;
55c9e7ca
JR
879 default:
880 return NULL;
881 }
882}
883
159b042f
JG
884const struct process_attr_tracker *trace_ust_get_process_attr_tracker(
885 struct ltt_ust_session *session,
886 enum lttng_process_attr process_attr)
887{
888 return (const struct process_attr_tracker *)
889 _trace_ust_get_process_attr_tracker(
890 session, process_attr);
891}
892
a9ad0c8f
MD
893/*
894 * The session lock is held when calling this function.
895 */
159b042f 896int trace_ust_id_tracker_lookup(enum lttng_process_attr process_attr,
55c9e7ca
JR
897 struct ltt_ust_session *session,
898 int id)
a9ad0c8f
MD
899{
900 struct lttng_ht_iter iter;
55c9e7ca 901 struct ust_id_tracker *id_tracker;
a9ad0c8f 902
159b042f 903 id_tracker = get_id_tracker(session, process_attr);
55c9e7ca
JR
904 if (!id_tracker) {
905 abort();
906 }
907 if (!id_tracker->ht) {
a9ad0c8f
MD
908 return 1;
909 }
55c9e7ca 910 if (id_tracker_lookup(id_tracker, id, &iter)) {
a9ad0c8f
MD
911 return 1;
912 }
913 return 0;
914}
915
916/*
917 * Called with the session lock held.
918 */
159b042f 919enum lttng_error_code trace_ust_process_attr_tracker_set_tracking_policy(
55c9e7ca 920 struct ltt_ust_session *session,
159b042f
JG
921 enum lttng_process_attr process_attr,
922 enum lttng_tracking_policy policy)
a9ad0c8f 923{
159b042f
JG
924 int ret;
925 enum lttng_error_code ret_code = LTTNG_OK;
926 struct ust_id_tracker *id_tracker =
927 get_id_tracker(session, process_attr);
928 struct process_attr_tracker *tracker =
929 _trace_ust_get_process_attr_tracker(
930 session, process_attr);
88e3c2f5 931 bool should_update_apps = false;
159b042f 932 enum lttng_tracking_policy previous_policy;
55c9e7ca 933
159b042f
JG
934 if (!tracker) {
935 ret_code = LTTNG_ERR_INVALID;
936 goto end;
55c9e7ca 937 }
a9ad0c8f 938
159b042f
JG
939 previous_policy = process_attr_tracker_get_tracking_policy(tracker);
940 ret = process_attr_tracker_set_tracking_policy(tracker, policy);
941 if (ret) {
942 ret_code = LTTNG_ERR_UNK;
55c9e7ca
JR
943 goto end;
944 }
945
159b042f
JG
946 if (previous_policy == policy) {
947 goto end;
55c9e7ca 948 }
159b042f
JG
949
950 switch (policy) {
951 case LTTNG_TRACKING_POLICY_INCLUDE_ALL:
952 /* Track all values: destroy tracker if exists. */
55c9e7ca
JR
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 957 }
159b042f
JG
958 break;
959 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL:
960 case LTTNG_TRACKING_POLICY_INCLUDE_SET:
961 /* fall-through. */
962 fini_id_tracker(id_tracker);
7966af57 963 ret_code = (lttng_error_code) init_id_tracker(id_tracker);
159b042f
JG
964 if (ret_code != LTTNG_OK) {
965 ERR("Error initializing ID tracker");
966 goto end;
a9ad0c8f 967 }
159b042f
JG
968 /* Remove all apps from session. */
969 should_update_apps = true;
970 break;
971 default:
972 abort();
a9ad0c8f 973 }
88e3c2f5
JG
974 if (should_update_apps && session->active) {
975 ust_app_global_update_all(session);
976 }
a9ad0c8f 977end:
159b042f 978 return ret_code;
a9ad0c8f
MD
979}
980
159b042f
JG
981/* Called with the session lock held. */
982enum lttng_error_code trace_ust_process_attr_tracker_inclusion_set_add_value(
55c9e7ca 983 struct ltt_ust_session *session,
159b042f
JG
984 enum lttng_process_attr process_attr,
985 const struct process_attr_value *value)
a9ad0c8f 986{
159b042f 987 enum lttng_error_code ret_code = LTTNG_OK;
a5a30920 988 bool should_update_apps = false;
159b042f
JG
989 struct ust_id_tracker *id_tracker =
990 get_id_tracker(session, process_attr);
991 struct process_attr_tracker *tracker;
992 int integral_value;
993 enum process_attr_tracker_status status;
994 struct ust_app *app;
55c9e7ca 995
159b042f
JG
996 /*
997 * Convert process attribute tracker value to the integral
998 * representation required by the kern-ctl API.
999 */
1000 switch (process_attr) {
1001 case LTTNG_PROCESS_ATTR_PROCESS_ID:
1002 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID:
1003 integral_value = (int) value->value.pid;
1004 break;
1005 case LTTNG_PROCESS_ATTR_USER_ID:
1006 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID:
1007 if (value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME) {
1008 uid_t uid;
1009
1010 ret_code = utils_user_id_from_name(
1011 value->value.user_name, &uid);
1012 if (ret_code != LTTNG_OK) {
1013 goto end;
1014 }
1015 integral_value = (int) uid;
1016 } else {
1017 integral_value = (int) value->value.uid;
1018 }
1019 break;
1020 case LTTNG_PROCESS_ATTR_GROUP_ID:
1021 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID:
1022 if (value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME) {
1023 gid_t gid;
1024
1025 ret_code = utils_group_id_from_name(
1026 value->value.group_name, &gid);
1027 if (ret_code != LTTNG_OK) {
1028 goto end;
1029 }
1030 integral_value = (int) gid;
1031 } else {
1032 integral_value = (int) value->value.gid;
1033 }
1034 break;
1035 default:
1036 ret_code = LTTNG_ERR_INVALID;
55c9e7ca
JR
1037 goto end;
1038 }
a9ad0c8f 1039
159b042f
JG
1040 tracker = _trace_ust_get_process_attr_tracker(session, process_attr);
1041 if (!tracker) {
1042 ret_code = LTTNG_ERR_INVALID;
74675e31 1043 goto end;
55c9e7ca
JR
1044 }
1045
159b042f
JG
1046 status = process_attr_tracker_inclusion_set_add_value(tracker, value);
1047 if (status != PROCESS_ATTR_TRACKER_STATUS_OK) {
1048 switch (status) {
1049 case PROCESS_ATTR_TRACKER_STATUS_EXISTS:
1050 ret_code = LTTNG_ERR_PROCESS_ATTR_EXISTS;
1051 break;
1052 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY:
1053 ret_code = LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY;
1054 break;
1055 case PROCESS_ATTR_TRACKER_STATUS_ERROR:
1056 default:
1057 ret_code = LTTNG_ERR_UNK;
1058 break;
a9ad0c8f 1059 }
159b042f
JG
1060 goto end;
1061 }
a9ad0c8f 1062
159b042f
JG
1063 DBG("User space track %s %d for session id %" PRIu64,
1064 lttng_process_attr_to_string(process_attr),
1065 integral_value, session->id);
a9ad0c8f 1066
7966af57 1067 ret_code = (lttng_error_code) id_tracker_add_id(id_tracker, integral_value);
159b042f
JG
1068 if (ret_code != LTTNG_OK) {
1069 goto end;
1070 }
1071 /* Add session to application */
1072 switch (process_attr) {
1073 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID:
1074 app = ust_app_find_by_pid(integral_value);
1075 if (app) {
a5a30920 1076 should_update_apps = true;
a9ad0c8f 1077 }
159b042f
JG
1078 break;
1079 default:
1080 should_update_apps = true;
1081 break;
a9ad0c8f 1082 }
a5a30920
JR
1083 if (should_update_apps && session->active) {
1084 ust_app_global_update_all(session);
1085 }
a9ad0c8f 1086end:
159b042f 1087 return ret_code;
a9ad0c8f
MD
1088}
1089
159b042f
JG
1090/* Called with the session lock held. */
1091enum lttng_error_code trace_ust_process_attr_tracker_inclusion_set_remove_value(
55c9e7ca 1092 struct ltt_ust_session *session,
159b042f
JG
1093 enum lttng_process_attr process_attr,
1094 const struct process_attr_value *value)
a5dfbb9d 1095{
159b042f
JG
1096 enum lttng_error_code ret_code = LTTNG_OK;
1097 bool should_update_apps = false;
1098 struct ust_id_tracker *id_tracker =
1099 get_id_tracker(session, process_attr);
1100 struct process_attr_tracker *tracker;
1101 int integral_value;
1102 enum process_attr_tracker_status status;
1103 struct ust_app *app;
a5dfbb9d 1104
159b042f
JG
1105 /*
1106 * Convert process attribute tracker value to the integral
1107 * representation required by the kern-ctl API.
1108 */
1109 switch (process_attr) {
1110 case LTTNG_PROCESS_ATTR_PROCESS_ID:
1111 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID:
1112 integral_value = (int) value->value.pid;
1113 break;
1114 case LTTNG_PROCESS_ATTR_USER_ID:
1115 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID:
1116 if (value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME) {
1117 uid_t uid;
1118
1119 ret_code = utils_user_id_from_name(
1120 value->value.user_name, &uid);
1121 if (ret_code != LTTNG_OK) {
1122 goto end;
1123 }
1124 integral_value = (int) uid;
1125 } else {
1126 integral_value = (int) value->value.uid;
1127 }
1128 break;
1129 case LTTNG_PROCESS_ATTR_GROUP_ID:
1130 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID:
1131 if (value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME) {
1132 gid_t gid;
1133
1134 ret_code = utils_group_id_from_name(
1135 value->value.group_name, &gid);
1136 if (ret_code != LTTNG_OK) {
1137 goto end;
1138 }
1139 integral_value = (int) gid;
1140 } else {
1141 integral_value = (int) value->value.gid;
1142 }
1143 break;
1144 default:
1145 ret_code = LTTNG_ERR_INVALID;
1146 goto end;
a5dfbb9d
MD
1147 }
1148
159b042f
JG
1149 tracker = _trace_ust_get_process_attr_tracker(session, process_attr);
1150 if (!tracker) {
1151 ret_code = LTTNG_ERR_INVALID;
74675e31 1152 goto end;
159b042f
JG
1153 }
1154
1155 status = process_attr_tracker_inclusion_set_remove_value(
1156 tracker, value);
1157 if (status != PROCESS_ATTR_TRACKER_STATUS_OK) {
1158 switch (status) {
1159 case PROCESS_ATTR_TRACKER_STATUS_MISSING:
1160 ret_code = LTTNG_ERR_PROCESS_ATTR_MISSING;
1161 break;
1162 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY:
1163 ret_code = LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY;
1164 break;
1165 case PROCESS_ATTR_TRACKER_STATUS_ERROR:
1166 default:
1167 ret_code = LTTNG_ERR_UNK;
1168 break;
1169 }
a7a533cd 1170 goto end;
a5dfbb9d 1171 }
a7a533cd 1172
159b042f
JG
1173 DBG("User space untrack %s %d for session id %" PRIu64,
1174 lttng_process_attr_to_string(process_attr),
1175 integral_value, session->id);
1176
7966af57 1177 ret_code = (lttng_error_code) id_tracker_del_id(id_tracker, integral_value);
159b042f 1178 if (ret_code != LTTNG_OK) {
a7a533cd
JR
1179 goto end;
1180 }
159b042f
JG
1181 /* Add session to application */
1182 switch (process_attr) {
1183 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID:
1184 app = ust_app_find_by_pid(integral_value);
1185 if (app) {
1186 should_update_apps = true;
1187 }
1188 break;
1189 default:
1190 should_update_apps = true;
1191 break;
1192 }
1193 if (should_update_apps && session->active) {
1194 ust_app_global_update_all(session);
1195 }
a7a533cd 1196end:
159b042f 1197 return ret_code;
a5dfbb9d
MD
1198}
1199
f6a9efaa
DG
1200/*
1201 * RCU safe free context structure.
1202 */
1203static void destroy_context_rcu(struct rcu_head *head)
1204{
bec39940 1205 struct lttng_ht_node_ulong *node =
0114db0e 1206 lttng::utils::container_of(head, &lttng_ht_node_ulong::head);
f6a9efaa 1207 struct ltt_ust_context *ctx =
0114db0e 1208 lttng::utils::container_of(node, &ltt_ust_context::node);
f6a9efaa 1209
bdf64013 1210 trace_ust_destroy_context(ctx);
f6a9efaa
DG
1211}
1212
1213/*
1214 * Cleanup UST context hash table.
1215 */
7bd39047 1216static void destroy_contexts(struct lttng_ht *ht)
f6a9efaa
DG
1217{
1218 int ret;
bec39940
DG
1219 struct lttng_ht_node_ulong *node;
1220 struct lttng_ht_iter iter;
31746f93 1221 struct ltt_ust_context *ctx;
f6a9efaa 1222
a0377dfe 1223 LTTNG_ASSERT(ht);
0525e9ae 1224
36b588ed 1225 rcu_read_lock();
bec39940 1226 cds_lfht_for_each_entry(ht->ht, &iter.iter, node, node) {
31746f93 1227 /* Remove from ordered list. */
0114db0e 1228 ctx = lttng::utils::container_of(node, &ltt_ust_context::node);
31746f93
DG
1229 cds_list_del(&ctx->list);
1230 /* Remove from channel's hash table. */
bec39940 1231 ret = lttng_ht_del(ht, &iter);
f6a9efaa
DG
1232 if (!ret) {
1233 call_rcu(&node->head, destroy_context_rcu);
1234 }
f6a9efaa 1235 }
36b588ed 1236 rcu_read_unlock();
ed52805d 1237
3c339053 1238 lttng_ht_destroy(ht);
f6a9efaa
DG
1239}
1240
97ee3a89
DG
1241/*
1242 * Cleanup ust event structure.
1243 */
1244void trace_ust_destroy_event(struct ltt_ust_event *event)
1245{
a0377dfe 1246 LTTNG_ASSERT(event);
0525e9ae 1247
f6a9efaa 1248 DBG2("Trace destroy UST event %s", event->attr.name);
6b453b5e 1249 free(event->filter_expression);
53a80697 1250 free(event->filter);
40024f8a 1251 free(event->exclusion);
97ee3a89
DG
1252 free(event);
1253}
1254
bdf64013
JG
1255/*
1256 * Cleanup ust context structure.
1257 */
1258void trace_ust_destroy_context(struct ltt_ust_context *ctx)
1259{
a0377dfe 1260 LTTNG_ASSERT(ctx);
bdf64013 1261
fc4b93fa 1262 if (ctx->ctx.ctx == LTTNG_UST_ABI_CONTEXT_APP_CONTEXT) {
bdf64013
JG
1263 free(ctx->ctx.u.app_ctx.provider_name);
1264 free(ctx->ctx.u.app_ctx.ctx_name);
1265 }
1266 free(ctx);
1267}
1268
f6a9efaa
DG
1269/*
1270 * URCU intermediate call to complete destroy event.
1271 */
1272static void destroy_event_rcu(struct rcu_head *head)
1273{
bec39940 1274 struct lttng_ht_node_str *node =
0114db0e 1275 lttng::utils::container_of(head, &lttng_ht_node_str::head);
f6a9efaa 1276 struct ltt_ust_event *event =
0114db0e 1277 lttng::utils::container_of(node, &ltt_ust_event::node);
f6a9efaa
DG
1278
1279 trace_ust_destroy_event(event);
1280}
1281
284d8f55
DG
1282/*
1283 * Cleanup UST events hashtable.
1284 */
7bd39047 1285static void destroy_events(struct lttng_ht *events)
ed52805d
DG
1286{
1287 int ret;
bec39940
DG
1288 struct lttng_ht_node_str *node;
1289 struct lttng_ht_iter iter;
ed52805d 1290
a0377dfe 1291 LTTNG_ASSERT(events);
0525e9ae 1292
36b588ed 1293 rcu_read_lock();
bec39940
DG
1294 cds_lfht_for_each_entry(events->ht, &iter.iter, node, node) {
1295 ret = lttng_ht_del(events, &iter);
a0377dfe 1296 LTTNG_ASSERT(!ret);
7bd39047 1297 call_rcu(&node->head, destroy_event_rcu);
ed52805d 1298 }
36b588ed 1299 rcu_read_unlock();
ed52805d 1300
3c339053 1301 lttng_ht_destroy(events);
ed52805d
DG
1302}
1303
97ee3a89
DG
1304/*
1305 * Cleanup ust channel structure.
36b588ed
MD
1306 *
1307 * Should _NOT_ be called with RCU read lock held.
97ee3a89 1308 */
36b588ed 1309static void _trace_ust_destroy_channel(struct ltt_ust_channel *channel)
97ee3a89 1310{
a0377dfe 1311 LTTNG_ASSERT(channel);
0525e9ae 1312
f6a9efaa 1313 DBG2("Trace destroy UST channel %s", channel->name);
97ee3a89 1314
97ee3a89 1315 free(channel);
f6a9efaa
DG
1316}
1317
1318/*
1319 * URCU intermediate call to complete destroy channel.
1320 */
1321static void destroy_channel_rcu(struct rcu_head *head)
1322{
bec39940 1323 struct lttng_ht_node_str *node =
0114db0e 1324 lttng::utils::container_of(head, &lttng_ht_node_str::head);
f6a9efaa 1325 struct ltt_ust_channel *channel =
0114db0e 1326 lttng::utils::container_of(node, &ltt_ust_channel::node);
f6a9efaa 1327
36b588ed
MD
1328 _trace_ust_destroy_channel(channel);
1329}
1330
1331void trace_ust_destroy_channel(struct ltt_ust_channel *channel)
1332{
627cbbe7
MD
1333 /* Destroying all events of the channel */
1334 destroy_events(channel->events);
1335 /* Destroying all context of the channel */
1336 destroy_contexts(channel->ctx);
1337
36b588ed 1338 call_rcu(&channel->node.head, destroy_channel_rcu);
97ee3a89
DG
1339}
1340
d5979e4a
DG
1341/*
1342 * Remove an UST channel from a channel HT.
1343 */
1344void trace_ust_delete_channel(struct lttng_ht *ht,
1345 struct ltt_ust_channel *channel)
1346{
1347 int ret;
1348 struct lttng_ht_iter iter;
1349
a0377dfe
FD
1350 LTTNG_ASSERT(ht);
1351 LTTNG_ASSERT(channel);
d5979e4a
DG
1352
1353 iter.iter.node = &channel->node.node;
1354 ret = lttng_ht_del(ht, &iter);
a0377dfe 1355 LTTNG_ASSERT(!ret);
d5979e4a
DG
1356}
1357
d7bfb9b0
JG
1358int trace_ust_regenerate_metadata(struct ltt_ust_session *usess)
1359{
1360 int ret = 0;
1361 struct buffer_reg_uid *uid_reg = NULL;
1362 struct buffer_reg_session *session_reg = NULL;
1363
1364 rcu_read_lock();
1365 cds_list_for_each_entry(uid_reg, &usess->buffer_reg_uid_list, lnode) {
b0f2e8db 1366 lsu::registry_session *registry;
d7bfb9b0
JG
1367
1368 session_reg = uid_reg->registry;
1369 registry = session_reg->reg.ust;
1370
1371 try {
1372 registry->regenerate_metadata();
1373 } catch (const std::exception& ex) {
1374 ERR("Failed to regenerate user space session metadata: %s", ex.what());
1375 ret = -1;
1376 goto end;
1377 }
1378 }
1379
1380end:
1381 rcu_read_unlock();
1382 return ret;
1383}
1384
97ee3a89 1385/*
f6a9efaa 1386 * Iterate over a hash table containing channels and cleanup safely.
97ee3a89 1387 */
bec39940 1388static void destroy_channels(struct lttng_ht *channels)
f6a9efaa 1389{
bec39940
DG
1390 struct lttng_ht_node_str *node;
1391 struct lttng_ht_iter iter;
f6a9efaa 1392
a0377dfe 1393 LTTNG_ASSERT(channels);
0525e9ae 1394
24d1723f 1395 rcu_read_lock();
bec39940 1396 cds_lfht_for_each_entry(channels->ht, &iter.iter, node, node) {
627cbbe7 1397 struct ltt_ust_channel *chan =
0114db0e 1398 lttng::utils::container_of(node, &ltt_ust_channel::node);
627cbbe7
MD
1399
1400 trace_ust_delete_channel(channels, chan);
1401 trace_ust_destroy_channel(chan);
f6a9efaa 1402 }
36b588ed 1403 rcu_read_unlock();
ed52805d 1404
3c339053 1405 lttng_ht_destroy(channels);
f6a9efaa
DG
1406}
1407
f6a9efaa
DG
1408/*
1409 * Cleanup UST global domain.
1410 */
1411static void destroy_domain_global(struct ltt_ust_domain_global *dom)
1412{
a0377dfe 1413 LTTNG_ASSERT(dom);
0525e9ae 1414
f6a9efaa
DG
1415 destroy_channels(dom->channels);
1416}
97ee3a89 1417
f6a9efaa 1418/*
d070c424
MD
1419 * Cleanup ust session structure, keeping data required by
1420 * destroy notifier.
f6a9efaa
DG
1421 */
1422void trace_ust_destroy_session(struct ltt_ust_session *session)
1423{
fefd409b 1424 struct agent *agt;
7972aab2 1425 struct buffer_reg_uid *reg, *sreg;
fefd409b 1426 struct lttng_ht_iter iter;
7972aab2 1427
a0377dfe 1428 LTTNG_ASSERT(session);
97ee3a89 1429
d9bf3ca4 1430 DBG2("Trace UST destroy session %" PRIu64, session->id);
f6a9efaa 1431
f6a9efaa
DG
1432 /* Cleaning up UST domain */
1433 destroy_domain_global(&session->domain_global);
fefd409b 1434
0c0fcd77 1435 rcu_read_lock();
fefd409b 1436 cds_lfht_for_each_entry(session->agents->ht, &iter.iter, agt, node.node) {
d0edf546
JG
1437 int ret = lttng_ht_del(session->agents, &iter);
1438
a0377dfe 1439 LTTNG_ASSERT(!ret);
fefd409b
DG
1440 agent_destroy(agt);
1441 }
0c0fcd77 1442 rcu_read_unlock();
7972aab2 1443
3c339053 1444 lttng_ht_destroy(session->agents);
8ada2175 1445
7972aab2
DG
1446 /* Cleanup UID buffer registry object(s). */
1447 cds_list_for_each_entry_safe(reg, sreg, &session->buffer_reg_uid_list,
1448 lnode) {
1449 cds_list_del(&reg->lnode);
1450 buffer_reg_uid_remove(reg);
1451 buffer_reg_uid_destroy(reg, session->consumer);
1452 }
44d3bd01 1453
159b042f
JG
1454 process_attr_tracker_destroy(session->tracker_vpid);
1455 process_attr_tracker_destroy(session->tracker_vuid);
1456 process_attr_tracker_destroy(session->tracker_vgid);
55c9e7ca
JR
1457
1458 fini_id_tracker(&session->vpid_tracker);
1459 fini_id_tracker(&session->vuid_tracker);
1460 fini_id_tracker(&session->vgid_tracker);
82b69413 1461 lttng_trace_chunk_put(session->current_trace_chunk);
d070c424
MD
1462}
1463
1464/* Free elements needed by destroy notifiers. */
1465void trace_ust_free_session(struct ltt_ust_session *session)
1466{
1467 consumer_output_put(session->consumer);
97ee3a89
DG
1468 free(session);
1469}
This page took 0.161997 seconds and 4 git commands to generate.