Move ust channel registry inside session registry
[lttng-tools.git] / src / bin / lttng-sessiond / trace-ust.c
... / ...
CommitLineData
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 _GNU_SOURCE
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
23
24#include <common/common.h>
25#include <common/defaults.h>
26
27#include "trace-ust.h"
28
29/*
30 * Match function for the events hash table lookup.
31 *
32 * Matches by name only. Used by the disable command.
33 */
34int trace_ust_ht_match_event_by_name(struct cds_lfht_node *node,
35 const void *_key)
36{
37 struct ltt_ust_event *event;
38 const char *name;
39
40 assert(node);
41 assert(_key);
42
43 event = caa_container_of(node, struct ltt_ust_event, node.node);
44 name = _key;
45
46 /* Event name */
47 if (strncmp(event->attr.name, name, sizeof(event->attr.name)) != 0) {
48 goto no_match;
49 }
50
51 /* Match */
52 return 1;
53
54no_match:
55 return 0;
56}
57
58/*
59 * Match function for the hash table lookup.
60 *
61 * It matches an ust event based on three attributes which are the event name,
62 * the filter bytecode and the loglevel.
63 */
64int trace_ust_ht_match_event(struct cds_lfht_node *node, const void *_key)
65{
66 struct ltt_ust_event *event;
67 const struct ltt_ust_ht_key *key;
68
69 assert(node);
70 assert(_key);
71
72 event = caa_container_of(node, struct ltt_ust_event, node.node);
73 key = _key;
74
75 /* Match the 3 elements of the key: name, filter and loglevel. */
76
77 /* Event name */
78 if (strncmp(event->attr.name, key->name, sizeof(event->attr.name)) != 0) {
79 goto no_match;
80 }
81
82 /* Event loglevel. */
83 if (event->attr.loglevel != key->loglevel) {
84 if (event->attr.loglevel_type == LTTNG_UST_LOGLEVEL_ALL
85 && key->loglevel == 0 && event->attr.loglevel == -1) {
86 /*
87 * Match is accepted. This is because on event creation, the
88 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
89 * -1 are accepted for this loglevel type since 0 is the one set by
90 * the API when receiving an enable event.
91 */
92 } else {
93 goto no_match;
94 }
95 }
96
97 /* Only one of the filters is NULL, fail. */
98 if ((key->filter && !event->filter) || (!key->filter && event->filter)) {
99 goto no_match;
100 }
101
102 if (key->filter && event->filter) {
103 /* Both filters exists, check length followed by the bytecode. */
104 if (event->filter->len != key->filter->len ||
105 memcmp(event->filter->data, key->filter->data,
106 event->filter->len) != 0) {
107 goto no_match;
108 }
109 }
110
111 /* Match. */
112 return 1;
113
114no_match:
115 return 0;
116}
117
118/*
119 * Find the channel in the hashtable and return channel pointer. RCU read side
120 * lock MUST be acquired before calling this.
121 */
122struct ltt_ust_channel *trace_ust_find_channel_by_name(struct lttng_ht *ht,
123 char *name)
124{
125 struct lttng_ht_node_str *node;
126 struct lttng_ht_iter iter;
127
128 lttng_ht_lookup(ht, (void *)name, &iter);
129 node = lttng_ht_iter_get_node_str(&iter);
130 if (node == NULL) {
131 goto error;
132 }
133
134 DBG2("Trace UST channel %s found by name", name);
135
136 return caa_container_of(node, struct ltt_ust_channel, node);
137
138error:
139 DBG2("Trace UST channel %s not found by name", name);
140 return NULL;
141}
142
143/*
144 * Find the event in the hashtable and return event pointer. RCU read side lock
145 * MUST be acquired before calling this.
146 */
147struct ltt_ust_event *trace_ust_find_event(struct lttng_ht *ht,
148 char *name, struct lttng_filter_bytecode *filter, int loglevel)
149{
150 struct lttng_ht_node_str *node;
151 struct lttng_ht_iter iter;
152 struct ltt_ust_ht_key key;
153
154 assert(name);
155 assert(ht);
156
157 key.name = name;
158 key.filter = filter;
159 key.loglevel = loglevel;
160
161 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
162 trace_ust_ht_match_event, &key, &iter.iter);
163 node = lttng_ht_iter_get_node_str(&iter);
164 if (node == NULL) {
165 goto error;
166 }
167
168 DBG2("Trace UST event %s found", key.name);
169
170 return caa_container_of(node, struct ltt_ust_event, node);
171
172error:
173 DBG2("Trace UST event %s NOT found", key.name);
174 return NULL;
175}
176
177/*
178 * Allocate and initialize a ust session data structure.
179 *
180 * Return pointer to structure or NULL.
181 */
182struct ltt_ust_session *trace_ust_create_session(char *path,
183 unsigned int session_id)
184{
185 struct ltt_ust_session *lus;
186
187 /* Allocate a new ltt ust session */
188 lus = zmalloc(sizeof(struct ltt_ust_session));
189 if (lus == NULL) {
190 PERROR("create ust session zmalloc");
191 goto error;
192 }
193
194 /* Init data structure */
195 lus->id = session_id;
196 lus->start_trace = 0;
197
198 /* Alloc UST domain hash tables */
199 lus->domain_pid = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
200 lus->domain_exec = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
201
202 /* Alloc UST global domain channels' HT */
203 lus->domain_global.channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
204
205 lus->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
206 if (lus->consumer == NULL) {
207 goto error_consumer;
208 }
209
210 /*
211 * The tmp_consumer stays NULL until a set_consumer_uri command is
212 * executed. At this point, the consumer should be nullify until an
213 * enable_consumer command. This assignment is symbolic since we've zmalloc
214 * the struct.
215 */
216 lus->tmp_consumer = NULL;
217
218 /* Use the default consumer output which is the tracing session path. */
219 if (*path != '\0') {
220 int ret;
221
222 ret = snprintf(lus->consumer->dst.trace_path, PATH_MAX,
223 "%s" DEFAULT_UST_TRACE_DIR, path);
224 if (ret < 0) {
225 PERROR("snprintf UST consumer trace path");
226 goto error_path;
227 }
228
229 /* Set session path */
230 ret = snprintf(lus->pathname, PATH_MAX, "%s" DEFAULT_UST_TRACE_DIR,
231 path);
232 if (ret < 0) {
233 PERROR("snprintf kernel traces path");
234 goto error_path;
235 }
236 }
237
238 DBG2("UST trace session create successful");
239
240 return lus;
241
242error_path:
243 consumer_destroy_output(lus->consumer);
244error_consumer:
245 lttng_ht_destroy(lus->domain_global.channels);
246 lttng_ht_destroy(lus->domain_exec);
247 lttng_ht_destroy(lus->domain_pid);
248 free(lus);
249error:
250 return NULL;
251}
252
253/*
254 * Allocate and initialize a ust channel data structure.
255 *
256 * Return pointer to structure or NULL.
257 */
258struct ltt_ust_channel *trace_ust_create_channel(struct lttng_channel *chan,
259 char *path)
260{
261 int ret;
262 struct ltt_ust_channel *luc;
263
264 assert(chan);
265 assert(path);
266
267 luc = zmalloc(sizeof(struct ltt_ust_channel));
268 if (luc == NULL) {
269 PERROR("ltt_ust_channel zmalloc");
270 goto error;
271 }
272
273 /* Copy UST channel attributes */
274 luc->attr.overwrite = chan->attr.overwrite;
275 luc->attr.subbuf_size = chan->attr.subbuf_size;
276 luc->attr.num_subbuf = chan->attr.num_subbuf;
277 luc->attr.switch_timer_interval = chan->attr.switch_timer_interval;
278 luc->attr.read_timer_interval = chan->attr.read_timer_interval;
279 luc->attr.output = (enum lttng_ust_output) chan->attr.output;
280
281 /* Translate to UST output enum */
282 switch (luc->attr.output) {
283 default:
284 luc->attr.output = LTTNG_UST_MMAP;
285 break;
286 }
287
288 /* Copy channel name */
289 strncpy(luc->name, chan->name, sizeof(luc->name));
290 luc->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
291
292 /* Init node */
293 lttng_ht_node_init_str(&luc->node, luc->name);
294 /* Alloc hash tables */
295 luc->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
296 luc->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
297
298 /* Set trace output path */
299 ret = snprintf(luc->pathname, PATH_MAX, "%s", path);
300 if (ret < 0) {
301 PERROR("asprintf ust create channel");
302 goto error_free_channel;
303 }
304
305 DBG2("Trace UST channel %s created", luc->name);
306
307 return luc;
308
309error_free_channel:
310 lttng_ht_destroy(luc->ctx);
311 lttng_ht_destroy(luc->events);
312 free(luc);
313error:
314 return NULL;
315}
316
317/*
318 * Allocate and initialize a ust event. Set name and event type.
319 *
320 * Return pointer to structure or NULL.
321 */
322struct ltt_ust_event *trace_ust_create_event(struct lttng_event *ev,
323 struct lttng_filter_bytecode *filter)
324{
325 struct ltt_ust_event *lue;
326
327 assert(ev);
328
329 lue = zmalloc(sizeof(struct ltt_ust_event));
330 if (lue == NULL) {
331 PERROR("ust event zmalloc");
332 goto error;
333 }
334
335 switch (ev->type) {
336 case LTTNG_EVENT_PROBE:
337 lue->attr.instrumentation = LTTNG_UST_PROBE;
338 break;
339 case LTTNG_EVENT_FUNCTION:
340 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
341 break;
342 case LTTNG_EVENT_FUNCTION_ENTRY:
343 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
344 break;
345 case LTTNG_EVENT_TRACEPOINT:
346 lue->attr.instrumentation = LTTNG_UST_TRACEPOINT;
347 break;
348 default:
349 ERR("Unknown ust instrumentation type (%d)", ev->type);
350 goto error_free_event;
351 }
352
353 /* Copy event name */
354 strncpy(lue->attr.name, ev->name, LTTNG_UST_SYM_NAME_LEN);
355 lue->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
356
357 switch (ev->loglevel_type) {
358 case LTTNG_EVENT_LOGLEVEL_ALL:
359 lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_ALL;
360 lue->attr.loglevel = -1; /* Force to -1 */
361 break;
362 case LTTNG_EVENT_LOGLEVEL_RANGE:
363 lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_RANGE;
364 lue->attr.loglevel = ev->loglevel;
365 break;
366 case LTTNG_EVENT_LOGLEVEL_SINGLE:
367 lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_SINGLE;
368 lue->attr.loglevel = ev->loglevel;
369 break;
370 default:
371 ERR("Unknown ust loglevel type (%d)", ev->loglevel_type);
372 goto error_free_event;
373 }
374
375 /* Same layout. */
376 lue->filter = (struct lttng_ust_filter_bytecode *) filter;
377
378 /* Init node */
379 lttng_ht_node_init_str(&lue->node, lue->attr.name);
380
381 DBG2("Trace UST event %s, loglevel (%d,%d) created",
382 lue->attr.name, lue->attr.loglevel_type,
383 lue->attr.loglevel);
384
385 return lue;
386
387error_free_event:
388 free(lue);
389error:
390 return NULL;
391}
392
393/*
394 * Allocate and initialize a ust metadata.
395 *
396 * Return pointer to structure or NULL.
397 */
398struct ltt_ust_metadata *trace_ust_create_metadata(char *path)
399{
400 int ret;
401 struct ltt_ust_metadata *lum;
402
403 assert(path);
404
405 lum = zmalloc(sizeof(struct ltt_ust_metadata));
406 if (lum == NULL) {
407 PERROR("ust metadata zmalloc");
408 goto error;
409 }
410
411 /* Set default attributes */
412 lum->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
413 lum->attr.subbuf_size = default_get_metadata_subbuf_size();
414 lum->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
415 lum->attr.switch_timer_interval = DEFAULT_UST_CHANNEL_SWITCH_TIMER;
416 lum->attr.read_timer_interval = DEFAULT_UST_CHANNEL_READ_TIMER;
417 lum->attr.output = LTTNG_UST_MMAP;
418
419 lum->handle = -1;
420 /* Set metadata trace path */
421 ret = snprintf(lum->pathname, PATH_MAX, "%s/" DEFAULT_METADATA_NAME, path);
422 if (ret < 0) {
423 PERROR("asprintf ust metadata");
424 goto error_free_metadata;
425 }
426
427 return lum;
428
429error_free_metadata:
430 free(lum);
431error:
432 return NULL;
433}
434
435/*
436 * Allocate and initialize an UST context.
437 *
438 * Return pointer to structure or NULL.
439 */
440struct ltt_ust_context *trace_ust_create_context(
441 struct lttng_event_context *ctx)
442{
443 struct ltt_ust_context *uctx;
444 enum lttng_ust_context_type utype;
445
446 assert(ctx);
447
448 switch (ctx->ctx) {
449 case LTTNG_EVENT_CONTEXT_VTID:
450 utype = LTTNG_UST_CONTEXT_VTID;
451 break;
452 case LTTNG_EVENT_CONTEXT_VPID:
453 utype = LTTNG_UST_CONTEXT_VPID;
454 break;
455 case LTTNG_EVENT_CONTEXT_PTHREAD_ID:
456 utype = LTTNG_UST_CONTEXT_PTHREAD_ID;
457 break;
458 case LTTNG_EVENT_CONTEXT_PROCNAME:
459 utype = LTTNG_UST_CONTEXT_PROCNAME;
460 break;
461 default:
462 ERR("Invalid UST context");
463 return NULL;
464 }
465
466 uctx = zmalloc(sizeof(struct ltt_ust_context));
467 if (uctx == NULL) {
468 PERROR("zmalloc ltt_ust_context");
469 goto error;
470 }
471
472 uctx->ctx.ctx = utype;
473 lttng_ht_node_init_ulong(&uctx->node, (unsigned long) uctx->ctx.ctx);
474
475 return uctx;
476
477error:
478 return NULL;
479}
480
481/*
482 * RCU safe free context structure.
483 */
484static void destroy_context_rcu(struct rcu_head *head)
485{
486 struct lttng_ht_node_ulong *node =
487 caa_container_of(head, struct lttng_ht_node_ulong, head);
488 struct ltt_ust_context *ctx =
489 caa_container_of(node, struct ltt_ust_context, node);
490
491 free(ctx);
492}
493
494/*
495 * Cleanup UST context hash table.
496 */
497static void destroy_contexts(struct lttng_ht *ht)
498{
499 int ret;
500 struct lttng_ht_node_ulong *node;
501 struct lttng_ht_iter iter;
502
503 assert(ht);
504
505 cds_lfht_for_each_entry(ht->ht, &iter.iter, node, node) {
506 ret = lttng_ht_del(ht, &iter);
507 if (!ret) {
508 call_rcu(&node->head, destroy_context_rcu);
509 }
510 }
511
512 lttng_ht_destroy(ht);
513}
514
515/*
516 * Cleanup ust event structure.
517 */
518void trace_ust_destroy_event(struct ltt_ust_event *event)
519{
520 assert(event);
521
522 DBG2("Trace destroy UST event %s", event->attr.name);
523 free(event->filter);
524 free(event);
525}
526
527/*
528 * URCU intermediate call to complete destroy event.
529 */
530static void destroy_event_rcu(struct rcu_head *head)
531{
532 struct lttng_ht_node_str *node =
533 caa_container_of(head, struct lttng_ht_node_str, head);
534 struct ltt_ust_event *event =
535 caa_container_of(node, struct ltt_ust_event, node);
536
537 trace_ust_destroy_event(event);
538}
539
540/*
541 * Cleanup UST events hashtable.
542 */
543static void destroy_events(struct lttng_ht *events)
544{
545 int ret;
546 struct lttng_ht_node_str *node;
547 struct lttng_ht_iter iter;
548
549 assert(events);
550
551 cds_lfht_for_each_entry(events->ht, &iter.iter, node, node) {
552 ret = lttng_ht_del(events, &iter);
553 assert(!ret);
554 call_rcu(&node->head, destroy_event_rcu);
555 }
556
557 lttng_ht_destroy(events);
558}
559
560/*
561 * Cleanup ust channel structure.
562 */
563void trace_ust_destroy_channel(struct ltt_ust_channel *channel)
564{
565 assert(channel);
566
567 DBG2("Trace destroy UST channel %s", channel->name);
568
569 rcu_read_lock();
570
571 /* Destroying all events of the channel */
572 destroy_events(channel->events);
573 /* Destroying all context of the channel */
574 destroy_contexts(channel->ctx);
575
576 free(channel);
577
578 rcu_read_unlock();
579}
580
581/*
582 * URCU intermediate call to complete destroy channel.
583 */
584static void destroy_channel_rcu(struct rcu_head *head)
585{
586 struct lttng_ht_node_str *node =
587 caa_container_of(head, struct lttng_ht_node_str, head);
588 struct ltt_ust_channel *channel =
589 caa_container_of(node, struct ltt_ust_channel, node);
590
591 trace_ust_destroy_channel(channel);
592}
593
594/*
595 * Cleanup ust metadata structure.
596 */
597void trace_ust_destroy_metadata(struct ltt_ust_metadata *metadata)
598{
599 assert(metadata);
600
601 if (!metadata->handle) {
602 return;
603 }
604 DBG2("Trace UST destroy metadata %d", metadata->handle);
605 free(metadata);
606}
607
608/*
609 * Iterate over a hash table containing channels and cleanup safely.
610 */
611static void destroy_channels(struct lttng_ht *channels)
612{
613 int ret;
614 struct lttng_ht_node_str *node;
615 struct lttng_ht_iter iter;
616
617 assert(channels);
618
619 rcu_read_lock();
620
621 cds_lfht_for_each_entry(channels->ht, &iter.iter, node, node) {
622 ret = lttng_ht_del(channels, &iter);
623 assert(!ret);
624 call_rcu(&node->head, destroy_channel_rcu);
625 }
626
627 lttng_ht_destroy(channels);
628
629 rcu_read_unlock();
630}
631
632/*
633 * Cleanup UST pid domain.
634 */
635static void destroy_domain_pid(struct lttng_ht *ht)
636{
637 int ret;
638 struct lttng_ht_iter iter;
639 struct ltt_ust_domain_pid *dpid;
640
641 assert(ht);
642
643 cds_lfht_for_each_entry(ht->ht, &iter.iter, dpid, node.node) {
644 ret = lttng_ht_del(ht , &iter);
645 assert(!ret);
646 destroy_channels(dpid->channels);
647 }
648
649 lttng_ht_destroy(ht);
650}
651
652/*
653 * Cleanup UST exec name domain.
654 */
655static void destroy_domain_exec(struct lttng_ht *ht)
656{
657 int ret;
658 struct lttng_ht_iter iter;
659 struct ltt_ust_domain_exec *dexec;
660
661 assert(ht);
662
663 cds_lfht_for_each_entry(ht->ht, &iter.iter, dexec, node.node) {
664 ret = lttng_ht_del(ht , &iter);
665 assert(!ret);
666 destroy_channels(dexec->channels);
667 }
668
669 lttng_ht_destroy(ht);
670}
671
672/*
673 * Cleanup UST global domain.
674 */
675static void destroy_domain_global(struct ltt_ust_domain_global *dom)
676{
677 assert(dom);
678
679 destroy_channels(dom->channels);
680}
681
682/*
683 * Cleanup ust session structure
684 */
685void trace_ust_destroy_session(struct ltt_ust_session *session)
686{
687 assert(session);
688
689 rcu_read_lock();
690
691 DBG2("Trace UST destroy session %u", session->id);
692
693 /* Cleaning up UST domain */
694 destroy_domain_global(&session->domain_global);
695 destroy_domain_pid(session->domain_pid);
696 destroy_domain_exec(session->domain_exec);
697
698 consumer_destroy_output(session->consumer);
699 consumer_destroy_output(session->tmp_consumer);
700
701 free(session);
702
703 rcu_read_unlock();
704}
This page took 0.026712 seconds and 4 git commands to generate.