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