Fix: sessiond: wrong variable checked for error code
[lttng-tools.git] / src / bin / lttng-sessiond / trace-ust.c
CommitLineData
97ee3a89 1/*
ab5be9fa
MJ
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
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,
86 key->loglevel_value, LTTNG_UST_LOGLEVEL_ALL);
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,
2106efa0 197 char *name, struct lttng_filter_bytecode *filter,
b953b8cd
PP
198 enum lttng_ust_loglevel_type loglevel_type, 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
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;
288 lus->metadata_attr.output = LTTNG_UST_MMAP;
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;
6775595e 365 luc->attr.output = (enum lttng_ust_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:
374 luc->attr.output = LTTNG_UST_MMAP;
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 }
97ee3a89
DG
388 luc->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
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,
561c6897 449 struct lttng_filter_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:
39687410 475 local_ust_event->attr.instrumentation = LTTNG_UST_PROBE;
97ee3a89
DG
476 break;
477 case LTTNG_EVENT_FUNCTION:
39687410 478 local_ust_event->attr.instrumentation = LTTNG_UST_FUNCTION;
97ee3a89
DG
479 break;
480 case LTTNG_EVENT_FUNCTION_ENTRY:
39687410 481 local_ust_event->attr.instrumentation = LTTNG_UST_FUNCTION;
97ee3a89
DG
482 break;
483 case LTTNG_EVENT_TRACEPOINT:
39687410 484 local_ust_event->attr.instrumentation = LTTNG_UST_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 */
39687410
FD
493 strncpy(local_ust_event->attr.name, ev->name, LTTNG_UST_SYM_NAME_LEN);
494 local_ust_event->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
97ee3a89 495
0cda4f28 496 switch (ev->loglevel_type) {
8005f29a 497 case LTTNG_EVENT_LOGLEVEL_ALL:
39687410
FD
498 local_ust_event->attr.loglevel_type = LTTNG_UST_LOGLEVEL_ALL;
499 local_ust_event->attr.loglevel = -1; /* Force to -1 */
0cda4f28 500 break;
8005f29a 501 case LTTNG_EVENT_LOGLEVEL_RANGE:
39687410
FD
502 local_ust_event->attr.loglevel_type = LTTNG_UST_LOGLEVEL_RANGE;
503 local_ust_event->attr.loglevel = ev->loglevel;
8005f29a
MD
504 break;
505 case LTTNG_EVENT_LOGLEVEL_SINGLE:
39687410
FD
506 local_ust_event->attr.loglevel_type = LTTNG_UST_LOGLEVEL_SINGLE;
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
MD
547 case LTTNG_EVENT_CONTEXT_VTID:
548 utype = LTTNG_UST_CONTEXT_VTID;
549 break;
550 case LTTNG_EVENT_CONTEXT_VPID:
551 utype = LTTNG_UST_CONTEXT_VPID;
552 break;
553 case LTTNG_EVENT_CONTEXT_PTHREAD_ID:
554 utype = LTTNG_UST_CONTEXT_PTHREAD_ID;
555 break;
556 case LTTNG_EVENT_CONTEXT_PROCNAME:
557 utype = LTTNG_UST_CONTEXT_PROCNAME;
558 break;
7c612c2e
WP
559 case LTTNG_EVENT_CONTEXT_IP:
560 utype = LTTNG_UST_CONTEXT_IP;
561 break;
aa3514e9 562 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
354e561b
MD
563 if (!ustctl_has_perf_counters()) {
564 utype = -1;
565 WARN("Perf counters not implemented in UST");
566 } else {
567 utype = LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER;
568 }
aa3514e9 569 break;
bdf64013
JG
570 case LTTNG_EVENT_CONTEXT_APP_CONTEXT:
571 utype = LTTNG_UST_CONTEXT_APP_CONTEXT;
572 break;
f17b8732
MJ
573 case LTTNG_EVENT_CONTEXT_CGROUP_NS:
574 utype = LTTNG_UST_CONTEXT_CGROUP_NS;
575 break;
576 case LTTNG_EVENT_CONTEXT_IPC_NS:
577 utype = LTTNG_UST_CONTEXT_IPC_NS;
578 break;
579 case LTTNG_EVENT_CONTEXT_MNT_NS:
580 utype = LTTNG_UST_CONTEXT_MNT_NS;
581 break;
582 case LTTNG_EVENT_CONTEXT_NET_NS:
583 utype = LTTNG_UST_CONTEXT_NET_NS;
584 break;
585 case LTTNG_EVENT_CONTEXT_PID_NS:
586 utype = LTTNG_UST_CONTEXT_PID_NS;
587 break;
588 case LTTNG_EVENT_CONTEXT_USER_NS:
589 utype = LTTNG_UST_CONTEXT_USER_NS;
590 break;
591 case LTTNG_EVENT_CONTEXT_UTS_NS:
592 utype = LTTNG_UST_CONTEXT_UTS_NS;
593 break;
4fc59cb8
MJ
594 case LTTNG_EVENT_CONTEXT_VUID:
595 utype = LTTNG_UST_CONTEXT_VUID;
596 break;
597 case LTTNG_EVENT_CONTEXT_VEUID:
598 utype = LTTNG_UST_CONTEXT_VEUID;
599 break;
600 case LTTNG_EVENT_CONTEXT_VSUID:
601 utype = LTTNG_UST_CONTEXT_VSUID;
602 break;
603 case LTTNG_EVENT_CONTEXT_VGID:
604 utype = LTTNG_UST_CONTEXT_VGID;
605 break;
606 case LTTNG_EVENT_CONTEXT_VEGID:
607 utype = LTTNG_UST_CONTEXT_VEGID;
608 break;
609 case LTTNG_EVENT_CONTEXT_VSGID:
610 utype = LTTNG_UST_CONTEXT_VSGID;
611 break;
9197c5c4 612 default:
aa3514e9
MD
613 utype = -1;
614 break;
615 }
616 return utype;
617}
618
619/*
620 * Return 1 if contexts match, 0 otherwise.
621 */
df4f5a87
JG
622int trace_ust_match_context(const struct ltt_ust_context *uctx,
623 const struct lttng_event_context *ctx)
aa3514e9
MD
624{
625 int utype;
626
627 utype = trace_ust_context_type_event_to_ust(ctx->ctx);
628 if (utype < 0) {
629 return 0;
630 }
631 if (uctx->ctx.ctx != utype) {
632 return 0;
633 }
634 switch (utype) {
635 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER:
636 if (uctx->ctx.u.perf_counter.type
637 != ctx->u.perf_counter.type) {
638 return 0;
639 }
640 if (uctx->ctx.u.perf_counter.config
641 != ctx->u.perf_counter.config) {
642 return 0;
643 }
644 if (strncmp(uctx->ctx.u.perf_counter.name,
645 ctx->u.perf_counter.name,
646 LTTNG_UST_SYM_NAME_LEN)) {
647 return 0;
648 }
649 break;
54fb33cf
JG
650 case LTTNG_UST_CONTEXT_APP_CONTEXT:
651 assert(uctx->ctx.u.app_ctx.provider_name);
652 assert(uctx->ctx.u.app_ctx.ctx_name);
653 if (strcmp(uctx->ctx.u.app_ctx.provider_name,
654 ctx->u.app_ctx.provider_name) ||
655 strcmp(uctx->ctx.u.app_ctx.ctx_name,
656 ctx->u.app_ctx.ctx_name)) {
657 return 0;
658 }
aa3514e9
MD
659 default:
660 break;
661
662 }
663 return 1;
664}
665
666/*
667 * Allocate and initialize an UST context.
668 *
669 * Return pointer to structure or NULL.
670 */
671struct ltt_ust_context *trace_ust_create_context(
df4f5a87 672 const struct lttng_event_context *ctx)
aa3514e9 673{
bdf64013 674 struct ltt_ust_context *uctx = NULL;
aa3514e9
MD
675 int utype;
676
677 assert(ctx);
678
679 utype = trace_ust_context_type_event_to_ust(ctx->ctx);
680 if (utype < 0) {
1cf992b8 681 ERR("Invalid UST context");
bdf64013 682 goto end;
9197c5c4 683 }
55cc08a6
DG
684
685 uctx = zmalloc(sizeof(struct ltt_ust_context));
bdf64013 686 if (!uctx) {
55cc08a6 687 PERROR("zmalloc ltt_ust_context");
bdf64013 688 goto end;
55cc08a6
DG
689 }
690
aa3514e9
MD
691 uctx->ctx.ctx = (enum lttng_ust_context_type) utype;
692 switch (utype) {
693 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER:
694 uctx->ctx.u.perf_counter.type = ctx->u.perf_counter.type;
695 uctx->ctx.u.perf_counter.config = ctx->u.perf_counter.config;
696 strncpy(uctx->ctx.u.perf_counter.name, ctx->u.perf_counter.name,
697 LTTNG_UST_SYM_NAME_LEN);
698 uctx->ctx.u.perf_counter.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
699 break;
bdf64013
JG
700 case LTTNG_UST_CONTEXT_APP_CONTEXT:
701 {
702 char *provider_name = NULL, *ctx_name = NULL;
703
704 provider_name = strdup(ctx->u.app_ctx.provider_name);
705 if (!provider_name) {
706 goto error;
707 }
708 uctx->ctx.u.app_ctx.provider_name = provider_name;
709
710 ctx_name = strdup(ctx->u.app_ctx.ctx_name);
711 if (!ctx_name) {
712 goto error;
713 }
714 uctx->ctx.u.app_ctx.ctx_name = ctx_name;
715 break;
716 }
aa3514e9
MD
717 default:
718 break;
719 }
bec39940 720 lttng_ht_node_init_ulong(&uctx->node, (unsigned long) uctx->ctx.ctx);
bdf64013 721end:
55cc08a6 722 return uctx;
55cc08a6 723error:
bdf64013 724 trace_ust_destroy_context(uctx);
55cc08a6
DG
725 return NULL;
726}
727
55c9e7ca 728static void destroy_id_tracker_node_rcu(struct rcu_head *head)
a9ad0c8f 729{
55c9e7ca
JR
730 struct ust_id_tracker_node *tracker_node = caa_container_of(
731 head, struct ust_id_tracker_node, node.head);
a9ad0c8f
MD
732 free(tracker_node);
733}
734
55c9e7ca 735static void destroy_id_tracker_node(struct ust_id_tracker_node *tracker_node)
a9ad0c8f 736{
55c9e7ca 737 call_rcu(&tracker_node->node.head, destroy_id_tracker_node_rcu);
a9ad0c8f
MD
738}
739
55c9e7ca 740static int init_id_tracker(struct ust_id_tracker *id_tracker)
a9ad0c8f 741{
55c9e7ca 742 int ret = LTTNG_OK;
a9ad0c8f 743
55c9e7ca
JR
744 id_tracker->ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
745 if (!id_tracker->ht) {
746 ret = LTTNG_ERR_NOMEM;
a9ad0c8f
MD
747 goto end;
748 }
749
750end:
751 return ret;
752}
753
754/*
55c9e7ca 755 * Teardown id tracker content, but don't free id_tracker object.
a9ad0c8f 756 */
55c9e7ca 757static void fini_id_tracker(struct ust_id_tracker *id_tracker)
a9ad0c8f 758{
55c9e7ca 759 struct ust_id_tracker_node *tracker_node;
a9ad0c8f
MD
760 struct lttng_ht_iter iter;
761
55c9e7ca 762 if (!id_tracker->ht) {
a9ad0c8f
MD
763 return;
764 }
765 rcu_read_lock();
55c9e7ca
JR
766 cds_lfht_for_each_entry (id_tracker->ht->ht, &iter.iter, tracker_node,
767 node.node) {
768 int ret = lttng_ht_del(id_tracker->ht, &iter);
a9ad0c8f
MD
769
770 assert(!ret);
55c9e7ca 771 destroy_id_tracker_node(tracker_node);
a9ad0c8f
MD
772 }
773 rcu_read_unlock();
55c9e7ca
JR
774 ht_cleanup_push(id_tracker->ht);
775 id_tracker->ht = NULL;
a9ad0c8f
MD
776}
777
55c9e7ca
JR
778static struct ust_id_tracker_node *id_tracker_lookup(
779 struct ust_id_tracker *id_tracker,
780 int id,
a9ad0c8f
MD
781 struct lttng_ht_iter *iter)
782{
55c9e7ca 783 unsigned long _id = (unsigned long) id;
a9ad0c8f
MD
784 struct lttng_ht_node_ulong *node;
785
55c9e7ca 786 lttng_ht_lookup(id_tracker->ht, (void *) _id, iter);
a9ad0c8f
MD
787 node = lttng_ht_iter_get_node_ulong(iter);
788 if (node) {
55c9e7ca 789 return caa_container_of(node, struct ust_id_tracker_node, node);
a9ad0c8f
MD
790 } else {
791 return NULL;
792 }
793}
794
55c9e7ca 795static int id_tracker_add_id(struct ust_id_tracker *id_tracker, int id)
a9ad0c8f
MD
796{
797 int retval = LTTNG_OK;
55c9e7ca 798 struct ust_id_tracker_node *tracker_node;
a9ad0c8f
MD
799 struct lttng_ht_iter iter;
800
55c9e7ca 801 if (id < 0) {
a9ad0c8f
MD
802 retval = LTTNG_ERR_INVALID;
803 goto end;
804 }
55c9e7ca 805 tracker_node = id_tracker_lookup(id_tracker, id, &iter);
a9ad0c8f
MD
806 if (tracker_node) {
807 /* Already exists. */
159b042f 808 retval = LTTNG_ERR_PROCESS_ATTR_EXISTS;
a9ad0c8f
MD
809 goto end;
810 }
811 tracker_node = zmalloc(sizeof(*tracker_node));
812 if (!tracker_node) {
813 retval = LTTNG_ERR_NOMEM;
814 goto end;
815 }
55c9e7ca
JR
816 lttng_ht_node_init_ulong(&tracker_node->node, (unsigned long) id);
817 lttng_ht_add_unique_ulong(id_tracker->ht, &tracker_node->node);
a9ad0c8f
MD
818end:
819 return retval;
820}
821
55c9e7ca 822static int id_tracker_del_id(struct ust_id_tracker *id_tracker, int id)
a9ad0c8f
MD
823{
824 int retval = LTTNG_OK, ret;
55c9e7ca 825 struct ust_id_tracker_node *tracker_node;
a9ad0c8f
MD
826 struct lttng_ht_iter iter;
827
55c9e7ca 828 if (id < 0) {
a9ad0c8f
MD
829 retval = LTTNG_ERR_INVALID;
830 goto end;
831 }
55c9e7ca 832 tracker_node = id_tracker_lookup(id_tracker, id, &iter);
a9ad0c8f
MD
833 if (!tracker_node) {
834 /* Not found */
159b042f 835 retval = LTTNG_ERR_PROCESS_ATTR_MISSING;
a9ad0c8f
MD
836 goto end;
837 }
55c9e7ca 838 ret = lttng_ht_del(id_tracker->ht, &iter);
a9ad0c8f
MD
839 assert(!ret);
840
55c9e7ca 841 destroy_id_tracker_node(tracker_node);
a9ad0c8f
MD
842end:
843 return retval;
844}
845
55c9e7ca 846static struct ust_id_tracker *get_id_tracker(struct ltt_ust_session *session,
159b042f 847 enum lttng_process_attr process_attr)
55c9e7ca 848{
159b042f
JG
849 switch (process_attr) {
850 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID:
55c9e7ca 851 return &session->vpid_tracker;
159b042f 852 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID:
55c9e7ca 853 return &session->vuid_tracker;
159b042f 854 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID:
55c9e7ca
JR
855 return &session->vgid_tracker;
856 default:
857 return NULL;
858 }
859}
860
159b042f 861static struct process_attr_tracker *_trace_ust_get_process_attr_tracker(
55c9e7ca 862 struct ltt_ust_session *session,
159b042f 863 enum lttng_process_attr process_attr)
55c9e7ca 864{
159b042f
JG
865 switch (process_attr) {
866 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID:
867 return session->tracker_vpid;
868 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID:
869 return session->tracker_vuid;
870 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID:
871 return session->tracker_vgid;
55c9e7ca
JR
872 default:
873 return NULL;
874 }
875}
876
159b042f
JG
877const struct process_attr_tracker *trace_ust_get_process_attr_tracker(
878 struct ltt_ust_session *session,
879 enum lttng_process_attr process_attr)
880{
881 return (const struct process_attr_tracker *)
882 _trace_ust_get_process_attr_tracker(
883 session, process_attr);
884}
885
a9ad0c8f
MD
886/*
887 * The session lock is held when calling this function.
888 */
159b042f 889int trace_ust_id_tracker_lookup(enum lttng_process_attr process_attr,
55c9e7ca
JR
890 struct ltt_ust_session *session,
891 int id)
a9ad0c8f
MD
892{
893 struct lttng_ht_iter iter;
55c9e7ca 894 struct ust_id_tracker *id_tracker;
a9ad0c8f 895
159b042f 896 id_tracker = get_id_tracker(session, process_attr);
55c9e7ca
JR
897 if (!id_tracker) {
898 abort();
899 }
900 if (!id_tracker->ht) {
a9ad0c8f
MD
901 return 1;
902 }
55c9e7ca 903 if (id_tracker_lookup(id_tracker, id, &iter)) {
a9ad0c8f
MD
904 return 1;
905 }
906 return 0;
907}
908
909/*
910 * Called with the session lock held.
911 */
159b042f 912enum lttng_error_code trace_ust_process_attr_tracker_set_tracking_policy(
55c9e7ca 913 struct ltt_ust_session *session,
159b042f
JG
914 enum lttng_process_attr process_attr,
915 enum lttng_tracking_policy policy)
a9ad0c8f 916{
159b042f
JG
917 int ret;
918 enum lttng_error_code ret_code = LTTNG_OK;
919 struct ust_id_tracker *id_tracker =
920 get_id_tracker(session, process_attr);
921 struct process_attr_tracker *tracker =
922 _trace_ust_get_process_attr_tracker(
923 session, process_attr);
88e3c2f5 924 bool should_update_apps = false;
159b042f 925 enum lttng_tracking_policy previous_policy;
55c9e7ca 926
159b042f
JG
927 if (!tracker) {
928 ret_code = LTTNG_ERR_INVALID;
929 goto end;
55c9e7ca 930 }
a9ad0c8f 931
159b042f
JG
932 previous_policy = process_attr_tracker_get_tracking_policy(tracker);
933 ret = process_attr_tracker_set_tracking_policy(tracker, policy);
934 if (ret) {
935 ret_code = LTTNG_ERR_UNK;
55c9e7ca
JR
936 goto end;
937 }
938
159b042f
JG
939 if (previous_policy == policy) {
940 goto end;
55c9e7ca 941 }
159b042f
JG
942
943 switch (policy) {
944 case LTTNG_TRACKING_POLICY_INCLUDE_ALL:
945 /* Track all values: destroy tracker if exists. */
55c9e7ca
JR
946 if (id_tracker->ht) {
947 fini_id_tracker(id_tracker);
a9ad0c8f 948 /* Ensure all apps have session. */
88e3c2f5 949 should_update_apps = true;
a9ad0c8f 950 }
159b042f
JG
951 break;
952 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL:
953 case LTTNG_TRACKING_POLICY_INCLUDE_SET:
954 /* fall-through. */
955 fini_id_tracker(id_tracker);
956 ret_code = init_id_tracker(id_tracker);
957 if (ret_code != LTTNG_OK) {
958 ERR("Error initializing ID tracker");
959 goto end;
a9ad0c8f 960 }
159b042f
JG
961 /* Remove all apps from session. */
962 should_update_apps = true;
963 break;
964 default:
965 abort();
a9ad0c8f 966 }
88e3c2f5
JG
967 if (should_update_apps && session->active) {
968 ust_app_global_update_all(session);
969 }
a9ad0c8f 970end:
159b042f 971 return ret_code;
a9ad0c8f
MD
972}
973
159b042f
JG
974/* Called with the session lock held. */
975enum lttng_error_code trace_ust_process_attr_tracker_inclusion_set_add_value(
55c9e7ca 976 struct ltt_ust_session *session,
159b042f
JG
977 enum lttng_process_attr process_attr,
978 const struct process_attr_value *value)
a9ad0c8f 979{
159b042f 980 enum lttng_error_code ret_code = LTTNG_OK;
a5a30920 981 bool should_update_apps = false;
159b042f
JG
982 struct ust_id_tracker *id_tracker =
983 get_id_tracker(session, process_attr);
984 struct process_attr_tracker *tracker;
985 int integral_value;
986 enum process_attr_tracker_status status;
987 struct ust_app *app;
55c9e7ca 988
159b042f
JG
989 /*
990 * Convert process attribute tracker value to the integral
991 * representation required by the kern-ctl API.
992 */
993 switch (process_attr) {
994 case LTTNG_PROCESS_ATTR_PROCESS_ID:
995 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID:
996 integral_value = (int) value->value.pid;
997 break;
998 case LTTNG_PROCESS_ATTR_USER_ID:
999 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID:
1000 if (value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME) {
1001 uid_t uid;
1002
1003 ret_code = utils_user_id_from_name(
1004 value->value.user_name, &uid);
1005 if (ret_code != LTTNG_OK) {
1006 goto end;
1007 }
1008 integral_value = (int) uid;
1009 } else {
1010 integral_value = (int) value->value.uid;
1011 }
1012 break;
1013 case LTTNG_PROCESS_ATTR_GROUP_ID:
1014 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID:
1015 if (value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME) {
1016 gid_t gid;
1017
1018 ret_code = utils_group_id_from_name(
1019 value->value.group_name, &gid);
1020 if (ret_code != LTTNG_OK) {
1021 goto end;
1022 }
1023 integral_value = (int) gid;
1024 } else {
1025 integral_value = (int) value->value.gid;
1026 }
1027 break;
1028 default:
1029 ret_code = LTTNG_ERR_INVALID;
55c9e7ca
JR
1030 goto end;
1031 }
a9ad0c8f 1032
159b042f
JG
1033 tracker = _trace_ust_get_process_attr_tracker(session, process_attr);
1034 if (!tracker) {
1035 ret_code = LTTNG_ERR_INVALID;
74675e31 1036 goto end;
55c9e7ca
JR
1037 }
1038
159b042f
JG
1039 status = process_attr_tracker_inclusion_set_add_value(tracker, value);
1040 if (status != PROCESS_ATTR_TRACKER_STATUS_OK) {
1041 switch (status) {
1042 case PROCESS_ATTR_TRACKER_STATUS_EXISTS:
1043 ret_code = LTTNG_ERR_PROCESS_ATTR_EXISTS;
1044 break;
1045 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY:
1046 ret_code = LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY;
1047 break;
1048 case PROCESS_ATTR_TRACKER_STATUS_ERROR:
1049 default:
1050 ret_code = LTTNG_ERR_UNK;
1051 break;
a9ad0c8f 1052 }
159b042f
JG
1053 goto end;
1054 }
a9ad0c8f 1055
159b042f
JG
1056 DBG("User space track %s %d for session id %" PRIu64,
1057 lttng_process_attr_to_string(process_attr),
1058 integral_value, session->id);
a9ad0c8f 1059
159b042f
JG
1060 ret_code = id_tracker_add_id(id_tracker, integral_value);
1061 if (ret_code != LTTNG_OK) {
1062 goto end;
1063 }
1064 /* Add session to application */
1065 switch (process_attr) {
1066 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID:
1067 app = ust_app_find_by_pid(integral_value);
1068 if (app) {
a5a30920 1069 should_update_apps = true;
a9ad0c8f 1070 }
159b042f
JG
1071 break;
1072 default:
1073 should_update_apps = true;
1074 break;
a9ad0c8f 1075 }
a5a30920
JR
1076 if (should_update_apps && session->active) {
1077 ust_app_global_update_all(session);
1078 }
a9ad0c8f 1079end:
159b042f 1080 return ret_code;
a9ad0c8f
MD
1081}
1082
159b042f
JG
1083/* Called with the session lock held. */
1084enum lttng_error_code trace_ust_process_attr_tracker_inclusion_set_remove_value(
55c9e7ca 1085 struct ltt_ust_session *session,
159b042f
JG
1086 enum lttng_process_attr process_attr,
1087 const struct process_attr_value *value)
a5dfbb9d 1088{
159b042f
JG
1089 enum lttng_error_code ret_code = LTTNG_OK;
1090 bool should_update_apps = false;
1091 struct ust_id_tracker *id_tracker =
1092 get_id_tracker(session, process_attr);
1093 struct process_attr_tracker *tracker;
1094 int integral_value;
1095 enum process_attr_tracker_status status;
1096 struct ust_app *app;
a5dfbb9d 1097
159b042f
JG
1098 /*
1099 * Convert process attribute tracker value to the integral
1100 * representation required by the kern-ctl API.
1101 */
1102 switch (process_attr) {
1103 case LTTNG_PROCESS_ATTR_PROCESS_ID:
1104 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID:
1105 integral_value = (int) value->value.pid;
1106 break;
1107 case LTTNG_PROCESS_ATTR_USER_ID:
1108 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID:
1109 if (value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME) {
1110 uid_t uid;
1111
1112 ret_code = utils_user_id_from_name(
1113 value->value.user_name, &uid);
1114 if (ret_code != LTTNG_OK) {
1115 goto end;
1116 }
1117 integral_value = (int) uid;
1118 } else {
1119 integral_value = (int) value->value.uid;
1120 }
1121 break;
1122 case LTTNG_PROCESS_ATTR_GROUP_ID:
1123 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID:
1124 if (value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME) {
1125 gid_t gid;
1126
1127 ret_code = utils_group_id_from_name(
1128 value->value.group_name, &gid);
1129 if (ret_code != LTTNG_OK) {
1130 goto end;
1131 }
1132 integral_value = (int) gid;
1133 } else {
1134 integral_value = (int) value->value.gid;
1135 }
1136 break;
1137 default:
1138 ret_code = LTTNG_ERR_INVALID;
1139 goto end;
a5dfbb9d
MD
1140 }
1141
159b042f
JG
1142 tracker = _trace_ust_get_process_attr_tracker(session, process_attr);
1143 if (!tracker) {
1144 ret_code = LTTNG_ERR_INVALID;
74675e31 1145 goto end;
159b042f
JG
1146 }
1147
1148 status = process_attr_tracker_inclusion_set_remove_value(
1149 tracker, value);
1150 if (status != PROCESS_ATTR_TRACKER_STATUS_OK) {
1151 switch (status) {
1152 case PROCESS_ATTR_TRACKER_STATUS_MISSING:
1153 ret_code = LTTNG_ERR_PROCESS_ATTR_MISSING;
1154 break;
1155 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY:
1156 ret_code = LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY;
1157 break;
1158 case PROCESS_ATTR_TRACKER_STATUS_ERROR:
1159 default:
1160 ret_code = LTTNG_ERR_UNK;
1161 break;
1162 }
a7a533cd 1163 goto end;
a5dfbb9d 1164 }
a7a533cd 1165
159b042f
JG
1166 DBG("User space untrack %s %d for session id %" PRIu64,
1167 lttng_process_attr_to_string(process_attr),
1168 integral_value, session->id);
1169
1170 ret_code = id_tracker_del_id(id_tracker, integral_value);
1171 if (ret_code != LTTNG_OK) {
a7a533cd
JR
1172 goto end;
1173 }
159b042f
JG
1174 /* Add session to application */
1175 switch (process_attr) {
1176 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID:
1177 app = ust_app_find_by_pid(integral_value);
1178 if (app) {
1179 should_update_apps = true;
1180 }
1181 break;
1182 default:
1183 should_update_apps = true;
1184 break;
1185 }
1186 if (should_update_apps && session->active) {
1187 ust_app_global_update_all(session);
1188 }
a7a533cd 1189end:
159b042f 1190 return ret_code;
a5dfbb9d
MD
1191}
1192
f6a9efaa
DG
1193/*
1194 * RCU safe free context structure.
1195 */
1196static void destroy_context_rcu(struct rcu_head *head)
1197{
bec39940
DG
1198 struct lttng_ht_node_ulong *node =
1199 caa_container_of(head, struct lttng_ht_node_ulong, head);
f6a9efaa
DG
1200 struct ltt_ust_context *ctx =
1201 caa_container_of(node, struct ltt_ust_context, node);
1202
bdf64013 1203 trace_ust_destroy_context(ctx);
f6a9efaa
DG
1204}
1205
1206/*
1207 * Cleanup UST context hash table.
1208 */
7bd39047 1209static void destroy_contexts(struct lttng_ht *ht)
f6a9efaa
DG
1210{
1211 int ret;
bec39940
DG
1212 struct lttng_ht_node_ulong *node;
1213 struct lttng_ht_iter iter;
31746f93 1214 struct ltt_ust_context *ctx;
f6a9efaa 1215
0525e9ae
DG
1216 assert(ht);
1217
36b588ed 1218 rcu_read_lock();
bec39940 1219 cds_lfht_for_each_entry(ht->ht, &iter.iter, node, node) {
31746f93
DG
1220 /* Remove from ordered list. */
1221 ctx = caa_container_of(node, struct ltt_ust_context, node);
1222 cds_list_del(&ctx->list);
1223 /* Remove from channel's hash table. */
bec39940 1224 ret = lttng_ht_del(ht, &iter);
f6a9efaa
DG
1225 if (!ret) {
1226 call_rcu(&node->head, destroy_context_rcu);
1227 }
f6a9efaa 1228 }
36b588ed 1229 rcu_read_unlock();
ed52805d 1230
0b2dc8df 1231 ht_cleanup_push(ht);
f6a9efaa
DG
1232}
1233
97ee3a89
DG
1234/*
1235 * Cleanup ust event structure.
1236 */
1237void trace_ust_destroy_event(struct ltt_ust_event *event)
1238{
0525e9ae
DG
1239 assert(event);
1240
f6a9efaa 1241 DBG2("Trace destroy UST event %s", event->attr.name);
6b453b5e 1242 free(event->filter_expression);
53a80697 1243 free(event->filter);
40024f8a 1244 free(event->exclusion);
97ee3a89
DG
1245 free(event);
1246}
1247
bdf64013
JG
1248/*
1249 * Cleanup ust context structure.
1250 */
1251void trace_ust_destroy_context(struct ltt_ust_context *ctx)
1252{
1253 assert(ctx);
1254
1255 if (ctx->ctx.ctx == LTTNG_UST_CONTEXT_APP_CONTEXT) {
1256 free(ctx->ctx.u.app_ctx.provider_name);
1257 free(ctx->ctx.u.app_ctx.ctx_name);
1258 }
1259 free(ctx);
1260}
1261
f6a9efaa
DG
1262/*
1263 * URCU intermediate call to complete destroy event.
1264 */
1265static void destroy_event_rcu(struct rcu_head *head)
1266{
bec39940
DG
1267 struct lttng_ht_node_str *node =
1268 caa_container_of(head, struct lttng_ht_node_str, head);
f6a9efaa
DG
1269 struct ltt_ust_event *event =
1270 caa_container_of(node, struct ltt_ust_event, node);
1271
1272 trace_ust_destroy_event(event);
1273}
1274
284d8f55
DG
1275/*
1276 * Cleanup UST events hashtable.
1277 */
7bd39047 1278static void destroy_events(struct lttng_ht *events)
ed52805d
DG
1279{
1280 int ret;
bec39940
DG
1281 struct lttng_ht_node_str *node;
1282 struct lttng_ht_iter iter;
ed52805d 1283
0525e9ae
DG
1284 assert(events);
1285
36b588ed 1286 rcu_read_lock();
bec39940
DG
1287 cds_lfht_for_each_entry(events->ht, &iter.iter, node, node) {
1288 ret = lttng_ht_del(events, &iter);
7bd39047
DG
1289 assert(!ret);
1290 call_rcu(&node->head, destroy_event_rcu);
ed52805d 1291 }
36b588ed 1292 rcu_read_unlock();
ed52805d 1293
0b2dc8df 1294 ht_cleanup_push(events);
ed52805d
DG
1295}
1296
97ee3a89
DG
1297/*
1298 * Cleanup ust channel structure.
36b588ed
MD
1299 *
1300 * Should _NOT_ be called with RCU read lock held.
97ee3a89 1301 */
36b588ed 1302static void _trace_ust_destroy_channel(struct ltt_ust_channel *channel)
97ee3a89 1303{
0525e9ae
DG
1304 assert(channel);
1305
f6a9efaa 1306 DBG2("Trace destroy UST channel %s", channel->name);
97ee3a89 1307
97ee3a89 1308 free(channel);
f6a9efaa
DG
1309}
1310
1311/*
1312 * URCU intermediate call to complete destroy channel.
1313 */
1314static void destroy_channel_rcu(struct rcu_head *head)
1315{
bec39940
DG
1316 struct lttng_ht_node_str *node =
1317 caa_container_of(head, struct lttng_ht_node_str, head);
f6a9efaa
DG
1318 struct ltt_ust_channel *channel =
1319 caa_container_of(node, struct ltt_ust_channel, node);
1320
36b588ed
MD
1321 _trace_ust_destroy_channel(channel);
1322}
1323
1324void trace_ust_destroy_channel(struct ltt_ust_channel *channel)
1325{
627cbbe7
MD
1326 /* Destroying all events of the channel */
1327 destroy_events(channel->events);
1328 /* Destroying all context of the channel */
1329 destroy_contexts(channel->ctx);
1330
36b588ed 1331 call_rcu(&channel->node.head, destroy_channel_rcu);
97ee3a89
DG
1332}
1333
d5979e4a
DG
1334/*
1335 * Remove an UST channel from a channel HT.
1336 */
1337void trace_ust_delete_channel(struct lttng_ht *ht,
1338 struct ltt_ust_channel *channel)
1339{
1340 int ret;
1341 struct lttng_ht_iter iter;
1342
1343 assert(ht);
1344 assert(channel);
1345
1346 iter.iter.node = &channel->node.node;
1347 ret = lttng_ht_del(ht, &iter);
1348 assert(!ret);
1349}
1350
97ee3a89 1351/*
f6a9efaa 1352 * Iterate over a hash table containing channels and cleanup safely.
97ee3a89 1353 */
bec39940 1354static void destroy_channels(struct lttng_ht *channels)
f6a9efaa 1355{
bec39940
DG
1356 struct lttng_ht_node_str *node;
1357 struct lttng_ht_iter iter;
f6a9efaa 1358
0525e9ae
DG
1359 assert(channels);
1360
24d1723f 1361 rcu_read_lock();
bec39940 1362 cds_lfht_for_each_entry(channels->ht, &iter.iter, node, node) {
627cbbe7
MD
1363 struct ltt_ust_channel *chan =
1364 caa_container_of(node, struct ltt_ust_channel, node);
1365
1366 trace_ust_delete_channel(channels, chan);
1367 trace_ust_destroy_channel(chan);
f6a9efaa 1368 }
36b588ed 1369 rcu_read_unlock();
ed52805d 1370
0b2dc8df 1371 ht_cleanup_push(channels);
f6a9efaa
DG
1372}
1373
f6a9efaa
DG
1374/*
1375 * Cleanup UST global domain.
1376 */
1377static void destroy_domain_global(struct ltt_ust_domain_global *dom)
1378{
0525e9ae
DG
1379 assert(dom);
1380
f6a9efaa
DG
1381 destroy_channels(dom->channels);
1382}
97ee3a89 1383
f6a9efaa 1384/*
d070c424
MD
1385 * Cleanup ust session structure, keeping data required by
1386 * destroy notifier.
36b588ed
MD
1387 *
1388 * Should *NOT* be called with RCU read-side lock held.
f6a9efaa
DG
1389 */
1390void trace_ust_destroy_session(struct ltt_ust_session *session)
1391{
fefd409b 1392 struct agent *agt;
7972aab2 1393 struct buffer_reg_uid *reg, *sreg;
fefd409b 1394 struct lttng_ht_iter iter;
7972aab2 1395
0525e9ae 1396 assert(session);
97ee3a89 1397
d9bf3ca4 1398 DBG2("Trace UST destroy session %" PRIu64, session->id);
f6a9efaa 1399
f6a9efaa
DG
1400 /* Cleaning up UST domain */
1401 destroy_domain_global(&session->domain_global);
fefd409b 1402
0c0fcd77 1403 rcu_read_lock();
fefd409b 1404 cds_lfht_for_each_entry(session->agents->ht, &iter.iter, agt, node.node) {
d0edf546
JG
1405 int ret = lttng_ht_del(session->agents, &iter);
1406
1407 assert(!ret);
fefd409b
DG
1408 agent_destroy(agt);
1409 }
0c0fcd77 1410 rcu_read_unlock();
7972aab2 1411
8ada2175
MD
1412 ht_cleanup_push(session->agents);
1413
7972aab2
DG
1414 /* Cleanup UID buffer registry object(s). */
1415 cds_list_for_each_entry_safe(reg, sreg, &session->buffer_reg_uid_list,
1416 lnode) {
1417 cds_list_del(&reg->lnode);
1418 buffer_reg_uid_remove(reg);
1419 buffer_reg_uid_destroy(reg, session->consumer);
1420 }
44d3bd01 1421
159b042f
JG
1422 process_attr_tracker_destroy(session->tracker_vpid);
1423 process_attr_tracker_destroy(session->tracker_vuid);
1424 process_attr_tracker_destroy(session->tracker_vgid);
55c9e7ca
JR
1425
1426 fini_id_tracker(&session->vpid_tracker);
1427 fini_id_tracker(&session->vuid_tracker);
1428 fini_id_tracker(&session->vgid_tracker);
82b69413 1429 lttng_trace_chunk_put(session->current_trace_chunk);
d070c424
MD
1430}
1431
1432/* Free elements needed by destroy notifiers. */
1433void trace_ust_free_session(struct ltt_ust_session *session)
1434{
1435 consumer_output_put(session->consumer);
97ee3a89
DG
1436 free(session);
1437}
This page took 0.157484 seconds and 5 git commands to generate.