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