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