Fix: sessiond: kernel: invalid error code check
[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) {
fd5f765d
JG
663 PERROR("Failed to disable kernel event: name = '%s', fd = %d",
664 event->event->name, event->fd);
19e70852 665 goto error;
e953ef25 666 }
f3ed775e 667
19e70852
DG
668 event->enabled = 0;
669 DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd);
670
f34daff7
DG
671 return 0;
672
673error:
d36b8583 674 return ret;
f34daff7 675}
aaf26714 676
6e911cad 677
ccf10263
MD
678int kernel_track_pid(struct ltt_kernel_session *session, int pid)
679{
7c493d31
MD
680 int ret;
681
ccf10263
MD
682 DBG("Kernel track PID %d for session id %" PRIu64 ".",
683 pid, session->id);
7c493d31
MD
684 ret = kernctl_track_pid(session->fd, pid);
685 if (!ret) {
686 return LTTNG_OK;
687 }
32af2c95 688 switch (-ret) {
7c493d31
MD
689 case EINVAL:
690 return LTTNG_ERR_INVALID;
691 case ENOMEM:
692 return LTTNG_ERR_NOMEM;
693 case EEXIST:
694 return LTTNG_ERR_PID_TRACKED;
695 default:
696 return LTTNG_ERR_UNK;
697 }
ccf10263
MD
698}
699
700int kernel_untrack_pid(struct ltt_kernel_session *session, int pid)
701{
7c493d31
MD
702 int ret;
703
ccf10263
MD
704 DBG("Kernel untrack PID %d for session id %" PRIu64 ".",
705 pid, session->id);
7c493d31
MD
706 ret = kernctl_untrack_pid(session->fd, pid);
707 if (!ret) {
708 return LTTNG_OK;
709 }
32af2c95 710 switch (-ret) {
7c493d31
MD
711 case EINVAL:
712 return LTTNG_ERR_INVALID;
713 case ENOMEM:
714 return LTTNG_ERR_NOMEM;
715 case ENOENT:
716 return LTTNG_ERR_PID_NOT_TRACKED;
717 default:
718 return LTTNG_ERR_UNK;
719 }
ccf10263
MD
720}
721
a5dfbb9d
MD
722ssize_t kernel_list_tracker_pids(struct ltt_kernel_session *session,
723 int **_pids)
724{
725 int fd, ret;
726 int pid;
727 ssize_t nbmem, count = 0;
728 FILE *fp;
729 int *pids;
730
731 fd = kernctl_list_tracker_pids(session->fd);
732 if (fd < 0) {
733 PERROR("kernel tracker pids list");
734 goto error;
735 }
736
737 fp = fdopen(fd, "r");
738 if (fp == NULL) {
739 PERROR("kernel tracker pids list fdopen");
740 goto error_fp;
741 }
742
743 nbmem = KERNEL_TRACKER_PIDS_INIT_LIST_SIZE;
744 pids = zmalloc(sizeof(*pids) * nbmem);
745 if (pids == NULL) {
746 PERROR("alloc list pids");
747 count = -ENOMEM;
748 goto end;
749 }
750
751 while (fscanf(fp, "process { pid = %u; };\n", &pid) == 1) {
752 if (count >= nbmem) {
753 int *new_pids;
754 size_t new_nbmem;
755
756 new_nbmem = nbmem << 1;
757 DBG("Reallocating pids list from %zu to %zu entries",
758 nbmem, new_nbmem);
759 new_pids = realloc(pids, new_nbmem * sizeof(*new_pids));
760 if (new_pids == NULL) {
761 PERROR("realloc list events");
762 free(pids);
763 count = -ENOMEM;
764 goto end;
765 }
766 /* Zero the new memory */
767 memset(new_pids + nbmem, 0,
768 (new_nbmem - nbmem) * sizeof(*new_pids));
769 nbmem = new_nbmem;
770 pids = new_pids;
771 }
772 pids[count++] = pid;
773 }
774
775 *_pids = pids;
776 DBG("Kernel list tracker pids done (%zd pids)", count);
777end:
778 ret = fclose(fp); /* closes both fp and fd */
779 if (ret) {
780 PERROR("fclose");
781 }
782 return count;
783
784error_fp:
785 ret = close(fd);
786 if (ret) {
787 PERROR("close");
788 }
789error:
790 return -1;
791}
792
aaf26714 793/*
050349bb
DG
794 * Create kernel metadata, open from the kernel tracer and add it to the
795 * kernel session.
aaf26714 796 */
a4b92340 797int kernel_open_metadata(struct ltt_kernel_session *session)
aaf26714
DG
798{
799 int ret;
74024a21 800 struct ltt_kernel_metadata *lkm = NULL;
aaf26714 801
0525e9ae
DG
802 assert(session);
803
54012638 804 /* Allocate kernel metadata */
a4b92340 805 lkm = trace_kernel_create_metadata();
54012638 806 if (lkm == NULL) {
aaf26714
DG
807 goto error;
808 }
809
54012638 810 /* Kernel tracer metadata creation */
f3ed775e 811 ret = kernctl_open_metadata(session->fd, &lkm->conf->attr);
aaf26714 812 if (ret < 0) {
74024a21 813 goto error_open;
aaf26714
DG
814 }
815
8c0faa1d 816 lkm->fd = ret;
d40f0359 817 lkm->key = ++next_kernel_channel_key;
7b395890
DG
818 /* Prevent fd duplication after execlp() */
819 ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC);
820 if (ret < 0) {
df0f840b 821 PERROR("fcntl session fd");
7b395890
DG
822 }
823
aaf26714 824 session->metadata = lkm;
8c0faa1d 825
00e2e675 826 DBG("Kernel metadata opened (fd: %d)", lkm->fd);
8c0faa1d
DG
827
828 return 0;
829
74024a21
DG
830error_open:
831 trace_kernel_destroy_metadata(lkm);
8c0faa1d 832error:
54012638 833 return -1;
8c0faa1d
DG
834}
835
836/*
050349bb 837 * Start tracing session.
8c0faa1d
DG
838 */
839int kernel_start_session(struct ltt_kernel_session *session)
840{
841 int ret;
842
0525e9ae
DG
843 assert(session);
844
8c0faa1d
DG
845 ret = kernctl_start_session(session->fd);
846 if (ret < 0) {
df0f840b 847 PERROR("ioctl start session");
8c0faa1d
DG
848 goto error;
849 }
850
851 DBG("Kernel session started");
852
853 return 0;
854
855error:
856 return ret;
857}
858
f3ed775e 859/*
050349bb 860 * Make a kernel wait to make sure in-flight probe have completed.
f3ed775e 861 */
32e10baa 862void kernel_wait_quiescent(void)
f3ed775e
DG
863{
864 int ret;
32e10baa 865 int fd = kernel_tracer_fd;
f3ed775e
DG
866
867 DBG("Kernel quiescent wait on %d", fd);
868
869 ret = kernctl_wait_quiescent(fd);
870 if (ret < 0) {
df0f840b 871 PERROR("wait quiescent ioctl");
f3ed775e
DG
872 ERR("Kernel quiescent wait failed");
873 }
874}
875
876/*
f3ed775e
DG
877 * Force flush buffer of metadata.
878 */
879int kernel_metadata_flush_buffer(int fd)
880{
881 int ret;
882
169d2cb7
DG
883 DBG("Kernel flushing metadata buffer on fd %d", fd);
884
f3ed775e
DG
885 ret = kernctl_buffer_flush(fd);
886 if (ret < 0) {
00e2e675 887 ERR("Fail to flush metadata buffers %d (ret: %d)", fd, ret);
f3ed775e
DG
888 }
889
890 return 0;
891}
892
893/*
050349bb 894 * Force flush buffer for channel.
f3ed775e
DG
895 */
896int kernel_flush_buffer(struct ltt_kernel_channel *channel)
897{
898 int ret;
899 struct ltt_kernel_stream *stream;
900
0525e9ae
DG
901 assert(channel);
902
f3ed775e
DG
903 DBG("Flush buffer for channel %s", channel->channel->name);
904
905 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
906 DBG("Flushing channel stream %d", stream->fd);
907 ret = kernctl_buffer_flush(stream->fd);
908 if (ret < 0) {
df0f840b 909 PERROR("ioctl");
f3ed775e
DG
910 ERR("Fail to flush buffer for stream %d (ret: %d)",
911 stream->fd, ret);
912 }
913 }
914
915 return 0;
916}
917
8c0faa1d 918/*
050349bb 919 * Stop tracing session.
8c0faa1d
DG
920 */
921int kernel_stop_session(struct ltt_kernel_session *session)
922{
923 int ret;
924
0525e9ae
DG
925 assert(session);
926
8c0faa1d
DG
927 ret = kernctl_stop_session(session->fd);
928 if (ret < 0) {
929 goto error;
930 }
931
932 DBG("Kernel session stopped");
933
934 return 0;
935
936error:
937 return ret;
938}
939
940/*
050349bb
DG
941 * Open stream of channel, register it to the kernel tracer and add it
942 * to the stream list of the channel.
8c0faa1d 943 *
1cfb4b98
MD
944 * Note: given that the streams may appear in random order wrt CPU
945 * number (e.g. cpu hotplug), the index value of the stream number in
946 * the stream name is not necessarily linked to the CPU number.
947 *
050349bb 948 * Return the number of created stream. Else, a negative value.
8c0faa1d 949 */
f3ed775e 950int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
8c0faa1d 951{
1cfb4b98 952 int ret;
8c0faa1d
DG
953 struct ltt_kernel_stream *lks;
954
0525e9ae
DG
955 assert(channel);
956
5a47c6a2 957 while ((ret = kernctl_create_stream(channel->fd)) >= 0) {
1cfb4b98
MD
958 lks = trace_kernel_create_stream(channel->channel->name,
959 channel->stream_count);
8c0faa1d 960 if (lks == NULL) {
799e2c4f
MD
961 ret = close(ret);
962 if (ret) {
963 PERROR("close");
964 }
8c0faa1d
DG
965 goto error;
966 }
967
968 lks->fd = ret;
7b395890
DG
969 /* Prevent fd duplication after execlp() */
970 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
971 if (ret < 0) {
df0f840b 972 PERROR("fcntl session fd");
7b395890
DG
973 }
974
1624d5b7
JD
975 lks->tracefile_size = channel->channel->attr.tracefile_size;
976 lks->tracefile_count = channel->channel->attr.tracefile_count;
977
1cfb4b98 978 /* Add stream to channel stream list */
8c0faa1d
DG
979 cds_list_add(&lks->list, &channel->stream_list.head);
980 channel->stream_count++;
8c0faa1d 981
00e2e675
DG
982 DBG("Kernel stream %s created (fd: %d, state: %d)", lks->name, lks->fd,
983 lks->state);
54012638 984 }
8c0faa1d
DG
985
986 return channel->stream_count;
987
988error:
54012638 989 return -1;
8c0faa1d
DG
990}
991
992/*
050349bb 993 * Open the metadata stream and set it to the kernel session.
8c0faa1d 994 */
f3ed775e 995int kernel_open_metadata_stream(struct ltt_kernel_session *session)
8c0faa1d
DG
996{
997 int ret;
998
0525e9ae
DG
999 assert(session);
1000
8c0faa1d
DG
1001 ret = kernctl_create_stream(session->metadata->fd);
1002 if (ret < 0) {
df0f840b 1003 PERROR("kernel create metadata stream");
8c0faa1d
DG
1004 goto error;
1005 }
1006
1007 DBG("Kernel metadata stream created (fd: %d)", ret);
1008 session->metadata_stream_fd = ret;
7b395890
DG
1009 /* Prevent fd duplication after execlp() */
1010 ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
1011 if (ret < 0) {
df0f840b 1012 PERROR("fcntl session fd");
7b395890 1013 }
aaf26714
DG
1014
1015 return 0;
1016
1017error:
54012638 1018 return -1;
aaf26714 1019}
2ef84c95
DG
1020
1021/*
9f19cc17 1022 * Get the event list from the kernel tracer and return the number of elements.
2ef84c95 1023 */
32e10baa 1024ssize_t kernel_list_events(struct lttng_event **events)
2ef84c95 1025{
53efb85a 1026 int fd, ret;
9f19cc17
DG
1027 char *event;
1028 size_t nbmem, count = 0;
2ef84c95 1029 FILE *fp;
9f19cc17 1030 struct lttng_event *elist;
2ef84c95 1031
0525e9ae
DG
1032 assert(events);
1033
32e10baa 1034 fd = kernctl_tracepoint_list(kernel_tracer_fd);
2ef84c95 1035 if (fd < 0) {
df0f840b 1036 PERROR("kernel tracepoint list");
2ef84c95
DG
1037 goto error;
1038 }
1039
1040 fp = fdopen(fd, "r");
1041 if (fp == NULL) {
df0f840b 1042 PERROR("kernel tracepoint list fdopen");
61b73b12 1043 goto error_fp;
2ef84c95
DG
1044 }
1045
1046 /*
1047 * Init memory size counter
1048 * See kernel-ctl.h for explanation of this value
1049 */
6725fe19 1050 nbmem = KERNEL_EVENT_INIT_LIST_SIZE;
ba7f0ae5 1051 elist = zmalloc(sizeof(struct lttng_event) * nbmem);
3b870559
MD
1052 if (elist == NULL) {
1053 PERROR("alloc list events");
1054 count = -ENOMEM;
1055 goto end;
1056 }
2ef84c95 1057
53efb85a 1058 while (fscanf(fp, "event { name = %m[^;]; };\n", &event) == 1) {
6725fe19 1059 if (count >= nbmem) {
3b870559 1060 struct lttng_event *new_elist;
53efb85a 1061 size_t new_nbmem;
3b870559 1062
53efb85a
MD
1063 new_nbmem = nbmem << 1;
1064 DBG("Reallocating event list from %zu to %zu bytes",
1065 nbmem, new_nbmem);
1066 new_elist = realloc(elist, new_nbmem * sizeof(struct lttng_event));
3b870559 1067 if (new_elist == NULL) {
df0f840b 1068 PERROR("realloc list events");
3b870559
MD
1069 free(event);
1070 free(elist);
61b73b12
MD
1071 count = -ENOMEM;
1072 goto end;
2ef84c95 1073 }
53efb85a
MD
1074 /* Zero the new memory */
1075 memset(new_elist + nbmem, 0,
1076 (new_nbmem - nbmem) * sizeof(struct lttng_event));
1077 nbmem = new_nbmem;
3b870559 1078 elist = new_elist;
2ef84c95 1079 }
99497cd0
MD
1080 strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN);
1081 elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
67b9d018 1082 elist[count].enabled = -1;
9f19cc17 1083 count++;
3b870559 1084 free(event);
2ef84c95
DG
1085 }
1086
9f19cc17 1087 *events = elist;
ced2f820 1088 DBG("Kernel list events done (%zu events)", count);
61b73b12 1089end:
799e2c4f
MD
1090 ret = fclose(fp); /* closes both fp and fd */
1091 if (ret) {
1092 PERROR("fclose");
1093 }
9f19cc17 1094 return count;
2ef84c95 1095
61b73b12 1096error_fp:
799e2c4f
MD
1097 ret = close(fd);
1098 if (ret) {
1099 PERROR("close");
1100 }
2ef84c95
DG
1101error:
1102 return -1;
1103}
096102bd
DG
1104
1105/*
1106 * Get kernel version and validate it.
1107 */
32e10baa 1108int kernel_validate_version(struct lttng_kernel_tracer_version *version,
88076e89 1109 struct lttng_kernel_tracer_abi_version *abi_version)
096102bd
DG
1110{
1111 int ret;
096102bd 1112
32e10baa 1113 ret = kernctl_tracer_version(kernel_tracer_fd, version);
096102bd 1114 if (ret < 0) {
521dd134 1115 ERR("Failed to retrieve the lttng-modules version");
096102bd
DG
1116 goto error;
1117 }
1118
1119 /* Validate version */
88076e89 1120 if (version->major != VERSION_MAJOR) {
c052142c 1121 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
88076e89 1122 version->major, VERSION_MAJOR);
096102bd 1123 goto error_version;
096102bd 1124 }
32e10baa 1125 ret = kernctl_tracer_abi_version(kernel_tracer_fd, abi_version);
c052142c 1126 if (ret < 0) {
521dd134 1127 ERR("Failed to retrieve lttng-modules ABI version");
c052142c
MD
1128 goto error;
1129 }
88076e89 1130 if (abi_version->major != LTTNG_MODULES_ABI_MAJOR_VERSION) {
521dd134 1131 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
88076e89 1132 abi_version->major, abi_version->minor,
c052142c
MD
1133 LTTNG_MODULES_ABI_MAJOR_VERSION);
1134 goto error;
1135 }
1136 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
88076e89
JD
1137 version->major, version->minor,
1138 abi_version->major, abi_version->minor);
096102bd
DG
1139 return 0;
1140
1141error_version:
096102bd
DG
1142 ret = -1;
1143
1144error:
521dd134 1145 ERR("Kernel tracer version check failed; kernel tracing will not be available");
096102bd
DG
1146 return ret;
1147}
335a95b7
MD
1148
1149/*
1150 * Kernel work-arounds called at the start of sessiond main().
1151 */
1152int init_kernel_workarounds(void)
1153{
8936c33a 1154 int ret;
335a95b7
MD
1155 FILE *fp;
1156
1157 /*
1158 * boot_id needs to be read once before being used concurrently
1159 * to deal with a Linux kernel race. A fix is proposed for
1160 * upstream, but the work-around is needed for older kernels.
1161 */
1162 fp = fopen("/proc/sys/kernel/random/boot_id", "r");
1163 if (!fp) {
1164 goto end_boot_id;
1165 }
1166 while (!feof(fp)) {
1167 char buf[37] = "";
1168
8936c33a
DG
1169 ret = fread(buf, 1, sizeof(buf), fp);
1170 if (ret < 0) {
1171 /* Ignore error, we don't really care */
1172 }
335a95b7 1173 }
799e2c4f
MD
1174 ret = fclose(fp);
1175 if (ret) {
1176 PERROR("fclose");
1177 }
335a95b7 1178end_boot_id:
335a95b7
MD
1179 return 0;
1180}
2f77fc4b
DG
1181
1182/*
b1f4d3d9 1183 * Teardown of a kernel session, keeping data required by destroy notifiers.
2f77fc4b
DG
1184 */
1185void kernel_destroy_session(struct ltt_kernel_session *ksess)
1186{
698fb2ed
JG
1187 struct lttng_trace_chunk *trace_chunk;
1188
2f77fc4b
DG
1189 if (ksess == NULL) {
1190 DBG3("No kernel session when tearing down session");
1191 return;
1192 }
1193
1194 DBG("Tearing down kernel session");
698fb2ed 1195 trace_chunk = ksess->current_trace_chunk;
2f77fc4b 1196
07b86b52 1197 /*
15dc512a
DG
1198 * Destroy channels on the consumer if at least one FD has been sent and we
1199 * are in no output mode because the streams are in *no* monitor mode so we
1200 * have to send a command to clean them up or else they leaked.
07b86b52 1201 */
15dc512a 1202 if (!ksess->output_traces && ksess->consumer_fds_sent) {
07b86b52
JD
1203 int ret;
1204 struct consumer_socket *socket;
1205 struct lttng_ht_iter iter;
1206
1207 /* For each consumer socket. */
d069d577 1208 rcu_read_lock();
07b86b52
JD
1209 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
1210 socket, node.node) {
1211 struct ltt_kernel_channel *chan;
1212
1213 /* For each channel, ask the consumer to destroy it. */
1214 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
1215 ret = kernel_consumer_destroy_channel(socket, chan);
1216 if (ret < 0) {
1217 /* Consumer is probably dead. Use next socket. */
1218 continue;
1219 }
1220 }
1221 }
d069d577 1222 rcu_read_unlock();
07b86b52
JD
1223 }
1224
2f77fc4b
DG
1225 /* Close any relayd session */
1226 consumer_output_send_destroy_relayd(ksess->consumer);
1227
1228 trace_kernel_destroy_session(ksess);
698fb2ed 1229 lttng_trace_chunk_put(trace_chunk);
2f77fc4b 1230}
fb5f35b6 1231
b1f4d3d9
MD
1232/* Teardown of data required by destroy notifiers. */
1233void kernel_free_session(struct ltt_kernel_session *ksess)
1234{
1235 if (ksess == NULL) {
1236 return;
1237 }
1238 trace_kernel_free_session(ksess);
1239}
1240
fb5f35b6
DG
1241/*
1242 * Destroy a kernel channel object. It does not do anything on the tracer side.
1243 */
1244void kernel_destroy_channel(struct ltt_kernel_channel *kchan)
1245{
1246 struct ltt_kernel_session *ksess = NULL;
1247
1248 assert(kchan);
1249 assert(kchan->channel);
1250
1251 DBG3("Kernel destroy channel %s", kchan->channel->name);
1252
1253 /* Update channel count of associated session. */
1254 if (kchan->session) {
1255 /* Keep pointer reference so we can update it after the destroy. */
1256 ksess = kchan->session;
1257 }
1258
1259 trace_kernel_destroy_channel(kchan);
1260
1261 /*
1262 * At this point the kernel channel is not visible anymore. This is safe
1263 * since in order to work on a visible kernel session, the tracing session
1264 * lock (ltt_session.lock) MUST be acquired.
1265 */
1266 if (ksess) {
1267 ksess->channel_count--;
1268 }
1269}
6dc3064a
DG
1270
1271/*
1272 * Take a snapshot for a given kernel session.
1273 *
8f07cd01 1274 * Return LTTNG_OK on success or else return a LTTNG_ERR code.
6dc3064a 1275 */
65ff8ea3
JG
1276enum lttng_error_code kernel_snapshot_record(
1277 struct ltt_kernel_session *ksess,
61ace1d3 1278 const struct consumer_output *output, int wait,
d07ceecd 1279 uint64_t nb_packets_per_stream)
6dc3064a 1280{
2a06df8d 1281 int err, ret, saved_metadata_fd;
8f07cd01 1282 enum lttng_error_code status = LTTNG_OK;
6dc3064a
DG
1283 struct consumer_socket *socket;
1284 struct lttng_ht_iter iter;
1285 struct ltt_kernel_metadata *saved_metadata;
1819b04a 1286 char *trace_path = NULL;
6dc3064a
DG
1287
1288 assert(ksess);
1289 assert(ksess->consumer);
1290 assert(output);
1291
1292 DBG("Kernel snapshot record started");
1293
1294 /* Save current metadata since the following calls will change it. */
1295 saved_metadata = ksess->metadata;
1296 saved_metadata_fd = ksess->metadata_stream_fd;
1297
1298 rcu_read_lock();
1299
1300 ret = kernel_open_metadata(ksess);
1301 if (ret < 0) {
8f07cd01 1302 status = LTTNG_ERR_KERN_META_FAIL;
6dc3064a
DG
1303 goto error;
1304 }
1305
1306 ret = kernel_open_metadata_stream(ksess);
1307 if (ret < 0) {
8f07cd01 1308 status = LTTNG_ERR_KERN_META_FAIL;
6dc3064a
DG
1309 goto error_open_stream;
1310 }
1311
1819b04a
MD
1312 trace_path = setup_channel_trace_path(ksess->consumer,
1313 DEFAULT_KERNEL_TRACE_DIR);
1314 if (!trace_path) {
1315 status = LTTNG_ERR_INVALID;
1316 goto error;
1317 }
6dc3064a 1318 /* Send metadata to consumer and snapshot everything. */
61ace1d3 1319 cds_lfht_for_each_entry(output->socks->ht, &iter.iter,
6dc3064a 1320 socket, node.node) {
6dc3064a 1321 struct ltt_kernel_channel *chan;
6dc3064a 1322
6dc3064a
DG
1323 pthread_mutex_lock(socket->lock);
1324 /* This stream must not be monitored by the consumer. */
07b86b52 1325 ret = kernel_consumer_add_metadata(socket, ksess, 0);
6dc3064a 1326 pthread_mutex_unlock(socket->lock);
6dc3064a 1327 if (ret < 0) {
ac4c0721 1328 status = LTTNG_ERR_KERN_META_FAIL;
6dc3064a
DG
1329 goto error_consumer;
1330 }
1331
1332 /* For each channel, ask the consumer to snapshot it. */
1333 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
8f07cd01 1334 status = consumer_snapshot_channel(socket, chan->key, output, 0,
5c786ded 1335 ksess->uid, ksess->gid,
1819b04a 1336 trace_path, wait,
e5148e25 1337 nb_packets_per_stream);
8f07cd01 1338 if (status != LTTNG_OK) {
2a06df8d
DG
1339 (void) kernel_consumer_destroy_metadata(socket,
1340 ksess->metadata);
6dc3064a
DG
1341 goto error_consumer;
1342 }
1343 }
1344
1345 /* Snapshot metadata, */
8f07cd01 1346 status = consumer_snapshot_channel(socket, ksess->metadata->key, output,
1819b04a 1347 1, ksess->uid, ksess->gid, trace_path, wait, 0);
8f07cd01 1348 if (status != LTTNG_OK) {
6dc3064a
DG
1349 goto error_consumer;
1350 }
07b86b52
JD
1351
1352 /*
1353 * The metadata snapshot is done, ask the consumer to destroy it since
1354 * it's not monitored on the consumer side.
1355 */
1356 (void) kernel_consumer_destroy_metadata(socket, ksess->metadata);
6dc3064a
DG
1357 }
1358
1359error_consumer:
1360 /* Close newly opened metadata stream. It's now on the consumer side. */
2a06df8d
DG
1361 err = close(ksess->metadata_stream_fd);
1362 if (err < 0) {
6dc3064a
DG
1363 PERROR("close snapshot kernel");
1364 }
1365
1366error_open_stream:
1367 trace_kernel_destroy_metadata(ksess->metadata);
1368error:
1369 /* Restore metadata state.*/
1370 ksess->metadata = saved_metadata;
1371 ksess->metadata_stream_fd = saved_metadata_fd;
6dc3064a 1372 rcu_read_unlock();
1819b04a 1373 free(trace_path);
8f07cd01 1374 return status;
6dc3064a 1375}
834978fd
DG
1376
1377/*
1378 * Get the syscall mask array from the kernel tracer.
1379 *
1380 * Return 0 on success else a negative value. In both case, syscall_mask should
1381 * be freed.
1382 */
1383int kernel_syscall_mask(int chan_fd, char **syscall_mask, uint32_t *nr_bits)
1384{
1385 assert(syscall_mask);
1386 assert(nr_bits);
1387
1388 return kernctl_syscall_mask(chan_fd, syscall_mask, nr_bits);
1389}
6e21424e
JR
1390
1391/*
1392 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1393 * version number.
1394 *
1395 * Return 1 on success, 0 when feature is not supported, negative value in case
1396 * of errors.
1397 */
32e10baa 1398int kernel_supports_ring_buffer_snapshot_sample_positions(void)
6e21424e
JR
1399{
1400 int ret = 0; // Not supported by default
1401 struct lttng_kernel_tracer_abi_version abi;
1402
32e10baa 1403 ret = kernctl_tracer_abi_version(kernel_tracer_fd, &abi);
6e21424e
JR
1404 if (ret < 0) {
1405 ERR("Failed to retrieve lttng-modules ABI version");
1406 goto error;
1407 }
1408
1409 /*
1410 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1411 */
1412 if (abi.major >= 2 && abi.minor >= 3) {
1413 /* Supported */
1414 ret = 1;
1415 } else {
1416 /* Not supported */
1417 ret = 0;
1418 }
1419error:
1420 return ret;
1421}
5c408ad8 1422
c8eabe73
MD
1423/*
1424 * Check for the support of the packet sequence number via abi version number.
1425 *
1426 * Return 1 on success, 0 when feature is not supported, negative value in case
1427 * of errors.
1428 */
1429int kernel_supports_ring_buffer_packet_sequence_number(void)
1430{
1431 int ret = 0; // Not supported by default
1432 struct lttng_kernel_tracer_abi_version abi;
1433
1434 ret = kernctl_tracer_abi_version(kernel_tracer_fd, &abi);
1435 if (ret < 0) {
1436 ERR("Failed to retrieve lttng-modules ABI version");
1437 goto error;
1438 }
1439
1440 /*
6ae77d9f
JG
1441 * Packet sequence number was introduced in LTTng 2.8,
1442 * lttng-modules ABI 2.1.
c8eabe73 1443 */
6ae77d9f 1444 if (abi.major >= 2 && abi.minor >= 1) {
c8eabe73
MD
1445 /* Supported */
1446 ret = 1;
1447 } else {
1448 /* Not supported */
1449 ret = 0;
1450 }
1451error:
1452 return ret;
1453}
1454
5c408ad8
JD
1455/*
1456 * Rotate a kernel session.
1457 *
01d7dcc5 1458 * Return LTTNG_OK on success or else an LTTng error code.
5c408ad8 1459 */
01d7dcc5 1460enum lttng_error_code kernel_rotate_session(struct ltt_session *session)
5c408ad8
JD
1461{
1462 int ret;
01d7dcc5 1463 enum lttng_error_code status = LTTNG_OK;
5c408ad8
JD
1464 struct consumer_socket *socket;
1465 struct lttng_ht_iter iter;
1466 struct ltt_kernel_session *ksess = session->kernel_session;
1467
1468 assert(ksess);
1469 assert(ksess->consumer);
1470
1471 DBG("Rotate kernel session %s started (session %" PRIu64 ")",
1472 session->name, session->id);
1473
1474 rcu_read_lock();
1475
1476 /*
1477 * Note that this loop will end after one iteration given that there is
1478 * only one kernel consumer.
1479 */
1480 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
1481 socket, node.node) {
1482 struct ltt_kernel_channel *chan;
1483
e5148e25 1484 /* For each channel, ask the consumer to rotate it. */
5c408ad8 1485 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
82528808
JG
1486 DBG("Rotate kernel channel %" PRIu64 ", session %s",
1487 chan->key, session->name);
5c408ad8
JD
1488 ret = consumer_rotate_channel(socket, chan->key,
1489 ksess->uid, ksess->gid, ksess->consumer,
e5148e25 1490 /* is_metadata_channel */ false);
5c408ad8 1491 if (ret < 0) {
01d7dcc5 1492 status = LTTNG_ERR_KERN_CONSUMER_FAIL;
5c408ad8
JD
1493 goto error;
1494 }
1495 }
1496
1497 /*
1498 * Rotate the metadata channel.
1499 */
22a1b931 1500 ret = consumer_rotate_channel(socket, ksess->metadata->key,
5c408ad8 1501 ksess->uid, ksess->gid, ksess->consumer,
e5148e25 1502 /* is_metadata_channel */ true);
5c408ad8 1503 if (ret < 0) {
01d7dcc5 1504 status = LTTNG_ERR_KERN_CONSUMER_FAIL;
5c408ad8
JD
1505 goto error;
1506 }
1507 }
1508
5c408ad8
JD
1509error:
1510 rcu_read_unlock();
01d7dcc5 1511 return status;
5c408ad8 1512}
e5148e25
JG
1513
1514enum lttng_error_code kernel_create_channel_subdirectories(
1515 const struct ltt_kernel_session *ksess)
1516{
1517 enum lttng_error_code ret = LTTNG_OK;
1518 enum lttng_trace_chunk_status chunk_status;
1519
1520 rcu_read_lock();
1521 assert(ksess->current_trace_chunk);
1522
1523 /*
1524 * Create the index subdirectory which will take care
1525 * of implicitly creating the channel's path.
1526 */
1527 chunk_status = lttng_trace_chunk_create_subdirectory(
1528 ksess->current_trace_chunk,
1529 DEFAULT_KERNEL_TRACE_DIR "/" DEFAULT_INDEX_DIR);
1530 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
1531 ret = LTTNG_ERR_CREATE_DIR_FAIL;
1532 goto error;
1533 }
1534error:
1535 rcu_read_unlock();
1536 return ret;
1537}
32e10baa
MD
1538
1539/*
1540 * Setup necessary data for kernel tracer action.
1541 */
1542LTTNG_HIDDEN
1543int init_kernel_tracer(void)
1544{
1545 int ret;
1546 bool is_root = !getuid();
1547
1548 /* Modprobe lttng kernel modules */
1549 ret = modprobe_lttng_control();
1550 if (ret < 0) {
1551 goto error;
1552 }
1553
1554 /* Open debugfs lttng */
1555 kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
1556 if (kernel_tracer_fd < 0) {
1557 DBG("Failed to open %s", module_proc_lttng);
1558 goto error_open;
1559 }
1560
1561 /* Validate kernel version */
1562 ret = kernel_validate_version(&kernel_tracer_version,
1563 &kernel_tracer_abi_version);
1564 if (ret < 0) {
1565 goto error_version;
1566 }
1567
1568 ret = modprobe_lttng_data();
1569 if (ret < 0) {
1570 goto error_modules;
1571 }
1572
1573 ret = kernel_supports_ring_buffer_snapshot_sample_positions();
1574 if (ret < 0) {
1575 goto error_modules;
1576 }
1577
1578 if (ret < 1) {
1579 WARN("Kernel tracer does not support buffer monitoring. "
1580 "The monitoring timer of channels in the kernel domain "
1581 "will be set to 0 (disabled).");
1582 }
1583
1584 DBG("Kernel tracer fd %d", kernel_tracer_fd);
1585
1586 ret = syscall_init_table(kernel_tracer_fd);
1587 if (ret < 0) {
1588 ERR("Unable to populate syscall table. Syscall tracing won't "
1589 "work for this session daemon.");
1590 }
1591 return 0;
1592
1593error_version:
1594 modprobe_remove_lttng_control();
1595 ret = close(kernel_tracer_fd);
1596 if (ret) {
1597 PERROR("close");
1598 }
1599 kernel_tracer_fd = -1;
1600 return LTTNG_ERR_KERN_VERSION;
1601
1602error_modules:
1603 ret = close(kernel_tracer_fd);
1604 if (ret) {
1605 PERROR("close");
1606 }
1607
1608error_open:
1609 modprobe_remove_lttng_control();
1610
1611error:
1612 WARN("No kernel tracer available");
1613 kernel_tracer_fd = -1;
1614 if (!is_root) {
1615 return LTTNG_ERR_NEED_ROOT_SESSIOND;
1616 } else {
1617 return LTTNG_ERR_KERN_NA;
1618 }
1619}
1620
1621LTTNG_HIDDEN
1622void cleanup_kernel_tracer(void)
1623{
1624 int ret;
1625
1626 DBG2("Closing kernel fd");
1627 if (kernel_tracer_fd >= 0) {
1628 ret = close(kernel_tracer_fd);
1629 if (ret) {
1630 PERROR("close");
1631 }
1632 kernel_tracer_fd = -1;
1633 }
1634 DBG("Unloading kernel modules");
1635 modprobe_remove_lttng_all();
1636 free(syscall_table);
1637}
1638
1639LTTNG_HIDDEN
1640bool kernel_tracer_is_initialized(void)
1641{
1642 return kernel_tracer_fd >= 0;
1643}
This page took 0.192372 seconds and 4 git commands to generate.