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