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