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