sessiond: enforce mmap output type for kernel metadata channel
[lttng-tools.git] / src / bin / lttng-sessiond / trace-kernel.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
24 #include <common/common.h>
25 #include <common/defaults.h>
26
27 #include "consumer.h"
28 #include "trace-kernel.h"
29
30 /*
31 * Find the channel name for the given kernel session.
32 */
33 struct ltt_kernel_channel *trace_kernel_get_channel_by_name(
34 char *name, struct ltt_kernel_session *session)
35 {
36 struct ltt_kernel_channel *chan;
37
38 assert(session);
39 assert(name);
40
41 /*
42 * If we receive an empty string for channel name, it means the
43 * default channel name is requested.
44 */
45 if (name[0] == '\0')
46 name = DEFAULT_CHANNEL_NAME;
47
48 DBG("Trying to find channel %s", name);
49
50 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
51 if (strcmp(name, chan->channel->name) == 0) {
52 DBG("Found channel by name %s", name);
53 return chan;
54 }
55 }
56
57 return NULL;
58 }
59
60 /*
61 * Find the event for the given channel.
62 */
63 struct ltt_kernel_event *trace_kernel_find_event(
64 char *name, struct ltt_kernel_channel *channel,
65 enum lttng_event_type type,
66 struct lttng_filter_bytecode *filter)
67 {
68 struct ltt_kernel_event *ev;
69 int found = 0;
70
71 assert(name);
72 assert(channel);
73
74 cds_list_for_each_entry(ev, &channel->events_list.head, list) {
75 if (type != LTTNG_EVENT_ALL && ev->type != type) {
76 continue;
77 }
78 if (strcmp(name, ev->event->name)) {
79 continue;
80 }
81 if ((ev->filter && !filter) || (!ev->filter && filter)) {
82 continue;
83 }
84 if (ev->filter && filter) {
85 if (ev->filter->len != filter->len ||
86 memcmp(ev->filter->data, filter->data,
87 filter->len) != 0) {
88 continue;
89 }
90 }
91 found = 1;
92 break;
93 }
94 if (found) {
95 DBG("Found event %s for channel %s", name,
96 channel->channel->name);
97 return ev;
98 } else {
99 return NULL;
100 }
101 }
102
103 /*
104 * Find the event name for the given channel.
105 */
106 struct ltt_kernel_event *trace_kernel_get_event_by_name(
107 char *name, struct ltt_kernel_channel *channel,
108 enum lttng_event_type type)
109 {
110 struct ltt_kernel_event *ev;
111 int found = 0;
112
113 assert(name);
114 assert(channel);
115
116 cds_list_for_each_entry(ev, &channel->events_list.head, list) {
117 if (type != LTTNG_EVENT_ALL && ev->type != type) {
118 continue;
119 }
120 if (strcmp(name, ev->event->name)) {
121 continue;
122 }
123 found = 1;
124 break;
125 }
126 if (found) {
127 DBG("Found event %s for channel %s", name,
128 channel->channel->name);
129 return ev;
130 } else {
131 return NULL;
132 }
133 }
134
135 /*
136 * Allocate and initialize a kernel session data structure.
137 *
138 * Return pointer to structure or NULL.
139 */
140 struct ltt_kernel_session *trace_kernel_create_session(void)
141 {
142 struct ltt_kernel_session *lks = NULL;
143
144 /* Allocate a new ltt kernel session */
145 lks = zmalloc(sizeof(struct ltt_kernel_session));
146 if (lks == NULL) {
147 PERROR("create kernel session zmalloc");
148 goto alloc_error;
149 }
150
151 /* Init data structure */
152 lks->fd = -1;
153 lks->metadata_stream_fd = -1;
154 lks->channel_count = 0;
155 lks->stream_count_global = 0;
156 lks->metadata = NULL;
157 CDS_INIT_LIST_HEAD(&lks->channel_list.head);
158
159 lks->tracker_list_pid = lttng_tracker_list_create();
160 if (!lks->tracker_list_pid) {
161 goto error;
162 }
163 lks->tracker_list_vpid = lttng_tracker_list_create();
164 if (!lks->tracker_list_vpid) {
165 goto error;
166 }
167 lks->tracker_list_uid = lttng_tracker_list_create();
168 if (!lks->tracker_list_uid) {
169 goto error;
170 }
171 lks->tracker_list_vuid = lttng_tracker_list_create();
172 if (!lks->tracker_list_vuid) {
173 goto error;
174 }
175 lks->tracker_list_gid = lttng_tracker_list_create();
176 if (!lks->tracker_list_gid) {
177 goto error;
178 }
179 lks->tracker_list_vgid = lttng_tracker_list_create();
180 if (!lks->tracker_list_vgid) {
181 goto error;
182 }
183 lks->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
184 if (lks->consumer == NULL) {
185 goto error;
186 }
187
188 return lks;
189
190 error:
191 lttng_tracker_list_destroy(lks->tracker_list_pid);
192 lttng_tracker_list_destroy(lks->tracker_list_vpid);
193 lttng_tracker_list_destroy(lks->tracker_list_uid);
194 lttng_tracker_list_destroy(lks->tracker_list_vuid);
195 lttng_tracker_list_destroy(lks->tracker_list_gid);
196 lttng_tracker_list_destroy(lks->tracker_list_vgid);
197 free(lks);
198
199 alloc_error:
200 return NULL;
201 }
202
203 /*
204 * Allocate and initialize a kernel channel data structure.
205 *
206 * Return pointer to structure or NULL.
207 */
208 struct ltt_kernel_channel *trace_kernel_create_channel(
209 struct lttng_channel *chan)
210 {
211 struct ltt_kernel_channel *lkc;
212
213 assert(chan);
214
215 lkc = zmalloc(sizeof(struct ltt_kernel_channel));
216 if (lkc == NULL) {
217 PERROR("ltt_kernel_channel zmalloc");
218 goto error;
219 }
220
221 lkc->channel = zmalloc(sizeof(struct lttng_channel));
222 if (lkc->channel == NULL) {
223 PERROR("lttng_channel zmalloc");
224 free(lkc);
225 goto error;
226 }
227 memcpy(lkc->channel, chan, sizeof(struct lttng_channel));
228
229 /*
230 * If we receive an empty string for channel name, it means the
231 * default channel name is requested.
232 */
233 if (chan->name[0] == '\0') {
234 strncpy(lkc->channel->name, DEFAULT_CHANNEL_NAME,
235 sizeof(lkc->channel->name));
236 }
237 lkc->channel->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
238
239 lkc->fd = -1;
240 lkc->stream_count = 0;
241 lkc->event_count = 0;
242 lkc->enabled = 1;
243 /* Init linked list */
244 CDS_INIT_LIST_HEAD(&lkc->events_list.head);
245 CDS_INIT_LIST_HEAD(&lkc->stream_list.head);
246 CDS_INIT_LIST_HEAD(&lkc->ctx_list);
247
248 return lkc;
249
250 error:
251 return NULL;
252 }
253
254 /*
255 * Allocate and init a kernel context object.
256 *
257 * Return the allocated object or NULL on error.
258 */
259 struct ltt_kernel_context *trace_kernel_create_context(
260 struct lttng_kernel_context *ctx)
261 {
262 struct ltt_kernel_context *kctx;
263
264 kctx = zmalloc(sizeof(*kctx));
265 if (!kctx) {
266 PERROR("zmalloc kernel context");
267 goto error;
268 }
269
270 if (ctx) {
271 memcpy(&kctx->ctx, ctx, sizeof(kctx->ctx));
272 }
273 error:
274 return kctx;
275 }
276
277 /*
278 * Allocate and init a kernel context object from an existing kernel context
279 * object.
280 *
281 * Return the allocated object or NULL on error.
282 */
283 struct ltt_kernel_context *trace_kernel_copy_context(
284 struct ltt_kernel_context *kctx)
285 {
286 struct ltt_kernel_context *kctx_copy;
287
288 assert(kctx);
289 kctx_copy = zmalloc(sizeof(*kctx_copy));
290 if (!kctx_copy) {
291 PERROR("zmalloc ltt_kernel_context");
292 goto error;
293 }
294
295 memcpy(kctx_copy, kctx, sizeof(*kctx_copy));
296 memset(&kctx_copy->list, 0, sizeof(kctx_copy->list));
297
298 error:
299 return kctx_copy;
300 }
301
302 /*
303 * Allocate and initialize a kernel event. Set name and event type.
304 * We own filter_expression, and filter.
305 *
306 * Return pointer to structure or NULL.
307 */
308 struct ltt_kernel_event *trace_kernel_create_event(struct lttng_event *ev,
309 char *filter_expression, struct lttng_filter_bytecode *filter)
310 {
311 struct ltt_kernel_event *lke;
312 struct lttng_kernel_event *attr;
313
314 assert(ev);
315
316 lke = zmalloc(sizeof(struct ltt_kernel_event));
317 attr = zmalloc(sizeof(struct lttng_kernel_event));
318 if (lke == NULL || attr == NULL) {
319 PERROR("kernel event zmalloc");
320 goto error;
321 }
322
323 switch (ev->type) {
324 case LTTNG_EVENT_PROBE:
325 attr->instrumentation = LTTNG_KERNEL_KPROBE;
326 attr->u.kprobe.addr = ev->attr.probe.addr;
327 attr->u.kprobe.offset = ev->attr.probe.offset;
328 strncpy(attr->u.kprobe.symbol_name,
329 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
330 attr->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
331 break;
332 case LTTNG_EVENT_FUNCTION:
333 attr->instrumentation = LTTNG_KERNEL_KRETPROBE;
334 attr->u.kretprobe.addr = ev->attr.probe.addr;
335 attr->u.kretprobe.offset = ev->attr.probe.offset;
336 strncpy(attr->u.kretprobe.symbol_name,
337 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
338 attr->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
339 break;
340 case LTTNG_EVENT_FUNCTION_ENTRY:
341 attr->instrumentation = LTTNG_KERNEL_FUNCTION;
342 strncpy(attr->u.ftrace.symbol_name,
343 ev->attr.ftrace.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
344 attr->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
345 break;
346 case LTTNG_EVENT_TRACEPOINT:
347 attr->instrumentation = LTTNG_KERNEL_TRACEPOINT;
348 break;
349 case LTTNG_EVENT_SYSCALL:
350 attr->instrumentation = LTTNG_KERNEL_SYSCALL;
351 break;
352 case LTTNG_EVENT_ALL:
353 attr->instrumentation = LTTNG_KERNEL_ALL;
354 break;
355 default:
356 ERR("Unknown kernel instrumentation type (%d)", ev->type);
357 goto error;
358 }
359
360 /* Copy event name */
361 strncpy(attr->name, ev->name, LTTNG_KERNEL_SYM_NAME_LEN);
362 attr->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
363
364 /* Setting up a kernel event */
365 lke->fd = -1;
366 lke->event = attr;
367 lke->enabled = 1;
368 lke->filter_expression = filter_expression;
369 lke->filter = filter;
370
371 return lke;
372
373 error:
374 free(filter_expression);
375 free(filter);
376 free(lke);
377 free(attr);
378 return NULL;
379 }
380
381 /*
382 * Allocate and initialize a kernel metadata.
383 *
384 * Return pointer to structure or NULL.
385 */
386 struct ltt_kernel_metadata *trace_kernel_create_metadata(void)
387 {
388 int ret;
389 struct ltt_kernel_metadata *lkm;
390 struct lttng_channel *chan;
391
392 lkm = zmalloc(sizeof(struct ltt_kernel_metadata));
393 chan = zmalloc(sizeof(struct lttng_channel));
394 if (lkm == NULL || chan == NULL) {
395 PERROR("kernel metadata zmalloc");
396 goto error;
397 }
398
399 ret = lttng_strncpy(
400 chan->name, DEFAULT_METADATA_NAME, sizeof(chan->name));
401 if (ret) {
402 ERR("Failed to initialize metadata channel name to `%s`",
403 DEFAULT_METADATA_NAME);
404 goto error;
405 }
406
407 /* Set default attributes */
408 chan->attr.overwrite = DEFAULT_METADATA_OVERWRITE;
409 chan->attr.subbuf_size = default_get_metadata_subbuf_size();
410 chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
411 chan->attr.switch_timer_interval = DEFAULT_METADATA_SWITCH_TIMER;
412 chan->attr.read_timer_interval = DEFAULT_METADATA_READ_TIMER;;
413
414
415 /*
416 * The metadata channel of kernel sessions must use the "mmap"
417 * back-end since the consumer daemon accumulates complete
418 * metadata units before sending them to the relay daemon in
419 * live mode. The consumer daemon also needs to extract the contents
420 * of the metadata cache when computing a rotation position.
421 *
422 * In both cases, it is not possible to rely on the splice
423 * back-end as the consumer daemon may need to accumulate more
424 * content than can be backed by the ring buffer's underlying
425 * pages.
426 */
427 chan->attr.output = LTTNG_EVENT_MMAP;
428 chan->attr.tracefile_size = 0;
429 chan->attr.tracefile_count = 0;
430 chan->attr.live_timer_interval = 0;
431
432 /* Init metadata */
433 lkm->fd = -1;
434 lkm->conf = chan;
435
436 return lkm;
437
438 error:
439 free(lkm);
440 free(chan);
441 return NULL;
442 }
443
444 /*
445 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
446 * default.
447 *
448 * Return pointer to structure or NULL.
449 */
450 struct ltt_kernel_stream *trace_kernel_create_stream(const char *name,
451 unsigned int count)
452 {
453 int ret;
454 struct ltt_kernel_stream *lks;
455
456 assert(name);
457
458 lks = zmalloc(sizeof(struct ltt_kernel_stream));
459 if (lks == NULL) {
460 PERROR("kernel stream zmalloc");
461 goto error;
462 }
463
464 /* Set name */
465 ret = snprintf(lks->name, sizeof(lks->name), "%s_%u", name, count);
466 if (ret < 0) {
467 PERROR("snprintf stream name");
468 goto error;
469 }
470 lks->name[sizeof(lks->name) - 1] = '\0';
471
472 /* Init stream */
473 lks->fd = -1;
474 lks->state = 0;
475 lks->cpu = count;
476
477 return lks;
478
479 error:
480 return NULL;
481 }
482
483 /*
484 * Cleanup kernel stream structure.
485 */
486 void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream)
487 {
488 assert(stream);
489
490 DBG("[trace] Closing stream fd %d", stream->fd);
491 /* Close kernel fd */
492 if (stream->fd >= 0) {
493 int ret;
494
495 ret = close(stream->fd);
496 if (ret) {
497 PERROR("close");
498 }
499 }
500 /* Remove from stream list */
501 cds_list_del(&stream->list);
502
503 free(stream);
504 }
505
506 /*
507 * Cleanup kernel event structure.
508 */
509 void trace_kernel_destroy_event(struct ltt_kernel_event *event)
510 {
511 assert(event);
512
513 if (event->fd >= 0) {
514 int ret;
515
516 DBG("[trace] Closing event fd %d", event->fd);
517 /* Close kernel fd */
518 ret = close(event->fd);
519 if (ret) {
520 PERROR("close");
521 }
522 } else {
523 DBG("[trace] Tearing down event (no associated fd)");
524 }
525
526 /* Remove from event list */
527 cds_list_del(&event->list);
528
529 free(event->filter_expression);
530 free(event->filter);
531
532 free(event->event);
533 free(event);
534 }
535
536 /*
537 * Cleanup kernel context structure.
538 */
539 void trace_kernel_destroy_context(struct ltt_kernel_context *ctx)
540 {
541 assert(ctx);
542
543 if (ctx->in_list) {
544 cds_list_del(&ctx->list);
545 }
546 free(ctx);
547 }
548
549 /*
550 * Cleanup kernel channel structure.
551 */
552 void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel)
553 {
554 struct ltt_kernel_stream *stream, *stmp;
555 struct ltt_kernel_event *event, *etmp;
556 struct ltt_kernel_context *ctx, *ctmp;
557 int ret;
558
559 assert(channel);
560
561 DBG("[trace] Closing channel fd %d", channel->fd);
562 /* Close kernel fd */
563 if (channel->fd >= 0) {
564 ret = close(channel->fd);
565 if (ret) {
566 PERROR("close");
567 }
568 }
569
570 /* For each stream in the channel list */
571 cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) {
572 trace_kernel_destroy_stream(stream);
573 }
574
575 /* For each event in the channel list */
576 cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) {
577 trace_kernel_destroy_event(event);
578 }
579
580 /* For each context in the channel list */
581 cds_list_for_each_entry_safe(ctx, ctmp, &channel->ctx_list, list) {
582 trace_kernel_destroy_context(ctx);
583 }
584
585 /* Remove from channel list */
586 cds_list_del(&channel->list);
587
588 free(channel->channel);
589 free(channel);
590 }
591
592 /*
593 * Cleanup kernel metadata structure.
594 */
595 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata)
596 {
597 assert(metadata);
598
599 DBG("[trace] Closing metadata fd %d", metadata->fd);
600 /* Close kernel fd */
601 if (metadata->fd >= 0) {
602 int ret;
603
604 ret = close(metadata->fd);
605 if (ret) {
606 PERROR("close");
607 }
608 }
609
610 free(metadata->conf);
611 free(metadata);
612 }
613
614 /*
615 * Cleanup kernel session structure
616 *
617 * Should *NOT* be called with RCU read-side lock held.
618 */
619 void trace_kernel_destroy_session(struct ltt_kernel_session *session)
620 {
621 struct ltt_kernel_channel *channel, *ctmp;
622 int ret;
623
624 assert(session);
625
626 DBG("[trace] Closing session fd %d", session->fd);
627 /* Close kernel fds */
628 if (session->fd >= 0) {
629 ret = close(session->fd);
630 if (ret) {
631 PERROR("close");
632 }
633 }
634
635 if (session->metadata_stream_fd >= 0) {
636 DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd);
637 ret = close(session->metadata_stream_fd);
638 if (ret) {
639 PERROR("close");
640 }
641 }
642
643 if (session->metadata != NULL) {
644 trace_kernel_destroy_metadata(session->metadata);
645 }
646
647 cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) {
648 trace_kernel_destroy_channel(channel);
649 }
650
651 /* Wipe consumer output object */
652 consumer_output_put(session->consumer);
653
654 lttng_tracker_list_destroy(session->tracker_list_pid);
655 lttng_tracker_list_destroy(session->tracker_list_vpid);
656 lttng_tracker_list_destroy(session->tracker_list_uid);
657 lttng_tracker_list_destroy(session->tracker_list_vuid);
658 lttng_tracker_list_destroy(session->tracker_list_gid);
659 lttng_tracker_list_destroy(session->tracker_list_vgid);
660
661 free(session);
662 }
This page took 0.05217 seconds and 5 git commands to generate.