SoW-2019-0007-2: Dynamic Snapshot: Triggers send partial event payload with notifications
[lttng-tools.git] / src / bin / lttng-sessiond / kernel.c
CommitLineData
20fe2104 1/*
ab5be9fa 2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
20fe2104 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
20fe2104 5 *
20fe2104
DG
6 */
7
159b042f
JG
8#include "bin/lttng-sessiond/tracker.h"
9#include "common/tracker.h"
10#include "common/utils.h"
d42266a4 11#include "lttng/event.h"
159b042f
JG
12#include "lttng/lttng-error.h"
13#include "lttng/tracker.h"
6c1c0768 14#define _LGPL_SOURCE
7b395890 15#include <fcntl.h>
20fe2104
DG
16#include <stdlib.h>
17#include <stdio.h>
f34daff7 18#include <string.h>
8c0faa1d 19#include <unistd.h>
77c7c900 20#include <inttypes.h>
7d268848 21#include <sys/types.h>
20fe2104 22
990570ed 23#include <common/common.h>
5024c2ac 24#include <common/hashtable/utils.h>
82b69413 25#include <common/trace-chunk.h>
db758600 26#include <common/kernel-ctl/kernel-ctl.h>
c052142c 27#include <common/kernel-ctl/kernel-ioctl.h>
42224349 28#include <common/sessiond-comm/sessiond-comm.h>
1e307fab 29
5024c2ac
JR
30#include <lttng/userspace-probe.h>
31#include <lttng/userspace-probe-internal.h>
32#include <lttng/condition/event-rule.h>
33#include <lttng/condition/event-rule-internal.h>
34#include <lttng/event-rule/event-rule.h>
35#include <lttng/event-rule/event-rule-internal.h>
36#include <lttng/event-rule/uprobe-internal.h>
37
7d268848
MD
38#include "lttng-sessiond.h"
39#include "lttng-syscall.h"
2f77fc4b 40#include "consumer.h"
4771f025 41#include "kernel.h"
6dc3064a 42#include "kernel-consumer.h"
096102bd 43#include "kern-modules.h"
834978fd 44#include "utils.h"
5c408ad8 45#include "rotate.h"
7d268848 46#include "modprobe.h"
5024c2ac 47#include "notification-thread-commands.h"
20fe2104 48
e1f3997a
JD
49/*
50 * Key used to reference a channel between the sessiond and the consumer. This
51 * is only read and updated with the session_list lock held.
52 */
53static uint64_t next_kernel_channel_key;
54
7d268848
MD
55static const char *module_proc_lttng = "/proc/lttng";
56
57static int kernel_tracer_fd = -1;
5024c2ac
JR
58static int kernel_tracer_trigger_group_fd = -1;
59static int kernel_tracer_trigger_group_notification_fd = -1;
60static struct ltt_kernel_token_event_rule_list kernel_tracer_token_list;
7d268848 61
d65106b1 62/*
050349bb 63 * Add context on a kernel channel.
df3c77c8
JG
64 *
65 * Assumes the ownership of ctx.
d65106b1
DG
66 */
67int kernel_add_channel_context(struct ltt_kernel_channel *chan,
645328ae 68 struct ltt_kernel_context *ctx)
d65106b1
DG
69{
70 int ret;
71
0525e9ae
DG
72 assert(chan);
73 assert(ctx);
74
d65106b1 75 DBG("Adding context to channel %s", chan->channel->name);
645328ae 76 ret = kernctl_add_context(chan->fd, &ctx->ctx);
d65106b1 77 if (ret < 0) {
32af2c95 78 switch (-ret) {
1ae5e83e
JD
79 case ENOSYS:
80 /* Exists but not available for this kernel */
81 ret = LTTNG_ERR_KERN_CONTEXT_UNAVAILABLE;
82 goto error;
83 case EEXIST:
b579acd9
DG
84 /* If EEXIST, we just ignore the error */
85 ret = 0;
1ae5e83e
JD
86 goto end;
87 default:
88 PERROR("add context ioctl");
89 ret = LTTNG_ERR_KERN_CONTEXT_FAIL;
90 goto error;
b579acd9 91 }
d65106b1 92 }
21ed98c1 93 ret = 0;
d65106b1 94
1ae5e83e 95end:
645328ae 96 cds_list_add_tail(&ctx->list, &chan->ctx_list);
ba985c3a 97 ctx->in_list = true;
df3c77c8 98 ctx = NULL;
d65106b1 99error:
df3c77c8
JG
100 if (ctx) {
101 trace_kernel_destroy_context(ctx);
102 }
d65106b1
DG
103 return ret;
104}
105
20fe2104 106/*
050349bb
DG
107 * Create a new kernel session, register it to the kernel tracer and add it to
108 * the session daemon session.
20fe2104 109 */
7d268848 110int kernel_create_session(struct ltt_session *session)
20fe2104
DG
111{
112 int ret;
113 struct ltt_kernel_session *lks;
114
0525e9ae
DG
115 assert(session);
116
54012638 117 /* Allocate data structure */
dec56f6c 118 lks = trace_kernel_create_session();
20fe2104 119 if (lks == NULL) {
54012638 120 ret = -1;
20fe2104
DG
121 goto error;
122 }
123
54012638 124 /* Kernel tracer session creation */
7d268848 125 ret = kernctl_create_session(kernel_tracer_fd);
20fe2104 126 if (ret < 0) {
df0f840b 127 PERROR("ioctl kernel create session");
20fe2104
DG
128 goto error;
129 }
130
20fe2104 131 lks->fd = ret;
7b395890
DG
132 /* Prevent fd duplication after execlp() */
133 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
134 if (ret < 0) {
df0f840b 135 PERROR("fcntl session fd");
7b395890
DG
136 }
137
53632229 138 lks->id = session->id;
3bd1e081 139 lks->consumer_fds_sent = 0;
8c0faa1d 140 session->kernel_session = lks;
8c0faa1d
DG
141
142 DBG("Kernel session created (fd: %d)", lks->fd);
20fe2104 143
8ff4109e
JR
144 /*
145 * This is necessary since the creation time is present in the session
146 * name when it is generated.
147 */
148 if (session->has_auto_generated_name) {
149 ret = kernctl_session_set_name(lks->fd, DEFAULT_SESSION_NAME);
150 } else {
151 ret = kernctl_session_set_name(lks->fd, session->name);
152 }
153 if (ret) {
154 WARN("Could not set kernel session name for session %" PRIu64 " name: %s",
155 session->id, session->name);
156 }
157
e04b2181
JR
158 ret = kernctl_session_set_creation_time(lks->fd, session->creation_time);
159 if (ret) {
160 WARN("Could not set kernel session creation time for session %" PRIu64 " name: %s",
161 session->id, session->name);
162 }
163
20fe2104
DG
164 return 0;
165
166error:
5f62c685
DG
167 if (lks) {
168 trace_kernel_destroy_session(lks);
d070c424 169 trace_kernel_free_session(lks);
5f62c685 170 }
20fe2104
DG
171 return ret;
172}
173
174/*
050349bb
DG
175 * Create a kernel channel, register it to the kernel tracer and add it to the
176 * kernel session.
20fe2104 177 */
050349bb 178int kernel_create_channel(struct ltt_kernel_session *session,
fdd9eb17 179 struct lttng_channel *chan)
20fe2104
DG
180{
181 int ret;
182 struct ltt_kernel_channel *lkc;
20fe2104 183
0525e9ae
DG
184 assert(session);
185 assert(chan);
0525e9ae 186
54012638 187 /* Allocate kernel channel */
fdd9eb17 188 lkc = trace_kernel_create_channel(chan);
54012638 189 if (lkc == NULL) {
20fe2104
DG
190 goto error;
191 }
192
ecc48a90 193 DBG3("Kernel create channel %s with attr: %d, %" PRIu64 ", %" PRIu64 ", %u, %u, %d, %d",
fdd9eb17 194 chan->name, lkc->channel->attr.overwrite,
173af62f
DG
195 lkc->channel->attr.subbuf_size, lkc->channel->attr.num_subbuf,
196 lkc->channel->attr.switch_timer_interval, lkc->channel->attr.read_timer_interval,
ecc48a90 197 lkc->channel->attr.live_timer_interval, lkc->channel->attr.output);
173af62f 198
54012638 199 /* Kernel tracer channel creation */
f3ed775e 200 ret = kernctl_create_channel(session->fd, &lkc->channel->attr);
20fe2104 201 if (ret < 0) {
df0f840b 202 PERROR("ioctl kernel create channel");
20fe2104
DG
203 goto error;
204 }
205
54012638 206 /* Setup the channel fd */
20fe2104 207 lkc->fd = ret;
7b395890
DG
208 /* Prevent fd duplication after execlp() */
209 ret = fcntl(lkc->fd, F_SETFD, FD_CLOEXEC);
210 if (ret < 0) {
df0f840b 211 PERROR("fcntl session fd");
7b395890
DG
212 }
213
54012638 214 /* Add channel to session */
8c0faa1d
DG
215 cds_list_add(&lkc->list, &session->channel_list.head);
216 session->channel_count++;
fb5f35b6 217 lkc->session = session;
e1f3997a 218 lkc->key = ++next_kernel_channel_key;
20fe2104 219
e1f3997a
JD
220 DBG("Kernel channel %s created (fd: %d, key: %" PRIu64 ")",
221 lkc->channel->name, lkc->fd, lkc->key);
20fe2104
DG
222
223 return 0;
224
225error:
5f62c685
DG
226 if (lkc) {
227 free(lkc->channel);
228 free(lkc);
229 }
54012638 230 return -1;
20fe2104 231}
f34daff7 232
5024c2ac
JR
233/*
234 * Create a kernel channel, register it to the kernel tracer and add it to the
235 * kernel session.
236 */
237static
238int kernel_create_trigger_group(int *trigger_group_fd)
239{
240 int ret;
241 int local_fd = -1;
242
243 assert(trigger_group_fd);
244
245 /* Kernel tracer channel creation */
246 ret = kernctl_create_trigger_group(kernel_tracer_fd);
247 if (ret < 0) {
248 PERROR("ioctl kernel create trigger group");
249 ret = -1;
250 goto error;
251 }
252
253 /* Store locally */
254 local_fd = ret;
255
256 /* Prevent fd duplication after execlp() */
257 ret = fcntl(local_fd, F_SETFD, FD_CLOEXEC);
258 if (ret < 0) {
259 PERROR("fcntl session fd");
260 }
261
262 DBG("Kernel trigger group created (fd: %d)",
263 local_fd);
264 ret = 0;
265
266error:
267 *trigger_group_fd = local_fd;
268 return ret;
269}
270
410b78a0
FD
271/*
272 * Compute the offset of the instrumentation byte in the binary based on the
273 * function probe location using the ELF lookup method.
274 *
275 * Returns 0 on success and set the offset out parameter to the offset of the
276 * elf symbol
277 * Returns -1 on error
278 */
279static
280int extract_userspace_probe_offset_function_elf(
87597c2c 281 const struct lttng_userspace_probe_location *probe_location,
5024c2ac 282 uid_t uid, gid_t gid, uint64_t *offset)
410b78a0
FD
283{
284 int fd;
285 int ret = 0;
286 const char *symbol = NULL;
87597c2c 287 const struct lttng_userspace_probe_location_lookup_method *lookup = NULL;
410b78a0
FD
288 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type;
289
410b78a0
FD
290 assert(lttng_userspace_probe_location_get_type(probe_location) ==
291 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION);
292
293 lookup = lttng_userspace_probe_location_get_lookup_method(
294 probe_location);
295 if (!lookup) {
296 ret = -1;
297 goto end;
298 }
299
300 lookup_method_type =
301 lttng_userspace_probe_location_lookup_method_get_type(lookup);
302
303 assert(lookup_method_type ==
304 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF);
305
306 symbol = lttng_userspace_probe_location_function_get_function_name(
307 probe_location);
308 if (!symbol) {
309 ret = -1;
310 goto end;
311 }
312
313 fd = lttng_userspace_probe_location_function_get_binary_fd(probe_location);
314 if (fd < 0) {
315 ret = -1;
316 goto end;
317 }
318
5024c2ac 319 ret = run_as_extract_elf_symbol_offset(fd, symbol, uid, gid, offset);
410b78a0
FD
320 if (ret < 0) {
321 DBG("userspace probe offset calculation failed for "
322 "function %s", symbol);
323 goto end;
324 }
325
326 DBG("userspace probe elf offset for %s is 0x%jd", symbol, (intmax_t)(*offset));
327end:
328 return ret;
329}
330
331/*
332 * Compute the offsets of the instrumentation bytes in the binary based on the
333 * tracepoint probe location using the SDT lookup method. This function
334 * allocates the offsets buffer, the caller must free it.
335 *
336 * Returns 0 on success and set the offset out parameter to the offsets of the
337 * SDT tracepoint.
338 * Returns -1 on error.
339 */
340static
341int extract_userspace_probe_offset_tracepoint_sdt(
87597c2c 342 const struct lttng_userspace_probe_location *probe_location,
5024c2ac 343 uid_t uid, gid_t gid, uint64_t **offsets,
410b78a0
FD
344 uint32_t *offsets_count)
345{
346 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type;
87597c2c 347 const struct lttng_userspace_probe_location_lookup_method *lookup = NULL;
410b78a0
FD
348 const char *probe_name = NULL, *provider_name = NULL;
349 int ret = 0;
350 int fd, i;
351
352 assert(lttng_userspace_probe_location_get_type(probe_location) ==
353 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT);
354
355 lookup = lttng_userspace_probe_location_get_lookup_method(probe_location);
356 if (!lookup) {
357 ret = -1;
358 goto end;
359 }
360
361 lookup_method_type =
362 lttng_userspace_probe_location_lookup_method_get_type(lookup);
363
364 assert(lookup_method_type ==
365 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT);
366
367
368 probe_name = lttng_userspace_probe_location_tracepoint_get_probe_name(
369 probe_location);
370 if (!probe_name) {
371 ret = -1;
372 goto end;
373 }
374
375 provider_name = lttng_userspace_probe_location_tracepoint_get_provider_name(
376 probe_location);
377 if (!provider_name) {
378 ret = -1;
379 goto end;
380 }
381
382 fd = lttng_userspace_probe_location_tracepoint_get_binary_fd(probe_location);
383 if (fd < 0) {
384 ret = -1;
385 goto end;
386 }
387
388 ret = run_as_extract_sdt_probe_offsets(fd, provider_name, probe_name,
5024c2ac 389 uid, gid, offsets, offsets_count);
410b78a0
FD
390 if (ret < 0) {
391 DBG("userspace probe offset calculation failed for sdt "
392 "probe %s:%s", provider_name, probe_name);
393 goto end;
394 }
395
396 if (*offsets_count == 0) {
397 DBG("no userspace probe offset found");
398 goto end;
399 }
400
401 DBG("%u userspace probe SDT offsets found for %s:%s at:",
402 *offsets_count, provider_name, probe_name);
403 for (i = 0; i < *offsets_count; i++) {
404 DBG("\t0x%jd", (intmax_t)((*offsets)[i]));
405 }
406end:
407 return ret;
408}
409
410b78a0 410static
5024c2ac
JR
411int userspace_probe_add_callsite(
412 const struct lttng_userspace_probe_location *location,
413 uid_t uid, gid_t gid, int fd)
410b78a0 414{
87597c2c 415 const struct lttng_userspace_probe_location_lookup_method *lookup_method = NULL;
410b78a0 416 enum lttng_userspace_probe_location_lookup_method_type type;
410b78a0
FD
417 int ret;
418
5024c2ac 419 lookup_method = lttng_userspace_probe_location_get_lookup_method(location);
410b78a0
FD
420 if (!lookup_method) {
421 ret = -1;
422 goto end;
423 }
424
425 type = lttng_userspace_probe_location_lookup_method_get_type(lookup_method);
426 switch (type) {
427 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF:
428 {
429 struct lttng_kernel_event_callsite callsite;
430 uint64_t offset;
431
5024c2ac
JR
432 ret = extract_userspace_probe_offset_function_elf(location,
433 uid, gid, &offset);
410b78a0
FD
434 if (ret) {
435 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
436 goto end;
437 }
438
439 callsite.u.uprobe.offset = offset;
440 ret = kernctl_add_callsite(fd, &callsite);
441 if (ret) {
5024c2ac 442 WARN("Adding callsite to ELF userspace probe failed.");
410b78a0
FD
443 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
444 goto end;
445 }
446 break;
447 }
448 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT:
449 {
450 int i;
451 uint64_t *offsets = NULL;
452 uint32_t offsets_count;
453 struct lttng_kernel_event_callsite callsite;
454
455 /*
456 * This call allocates the offsets buffer. This buffer must be freed
457 * by the caller
458 */
5024c2ac
JR
459 ret = extract_userspace_probe_offset_tracepoint_sdt(location,
460 uid, gid, &offsets, &offsets_count);
410b78a0
FD
461 if (ret) {
462 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
463 goto end;
464 }
465 for (i = 0; i < offsets_count; i++) {
466 callsite.u.uprobe.offset = offsets[i];
467 ret = kernctl_add_callsite(fd, &callsite);
468 if (ret) {
5024c2ac
JR
469 WARN("Adding callsite to SDT userspace probe "
470 "failed.");
410b78a0
FD
471 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
472 free(offsets);
473 goto end;
474 }
475 }
476 free(offsets);
477 break;
478 }
479 default:
480 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
481 goto end;
482 }
483end:
484 return ret;
485}
486
5024c2ac
JR
487/*
488 * Extract the offsets of the instrumentation point for the different lookup
489 * methods.
490 */
491static
492int userspace_probe_event_add_callsites(struct lttng_event *ev,
493 struct ltt_kernel_session *session, int fd)
494{
495 const struct lttng_userspace_probe_location *location = NULL;
496 int ret;
497
498 assert(ev);
499 assert(ev->type == LTTNG_EVENT_USERSPACE_PROBE);
500
501 location = lttng_event_get_userspace_probe_location(ev);
502 if (!location) {
503 ret = -1;
504 goto end;
505 }
506
507 ret = userspace_probe_add_callsite(location, session->uid, session->gid,
508 fd);
509 if (ret) {
510 WARN("Adding callsite to userspace probe event \"%s\" "
511 "failed.", ev->name);
512 }
513
514end:
515 return ret;
516}
517
518/*
519 * Extract the offsets of the instrumentation point for the different lookup
520 * methods.
521 */
522static int userspace_probe_event_rule_add_callsites(
523 const struct lttng_event_rule *rule,
524 const struct lttng_credentials *creds,
525 int fd)
526{
527 const struct lttng_userspace_probe_location *location = NULL;
528 enum lttng_event_rule_status status;
529 int ret;
530
531 assert(rule);
532 assert(creds);
533 assert(lttng_event_rule_get_type(rule) == LTTNG_EVENT_RULE_TYPE_UPROBE);
534
535 status = lttng_event_rule_uprobe_get_location(rule, &location);
536 if (status != LTTNG_EVENT_RULE_STATUS_OK || !location) {
537 ret = -1;
538 goto end;
539 }
540
541 ret = userspace_probe_add_callsite(location, creds->uid, creds->gid,
542 fd);
543 if (ret) {
544 WARN("Adding callsite to userspace probe object %d"
545 "failed.", fd);
546 }
547
548end:
549 return ret;
550}
551
f34daff7 552/*
050349bb
DG
553 * Create a kernel event, enable it to the kernel tracer and add it to the
554 * channel event list of the kernel session.
49d21f93 555 * We own filter_expression and filter.
f34daff7 556 */
050349bb 557int kernel_create_event(struct lttng_event *ev,
00a62084
MD
558 struct ltt_kernel_channel *channel,
559 char *filter_expression,
5024c2ac 560 struct lttng_bytecode *filter)
f34daff7 561{
71a3bb01
FD
562 int err, fd;
563 enum lttng_error_code ret;
f34daff7 564 struct ltt_kernel_event *event;
f34daff7 565
0525e9ae
DG
566 assert(ev);
567 assert(channel);
568
a969e101 569 /* We pass ownership of filter_expression and filter */
71a3bb01
FD
570 ret = trace_kernel_create_event(ev, filter_expression,
571 filter, &event);
572 if (ret != LTTNG_OK) {
f34daff7
DG
573 goto error;
574 }
575
71a3bb01
FD
576 fd = kernctl_create_event(channel->fd, event->event);
577 if (fd < 0) {
578 switch (-fd) {
bd29c13d 579 case EEXIST:
71a3bb01 580 ret = LTTNG_ERR_KERN_EVENT_EXIST;
bd29c13d
DG
581 break;
582 case ENOSYS:
583 WARN("Event type not implemented");
71a3bb01 584 ret = LTTNG_ERR_KERN_EVENT_ENOSYS;
bd29c13d 585 break;
8197a339
DG
586 case ENOENT:
587 WARN("Event %s not found!", ev->name);
71a3bb01 588 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
8197a339 589 break;
bd29c13d 590 default:
71a3bb01 591 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
d87bfb32
DG
592 PERROR("create event ioctl");
593 }
e953ef25 594 goto free_event;
8c0faa1d 595 }
f34daff7 596
d0ae4ea8 597 event->type = ev->type;
71a3bb01 598 event->fd = fd;
7b395890 599 /* Prevent fd duplication after execlp() */
71a3bb01
FD
600 err = fcntl(event->fd, F_SETFD, FD_CLOEXEC);
601 if (err < 0) {
df0f840b 602 PERROR("fcntl session fd");
7b395890
DG
603 }
604
00a62084 605 if (filter) {
71a3bb01
FD
606 err = kernctl_filter(event->fd, filter);
607 if (err < 0) {
608 switch (-err) {
609 case ENOMEM:
610 ret = LTTNG_ERR_FILTER_NOMEM;
611 break;
612 default:
613 ret = LTTNG_ERR_FILTER_INVAL;
614 break;
615 }
00a62084
MD
616 goto filter_error;
617 }
618 }
619
dcabc190 620 if (ev->type == LTTNG_EVENT_USERSPACE_PROBE) {
5024c2ac
JR
621 ret = userspace_probe_event_add_callsites(ev, channel->session,
622 event->fd);
dcabc190
FD
623 if (ret) {
624 goto add_callsite_error;
625 }
626 }
627
71a3bb01
FD
628 err = kernctl_enable(event->fd);
629 if (err < 0) {
630 switch (-err) {
00a62084
MD
631 case EEXIST:
632 ret = LTTNG_ERR_KERN_EVENT_EXIST;
633 break;
634 default:
635 PERROR("enable kernel event");
71a3bb01 636 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
00a62084
MD
637 break;
638 }
639 goto enable_error;
640 }
641
f3ed775e
DG
642 /* Add event to event list */
643 cds_list_add(&event->list, &channel->events_list.head);
cbbbb275
DG
644 channel->event_count++;
645
e953ef25
DG
646 DBG("Event %s created (fd: %d)", ev->name, event->fd);
647
648 return 0;
649
dcabc190 650add_callsite_error:
00a62084
MD
651enable_error:
652filter_error:
653 {
654 int closeret;
655
656 closeret = close(event->fd);
657 if (closeret) {
658 PERROR("close event fd");
659 }
660 }
e953ef25
DG
661free_event:
662 free(event);
663error:
d87bfb32 664 return ret;
e953ef25
DG
665}
666
26cc6b4e 667/*
050349bb 668 * Disable a kernel channel.
26cc6b4e
DG
669 */
670int kernel_disable_channel(struct ltt_kernel_channel *chan)
671{
672 int ret;
673
0525e9ae
DG
674 assert(chan);
675
26cc6b4e
DG
676 ret = kernctl_disable(chan->fd);
677 if (ret < 0) {
df0f840b 678 PERROR("disable chan ioctl");
26cc6b4e
DG
679 goto error;
680 }
681
682 chan->enabled = 0;
e1f3997a
JD
683 DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64 ")",
684 chan->channel->name, chan->fd, chan->key);
26cc6b4e
DG
685
686 return 0;
687
688error:
689 return ret;
690}
691
d36b8583 692/*
050349bb 693 * Enable a kernel channel.
d36b8583
DG
694 */
695int kernel_enable_channel(struct ltt_kernel_channel *chan)
696{
697 int ret;
698
0525e9ae
DG
699 assert(chan);
700
d36b8583 701 ret = kernctl_enable(chan->fd);
32af2c95 702 if (ret < 0 && ret != -EEXIST) {
df0f840b 703 PERROR("Enable kernel chan");
d36b8583
DG
704 goto error;
705 }
706
707 chan->enabled = 1;
e1f3997a
JD
708 DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64 ")",
709 chan->channel->name, chan->fd, chan->key);
d36b8583
DG
710
711 return 0;
712
713error:
714 return ret;
715}
716
19e70852 717/*
050349bb 718 * Enable a kernel event.
19e70852
DG
719 */
720int kernel_enable_event(struct ltt_kernel_event *event)
721{
722 int ret;
723
0525e9ae
DG
724 assert(event);
725
19e70852 726 ret = kernctl_enable(event->fd);
42224349 727 if (ret < 0) {
32af2c95 728 switch (-ret) {
42224349 729 case EEXIST:
f73fabfd 730 ret = LTTNG_ERR_KERN_EVENT_EXIST;
42224349
DG
731 break;
732 default:
733 PERROR("enable kernel event");
734 break;
735 }
19e70852
DG
736 goto error;
737 }
738
739 event->enabled = 1;
740 DBG("Kernel event %s enabled (fd: %d)", event->event->name, event->fd);
741
742 return 0;
743
744error:
d36b8583 745 return ret;
19e70852
DG
746}
747
e953ef25 748/*
050349bb 749 * Disable a kernel event.
e953ef25 750 */
19e70852 751int kernel_disable_event(struct ltt_kernel_event *event)
e953ef25
DG
752{
753 int ret;
19e70852 754
0525e9ae
DG
755 assert(event);
756
19e70852 757 ret = kernctl_disable(event->fd);
42224349 758 if (ret < 0) {
32af2c95 759 switch (-ret) {
42224349 760 case EEXIST:
f73fabfd 761 ret = LTTNG_ERR_KERN_EVENT_EXIST;
42224349
DG
762 break;
763 default:
764 PERROR("disable kernel event");
765 break;
766 }
19e70852 767 goto error;
e953ef25 768 }
f3ed775e 769
19e70852
DG
770 event->enabled = 0;
771 DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd);
772
f34daff7
DG
773 return 0;
774
775error:
d36b8583 776 return ret;
f34daff7 777}
aaf26714 778
5024c2ac
JR
779/*
780 * Disable a kernel trigger.
781 */
782static
783int kernel_disable_token_event_rule(struct ltt_kernel_token_event_rule *event)
784{
785 int ret;
786
787 assert(event);
788
789 ret = kernctl_disable(event->fd);
790 if (ret < 0) {
791 switch (-ret) {
792 case EEXIST:
793 ret = LTTNG_ERR_KERN_EVENT_EXIST;
794 break;
795 default:
796 PERROR("disable kernel event");
797 break;
798 }
799 goto error;
800 }
801
802 event->enabled = 0;
803 DBG("Kernel trigger token %" PRIu64" disabled (fd: %d)", event->token, event->fd);
804
805 return 0;
806
807error:
808 return ret;
809}
810
159b042f
JG
811static
812struct process_attr_tracker *_kernel_get_process_attr_tracker(
55c9e7ca 813 struct ltt_kernel_session *session,
159b042f 814 enum lttng_process_attr process_attr)
55c9e7ca 815{
159b042f
JG
816 switch (process_attr) {
817 case LTTNG_PROCESS_ATTR_PROCESS_ID:
818 return session->tracker_pid;
819 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID:
820 return session->tracker_vpid;
821 case LTTNG_PROCESS_ATTR_USER_ID:
822 return session->tracker_uid;
823 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID:
824 return session->tracker_vuid;
825 case LTTNG_PROCESS_ATTR_GROUP_ID:
826 return session->tracker_gid;
827 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID:
828 return session->tracker_vgid;
55c9e7ca
JR
829 default:
830 return NULL;
831 }
832}
6e911cad 833
159b042f 834const struct process_attr_tracker *kernel_get_process_attr_tracker(
55c9e7ca 835 struct ltt_kernel_session *session,
159b042f 836 enum lttng_process_attr process_attr)
ccf10263 837{
159b042f
JG
838 return (const struct process_attr_tracker *)
839 _kernel_get_process_attr_tracker(session, process_attr);
840}
7c493d31 841
159b042f
JG
842enum lttng_error_code kernel_process_attr_tracker_set_tracking_policy(
843 struct ltt_kernel_session *session,
844 enum lttng_process_attr process_attr,
845 enum lttng_tracking_policy policy)
846{
847 int ret;
848 enum lttng_error_code ret_code = LTTNG_OK;
849 struct process_attr_tracker *tracker =
850 _kernel_get_process_attr_tracker(session, process_attr);
851 enum lttng_tracking_policy previous_policy;
55c9e7ca 852
159b042f
JG
853 if (!tracker) {
854 ret_code = LTTNG_ERR_INVALID;
855 goto end;
7c493d31 856 }
ccf10263 857
159b042f
JG
858 previous_policy = process_attr_tracker_get_tracking_policy(tracker);
859 ret = process_attr_tracker_set_tracking_policy(tracker, policy);
860 if (ret) {
861 ret_code = LTTNG_ERR_UNK;
862 goto end;
55c9e7ca 863 }
7c493d31 864
159b042f 865 if (previous_policy == policy) {
55c9e7ca 866 goto end;
7c493d31 867 }
55c9e7ca 868
159b042f
JG
869 switch (policy) {
870 case LTTNG_TRACKING_POLICY_INCLUDE_ALL:
871 if (process_attr == LTTNG_PROCESS_ATTR_PROCESS_ID) {
872 /*
873 * Maintain a special case for the process ID process
874 * attribute tracker as it was the only supported
875 * attribute prior to 2.12.
876 */
877 ret = kernctl_track_pid(session->fd, -1);
878 } else {
879 ret = kernctl_track_id(session->fd, process_attr, -1);
55c9e7ca
JR
880 }
881 break;
159b042f
JG
882 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL:
883 case LTTNG_TRACKING_POLICY_INCLUDE_SET:
884 /* fall-through. */
885 if (process_attr == LTTNG_PROCESS_ATTR_PROCESS_ID) {
886 /*
887 * Maintain a special case for the process ID process
888 * attribute tracker as it was the only supported
889 * attribute prior to 2.12.
890 */
891 ret = kernctl_untrack_pid(session->fd, -1);
892 } else {
893 ret = kernctl_untrack_id(session->fd, process_attr, -1);
55c9e7ca
JR
894 }
895 break;
896 default:
159b042f 897 abort();
55c9e7ca 898 }
159b042f 899 /* kern-ctl error handling */
32af2c95 900 switch (-ret) {
159b042f
JG
901 case 0:
902 ret_code = LTTNG_OK;
903 break;
7c493d31 904 case EINVAL:
159b042f 905 ret_code = LTTNG_ERR_INVALID;
55c9e7ca 906 break;
7c493d31 907 case ENOMEM:
159b042f 908 ret_code = LTTNG_ERR_NOMEM;
55c9e7ca
JR
909 break;
910 case EEXIST:
159b042f 911 ret_code = LTTNG_ERR_PROCESS_ATTR_EXISTS;
55c9e7ca 912 break;
7c493d31 913 default:
159b042f 914 ret_code = LTTNG_ERR_UNK;
55c9e7ca
JR
915 break;
916 }
55c9e7ca 917end:
159b042f 918 return ret_code;
ccf10263
MD
919}
920
159b042f 921enum lttng_error_code kernel_process_attr_tracker_inclusion_set_add_value(
55c9e7ca 922 struct ltt_kernel_session *session,
159b042f
JG
923 enum lttng_process_attr process_attr,
924 const struct process_attr_value *value)
a5dfbb9d 925{
159b042f
JG
926 int ret, integral_value;
927 enum lttng_error_code ret_code;
928 struct process_attr_tracker *tracker;
929 enum process_attr_tracker_status status;
a5dfbb9d 930
159b042f
JG
931 /*
932 * Convert process attribute tracker value to the integral
933 * representation required by the kern-ctl API.
934 */
935 switch (process_attr) {
936 case LTTNG_PROCESS_ATTR_PROCESS_ID:
937 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID:
938 integral_value = (int) value->value.pid;
55c9e7ca 939 break;
159b042f
JG
940 case LTTNG_PROCESS_ATTR_USER_ID:
941 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID:
942 if (value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME) {
943 uid_t uid;
944
945 ret_code = utils_user_id_from_name(
946 value->value.user_name, &uid);
947 if (ret_code != LTTNG_OK) {
948 goto end;
949 }
950 integral_value = (int) uid;
951 } else {
952 integral_value = (int) value->value.uid;
55c9e7ca
JR
953 }
954 break;
159b042f
JG
955 case LTTNG_PROCESS_ATTR_GROUP_ID:
956 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID:
957 if (value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME) {
958 gid_t gid;
959
960 ret_code = utils_group_id_from_name(
961 value->value.group_name, &gid);
962 if (ret_code != LTTNG_OK) {
963 goto end;
964 }
965 integral_value = (int) gid;
966 } else {
967 integral_value = (int) value->value.gid;
a5dfbb9d 968 }
55c9e7ca
JR
969 break;
970 default:
159b042f
JG
971 ret_code = LTTNG_ERR_INVALID;
972 goto end;
55c9e7ca
JR
973 }
974
159b042f
JG
975 tracker = _kernel_get_process_attr_tracker(session, process_attr);
976 if (!tracker) {
977 ret_code = LTTNG_ERR_INVALID;
978 goto end;
979 }
980
981 status = process_attr_tracker_inclusion_set_add_value(tracker, value);
982 if (status != PROCESS_ATTR_TRACKER_STATUS_OK) {
983 switch (status) {
984 case PROCESS_ATTR_TRACKER_STATUS_EXISTS:
985 ret_code = LTTNG_ERR_PROCESS_ATTR_EXISTS;
986 break;
987 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY:
988 ret_code = LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY;
989 break;
990 case PROCESS_ATTR_TRACKER_STATUS_ERROR:
991 default:
992 ret_code = LTTNG_ERR_UNK;
993 break;
994 }
995 goto end;
996 }
997
998 DBG("Kernel track %s %d for session id %" PRIu64,
999 lttng_process_attr_to_string(process_attr),
1000 integral_value, session->id);
1001 if (process_attr == LTTNG_PROCESS_ATTR_PROCESS_ID) {
1002 /*
1003 * Maintain a special case for the process ID process attribute
1004 * tracker as it was the only supported attribute prior to 2.12.
1005 */
1006 ret = kernctl_track_pid(session->fd, integral_value);
1007 } else {
1008 ret = kernctl_track_id(
1009 session->fd, process_attr, integral_value);
1010 }
1011 if (ret == 0) {
1012 ret_code = LTTNG_OK;
1013 goto end;
1014 }
1015
1016 kernel_wait_quiescent();
1017
1018 /* kern-ctl error handling */
55c9e7ca 1019 switch (-ret) {
159b042f
JG
1020 case 0:
1021 ret_code = LTTNG_OK;
1022 break;
55c9e7ca 1023 case EINVAL:
159b042f 1024 ret_code = LTTNG_ERR_INVALID;
55c9e7ca
JR
1025 break;
1026 case ENOMEM:
159b042f 1027 ret_code = LTTNG_ERR_NOMEM;
55c9e7ca
JR
1028 break;
1029 case EEXIST:
159b042f 1030 ret_code = LTTNG_ERR_PROCESS_ATTR_EXISTS;
55c9e7ca
JR
1031 break;
1032 default:
159b042f 1033 ret_code = LTTNG_ERR_UNK;
55c9e7ca 1034 break;
a5dfbb9d
MD
1035 }
1036
159b042f
JG
1037 /* Attempt to remove the value from the tracker. */
1038 status = process_attr_tracker_inclusion_set_remove_value(
1039 tracker, value);
1040 if (status != PROCESS_ATTR_TRACKER_STATUS_OK) {
1041 ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error",
1042 lttng_process_attr_to_string(process_attr),
1043 integral_value);
55c9e7ca 1044 }
a5dfbb9d 1045end:
159b042f 1046 return ret_code;
55c9e7ca 1047}
a5dfbb9d 1048
159b042f 1049enum lttng_error_code kernel_process_attr_tracker_inclusion_set_remove_value(
55c9e7ca 1050 struct ltt_kernel_session *session,
159b042f
JG
1051 enum lttng_process_attr process_attr,
1052 const struct process_attr_value *value)
55c9e7ca 1053{
159b042f
JG
1054 int ret, integral_value;
1055 enum lttng_error_code ret_code;
1056 struct process_attr_tracker *tracker;
1057 enum process_attr_tracker_status status;
1058
1059 /*
1060 * Convert process attribute tracker value to the integral
1061 * representation required by the kern-ctl API.
1062 */
1063 switch (process_attr) {
1064 case LTTNG_PROCESS_ATTR_PROCESS_ID:
1065 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID:
1066 integral_value = (int) value->value.pid;
1067 break;
1068 case LTTNG_PROCESS_ATTR_USER_ID:
1069 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID:
1070 if (value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME) {
1071 uid_t uid;
1072
1073 ret_code = utils_user_id_from_name(
1074 value->value.user_name, &uid);
1075 if (ret_code != LTTNG_OK) {
1076 goto end;
1077 }
1078 integral_value = (int) uid;
1079 } else {
1080 integral_value = (int) value->value.uid;
1081 }
1082 break;
1083 case LTTNG_PROCESS_ATTR_GROUP_ID:
1084 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID:
1085 if (value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME) {
1086 gid_t gid;
1087
1088 ret_code = utils_group_id_from_name(
1089 value->value.group_name, &gid);
1090 if (ret_code != LTTNG_OK) {
1091 goto end;
1092 }
1093 integral_value = (int) gid;
1094 } else {
1095 integral_value = (int) value->value.gid;
1096 }
1097 break;
1098 default:
1099 ret_code = LTTNG_ERR_INVALID;
1100 goto end;
1101 }
55c9e7ca 1102
159b042f
JG
1103 tracker = _kernel_get_process_attr_tracker(session, process_attr);
1104 if (!tracker) {
1105 ret_code = LTTNG_ERR_INVALID;
a7a533cd 1106 goto end;
a5dfbb9d 1107 }
a7a533cd 1108
159b042f
JG
1109 status = process_attr_tracker_inclusion_set_remove_value(
1110 tracker, value);
1111 if (status != PROCESS_ATTR_TRACKER_STATUS_OK) {
1112 switch (status) {
1113 case PROCESS_ATTR_TRACKER_STATUS_MISSING:
1114 ret_code = LTTNG_ERR_PROCESS_ATTR_MISSING;
1115 break;
1116 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY:
1117 ret_code = LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY;
1118 break;
1119 case PROCESS_ATTR_TRACKER_STATUS_ERROR:
1120 default:
1121 ret_code = LTTNG_ERR_UNK;
1122 break;
1123 }
a7a533cd
JR
1124 goto end;
1125 }
1126
159b042f
JG
1127 DBG("Kernel track %s %d for session id %" PRIu64,
1128 lttng_process_attr_to_string(process_attr),
1129 integral_value, session->id);
1130 if (process_attr == LTTNG_PROCESS_ATTR_PROCESS_ID) {
1131 /*
1132 * Maintain a special case for the process ID process attribute
1133 * tracker as it was the only supported attribute prior to 2.12.
1134 */
1135 ret = kernctl_untrack_pid(session->fd, integral_value);
1136 } else {
1137 ret = kernctl_untrack_id(
1138 session->fd, process_attr, integral_value);
1139 }
1140 if (ret == 0) {
1141 ret_code = LTTNG_OK;
1142 goto end;
1143 }
1144 kernel_wait_quiescent();
1145
1146 /* kern-ctl error handling */
1147 switch (-ret) {
1148 case 0:
1149 ret_code = LTTNG_OK;
1150 break;
1151 case EINVAL:
1152 ret_code = LTTNG_ERR_INVALID;
1153 break;
1154 case ENOMEM:
1155 ret_code = LTTNG_ERR_NOMEM;
1156 break;
1157 case ENOENT:
1158 ret_code = LTTNG_ERR_PROCESS_ATTR_MISSING;
1159 break;
1160 default:
1161 ret_code = LTTNG_ERR_UNK;
1162 break;
1163 }
1164
1165 /* Attempt to add the value to the tracker. */
1166 status = process_attr_tracker_inclusion_set_add_value(
1167 tracker, value);
1168 if (status != PROCESS_ATTR_TRACKER_STATUS_OK) {
1169 ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error",
1170 lttng_process_attr_to_string(process_attr),
1171 integral_value);
1172 }
a7a533cd 1173end:
159b042f 1174 return ret_code;
a5dfbb9d
MD
1175}
1176
aaf26714 1177/*
050349bb
DG
1178 * Create kernel metadata, open from the kernel tracer and add it to the
1179 * kernel session.
aaf26714 1180 */
a4b92340 1181int kernel_open_metadata(struct ltt_kernel_session *session)
aaf26714
DG
1182{
1183 int ret;
74024a21 1184 struct ltt_kernel_metadata *lkm = NULL;
aaf26714 1185
0525e9ae
DG
1186 assert(session);
1187
54012638 1188 /* Allocate kernel metadata */
a4b92340 1189 lkm = trace_kernel_create_metadata();
54012638 1190 if (lkm == NULL) {
aaf26714
DG
1191 goto error;
1192 }
1193
54012638 1194 /* Kernel tracer metadata creation */
f3ed775e 1195 ret = kernctl_open_metadata(session->fd, &lkm->conf->attr);
aaf26714 1196 if (ret < 0) {
74024a21 1197 goto error_open;
aaf26714
DG
1198 }
1199
8c0faa1d 1200 lkm->fd = ret;
d40f0359 1201 lkm->key = ++next_kernel_channel_key;
7b395890
DG
1202 /* Prevent fd duplication after execlp() */
1203 ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC);
1204 if (ret < 0) {
df0f840b 1205 PERROR("fcntl session fd");
7b395890
DG
1206 }
1207
aaf26714 1208 session->metadata = lkm;
8c0faa1d 1209
00e2e675 1210 DBG("Kernel metadata opened (fd: %d)", lkm->fd);
8c0faa1d
DG
1211
1212 return 0;
1213
74024a21
DG
1214error_open:
1215 trace_kernel_destroy_metadata(lkm);
8c0faa1d 1216error:
54012638 1217 return -1;
8c0faa1d
DG
1218}
1219
1220/*
050349bb 1221 * Start tracing session.
8c0faa1d
DG
1222 */
1223int kernel_start_session(struct ltt_kernel_session *session)
1224{
1225 int ret;
1226
0525e9ae
DG
1227 assert(session);
1228
8c0faa1d
DG
1229 ret = kernctl_start_session(session->fd);
1230 if (ret < 0) {
df0f840b 1231 PERROR("ioctl start session");
8c0faa1d
DG
1232 goto error;
1233 }
1234
1235 DBG("Kernel session started");
1236
1237 return 0;
1238
1239error:
1240 return ret;
1241}
1242
f3ed775e 1243/*
050349bb 1244 * Make a kernel wait to make sure in-flight probe have completed.
f3ed775e 1245 */
7d268848 1246void kernel_wait_quiescent(void)
f3ed775e
DG
1247{
1248 int ret;
7d268848 1249 int fd = kernel_tracer_fd;
f3ed775e
DG
1250
1251 DBG("Kernel quiescent wait on %d", fd);
1252
1253 ret = kernctl_wait_quiescent(fd);
1254 if (ret < 0) {
df0f840b 1255 PERROR("wait quiescent ioctl");
f3ed775e
DG
1256 ERR("Kernel quiescent wait failed");
1257 }
1258}
1259
1260/*
f3ed775e
DG
1261 * Force flush buffer of metadata.
1262 */
1263int kernel_metadata_flush_buffer(int fd)
1264{
1265 int ret;
1266
169d2cb7
DG
1267 DBG("Kernel flushing metadata buffer on fd %d", fd);
1268
f3ed775e
DG
1269 ret = kernctl_buffer_flush(fd);
1270 if (ret < 0) {
00e2e675 1271 ERR("Fail to flush metadata buffers %d (ret: %d)", fd, ret);
f3ed775e
DG
1272 }
1273
1274 return 0;
1275}
1276
1277/*
050349bb 1278 * Force flush buffer for channel.
f3ed775e
DG
1279 */
1280int kernel_flush_buffer(struct ltt_kernel_channel *channel)
1281{
1282 int ret;
1283 struct ltt_kernel_stream *stream;
1284
0525e9ae
DG
1285 assert(channel);
1286
f3ed775e
DG
1287 DBG("Flush buffer for channel %s", channel->channel->name);
1288
1289 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
1290 DBG("Flushing channel stream %d", stream->fd);
1291 ret = kernctl_buffer_flush(stream->fd);
1292 if (ret < 0) {
df0f840b 1293 PERROR("ioctl");
f3ed775e
DG
1294 ERR("Fail to flush buffer for stream %d (ret: %d)",
1295 stream->fd, ret);
1296 }
1297 }
1298
1299 return 0;
1300}
1301
8c0faa1d 1302/*
050349bb 1303 * Stop tracing session.
8c0faa1d
DG
1304 */
1305int kernel_stop_session(struct ltt_kernel_session *session)
1306{
1307 int ret;
1308
0525e9ae
DG
1309 assert(session);
1310
8c0faa1d
DG
1311 ret = kernctl_stop_session(session->fd);
1312 if (ret < 0) {
1313 goto error;
1314 }
1315
1316 DBG("Kernel session stopped");
1317
1318 return 0;
1319
1320error:
1321 return ret;
1322}
1323
1324/*
050349bb
DG
1325 * Open stream of channel, register it to the kernel tracer and add it
1326 * to the stream list of the channel.
8c0faa1d 1327 *
1cfb4b98
MD
1328 * Note: given that the streams may appear in random order wrt CPU
1329 * number (e.g. cpu hotplug), the index value of the stream number in
1330 * the stream name is not necessarily linked to the CPU number.
1331 *
050349bb 1332 * Return the number of created stream. Else, a negative value.
8c0faa1d 1333 */
f3ed775e 1334int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
8c0faa1d 1335{
1cfb4b98 1336 int ret;
8c0faa1d
DG
1337 struct ltt_kernel_stream *lks;
1338
0525e9ae
DG
1339 assert(channel);
1340
5a47c6a2 1341 while ((ret = kernctl_create_stream(channel->fd)) >= 0) {
1cfb4b98
MD
1342 lks = trace_kernel_create_stream(channel->channel->name,
1343 channel->stream_count);
8c0faa1d 1344 if (lks == NULL) {
799e2c4f
MD
1345 ret = close(ret);
1346 if (ret) {
1347 PERROR("close");
1348 }
8c0faa1d
DG
1349 goto error;
1350 }
1351
1352 lks->fd = ret;
7b395890
DG
1353 /* Prevent fd duplication after execlp() */
1354 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
1355 if (ret < 0) {
df0f840b 1356 PERROR("fcntl session fd");
7b395890
DG
1357 }
1358
1624d5b7
JD
1359 lks->tracefile_size = channel->channel->attr.tracefile_size;
1360 lks->tracefile_count = channel->channel->attr.tracefile_count;
1361
1cfb4b98 1362 /* Add stream to channel stream list */
8c0faa1d
DG
1363 cds_list_add(&lks->list, &channel->stream_list.head);
1364 channel->stream_count++;
8c0faa1d 1365
00e2e675
DG
1366 DBG("Kernel stream %s created (fd: %d, state: %d)", lks->name, lks->fd,
1367 lks->state);
54012638 1368 }
8c0faa1d
DG
1369
1370 return channel->stream_count;
1371
1372error:
54012638 1373 return -1;
8c0faa1d
DG
1374}
1375
1376/*
050349bb 1377 * Open the metadata stream and set it to the kernel session.
8c0faa1d 1378 */
f3ed775e 1379int kernel_open_metadata_stream(struct ltt_kernel_session *session)
8c0faa1d
DG
1380{
1381 int ret;
1382
0525e9ae
DG
1383 assert(session);
1384
8c0faa1d
DG
1385 ret = kernctl_create_stream(session->metadata->fd);
1386 if (ret < 0) {
df0f840b 1387 PERROR("kernel create metadata stream");
8c0faa1d
DG
1388 goto error;
1389 }
1390
1391 DBG("Kernel metadata stream created (fd: %d)", ret);
1392 session->metadata_stream_fd = ret;
7b395890
DG
1393 /* Prevent fd duplication after execlp() */
1394 ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
1395 if (ret < 0) {
df0f840b 1396 PERROR("fcntl session fd");
7b395890 1397 }
aaf26714
DG
1398
1399 return 0;
1400
1401error:
54012638 1402 return -1;
aaf26714 1403}
2ef84c95
DG
1404
1405/*
9f19cc17 1406 * Get the event list from the kernel tracer and return the number of elements.
2ef84c95 1407 */
7d268848 1408ssize_t kernel_list_events(struct lttng_event **events)
2ef84c95 1409{
53efb85a 1410 int fd, ret;
9f19cc17
DG
1411 char *event;
1412 size_t nbmem, count = 0;
2ef84c95 1413 FILE *fp;
9f19cc17 1414 struct lttng_event *elist;
2ef84c95 1415
0525e9ae
DG
1416 assert(events);
1417
7d268848 1418 fd = kernctl_tracepoint_list(kernel_tracer_fd);
2ef84c95 1419 if (fd < 0) {
df0f840b 1420 PERROR("kernel tracepoint list");
2ef84c95
DG
1421 goto error;
1422 }
1423
1424 fp = fdopen(fd, "r");
1425 if (fp == NULL) {
df0f840b 1426 PERROR("kernel tracepoint list fdopen");
61b73b12 1427 goto error_fp;
2ef84c95
DG
1428 }
1429
1430 /*
1431 * Init memory size counter
1432 * See kernel-ctl.h for explanation of this value
1433 */
6725fe19 1434 nbmem = KERNEL_EVENT_INIT_LIST_SIZE;
ba7f0ae5 1435 elist = zmalloc(sizeof(struct lttng_event) * nbmem);
3b870559
MD
1436 if (elist == NULL) {
1437 PERROR("alloc list events");
1438 count = -ENOMEM;
1439 goto end;
1440 }
2ef84c95 1441
53efb85a 1442 while (fscanf(fp, "event { name = %m[^;]; };\n", &event) == 1) {
6725fe19 1443 if (count >= nbmem) {
3b870559 1444 struct lttng_event *new_elist;
53efb85a 1445 size_t new_nbmem;
3b870559 1446
53efb85a
MD
1447 new_nbmem = nbmem << 1;
1448 DBG("Reallocating event list from %zu to %zu bytes",
1449 nbmem, new_nbmem);
1450 new_elist = realloc(elist, new_nbmem * sizeof(struct lttng_event));
3b870559 1451 if (new_elist == NULL) {
df0f840b 1452 PERROR("realloc list events");
3b870559
MD
1453 free(event);
1454 free(elist);
61b73b12
MD
1455 count = -ENOMEM;
1456 goto end;
2ef84c95 1457 }
53efb85a
MD
1458 /* Zero the new memory */
1459 memset(new_elist + nbmem, 0,
1460 (new_nbmem - nbmem) * sizeof(struct lttng_event));
1461 nbmem = new_nbmem;
3b870559 1462 elist = new_elist;
2ef84c95 1463 }
99497cd0
MD
1464 strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN);
1465 elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
67b9d018 1466 elist[count].enabled = -1;
9f19cc17 1467 count++;
3b870559 1468 free(event);
2ef84c95
DG
1469 }
1470
9f19cc17 1471 *events = elist;
ced2f820 1472 DBG("Kernel list events done (%zu events)", count);
61b73b12 1473end:
799e2c4f
MD
1474 ret = fclose(fp); /* closes both fp and fd */
1475 if (ret) {
1476 PERROR("fclose");
1477 }
9f19cc17 1478 return count;
2ef84c95 1479
61b73b12 1480error_fp:
799e2c4f
MD
1481 ret = close(fd);
1482 if (ret) {
1483 PERROR("close");
1484 }
2ef84c95
DG
1485error:
1486 return -1;
1487}
096102bd
DG
1488
1489/*
1490 * Get kernel version and validate it.
1491 */
7d268848 1492int kernel_validate_version(struct lttng_kernel_tracer_version *version,
88076e89 1493 struct lttng_kernel_tracer_abi_version *abi_version)
096102bd
DG
1494{
1495 int ret;
096102bd 1496
7d268848 1497 ret = kernctl_tracer_version(kernel_tracer_fd, version);
096102bd 1498 if (ret < 0) {
521dd134 1499 ERR("Failed to retrieve the lttng-modules version");
096102bd
DG
1500 goto error;
1501 }
1502
1503 /* Validate version */
88076e89 1504 if (version->major != VERSION_MAJOR) {
c052142c 1505 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
88076e89 1506 version->major, VERSION_MAJOR);
096102bd 1507 goto error_version;
096102bd 1508 }
7d268848 1509 ret = kernctl_tracer_abi_version(kernel_tracer_fd, abi_version);
c052142c 1510 if (ret < 0) {
521dd134 1511 ERR("Failed to retrieve lttng-modules ABI version");
c052142c
MD
1512 goto error;
1513 }
88076e89 1514 if (abi_version->major != LTTNG_MODULES_ABI_MAJOR_VERSION) {
521dd134 1515 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
88076e89 1516 abi_version->major, abi_version->minor,
c052142c
MD
1517 LTTNG_MODULES_ABI_MAJOR_VERSION);
1518 goto error;
1519 }
1520 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
88076e89
JD
1521 version->major, version->minor,
1522 abi_version->major, abi_version->minor);
096102bd
DG
1523 return 0;
1524
1525error_version:
096102bd
DG
1526 ret = -1;
1527
1528error:
521dd134 1529 ERR("Kernel tracer version check failed; kernel tracing will not be available");
096102bd
DG
1530 return ret;
1531}
335a95b7
MD
1532
1533/*
1534 * Kernel work-arounds called at the start of sessiond main().
1535 */
1536int init_kernel_workarounds(void)
1537{
8936c33a 1538 int ret;
335a95b7
MD
1539 FILE *fp;
1540
1541 /*
1542 * boot_id needs to be read once before being used concurrently
1543 * to deal with a Linux kernel race. A fix is proposed for
1544 * upstream, but the work-around is needed for older kernels.
1545 */
1546 fp = fopen("/proc/sys/kernel/random/boot_id", "r");
1547 if (!fp) {
1548 goto end_boot_id;
1549 }
1550 while (!feof(fp)) {
1551 char buf[37] = "";
1552
8936c33a
DG
1553 ret = fread(buf, 1, sizeof(buf), fp);
1554 if (ret < 0) {
1555 /* Ignore error, we don't really care */
1556 }
335a95b7 1557 }
799e2c4f
MD
1558 ret = fclose(fp);
1559 if (ret) {
1560 PERROR("fclose");
1561 }
335a95b7 1562end_boot_id:
335a95b7
MD
1563 return 0;
1564}
2f77fc4b
DG
1565
1566/*
d070c424 1567 * Teardown of a kernel session, keeping data required by destroy notifiers.
2f77fc4b
DG
1568 */
1569void kernel_destroy_session(struct ltt_kernel_session *ksess)
1570{
82b69413
JG
1571 struct lttng_trace_chunk *trace_chunk;
1572
2f77fc4b
DG
1573 if (ksess == NULL) {
1574 DBG3("No kernel session when tearing down session");
1575 return;
1576 }
1577
1578 DBG("Tearing down kernel session");
82b69413 1579 trace_chunk = ksess->current_trace_chunk;
2f77fc4b 1580
07b86b52 1581 /*
15dc512a
DG
1582 * Destroy channels on the consumer if at least one FD has been sent and we
1583 * are in no output mode because the streams are in *no* monitor mode so we
1584 * have to send a command to clean them up or else they leaked.
07b86b52 1585 */
15dc512a 1586 if (!ksess->output_traces && ksess->consumer_fds_sent) {
07b86b52
JD
1587 int ret;
1588 struct consumer_socket *socket;
1589 struct lttng_ht_iter iter;
1590
1591 /* For each consumer socket. */
d069d577 1592 rcu_read_lock();
07b86b52
JD
1593 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
1594 socket, node.node) {
1595 struct ltt_kernel_channel *chan;
1596
1597 /* For each channel, ask the consumer to destroy it. */
1598 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
1599 ret = kernel_consumer_destroy_channel(socket, chan);
1600 if (ret < 0) {
1601 /* Consumer is probably dead. Use next socket. */
1602 continue;
1603 }
1604 }
1605 }
d069d577 1606 rcu_read_unlock();
07b86b52
JD
1607 }
1608
2f77fc4b
DG
1609 /* Close any relayd session */
1610 consumer_output_send_destroy_relayd(ksess->consumer);
1611
1612 trace_kernel_destroy_session(ksess);
82b69413 1613 lttng_trace_chunk_put(trace_chunk);
2f77fc4b 1614}
fb5f35b6 1615
d070c424
MD
1616/* Teardown of data required by destroy notifiers. */
1617void kernel_free_session(struct ltt_kernel_session *ksess)
1618{
1619 if (ksess == NULL) {
1620 return;
1621 }
1622 trace_kernel_free_session(ksess);
1623}
1624
fb5f35b6
DG
1625/*
1626 * Destroy a kernel channel object. It does not do anything on the tracer side.
1627 */
1628void kernel_destroy_channel(struct ltt_kernel_channel *kchan)
1629{
1630 struct ltt_kernel_session *ksess = NULL;
1631
1632 assert(kchan);
1633 assert(kchan->channel);
1634
1635 DBG3("Kernel destroy channel %s", kchan->channel->name);
1636
1637 /* Update channel count of associated session. */
1638 if (kchan->session) {
1639 /* Keep pointer reference so we can update it after the destroy. */
1640 ksess = kchan->session;
1641 }
1642
1643 trace_kernel_destroy_channel(kchan);
1644
1645 /*
1646 * At this point the kernel channel is not visible anymore. This is safe
1647 * since in order to work on a visible kernel session, the tracing session
1648 * lock (ltt_session.lock) MUST be acquired.
1649 */
1650 if (ksess) {
1651 ksess->channel_count--;
1652 }
1653}
6dc3064a
DG
1654
1655/*
1656 * Take a snapshot for a given kernel session.
1657 *
9a654598 1658 * Return LTTNG_OK on success or else return a LTTNG_ERR code.
6dc3064a 1659 */
fb9a95c4
JG
1660enum lttng_error_code kernel_snapshot_record(
1661 struct ltt_kernel_session *ksess,
348a81dc 1662 const struct consumer_output *output, int wait,
d07ceecd 1663 uint64_t nb_packets_per_stream)
6dc3064a 1664{
2a06df8d 1665 int err, ret, saved_metadata_fd;
9a654598 1666 enum lttng_error_code status = LTTNG_OK;
6dc3064a
DG
1667 struct consumer_socket *socket;
1668 struct lttng_ht_iter iter;
1669 struct ltt_kernel_metadata *saved_metadata;
3b967712 1670 char *trace_path = NULL;
5da88b0f 1671 size_t consumer_path_offset = 0;
6dc3064a
DG
1672
1673 assert(ksess);
1674 assert(ksess->consumer);
1675 assert(output);
1676
1677 DBG("Kernel snapshot record started");
1678
1679 /* Save current metadata since the following calls will change it. */
1680 saved_metadata = ksess->metadata;
1681 saved_metadata_fd = ksess->metadata_stream_fd;
1682
1683 rcu_read_lock();
1684
1685 ret = kernel_open_metadata(ksess);
1686 if (ret < 0) {
9a654598 1687 status = LTTNG_ERR_KERN_META_FAIL;
6dc3064a
DG
1688 goto error;
1689 }
1690
1691 ret = kernel_open_metadata_stream(ksess);
1692 if (ret < 0) {
9a654598 1693 status = LTTNG_ERR_KERN_META_FAIL;
6dc3064a
DG
1694 goto error_open_stream;
1695 }
1696
3b967712 1697 trace_path = setup_channel_trace_path(ksess->consumer,
5da88b0f 1698 DEFAULT_KERNEL_TRACE_DIR, &consumer_path_offset);
3b967712
MD
1699 if (!trace_path) {
1700 status = LTTNG_ERR_INVALID;
1701 goto error;
1702 }
6dc3064a 1703 /* Send metadata to consumer and snapshot everything. */
348a81dc 1704 cds_lfht_for_each_entry(output->socks->ht, &iter.iter,
6dc3064a 1705 socket, node.node) {
6dc3064a 1706 struct ltt_kernel_channel *chan;
6dc3064a 1707
6dc3064a
DG
1708 pthread_mutex_lock(socket->lock);
1709 /* This stream must not be monitored by the consumer. */
07b86b52 1710 ret = kernel_consumer_add_metadata(socket, ksess, 0);
6dc3064a 1711 pthread_mutex_unlock(socket->lock);
6dc3064a 1712 if (ret < 0) {
0ed78e50 1713 status = LTTNG_ERR_KERN_META_FAIL;
6dc3064a
DG
1714 goto error_consumer;
1715 }
1716
1717 /* For each channel, ask the consumer to snapshot it. */
1718 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
9a654598 1719 status = consumer_snapshot_channel(socket, chan->key, output, 0,
5c786ded 1720 ksess->uid, ksess->gid,
5da88b0f 1721 &trace_path[consumer_path_offset], wait,
d2956687 1722 nb_packets_per_stream);
9a654598 1723 if (status != LTTNG_OK) {
2a06df8d
DG
1724 (void) kernel_consumer_destroy_metadata(socket,
1725 ksess->metadata);
6dc3064a
DG
1726 goto error_consumer;
1727 }
1728 }
1729
1730 /* Snapshot metadata, */
9a654598 1731 status = consumer_snapshot_channel(socket, ksess->metadata->key, output,
5da88b0f
MD
1732 1, ksess->uid, ksess->gid, &trace_path[consumer_path_offset],
1733 wait, 0);
9a654598 1734 if (status != LTTNG_OK) {
6dc3064a
DG
1735 goto error_consumer;
1736 }
07b86b52
JD
1737
1738 /*
1739 * The metadata snapshot is done, ask the consumer to destroy it since
1740 * it's not monitored on the consumer side.
1741 */
1742 (void) kernel_consumer_destroy_metadata(socket, ksess->metadata);
6dc3064a
DG
1743 }
1744
1745error_consumer:
1746 /* Close newly opened metadata stream. It's now on the consumer side. */
2a06df8d
DG
1747 err = close(ksess->metadata_stream_fd);
1748 if (err < 0) {
6dc3064a
DG
1749 PERROR("close snapshot kernel");
1750 }
1751
1752error_open_stream:
1753 trace_kernel_destroy_metadata(ksess->metadata);
1754error:
1755 /* Restore metadata state.*/
1756 ksess->metadata = saved_metadata;
1757 ksess->metadata_stream_fd = saved_metadata_fd;
6dc3064a 1758 rcu_read_unlock();
3b967712 1759 free(trace_path);
9a654598 1760 return status;
6dc3064a 1761}
834978fd
DG
1762
1763/*
1764 * Get the syscall mask array from the kernel tracer.
1765 *
1766 * Return 0 on success else a negative value. In both case, syscall_mask should
1767 * be freed.
1768 */
1769int kernel_syscall_mask(int chan_fd, char **syscall_mask, uint32_t *nr_bits)
1770{
1771 assert(syscall_mask);
1772 assert(nr_bits);
1773
1774 return kernctl_syscall_mask(chan_fd, syscall_mask, nr_bits);
1775}
6e21424e
JR
1776
1777/*
1778 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1779 * version number.
1780 *
1781 * Return 1 on success, 0 when feature is not supported, negative value in case
1782 * of errors.
1783 */
7d268848 1784int kernel_supports_ring_buffer_snapshot_sample_positions(void)
6e21424e
JR
1785{
1786 int ret = 0; // Not supported by default
1787 struct lttng_kernel_tracer_abi_version abi;
1788
7d268848 1789 ret = kernctl_tracer_abi_version(kernel_tracer_fd, &abi);
6e21424e
JR
1790 if (ret < 0) {
1791 ERR("Failed to retrieve lttng-modules ABI version");
1792 goto error;
1793 }
1794
1795 /*
1796 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1797 */
1798 if (abi.major >= 2 && abi.minor >= 3) {
1799 /* Supported */
1800 ret = 1;
1801 } else {
1802 /* Not supported */
1803 ret = 0;
1804 }
1805error:
1806 return ret;
1807}
5c408ad8 1808
a40a503f
MD
1809/*
1810 * Check for the support of the packet sequence number via abi version number.
1811 *
1812 * Return 1 on success, 0 when feature is not supported, negative value in case
1813 * of errors.
1814 */
1815int kernel_supports_ring_buffer_packet_sequence_number(void)
1816{
1817 int ret = 0; // Not supported by default
1818 struct lttng_kernel_tracer_abi_version abi;
1819
1820 ret = kernctl_tracer_abi_version(kernel_tracer_fd, &abi);
1821 if (ret < 0) {
1822 ERR("Failed to retrieve lttng-modules ABI version");
1823 goto error;
1824 }
1825
1826 /*
3029bc92
JG
1827 * Packet sequence number was introduced in LTTng 2.8,
1828 * lttng-modules ABI 2.1.
a40a503f 1829 */
3029bc92 1830 if (abi.major >= 2 && abi.minor >= 1) {
a40a503f
MD
1831 /* Supported */
1832 ret = 1;
1833 } else {
1834 /* Not supported */
1835 ret = 0;
1836 }
1837error:
1838 return ret;
1839}
1840
5c408ad8
JD
1841/*
1842 * Rotate a kernel session.
1843 *
d5a1b7aa 1844 * Return LTTNG_OK on success or else an LTTng error code.
5c408ad8 1845 */
d5a1b7aa 1846enum lttng_error_code kernel_rotate_session(struct ltt_session *session)
5c408ad8
JD
1847{
1848 int ret;
d5a1b7aa 1849 enum lttng_error_code status = LTTNG_OK;
5c408ad8
JD
1850 struct consumer_socket *socket;
1851 struct lttng_ht_iter iter;
1852 struct ltt_kernel_session *ksess = session->kernel_session;
1853
1854 assert(ksess);
1855 assert(ksess->consumer);
1856
1857 DBG("Rotate kernel session %s started (session %" PRIu64 ")",
1858 session->name, session->id);
1859
1860 rcu_read_lock();
1861
1862 /*
1863 * Note that this loop will end after one iteration given that there is
1864 * only one kernel consumer.
1865 */
1866 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
1867 socket, node.node) {
1868 struct ltt_kernel_channel *chan;
1869
d2956687 1870 /* For each channel, ask the consumer to rotate it. */
5c408ad8 1871 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
92816cc3
JG
1872 DBG("Rotate kernel channel %" PRIu64 ", session %s",
1873 chan->key, session->name);
5c408ad8
JD
1874 ret = consumer_rotate_channel(socket, chan->key,
1875 ksess->uid, ksess->gid, ksess->consumer,
d2956687 1876 /* is_metadata_channel */ false);
5c408ad8 1877 if (ret < 0) {
7e0de219 1878 status = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
5c408ad8
JD
1879 goto error;
1880 }
1881 }
1882
1883 /*
1884 * Rotate the metadata channel.
1885 */
22a1b931 1886 ret = consumer_rotate_channel(socket, ksess->metadata->key,
5c408ad8 1887 ksess->uid, ksess->gid, ksess->consumer,
d2956687 1888 /* is_metadata_channel */ true);
5c408ad8 1889 if (ret < 0) {
7e0de219 1890 status = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
5c408ad8
JD
1891 goto error;
1892 }
1893 }
1894
5c408ad8
JD
1895error:
1896 rcu_read_unlock();
d5a1b7aa 1897 return status;
5c408ad8 1898}
d2956687
JG
1899
1900enum lttng_error_code kernel_create_channel_subdirectories(
1901 const struct ltt_kernel_session *ksess)
1902{
1903 enum lttng_error_code ret = LTTNG_OK;
1904 enum lttng_trace_chunk_status chunk_status;
1905
1906 rcu_read_lock();
1907 assert(ksess->current_trace_chunk);
1908
1909 /*
1910 * Create the index subdirectory which will take care
1911 * of implicitly creating the channel's path.
1912 */
1913 chunk_status = lttng_trace_chunk_create_subdirectory(
1914 ksess->current_trace_chunk,
1915 DEFAULT_KERNEL_TRACE_DIR "/" DEFAULT_INDEX_DIR);
1916 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
1917 ret = LTTNG_ERR_CREATE_DIR_FAIL;
1918 goto error;
1919 }
1920error:
1921 rcu_read_unlock();
1922 return ret;
1923}
7d268848
MD
1924
1925/*
1926 * Setup necessary data for kernel tracer action.
1927 */
1928LTTNG_HIDDEN
1929int init_kernel_tracer(void)
1930{
1931 int ret;
1932 bool is_root = !getuid();
1933
1934 /* Modprobe lttng kernel modules */
1935 ret = modprobe_lttng_control();
1936 if (ret < 0) {
1937 goto error;
1938 }
1939
1940 /* Open debugfs lttng */
1941 kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
1942 if (kernel_tracer_fd < 0) {
1943 DBG("Failed to open %s", module_proc_lttng);
1944 goto error_open;
1945 }
1946
1947 /* Validate kernel version */
1948 ret = kernel_validate_version(&kernel_tracer_version,
1949 &kernel_tracer_abi_version);
1950 if (ret < 0) {
1951 goto error_version;
1952 }
1953
1954 ret = modprobe_lttng_data();
1955 if (ret < 0) {
1956 goto error_modules;
1957 }
1958
1959 ret = kernel_supports_ring_buffer_snapshot_sample_positions();
1960 if (ret < 0) {
1961 goto error_modules;
1962 }
7d268848
MD
1963 if (ret < 1) {
1964 WARN("Kernel tracer does not support buffer monitoring. "
1965 "The monitoring timer of channels in the kernel domain "
1966 "will be set to 0 (disabled).");
1967 }
1968
5024c2ac
JR
1969 ret = kernel_create_trigger_group(&kernel_tracer_trigger_group_fd);
1970 if (ret < 0) {
1971 /* TODO: error handling if it is not supported etc. */
1972 WARN("Failed trigger group creation");
1973 kernel_tracer_trigger_group_fd = -1;
1974 /* This is not fatal */
1975 } else {
1976 ret = kernel_create_trigger_group_notification_fd(&kernel_tracer_trigger_group_notification_fd);
1977 if (ret < 0) {
1978 goto error_modules;
1979 }
1980 }
1981
1982 CDS_INIT_LIST_HEAD(&kernel_tracer_token_list.head);
1983
7d268848 1984 DBG("Kernel tracer fd %d", kernel_tracer_fd);
5024c2ac
JR
1985 DBG("Kernel tracer trigger group fd %d", kernel_tracer_trigger_group_fd);
1986 DBG("Kernel tracer trigger group notificationi fd %d", kernel_tracer_trigger_group_notification_fd);
7d268848
MD
1987
1988 ret = syscall_init_table(kernel_tracer_fd);
1989 if (ret < 0) {
1990 ERR("Unable to populate syscall table. Syscall tracing won't "
1991 "work for this session daemon.");
1992 }
5024c2ac 1993
7d268848
MD
1994 return 0;
1995
1996error_version:
1997 modprobe_remove_lttng_control();
1998 ret = close(kernel_tracer_fd);
1999 if (ret) {
2000 PERROR("close");
2001 }
2002 kernel_tracer_fd = -1;
2003 return LTTNG_ERR_KERN_VERSION;
2004
2005error_modules:
2006 ret = close(kernel_tracer_fd);
2007 if (ret) {
2008 PERROR("close");
2009 }
2010
2011error_open:
2012 modprobe_remove_lttng_control();
2013
2014error:
2015 WARN("No kernel tracer available");
2016 kernel_tracer_fd = -1;
2017 if (!is_root) {
2018 return LTTNG_ERR_NEED_ROOT_SESSIOND;
2019 } else {
2020 return LTTNG_ERR_KERN_NA;
2021 }
2022}
2023
2024LTTNG_HIDDEN
2025void cleanup_kernel_tracer(void)
2026{
2027 int ret;
2028
5024c2ac
JR
2029 struct ltt_kernel_token_event_rule *rule, *rtmp;
2030 cds_list_for_each_entry_safe(rule, rtmp, &kernel_tracer_token_list.head, list) {
2031 kernel_disable_token_event_rule(rule);
2032 trace_kernel_destroy_token_event_rule(rule);
2033 }
2034
2035 DBG2("Closing kernel trigger group notification fd");
2036 if (kernel_tracer_trigger_group_notification_fd >= 0) {
2037 ret = close(kernel_tracer_trigger_group_notification_fd);
2038 if (ret) {
2039 PERROR("close");
2040 }
2041 kernel_tracer_trigger_group_notification_fd = -1;
2042 }
2043
2044 /* TODO: do we iterate over the list to remove all token? */
2045 DBG2("Closing kernel trigger group fd");
2046 if (kernel_tracer_trigger_group_fd >= 0) {
2047 ret = close(kernel_tracer_trigger_group_fd);
2048 if (ret) {
2049 PERROR("close");
2050 }
2051 kernel_tracer_trigger_group_fd = -1;
2052 }
2053
7d268848
MD
2054 DBG2("Closing kernel fd");
2055 if (kernel_tracer_fd >= 0) {
2056 ret = close(kernel_tracer_fd);
2057 if (ret) {
2058 PERROR("close");
2059 }
2060 kernel_tracer_fd = -1;
2061 }
5024c2ac 2062
7d268848
MD
2063 DBG("Unloading kernel modules");
2064 modprobe_remove_lttng_all();
2065 free(syscall_table);
2066}
2067
2068LTTNG_HIDDEN
2069bool kernel_tracer_is_initialized(void)
2070{
2071 return kernel_tracer_fd >= 0;
2072}
bca212a2
MD
2073
2074/*
2075 * Clear a kernel session.
2076 *
2077 * Return LTTNG_OK on success or else an LTTng error code.
2078 */
2079enum lttng_error_code kernel_clear_session(struct ltt_session *session)
2080{
2081 int ret;
2082 enum lttng_error_code status = LTTNG_OK;
2083 struct consumer_socket *socket;
2084 struct lttng_ht_iter iter;
2085 struct ltt_kernel_session *ksess = session->kernel_session;
2086
2087 assert(ksess);
2088 assert(ksess->consumer);
2089
2090 DBG("Clear kernel session %s (session %" PRIu64 ")",
2091 session->name, session->id);
2092
2093 rcu_read_lock();
2094
2095 if (ksess->active) {
2096 ERR("Expecting inactive session %s (%" PRIu64 ")", session->name, session->id);
2097 status = LTTNG_ERR_FATAL;
2098 goto end;
2099 }
2100
2101 /*
2102 * Note that this loop will end after one iteration given that there is
2103 * only one kernel consumer.
2104 */
2105 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
2106 socket, node.node) {
2107 struct ltt_kernel_channel *chan;
2108
2109 /* For each channel, ask the consumer to clear it. */
2110 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
2111 DBG("Clear kernel channel %" PRIu64 ", session %s",
2112 chan->key, session->name);
2113 ret = consumer_clear_channel(socket, chan->key);
2114 if (ret < 0) {
2115 goto error;
2116 }
2117 }
2118
2119 if (!ksess->metadata) {
2120 /*
2121 * Nothing to do for the metadata.
2122 * This is a snapshot session.
2123 * The metadata is genererated on the fly.
2124 */
2125 continue;
2126 }
2127
2128 /*
2129 * Clear the metadata channel.
2130 * Metadata channel is not cleared per se but we still need to
2131 * perform a rotation operation on it behind the scene.
2132 */
2133 ret = consumer_clear_channel(socket, ksess->metadata->key);
2134 if (ret < 0) {
2135 goto error;
2136 }
2137 }
2138
2139 goto end;
2140error:
2141 switch (-ret) {
2142 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED:
2143 status = LTTNG_ERR_CLEAR_RELAY_DISALLOWED;
2144 break;
2145 default:
2146 status = LTTNG_ERR_CLEAR_FAIL_CONSUMER;
2147 break;
2148 }
2149end:
2150 rcu_read_unlock();
2151 return status;
2152}
5024c2ac
JR
2153
2154enum lttng_error_code kernel_create_trigger_group_notification_fd(
2155 int *trigger_group_notification_fd)
2156{
2157 enum lttng_error_code ret = LTTNG_OK;
2158 int local_fd = -1;
2159
2160 assert(trigger_group_notification_fd);
2161
2162 ret = kernctl_create_trigger_group_notification_fd(kernel_tracer_trigger_group_fd);
2163 if (ret < 0) {
2164 PERROR("ioctl kernel create trigger group");
2165 ret = -1;
2166 goto error;
2167 }
2168
2169 /* Store locally */
2170 local_fd = ret;
2171
2172 /* Prevent fd duplication after execlp() */
2173 ret = fcntl(local_fd, F_SETFD, FD_CLOEXEC);
2174 if (ret < 0) {
2175 PERROR("fcntl session fd");
2176 }
2177
2178 DBG("Kernel trigger group notification created (fd: %d)",
2179 local_fd);
2180 ret = 0;
2181
2182error:
2183 *trigger_group_notification_fd = local_fd;
2184 return ret;
2185}
2186
2187enum lttng_error_code kernel_destroy_trigger_group_notification_fd(
2188 int trigger_group_notification_fd)
2189{
2190 enum lttng_error_code ret = LTTNG_OK;
2191 DBG("Closing trigger group notification fd %d", trigger_group_notification_fd);
2192 if (trigger_group_notification_fd >= 0) {
2193 ret = close(trigger_group_notification_fd);
2194 if (ret) {
2195 PERROR("close");
2196 }
2197 }
2198 return ret;
2199}
2200
2201static int kernel_create_token_event_rule(struct lttng_trigger *trigger,
2202 const struct lttng_credentials *creds, uint64_t token)
2203{
2204 int err, fd;
2205 enum lttng_error_code ret;
2206 struct ltt_kernel_token_event_rule *event;
2207 struct lttng_kernel_trigger kernel_trigger;
2208 unsigned int capture_bytecode_count = 0;
2209 struct lttng_condition *condition = NULL;
2210 struct lttng_event_rule *event_rule = NULL;
2211
2212 assert(trigger);
2213
2214 condition = lttng_trigger_get_condition(trigger);
2215 assert(condition);
2216 assert(lttng_condition_get_type(condition) == LTTNG_CONDITION_TYPE_EVENT_RULE_HIT);
2217
2218 lttng_condition_event_rule_get_rule_no_const(condition, &event_rule);
2219 assert(event_rule);
2220 assert(lttng_event_rule_get_type(event_rule) != LTTNG_EVENT_RULE_TYPE_UNKNOWN);
2221
2222 /* TODO: Should we check that the event-rule is kernel oriented here? */
2223
2224 ret = trace_kernel_create_token_event_rule(event_rule, token, &event);
2225 if (ret != LTTNG_OK) {
2226 goto error;
2227 }
2228
2229 trace_kernel_init_trigger_from_event_rule(event->event_rule, &kernel_trigger);
2230 kernel_trigger.id = event->token;
2231
2232 fd = kernctl_create_trigger(kernel_tracer_trigger_group_fd, &kernel_trigger);
2233 if (fd < 0) {
2234 switch (-fd) {
2235 case EEXIST:
2236 ret = LTTNG_ERR_KERN_EVENT_EXIST;
2237 break;
2238 case ENOSYS:
2239 WARN("Trigger type not implemented");
2240 ret = LTTNG_ERR_KERN_EVENT_ENOSYS;
2241 break;
2242 case ENOENT:
2243 WARN("Event %s not found!", kernel_trigger.name);
2244 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
2245 break;
2246 default:
2247 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
2248 PERROR("create trigger ioctl");
2249 }
2250 goto free_event;
2251 }
2252
2253 event->fd = fd;
2254 /* Prevent fd duplication after execlp() */
2255 err = fcntl(event->fd, F_SETFD, FD_CLOEXEC);
2256 if (err < 0) {
2257 PERROR("fcntl session fd");
2258 }
2259
2260 if (event->filter) {
2261 err = kernctl_filter(event->fd, event->filter);
2262 if (err < 0) {
2263 switch (-err) {
2264 case ENOMEM:
2265 ret = LTTNG_ERR_FILTER_NOMEM;
2266 break;
2267 default:
2268 ret = LTTNG_ERR_FILTER_INVAL;
2269 break;
2270 }
2271 goto filter_error;
2272 }
2273 }
2274
2275 if (lttng_event_rule_get_type(event->event_rule) ==
2276 LTTNG_EVENT_RULE_TYPE_UPROBE) {
2277 ret = userspace_probe_event_rule_add_callsites(
2278 event->event_rule, creds, event->fd);
2279 if (ret) {
2280 goto add_callsite_error;
2281 }
2282 }
2283
2284 /* Set the capture bytecode */
2285 capture_bytecode_count = lttng_trigger_get_capture_bytecode_count(trigger);
2286 for (unsigned int i = 0; i < capture_bytecode_count; i++) {
2287 const struct lttng_bytecode *capture_bytecode = lttng_trigger_get_capture_bytecode_at_index(trigger, i);
2288 ret = kernctl_capture(event->fd, capture_bytecode);
2289 if (ret < 0) {
2290 goto error;
2291 }
2292 }
2293
2294 err = kernctl_enable(event->fd);
2295 if (err < 0) {
2296 switch (-err) {
2297 case EEXIST:
2298 ret = LTTNG_ERR_KERN_EVENT_EXIST;
2299 break;
2300 default:
2301 PERROR("enable kernel trigger");
2302 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
2303 break;
2304 }
2305 goto enable_error;
2306 }
2307
2308 /* Add event to event list */
2309 cds_list_add(&event->list, &kernel_tracer_token_list.head);
2310
2311 DBG("Trigger %s created (fd: %d)", kernel_trigger.name, event->fd);
2312
2313 return 0;
2314
2315add_callsite_error:
2316enable_error:
2317filter_error:
2318 {
2319 int closeret;
2320
2321 closeret = close(event->fd);
2322 if (closeret) {
2323 PERROR("close event fd");
2324 }
2325 }
2326free_event:
2327 free(event);
2328error:
2329 return ret;
2330}
2331
2332enum lttng_error_code kernel_update_tokens(void)
2333{
2334 enum lttng_error_code ret = LTTNG_OK;
2335 enum lttng_trigger_status t_status;
2336 struct ltt_kernel_token_event_rule *token_event_rule_element;
2337 struct lttng_triggers *triggers;
2338 unsigned int count;
2339
2340 /* TODO error handling */
2341
2342 /* Get list of token trigger from the notification thread here */
2343 rcu_read_lock();
2344 pthread_mutex_lock(&notification_trigger_tokens_ht_lock);
2345 ret = notification_thread_command_get_tokens(notification_thread_handle, &triggers);
2346 if (ret != LTTNG_OK) {
2347 ret = -1;
2348 goto end;
2349 }
2350
2351 assert(triggers);
2352
2353 t_status = lttng_triggers_get_count(triggers, &count);
2354 if (t_status != LTTNG_TRIGGER_STATUS_OK) {
2355 ret = -1;
2356 goto end;
2357 }
2358
2359 for (unsigned int i = 0; i < count; i++) {
2360 struct lttng_condition *condition;
2361 struct lttng_event_rule *event_rule;
2362 struct lttng_trigger *trigger;
2363 struct ltt_kernel_token_event_rule *k_token;
2364 const struct lttng_credentials *creds;
2365 uint64_t token;
2366
2367 trigger = lttng_triggers_get_pointer_of_index(triggers, i);
2368 assert(trigger);
2369
2370 /* TODO: error checking and type checking */
2371 token = lttng_trigger_get_key(trigger);
2372 condition = lttng_trigger_get_condition(trigger);
2373 (void) lttng_condition_event_rule_get_rule_no_const(condition, &event_rule);
2374
2375 if (lttng_event_rule_get_domain_type(event_rule) != LTTNG_DOMAIN_KERNEL) {
2376 /* Skip ust related trigger */
2377 continue;
2378 }
2379
2380 creds = lttng_trigger_get_credentials(trigger);
2381 /* Iterate over all known token trigger */
2382 k_token = trace_kernel_find_trigger_by_token(&kernel_tracer_token_list, token);
2383 if (!k_token) {
2384 ret = kernel_create_token_event_rule(trigger, creds, token);
2385 if (ret < 0) {
2386 goto end;
2387 }
2388 }
2389 }
2390
2391 /* Remove all unknown trigger from the app
2392 * TODO find a way better way then this, do it on the unregister command
2393 * and be specific on the token to remove instead of going over all
2394 * trigger known to the app. This is sub optimal.
2395 */
2396 cds_list_for_each_entry (token_event_rule_element, &kernel_tracer_token_list.head,
2397 list) {
2398 uint64_t token;
2399 bool found = false;
2400
2401 token = token_event_rule_element->token;
2402
2403 /*
2404 * Check if the app event trigger still exists on the
2405 * notification side.
2406 * TODO: might want to change the backing data struct of the
2407 * lttng_triggers object to allow quick lookup?
2408 * For kernel mostly all of this can be removed once we delete
2409 * on a per trigger basis.
2410 */
2411
2412 for (unsigned int i = 0; i < count; i++) {
2413 struct lttng_trigger *trigger;
2414 uint64_t inner_token;
2415
2416 trigger = lttng_triggers_get_pointer_of_index(
2417 triggers, i);
2418 assert(trigger);
2419
2420 inner_token = lttng_trigger_get_key(trigger);
2421
2422 if (inner_token == token) {
2423 found = true;
2424 break;
2425 }
2426 }
2427
2428 if (found) {
2429 /* Still valid */
2430 continue;
2431 }
2432
2433 kernel_disable_token_event_rule(token_event_rule_element);
2434 trace_kernel_destroy_token_event_rule(token_event_rule_element);
2435 }
2436end:
2437 lttng_triggers_destroy(triggers);
2438 rcu_read_unlock();
2439 pthread_mutex_unlock(&notification_trigger_tokens_ht_lock);
2440 return ret;
2441
2442}
2443
2444int kernel_get_notification_fd(void)
2445{
2446 return kernel_tracer_trigger_group_notification_fd;
2447}
This page took 0.205455 seconds and 5 git commands to generate.