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