Notify java agent of enabled application contexts
[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
360 /* Translate to UST output enum */
361 switch (luc->attr.output) {
362 default:
363 luc->attr.output = LTTNG_UST_MMAP;
364 break;
365 }
366
367 /*
368 * If we receive an empty string for channel name, it means the
369 * default channel name is requested.
370 */
371 if (chan->name[0] == '\0') {
372 strncpy(luc->name, DEFAULT_CHANNEL_NAME, sizeof(luc->name));
373 } else {
374 /* Copy channel name */
375 strncpy(luc->name, chan->name, sizeof(luc->name));
376 }
377 luc->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
378
379 /* Init node */
380 lttng_ht_node_init_str(&luc->node, luc->name);
381 CDS_INIT_LIST_HEAD(&luc->ctx_list);
382
383 /* Alloc hash tables */
384 luc->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
385 luc->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
386
387 /* On-disk circular buffer parameters */
388 luc->tracefile_size = chan->attr.tracefile_size;
389 luc->tracefile_count = chan->attr.tracefile_count;
390
391 DBG2("Trace UST channel %s created", luc->name);
392
393 error:
394 return luc;
395 }
396
397 /*
398 * Validates an exclusion list.
399 *
400 * Returns 0 if valid, negative value if invalid.
401 */
402 static int validate_exclusion(struct lttng_event_exclusion *exclusion)
403 {
404 size_t i;
405 int ret = 0;
406
407 assert(exclusion);
408
409 for (i = 0; i < exclusion->count; ++i) {
410 size_t j;
411 const char *name_a =
412 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, i);
413
414 for (j = 0; j < i; ++j) {
415 const char *name_b =
416 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, j);
417
418 if (!strncmp(name_a, name_b, LTTNG_SYMBOL_NAME_LEN)) {
419 /* Match! */
420 ret = -1;
421 goto end;
422 }
423 }
424 }
425
426 end:
427 return ret;
428 }
429
430 /*
431 * Allocate and initialize a ust event. Set name and event type.
432 * We own filter_expression, filter, and exclusion.
433 *
434 * Return pointer to structure or NULL.
435 */
436 struct ltt_ust_event *trace_ust_create_event(struct lttng_event *ev,
437 char *filter_expression,
438 struct lttng_filter_bytecode *filter,
439 struct lttng_event_exclusion *exclusion,
440 bool internal_event)
441 {
442 struct ltt_ust_event *lue;
443
444 assert(ev);
445
446 if (exclusion && validate_exclusion(exclusion)) {
447 goto error;
448 }
449
450 lue = zmalloc(sizeof(struct ltt_ust_event));
451 if (lue == NULL) {
452 PERROR("ust event zmalloc");
453 goto error;
454 }
455
456 lue->internal = internal_event;
457
458 switch (ev->type) {
459 case LTTNG_EVENT_PROBE:
460 lue->attr.instrumentation = LTTNG_UST_PROBE;
461 break;
462 case LTTNG_EVENT_FUNCTION:
463 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
464 break;
465 case LTTNG_EVENT_FUNCTION_ENTRY:
466 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
467 break;
468 case LTTNG_EVENT_TRACEPOINT:
469 lue->attr.instrumentation = LTTNG_UST_TRACEPOINT;
470 break;
471 default:
472 ERR("Unknown ust instrumentation type (%d)", ev->type);
473 goto error_free_event;
474 }
475
476 /* Copy event name */
477 strncpy(lue->attr.name, ev->name, LTTNG_UST_SYM_NAME_LEN);
478 lue->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
479
480 switch (ev->loglevel_type) {
481 case LTTNG_EVENT_LOGLEVEL_ALL:
482 lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_ALL;
483 lue->attr.loglevel = -1; /* Force to -1 */
484 break;
485 case LTTNG_EVENT_LOGLEVEL_RANGE:
486 lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_RANGE;
487 lue->attr.loglevel = ev->loglevel;
488 break;
489 case LTTNG_EVENT_LOGLEVEL_SINGLE:
490 lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_SINGLE;
491 lue->attr.loglevel = ev->loglevel;
492 break;
493 default:
494 ERR("Unknown ust loglevel type (%d)", ev->loglevel_type);
495 goto error_free_event;
496 }
497
498 /* Same layout. */
499 lue->filter_expression = filter_expression;
500 lue->filter = filter;
501 lue->exclusion = exclusion;
502
503 /* Init node */
504 lttng_ht_node_init_str(&lue->node, lue->attr.name);
505
506 DBG2("Trace UST event %s, loglevel (%d,%d) created",
507 lue->attr.name, lue->attr.loglevel_type,
508 lue->attr.loglevel);
509
510 return lue;
511
512 error_free_event:
513 free(lue);
514 error:
515 free(filter_expression);
516 free(filter);
517 free(exclusion);
518 return NULL;
519 }
520
521 static
522 int trace_ust_context_type_event_to_ust(
523 enum lttng_event_context_type type)
524 {
525 int utype;
526
527 switch (type) {
528 case LTTNG_EVENT_CONTEXT_VTID:
529 utype = LTTNG_UST_CONTEXT_VTID;
530 break;
531 case LTTNG_EVENT_CONTEXT_VPID:
532 utype = LTTNG_UST_CONTEXT_VPID;
533 break;
534 case LTTNG_EVENT_CONTEXT_PTHREAD_ID:
535 utype = LTTNG_UST_CONTEXT_PTHREAD_ID;
536 break;
537 case LTTNG_EVENT_CONTEXT_PROCNAME:
538 utype = LTTNG_UST_CONTEXT_PROCNAME;
539 break;
540 case LTTNG_EVENT_CONTEXT_IP:
541 utype = LTTNG_UST_CONTEXT_IP;
542 break;
543 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
544 if (!ustctl_has_perf_counters()) {
545 utype = -1;
546 WARN("Perf counters not implemented in UST");
547 } else {
548 utype = LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER;
549 }
550 break;
551 case LTTNG_EVENT_CONTEXT_APP_CONTEXT:
552 utype = LTTNG_UST_CONTEXT_APP_CONTEXT;
553 break;
554 default:
555 utype = -1;
556 break;
557 }
558 return utype;
559 }
560
561 /*
562 * Return 1 if contexts match, 0 otherwise.
563 */
564 int trace_ust_match_context(struct ltt_ust_context *uctx,
565 struct lttng_event_context *ctx)
566 {
567 int utype;
568
569 utype = trace_ust_context_type_event_to_ust(ctx->ctx);
570 if (utype < 0) {
571 return 0;
572 }
573 if (uctx->ctx.ctx != utype) {
574 return 0;
575 }
576 switch (utype) {
577 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER:
578 if (uctx->ctx.u.perf_counter.type
579 != ctx->u.perf_counter.type) {
580 return 0;
581 }
582 if (uctx->ctx.u.perf_counter.config
583 != ctx->u.perf_counter.config) {
584 return 0;
585 }
586 if (strncmp(uctx->ctx.u.perf_counter.name,
587 ctx->u.perf_counter.name,
588 LTTNG_UST_SYM_NAME_LEN)) {
589 return 0;
590 }
591 break;
592 default:
593 break;
594
595 }
596 return 1;
597 }
598
599 /*
600 * Allocate and initialize an UST context.
601 *
602 * Return pointer to structure or NULL.
603 */
604 struct ltt_ust_context *trace_ust_create_context(
605 struct lttng_event_context *ctx)
606 {
607 struct ltt_ust_context *uctx = NULL;
608 int utype;
609
610 assert(ctx);
611
612 utype = trace_ust_context_type_event_to_ust(ctx->ctx);
613 if (utype < 0) {
614 ERR("Invalid UST context");
615 goto end;
616 }
617
618 uctx = zmalloc(sizeof(struct ltt_ust_context));
619 if (!uctx) {
620 PERROR("zmalloc ltt_ust_context");
621 goto end;
622 }
623
624 uctx->ctx.ctx = (enum lttng_ust_context_type) utype;
625 switch (utype) {
626 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER:
627 uctx->ctx.u.perf_counter.type = ctx->u.perf_counter.type;
628 uctx->ctx.u.perf_counter.config = ctx->u.perf_counter.config;
629 strncpy(uctx->ctx.u.perf_counter.name, ctx->u.perf_counter.name,
630 LTTNG_UST_SYM_NAME_LEN);
631 uctx->ctx.u.perf_counter.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
632 break;
633 case LTTNG_UST_CONTEXT_APP_CONTEXT:
634 {
635 char *provider_name = NULL, *ctx_name = NULL;
636
637 provider_name = strdup(ctx->u.app_ctx.provider_name);
638 if (!provider_name) {
639 goto error;
640 }
641 uctx->ctx.u.app_ctx.provider_name = provider_name;
642
643 ctx_name = strdup(ctx->u.app_ctx.ctx_name);
644 if (!ctx_name) {
645 goto error;
646 }
647 uctx->ctx.u.app_ctx.ctx_name = ctx_name;
648 break;
649 }
650 default:
651 break;
652 }
653 lttng_ht_node_init_ulong(&uctx->node, (unsigned long) uctx->ctx.ctx);
654 end:
655 return uctx;
656 error:
657 trace_ust_destroy_context(uctx);
658 return NULL;
659 }
660
661 static
662 void destroy_pid_tracker_node_rcu(struct rcu_head *head)
663 {
664 struct ust_pid_tracker_node *tracker_node =
665 caa_container_of(head, struct ust_pid_tracker_node, node.head);
666 free(tracker_node);
667 }
668
669 static
670 void destroy_pid_tracker_node(struct ust_pid_tracker_node *tracker_node)
671 {
672
673 call_rcu(&tracker_node->node.head, destroy_pid_tracker_node_rcu);
674 }
675
676 static
677 int init_pid_tracker(struct ust_pid_tracker *pid_tracker)
678 {
679 int ret = 0;
680
681 pid_tracker->ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
682 if (!pid_tracker->ht) {
683 ret = -1;
684 goto end;
685 }
686
687 end:
688 return ret;
689 }
690
691 /*
692 * Teardown pid tracker content, but don't free pid_tracker object.
693 */
694 static
695 void fini_pid_tracker(struct ust_pid_tracker *pid_tracker)
696 {
697 struct ust_pid_tracker_node *tracker_node;
698 struct lttng_ht_iter iter;
699
700 if (!pid_tracker->ht) {
701 return;
702 }
703 rcu_read_lock();
704 cds_lfht_for_each_entry(pid_tracker->ht->ht,
705 &iter.iter, tracker_node, node.node) {
706 int ret = lttng_ht_del(pid_tracker->ht, &iter);
707
708 assert(!ret);
709 destroy_pid_tracker_node(tracker_node);
710 }
711 rcu_read_unlock();
712 ht_cleanup_push(pid_tracker->ht);
713 pid_tracker->ht = NULL;
714 }
715
716 static
717 struct ust_pid_tracker_node *pid_tracker_lookup(
718 struct ust_pid_tracker *pid_tracker, int pid,
719 struct lttng_ht_iter *iter)
720 {
721 unsigned long _pid = (unsigned long) pid;
722 struct lttng_ht_node_ulong *node;
723
724 lttng_ht_lookup(pid_tracker->ht, (void *) _pid, iter);
725 node = lttng_ht_iter_get_node_ulong(iter);
726 if (node) {
727 return caa_container_of(node, struct ust_pid_tracker_node,
728 node);
729 } else {
730 return NULL;
731 }
732 }
733
734 static
735 int pid_tracker_add_pid(struct ust_pid_tracker *pid_tracker, int pid)
736 {
737 int retval = LTTNG_OK;
738 struct ust_pid_tracker_node *tracker_node;
739 struct lttng_ht_iter iter;
740
741 if (pid < 0) {
742 retval = LTTNG_ERR_INVALID;
743 goto end;
744 }
745 tracker_node = pid_tracker_lookup(pid_tracker, pid, &iter);
746 if (tracker_node) {
747 /* Already exists. */
748 retval = LTTNG_ERR_PID_TRACKED;
749 goto end;
750 }
751 tracker_node = zmalloc(sizeof(*tracker_node));
752 if (!tracker_node) {
753 retval = LTTNG_ERR_NOMEM;
754 goto end;
755 }
756 lttng_ht_node_init_ulong(&tracker_node->node, (unsigned long) pid);
757 lttng_ht_add_unique_ulong(pid_tracker->ht, &tracker_node->node);
758 end:
759 return retval;
760 }
761
762 static
763 int pid_tracker_del_pid(struct ust_pid_tracker *pid_tracker, int pid)
764 {
765 int retval = LTTNG_OK, ret;
766 struct ust_pid_tracker_node *tracker_node;
767 struct lttng_ht_iter iter;
768
769 if (pid < 0) {
770 retval = LTTNG_ERR_INVALID;
771 goto end;
772 }
773 tracker_node = pid_tracker_lookup(pid_tracker, pid, &iter);
774 if (!tracker_node) {
775 /* Not found */
776 retval = LTTNG_ERR_PID_NOT_TRACKED;
777 goto end;
778 }
779 ret = lttng_ht_del(pid_tracker->ht, &iter);
780 assert(!ret);
781
782 destroy_pid_tracker_node(tracker_node);
783 end:
784 return retval;
785 }
786
787 /*
788 * The session lock is held when calling this function.
789 */
790 int trace_ust_pid_tracker_lookup(struct ltt_ust_session *session, int pid)
791 {
792 struct lttng_ht_iter iter;
793
794 if (!session->pid_tracker.ht) {
795 return 1;
796 }
797 if (pid_tracker_lookup(&session->pid_tracker, pid, &iter)) {
798 return 1;
799 }
800 return 0;
801 }
802
803 /*
804 * Called with the session lock held.
805 */
806 int trace_ust_track_pid(struct ltt_ust_session *session, int pid)
807 {
808 int retval = LTTNG_OK;
809
810 if (pid == -1) {
811 /* Track all pids: destroy tracker if exists. */
812 if (session->pid_tracker.ht) {
813 fini_pid_tracker(&session->pid_tracker);
814 /* Ensure all apps have session. */
815 ust_app_global_update_all(session);
816 }
817 } else {
818 int ret;
819
820 if (!session->pid_tracker.ht) {
821 /* Create tracker. */
822 if (init_pid_tracker(&session->pid_tracker)) {
823 ERR("Error initializing PID tracker");
824 retval = LTTNG_ERR_NOMEM;
825 goto end;
826 }
827 ret = pid_tracker_add_pid(&session->pid_tracker, pid);
828 if (ret != LTTNG_OK) {
829 retval = ret;
830 fini_pid_tracker(&session->pid_tracker);
831 goto end;
832 }
833 /* Remove all apps from session except pid. */
834 ust_app_global_update_all(session);
835 } else {
836 struct ust_app *app;
837
838 ret = pid_tracker_add_pid(&session->pid_tracker, pid);
839 if (ret != LTTNG_OK) {
840 retval = ret;
841 goto end;
842 }
843 /* Add session to application */
844 app = ust_app_find_by_pid(pid);
845 if (app) {
846 ust_app_global_update(session, app);
847 }
848 }
849 }
850 end:
851 return retval;
852 }
853
854 /*
855 * Called with the session lock held.
856 */
857 int trace_ust_untrack_pid(struct ltt_ust_session *session, int pid)
858 {
859 int retval = LTTNG_OK;
860
861 if (pid == -1) {
862 /* Create empty tracker, replace old tracker. */
863 struct ust_pid_tracker tmp_tracker;
864
865 tmp_tracker = session->pid_tracker;
866 if (init_pid_tracker(&session->pid_tracker)) {
867 ERR("Error initializing PID tracker");
868 retval = LTTNG_ERR_NOMEM;
869 /* Rollback operation. */
870 session->pid_tracker = tmp_tracker;
871 goto end;
872 }
873 fini_pid_tracker(&tmp_tracker);
874
875 /* Remove session from all applications */
876 ust_app_global_update_all(session);
877 } else {
878 int ret;
879 struct ust_app *app;
880
881 if (!session->pid_tracker.ht) {
882 /* No PID being tracked. */
883 retval = LTTNG_ERR_PID_NOT_TRACKED;
884 goto end;
885 }
886 /* Remove PID from tracker */
887 ret = pid_tracker_del_pid(&session->pid_tracker, pid);
888 if (ret != LTTNG_OK) {
889 retval = ret;
890 goto end;
891 }
892 /* Remove session from application. */
893 app = ust_app_find_by_pid(pid);
894 if (app) {
895 ust_app_global_update(session, app);
896 }
897 }
898 end:
899 return retval;
900 }
901
902 /*
903 * Called with session lock held.
904 */
905 ssize_t trace_ust_list_tracker_pids(struct ltt_ust_session *session,
906 int32_t **_pids)
907 {
908 struct ust_pid_tracker_node *tracker_node;
909 struct lttng_ht_iter iter;
910 unsigned long count, i = 0;
911 long approx[2];
912 int32_t *pids;
913 int ret = 0;
914
915 if (!session->pid_tracker.ht) {
916 /* Tracker disabled. Set first entry to -1. */
917 pids = zmalloc(sizeof(*pids));
918 if (!pids) {
919 ret = -1;
920 goto end;
921 }
922 pids[0] = -1;
923 *_pids = pids;
924 return 1;
925 }
926
927 rcu_read_lock();
928 cds_lfht_count_nodes(session->pid_tracker.ht->ht,
929 &approx[0], &count, &approx[1]);
930 pids = zmalloc(sizeof(*pids) * count);
931 if (!pids) {
932 ret = -1;
933 goto end;
934 }
935 cds_lfht_for_each_entry(session->pid_tracker.ht->ht,
936 &iter.iter, tracker_node, node.node) {
937 pids[i++] = tracker_node->node.key;
938 }
939 *_pids = pids;
940 ret = count;
941 end:
942 rcu_read_unlock();
943 return ret;
944 }
945
946 /*
947 * RCU safe free context structure.
948 */
949 static void destroy_context_rcu(struct rcu_head *head)
950 {
951 struct lttng_ht_node_ulong *node =
952 caa_container_of(head, struct lttng_ht_node_ulong, head);
953 struct ltt_ust_context *ctx =
954 caa_container_of(node, struct ltt_ust_context, node);
955
956 trace_ust_destroy_context(ctx);
957 }
958
959 /*
960 * Cleanup UST context hash table.
961 */
962 static void destroy_contexts(struct lttng_ht *ht)
963 {
964 int ret;
965 struct lttng_ht_node_ulong *node;
966 struct lttng_ht_iter iter;
967 struct ltt_ust_context *ctx;
968
969 assert(ht);
970
971 rcu_read_lock();
972 cds_lfht_for_each_entry(ht->ht, &iter.iter, node, node) {
973 /* Remove from ordered list. */
974 ctx = caa_container_of(node, struct ltt_ust_context, node);
975 cds_list_del(&ctx->list);
976 /* Remove from channel's hash table. */
977 ret = lttng_ht_del(ht, &iter);
978 if (!ret) {
979 call_rcu(&node->head, destroy_context_rcu);
980 }
981 }
982 rcu_read_unlock();
983
984 ht_cleanup_push(ht);
985 }
986
987 /*
988 * Cleanup ust event structure.
989 */
990 void trace_ust_destroy_event(struct ltt_ust_event *event)
991 {
992 assert(event);
993
994 DBG2("Trace destroy UST event %s", event->attr.name);
995 free(event->filter_expression);
996 free(event->filter);
997 free(event->exclusion);
998 free(event);
999 }
1000
1001 /*
1002 * Cleanup ust context structure.
1003 */
1004 void trace_ust_destroy_context(struct ltt_ust_context *ctx)
1005 {
1006 assert(ctx);
1007
1008 if (ctx->ctx.ctx == LTTNG_UST_CONTEXT_APP_CONTEXT) {
1009 free(ctx->ctx.u.app_ctx.provider_name);
1010 free(ctx->ctx.u.app_ctx.ctx_name);
1011 }
1012 free(ctx);
1013 }
1014
1015 /*
1016 * URCU intermediate call to complete destroy event.
1017 */
1018 static void destroy_event_rcu(struct rcu_head *head)
1019 {
1020 struct lttng_ht_node_str *node =
1021 caa_container_of(head, struct lttng_ht_node_str, head);
1022 struct ltt_ust_event *event =
1023 caa_container_of(node, struct ltt_ust_event, node);
1024
1025 trace_ust_destroy_event(event);
1026 }
1027
1028 /*
1029 * Cleanup UST events hashtable.
1030 */
1031 static void destroy_events(struct lttng_ht *events)
1032 {
1033 int ret;
1034 struct lttng_ht_node_str *node;
1035 struct lttng_ht_iter iter;
1036
1037 assert(events);
1038
1039 rcu_read_lock();
1040 cds_lfht_for_each_entry(events->ht, &iter.iter, node, node) {
1041 ret = lttng_ht_del(events, &iter);
1042 assert(!ret);
1043 call_rcu(&node->head, destroy_event_rcu);
1044 }
1045 rcu_read_unlock();
1046
1047 ht_cleanup_push(events);
1048 }
1049
1050 /*
1051 * Cleanup ust channel structure.
1052 *
1053 * Should _NOT_ be called with RCU read lock held.
1054 */
1055 static void _trace_ust_destroy_channel(struct ltt_ust_channel *channel)
1056 {
1057 assert(channel);
1058
1059 DBG2("Trace destroy UST channel %s", channel->name);
1060
1061 free(channel);
1062 }
1063
1064 /*
1065 * URCU intermediate call to complete destroy channel.
1066 */
1067 static void destroy_channel_rcu(struct rcu_head *head)
1068 {
1069 struct lttng_ht_node_str *node =
1070 caa_container_of(head, struct lttng_ht_node_str, head);
1071 struct ltt_ust_channel *channel =
1072 caa_container_of(node, struct ltt_ust_channel, node);
1073
1074 _trace_ust_destroy_channel(channel);
1075 }
1076
1077 void trace_ust_destroy_channel(struct ltt_ust_channel *channel)
1078 {
1079 /* Destroying all events of the channel */
1080 destroy_events(channel->events);
1081 /* Destroying all context of the channel */
1082 destroy_contexts(channel->ctx);
1083
1084 call_rcu(&channel->node.head, destroy_channel_rcu);
1085 }
1086
1087 /*
1088 * Remove an UST channel from a channel HT.
1089 */
1090 void trace_ust_delete_channel(struct lttng_ht *ht,
1091 struct ltt_ust_channel *channel)
1092 {
1093 int ret;
1094 struct lttng_ht_iter iter;
1095
1096 assert(ht);
1097 assert(channel);
1098
1099 iter.iter.node = &channel->node.node;
1100 ret = lttng_ht_del(ht, &iter);
1101 assert(!ret);
1102 }
1103
1104 /*
1105 * Iterate over a hash table containing channels and cleanup safely.
1106 */
1107 static void destroy_channels(struct lttng_ht *channels)
1108 {
1109 struct lttng_ht_node_str *node;
1110 struct lttng_ht_iter iter;
1111
1112 assert(channels);
1113
1114 rcu_read_lock();
1115 cds_lfht_for_each_entry(channels->ht, &iter.iter, node, node) {
1116 struct ltt_ust_channel *chan =
1117 caa_container_of(node, struct ltt_ust_channel, node);
1118
1119 trace_ust_delete_channel(channels, chan);
1120 trace_ust_destroy_channel(chan);
1121 }
1122 rcu_read_unlock();
1123
1124 ht_cleanup_push(channels);
1125 }
1126
1127 /*
1128 * Cleanup UST global domain.
1129 */
1130 static void destroy_domain_global(struct ltt_ust_domain_global *dom)
1131 {
1132 assert(dom);
1133
1134 destroy_channels(dom->channels);
1135 }
1136
1137 /*
1138 * Cleanup ust session structure
1139 *
1140 * Should *NOT* be called with RCU read-side lock held.
1141 */
1142 void trace_ust_destroy_session(struct ltt_ust_session *session)
1143 {
1144 struct agent *agt;
1145 struct buffer_reg_uid *reg, *sreg;
1146 struct lttng_ht_iter iter;
1147
1148 assert(session);
1149
1150 DBG2("Trace UST destroy session %" PRIu64, session->id);
1151
1152 /* Cleaning up UST domain */
1153 destroy_domain_global(&session->domain_global);
1154
1155 rcu_read_lock();
1156 cds_lfht_for_each_entry(session->agents->ht, &iter.iter, agt, node.node) {
1157 int ret = lttng_ht_del(session->agents, &iter);
1158
1159 assert(!ret);
1160 agent_destroy(agt);
1161 }
1162 rcu_read_unlock();
1163
1164 ht_cleanup_push(session->agents);
1165
1166 /* Cleanup UID buffer registry object(s). */
1167 cds_list_for_each_entry_safe(reg, sreg, &session->buffer_reg_uid_list,
1168 lnode) {
1169 cds_list_del(&reg->lnode);
1170 buffer_reg_uid_remove(reg);
1171 buffer_reg_uid_destroy(reg, session->consumer);
1172 }
1173
1174 consumer_output_put(session->consumer);
1175
1176 fini_pid_tracker(&session->pid_tracker);
1177
1178 free(session);
1179 }
This page took 0.054751 seconds and 4 git commands to generate.