sessiond: enforce mmap output type for kernel metadata channel
[lttng-tools.git] / src / bin / lttng-sessiond / kernel.c
CommitLineData
20fe2104
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
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.
20fe2104
DG
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 *
d14d33bf
AM
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.
20fe2104
DG
16 */
17
6c1c0768 18#define _LGPL_SOURCE
17136d8c 19
7b395890 20#include <fcntl.h>
20fe2104
DG
21#include <stdlib.h>
22#include <stdio.h>
f34daff7 23#include <string.h>
8c0faa1d 24#include <unistd.h>
77c7c900 25#include <inttypes.h>
32e10baa 26#include <sys/types.h>
20fe2104 27
990570ed 28#include <common/common.h>
698fb2ed 29#include <common/trace-chunk.h>
db758600 30#include <common/kernel-ctl/kernel-ctl.h>
c052142c 31#include <common/kernel-ctl/kernel-ioctl.h>
42224349 32#include <common/sessiond-comm/sessiond-comm.h>
1e307fab 33
32e10baa
MD
34#include "lttng-sessiond.h"
35#include "lttng-syscall.h"
2f77fc4b 36#include "consumer.h"
4771f025 37#include "kernel.h"
6dc3064a 38#include "kernel-consumer.h"
096102bd 39#include "kern-modules.h"
834978fd 40#include "utils.h"
5c408ad8 41#include "rotate.h"
32e10baa 42#include "modprobe.h"
20fe2104 43
e1f3997a
JD
44/*
45 * Key used to reference a channel between the sessiond and the consumer. This
46 * is only read and updated with the session_list lock held.
47 */
48static uint64_t next_kernel_channel_key;
49
32e10baa
MD
50static const char *module_proc_lttng = "/proc/lttng";
51
52static int kernel_tracer_fd = -1;
53
410b78a0
FD
54#include <lttng/userspace-probe.h>
55#include <lttng/userspace-probe-internal.h>
d65106b1 56/*
050349bb 57 * Add context on a kernel channel.
df3c77c8
JG
58 *
59 * Assumes the ownership of ctx.
d65106b1
DG
60 */
61int kernel_add_channel_context(struct ltt_kernel_channel *chan,
645328ae 62 struct ltt_kernel_context *ctx)
d65106b1
DG
63{
64 int ret;
65
0525e9ae
DG
66 assert(chan);
67 assert(ctx);
68
d65106b1 69 DBG("Adding context to channel %s", chan->channel->name);
645328ae 70 ret = kernctl_add_context(chan->fd, &ctx->ctx);
d65106b1 71 if (ret < 0) {
32af2c95 72 switch (-ret) {
1ae5e83e
JD
73 case ENOSYS:
74 /* Exists but not available for this kernel */
75 ret = LTTNG_ERR_KERN_CONTEXT_UNAVAILABLE;
76 goto error;
77 case EEXIST:
b579acd9
DG
78 /* If EEXIST, we just ignore the error */
79 ret = 0;
1ae5e83e
JD
80 goto end;
81 default:
82 PERROR("add context ioctl");
83 ret = LTTNG_ERR_KERN_CONTEXT_FAIL;
84 goto error;
b579acd9 85 }
d65106b1 86 }
21ed98c1 87 ret = 0;
d65106b1 88
1ae5e83e 89end:
645328ae 90 cds_list_add_tail(&ctx->list, &chan->ctx_list);
ba985c3a 91 ctx->in_list = true;
df3c77c8 92 ctx = NULL;
d65106b1 93error:
df3c77c8
JG
94 if (ctx) {
95 trace_kernel_destroy_context(ctx);
96 }
d65106b1
DG
97 return ret;
98}
99
20fe2104 100/*
050349bb
DG
101 * Create a new kernel session, register it to the kernel tracer and add it to
102 * the session daemon session.
20fe2104 103 */
32e10baa 104int kernel_create_session(struct ltt_session *session)
20fe2104
DG
105{
106 int ret;
107 struct ltt_kernel_session *lks;
108
0525e9ae
DG
109 assert(session);
110
54012638 111 /* Allocate data structure */
dec56f6c 112 lks = trace_kernel_create_session();
20fe2104 113 if (lks == NULL) {
54012638 114 ret = -1;
20fe2104
DG
115 goto error;
116 }
117
54012638 118 /* Kernel tracer session creation */
32e10baa 119 ret = kernctl_create_session(kernel_tracer_fd);
20fe2104 120 if (ret < 0) {
df0f840b 121 PERROR("ioctl kernel create session");
20fe2104
DG
122 goto error;
123 }
124
20fe2104 125 lks->fd = ret;
7b395890
DG
126 /* Prevent fd duplication after execlp() */
127 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
128 if (ret < 0) {
df0f840b 129 PERROR("fcntl session fd");
7b395890
DG
130 }
131
53632229 132 lks->id = session->id;
3bd1e081 133 lks->consumer_fds_sent = 0;
8c0faa1d 134 session->kernel_session = lks;
8c0faa1d
DG
135
136 DBG("Kernel session created (fd: %d)", lks->fd);
20fe2104 137
21008574
JR
138 /*
139 * This is necessary since the creation time is present in the session
140 * name when it is generated.
141 */
142 if (session->has_auto_generated_name) {
143 ret = kernctl_session_set_name(lks->fd, DEFAULT_SESSION_NAME);
144 } else {
145 ret = kernctl_session_set_name(lks->fd, session->name);
146 }
147 if (ret) {
148 WARN("Could not set kernel session name for session %" PRIu64 " name: %s",
149 session->id, session->name);
150 }
151
93245d75
JR
152 ret = kernctl_session_set_creation_time(lks->fd, session->creation_time);
153 if (ret) {
154 WARN("Could not set kernel session creation time for session %" PRIu64 " name: %s",
155 session->id, session->name);
156 }
157
20fe2104
DG
158 return 0;
159
160error:
5f62c685
DG
161 if (lks) {
162 trace_kernel_destroy_session(lks);
b1f4d3d9 163 trace_kernel_free_session(lks);
5f62c685 164 }
20fe2104
DG
165 return ret;
166}
167
168/*
050349bb
DG
169 * Create a kernel channel, register it to the kernel tracer and add it to the
170 * kernel session.
20fe2104 171 */
050349bb 172int kernel_create_channel(struct ltt_kernel_session *session,
fdd9eb17 173 struct lttng_channel *chan)
20fe2104
DG
174{
175 int ret;
176 struct ltt_kernel_channel *lkc;
20fe2104 177
0525e9ae
DG
178 assert(session);
179 assert(chan);
0525e9ae 180
54012638 181 /* Allocate kernel channel */
fdd9eb17 182 lkc = trace_kernel_create_channel(chan);
54012638 183 if (lkc == NULL) {
20fe2104
DG
184 goto error;
185 }
186
ecc48a90 187 DBG3("Kernel create channel %s with attr: %d, %" PRIu64 ", %" PRIu64 ", %u, %u, %d, %d",
fdd9eb17 188 chan->name, lkc->channel->attr.overwrite,
173af62f
DG
189 lkc->channel->attr.subbuf_size, lkc->channel->attr.num_subbuf,
190 lkc->channel->attr.switch_timer_interval, lkc->channel->attr.read_timer_interval,
ecc48a90 191 lkc->channel->attr.live_timer_interval, lkc->channel->attr.output);
173af62f 192
54012638 193 /* Kernel tracer channel creation */
f3ed775e 194 ret = kernctl_create_channel(session->fd, &lkc->channel->attr);
20fe2104 195 if (ret < 0) {
df0f840b 196 PERROR("ioctl kernel create channel");
20fe2104
DG
197 goto error;
198 }
199
54012638 200 /* Setup the channel fd */
20fe2104 201 lkc->fd = ret;
7b395890
DG
202 /* Prevent fd duplication after execlp() */
203 ret = fcntl(lkc->fd, F_SETFD, FD_CLOEXEC);
204 if (ret < 0) {
df0f840b 205 PERROR("fcntl session fd");
7b395890
DG
206 }
207
54012638 208 /* Add channel to session */
8c0faa1d
DG
209 cds_list_add(&lkc->list, &session->channel_list.head);
210 session->channel_count++;
fb5f35b6 211 lkc->session = session;
e1f3997a 212 lkc->key = ++next_kernel_channel_key;
20fe2104 213
e1f3997a
JD
214 DBG("Kernel channel %s created (fd: %d, key: %" PRIu64 ")",
215 lkc->channel->name, lkc->fd, lkc->key);
20fe2104
DG
216
217 return 0;
218
219error:
5f62c685
DG
220 if (lkc) {
221 free(lkc->channel);
222 free(lkc);
223 }
54012638 224 return -1;
20fe2104 225}
f34daff7 226
410b78a0
FD
227/*
228 * Compute the offset of the instrumentation byte in the binary based on the
229 * function probe location using the ELF lookup method.
230 *
231 * Returns 0 on success and set the offset out parameter to the offset of the
232 * elf symbol
233 * Returns -1 on error
234 */
235static
236int extract_userspace_probe_offset_function_elf(
87597c2c 237 const struct lttng_userspace_probe_location *probe_location,
410b78a0
FD
238 struct ltt_kernel_session *session, uint64_t *offset)
239{
240 int fd;
241 int ret = 0;
242 const char *symbol = NULL;
87597c2c 243 const struct lttng_userspace_probe_location_lookup_method *lookup = NULL;
410b78a0
FD
244 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type;
245
410b78a0
FD
246 assert(lttng_userspace_probe_location_get_type(probe_location) ==
247 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION);
248
249 lookup = lttng_userspace_probe_location_get_lookup_method(
250 probe_location);
251 if (!lookup) {
252 ret = -1;
253 goto end;
254 }
255
256 lookup_method_type =
257 lttng_userspace_probe_location_lookup_method_get_type(lookup);
258
259 assert(lookup_method_type ==
260 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF);
261
262 symbol = lttng_userspace_probe_location_function_get_function_name(
263 probe_location);
264 if (!symbol) {
265 ret = -1;
266 goto end;
267 }
268
269 fd = lttng_userspace_probe_location_function_get_binary_fd(probe_location);
270 if (fd < 0) {
271 ret = -1;
272 goto end;
273 }
274
275 ret = run_as_extract_elf_symbol_offset(fd, symbol, session->uid,
276 session->gid, offset);
277 if (ret < 0) {
278 DBG("userspace probe offset calculation failed for "
279 "function %s", symbol);
280 goto end;
281 }
282
283 DBG("userspace probe elf offset for %s is 0x%jd", symbol, (intmax_t)(*offset));
284end:
285 return ret;
286}
287
288/*
289 * Compute the offsets of the instrumentation bytes in the binary based on the
290 * tracepoint probe location using the SDT lookup method. This function
291 * allocates the offsets buffer, the caller must free it.
292 *
293 * Returns 0 on success and set the offset out parameter to the offsets of the
294 * SDT tracepoint.
295 * Returns -1 on error.
296 */
297static
298int extract_userspace_probe_offset_tracepoint_sdt(
87597c2c 299 const struct lttng_userspace_probe_location *probe_location,
410b78a0
FD
300 struct ltt_kernel_session *session, uint64_t **offsets,
301 uint32_t *offsets_count)
302{
303 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type;
87597c2c 304 const struct lttng_userspace_probe_location_lookup_method *lookup = NULL;
410b78a0
FD
305 const char *probe_name = NULL, *provider_name = NULL;
306 int ret = 0;
307 int fd, i;
308
309 assert(lttng_userspace_probe_location_get_type(probe_location) ==
310 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT);
311
312 lookup = lttng_userspace_probe_location_get_lookup_method(probe_location);
313 if (!lookup) {
314 ret = -1;
315 goto end;
316 }
317
318 lookup_method_type =
319 lttng_userspace_probe_location_lookup_method_get_type(lookup);
320
321 assert(lookup_method_type ==
322 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT);
323
324
325 probe_name = lttng_userspace_probe_location_tracepoint_get_probe_name(
326 probe_location);
327 if (!probe_name) {
328 ret = -1;
329 goto end;
330 }
331
332 provider_name = lttng_userspace_probe_location_tracepoint_get_provider_name(
333 probe_location);
334 if (!provider_name) {
335 ret = -1;
336 goto end;
337 }
338
339 fd = lttng_userspace_probe_location_tracepoint_get_binary_fd(probe_location);
340 if (fd < 0) {
341 ret = -1;
342 goto end;
343 }
344
345 ret = run_as_extract_sdt_probe_offsets(fd, provider_name, probe_name,
346 session->uid, session->gid, offsets, offsets_count);
347 if (ret < 0) {
348 DBG("userspace probe offset calculation failed for sdt "
349 "probe %s:%s", provider_name, probe_name);
350 goto end;
351 }
352
353 if (*offsets_count == 0) {
354 DBG("no userspace probe offset found");
355 goto end;
356 }
357
358 DBG("%u userspace probe SDT offsets found for %s:%s at:",
359 *offsets_count, provider_name, probe_name);
360 for (i = 0; i < *offsets_count; i++) {
361 DBG("\t0x%jd", (intmax_t)((*offsets)[i]));
362 }
363end:
364 return ret;
365}
366
367/*
368 * Extract the offsets of the instrumentation point for the different lookup
369 * methods.
370 */
371static
372int userspace_probe_add_callsites(struct lttng_event *ev,
373 struct ltt_kernel_session *session, int fd)
374{
87597c2c 375 const struct lttng_userspace_probe_location_lookup_method *lookup_method = NULL;
410b78a0 376 enum lttng_userspace_probe_location_lookup_method_type type;
87597c2c 377 const struct lttng_userspace_probe_location *location = NULL;
410b78a0
FD
378 int ret;
379
380 assert(ev);
381 assert(ev->type == LTTNG_EVENT_USERSPACE_PROBE);
382
383 location = lttng_event_get_userspace_probe_location(ev);
384 if (!location) {
385 ret = -1;
386 goto end;
387 }
388 lookup_method =
389 lttng_userspace_probe_location_get_lookup_method(location);
390 if (!lookup_method) {
391 ret = -1;
392 goto end;
393 }
394
395 type = lttng_userspace_probe_location_lookup_method_get_type(lookup_method);
396 switch (type) {
397 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF:
398 {
399 struct lttng_kernel_event_callsite callsite;
400 uint64_t offset;
401
402 ret = extract_userspace_probe_offset_function_elf(location, session, &offset);
403 if (ret) {
404 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
405 goto end;
406 }
407
408 callsite.u.uprobe.offset = offset;
409 ret = kernctl_add_callsite(fd, &callsite);
410 if (ret) {
411 WARN("Adding callsite to userspace probe "
412 "event %s failed.", ev->name);
413 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
414 goto end;
415 }
416 break;
417 }
418 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT:
419 {
420 int i;
421 uint64_t *offsets = NULL;
422 uint32_t offsets_count;
423 struct lttng_kernel_event_callsite callsite;
424
425 /*
426 * This call allocates the offsets buffer. This buffer must be freed
427 * by the caller
428 */
429 ret = extract_userspace_probe_offset_tracepoint_sdt(location, session,
430 &offsets, &offsets_count);
431 if (ret) {
432 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
433 goto end;
434 }
435 for (i = 0; i < offsets_count; i++) {
436 callsite.u.uprobe.offset = offsets[i];
437 ret = kernctl_add_callsite(fd, &callsite);
438 if (ret) {
439 WARN("Adding callsite to userspace probe "
440 "event %s failed.", ev->name);
441 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
442 free(offsets);
443 goto end;
444 }
445 }
446 free(offsets);
447 break;
448 }
449 default:
450 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
451 goto end;
452 }
453end:
454 return ret;
455}
456
f34daff7 457/*
050349bb
DG
458 * Create a kernel event, enable it to the kernel tracer and add it to the
459 * channel event list of the kernel session.
49d21f93 460 * We own filter_expression and filter.
f34daff7 461 */
050349bb 462int kernel_create_event(struct lttng_event *ev,
00a62084
MD
463 struct ltt_kernel_channel *channel,
464 char *filter_expression,
465 struct lttng_filter_bytecode *filter)
f34daff7 466{
71a3bb01
FD
467 int err, fd;
468 enum lttng_error_code ret;
f34daff7 469 struct ltt_kernel_event *event;
f34daff7 470
0525e9ae
DG
471 assert(ev);
472 assert(channel);
473
a969e101 474 /* We pass ownership of filter_expression and filter */
71a3bb01
FD
475 ret = trace_kernel_create_event(ev, filter_expression,
476 filter, &event);
477 if (ret != LTTNG_OK) {
f34daff7
DG
478 goto error;
479 }
480
71a3bb01
FD
481 fd = kernctl_create_event(channel->fd, event->event);
482 if (fd < 0) {
483 switch (-fd) {
bd29c13d 484 case EEXIST:
71a3bb01 485 ret = LTTNG_ERR_KERN_EVENT_EXIST;
bd29c13d
DG
486 break;
487 case ENOSYS:
488 WARN("Event type not implemented");
71a3bb01 489 ret = LTTNG_ERR_KERN_EVENT_ENOSYS;
bd29c13d 490 break;
8197a339
DG
491 case ENOENT:
492 WARN("Event %s not found!", ev->name);
71a3bb01 493 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
8197a339 494 break;
bd29c13d 495 default:
71a3bb01 496 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
d87bfb32
DG
497 PERROR("create event ioctl");
498 }
e953ef25 499 goto free_event;
8c0faa1d 500 }
f34daff7 501
d0ae4ea8 502 event->type = ev->type;
71a3bb01 503 event->fd = fd;
7b395890 504 /* Prevent fd duplication after execlp() */
71a3bb01
FD
505 err = fcntl(event->fd, F_SETFD, FD_CLOEXEC);
506 if (err < 0) {
df0f840b 507 PERROR("fcntl session fd");
7b395890
DG
508 }
509
00a62084 510 if (filter) {
71a3bb01
FD
511 err = kernctl_filter(event->fd, filter);
512 if (err < 0) {
513 switch (-err) {
514 case ENOMEM:
515 ret = LTTNG_ERR_FILTER_NOMEM;
516 break;
517 default:
518 ret = LTTNG_ERR_FILTER_INVAL;
519 break;
520 }
00a62084
MD
521 goto filter_error;
522 }
523 }
524
dcabc190
FD
525 if (ev->type == LTTNG_EVENT_USERSPACE_PROBE) {
526 ret = userspace_probe_add_callsites(ev, channel->session, event->fd);
527 if (ret) {
528 goto add_callsite_error;
529 }
530 }
531
71a3bb01
FD
532 err = kernctl_enable(event->fd);
533 if (err < 0) {
534 switch (-err) {
00a62084
MD
535 case EEXIST:
536 ret = LTTNG_ERR_KERN_EVENT_EXIST;
537 break;
538 default:
539 PERROR("enable kernel event");
71a3bb01 540 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
00a62084
MD
541 break;
542 }
543 goto enable_error;
544 }
545
f3ed775e
DG
546 /* Add event to event list */
547 cds_list_add(&event->list, &channel->events_list.head);
cbbbb275
DG
548 channel->event_count++;
549
e953ef25
DG
550 DBG("Event %s created (fd: %d)", ev->name, event->fd);
551
552 return 0;
553
dcabc190 554add_callsite_error:
00a62084
MD
555enable_error:
556filter_error:
557 {
558 int closeret;
559
560 closeret = close(event->fd);
561 if (closeret) {
562 PERROR("close event fd");
563 }
564 }
e953ef25
DG
565free_event:
566 free(event);
567error:
d87bfb32 568 return ret;
e953ef25
DG
569}
570
26cc6b4e 571/*
050349bb 572 * Disable a kernel channel.
26cc6b4e
DG
573 */
574int kernel_disable_channel(struct ltt_kernel_channel *chan)
575{
576 int ret;
577
0525e9ae
DG
578 assert(chan);
579
26cc6b4e
DG
580 ret = kernctl_disable(chan->fd);
581 if (ret < 0) {
df0f840b 582 PERROR("disable chan ioctl");
26cc6b4e
DG
583 goto error;
584 }
585
586 chan->enabled = 0;
e1f3997a
JD
587 DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64 ")",
588 chan->channel->name, chan->fd, chan->key);
26cc6b4e
DG
589
590 return 0;
591
592error:
593 return ret;
594}
595
d36b8583 596/*
050349bb 597 * Enable a kernel channel.
d36b8583
DG
598 */
599int kernel_enable_channel(struct ltt_kernel_channel *chan)
600{
601 int ret;
602
0525e9ae
DG
603 assert(chan);
604
d36b8583 605 ret = kernctl_enable(chan->fd);
32af2c95 606 if (ret < 0 && ret != -EEXIST) {
df0f840b 607 PERROR("Enable kernel chan");
d36b8583
DG
608 goto error;
609 }
610
611 chan->enabled = 1;
e1f3997a
JD
612 DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64 ")",
613 chan->channel->name, chan->fd, chan->key);
d36b8583
DG
614
615 return 0;
616
617error:
618 return ret;
619}
620
19e70852 621/*
050349bb 622 * Enable a kernel event.
19e70852
DG
623 */
624int kernel_enable_event(struct ltt_kernel_event *event)
625{
626 int ret;
627
0525e9ae
DG
628 assert(event);
629
19e70852 630 ret = kernctl_enable(event->fd);
42224349 631 if (ret < 0) {
32af2c95 632 switch (-ret) {
42224349 633 case EEXIST:
f73fabfd 634 ret = LTTNG_ERR_KERN_EVENT_EXIST;
42224349
DG
635 break;
636 default:
637 PERROR("enable kernel event");
638 break;
639 }
19e70852
DG
640 goto error;
641 }
642
643 event->enabled = 1;
644 DBG("Kernel event %s enabled (fd: %d)", event->event->name, event->fd);
645
646 return 0;
647
648error:
d36b8583 649 return ret;
19e70852
DG
650}
651
e953ef25 652/*
050349bb 653 * Disable a kernel event.
e953ef25 654 */
19e70852 655int kernel_disable_event(struct ltt_kernel_event *event)
e953ef25
DG
656{
657 int ret;
19e70852 658
0525e9ae
DG
659 assert(event);
660
19e70852 661 ret = kernctl_disable(event->fd);
42224349 662 if (ret < 0) {
32af2c95 663 switch (-ret) {
42224349 664 case EEXIST:
f73fabfd 665 ret = LTTNG_ERR_KERN_EVENT_EXIST;
42224349
DG
666 break;
667 default:
668 PERROR("disable kernel event");
669 break;
670 }
19e70852 671 goto error;
e953ef25 672 }
f3ed775e 673
19e70852
DG
674 event->enabled = 0;
675 DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd);
676
f34daff7
DG
677 return 0;
678
679error:
d36b8583 680 return ret;
f34daff7 681}
aaf26714 682
6e911cad 683
ccf10263
MD
684int kernel_track_pid(struct ltt_kernel_session *session, int pid)
685{
7c493d31
MD
686 int ret;
687
ccf10263
MD
688 DBG("Kernel track PID %d for session id %" PRIu64 ".",
689 pid, session->id);
7c493d31
MD
690 ret = kernctl_track_pid(session->fd, pid);
691 if (!ret) {
692 return LTTNG_OK;
693 }
32af2c95 694 switch (-ret) {
7c493d31
MD
695 case EINVAL:
696 return LTTNG_ERR_INVALID;
697 case ENOMEM:
698 return LTTNG_ERR_NOMEM;
699 case EEXIST:
700 return LTTNG_ERR_PID_TRACKED;
701 default:
702 return LTTNG_ERR_UNK;
703 }
ccf10263
MD
704}
705
706int kernel_untrack_pid(struct ltt_kernel_session *session, int pid)
707{
7c493d31
MD
708 int ret;
709
ccf10263
MD
710 DBG("Kernel untrack PID %d for session id %" PRIu64 ".",
711 pid, session->id);
7c493d31
MD
712 ret = kernctl_untrack_pid(session->fd, pid);
713 if (!ret) {
714 return LTTNG_OK;
715 }
32af2c95 716 switch (-ret) {
7c493d31
MD
717 case EINVAL:
718 return LTTNG_ERR_INVALID;
719 case ENOMEM:
720 return LTTNG_ERR_NOMEM;
721 case ENOENT:
722 return LTTNG_ERR_PID_NOT_TRACKED;
723 default:
724 return LTTNG_ERR_UNK;
725 }
ccf10263
MD
726}
727
a5dfbb9d
MD
728ssize_t kernel_list_tracker_pids(struct ltt_kernel_session *session,
729 int **_pids)
730{
731 int fd, ret;
732 int pid;
733 ssize_t nbmem, count = 0;
734 FILE *fp;
735 int *pids;
736
737 fd = kernctl_list_tracker_pids(session->fd);
738 if (fd < 0) {
739 PERROR("kernel tracker pids list");
740 goto error;
741 }
742
743 fp = fdopen(fd, "r");
744 if (fp == NULL) {
745 PERROR("kernel tracker pids list fdopen");
746 goto error_fp;
747 }
748
749 nbmem = KERNEL_TRACKER_PIDS_INIT_LIST_SIZE;
750 pids = zmalloc(sizeof(*pids) * nbmem);
751 if (pids == NULL) {
752 PERROR("alloc list pids");
753 count = -ENOMEM;
754 goto end;
755 }
756
757 while (fscanf(fp, "process { pid = %u; };\n", &pid) == 1) {
758 if (count >= nbmem) {
759 int *new_pids;
760 size_t new_nbmem;
761
762 new_nbmem = nbmem << 1;
763 DBG("Reallocating pids list from %zu to %zu entries",
764 nbmem, new_nbmem);
765 new_pids = realloc(pids, new_nbmem * sizeof(*new_pids));
766 if (new_pids == NULL) {
767 PERROR("realloc list events");
768 free(pids);
769 count = -ENOMEM;
770 goto end;
771 }
772 /* Zero the new memory */
773 memset(new_pids + nbmem, 0,
774 (new_nbmem - nbmem) * sizeof(*new_pids));
775 nbmem = new_nbmem;
776 pids = new_pids;
777 }
778 pids[count++] = pid;
779 }
780
781 *_pids = pids;
782 DBG("Kernel list tracker pids done (%zd pids)", count);
783end:
784 ret = fclose(fp); /* closes both fp and fd */
785 if (ret) {
786 PERROR("fclose");
787 }
788 return count;
789
790error_fp:
791 ret = close(fd);
792 if (ret) {
793 PERROR("close");
794 }
795error:
796 return -1;
797}
798
aaf26714 799/*
050349bb
DG
800 * Create kernel metadata, open from the kernel tracer and add it to the
801 * kernel session.
aaf26714 802 */
a4b92340 803int kernel_open_metadata(struct ltt_kernel_session *session)
aaf26714
DG
804{
805 int ret;
74024a21 806 struct ltt_kernel_metadata *lkm = NULL;
aaf26714 807
0525e9ae
DG
808 assert(session);
809
54012638 810 /* Allocate kernel metadata */
a4b92340 811 lkm = trace_kernel_create_metadata();
54012638 812 if (lkm == NULL) {
aaf26714
DG
813 goto error;
814 }
815
54012638 816 /* Kernel tracer metadata creation */
f3ed775e 817 ret = kernctl_open_metadata(session->fd, &lkm->conf->attr);
aaf26714 818 if (ret < 0) {
74024a21 819 goto error_open;
aaf26714
DG
820 }
821
8c0faa1d 822 lkm->fd = ret;
d40f0359 823 lkm->key = ++next_kernel_channel_key;
7b395890
DG
824 /* Prevent fd duplication after execlp() */
825 ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC);
826 if (ret < 0) {
df0f840b 827 PERROR("fcntl session fd");
7b395890
DG
828 }
829
aaf26714 830 session->metadata = lkm;
8c0faa1d 831
00e2e675 832 DBG("Kernel metadata opened (fd: %d)", lkm->fd);
8c0faa1d
DG
833
834 return 0;
835
74024a21
DG
836error_open:
837 trace_kernel_destroy_metadata(lkm);
8c0faa1d 838error:
54012638 839 return -1;
8c0faa1d
DG
840}
841
842/*
050349bb 843 * Start tracing session.
8c0faa1d
DG
844 */
845int kernel_start_session(struct ltt_kernel_session *session)
846{
847 int ret;
848
0525e9ae
DG
849 assert(session);
850
8c0faa1d
DG
851 ret = kernctl_start_session(session->fd);
852 if (ret < 0) {
df0f840b 853 PERROR("ioctl start session");
8c0faa1d
DG
854 goto error;
855 }
856
857 DBG("Kernel session started");
858
859 return 0;
860
861error:
862 return ret;
863}
864
f3ed775e 865/*
050349bb 866 * Make a kernel wait to make sure in-flight probe have completed.
f3ed775e 867 */
32e10baa 868void kernel_wait_quiescent(void)
f3ed775e
DG
869{
870 int ret;
32e10baa 871 int fd = kernel_tracer_fd;
f3ed775e
DG
872
873 DBG("Kernel quiescent wait on %d", fd);
874
875 ret = kernctl_wait_quiescent(fd);
876 if (ret < 0) {
df0f840b 877 PERROR("wait quiescent ioctl");
f3ed775e
DG
878 ERR("Kernel quiescent wait failed");
879 }
880}
881
882/*
f3ed775e
DG
883 * Force flush buffer of metadata.
884 */
885int kernel_metadata_flush_buffer(int fd)
886{
887 int ret;
888
169d2cb7
DG
889 DBG("Kernel flushing metadata buffer on fd %d", fd);
890
f3ed775e
DG
891 ret = kernctl_buffer_flush(fd);
892 if (ret < 0) {
00e2e675 893 ERR("Fail to flush metadata buffers %d (ret: %d)", fd, ret);
f3ed775e
DG
894 }
895
896 return 0;
897}
898
899/*
050349bb 900 * Force flush buffer for channel.
f3ed775e
DG
901 */
902int kernel_flush_buffer(struct ltt_kernel_channel *channel)
903{
904 int ret;
905 struct ltt_kernel_stream *stream;
906
0525e9ae
DG
907 assert(channel);
908
f3ed775e
DG
909 DBG("Flush buffer for channel %s", channel->channel->name);
910
911 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
912 DBG("Flushing channel stream %d", stream->fd);
913 ret = kernctl_buffer_flush(stream->fd);
914 if (ret < 0) {
df0f840b 915 PERROR("ioctl");
f3ed775e
DG
916 ERR("Fail to flush buffer for stream %d (ret: %d)",
917 stream->fd, ret);
918 }
919 }
920
921 return 0;
922}
923
8c0faa1d 924/*
050349bb 925 * Stop tracing session.
8c0faa1d
DG
926 */
927int kernel_stop_session(struct ltt_kernel_session *session)
928{
929 int ret;
930
0525e9ae
DG
931 assert(session);
932
8c0faa1d
DG
933 ret = kernctl_stop_session(session->fd);
934 if (ret < 0) {
935 goto error;
936 }
937
938 DBG("Kernel session stopped");
939
940 return 0;
941
942error:
943 return ret;
944}
945
946/*
050349bb
DG
947 * Open stream of channel, register it to the kernel tracer and add it
948 * to the stream list of the channel.
8c0faa1d 949 *
1cfb4b98
MD
950 * Note: given that the streams may appear in random order wrt CPU
951 * number (e.g. cpu hotplug), the index value of the stream number in
952 * the stream name is not necessarily linked to the CPU number.
953 *
050349bb 954 * Return the number of created stream. Else, a negative value.
8c0faa1d 955 */
f3ed775e 956int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
8c0faa1d 957{
1cfb4b98 958 int ret;
8c0faa1d
DG
959 struct ltt_kernel_stream *lks;
960
0525e9ae
DG
961 assert(channel);
962
5a47c6a2 963 while ((ret = kernctl_create_stream(channel->fd)) >= 0) {
1cfb4b98
MD
964 lks = trace_kernel_create_stream(channel->channel->name,
965 channel->stream_count);
8c0faa1d 966 if (lks == NULL) {
799e2c4f
MD
967 ret = close(ret);
968 if (ret) {
969 PERROR("close");
970 }
8c0faa1d
DG
971 goto error;
972 }
973
974 lks->fd = ret;
7b395890
DG
975 /* Prevent fd duplication after execlp() */
976 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
977 if (ret < 0) {
df0f840b 978 PERROR("fcntl session fd");
7b395890
DG
979 }
980
1624d5b7
JD
981 lks->tracefile_size = channel->channel->attr.tracefile_size;
982 lks->tracefile_count = channel->channel->attr.tracefile_count;
983
1cfb4b98 984 /* Add stream to channel stream list */
8c0faa1d
DG
985 cds_list_add(&lks->list, &channel->stream_list.head);
986 channel->stream_count++;
8c0faa1d 987
00e2e675
DG
988 DBG("Kernel stream %s created (fd: %d, state: %d)", lks->name, lks->fd,
989 lks->state);
54012638 990 }
8c0faa1d
DG
991
992 return channel->stream_count;
993
994error:
54012638 995 return -1;
8c0faa1d
DG
996}
997
998/*
050349bb 999 * Open the metadata stream and set it to the kernel session.
8c0faa1d 1000 */
f3ed775e 1001int kernel_open_metadata_stream(struct ltt_kernel_session *session)
8c0faa1d
DG
1002{
1003 int ret;
1004
0525e9ae
DG
1005 assert(session);
1006
8c0faa1d
DG
1007 ret = kernctl_create_stream(session->metadata->fd);
1008 if (ret < 0) {
df0f840b 1009 PERROR("kernel create metadata stream");
8c0faa1d
DG
1010 goto error;
1011 }
1012
1013 DBG("Kernel metadata stream created (fd: %d)", ret);
1014 session->metadata_stream_fd = ret;
7b395890
DG
1015 /* Prevent fd duplication after execlp() */
1016 ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
1017 if (ret < 0) {
df0f840b 1018 PERROR("fcntl session fd");
7b395890 1019 }
aaf26714
DG
1020
1021 return 0;
1022
1023error:
54012638 1024 return -1;
aaf26714 1025}
2ef84c95
DG
1026
1027/*
9f19cc17 1028 * Get the event list from the kernel tracer and return the number of elements.
2ef84c95 1029 */
32e10baa 1030ssize_t kernel_list_events(struct lttng_event **events)
2ef84c95 1031{
53efb85a 1032 int fd, ret;
9f19cc17
DG
1033 char *event;
1034 size_t nbmem, count = 0;
2ef84c95 1035 FILE *fp;
9f19cc17 1036 struct lttng_event *elist;
2ef84c95 1037
0525e9ae
DG
1038 assert(events);
1039
32e10baa 1040 fd = kernctl_tracepoint_list(kernel_tracer_fd);
2ef84c95 1041 if (fd < 0) {
df0f840b 1042 PERROR("kernel tracepoint list");
2ef84c95
DG
1043 goto error;
1044 }
1045
1046 fp = fdopen(fd, "r");
1047 if (fp == NULL) {
df0f840b 1048 PERROR("kernel tracepoint list fdopen");
61b73b12 1049 goto error_fp;
2ef84c95
DG
1050 }
1051
1052 /*
1053 * Init memory size counter
1054 * See kernel-ctl.h for explanation of this value
1055 */
6725fe19 1056 nbmem = KERNEL_EVENT_INIT_LIST_SIZE;
ba7f0ae5 1057 elist = zmalloc(sizeof(struct lttng_event) * nbmem);
3b870559
MD
1058 if (elist == NULL) {
1059 PERROR("alloc list events");
1060 count = -ENOMEM;
1061 goto end;
1062 }
2ef84c95 1063
53efb85a 1064 while (fscanf(fp, "event { name = %m[^;]; };\n", &event) == 1) {
6725fe19 1065 if (count >= nbmem) {
3b870559 1066 struct lttng_event *new_elist;
53efb85a 1067 size_t new_nbmem;
3b870559 1068
53efb85a
MD
1069 new_nbmem = nbmem << 1;
1070 DBG("Reallocating event list from %zu to %zu bytes",
1071 nbmem, new_nbmem);
1072 new_elist = realloc(elist, new_nbmem * sizeof(struct lttng_event));
3b870559 1073 if (new_elist == NULL) {
df0f840b 1074 PERROR("realloc list events");
3b870559
MD
1075 free(event);
1076 free(elist);
61b73b12
MD
1077 count = -ENOMEM;
1078 goto end;
2ef84c95 1079 }
53efb85a
MD
1080 /* Zero the new memory */
1081 memset(new_elist + nbmem, 0,
1082 (new_nbmem - nbmem) * sizeof(struct lttng_event));
1083 nbmem = new_nbmem;
3b870559 1084 elist = new_elist;
2ef84c95 1085 }
99497cd0
MD
1086 strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN);
1087 elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
67b9d018 1088 elist[count].enabled = -1;
9f19cc17 1089 count++;
3b870559 1090 free(event);
2ef84c95
DG
1091 }
1092
9f19cc17 1093 *events = elist;
ced2f820 1094 DBG("Kernel list events done (%zu events)", count);
61b73b12 1095end:
799e2c4f
MD
1096 ret = fclose(fp); /* closes both fp and fd */
1097 if (ret) {
1098 PERROR("fclose");
1099 }
9f19cc17 1100 return count;
2ef84c95 1101
61b73b12 1102error_fp:
799e2c4f
MD
1103 ret = close(fd);
1104 if (ret) {
1105 PERROR("close");
1106 }
2ef84c95
DG
1107error:
1108 return -1;
1109}
096102bd
DG
1110
1111/*
1112 * Get kernel version and validate it.
1113 */
32e10baa 1114int kernel_validate_version(struct lttng_kernel_tracer_version *version,
88076e89 1115 struct lttng_kernel_tracer_abi_version *abi_version)
096102bd
DG
1116{
1117 int ret;
096102bd 1118
32e10baa 1119 ret = kernctl_tracer_version(kernel_tracer_fd, version);
096102bd 1120 if (ret < 0) {
521dd134 1121 ERR("Failed to retrieve the lttng-modules version");
096102bd
DG
1122 goto error;
1123 }
1124
1125 /* Validate version */
88076e89 1126 if (version->major != VERSION_MAJOR) {
c052142c 1127 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
88076e89 1128 version->major, VERSION_MAJOR);
096102bd 1129 goto error_version;
096102bd 1130 }
32e10baa 1131 ret = kernctl_tracer_abi_version(kernel_tracer_fd, abi_version);
c052142c 1132 if (ret < 0) {
521dd134 1133 ERR("Failed to retrieve lttng-modules ABI version");
c052142c
MD
1134 goto error;
1135 }
88076e89 1136 if (abi_version->major != LTTNG_MODULES_ABI_MAJOR_VERSION) {
521dd134 1137 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
88076e89 1138 abi_version->major, abi_version->minor,
c052142c
MD
1139 LTTNG_MODULES_ABI_MAJOR_VERSION);
1140 goto error;
1141 }
1142 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
88076e89
JD
1143 version->major, version->minor,
1144 abi_version->major, abi_version->minor);
096102bd
DG
1145 return 0;
1146
1147error_version:
096102bd
DG
1148 ret = -1;
1149
1150error:
521dd134 1151 ERR("Kernel tracer version check failed; kernel tracing will not be available");
096102bd
DG
1152 return ret;
1153}
335a95b7
MD
1154
1155/*
1156 * Kernel work-arounds called at the start of sessiond main().
1157 */
1158int init_kernel_workarounds(void)
1159{
8936c33a 1160 int ret;
335a95b7
MD
1161 FILE *fp;
1162
1163 /*
1164 * boot_id needs to be read once before being used concurrently
1165 * to deal with a Linux kernel race. A fix is proposed for
1166 * upstream, but the work-around is needed for older kernels.
1167 */
1168 fp = fopen("/proc/sys/kernel/random/boot_id", "r");
1169 if (!fp) {
1170 goto end_boot_id;
1171 }
1172 while (!feof(fp)) {
1173 char buf[37] = "";
1174
8936c33a
DG
1175 ret = fread(buf, 1, sizeof(buf), fp);
1176 if (ret < 0) {
1177 /* Ignore error, we don't really care */
1178 }
335a95b7 1179 }
799e2c4f
MD
1180 ret = fclose(fp);
1181 if (ret) {
1182 PERROR("fclose");
1183 }
335a95b7 1184end_boot_id:
335a95b7
MD
1185 return 0;
1186}
2f77fc4b
DG
1187
1188/*
b1f4d3d9 1189 * Teardown of a kernel session, keeping data required by destroy notifiers.
2f77fc4b
DG
1190 */
1191void kernel_destroy_session(struct ltt_kernel_session *ksess)
1192{
698fb2ed
JG
1193 struct lttng_trace_chunk *trace_chunk;
1194
2f77fc4b
DG
1195 if (ksess == NULL) {
1196 DBG3("No kernel session when tearing down session");
1197 return;
1198 }
1199
1200 DBG("Tearing down kernel session");
698fb2ed 1201 trace_chunk = ksess->current_trace_chunk;
2f77fc4b 1202
07b86b52 1203 /*
15dc512a
DG
1204 * Destroy channels on the consumer if at least one FD has been sent and we
1205 * are in no output mode because the streams are in *no* monitor mode so we
1206 * have to send a command to clean them up or else they leaked.
07b86b52 1207 */
15dc512a 1208 if (!ksess->output_traces && ksess->consumer_fds_sent) {
07b86b52
JD
1209 int ret;
1210 struct consumer_socket *socket;
1211 struct lttng_ht_iter iter;
1212
1213 /* For each consumer socket. */
d069d577 1214 rcu_read_lock();
07b86b52
JD
1215 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
1216 socket, node.node) {
1217 struct ltt_kernel_channel *chan;
1218
1219 /* For each channel, ask the consumer to destroy it. */
1220 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
1221 ret = kernel_consumer_destroy_channel(socket, chan);
1222 if (ret < 0) {
1223 /* Consumer is probably dead. Use next socket. */
1224 continue;
1225 }
1226 }
1227 }
d069d577 1228 rcu_read_unlock();
07b86b52
JD
1229 }
1230
2f77fc4b
DG
1231 /* Close any relayd session */
1232 consumer_output_send_destroy_relayd(ksess->consumer);
1233
1234 trace_kernel_destroy_session(ksess);
698fb2ed 1235 lttng_trace_chunk_put(trace_chunk);
2f77fc4b 1236}
fb5f35b6 1237
b1f4d3d9
MD
1238/* Teardown of data required by destroy notifiers. */
1239void kernel_free_session(struct ltt_kernel_session *ksess)
1240{
1241 if (ksess == NULL) {
1242 return;
1243 }
1244 trace_kernel_free_session(ksess);
1245}
1246
fb5f35b6
DG
1247/*
1248 * Destroy a kernel channel object. It does not do anything on the tracer side.
1249 */
1250void kernel_destroy_channel(struct ltt_kernel_channel *kchan)
1251{
1252 struct ltt_kernel_session *ksess = NULL;
1253
1254 assert(kchan);
1255 assert(kchan->channel);
1256
1257 DBG3("Kernel destroy channel %s", kchan->channel->name);
1258
1259 /* Update channel count of associated session. */
1260 if (kchan->session) {
1261 /* Keep pointer reference so we can update it after the destroy. */
1262 ksess = kchan->session;
1263 }
1264
1265 trace_kernel_destroy_channel(kchan);
1266
1267 /*
1268 * At this point the kernel channel is not visible anymore. This is safe
1269 * since in order to work on a visible kernel session, the tracing session
1270 * lock (ltt_session.lock) MUST be acquired.
1271 */
1272 if (ksess) {
1273 ksess->channel_count--;
1274 }
1275}
6dc3064a
DG
1276
1277/*
1278 * Take a snapshot for a given kernel session.
1279 *
8f07cd01 1280 * Return LTTNG_OK on success or else return a LTTNG_ERR code.
6dc3064a 1281 */
65ff8ea3
JG
1282enum lttng_error_code kernel_snapshot_record(
1283 struct ltt_kernel_session *ksess,
61ace1d3 1284 const struct consumer_output *output, int wait,
d07ceecd 1285 uint64_t nb_packets_per_stream)
6dc3064a 1286{
2a06df8d 1287 int err, ret, saved_metadata_fd;
8f07cd01 1288 enum lttng_error_code status = LTTNG_OK;
6dc3064a
DG
1289 struct consumer_socket *socket;
1290 struct lttng_ht_iter iter;
1291 struct ltt_kernel_metadata *saved_metadata;
1819b04a 1292 char *trace_path = NULL;
6dc3064a
DG
1293
1294 assert(ksess);
1295 assert(ksess->consumer);
1296 assert(output);
1297
1298 DBG("Kernel snapshot record started");
1299
1300 /* Save current metadata since the following calls will change it. */
1301 saved_metadata = ksess->metadata;
1302 saved_metadata_fd = ksess->metadata_stream_fd;
1303
1304 rcu_read_lock();
1305
1306 ret = kernel_open_metadata(ksess);
1307 if (ret < 0) {
8f07cd01 1308 status = LTTNG_ERR_KERN_META_FAIL;
6dc3064a
DG
1309 goto error;
1310 }
1311
1312 ret = kernel_open_metadata_stream(ksess);
1313 if (ret < 0) {
8f07cd01 1314 status = LTTNG_ERR_KERN_META_FAIL;
6dc3064a
DG
1315 goto error_open_stream;
1316 }
1317
1819b04a
MD
1318 trace_path = setup_channel_trace_path(ksess->consumer,
1319 DEFAULT_KERNEL_TRACE_DIR);
1320 if (!trace_path) {
1321 status = LTTNG_ERR_INVALID;
1322 goto error;
1323 }
6dc3064a 1324 /* Send metadata to consumer and snapshot everything. */
61ace1d3 1325 cds_lfht_for_each_entry(output->socks->ht, &iter.iter,
6dc3064a 1326 socket, node.node) {
6dc3064a 1327 struct ltt_kernel_channel *chan;
6dc3064a 1328
6dc3064a
DG
1329 pthread_mutex_lock(socket->lock);
1330 /* This stream must not be monitored by the consumer. */
07b86b52 1331 ret = kernel_consumer_add_metadata(socket, ksess, 0);
6dc3064a 1332 pthread_mutex_unlock(socket->lock);
6dc3064a 1333 if (ret < 0) {
ac4c0721 1334 status = LTTNG_ERR_KERN_META_FAIL;
6dc3064a
DG
1335 goto error_consumer;
1336 }
1337
1338 /* For each channel, ask the consumer to snapshot it. */
1339 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
8f07cd01 1340 status = consumer_snapshot_channel(socket, chan->key, output, 0,
5c786ded 1341 ksess->uid, ksess->gid,
1819b04a 1342 trace_path, wait,
e5148e25 1343 nb_packets_per_stream);
8f07cd01 1344 if (status != LTTNG_OK) {
2a06df8d
DG
1345 (void) kernel_consumer_destroy_metadata(socket,
1346 ksess->metadata);
6dc3064a
DG
1347 goto error_consumer;
1348 }
1349 }
1350
1351 /* Snapshot metadata, */
8f07cd01 1352 status = consumer_snapshot_channel(socket, ksess->metadata->key, output,
1819b04a 1353 1, ksess->uid, ksess->gid, trace_path, wait, 0);
8f07cd01 1354 if (status != LTTNG_OK) {
6dc3064a
DG
1355 goto error_consumer;
1356 }
07b86b52
JD
1357
1358 /*
1359 * The metadata snapshot is done, ask the consumer to destroy it since
1360 * it's not monitored on the consumer side.
1361 */
1362 (void) kernel_consumer_destroy_metadata(socket, ksess->metadata);
6dc3064a
DG
1363 }
1364
1365error_consumer:
1366 /* Close newly opened metadata stream. It's now on the consumer side. */
2a06df8d
DG
1367 err = close(ksess->metadata_stream_fd);
1368 if (err < 0) {
6dc3064a
DG
1369 PERROR("close snapshot kernel");
1370 }
1371
1372error_open_stream:
1373 trace_kernel_destroy_metadata(ksess->metadata);
1374error:
1375 /* Restore metadata state.*/
1376 ksess->metadata = saved_metadata;
1377 ksess->metadata_stream_fd = saved_metadata_fd;
6dc3064a 1378 rcu_read_unlock();
1819b04a 1379 free(trace_path);
8f07cd01 1380 return status;
6dc3064a 1381}
834978fd
DG
1382
1383/*
1384 * Get the syscall mask array from the kernel tracer.
1385 *
1386 * Return 0 on success else a negative value. In both case, syscall_mask should
1387 * be freed.
1388 */
1389int kernel_syscall_mask(int chan_fd, char **syscall_mask, uint32_t *nr_bits)
1390{
1391 assert(syscall_mask);
1392 assert(nr_bits);
1393
1394 return kernctl_syscall_mask(chan_fd, syscall_mask, nr_bits);
1395}
6e21424e
JR
1396
1397/*
1398 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1399 * version number.
1400 *
1401 * Return 1 on success, 0 when feature is not supported, negative value in case
1402 * of errors.
1403 */
32e10baa 1404int kernel_supports_ring_buffer_snapshot_sample_positions(void)
6e21424e
JR
1405{
1406 int ret = 0; // Not supported by default
1407 struct lttng_kernel_tracer_abi_version abi;
1408
32e10baa 1409 ret = kernctl_tracer_abi_version(kernel_tracer_fd, &abi);
6e21424e
JR
1410 if (ret < 0) {
1411 ERR("Failed to retrieve lttng-modules ABI version");
1412 goto error;
1413 }
1414
1415 /*
1416 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1417 */
1418 if (abi.major >= 2 && abi.minor >= 3) {
1419 /* Supported */
1420 ret = 1;
1421 } else {
1422 /* Not supported */
1423 ret = 0;
1424 }
1425error:
1426 return ret;
1427}
5c408ad8 1428
c8eabe73
MD
1429/*
1430 * Check for the support of the packet sequence number via abi version number.
1431 *
1432 * Return 1 on success, 0 when feature is not supported, negative value in case
1433 * of errors.
1434 */
1435int kernel_supports_ring_buffer_packet_sequence_number(void)
1436{
1437 int ret = 0; // Not supported by default
1438 struct lttng_kernel_tracer_abi_version abi;
1439
1440 ret = kernctl_tracer_abi_version(kernel_tracer_fd, &abi);
1441 if (ret < 0) {
1442 ERR("Failed to retrieve lttng-modules ABI version");
1443 goto error;
1444 }
1445
1446 /*
6ae77d9f
JG
1447 * Packet sequence number was introduced in LTTng 2.8,
1448 * lttng-modules ABI 2.1.
c8eabe73 1449 */
6ae77d9f 1450 if (abi.major >= 2 && abi.minor >= 1) {
c8eabe73
MD
1451 /* Supported */
1452 ret = 1;
1453 } else {
1454 /* Not supported */
1455 ret = 0;
1456 }
1457error:
1458 return ret;
1459}
1460
5c408ad8
JD
1461/*
1462 * Rotate a kernel session.
1463 *
01d7dcc5 1464 * Return LTTNG_OK on success or else an LTTng error code.
5c408ad8 1465 */
01d7dcc5 1466enum lttng_error_code kernel_rotate_session(struct ltt_session *session)
5c408ad8
JD
1467{
1468 int ret;
01d7dcc5 1469 enum lttng_error_code status = LTTNG_OK;
5c408ad8
JD
1470 struct consumer_socket *socket;
1471 struct lttng_ht_iter iter;
1472 struct ltt_kernel_session *ksess = session->kernel_session;
1473
1474 assert(ksess);
1475 assert(ksess->consumer);
1476
1477 DBG("Rotate kernel session %s started (session %" PRIu64 ")",
1478 session->name, session->id);
1479
1480 rcu_read_lock();
1481
1482 /*
1483 * Note that this loop will end after one iteration given that there is
1484 * only one kernel consumer.
1485 */
1486 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
1487 socket, node.node) {
1488 struct ltt_kernel_channel *chan;
1489
e5148e25 1490 /* For each channel, ask the consumer to rotate it. */
5c408ad8 1491 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
82528808
JG
1492 DBG("Rotate kernel channel %" PRIu64 ", session %s",
1493 chan->key, session->name);
5c408ad8
JD
1494 ret = consumer_rotate_channel(socket, chan->key,
1495 ksess->uid, ksess->gid, ksess->consumer,
e5148e25 1496 /* is_metadata_channel */ false);
5c408ad8 1497 if (ret < 0) {
01d7dcc5 1498 status = LTTNG_ERR_KERN_CONSUMER_FAIL;
5c408ad8
JD
1499 goto error;
1500 }
1501 }
1502
1503 /*
1504 * Rotate the metadata channel.
1505 */
22a1b931 1506 ret = consumer_rotate_channel(socket, ksess->metadata->key,
5c408ad8 1507 ksess->uid, ksess->gid, ksess->consumer,
e5148e25 1508 /* is_metadata_channel */ true);
5c408ad8 1509 if (ret < 0) {
01d7dcc5 1510 status = LTTNG_ERR_KERN_CONSUMER_FAIL;
5c408ad8
JD
1511 goto error;
1512 }
1513 }
1514
5c408ad8
JD
1515error:
1516 rcu_read_unlock();
01d7dcc5 1517 return status;
5c408ad8 1518}
e5148e25
JG
1519
1520enum lttng_error_code kernel_create_channel_subdirectories(
1521 const struct ltt_kernel_session *ksess)
1522{
1523 enum lttng_error_code ret = LTTNG_OK;
1524 enum lttng_trace_chunk_status chunk_status;
1525
1526 rcu_read_lock();
1527 assert(ksess->current_trace_chunk);
1528
1529 /*
1530 * Create the index subdirectory which will take care
1531 * of implicitly creating the channel's path.
1532 */
1533 chunk_status = lttng_trace_chunk_create_subdirectory(
1534 ksess->current_trace_chunk,
1535 DEFAULT_KERNEL_TRACE_DIR "/" DEFAULT_INDEX_DIR);
1536 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
1537 ret = LTTNG_ERR_CREATE_DIR_FAIL;
1538 goto error;
1539 }
1540error:
1541 rcu_read_unlock();
1542 return ret;
1543}
32e10baa
MD
1544
1545/*
1546 * Setup necessary data for kernel tracer action.
1547 */
1548LTTNG_HIDDEN
1549int init_kernel_tracer(void)
1550{
1551 int ret;
1552 bool is_root = !getuid();
1553
1554 /* Modprobe lttng kernel modules */
1555 ret = modprobe_lttng_control();
1556 if (ret < 0) {
1557 goto error;
1558 }
1559
1560 /* Open debugfs lttng */
1561 kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
1562 if (kernel_tracer_fd < 0) {
1563 DBG("Failed to open %s", module_proc_lttng);
1564 goto error_open;
1565 }
1566
1567 /* Validate kernel version */
1568 ret = kernel_validate_version(&kernel_tracer_version,
1569 &kernel_tracer_abi_version);
1570 if (ret < 0) {
1571 goto error_version;
1572 }
1573
1574 ret = modprobe_lttng_data();
1575 if (ret < 0) {
1576 goto error_modules;
1577 }
1578
1579 ret = kernel_supports_ring_buffer_snapshot_sample_positions();
1580 if (ret < 0) {
1581 goto error_modules;
1582 }
1583
1584 if (ret < 1) {
1585 WARN("Kernel tracer does not support buffer monitoring. "
1586 "The monitoring timer of channels in the kernel domain "
1587 "will be set to 0 (disabled).");
1588 }
1589
1590 DBG("Kernel tracer fd %d", kernel_tracer_fd);
1591
1592 ret = syscall_init_table(kernel_tracer_fd);
1593 if (ret < 0) {
1594 ERR("Unable to populate syscall table. Syscall tracing won't "
1595 "work for this session daemon.");
1596 }
1597 return 0;
1598
1599error_version:
1600 modprobe_remove_lttng_control();
1601 ret = close(kernel_tracer_fd);
1602 if (ret) {
1603 PERROR("close");
1604 }
1605 kernel_tracer_fd = -1;
1606 return LTTNG_ERR_KERN_VERSION;
1607
1608error_modules:
1609 ret = close(kernel_tracer_fd);
1610 if (ret) {
1611 PERROR("close");
1612 }
1613
1614error_open:
1615 modprobe_remove_lttng_control();
1616
1617error:
1618 WARN("No kernel tracer available");
1619 kernel_tracer_fd = -1;
1620 if (!is_root) {
1621 return LTTNG_ERR_NEED_ROOT_SESSIOND;
1622 } else {
1623 return LTTNG_ERR_KERN_NA;
1624 }
1625}
1626
1627LTTNG_HIDDEN
1628void cleanup_kernel_tracer(void)
1629{
1630 int ret;
1631
1632 DBG2("Closing kernel fd");
1633 if (kernel_tracer_fd >= 0) {
1634 ret = close(kernel_tracer_fd);
1635 if (ret) {
1636 PERROR("close");
1637 }
1638 kernel_tracer_fd = -1;
1639 }
1640 DBG("Unloading kernel modules");
1641 modprobe_remove_lttng_all();
1642 free(syscall_table);
1643}
1644
1645LTTNG_HIDDEN
1646bool kernel_tracer_is_initialized(void)
1647{
1648 return kernel_tracer_fd >= 0;
1649}
This page took 0.146759 seconds and 4 git commands to generate.