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