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