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