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