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