Fix: only synchronize application configuration on tracing start
[lttng-tools.git] / src / bin / lttng-sessiond / trace-ust.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
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.
8 *
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.
13 *
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.
17 */
18
19 #define _LGPL_SOURCE
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <inttypes.h>
25
26 #include <common/common.h>
27 #include <common/defaults.h>
28
29 #include "buffer-registry.h"
30 #include "trace-ust.h"
31 #include "utils.h"
32 #include "ust-app.h"
33 #include "agent.h"
34
35 /*
36 * Match function for the events hash table lookup.
37 *
38 * Matches by name only. Used by the disable command.
39 */
40 int 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
57 /* Match */
58 return 1;
59
60 no_match:
61 return 0;
62 }
63
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 */
70 int 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;
74 int ev_loglevel_value;
75 int ll_match;
76
77 assert(node);
78 assert(_key);
79
80 event = caa_container_of(node, struct ltt_ust_event, node.node);
81 key = _key;
82 ev_loglevel_value = event->attr.loglevel;
83
84 /* Match the 4 elements of the key: name, filter, loglevel, exclusions. */
85
86 /* Event name */
87 if (strncmp(event->attr.name, key->name, sizeof(event->attr.name)) != 0) {
88 goto no_match;
89 }
90
91 /* Event loglevel value and type. */
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) {
97 goto no_match;
98 }
99
100 /* Only one of the filters is NULL, fail. */
101 if ((key->filter && !event->filter) || (!key->filter && event->filter)) {
102 goto no_match;
103 }
104
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 }
112 }
113
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) {
120 size_t i;
121
122 /* Check exclusion counts first. */
123 if (event->exclusion->count != key->exclusion->count) {
124 goto no_match;
125 }
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 }
160 }
161 /* Match. */
162 return 1;
163
164 no_match:
165 return 0;
166 }
167
168 /*
169 * Find the channel in the hashtable and return channel pointer. RCU read side
170 * lock MUST be acquired before calling this.
171 */
172 struct ltt_ust_channel *trace_ust_find_channel_by_name(struct lttng_ht *ht,
173 char *name)
174 {
175 struct lttng_ht_node_str *node;
176 struct lttng_ht_iter iter;
177
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
185 lttng_ht_lookup(ht, (void *)name, &iter);
186 node = lttng_ht_iter_get_node_str(&iter);
187 if (node == NULL) {
188 goto error;
189 }
190
191 DBG2("Trace UST channel %s found by name", name);
192
193 return caa_container_of(node, struct ltt_ust_channel, node);
194
195 error:
196 DBG2("Trace UST channel %s not found by name", name);
197 return NULL;
198 }
199
200 /*
201 * Find the event in the hashtable and return event pointer. RCU read side lock
202 * MUST be acquired before calling this.
203 */
204 struct ltt_ust_event *trace_ust_find_event(struct lttng_ht *ht,
205 char *name, struct lttng_filter_bytecode *filter,
206 enum lttng_ust_loglevel_type loglevel_type, int loglevel_value,
207 struct lttng_event_exclusion *exclusion)
208 {
209 struct lttng_ht_node_str *node;
210 struct lttng_ht_iter iter;
211 struct ltt_ust_ht_key key;
212
213 assert(name);
214 assert(ht);
215
216 key.name = name;
217 key.filter = filter;
218 key.loglevel_type = loglevel_type;
219 key.loglevel_value = loglevel_value;
220 key.exclusion = exclusion;
221
222 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
223 trace_ust_ht_match_event, &key, &iter.iter);
224 node = lttng_ht_iter_get_node_str(&iter);
225 if (node == NULL) {
226 goto error;
227 }
228
229 DBG2("Trace UST event %s found", key.name);
230
231 return caa_container_of(node, struct ltt_ust_event, node);
232
233 error:
234 DBG2("Trace UST event %s NOT found", key.name);
235 return NULL;
236 }
237
238 /*
239 * Lookup an agent in the session agents hash table by domain type and return
240 * the object if found else NULL.
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.
244 */
245 struct 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
266 end:
267 return agt;
268 }
269
270 /*
271 * Allocate and initialize a ust session data structure.
272 *
273 * Return pointer to structure or NULL.
274 */
275 struct ltt_ust_session *trace_ust_create_session(uint64_t session_id)
276 {
277 struct ltt_ust_session *lus;
278
279 /* Allocate a new ltt ust session */
280 lus = zmalloc(sizeof(struct ltt_ust_session));
281 if (lus == NULL) {
282 PERROR("create ust session zmalloc");
283 goto error;
284 }
285
286 /* Init data structure */
287 lus->id = session_id;
288 lus->active = 0;
289
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
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 */
304 lus->buffer_type = LTTNG_BUFFER_PER_UID;
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);
309
310 /* Alloc UST global domain channels' HT */
311 lus->domain_global.channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
312 /* Alloc agent hash table. */
313 lus->agents = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
314
315 lus->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
316 if (lus->consumer == NULL) {
317 goto error_consumer;
318 }
319
320 DBG2("UST trace session create successful");
321
322 return lus;
323
324 error_consumer:
325 ht_cleanup_push(lus->domain_global.channels);
326 ht_cleanup_push(lus->agents);
327 free(lus);
328 error:
329 return NULL;
330 }
331
332 /*
333 * Allocate and initialize a ust channel data structure.
334 *
335 * Return pointer to structure or NULL.
336 */
337 struct ltt_ust_channel *trace_ust_create_channel(struct lttng_channel *chan,
338 enum lttng_domain_type domain)
339 {
340 struct ltt_ust_channel *luc;
341
342 assert(chan);
343
344 luc = zmalloc(sizeof(struct ltt_ust_channel));
345 if (luc == NULL) {
346 PERROR("ltt_ust_channel zmalloc");
347 goto error;
348 }
349
350 luc->domain = domain;
351
352 /* Copy UST channel attributes */
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;
358 luc->attr.output = (enum lttng_ust_output) chan->attr.output;
359 luc->monitor_timer_interval = ((struct lttng_channel_extended *)
360 chan->attr.extended.ptr)->monitor_timer_interval;
361 luc->attr.u.s.blocking_timeout = ((struct lttng_channel_extended *)
362 chan->attr.extended.ptr)->blocking_timeout;
363
364 /* Translate to UST output enum */
365 switch (luc->attr.output) {
366 default:
367 luc->attr.output = LTTNG_UST_MMAP;
368 break;
369 }
370
371 /*
372 * If we receive an empty string for channel name, it means the
373 * default channel name is requested.
374 */
375 if (chan->name[0] == '\0') {
376 strncpy(luc->name, DEFAULT_CHANNEL_NAME, sizeof(luc->name));
377 } else {
378 /* Copy channel name */
379 strncpy(luc->name, chan->name, sizeof(luc->name));
380 }
381 luc->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
382
383 /* Init node */
384 lttng_ht_node_init_str(&luc->node, luc->name);
385 CDS_INIT_LIST_HEAD(&luc->ctx_list);
386
387 /* Alloc hash tables */
388 luc->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
389 luc->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
390
391 /* On-disk circular buffer parameters */
392 luc->tracefile_size = chan->attr.tracefile_size;
393 luc->tracefile_count = chan->attr.tracefile_count;
394
395 DBG2("Trace UST channel %s created", luc->name);
396
397 error:
398 return luc;
399 }
400
401 /*
402 * Validates an exclusion list.
403 *
404 * Returns 0 if valid, negative value if invalid.
405 */
406 static int validate_exclusion(struct lttng_event_exclusion *exclusion)
407 {
408 size_t i;
409 int ret = 0;
410
411 assert(exclusion);
412
413 for (i = 0; i < exclusion->count; ++i) {
414 size_t j;
415 const char *name_a =
416 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, i);
417
418 for (j = 0; j < i; ++j) {
419 const char *name_b =
420 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, j);
421
422 if (!strncmp(name_a, name_b, LTTNG_SYMBOL_NAME_LEN)) {
423 /* Match! */
424 ret = -1;
425 goto end;
426 }
427 }
428 }
429
430 end:
431 return ret;
432 }
433
434 /*
435 * Allocate and initialize a ust event. Set name and event type.
436 * We own filter_expression, filter, and exclusion.
437 *
438 * Return an lttng_error_code
439 */
440 enum lttng_error_code trace_ust_create_event(struct lttng_event *ev,
441 char *filter_expression,
442 struct lttng_filter_bytecode *filter,
443 struct lttng_event_exclusion *exclusion,
444 bool internal_event,
445 struct ltt_ust_event **ust_event)
446 {
447 struct ltt_ust_event *local_ust_event;
448 enum lttng_error_code ret = LTTNG_OK;
449
450 assert(ev);
451
452 if (exclusion && validate_exclusion(exclusion)) {
453 ret = LTTNG_ERR_INVALID;
454 goto error;
455 }
456
457 local_ust_event = zmalloc(sizeof(struct ltt_ust_event));
458 if (local_ust_event == NULL) {
459 PERROR("ust event zmalloc");
460 ret = LTTNG_ERR_NOMEM;
461 goto error;
462 }
463
464 local_ust_event->internal = internal_event;
465
466 switch (ev->type) {
467 case LTTNG_EVENT_PROBE:
468 local_ust_event->attr.instrumentation = LTTNG_UST_PROBE;
469 break;
470 case LTTNG_EVENT_FUNCTION:
471 local_ust_event->attr.instrumentation = LTTNG_UST_FUNCTION;
472 break;
473 case LTTNG_EVENT_FUNCTION_ENTRY:
474 local_ust_event->attr.instrumentation = LTTNG_UST_FUNCTION;
475 break;
476 case LTTNG_EVENT_TRACEPOINT:
477 local_ust_event->attr.instrumentation = LTTNG_UST_TRACEPOINT;
478 break;
479 default:
480 ERR("Unknown ust instrumentation type (%d)", ev->type);
481 ret = LTTNG_ERR_INVALID;
482 goto error_free_event;
483 }
484
485 /* Copy event name */
486 strncpy(local_ust_event->attr.name, ev->name, LTTNG_UST_SYM_NAME_LEN);
487 local_ust_event->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
488
489 switch (ev->loglevel_type) {
490 case LTTNG_EVENT_LOGLEVEL_ALL:
491 local_ust_event->attr.loglevel_type = LTTNG_UST_LOGLEVEL_ALL;
492 local_ust_event->attr.loglevel = -1; /* Force to -1 */
493 break;
494 case LTTNG_EVENT_LOGLEVEL_RANGE:
495 local_ust_event->attr.loglevel_type = LTTNG_UST_LOGLEVEL_RANGE;
496 local_ust_event->attr.loglevel = ev->loglevel;
497 break;
498 case LTTNG_EVENT_LOGLEVEL_SINGLE:
499 local_ust_event->attr.loglevel_type = LTTNG_UST_LOGLEVEL_SINGLE;
500 local_ust_event->attr.loglevel = ev->loglevel;
501 break;
502 default:
503 ERR("Unknown ust loglevel type (%d)", ev->loglevel_type);
504 ret = LTTNG_ERR_INVALID;
505 goto error_free_event;
506 }
507
508 /* Same layout. */
509 local_ust_event->filter_expression = filter_expression;
510 local_ust_event->filter = filter;
511 local_ust_event->exclusion = exclusion;
512
513 /* Init node */
514 lttng_ht_node_init_str(&local_ust_event->node, local_ust_event->attr.name);
515
516 DBG2("Trace UST event %s, loglevel (%d,%d) created",
517 local_ust_event->attr.name, local_ust_event->attr.loglevel_type,
518 local_ust_event->attr.loglevel);
519
520 *ust_event = local_ust_event;
521
522 return ret;
523
524 error_free_event:
525 free(local_ust_event);
526 error:
527 free(filter_expression);
528 free(filter);
529 free(exclusion);
530 return ret;
531 }
532
533 static
534 int trace_ust_context_type_event_to_ust(
535 enum lttng_event_context_type type)
536 {
537 int utype;
538
539 switch (type) {
540 case LTTNG_EVENT_CONTEXT_VTID:
541 utype = LTTNG_UST_CONTEXT_VTID;
542 break;
543 case LTTNG_EVENT_CONTEXT_VPID:
544 utype = LTTNG_UST_CONTEXT_VPID;
545 break;
546 case LTTNG_EVENT_CONTEXT_PTHREAD_ID:
547 utype = LTTNG_UST_CONTEXT_PTHREAD_ID;
548 break;
549 case LTTNG_EVENT_CONTEXT_PROCNAME:
550 utype = LTTNG_UST_CONTEXT_PROCNAME;
551 break;
552 case LTTNG_EVENT_CONTEXT_IP:
553 utype = LTTNG_UST_CONTEXT_IP;
554 break;
555 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
556 if (!ustctl_has_perf_counters()) {
557 utype = -1;
558 WARN("Perf counters not implemented in UST");
559 } else {
560 utype = LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER;
561 }
562 break;
563 case LTTNG_EVENT_CONTEXT_APP_CONTEXT:
564 utype = LTTNG_UST_CONTEXT_APP_CONTEXT;
565 break;
566 default:
567 utype = -1;
568 break;
569 }
570 return utype;
571 }
572
573 /*
574 * Return 1 if contexts match, 0 otherwise.
575 */
576 int trace_ust_match_context(struct ltt_ust_context *uctx,
577 struct lttng_event_context *ctx)
578 {
579 int utype;
580
581 utype = trace_ust_context_type_event_to_ust(ctx->ctx);
582 if (utype < 0) {
583 return 0;
584 }
585 if (uctx->ctx.ctx != utype) {
586 return 0;
587 }
588 switch (utype) {
589 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER:
590 if (uctx->ctx.u.perf_counter.type
591 != ctx->u.perf_counter.type) {
592 return 0;
593 }
594 if (uctx->ctx.u.perf_counter.config
595 != ctx->u.perf_counter.config) {
596 return 0;
597 }
598 if (strncmp(uctx->ctx.u.perf_counter.name,
599 ctx->u.perf_counter.name,
600 LTTNG_UST_SYM_NAME_LEN)) {
601 return 0;
602 }
603 break;
604 case LTTNG_UST_CONTEXT_APP_CONTEXT:
605 assert(uctx->ctx.u.app_ctx.provider_name);
606 assert(uctx->ctx.u.app_ctx.ctx_name);
607 if (strcmp(uctx->ctx.u.app_ctx.provider_name,
608 ctx->u.app_ctx.provider_name) ||
609 strcmp(uctx->ctx.u.app_ctx.ctx_name,
610 ctx->u.app_ctx.ctx_name)) {
611 return 0;
612 }
613 default:
614 break;
615
616 }
617 return 1;
618 }
619
620 /*
621 * Allocate and initialize an UST context.
622 *
623 * Return pointer to structure or NULL.
624 */
625 struct ltt_ust_context *trace_ust_create_context(
626 struct lttng_event_context *ctx)
627 {
628 struct ltt_ust_context *uctx = NULL;
629 int utype;
630
631 assert(ctx);
632
633 utype = trace_ust_context_type_event_to_ust(ctx->ctx);
634 if (utype < 0) {
635 ERR("Invalid UST context");
636 goto end;
637 }
638
639 uctx = zmalloc(sizeof(struct ltt_ust_context));
640 if (!uctx) {
641 PERROR("zmalloc ltt_ust_context");
642 goto end;
643 }
644
645 uctx->ctx.ctx = (enum lttng_ust_context_type) utype;
646 switch (utype) {
647 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER:
648 uctx->ctx.u.perf_counter.type = ctx->u.perf_counter.type;
649 uctx->ctx.u.perf_counter.config = ctx->u.perf_counter.config;
650 strncpy(uctx->ctx.u.perf_counter.name, ctx->u.perf_counter.name,
651 LTTNG_UST_SYM_NAME_LEN);
652 uctx->ctx.u.perf_counter.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
653 break;
654 case LTTNG_UST_CONTEXT_APP_CONTEXT:
655 {
656 char *provider_name = NULL, *ctx_name = NULL;
657
658 provider_name = strdup(ctx->u.app_ctx.provider_name);
659 if (!provider_name) {
660 goto error;
661 }
662 uctx->ctx.u.app_ctx.provider_name = provider_name;
663
664 ctx_name = strdup(ctx->u.app_ctx.ctx_name);
665 if (!ctx_name) {
666 goto error;
667 }
668 uctx->ctx.u.app_ctx.ctx_name = ctx_name;
669 break;
670 }
671 default:
672 break;
673 }
674 lttng_ht_node_init_ulong(&uctx->node, (unsigned long) uctx->ctx.ctx);
675 end:
676 return uctx;
677 error:
678 trace_ust_destroy_context(uctx);
679 return NULL;
680 }
681
682 static
683 void destroy_pid_tracker_node_rcu(struct rcu_head *head)
684 {
685 struct ust_pid_tracker_node *tracker_node =
686 caa_container_of(head, struct ust_pid_tracker_node, node.head);
687 free(tracker_node);
688 }
689
690 static
691 void destroy_pid_tracker_node(struct ust_pid_tracker_node *tracker_node)
692 {
693
694 call_rcu(&tracker_node->node.head, destroy_pid_tracker_node_rcu);
695 }
696
697 static
698 int init_pid_tracker(struct ust_pid_tracker *pid_tracker)
699 {
700 int ret = 0;
701
702 pid_tracker->ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
703 if (!pid_tracker->ht) {
704 ret = -1;
705 goto end;
706 }
707
708 end:
709 return ret;
710 }
711
712 /*
713 * Teardown pid tracker content, but don't free pid_tracker object.
714 */
715 static
716 void fini_pid_tracker(struct ust_pid_tracker *pid_tracker)
717 {
718 struct ust_pid_tracker_node *tracker_node;
719 struct lttng_ht_iter iter;
720
721 if (!pid_tracker->ht) {
722 return;
723 }
724 rcu_read_lock();
725 cds_lfht_for_each_entry(pid_tracker->ht->ht,
726 &iter.iter, tracker_node, node.node) {
727 int ret = lttng_ht_del(pid_tracker->ht, &iter);
728
729 assert(!ret);
730 destroy_pid_tracker_node(tracker_node);
731 }
732 rcu_read_unlock();
733 ht_cleanup_push(pid_tracker->ht);
734 pid_tracker->ht = NULL;
735 }
736
737 static
738 struct ust_pid_tracker_node *pid_tracker_lookup(
739 struct ust_pid_tracker *pid_tracker, int pid,
740 struct lttng_ht_iter *iter)
741 {
742 unsigned long _pid = (unsigned long) pid;
743 struct lttng_ht_node_ulong *node;
744
745 lttng_ht_lookup(pid_tracker->ht, (void *) _pid, iter);
746 node = lttng_ht_iter_get_node_ulong(iter);
747 if (node) {
748 return caa_container_of(node, struct ust_pid_tracker_node,
749 node);
750 } else {
751 return NULL;
752 }
753 }
754
755 static
756 int pid_tracker_add_pid(struct ust_pid_tracker *pid_tracker, int pid)
757 {
758 int retval = LTTNG_OK;
759 struct ust_pid_tracker_node *tracker_node;
760 struct lttng_ht_iter iter;
761
762 if (pid < 0) {
763 retval = LTTNG_ERR_INVALID;
764 goto end;
765 }
766 tracker_node = pid_tracker_lookup(pid_tracker, pid, &iter);
767 if (tracker_node) {
768 /* Already exists. */
769 retval = LTTNG_ERR_PID_TRACKED;
770 goto end;
771 }
772 tracker_node = zmalloc(sizeof(*tracker_node));
773 if (!tracker_node) {
774 retval = LTTNG_ERR_NOMEM;
775 goto end;
776 }
777 lttng_ht_node_init_ulong(&tracker_node->node, (unsigned long) pid);
778 lttng_ht_add_unique_ulong(pid_tracker->ht, &tracker_node->node);
779 end:
780 return retval;
781 }
782
783 static
784 int pid_tracker_del_pid(struct ust_pid_tracker *pid_tracker, int pid)
785 {
786 int retval = LTTNG_OK, ret;
787 struct ust_pid_tracker_node *tracker_node;
788 struct lttng_ht_iter iter;
789
790 if (pid < 0) {
791 retval = LTTNG_ERR_INVALID;
792 goto end;
793 }
794 tracker_node = pid_tracker_lookup(pid_tracker, pid, &iter);
795 if (!tracker_node) {
796 /* Not found */
797 retval = LTTNG_ERR_PID_NOT_TRACKED;
798 goto end;
799 }
800 ret = lttng_ht_del(pid_tracker->ht, &iter);
801 assert(!ret);
802
803 destroy_pid_tracker_node(tracker_node);
804 end:
805 return retval;
806 }
807
808 /*
809 * The session lock is held when calling this function.
810 */
811 int trace_ust_pid_tracker_lookup(struct ltt_ust_session *session, int pid)
812 {
813 struct lttng_ht_iter iter;
814
815 if (!session->pid_tracker.ht) {
816 return 1;
817 }
818 if (pid_tracker_lookup(&session->pid_tracker, pid, &iter)) {
819 return 1;
820 }
821 return 0;
822 }
823
824 /*
825 * Called with the session lock held.
826 */
827 int trace_ust_track_pid(struct ltt_ust_session *session, int pid)
828 {
829 int retval = LTTNG_OK;
830 bool should_update_apps = false;
831
832 if (pid == -1) {
833 /* Track all pids: destroy tracker if exists. */
834 if (session->pid_tracker.ht) {
835 fini_pid_tracker(&session->pid_tracker);
836 /* Ensure all apps have session. */
837 should_update_apps = true;
838 }
839 } else {
840 int ret;
841
842 if (!session->pid_tracker.ht) {
843 /* Create tracker. */
844 if (init_pid_tracker(&session->pid_tracker)) {
845 ERR("Error initializing PID tracker");
846 retval = LTTNG_ERR_NOMEM;
847 goto end;
848 }
849 ret = pid_tracker_add_pid(&session->pid_tracker, pid);
850 if (ret != LTTNG_OK) {
851 retval = ret;
852 fini_pid_tracker(&session->pid_tracker);
853 goto end;
854 }
855 /* Remove all apps from session except pid. */
856 should_update_apps = true;
857 } else {
858 struct ust_app *app;
859
860 ret = pid_tracker_add_pid(&session->pid_tracker, pid);
861 if (ret != LTTNG_OK) {
862 retval = ret;
863 goto end;
864 }
865 /* Add session to application */
866 app = ust_app_find_by_pid(pid);
867 if (app) {
868 should_update_apps = true;
869 }
870 }
871 }
872 if (should_update_apps && session->active) {
873 ust_app_global_update_all(session);
874 }
875 end:
876 return retval;
877 }
878
879 /*
880 * Called with the session lock held.
881 */
882 int trace_ust_untrack_pid(struct ltt_ust_session *session, int pid)
883 {
884 int retval = LTTNG_OK;
885
886 if (pid == -1) {
887 /* Create empty tracker, replace old tracker. */
888 struct ust_pid_tracker tmp_tracker;
889
890 tmp_tracker = session->pid_tracker;
891 if (init_pid_tracker(&session->pid_tracker)) {
892 ERR("Error initializing PID tracker");
893 retval = LTTNG_ERR_NOMEM;
894 /* Rollback operation. */
895 session->pid_tracker = tmp_tracker;
896 goto end;
897 }
898 fini_pid_tracker(&tmp_tracker);
899
900 /* Remove session from all applications */
901 ust_app_global_update_all(session);
902 } else {
903 int ret;
904 struct ust_app *app;
905
906 if (!session->pid_tracker.ht) {
907 /* No PID being tracked. */
908 retval = LTTNG_ERR_PID_NOT_TRACKED;
909 goto end;
910 }
911 /* Remove PID from tracker */
912 ret = pid_tracker_del_pid(&session->pid_tracker, pid);
913 if (ret != LTTNG_OK) {
914 retval = ret;
915 goto end;
916 }
917 /* Remove session from application. */
918 app = ust_app_find_by_pid(pid);
919 if (app) {
920 ust_app_global_update(session, app);
921 }
922 }
923 end:
924 return retval;
925 }
926
927 /*
928 * Called with session lock held.
929 */
930 ssize_t trace_ust_list_tracker_pids(struct ltt_ust_session *session,
931 int32_t **_pids)
932 {
933 struct ust_pid_tracker_node *tracker_node;
934 struct lttng_ht_iter iter;
935 unsigned long count, i = 0;
936 long approx[2];
937 int32_t *pids;
938 int ret = 0;
939
940 if (!session->pid_tracker.ht) {
941 /* Tracker disabled. Set first entry to -1. */
942 pids = zmalloc(sizeof(*pids));
943 if (!pids) {
944 ret = -1;
945 goto end;
946 }
947 pids[0] = -1;
948 *_pids = pids;
949 return 1;
950 }
951
952 rcu_read_lock();
953 cds_lfht_count_nodes(session->pid_tracker.ht->ht,
954 &approx[0], &count, &approx[1]);
955 pids = zmalloc(sizeof(*pids) * count);
956 if (!pids) {
957 ret = -1;
958 goto end;
959 }
960 cds_lfht_for_each_entry(session->pid_tracker.ht->ht,
961 &iter.iter, tracker_node, node.node) {
962 pids[i++] = tracker_node->node.key;
963 }
964 *_pids = pids;
965 ret = count;
966 end:
967 rcu_read_unlock();
968 return ret;
969 }
970
971 /*
972 * RCU safe free context structure.
973 */
974 static void destroy_context_rcu(struct rcu_head *head)
975 {
976 struct lttng_ht_node_ulong *node =
977 caa_container_of(head, struct lttng_ht_node_ulong, head);
978 struct ltt_ust_context *ctx =
979 caa_container_of(node, struct ltt_ust_context, node);
980
981 trace_ust_destroy_context(ctx);
982 }
983
984 /*
985 * Cleanup UST context hash table.
986 */
987 static void destroy_contexts(struct lttng_ht *ht)
988 {
989 int ret;
990 struct lttng_ht_node_ulong *node;
991 struct lttng_ht_iter iter;
992 struct ltt_ust_context *ctx;
993
994 assert(ht);
995
996 rcu_read_lock();
997 cds_lfht_for_each_entry(ht->ht, &iter.iter, node, node) {
998 /* Remove from ordered list. */
999 ctx = caa_container_of(node, struct ltt_ust_context, node);
1000 cds_list_del(&ctx->list);
1001 /* Remove from channel's hash table. */
1002 ret = lttng_ht_del(ht, &iter);
1003 if (!ret) {
1004 call_rcu(&node->head, destroy_context_rcu);
1005 }
1006 }
1007 rcu_read_unlock();
1008
1009 ht_cleanup_push(ht);
1010 }
1011
1012 /*
1013 * Cleanup ust event structure.
1014 */
1015 void trace_ust_destroy_event(struct ltt_ust_event *event)
1016 {
1017 assert(event);
1018
1019 DBG2("Trace destroy UST event %s", event->attr.name);
1020 free(event->filter_expression);
1021 free(event->filter);
1022 free(event->exclusion);
1023 free(event);
1024 }
1025
1026 /*
1027 * Cleanup ust context structure.
1028 */
1029 void trace_ust_destroy_context(struct ltt_ust_context *ctx)
1030 {
1031 assert(ctx);
1032
1033 if (ctx->ctx.ctx == LTTNG_UST_CONTEXT_APP_CONTEXT) {
1034 free(ctx->ctx.u.app_ctx.provider_name);
1035 free(ctx->ctx.u.app_ctx.ctx_name);
1036 }
1037 free(ctx);
1038 }
1039
1040 /*
1041 * URCU intermediate call to complete destroy event.
1042 */
1043 static void destroy_event_rcu(struct rcu_head *head)
1044 {
1045 struct lttng_ht_node_str *node =
1046 caa_container_of(head, struct lttng_ht_node_str, head);
1047 struct ltt_ust_event *event =
1048 caa_container_of(node, struct ltt_ust_event, node);
1049
1050 trace_ust_destroy_event(event);
1051 }
1052
1053 /*
1054 * Cleanup UST events hashtable.
1055 */
1056 static void destroy_events(struct lttng_ht *events)
1057 {
1058 int ret;
1059 struct lttng_ht_node_str *node;
1060 struct lttng_ht_iter iter;
1061
1062 assert(events);
1063
1064 rcu_read_lock();
1065 cds_lfht_for_each_entry(events->ht, &iter.iter, node, node) {
1066 ret = lttng_ht_del(events, &iter);
1067 assert(!ret);
1068 call_rcu(&node->head, destroy_event_rcu);
1069 }
1070 rcu_read_unlock();
1071
1072 ht_cleanup_push(events);
1073 }
1074
1075 /*
1076 * Cleanup ust channel structure.
1077 *
1078 * Should _NOT_ be called with RCU read lock held.
1079 */
1080 static void _trace_ust_destroy_channel(struct ltt_ust_channel *channel)
1081 {
1082 assert(channel);
1083
1084 DBG2("Trace destroy UST channel %s", channel->name);
1085
1086 free(channel);
1087 }
1088
1089 /*
1090 * URCU intermediate call to complete destroy channel.
1091 */
1092 static void destroy_channel_rcu(struct rcu_head *head)
1093 {
1094 struct lttng_ht_node_str *node =
1095 caa_container_of(head, struct lttng_ht_node_str, head);
1096 struct ltt_ust_channel *channel =
1097 caa_container_of(node, struct ltt_ust_channel, node);
1098
1099 _trace_ust_destroy_channel(channel);
1100 }
1101
1102 void trace_ust_destroy_channel(struct ltt_ust_channel *channel)
1103 {
1104 /* Destroying all events of the channel */
1105 destroy_events(channel->events);
1106 /* Destroying all context of the channel */
1107 destroy_contexts(channel->ctx);
1108
1109 call_rcu(&channel->node.head, destroy_channel_rcu);
1110 }
1111
1112 /*
1113 * Remove an UST channel from a channel HT.
1114 */
1115 void trace_ust_delete_channel(struct lttng_ht *ht,
1116 struct ltt_ust_channel *channel)
1117 {
1118 int ret;
1119 struct lttng_ht_iter iter;
1120
1121 assert(ht);
1122 assert(channel);
1123
1124 iter.iter.node = &channel->node.node;
1125 ret = lttng_ht_del(ht, &iter);
1126 assert(!ret);
1127 }
1128
1129 /*
1130 * Iterate over a hash table containing channels and cleanup safely.
1131 */
1132 static void destroy_channels(struct lttng_ht *channels)
1133 {
1134 struct lttng_ht_node_str *node;
1135 struct lttng_ht_iter iter;
1136
1137 assert(channels);
1138
1139 rcu_read_lock();
1140 cds_lfht_for_each_entry(channels->ht, &iter.iter, node, node) {
1141 struct ltt_ust_channel *chan =
1142 caa_container_of(node, struct ltt_ust_channel, node);
1143
1144 trace_ust_delete_channel(channels, chan);
1145 trace_ust_destroy_channel(chan);
1146 }
1147 rcu_read_unlock();
1148
1149 ht_cleanup_push(channels);
1150 }
1151
1152 /*
1153 * Cleanup UST global domain.
1154 */
1155 static void destroy_domain_global(struct ltt_ust_domain_global *dom)
1156 {
1157 assert(dom);
1158
1159 destroy_channels(dom->channels);
1160 }
1161
1162 /*
1163 * Cleanup ust session structure
1164 *
1165 * Should *NOT* be called with RCU read-side lock held.
1166 */
1167 void trace_ust_destroy_session(struct ltt_ust_session *session)
1168 {
1169 struct agent *agt;
1170 struct buffer_reg_uid *reg, *sreg;
1171 struct lttng_ht_iter iter;
1172
1173 assert(session);
1174
1175 DBG2("Trace UST destroy session %" PRIu64, session->id);
1176
1177 /* Cleaning up UST domain */
1178 destroy_domain_global(&session->domain_global);
1179
1180 rcu_read_lock();
1181 cds_lfht_for_each_entry(session->agents->ht, &iter.iter, agt, node.node) {
1182 int ret = lttng_ht_del(session->agents, &iter);
1183
1184 assert(!ret);
1185 agent_destroy(agt);
1186 }
1187 rcu_read_unlock();
1188
1189 ht_cleanup_push(session->agents);
1190
1191 /* Cleanup UID buffer registry object(s). */
1192 cds_list_for_each_entry_safe(reg, sreg, &session->buffer_reg_uid_list,
1193 lnode) {
1194 cds_list_del(&reg->lnode);
1195 buffer_reg_uid_remove(reg);
1196 buffer_reg_uid_destroy(reg, session->consumer);
1197 }
1198
1199 consumer_output_put(session->consumer);
1200
1201 fini_pid_tracker(&session->pid_tracker);
1202
1203 free(session);
1204 }
This page took 0.083481 seconds and 5 git commands to generate.