Add UST namespace contexts
[lttng-tools.git] / src / bin / lttng-sessiond / trace-ust.c
CommitLineData
97ee3a89
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
bdf64013 3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
97ee3a89 4 *
d14d33bf
AM
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
97ee3a89 8 *
d14d33bf
AM
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
97ee3a89 13 *
d14d33bf
AM
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
97ee3a89
DG
17 */
18
6c1c0768 19#define _LGPL_SOURCE
97ee3a89
DG
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <unistd.h>
d9bf3ca4 24#include <inttypes.h>
97ee3a89 25
990570ed
DG
26#include <common/common.h>
27#include <common/defaults.h>
82b69413 28#include <common/trace-chunk.h>
97ee3a89 29
7972aab2 30#include "buffer-registry.h"
97ee3a89 31#include "trace-ust.h"
0b2dc8df 32#include "utils.h"
a9ad0c8f 33#include "ust-app.h"
7c1d2758 34#include "agent.h"
97ee3a89 35
025faf73
DG
36/*
37 * Match function for the events hash table lookup.
38 *
39 * Matches by name only. Used by the disable command.
40 */
18eace3b
DG
41int trace_ust_ht_match_event_by_name(struct cds_lfht_node *node,
42 const void *_key)
43{
44 struct ltt_ust_event *event;
45 const char *name;
46
47 assert(node);
48 assert(_key);
49
50 event = caa_container_of(node, struct ltt_ust_event, node.node);
51 name = _key;
52
53 /* Event name */
54 if (strncmp(event->attr.name, name, sizeof(event->attr.name)) != 0) {
55 goto no_match;
56 }
57
025faf73 58 /* Match */
18eace3b
DG
59 return 1;
60
61no_match:
62 return 0;
63}
64
025faf73
DG
65/*
66 * Match function for the hash table lookup.
67 *
68 * It matches an ust event based on three attributes which are the event name,
69 * the filter bytecode and the loglevel.
70 */
18eace3b
DG
71int trace_ust_ht_match_event(struct cds_lfht_node *node, const void *_key)
72{
73 struct ltt_ust_event *event;
74 const struct ltt_ust_ht_key *key;
2106efa0 75 int ev_loglevel_value;
19a97244 76 int ll_match;
18eace3b
DG
77
78 assert(node);
79 assert(_key);
80
81 event = caa_container_of(node, struct ltt_ust_event, node.node);
82 key = _key;
2106efa0 83 ev_loglevel_value = event->attr.loglevel;
18eace3b 84
f19e9f8b 85 /* Match the 4 elements of the key: name, filter, loglevel, exclusions. */
18eace3b
DG
86
87 /* Event name */
88 if (strncmp(event->attr.name, key->name, sizeof(event->attr.name)) != 0) {
18eace3b
DG
89 goto no_match;
90 }
91
b953b8cd 92 /* Event loglevel value and type. */
19a97244
PP
93 ll_match = loglevels_match(event->attr.loglevel_type,
94 ev_loglevel_value, key->loglevel_type,
95 key->loglevel_value, LTTNG_UST_LOGLEVEL_ALL);
96
97 if (!ll_match) {
b953b8cd 98 goto no_match;
18eace3b
DG
99 }
100
101 /* Only one of the filters is NULL, fail. */
102 if ((key->filter && !event->filter) || (!key->filter && event->filter)) {
18eace3b
DG
103 goto no_match;
104 }
105
025faf73
DG
106 if (key->filter && event->filter) {
107 /* Both filters exists, check length followed by the bytecode. */
108 if (event->filter->len != key->filter->len ||
109 memcmp(event->filter->data, key->filter->data,
110 event->filter->len) != 0) {
111 goto no_match;
112 }
18eace3b
DG
113 }
114
f19e9f8b
JI
115 /* If only one of the exclusions is NULL, fail. */
116 if ((key->exclusion && !event->exclusion) || (!key->exclusion && event->exclusion)) {
117 goto no_match;
118 }
119
120 if (key->exclusion && event->exclusion) {
a5b7e00c
PP
121 size_t i;
122
123 /* Check exclusion counts first. */
124 if (event->exclusion->count != key->exclusion->count) {
f19e9f8b
JI
125 goto no_match;
126 }
a5b7e00c
PP
127
128 /* Compare names individually. */
129 for (i = 0; i < event->exclusion->count; ++i) {
130 size_t j;
131 bool found = false;
132 const char *name_ev =
133 LTTNG_EVENT_EXCLUSION_NAME_AT(
134 event->exclusion, i);
135
136 /*
137 * Compare this exclusion name to all the key's
138 * exclusion names.
139 */
140 for (j = 0; j < key->exclusion->count; ++j) {
141 const char *name_key =
142 LTTNG_EVENT_EXCLUSION_NAME_AT(
143 key->exclusion, j);
144
145 if (!strncmp(name_ev, name_key,
146 LTTNG_SYMBOL_NAME_LEN)) {
147 /* Names match! */
148 found = true;
149 break;
150 }
151 }
152
153 /*
154 * If the current exclusion name was not found amongst
155 * the key's exclusion names, then there's no match.
156 */
157 if (!found) {
158 goto no_match;
159 }
160 }
f19e9f8b 161 }
025faf73
DG
162 /* Match. */
163 return 1;
18eace3b
DG
164
165no_match:
166 return 0;
18eace3b
DG
167}
168
0177d773 169/*
2223c96f
DG
170 * Find the channel in the hashtable and return channel pointer. RCU read side
171 * lock MUST be acquired before calling this.
0177d773 172 */
bec39940 173struct ltt_ust_channel *trace_ust_find_channel_by_name(struct lttng_ht *ht,
df4f5a87 174 const char *name)
0177d773 175{
bec39940
DG
176 struct lttng_ht_node_str *node;
177 struct lttng_ht_iter iter;
0177d773 178
85076754
MD
179 /*
180 * If we receive an empty string for channel name, it means the
181 * default channel name is requested.
182 */
183 if (name[0] == '\0')
184 name = DEFAULT_CHANNEL_NAME;
185
bec39940
DG
186 lttng_ht_lookup(ht, (void *)name, &iter);
187 node = lttng_ht_iter_get_node_str(&iter);
f6a9efaa 188 if (node == NULL) {
44d3bd01
DG
189 goto error;
190 }
191
f6a9efaa 192 DBG2("Trace UST channel %s found by name", name);
0177d773 193
f6a9efaa 194 return caa_container_of(node, struct ltt_ust_channel, node);
97ee3a89
DG
195
196error:
f6a9efaa 197 DBG2("Trace UST channel %s not found by name", name);
97ee3a89
DG
198 return NULL;
199}
200
201/*
2223c96f
DG
202 * Find the event in the hashtable and return event pointer. RCU read side lock
203 * MUST be acquired before calling this.
97ee3a89 204 */
18eace3b 205struct ltt_ust_event *trace_ust_find_event(struct lttng_ht *ht,
2106efa0 206 char *name, struct lttng_filter_bytecode *filter,
b953b8cd
PP
207 enum lttng_ust_loglevel_type loglevel_type, int loglevel_value,
208 struct lttng_event_exclusion *exclusion)
97ee3a89 209{
bec39940
DG
210 struct lttng_ht_node_str *node;
211 struct lttng_ht_iter iter;
18eace3b 212 struct ltt_ust_ht_key key;
97ee3a89 213
18eace3b
DG
214 assert(name);
215 assert(ht);
216
217 key.name = name;
218 key.filter = filter;
b953b8cd
PP
219 key.loglevel_type = loglevel_type;
220 key.loglevel_value = loglevel_value;
10646003 221 key.exclusion = exclusion;
18eace3b 222
18eace3b
DG
223 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
224 trace_ust_ht_match_event, &key, &iter.iter);
bec39940 225 node = lttng_ht_iter_get_node_str(&iter);
f6a9efaa 226 if (node == NULL) {
97ee3a89
DG
227 goto error;
228 }
229
18eace3b 230 DBG2("Trace UST event %s found", key.name);
f6a9efaa
DG
231
232 return caa_container_of(node, struct ltt_ust_event, node);
97ee3a89
DG
233
234error:
18eace3b 235 DBG2("Trace UST event %s NOT found", key.name);
97ee3a89
DG
236 return NULL;
237}
238
fefd409b
DG
239/*
240 * Lookup an agent in the session agents hash table by domain type and return
241 * the object if found else NULL.
4da703ad
JG
242 *
243 * RCU read side lock must be acquired before calling and only released
244 * once the agent is no longer in scope or being used.
fefd409b
DG
245 */
246struct agent *trace_ust_find_agent(struct ltt_ust_session *session,
247 enum lttng_domain_type domain_type)
248{
249 struct agent *agt = NULL;
250 struct lttng_ht_node_u64 *node;
251 struct lttng_ht_iter iter;
252 uint64_t key;
253
254 assert(session);
255
256 DBG3("Trace ust agent lookup for domain %d", domain_type);
257
258 key = domain_type;
259
260 lttng_ht_lookup(session->agents, &key, &iter);
261 node = lttng_ht_iter_get_node_u64(&iter);
262 if (!node) {
263 goto end;
264 }
265 agt = caa_container_of(node, struct agent, node);
266
267end:
268 return agt;
269}
270
97ee3a89
DG
271/*
272 * Allocate and initialize a ust session data structure.
273 *
274 * Return pointer to structure or NULL.
275 */
d9bf3ca4 276struct ltt_ust_session *trace_ust_create_session(uint64_t session_id)
97ee3a89
DG
277{
278 struct ltt_ust_session *lus;
279
280 /* Allocate a new ltt ust session */
ba7f0ae5 281 lus = zmalloc(sizeof(struct ltt_ust_session));
97ee3a89 282 if (lus == NULL) {
ba7f0ae5 283 PERROR("create ust session zmalloc");
97ee3a89
DG
284 goto error;
285 }
286
287 /* Init data structure */
a991f516 288 lus->id = session_id;
14fb1ebe 289 lus->active = 0;
97ee3a89 290
84ad93e8
DG
291 /* Set default metadata channel attribute. */
292 lus->metadata_attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
293 lus->metadata_attr.subbuf_size = default_get_metadata_subbuf_size();
294 lus->metadata_attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
295 lus->metadata_attr.switch_timer_interval = DEFAULT_METADATA_SWITCH_TIMER;
296 lus->metadata_attr.read_timer_interval = DEFAULT_METADATA_READ_TIMER;
297 lus->metadata_attr.output = LTTNG_UST_MMAP;
298
7972aab2
DG
299 /*
300 * Default buffer type. This can be changed through an enable channel
301 * requesting a different type. Note that this can only be changed once
302 * during the session lifetime which is at the first enable channel and
303 * only before start. The flag buffer_type_changed indicates the status.
304 */
8692d4e5 305 lus->buffer_type = LTTNG_BUFFER_PER_UID;
7972aab2
DG
306 /* Once set to 1, the buffer_type is immutable for the session. */
307 lus->buffer_type_changed = 0;
308 /* Init it in case it get used after allocation. */
309 CDS_INIT_LIST_HEAD(&lus->buffer_reg_uid_list);
f6a9efaa
DG
310
311 /* Alloc UST global domain channels' HT */
bec39940 312 lus->domain_global.channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
fefd409b
DG
313 /* Alloc agent hash table. */
314 lus->agents = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
44d3bd01 315
00e2e675
DG
316 lus->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
317 if (lus->consumer == NULL) {
09a90bcd 318 goto error_consumer;
00e2e675
DG
319 }
320
44d3bd01
DG
321 DBG2("UST trace session create successful");
322
97ee3a89
DG
323 return lus;
324
09a90bcd 325error_consumer:
0b2dc8df 326 ht_cleanup_push(lus->domain_global.channels);
fefd409b 327 ht_cleanup_push(lus->agents);
44844c29 328 free(lus);
97ee3a89
DG
329error:
330 return NULL;
331}
332
333/*
334 * Allocate and initialize a ust channel data structure.
335 *
336 * Return pointer to structure or NULL.
337 */
51755dc8
JG
338struct ltt_ust_channel *trace_ust_create_channel(struct lttng_channel *chan,
339 enum lttng_domain_type domain)
97ee3a89 340{
97ee3a89
DG
341 struct ltt_ust_channel *luc;
342
0525e9ae 343 assert(chan);
0525e9ae 344
37357452 345 luc = zmalloc(sizeof(struct ltt_ust_channel));
97ee3a89 346 if (luc == NULL) {
df0f840b 347 PERROR("ltt_ust_channel zmalloc");
97ee3a89
DG
348 goto error;
349 }
350
51755dc8
JG
351 luc->domain = domain;
352
44d3bd01 353 /* Copy UST channel attributes */
48842b30
DG
354 luc->attr.overwrite = chan->attr.overwrite;
355 luc->attr.subbuf_size = chan->attr.subbuf_size;
356 luc->attr.num_subbuf = chan->attr.num_subbuf;
357 luc->attr.switch_timer_interval = chan->attr.switch_timer_interval;
358 luc->attr.read_timer_interval = chan->attr.read_timer_interval;
6775595e 359 luc->attr.output = (enum lttng_ust_output) chan->attr.output;
82b4ebce
JG
360 luc->monitor_timer_interval = ((struct lttng_channel_extended *)
361 chan->attr.extended.ptr)->monitor_timer_interval;
491d1539
MD
362 luc->attr.u.s.blocking_timeout = ((struct lttng_channel_extended *)
363 chan->attr.extended.ptr)->blocking_timeout;
44d3bd01
DG
364
365 /* Translate to UST output enum */
366 switch (luc->attr.output) {
367 default:
368 luc->attr.output = LTTNG_UST_MMAP;
369 break;
97ee3a89 370 }
97ee3a89 371
85076754
MD
372 /*
373 * If we receive an empty string for channel name, it means the
374 * default channel name is requested.
375 */
376 if (chan->name[0] == '\0') {
377 strncpy(luc->name, DEFAULT_CHANNEL_NAME, sizeof(luc->name));
378 } else {
379 /* Copy channel name */
380 strncpy(luc->name, chan->name, sizeof(luc->name));
381 }
97ee3a89
DG
382 luc->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
383
f6a9efaa 384 /* Init node */
bec39940 385 lttng_ht_node_init_str(&luc->node, luc->name);
31746f93
DG
386 CDS_INIT_LIST_HEAD(&luc->ctx_list);
387
f6a9efaa 388 /* Alloc hash tables */
bec39940
DG
389 luc->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
390 luc->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
f6a9efaa 391
1624d5b7
JD
392 /* On-disk circular buffer parameters */
393 luc->tracefile_size = chan->attr.tracefile_size;
394 luc->tracefile_count = chan->attr.tracefile_count;
395
f6a9efaa
DG
396 DBG2("Trace UST channel %s created", luc->name);
397
97ee3a89 398error:
7972aab2 399 return luc;
97ee3a89
DG
400}
401
ad11a1d3
PP
402/*
403 * Validates an exclusion list.
404 *
405 * Returns 0 if valid, negative value if invalid.
406 */
407static int validate_exclusion(struct lttng_event_exclusion *exclusion)
408{
409 size_t i;
410 int ret = 0;
411
412 assert(exclusion);
413
414 for (i = 0; i < exclusion->count; ++i) {
415 size_t j;
416 const char *name_a =
417 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, i);
418
419 for (j = 0; j < i; ++j) {
420 const char *name_b =
421 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, j);
422
423 if (!strncmp(name_a, name_b, LTTNG_SYMBOL_NAME_LEN)) {
424 /* Match! */
425 ret = -1;
426 goto end;
427 }
428 }
429 }
430
431end:
432 return ret;
433}
434
97ee3a89
DG
435/*
436 * Allocate and initialize a ust event. Set name and event type.
49d21f93 437 * We own filter_expression, filter, and exclusion.
97ee3a89 438 *
39687410 439 * Return an lttng_error_code
97ee3a89 440 */
39687410 441enum lttng_error_code trace_ust_create_event(struct lttng_event *ev,
6b453b5e 442 char *filter_expression,
561c6897 443 struct lttng_filter_bytecode *filter,
88f06f15 444 struct lttng_event_exclusion *exclusion,
39687410
FD
445 bool internal_event,
446 struct ltt_ust_event **ust_event)
97ee3a89 447{
39687410
FD
448 struct ltt_ust_event *local_ust_event;
449 enum lttng_error_code ret = LTTNG_OK;
97ee3a89 450
0525e9ae
DG
451 assert(ev);
452
ad11a1d3 453 if (exclusion && validate_exclusion(exclusion)) {
39687410 454 ret = LTTNG_ERR_INVALID;
ad11a1d3
PP
455 goto error;
456 }
457
39687410
FD
458 local_ust_event = zmalloc(sizeof(struct ltt_ust_event));
459 if (local_ust_event == NULL) {
ba7f0ae5 460 PERROR("ust event zmalloc");
39687410 461 ret = LTTNG_ERR_NOMEM;
97ee3a89
DG
462 goto error;
463 }
464
39687410 465 local_ust_event->internal = internal_event;
88f06f15 466
97ee3a89
DG
467 switch (ev->type) {
468 case LTTNG_EVENT_PROBE:
39687410 469 local_ust_event->attr.instrumentation = LTTNG_UST_PROBE;
97ee3a89
DG
470 break;
471 case LTTNG_EVENT_FUNCTION:
39687410 472 local_ust_event->attr.instrumentation = LTTNG_UST_FUNCTION;
97ee3a89
DG
473 break;
474 case LTTNG_EVENT_FUNCTION_ENTRY:
39687410 475 local_ust_event->attr.instrumentation = LTTNG_UST_FUNCTION;
97ee3a89
DG
476 break;
477 case LTTNG_EVENT_TRACEPOINT:
39687410 478 local_ust_event->attr.instrumentation = LTTNG_UST_TRACEPOINT;
97ee3a89
DG
479 break;
480 default:
481 ERR("Unknown ust instrumentation type (%d)", ev->type);
39687410 482 ret = LTTNG_ERR_INVALID;
44844c29 483 goto error_free_event;
97ee3a89
DG
484 }
485
486 /* Copy event name */
39687410
FD
487 strncpy(local_ust_event->attr.name, ev->name, LTTNG_UST_SYM_NAME_LEN);
488 local_ust_event->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
97ee3a89 489
0cda4f28 490 switch (ev->loglevel_type) {
8005f29a 491 case LTTNG_EVENT_LOGLEVEL_ALL:
39687410
FD
492 local_ust_event->attr.loglevel_type = LTTNG_UST_LOGLEVEL_ALL;
493 local_ust_event->attr.loglevel = -1; /* Force to -1 */
0cda4f28 494 break;
8005f29a 495 case LTTNG_EVENT_LOGLEVEL_RANGE:
39687410
FD
496 local_ust_event->attr.loglevel_type = LTTNG_UST_LOGLEVEL_RANGE;
497 local_ust_event->attr.loglevel = ev->loglevel;
8005f29a
MD
498 break;
499 case LTTNG_EVENT_LOGLEVEL_SINGLE:
39687410
FD
500 local_ust_event->attr.loglevel_type = LTTNG_UST_LOGLEVEL_SINGLE;
501 local_ust_event->attr.loglevel = ev->loglevel;
0cda4f28
MD
502 break;
503 default:
4431494b 504 ERR("Unknown ust loglevel type (%d)", ev->loglevel_type);
39687410 505 ret = LTTNG_ERR_INVALID;
0cda4f28
MD
506 goto error_free_event;
507 }
508
025faf73 509 /* Same layout. */
39687410
FD
510 local_ust_event->filter_expression = filter_expression;
511 local_ust_event->filter = filter;
512 local_ust_event->exclusion = exclusion;
0cda4f28 513
f6a9efaa 514 /* Init node */
39687410 515 lttng_ht_node_init_str(&local_ust_event->node, local_ust_event->attr.name);
97ee3a89 516
300b8fd5 517 DBG2("Trace UST event %s, loglevel (%d,%d) created",
39687410
FD
518 local_ust_event->attr.name, local_ust_event->attr.loglevel_type,
519 local_ust_event->attr.loglevel);
520
521 *ust_event = local_ust_event;
284d8f55 522
39687410 523 return ret;
97ee3a89 524
44844c29 525error_free_event:
39687410 526 free(local_ust_event);
97ee3a89 527error:
49d21f93
MD
528 free(filter_expression);
529 free(filter);
530 free(exclusion);
39687410 531 return ret;
97ee3a89
DG
532}
533
aa3514e9 534static
bdf64013
JG
535int trace_ust_context_type_event_to_ust(
536 enum lttng_event_context_type type)
55cc08a6 537{
aa3514e9 538 int utype;
0525e9ae 539
aa3514e9 540 switch (type) {
9197c5c4
MD
541 case LTTNG_EVENT_CONTEXT_VTID:
542 utype = LTTNG_UST_CONTEXT_VTID;
543 break;
544 case LTTNG_EVENT_CONTEXT_VPID:
545 utype = LTTNG_UST_CONTEXT_VPID;
546 break;
547 case LTTNG_EVENT_CONTEXT_PTHREAD_ID:
548 utype = LTTNG_UST_CONTEXT_PTHREAD_ID;
549 break;
550 case LTTNG_EVENT_CONTEXT_PROCNAME:
551 utype = LTTNG_UST_CONTEXT_PROCNAME;
552 break;
7c612c2e
WP
553 case LTTNG_EVENT_CONTEXT_IP:
554 utype = LTTNG_UST_CONTEXT_IP;
555 break;
aa3514e9 556 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
354e561b
MD
557 if (!ustctl_has_perf_counters()) {
558 utype = -1;
559 WARN("Perf counters not implemented in UST");
560 } else {
561 utype = LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER;
562 }
aa3514e9 563 break;
bdf64013
JG
564 case LTTNG_EVENT_CONTEXT_APP_CONTEXT:
565 utype = LTTNG_UST_CONTEXT_APP_CONTEXT;
566 break;
f17b8732
MJ
567 case LTTNG_EVENT_CONTEXT_CGROUP_NS:
568 utype = LTTNG_UST_CONTEXT_CGROUP_NS;
569 break;
570 case LTTNG_EVENT_CONTEXT_IPC_NS:
571 utype = LTTNG_UST_CONTEXT_IPC_NS;
572 break;
573 case LTTNG_EVENT_CONTEXT_MNT_NS:
574 utype = LTTNG_UST_CONTEXT_MNT_NS;
575 break;
576 case LTTNG_EVENT_CONTEXT_NET_NS:
577 utype = LTTNG_UST_CONTEXT_NET_NS;
578 break;
579 case LTTNG_EVENT_CONTEXT_PID_NS:
580 utype = LTTNG_UST_CONTEXT_PID_NS;
581 break;
582 case LTTNG_EVENT_CONTEXT_USER_NS:
583 utype = LTTNG_UST_CONTEXT_USER_NS;
584 break;
585 case LTTNG_EVENT_CONTEXT_UTS_NS:
586 utype = LTTNG_UST_CONTEXT_UTS_NS;
587 break;
9197c5c4 588 default:
aa3514e9
MD
589 utype = -1;
590 break;
591 }
592 return utype;
593}
594
595/*
596 * Return 1 if contexts match, 0 otherwise.
597 */
df4f5a87
JG
598int trace_ust_match_context(const struct ltt_ust_context *uctx,
599 const struct lttng_event_context *ctx)
aa3514e9
MD
600{
601 int utype;
602
603 utype = trace_ust_context_type_event_to_ust(ctx->ctx);
604 if (utype < 0) {
605 return 0;
606 }
607 if (uctx->ctx.ctx != utype) {
608 return 0;
609 }
610 switch (utype) {
611 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER:
612 if (uctx->ctx.u.perf_counter.type
613 != ctx->u.perf_counter.type) {
614 return 0;
615 }
616 if (uctx->ctx.u.perf_counter.config
617 != ctx->u.perf_counter.config) {
618 return 0;
619 }
620 if (strncmp(uctx->ctx.u.perf_counter.name,
621 ctx->u.perf_counter.name,
622 LTTNG_UST_SYM_NAME_LEN)) {
623 return 0;
624 }
625 break;
54fb33cf
JG
626 case LTTNG_UST_CONTEXT_APP_CONTEXT:
627 assert(uctx->ctx.u.app_ctx.provider_name);
628 assert(uctx->ctx.u.app_ctx.ctx_name);
629 if (strcmp(uctx->ctx.u.app_ctx.provider_name,
630 ctx->u.app_ctx.provider_name) ||
631 strcmp(uctx->ctx.u.app_ctx.ctx_name,
632 ctx->u.app_ctx.ctx_name)) {
633 return 0;
634 }
aa3514e9
MD
635 default:
636 break;
637
638 }
639 return 1;
640}
641
642/*
643 * Allocate and initialize an UST context.
644 *
645 * Return pointer to structure or NULL.
646 */
647struct ltt_ust_context *trace_ust_create_context(
df4f5a87 648 const struct lttng_event_context *ctx)
aa3514e9 649{
bdf64013 650 struct ltt_ust_context *uctx = NULL;
aa3514e9
MD
651 int utype;
652
653 assert(ctx);
654
655 utype = trace_ust_context_type_event_to_ust(ctx->ctx);
656 if (utype < 0) {
1cf992b8 657 ERR("Invalid UST context");
bdf64013 658 goto end;
9197c5c4 659 }
55cc08a6
DG
660
661 uctx = zmalloc(sizeof(struct ltt_ust_context));
bdf64013 662 if (!uctx) {
55cc08a6 663 PERROR("zmalloc ltt_ust_context");
bdf64013 664 goto end;
55cc08a6
DG
665 }
666
aa3514e9
MD
667 uctx->ctx.ctx = (enum lttng_ust_context_type) utype;
668 switch (utype) {
669 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER:
670 uctx->ctx.u.perf_counter.type = ctx->u.perf_counter.type;
671 uctx->ctx.u.perf_counter.config = ctx->u.perf_counter.config;
672 strncpy(uctx->ctx.u.perf_counter.name, ctx->u.perf_counter.name,
673 LTTNG_UST_SYM_NAME_LEN);
674 uctx->ctx.u.perf_counter.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
675 break;
bdf64013
JG
676 case LTTNG_UST_CONTEXT_APP_CONTEXT:
677 {
678 char *provider_name = NULL, *ctx_name = NULL;
679
680 provider_name = strdup(ctx->u.app_ctx.provider_name);
681 if (!provider_name) {
682 goto error;
683 }
684 uctx->ctx.u.app_ctx.provider_name = provider_name;
685
686 ctx_name = strdup(ctx->u.app_ctx.ctx_name);
687 if (!ctx_name) {
688 goto error;
689 }
690 uctx->ctx.u.app_ctx.ctx_name = ctx_name;
691 break;
692 }
aa3514e9
MD
693 default:
694 break;
695 }
bec39940 696 lttng_ht_node_init_ulong(&uctx->node, (unsigned long) uctx->ctx.ctx);
bdf64013 697end:
55cc08a6 698 return uctx;
55cc08a6 699error:
bdf64013 700 trace_ust_destroy_context(uctx);
55cc08a6
DG
701 return NULL;
702}
703
a9ad0c8f
MD
704static
705void destroy_pid_tracker_node_rcu(struct rcu_head *head)
706{
707 struct ust_pid_tracker_node *tracker_node =
708 caa_container_of(head, struct ust_pid_tracker_node, node.head);
709 free(tracker_node);
710}
711
712static
713void destroy_pid_tracker_node(struct ust_pid_tracker_node *tracker_node)
714{
715
716 call_rcu(&tracker_node->node.head, destroy_pid_tracker_node_rcu);
717}
718
719static
720int init_pid_tracker(struct ust_pid_tracker *pid_tracker)
721{
722 int ret = 0;
723
724 pid_tracker->ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
725 if (!pid_tracker->ht) {
726 ret = -1;
727 goto end;
728 }
729
730end:
731 return ret;
732}
733
734/*
735 * Teardown pid tracker content, but don't free pid_tracker object.
736 */
737static
738void fini_pid_tracker(struct ust_pid_tracker *pid_tracker)
739{
740 struct ust_pid_tracker_node *tracker_node;
741 struct lttng_ht_iter iter;
742
743 if (!pid_tracker->ht) {
744 return;
745 }
746 rcu_read_lock();
747 cds_lfht_for_each_entry(pid_tracker->ht->ht,
748 &iter.iter, tracker_node, node.node) {
749 int ret = lttng_ht_del(pid_tracker->ht, &iter);
750
751 assert(!ret);
752 destroy_pid_tracker_node(tracker_node);
753 }
754 rcu_read_unlock();
755 ht_cleanup_push(pid_tracker->ht);
756 pid_tracker->ht = NULL;
757}
758
759static
760struct ust_pid_tracker_node *pid_tracker_lookup(
761 struct ust_pid_tracker *pid_tracker, int pid,
762 struct lttng_ht_iter *iter)
763{
764 unsigned long _pid = (unsigned long) pid;
765 struct lttng_ht_node_ulong *node;
766
767 lttng_ht_lookup(pid_tracker->ht, (void *) _pid, iter);
768 node = lttng_ht_iter_get_node_ulong(iter);
769 if (node) {
770 return caa_container_of(node, struct ust_pid_tracker_node,
771 node);
772 } else {
773 return NULL;
774 }
775}
776
777static
778int pid_tracker_add_pid(struct ust_pid_tracker *pid_tracker, int pid)
779{
780 int retval = LTTNG_OK;
781 struct ust_pid_tracker_node *tracker_node;
782 struct lttng_ht_iter iter;
783
784 if (pid < 0) {
785 retval = LTTNG_ERR_INVALID;
786 goto end;
787 }
788 tracker_node = pid_tracker_lookup(pid_tracker, pid, &iter);
789 if (tracker_node) {
790 /* Already exists. */
b93a4a13 791 retval = LTTNG_ERR_PID_TRACKED;
a9ad0c8f
MD
792 goto end;
793 }
794 tracker_node = zmalloc(sizeof(*tracker_node));
795 if (!tracker_node) {
796 retval = LTTNG_ERR_NOMEM;
797 goto end;
798 }
799 lttng_ht_node_init_ulong(&tracker_node->node, (unsigned long) pid);
800 lttng_ht_add_unique_ulong(pid_tracker->ht, &tracker_node->node);
801end:
802 return retval;
803}
804
805static
806int pid_tracker_del_pid(struct ust_pid_tracker *pid_tracker, int pid)
807{
808 int retval = LTTNG_OK, ret;
809 struct ust_pid_tracker_node *tracker_node;
810 struct lttng_ht_iter iter;
811
812 if (pid < 0) {
813 retval = LTTNG_ERR_INVALID;
814 goto end;
815 }
816 tracker_node = pid_tracker_lookup(pid_tracker, pid, &iter);
817 if (!tracker_node) {
818 /* Not found */
b93a4a13 819 retval = LTTNG_ERR_PID_NOT_TRACKED;
a9ad0c8f
MD
820 goto end;
821 }
822 ret = lttng_ht_del(pid_tracker->ht, &iter);
823 assert(!ret);
824
825 destroy_pid_tracker_node(tracker_node);
826end:
827 return retval;
828}
829
830/*
831 * The session lock is held when calling this function.
832 */
833int trace_ust_pid_tracker_lookup(struct ltt_ust_session *session, int pid)
834{
835 struct lttng_ht_iter iter;
836
837 if (!session->pid_tracker.ht) {
838 return 1;
839 }
840 if (pid_tracker_lookup(&session->pid_tracker, pid, &iter)) {
841 return 1;
842 }
843 return 0;
844}
845
846/*
847 * Called with the session lock held.
848 */
849int trace_ust_track_pid(struct ltt_ust_session *session, int pid)
850{
851 int retval = LTTNG_OK;
88e3c2f5 852 bool should_update_apps = false;
a9ad0c8f
MD
853
854 if (pid == -1) {
855 /* Track all pids: destroy tracker if exists. */
856 if (session->pid_tracker.ht) {
857 fini_pid_tracker(&session->pid_tracker);
858 /* Ensure all apps have session. */
88e3c2f5 859 should_update_apps = true;
a9ad0c8f
MD
860 }
861 } else {
862 int ret;
863
864 if (!session->pid_tracker.ht) {
865 /* Create tracker. */
866 if (init_pid_tracker(&session->pid_tracker)) {
867 ERR("Error initializing PID tracker");
868 retval = LTTNG_ERR_NOMEM;
869 goto end;
870 }
871 ret = pid_tracker_add_pid(&session->pid_tracker, pid);
872 if (ret != LTTNG_OK) {
873 retval = ret;
874 fini_pid_tracker(&session->pid_tracker);
875 goto end;
876 }
877 /* Remove all apps from session except pid. */
88e3c2f5 878 should_update_apps = true;
a9ad0c8f
MD
879 } else {
880 struct ust_app *app;
881
882 ret = pid_tracker_add_pid(&session->pid_tracker, pid);
883 if (ret != LTTNG_OK) {
884 retval = ret;
885 goto end;
886 }
887 /* Add session to application */
888 app = ust_app_find_by_pid(pid);
889 if (app) {
88e3c2f5 890 should_update_apps = true;
a9ad0c8f
MD
891 }
892 }
893 }
88e3c2f5
JG
894 if (should_update_apps && session->active) {
895 ust_app_global_update_all(session);
896 }
a9ad0c8f
MD
897end:
898 return retval;
899}
900
901/*
902 * Called with the session lock held.
903 */
904int trace_ust_untrack_pid(struct ltt_ust_session *session, int pid)
905{
906 int retval = LTTNG_OK;
907
908 if (pid == -1) {
909 /* Create empty tracker, replace old tracker. */
910 struct ust_pid_tracker tmp_tracker;
911
912 tmp_tracker = session->pid_tracker;
913 if (init_pid_tracker(&session->pid_tracker)) {
914 ERR("Error initializing PID tracker");
915 retval = LTTNG_ERR_NOMEM;
916 /* Rollback operation. */
917 session->pid_tracker = tmp_tracker;
918 goto end;
919 }
920 fini_pid_tracker(&tmp_tracker);
921
922 /* Remove session from all applications */
923 ust_app_global_update_all(session);
924 } else {
925 int ret;
926 struct ust_app *app;
927
928 if (!session->pid_tracker.ht) {
b894343d
JG
929 /* No PID being tracked. */
930 retval = LTTNG_ERR_PID_NOT_TRACKED;
a9ad0c8f
MD
931 goto end;
932 }
933 /* Remove PID from tracker */
934 ret = pid_tracker_del_pid(&session->pid_tracker, pid);
935 if (ret != LTTNG_OK) {
936 retval = ret;
937 goto end;
938 }
939 /* Remove session from application. */
940 app = ust_app_find_by_pid(pid);
941 if (app) {
942 ust_app_global_update(session, app);
943 }
944 }
945end:
946 return retval;
947}
948
a5dfbb9d
MD
949/*
950 * Called with session lock held.
951 */
952ssize_t trace_ust_list_tracker_pids(struct ltt_ust_session *session,
953 int32_t **_pids)
954{
955 struct ust_pid_tracker_node *tracker_node;
956 struct lttng_ht_iter iter;
957 unsigned long count, i = 0;
958 long approx[2];
959 int32_t *pids;
960 int ret = 0;
961
962 if (!session->pid_tracker.ht) {
963 /* Tracker disabled. Set first entry to -1. */
964 pids = zmalloc(sizeof(*pids));
965 if (!pids) {
966 ret = -1;
967 goto end;
968 }
969 pids[0] = -1;
970 *_pids = pids;
971 return 1;
972 }
973
974 rcu_read_lock();
975 cds_lfht_count_nodes(session->pid_tracker.ht->ht,
976 &approx[0], &count, &approx[1]);
977 pids = zmalloc(sizeof(*pids) * count);
978 if (!pids) {
979 ret = -1;
980 goto end;
981 }
982 cds_lfht_for_each_entry(session->pid_tracker.ht->ht,
983 &iter.iter, tracker_node, node.node) {
984 pids[i++] = tracker_node->node.key;
985 }
986 *_pids = pids;
987 ret = count;
988end:
989 rcu_read_unlock();
990 return ret;
991}
992
f6a9efaa
DG
993/*
994 * RCU safe free context structure.
995 */
996static void destroy_context_rcu(struct rcu_head *head)
997{
bec39940
DG
998 struct lttng_ht_node_ulong *node =
999 caa_container_of(head, struct lttng_ht_node_ulong, head);
f6a9efaa
DG
1000 struct ltt_ust_context *ctx =
1001 caa_container_of(node, struct ltt_ust_context, node);
1002
bdf64013 1003 trace_ust_destroy_context(ctx);
f6a9efaa
DG
1004}
1005
1006/*
1007 * Cleanup UST context hash table.
1008 */
7bd39047 1009static void destroy_contexts(struct lttng_ht *ht)
f6a9efaa
DG
1010{
1011 int ret;
bec39940
DG
1012 struct lttng_ht_node_ulong *node;
1013 struct lttng_ht_iter iter;
31746f93 1014 struct ltt_ust_context *ctx;
f6a9efaa 1015
0525e9ae
DG
1016 assert(ht);
1017
36b588ed 1018 rcu_read_lock();
bec39940 1019 cds_lfht_for_each_entry(ht->ht, &iter.iter, node, node) {
31746f93
DG
1020 /* Remove from ordered list. */
1021 ctx = caa_container_of(node, struct ltt_ust_context, node);
1022 cds_list_del(&ctx->list);
1023 /* Remove from channel's hash table. */
bec39940 1024 ret = lttng_ht_del(ht, &iter);
f6a9efaa
DG
1025 if (!ret) {
1026 call_rcu(&node->head, destroy_context_rcu);
1027 }
f6a9efaa 1028 }
36b588ed 1029 rcu_read_unlock();
ed52805d 1030
0b2dc8df 1031 ht_cleanup_push(ht);
f6a9efaa
DG
1032}
1033
97ee3a89
DG
1034/*
1035 * Cleanup ust event structure.
1036 */
1037void trace_ust_destroy_event(struct ltt_ust_event *event)
1038{
0525e9ae
DG
1039 assert(event);
1040
f6a9efaa 1041 DBG2("Trace destroy UST event %s", event->attr.name);
6b453b5e 1042 free(event->filter_expression);
53a80697 1043 free(event->filter);
40024f8a 1044 free(event->exclusion);
97ee3a89
DG
1045 free(event);
1046}
1047
bdf64013
JG
1048/*
1049 * Cleanup ust context structure.
1050 */
1051void trace_ust_destroy_context(struct ltt_ust_context *ctx)
1052{
1053 assert(ctx);
1054
1055 if (ctx->ctx.ctx == LTTNG_UST_CONTEXT_APP_CONTEXT) {
1056 free(ctx->ctx.u.app_ctx.provider_name);
1057 free(ctx->ctx.u.app_ctx.ctx_name);
1058 }
1059 free(ctx);
1060}
1061
f6a9efaa
DG
1062/*
1063 * URCU intermediate call to complete destroy event.
1064 */
1065static void destroy_event_rcu(struct rcu_head *head)
1066{
bec39940
DG
1067 struct lttng_ht_node_str *node =
1068 caa_container_of(head, struct lttng_ht_node_str, head);
f6a9efaa
DG
1069 struct ltt_ust_event *event =
1070 caa_container_of(node, struct ltt_ust_event, node);
1071
1072 trace_ust_destroy_event(event);
1073}
1074
284d8f55
DG
1075/*
1076 * Cleanup UST events hashtable.
1077 */
7bd39047 1078static void destroy_events(struct lttng_ht *events)
ed52805d
DG
1079{
1080 int ret;
bec39940
DG
1081 struct lttng_ht_node_str *node;
1082 struct lttng_ht_iter iter;
ed52805d 1083
0525e9ae
DG
1084 assert(events);
1085
36b588ed 1086 rcu_read_lock();
bec39940
DG
1087 cds_lfht_for_each_entry(events->ht, &iter.iter, node, node) {
1088 ret = lttng_ht_del(events, &iter);
7bd39047
DG
1089 assert(!ret);
1090 call_rcu(&node->head, destroy_event_rcu);
ed52805d 1091 }
36b588ed 1092 rcu_read_unlock();
ed52805d 1093
0b2dc8df 1094 ht_cleanup_push(events);
ed52805d
DG
1095}
1096
97ee3a89
DG
1097/*
1098 * Cleanup ust channel structure.
36b588ed
MD
1099 *
1100 * Should _NOT_ be called with RCU read lock held.
97ee3a89 1101 */
36b588ed 1102static void _trace_ust_destroy_channel(struct ltt_ust_channel *channel)
97ee3a89 1103{
0525e9ae
DG
1104 assert(channel);
1105
f6a9efaa 1106 DBG2("Trace destroy UST channel %s", channel->name);
97ee3a89 1107
97ee3a89 1108 free(channel);
f6a9efaa
DG
1109}
1110
1111/*
1112 * URCU intermediate call to complete destroy channel.
1113 */
1114static void destroy_channel_rcu(struct rcu_head *head)
1115{
bec39940
DG
1116 struct lttng_ht_node_str *node =
1117 caa_container_of(head, struct lttng_ht_node_str, head);
f6a9efaa
DG
1118 struct ltt_ust_channel *channel =
1119 caa_container_of(node, struct ltt_ust_channel, node);
1120
36b588ed
MD
1121 _trace_ust_destroy_channel(channel);
1122}
1123
1124void trace_ust_destroy_channel(struct ltt_ust_channel *channel)
1125{
627cbbe7
MD
1126 /* Destroying all events of the channel */
1127 destroy_events(channel->events);
1128 /* Destroying all context of the channel */
1129 destroy_contexts(channel->ctx);
1130
36b588ed 1131 call_rcu(&channel->node.head, destroy_channel_rcu);
97ee3a89
DG
1132}
1133
d5979e4a
DG
1134/*
1135 * Remove an UST channel from a channel HT.
1136 */
1137void trace_ust_delete_channel(struct lttng_ht *ht,
1138 struct ltt_ust_channel *channel)
1139{
1140 int ret;
1141 struct lttng_ht_iter iter;
1142
1143 assert(ht);
1144 assert(channel);
1145
1146 iter.iter.node = &channel->node.node;
1147 ret = lttng_ht_del(ht, &iter);
1148 assert(!ret);
1149}
1150
97ee3a89 1151/*
f6a9efaa 1152 * Iterate over a hash table containing channels and cleanup safely.
97ee3a89 1153 */
bec39940 1154static void destroy_channels(struct lttng_ht *channels)
f6a9efaa 1155{
bec39940
DG
1156 struct lttng_ht_node_str *node;
1157 struct lttng_ht_iter iter;
f6a9efaa 1158
0525e9ae
DG
1159 assert(channels);
1160
24d1723f 1161 rcu_read_lock();
bec39940 1162 cds_lfht_for_each_entry(channels->ht, &iter.iter, node, node) {
627cbbe7
MD
1163 struct ltt_ust_channel *chan =
1164 caa_container_of(node, struct ltt_ust_channel, node);
1165
1166 trace_ust_delete_channel(channels, chan);
1167 trace_ust_destroy_channel(chan);
f6a9efaa 1168 }
36b588ed 1169 rcu_read_unlock();
ed52805d 1170
0b2dc8df 1171 ht_cleanup_push(channels);
f6a9efaa
DG
1172}
1173
f6a9efaa
DG
1174/*
1175 * Cleanup UST global domain.
1176 */
1177static void destroy_domain_global(struct ltt_ust_domain_global *dom)
1178{
0525e9ae
DG
1179 assert(dom);
1180
f6a9efaa
DG
1181 destroy_channels(dom->channels);
1182}
97ee3a89 1183
f6a9efaa 1184/*
d070c424
MD
1185 * Cleanup ust session structure, keeping data required by
1186 * destroy notifier.
36b588ed
MD
1187 *
1188 * Should *NOT* be called with RCU read-side lock held.
f6a9efaa
DG
1189 */
1190void trace_ust_destroy_session(struct ltt_ust_session *session)
1191{
fefd409b 1192 struct agent *agt;
7972aab2 1193 struct buffer_reg_uid *reg, *sreg;
fefd409b 1194 struct lttng_ht_iter iter;
7972aab2 1195
0525e9ae 1196 assert(session);
97ee3a89 1197
d9bf3ca4 1198 DBG2("Trace UST destroy session %" PRIu64, session->id);
f6a9efaa 1199
f6a9efaa
DG
1200 /* Cleaning up UST domain */
1201 destroy_domain_global(&session->domain_global);
fefd409b 1202
0c0fcd77 1203 rcu_read_lock();
fefd409b 1204 cds_lfht_for_each_entry(session->agents->ht, &iter.iter, agt, node.node) {
d0edf546
JG
1205 int ret = lttng_ht_del(session->agents, &iter);
1206
1207 assert(!ret);
fefd409b
DG
1208 agent_destroy(agt);
1209 }
0c0fcd77 1210 rcu_read_unlock();
7972aab2 1211
8ada2175
MD
1212 ht_cleanup_push(session->agents);
1213
7972aab2
DG
1214 /* Cleanup UID buffer registry object(s). */
1215 cds_list_for_each_entry_safe(reg, sreg, &session->buffer_reg_uid_list,
1216 lnode) {
1217 cds_list_del(&reg->lnode);
1218 buffer_reg_uid_remove(reg);
1219 buffer_reg_uid_destroy(reg, session->consumer);
1220 }
44d3bd01 1221
a9ad0c8f 1222 fini_pid_tracker(&session->pid_tracker);
82b69413 1223 lttng_trace_chunk_put(session->current_trace_chunk);
d070c424
MD
1224}
1225
1226/* Free elements needed by destroy notifiers. */
1227void trace_ust_free_session(struct ltt_ust_session *session)
1228{
1229 consumer_output_put(session->consumer);
97ee3a89
DG
1230 free(session);
1231}
This page took 0.117348 seconds and 4 git commands to generate.