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