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