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