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