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