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