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