Commit | Line | Data |
---|---|---|
20fe2104 | 1 | /* |
21cf9b6b | 2 | * Copyright (C) 2011 EfficiOS Inc. |
20fe2104 | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
20fe2104 | 5 | * |
20fe2104 DG |
6 | */ |
7 | ||
6c1c0768 | 8 | #define _LGPL_SOURCE |
7b395890 | 9 | #include <fcntl.h> |
20fe2104 DG |
10 | #include <stdlib.h> |
11 | #include <stdio.h> | |
f34daff7 | 12 | #include <string.h> |
8c0faa1d | 13 | #include <unistd.h> |
77c7c900 | 14 | #include <inttypes.h> |
7d268848 | 15 | #include <sys/types.h> |
20fe2104 | 16 | |
990570ed | 17 | #include <common/common.h> |
352b58f5 | 18 | #include <common/hashtable/utils.h> |
82b69413 | 19 | #include <common/trace-chunk.h> |
db758600 | 20 | #include <common/kernel-ctl/kernel-ctl.h> |
c052142c | 21 | #include <common/kernel-ctl/kernel-ioctl.h> |
42224349 | 22 | #include <common/sessiond-comm/sessiond-comm.h> |
c5bf436c JG |
23 | #include <common/tracker.h> |
24 | #include <common/utils.h> | |
25 | #include <lttng/event.h> | |
26 | #include <lttng/lttng-error.h> | |
27 | #include <lttng/tracker.h> | |
1e307fab | 28 | |
352b58f5 JR |
29 | #include <lttng/userspace-probe.h> |
30 | #include <lttng/userspace-probe-internal.h> | |
670a26e4 JR |
31 | #include <lttng/condition/event-rule-matches.h> |
32 | #include <lttng/condition/event-rule-matches-internal.h> | |
352b58f5 JR |
33 | #include <lttng/event-rule/event-rule.h> |
34 | #include <lttng/event-rule/event-rule-internal.h> | |
46fd07ac | 35 | #include <lttng/event-rule/kernel-uprobe-internal.h> |
352b58f5 | 36 | |
90aa04a1 | 37 | #include "event-notifier-error-accounting.h" |
7d268848 MD |
38 | #include "lttng-sessiond.h" |
39 | #include "lttng-syscall.h" | |
352b58f5 | 40 | #include "condition-internal.h" |
2f77fc4b | 41 | #include "consumer.h" |
4771f025 | 42 | #include "kernel.h" |
6dc3064a | 43 | #include "kernel-consumer.h" |
096102bd | 44 | #include "kern-modules.h" |
90aa04a1 | 45 | #include "sessiond-config.h" |
834978fd | 46 | #include "utils.h" |
5c408ad8 | 47 | #include "rotate.h" |
7d268848 | 48 | #include "modprobe.h" |
c5bf436c | 49 | #include "tracker.h" |
352b58f5 | 50 | #include "notification-thread-commands.h" |
20fe2104 | 51 | |
e1f3997a JD |
52 | /* |
53 | * Key used to reference a channel between the sessiond and the consumer. This | |
54 | * is only read and updated with the session_list lock held. | |
55 | */ | |
56 | static uint64_t next_kernel_channel_key; | |
57 | ||
7d268848 MD |
58 | static const char *module_proc_lttng = "/proc/lttng"; |
59 | ||
60 | static int kernel_tracer_fd = -1; | |
352b58f5 JR |
61 | static int kernel_tracer_event_notifier_group_fd = -1; |
62 | static int kernel_tracer_event_notifier_group_notification_fd = -1; | |
63 | static struct cds_lfht *kernel_token_to_event_notifier_rule_ht; | |
7d268848 | 64 | |
d65106b1 | 65 | /* |
050349bb | 66 | * Add context on a kernel channel. |
df3c77c8 JG |
67 | * |
68 | * Assumes the ownership of ctx. | |
d65106b1 DG |
69 | */ |
70 | int kernel_add_channel_context(struct ltt_kernel_channel *chan, | |
645328ae | 71 | struct ltt_kernel_context *ctx) |
d65106b1 DG |
72 | { |
73 | int ret; | |
74 | ||
a0377dfe FD |
75 | LTTNG_ASSERT(chan); |
76 | LTTNG_ASSERT(ctx); | |
0525e9ae | 77 | |
d65106b1 | 78 | DBG("Adding context to channel %s", chan->channel->name); |
645328ae | 79 | ret = kernctl_add_context(chan->fd, &ctx->ctx); |
d65106b1 | 80 | if (ret < 0) { |
32af2c95 | 81 | switch (-ret) { |
1ae5e83e JD |
82 | case ENOSYS: |
83 | /* Exists but not available for this kernel */ | |
84 | ret = LTTNG_ERR_KERN_CONTEXT_UNAVAILABLE; | |
85 | goto error; | |
86 | case EEXIST: | |
b579acd9 DG |
87 | /* If EEXIST, we just ignore the error */ |
88 | ret = 0; | |
1ae5e83e JD |
89 | goto end; |
90 | default: | |
91 | PERROR("add context ioctl"); | |
92 | ret = LTTNG_ERR_KERN_CONTEXT_FAIL; | |
93 | goto error; | |
b579acd9 | 94 | } |
d65106b1 | 95 | } |
21ed98c1 | 96 | ret = 0; |
d65106b1 | 97 | |
1ae5e83e | 98 | end: |
645328ae | 99 | cds_list_add_tail(&ctx->list, &chan->ctx_list); |
ba985c3a | 100 | ctx->in_list = true; |
df3c77c8 | 101 | ctx = NULL; |
d65106b1 | 102 | error: |
df3c77c8 JG |
103 | if (ctx) { |
104 | trace_kernel_destroy_context(ctx); | |
105 | } | |
d65106b1 DG |
106 | return ret; |
107 | } | |
108 | ||
20fe2104 | 109 | /* |
050349bb DG |
110 | * Create a new kernel session, register it to the kernel tracer and add it to |
111 | * the session daemon session. | |
20fe2104 | 112 | */ |
7d268848 | 113 | int kernel_create_session(struct ltt_session *session) |
20fe2104 DG |
114 | { |
115 | int ret; | |
116 | struct ltt_kernel_session *lks; | |
117 | ||
a0377dfe | 118 | LTTNG_ASSERT(session); |
0525e9ae | 119 | |
54012638 | 120 | /* Allocate data structure */ |
dec56f6c | 121 | lks = trace_kernel_create_session(); |
20fe2104 | 122 | if (lks == NULL) { |
54012638 | 123 | ret = -1; |
20fe2104 DG |
124 | goto error; |
125 | } | |
126 | ||
54012638 | 127 | /* Kernel tracer session creation */ |
7d268848 | 128 | ret = kernctl_create_session(kernel_tracer_fd); |
20fe2104 | 129 | if (ret < 0) { |
df0f840b | 130 | PERROR("ioctl kernel create session"); |
20fe2104 DG |
131 | goto error; |
132 | } | |
133 | ||
20fe2104 | 134 | lks->fd = ret; |
7b395890 DG |
135 | /* Prevent fd duplication after execlp() */ |
136 | ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC); | |
137 | if (ret < 0) { | |
df0f840b | 138 | PERROR("fcntl session fd"); |
7b395890 DG |
139 | } |
140 | ||
53632229 | 141 | lks->id = session->id; |
3bd1e081 | 142 | lks->consumer_fds_sent = 0; |
8c0faa1d | 143 | session->kernel_session = lks; |
8c0faa1d DG |
144 | |
145 | DBG("Kernel session created (fd: %d)", lks->fd); | |
20fe2104 | 146 | |
8ff4109e JR |
147 | /* |
148 | * This is necessary since the creation time is present in the session | |
149 | * name when it is generated. | |
150 | */ | |
151 | if (session->has_auto_generated_name) { | |
152 | ret = kernctl_session_set_name(lks->fd, DEFAULT_SESSION_NAME); | |
153 | } else { | |
154 | ret = kernctl_session_set_name(lks->fd, session->name); | |
155 | } | |
156 | if (ret) { | |
157 | WARN("Could not set kernel session name for session %" PRIu64 " name: %s", | |
158 | session->id, session->name); | |
159 | } | |
160 | ||
e04b2181 JR |
161 | ret = kernctl_session_set_creation_time(lks->fd, session->creation_time); |
162 | if (ret) { | |
163 | WARN("Could not set kernel session creation time for session %" PRIu64 " name: %s", | |
164 | session->id, session->name); | |
165 | } | |
166 | ||
20fe2104 DG |
167 | return 0; |
168 | ||
169 | error: | |
5f62c685 DG |
170 | if (lks) { |
171 | trace_kernel_destroy_session(lks); | |
d070c424 | 172 | trace_kernel_free_session(lks); |
5f62c685 | 173 | } |
20fe2104 DG |
174 | return ret; |
175 | } | |
176 | ||
177 | /* | |
050349bb DG |
178 | * Create a kernel channel, register it to the kernel tracer and add it to the |
179 | * kernel session. | |
20fe2104 | 180 | */ |
050349bb | 181 | int kernel_create_channel(struct ltt_kernel_session *session, |
fdd9eb17 | 182 | struct lttng_channel *chan) |
20fe2104 DG |
183 | { |
184 | int ret; | |
185 | struct ltt_kernel_channel *lkc; | |
20fe2104 | 186 | |
a0377dfe FD |
187 | LTTNG_ASSERT(session); |
188 | LTTNG_ASSERT(chan); | |
0525e9ae | 189 | |
54012638 | 190 | /* Allocate kernel channel */ |
fdd9eb17 | 191 | lkc = trace_kernel_create_channel(chan); |
54012638 | 192 | if (lkc == NULL) { |
20fe2104 DG |
193 | goto error; |
194 | } | |
195 | ||
ecc48a90 | 196 | DBG3("Kernel create channel %s with attr: %d, %" PRIu64 ", %" PRIu64 ", %u, %u, %d, %d", |
fdd9eb17 | 197 | chan->name, lkc->channel->attr.overwrite, |
173af62f DG |
198 | lkc->channel->attr.subbuf_size, lkc->channel->attr.num_subbuf, |
199 | lkc->channel->attr.switch_timer_interval, lkc->channel->attr.read_timer_interval, | |
ecc48a90 | 200 | lkc->channel->attr.live_timer_interval, lkc->channel->attr.output); |
173af62f | 201 | |
54012638 | 202 | /* Kernel tracer channel creation */ |
f3ed775e | 203 | ret = kernctl_create_channel(session->fd, &lkc->channel->attr); |
20fe2104 | 204 | if (ret < 0) { |
df0f840b | 205 | PERROR("ioctl kernel create channel"); |
20fe2104 DG |
206 | goto error; |
207 | } | |
208 | ||
54012638 | 209 | /* Setup the channel fd */ |
20fe2104 | 210 | lkc->fd = ret; |
7b395890 DG |
211 | /* Prevent fd duplication after execlp() */ |
212 | ret = fcntl(lkc->fd, F_SETFD, FD_CLOEXEC); | |
213 | if (ret < 0) { | |
df0f840b | 214 | PERROR("fcntl session fd"); |
7b395890 DG |
215 | } |
216 | ||
54012638 | 217 | /* Add channel to session */ |
8c0faa1d DG |
218 | cds_list_add(&lkc->list, &session->channel_list.head); |
219 | session->channel_count++; | |
fb5f35b6 | 220 | lkc->session = session; |
e1f3997a | 221 | lkc->key = ++next_kernel_channel_key; |
20fe2104 | 222 | |
e1f3997a JD |
223 | DBG("Kernel channel %s created (fd: %d, key: %" PRIu64 ")", |
224 | lkc->channel->name, lkc->fd, lkc->key); | |
20fe2104 DG |
225 | |
226 | return 0; | |
227 | ||
228 | error: | |
5f62c685 DG |
229 | if (lkc) { |
230 | free(lkc->channel); | |
231 | free(lkc); | |
232 | } | |
54012638 | 233 | return -1; |
20fe2104 | 234 | } |
f34daff7 | 235 | |
352b58f5 JR |
236 | /* |
237 | * Create a kernel event notifier group, register it to the kernel tracer and | |
238 | * add it to the kernel session. | |
239 | */ | |
240 | static int kernel_create_event_notifier_group(int *event_notifier_group_fd) | |
241 | { | |
242 | int ret; | |
243 | int local_fd = -1; | |
244 | ||
a0377dfe | 245 | LTTNG_ASSERT(event_notifier_group_fd); |
352b58f5 JR |
246 | |
247 | /* Kernel event notifier group creation. */ | |
248 | ret = kernctl_create_event_notifier_group(kernel_tracer_fd); | |
249 | if (ret < 0) { | |
250 | PERROR("Failed to create kernel event notifier group"); | |
251 | ret = -1; | |
252 | goto error; | |
253 | } | |
254 | ||
255 | local_fd = ret; | |
256 | ||
257 | /* Prevent fd duplication after execlp(). */ | |
258 | ret = fcntl(local_fd, F_SETFD, FD_CLOEXEC); | |
259 | if (ret < 0) { | |
260 | PERROR("Failed to set FD_CLOEXEC on kernel event notifier group file descriptor: fd = %d", | |
261 | local_fd); | |
262 | goto error; | |
263 | } | |
264 | ||
265 | DBG("Created kernel event notifier group: fd = %d", local_fd); | |
266 | *event_notifier_group_fd = local_fd; | |
267 | local_fd = -1; | |
268 | ret = 0; | |
269 | error: | |
270 | if (local_fd >= 0) { | |
271 | ret = close(local_fd); | |
272 | if (ret) { | |
273 | PERROR("Failed to close kernel event notifier group file descriptor: fd = %d", | |
274 | local_fd); | |
275 | } | |
276 | } | |
277 | ||
278 | return ret; | |
279 | } | |
280 | ||
410b78a0 FD |
281 | /* |
282 | * Compute the offset of the instrumentation byte in the binary based on the | |
283 | * function probe location using the ELF lookup method. | |
284 | * | |
285 | * Returns 0 on success and set the offset out parameter to the offset of the | |
286 | * elf symbol | |
287 | * Returns -1 on error | |
288 | */ | |
289 | static | |
290 | int extract_userspace_probe_offset_function_elf( | |
87597c2c | 291 | const struct lttng_userspace_probe_location *probe_location, |
0a398bc4 | 292 | uid_t uid, gid_t gid, uint64_t *offset) |
410b78a0 FD |
293 | { |
294 | int fd; | |
295 | int ret = 0; | |
296 | const char *symbol = NULL; | |
87597c2c | 297 | const struct lttng_userspace_probe_location_lookup_method *lookup = NULL; |
410b78a0 FD |
298 | enum lttng_userspace_probe_location_lookup_method_type lookup_method_type; |
299 | ||
a0377dfe | 300 | LTTNG_ASSERT(lttng_userspace_probe_location_get_type(probe_location) == |
410b78a0 FD |
301 | LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION); |
302 | ||
303 | lookup = lttng_userspace_probe_location_get_lookup_method( | |
304 | probe_location); | |
305 | if (!lookup) { | |
306 | ret = -1; | |
307 | goto end; | |
308 | } | |
309 | ||
310 | lookup_method_type = | |
311 | lttng_userspace_probe_location_lookup_method_get_type(lookup); | |
312 | ||
a0377dfe | 313 | LTTNG_ASSERT(lookup_method_type == |
410b78a0 FD |
314 | LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF); |
315 | ||
316 | symbol = lttng_userspace_probe_location_function_get_function_name( | |
317 | probe_location); | |
318 | if (!symbol) { | |
319 | ret = -1; | |
320 | goto end; | |
321 | } | |
322 | ||
323 | fd = lttng_userspace_probe_location_function_get_binary_fd(probe_location); | |
324 | if (fd < 0) { | |
325 | ret = -1; | |
326 | goto end; | |
327 | } | |
328 | ||
0a398bc4 | 329 | ret = run_as_extract_elf_symbol_offset(fd, symbol, uid, gid, offset); |
410b78a0 FD |
330 | if (ret < 0) { |
331 | DBG("userspace probe offset calculation failed for " | |
332 | "function %s", symbol); | |
333 | goto end; | |
334 | } | |
335 | ||
336 | DBG("userspace probe elf offset for %s is 0x%jd", symbol, (intmax_t)(*offset)); | |
337 | end: | |
338 | return ret; | |
339 | } | |
340 | ||
341 | /* | |
342 | * Compute the offsets of the instrumentation bytes in the binary based on the | |
343 | * tracepoint probe location using the SDT lookup method. This function | |
344 | * allocates the offsets buffer, the caller must free it. | |
345 | * | |
346 | * Returns 0 on success and set the offset out parameter to the offsets of the | |
347 | * SDT tracepoint. | |
348 | * Returns -1 on error. | |
349 | */ | |
350 | static | |
351 | int extract_userspace_probe_offset_tracepoint_sdt( | |
87597c2c | 352 | const struct lttng_userspace_probe_location *probe_location, |
0a398bc4 | 353 | uid_t uid, gid_t gid, uint64_t **offsets, |
410b78a0 FD |
354 | uint32_t *offsets_count) |
355 | { | |
356 | enum lttng_userspace_probe_location_lookup_method_type lookup_method_type; | |
87597c2c | 357 | const struct lttng_userspace_probe_location_lookup_method *lookup = NULL; |
410b78a0 FD |
358 | const char *probe_name = NULL, *provider_name = NULL; |
359 | int ret = 0; | |
360 | int fd, i; | |
361 | ||
a0377dfe | 362 | LTTNG_ASSERT(lttng_userspace_probe_location_get_type(probe_location) == |
410b78a0 FD |
363 | LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT); |
364 | ||
365 | lookup = lttng_userspace_probe_location_get_lookup_method(probe_location); | |
366 | if (!lookup) { | |
367 | ret = -1; | |
368 | goto end; | |
369 | } | |
370 | ||
371 | lookup_method_type = | |
372 | lttng_userspace_probe_location_lookup_method_get_type(lookup); | |
373 | ||
a0377dfe | 374 | LTTNG_ASSERT(lookup_method_type == |
410b78a0 FD |
375 | LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT); |
376 | ||
377 | ||
378 | probe_name = lttng_userspace_probe_location_tracepoint_get_probe_name( | |
379 | probe_location); | |
380 | if (!probe_name) { | |
381 | ret = -1; | |
382 | goto end; | |
383 | } | |
384 | ||
385 | provider_name = lttng_userspace_probe_location_tracepoint_get_provider_name( | |
386 | probe_location); | |
387 | if (!provider_name) { | |
388 | ret = -1; | |
389 | goto end; | |
390 | } | |
391 | ||
392 | fd = lttng_userspace_probe_location_tracepoint_get_binary_fd(probe_location); | |
393 | if (fd < 0) { | |
394 | ret = -1; | |
395 | goto end; | |
396 | } | |
397 | ||
398 | ret = run_as_extract_sdt_probe_offsets(fd, provider_name, probe_name, | |
0a398bc4 | 399 | uid, gid, offsets, offsets_count); |
410b78a0 FD |
400 | if (ret < 0) { |
401 | DBG("userspace probe offset calculation failed for sdt " | |
402 | "probe %s:%s", provider_name, probe_name); | |
403 | goto end; | |
404 | } | |
405 | ||
406 | if (*offsets_count == 0) { | |
407 | DBG("no userspace probe offset found"); | |
408 | goto end; | |
409 | } | |
410 | ||
411 | DBG("%u userspace probe SDT offsets found for %s:%s at:", | |
412 | *offsets_count, provider_name, probe_name); | |
413 | for (i = 0; i < *offsets_count; i++) { | |
414 | DBG("\t0x%jd", (intmax_t)((*offsets)[i])); | |
415 | } | |
416 | end: | |
417 | return ret; | |
418 | } | |
419 | ||
410b78a0 | 420 | static |
0a398bc4 FD |
421 | int userspace_probe_add_callsite( |
422 | const struct lttng_userspace_probe_location *location, | |
423 | uid_t uid, gid_t gid, int fd) | |
410b78a0 | 424 | { |
87597c2c | 425 | const struct lttng_userspace_probe_location_lookup_method *lookup_method = NULL; |
410b78a0 | 426 | enum lttng_userspace_probe_location_lookup_method_type type; |
410b78a0 FD |
427 | int ret; |
428 | ||
0a398bc4 | 429 | lookup_method = lttng_userspace_probe_location_get_lookup_method(location); |
410b78a0 FD |
430 | if (!lookup_method) { |
431 | ret = -1; | |
432 | goto end; | |
433 | } | |
434 | ||
435 | type = lttng_userspace_probe_location_lookup_method_get_type(lookup_method); | |
436 | switch (type) { | |
437 | case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF: | |
438 | { | |
b8e2fb80 | 439 | struct lttng_kernel_abi_event_callsite callsite; |
410b78a0 FD |
440 | uint64_t offset; |
441 | ||
0a398bc4 FD |
442 | ret = extract_userspace_probe_offset_function_elf(location, |
443 | uid, gid, &offset); | |
410b78a0 FD |
444 | if (ret) { |
445 | ret = LTTNG_ERR_PROBE_LOCATION_INVAL; | |
446 | goto end; | |
447 | } | |
448 | ||
449 | callsite.u.uprobe.offset = offset; | |
450 | ret = kernctl_add_callsite(fd, &callsite); | |
451 | if (ret) { | |
0a398bc4 | 452 | WARN("Failed to add callsite to ELF userspace probe."); |
410b78a0 FD |
453 | ret = LTTNG_ERR_KERN_ENABLE_FAIL; |
454 | goto end; | |
455 | } | |
456 | break; | |
457 | } | |
458 | case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT: | |
459 | { | |
460 | int i; | |
461 | uint64_t *offsets = NULL; | |
462 | uint32_t offsets_count; | |
b8e2fb80 | 463 | struct lttng_kernel_abi_event_callsite callsite; |
410b78a0 FD |
464 | |
465 | /* | |
466 | * This call allocates the offsets buffer. This buffer must be freed | |
467 | * by the caller | |
468 | */ | |
0a398bc4 FD |
469 | ret = extract_userspace_probe_offset_tracepoint_sdt(location, |
470 | uid, gid, &offsets, &offsets_count); | |
410b78a0 FD |
471 | if (ret) { |
472 | ret = LTTNG_ERR_PROBE_LOCATION_INVAL; | |
473 | goto end; | |
474 | } | |
475 | for (i = 0; i < offsets_count; i++) { | |
476 | callsite.u.uprobe.offset = offsets[i]; | |
477 | ret = kernctl_add_callsite(fd, &callsite); | |
478 | if (ret) { | |
0a398bc4 | 479 | WARN("Failed to add callsite to SDT userspace probe"); |
410b78a0 FD |
480 | ret = LTTNG_ERR_KERN_ENABLE_FAIL; |
481 | free(offsets); | |
482 | goto end; | |
483 | } | |
484 | } | |
485 | free(offsets); | |
486 | break; | |
487 | } | |
488 | default: | |
489 | ret = LTTNG_ERR_PROBE_LOCATION_INVAL; | |
490 | goto end; | |
491 | } | |
492 | end: | |
493 | return ret; | |
494 | } | |
495 | ||
0a398bc4 FD |
496 | /* |
497 | * Extract the offsets of the instrumentation point for the different lookup | |
498 | * methods. | |
499 | */ | |
500 | static | |
501 | int userspace_probe_event_add_callsites(struct lttng_event *ev, | |
502 | struct ltt_kernel_session *session, int fd) | |
503 | { | |
504 | int ret; | |
505 | const struct lttng_userspace_probe_location *location = NULL; | |
506 | ||
a0377dfe FD |
507 | LTTNG_ASSERT(ev); |
508 | LTTNG_ASSERT(ev->type == LTTNG_EVENT_USERSPACE_PROBE); | |
0a398bc4 FD |
509 | |
510 | location = lttng_event_get_userspace_probe_location(ev); | |
511 | if (!location) { | |
512 | ret = -1; | |
513 | goto end; | |
514 | } | |
515 | ||
516 | ret = userspace_probe_add_callsite(location, session->uid, session->gid, | |
517 | fd); | |
518 | if (ret) { | |
519 | WARN("Failed to add callsite to userspace probe event '%s'", | |
520 | ev->name); | |
521 | } | |
522 | ||
523 | end: | |
524 | return ret; | |
525 | } | |
526 | ||
352b58f5 JR |
527 | /* |
528 | * Extract the offsets of the instrumentation point for the different look-up | |
529 | * methods. | |
530 | */ | |
531 | static int userspace_probe_event_rule_add_callsites( | |
532 | const struct lttng_event_rule *rule, | |
533 | const struct lttng_credentials *creds, | |
534 | int fd) | |
535 | { | |
536 | int ret; | |
537 | enum lttng_event_rule_status status; | |
538 | enum lttng_event_rule_type event_rule_type; | |
539 | const struct lttng_userspace_probe_location *location = NULL; | |
540 | ||
a0377dfe FD |
541 | LTTNG_ASSERT(rule); |
542 | LTTNG_ASSERT(creds); | |
352b58f5 JR |
543 | |
544 | event_rule_type = lttng_event_rule_get_type(rule); | |
a0377dfe | 545 | LTTNG_ASSERT(event_rule_type == LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE); |
352b58f5 | 546 | |
46fd07ac | 547 | status = lttng_event_rule_kernel_uprobe_get_location(rule, &location); |
352b58f5 JR |
548 | if (status != LTTNG_EVENT_RULE_STATUS_OK || !location) { |
549 | ret = -1; | |
550 | goto end; | |
551 | } | |
552 | ||
553 | ret = userspace_probe_add_callsite(location, | |
554 | lttng_credentials_get_uid(creds), | |
555 | lttng_credentials_get_gid(creds), fd); | |
556 | if (ret) { | |
557 | WARN("Failed to add callsite to user space probe object: fd = %d", | |
558 | fd); | |
559 | } | |
560 | ||
561 | end: | |
562 | return ret; | |
563 | } | |
564 | ||
f34daff7 | 565 | /* |
050349bb DG |
566 | * Create a kernel event, enable it to the kernel tracer and add it to the |
567 | * channel event list of the kernel session. | |
49d21f93 | 568 | * We own filter_expression and filter. |
f34daff7 | 569 | */ |
050349bb | 570 | int kernel_create_event(struct lttng_event *ev, |
00a62084 MD |
571 | struct ltt_kernel_channel *channel, |
572 | char *filter_expression, | |
2b00d462 | 573 | struct lttng_bytecode *filter) |
f34daff7 | 574 | { |
71a3bb01 FD |
575 | int err, fd; |
576 | enum lttng_error_code ret; | |
f34daff7 | 577 | struct ltt_kernel_event *event; |
f34daff7 | 578 | |
a0377dfe FD |
579 | LTTNG_ASSERT(ev); |
580 | LTTNG_ASSERT(channel); | |
0525e9ae | 581 | |
a969e101 | 582 | /* We pass ownership of filter_expression and filter */ |
71a3bb01 FD |
583 | ret = trace_kernel_create_event(ev, filter_expression, |
584 | filter, &event); | |
585 | if (ret != LTTNG_OK) { | |
f34daff7 DG |
586 | goto error; |
587 | } | |
588 | ||
71a3bb01 FD |
589 | fd = kernctl_create_event(channel->fd, event->event); |
590 | if (fd < 0) { | |
591 | switch (-fd) { | |
bd29c13d | 592 | case EEXIST: |
71a3bb01 | 593 | ret = LTTNG_ERR_KERN_EVENT_EXIST; |
bd29c13d DG |
594 | break; |
595 | case ENOSYS: | |
596 | WARN("Event type not implemented"); | |
71a3bb01 | 597 | ret = LTTNG_ERR_KERN_EVENT_ENOSYS; |
bd29c13d | 598 | break; |
8197a339 DG |
599 | case ENOENT: |
600 | WARN("Event %s not found!", ev->name); | |
71a3bb01 | 601 | ret = LTTNG_ERR_KERN_ENABLE_FAIL; |
8197a339 | 602 | break; |
bd29c13d | 603 | default: |
71a3bb01 | 604 | ret = LTTNG_ERR_KERN_ENABLE_FAIL; |
d87bfb32 DG |
605 | PERROR("create event ioctl"); |
606 | } | |
e953ef25 | 607 | goto free_event; |
8c0faa1d | 608 | } |
f34daff7 | 609 | |
d0ae4ea8 | 610 | event->type = ev->type; |
71a3bb01 | 611 | event->fd = fd; |
7b395890 | 612 | /* Prevent fd duplication after execlp() */ |
71a3bb01 FD |
613 | err = fcntl(event->fd, F_SETFD, FD_CLOEXEC); |
614 | if (err < 0) { | |
df0f840b | 615 | PERROR("fcntl session fd"); |
7b395890 DG |
616 | } |
617 | ||
00a62084 | 618 | if (filter) { |
71a3bb01 FD |
619 | err = kernctl_filter(event->fd, filter); |
620 | if (err < 0) { | |
621 | switch (-err) { | |
622 | case ENOMEM: | |
623 | ret = LTTNG_ERR_FILTER_NOMEM; | |
624 | break; | |
625 | default: | |
626 | ret = LTTNG_ERR_FILTER_INVAL; | |
627 | break; | |
628 | } | |
00a62084 MD |
629 | goto filter_error; |
630 | } | |
631 | } | |
632 | ||
dcabc190 | 633 | if (ev->type == LTTNG_EVENT_USERSPACE_PROBE) { |
7966af57 | 634 | ret = (lttng_error_code) userspace_probe_event_add_callsites(ev, channel->session, |
0a398bc4 | 635 | event->fd); |
dcabc190 FD |
636 | if (ret) { |
637 | goto add_callsite_error; | |
638 | } | |
639 | } | |
640 | ||
71a3bb01 FD |
641 | err = kernctl_enable(event->fd); |
642 | if (err < 0) { | |
643 | switch (-err) { | |
00a62084 MD |
644 | case EEXIST: |
645 | ret = LTTNG_ERR_KERN_EVENT_EXIST; | |
646 | break; | |
647 | default: | |
648 | PERROR("enable kernel event"); | |
71a3bb01 | 649 | ret = LTTNG_ERR_KERN_ENABLE_FAIL; |
00a62084 MD |
650 | break; |
651 | } | |
652 | goto enable_error; | |
653 | } | |
654 | ||
f3ed775e DG |
655 | /* Add event to event list */ |
656 | cds_list_add(&event->list, &channel->events_list.head); | |
cbbbb275 DG |
657 | channel->event_count++; |
658 | ||
e953ef25 DG |
659 | DBG("Event %s created (fd: %d)", ev->name, event->fd); |
660 | ||
661 | return 0; | |
662 | ||
dcabc190 | 663 | add_callsite_error: |
00a62084 MD |
664 | enable_error: |
665 | filter_error: | |
666 | { | |
667 | int closeret; | |
668 | ||
669 | closeret = close(event->fd); | |
670 | if (closeret) { | |
671 | PERROR("close event fd"); | |
672 | } | |
673 | } | |
e953ef25 DG |
674 | free_event: |
675 | free(event); | |
676 | error: | |
d87bfb32 | 677 | return ret; |
e953ef25 DG |
678 | } |
679 | ||
26cc6b4e | 680 | /* |
050349bb | 681 | * Disable a kernel channel. |
26cc6b4e DG |
682 | */ |
683 | int kernel_disable_channel(struct ltt_kernel_channel *chan) | |
684 | { | |
685 | int ret; | |
686 | ||
a0377dfe | 687 | LTTNG_ASSERT(chan); |
0525e9ae | 688 | |
26cc6b4e DG |
689 | ret = kernctl_disable(chan->fd); |
690 | if (ret < 0) { | |
df0f840b | 691 | PERROR("disable chan ioctl"); |
26cc6b4e DG |
692 | goto error; |
693 | } | |
694 | ||
695 | chan->enabled = 0; | |
e1f3997a JD |
696 | DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64 ")", |
697 | chan->channel->name, chan->fd, chan->key); | |
26cc6b4e DG |
698 | |
699 | return 0; | |
700 | ||
701 | error: | |
702 | return ret; | |
703 | } | |
704 | ||
d36b8583 | 705 | /* |
050349bb | 706 | * Enable a kernel channel. |
d36b8583 DG |
707 | */ |
708 | int kernel_enable_channel(struct ltt_kernel_channel *chan) | |
709 | { | |
710 | int ret; | |
711 | ||
a0377dfe | 712 | LTTNG_ASSERT(chan); |
0525e9ae | 713 | |
d36b8583 | 714 | ret = kernctl_enable(chan->fd); |
32af2c95 | 715 | if (ret < 0 && ret != -EEXIST) { |
df0f840b | 716 | PERROR("Enable kernel chan"); |
d36b8583 DG |
717 | goto error; |
718 | } | |
719 | ||
720 | chan->enabled = 1; | |
e1f3997a JD |
721 | DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64 ")", |
722 | chan->channel->name, chan->fd, chan->key); | |
d36b8583 DG |
723 | |
724 | return 0; | |
725 | ||
726 | error: | |
727 | return ret; | |
728 | } | |
729 | ||
19e70852 | 730 | /* |
050349bb | 731 | * Enable a kernel event. |
19e70852 DG |
732 | */ |
733 | int kernel_enable_event(struct ltt_kernel_event *event) | |
734 | { | |
735 | int ret; | |
736 | ||
a0377dfe | 737 | LTTNG_ASSERT(event); |
0525e9ae | 738 | |
19e70852 | 739 | ret = kernctl_enable(event->fd); |
42224349 | 740 | if (ret < 0) { |
32af2c95 | 741 | switch (-ret) { |
42224349 | 742 | case EEXIST: |
f73fabfd | 743 | ret = LTTNG_ERR_KERN_EVENT_EXIST; |
42224349 DG |
744 | break; |
745 | default: | |
746 | PERROR("enable kernel event"); | |
747 | break; | |
748 | } | |
19e70852 DG |
749 | goto error; |
750 | } | |
751 | ||
752 | event->enabled = 1; | |
753 | DBG("Kernel event %s enabled (fd: %d)", event->event->name, event->fd); | |
754 | ||
755 | return 0; | |
756 | ||
757 | error: | |
d36b8583 | 758 | return ret; |
19e70852 DG |
759 | } |
760 | ||
e953ef25 | 761 | /* |
050349bb | 762 | * Disable a kernel event. |
e953ef25 | 763 | */ |
19e70852 | 764 | int kernel_disable_event(struct ltt_kernel_event *event) |
e953ef25 DG |
765 | { |
766 | int ret; | |
19e70852 | 767 | |
a0377dfe | 768 | LTTNG_ASSERT(event); |
0525e9ae | 769 | |
19e70852 | 770 | ret = kernctl_disable(event->fd); |
42224349 | 771 | if (ret < 0) { |
d79bd829 JG |
772 | PERROR("Failed to disable kernel event: name = '%s', fd = %d", |
773 | event->event->name, event->fd); | |
19e70852 | 774 | goto error; |
e953ef25 | 775 | } |
f3ed775e | 776 | |
19e70852 DG |
777 | event->enabled = 0; |
778 | DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd); | |
779 | ||
f34daff7 DG |
780 | return 0; |
781 | ||
782 | error: | |
d36b8583 | 783 | return ret; |
f34daff7 | 784 | } |
aaf26714 | 785 | |
352b58f5 JR |
786 | /* |
787 | * Disable a kernel event notifier. | |
788 | */ | |
789 | static | |
790 | int kernel_disable_event_notifier_rule(struct ltt_kernel_event_notifier_rule *event) | |
791 | { | |
792 | int ret; | |
793 | ||
a0377dfe | 794 | LTTNG_ASSERT(event); |
352b58f5 JR |
795 | |
796 | rcu_read_lock(); | |
797 | cds_lfht_del(kernel_token_to_event_notifier_rule_ht, &event->ht_node); | |
798 | rcu_read_unlock(); | |
799 | ||
800 | ret = kernctl_disable(event->fd); | |
801 | if (ret < 0) { | |
d79bd829 JG |
802 | PERROR("Failed to disable kernel event notifier: fd = %d, token = %" PRIu64, |
803 | event->fd, event->token); | |
352b58f5 JR |
804 | goto error; |
805 | } | |
806 | ||
807 | event->enabled = 0; | |
808 | DBG("Disabled kernel event notifier: fd = %d, token = %" PRIu64, | |
809 | event->fd, event->token); | |
810 | ||
811 | error: | |
812 | return ret; | |
813 | } | |
814 | ||
159b042f JG |
815 | static |
816 | struct process_attr_tracker *_kernel_get_process_attr_tracker( | |
55c9e7ca | 817 | struct ltt_kernel_session *session, |
159b042f | 818 | enum lttng_process_attr process_attr) |
55c9e7ca | 819 | { |
159b042f JG |
820 | switch (process_attr) { |
821 | case LTTNG_PROCESS_ATTR_PROCESS_ID: | |
822 | return session->tracker_pid; | |
823 | case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID: | |
824 | return session->tracker_vpid; | |
825 | case LTTNG_PROCESS_ATTR_USER_ID: | |
826 | return session->tracker_uid; | |
827 | case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID: | |
828 | return session->tracker_vuid; | |
829 | case LTTNG_PROCESS_ATTR_GROUP_ID: | |
830 | return session->tracker_gid; | |
831 | case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID: | |
832 | return session->tracker_vgid; | |
55c9e7ca JR |
833 | default: |
834 | return NULL; | |
835 | } | |
836 | } | |
6e911cad | 837 | |
159b042f | 838 | const struct process_attr_tracker *kernel_get_process_attr_tracker( |
55c9e7ca | 839 | struct ltt_kernel_session *session, |
159b042f | 840 | enum lttng_process_attr process_attr) |
ccf10263 | 841 | { |
159b042f JG |
842 | return (const struct process_attr_tracker *) |
843 | _kernel_get_process_attr_tracker(session, process_attr); | |
844 | } | |
7c493d31 | 845 | |
159b042f JG |
846 | enum lttng_error_code kernel_process_attr_tracker_set_tracking_policy( |
847 | struct ltt_kernel_session *session, | |
848 | enum lttng_process_attr process_attr, | |
849 | enum lttng_tracking_policy policy) | |
850 | { | |
851 | int ret; | |
852 | enum lttng_error_code ret_code = LTTNG_OK; | |
853 | struct process_attr_tracker *tracker = | |
854 | _kernel_get_process_attr_tracker(session, process_attr); | |
855 | enum lttng_tracking_policy previous_policy; | |
55c9e7ca | 856 | |
159b042f JG |
857 | if (!tracker) { |
858 | ret_code = LTTNG_ERR_INVALID; | |
859 | goto end; | |
7c493d31 | 860 | } |
ccf10263 | 861 | |
159b042f JG |
862 | previous_policy = process_attr_tracker_get_tracking_policy(tracker); |
863 | ret = process_attr_tracker_set_tracking_policy(tracker, policy); | |
864 | if (ret) { | |
865 | ret_code = LTTNG_ERR_UNK; | |
866 | goto end; | |
55c9e7ca | 867 | } |
7c493d31 | 868 | |
159b042f | 869 | if (previous_policy == policy) { |
55c9e7ca | 870 | goto end; |
7c493d31 | 871 | } |
55c9e7ca | 872 | |
159b042f JG |
873 | switch (policy) { |
874 | case LTTNG_TRACKING_POLICY_INCLUDE_ALL: | |
875 | if (process_attr == LTTNG_PROCESS_ATTR_PROCESS_ID) { | |
876 | /* | |
877 | * Maintain a special case for the process ID process | |
878 | * attribute tracker as it was the only supported | |
879 | * attribute prior to 2.12. | |
880 | */ | |
881 | ret = kernctl_track_pid(session->fd, -1); | |
882 | } else { | |
883 | ret = kernctl_track_id(session->fd, process_attr, -1); | |
55c9e7ca JR |
884 | } |
885 | break; | |
159b042f JG |
886 | case LTTNG_TRACKING_POLICY_EXCLUDE_ALL: |
887 | case LTTNG_TRACKING_POLICY_INCLUDE_SET: | |
888 | /* fall-through. */ | |
889 | if (process_attr == LTTNG_PROCESS_ATTR_PROCESS_ID) { | |
890 | /* | |
891 | * Maintain a special case for the process ID process | |
892 | * attribute tracker as it was the only supported | |
893 | * attribute prior to 2.12. | |
894 | */ | |
895 | ret = kernctl_untrack_pid(session->fd, -1); | |
896 | } else { | |
897 | ret = kernctl_untrack_id(session->fd, process_attr, -1); | |
55c9e7ca JR |
898 | } |
899 | break; | |
900 | default: | |
159b042f | 901 | abort(); |
55c9e7ca | 902 | } |
159b042f | 903 | /* kern-ctl error handling */ |
32af2c95 | 904 | switch (-ret) { |
159b042f JG |
905 | case 0: |
906 | ret_code = LTTNG_OK; | |
907 | break; | |
7c493d31 | 908 | case EINVAL: |
159b042f | 909 | ret_code = LTTNG_ERR_INVALID; |
55c9e7ca | 910 | break; |
7c493d31 | 911 | case ENOMEM: |
159b042f | 912 | ret_code = LTTNG_ERR_NOMEM; |
55c9e7ca JR |
913 | break; |
914 | case EEXIST: | |
159b042f | 915 | ret_code = LTTNG_ERR_PROCESS_ATTR_EXISTS; |
55c9e7ca | 916 | break; |
7c493d31 | 917 | default: |
159b042f | 918 | ret_code = LTTNG_ERR_UNK; |
55c9e7ca JR |
919 | break; |
920 | } | |
55c9e7ca | 921 | end: |
159b042f | 922 | return ret_code; |
ccf10263 MD |
923 | } |
924 | ||
159b042f | 925 | enum lttng_error_code kernel_process_attr_tracker_inclusion_set_add_value( |
55c9e7ca | 926 | struct ltt_kernel_session *session, |
159b042f JG |
927 | enum lttng_process_attr process_attr, |
928 | const struct process_attr_value *value) | |
a5dfbb9d | 929 | { |
159b042f JG |
930 | int ret, integral_value; |
931 | enum lttng_error_code ret_code; | |
932 | struct process_attr_tracker *tracker; | |
933 | enum process_attr_tracker_status status; | |
a5dfbb9d | 934 | |
159b042f JG |
935 | /* |
936 | * Convert process attribute tracker value to the integral | |
937 | * representation required by the kern-ctl API. | |
938 | */ | |
939 | switch (process_attr) { | |
940 | case LTTNG_PROCESS_ATTR_PROCESS_ID: | |
941 | case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID: | |
942 | integral_value = (int) value->value.pid; | |
55c9e7ca | 943 | break; |
159b042f JG |
944 | case LTTNG_PROCESS_ATTR_USER_ID: |
945 | case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID: | |
946 | if (value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME) { | |
947 | uid_t uid; | |
948 | ||
949 | ret_code = utils_user_id_from_name( | |
950 | value->value.user_name, &uid); | |
951 | if (ret_code != LTTNG_OK) { | |
952 | goto end; | |
953 | } | |
954 | integral_value = (int) uid; | |
955 | } else { | |
956 | integral_value = (int) value->value.uid; | |
55c9e7ca JR |
957 | } |
958 | break; | |
159b042f JG |
959 | case LTTNG_PROCESS_ATTR_GROUP_ID: |
960 | case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID: | |
961 | if (value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME) { | |
962 | gid_t gid; | |
963 | ||
964 | ret_code = utils_group_id_from_name( | |
965 | value->value.group_name, &gid); | |
966 | if (ret_code != LTTNG_OK) { | |
967 | goto end; | |
968 | } | |
969 | integral_value = (int) gid; | |
970 | } else { | |
971 | integral_value = (int) value->value.gid; | |
a5dfbb9d | 972 | } |
55c9e7ca JR |
973 | break; |
974 | default: | |
159b042f JG |
975 | ret_code = LTTNG_ERR_INVALID; |
976 | goto end; | |
55c9e7ca JR |
977 | } |
978 | ||
159b042f JG |
979 | tracker = _kernel_get_process_attr_tracker(session, process_attr); |
980 | if (!tracker) { | |
981 | ret_code = LTTNG_ERR_INVALID; | |
982 | goto end; | |
983 | } | |
984 | ||
985 | status = process_attr_tracker_inclusion_set_add_value(tracker, value); | |
986 | if (status != PROCESS_ATTR_TRACKER_STATUS_OK) { | |
987 | switch (status) { | |
988 | case PROCESS_ATTR_TRACKER_STATUS_EXISTS: | |
989 | ret_code = LTTNG_ERR_PROCESS_ATTR_EXISTS; | |
990 | break; | |
991 | case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY: | |
992 | ret_code = LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY; | |
993 | break; | |
994 | case PROCESS_ATTR_TRACKER_STATUS_ERROR: | |
995 | default: | |
996 | ret_code = LTTNG_ERR_UNK; | |
997 | break; | |
998 | } | |
999 | goto end; | |
1000 | } | |
1001 | ||
1002 | DBG("Kernel track %s %d for session id %" PRIu64, | |
1003 | lttng_process_attr_to_string(process_attr), | |
1004 | integral_value, session->id); | |
1005 | if (process_attr == LTTNG_PROCESS_ATTR_PROCESS_ID) { | |
1006 | /* | |
1007 | * Maintain a special case for the process ID process attribute | |
1008 | * tracker as it was the only supported attribute prior to 2.12. | |
1009 | */ | |
1010 | ret = kernctl_track_pid(session->fd, integral_value); | |
1011 | } else { | |
1012 | ret = kernctl_track_id( | |
1013 | session->fd, process_attr, integral_value); | |
1014 | } | |
1015 | if (ret == 0) { | |
1016 | ret_code = LTTNG_OK; | |
1017 | goto end; | |
1018 | } | |
1019 | ||
1020 | kernel_wait_quiescent(); | |
1021 | ||
1022 | /* kern-ctl error handling */ | |
55c9e7ca | 1023 | switch (-ret) { |
159b042f JG |
1024 | case 0: |
1025 | ret_code = LTTNG_OK; | |
1026 | break; | |
55c9e7ca | 1027 | case EINVAL: |
159b042f | 1028 | ret_code = LTTNG_ERR_INVALID; |
55c9e7ca JR |
1029 | break; |
1030 | case ENOMEM: | |
159b042f | 1031 | ret_code = LTTNG_ERR_NOMEM; |
55c9e7ca JR |
1032 | break; |
1033 | case EEXIST: | |
159b042f | 1034 | ret_code = LTTNG_ERR_PROCESS_ATTR_EXISTS; |
55c9e7ca JR |
1035 | break; |
1036 | default: | |
159b042f | 1037 | ret_code = LTTNG_ERR_UNK; |
55c9e7ca | 1038 | break; |
a5dfbb9d MD |
1039 | } |
1040 | ||
159b042f JG |
1041 | /* Attempt to remove the value from the tracker. */ |
1042 | status = process_attr_tracker_inclusion_set_remove_value( | |
1043 | tracker, value); | |
1044 | if (status != PROCESS_ATTR_TRACKER_STATUS_OK) { | |
1045 | ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error", | |
1046 | lttng_process_attr_to_string(process_attr), | |
1047 | integral_value); | |
55c9e7ca | 1048 | } |
a5dfbb9d | 1049 | end: |
159b042f | 1050 | return ret_code; |
55c9e7ca | 1051 | } |
a5dfbb9d | 1052 | |
159b042f | 1053 | enum lttng_error_code kernel_process_attr_tracker_inclusion_set_remove_value( |
55c9e7ca | 1054 | struct ltt_kernel_session *session, |
159b042f JG |
1055 | enum lttng_process_attr process_attr, |
1056 | const struct process_attr_value *value) | |
55c9e7ca | 1057 | { |
159b042f JG |
1058 | int ret, integral_value; |
1059 | enum lttng_error_code ret_code; | |
1060 | struct process_attr_tracker *tracker; | |
1061 | enum process_attr_tracker_status status; | |
1062 | ||
1063 | /* | |
1064 | * Convert process attribute tracker value to the integral | |
1065 | * representation required by the kern-ctl API. | |
1066 | */ | |
1067 | switch (process_attr) { | |
1068 | case LTTNG_PROCESS_ATTR_PROCESS_ID: | |
1069 | case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID: | |
1070 | integral_value = (int) value->value.pid; | |
1071 | break; | |
1072 | case LTTNG_PROCESS_ATTR_USER_ID: | |
1073 | case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID: | |
1074 | if (value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME) { | |
1075 | uid_t uid; | |
1076 | ||
1077 | ret_code = utils_user_id_from_name( | |
1078 | value->value.user_name, &uid); | |
1079 | if (ret_code != LTTNG_OK) { | |
1080 | goto end; | |
1081 | } | |
1082 | integral_value = (int) uid; | |
1083 | } else { | |
1084 | integral_value = (int) value->value.uid; | |
1085 | } | |
1086 | break; | |
1087 | case LTTNG_PROCESS_ATTR_GROUP_ID: | |
1088 | case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID: | |
1089 | if (value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME) { | |
1090 | gid_t gid; | |
1091 | ||
1092 | ret_code = utils_group_id_from_name( | |
1093 | value->value.group_name, &gid); | |
1094 | if (ret_code != LTTNG_OK) { | |
1095 | goto end; | |
1096 | } | |
1097 | integral_value = (int) gid; | |
1098 | } else { | |
1099 | integral_value = (int) value->value.gid; | |
1100 | } | |
1101 | break; | |
1102 | default: | |
1103 | ret_code = LTTNG_ERR_INVALID; | |
1104 | goto end; | |
1105 | } | |
55c9e7ca | 1106 | |
159b042f JG |
1107 | tracker = _kernel_get_process_attr_tracker(session, process_attr); |
1108 | if (!tracker) { | |
1109 | ret_code = LTTNG_ERR_INVALID; | |
a7a533cd | 1110 | goto end; |
a5dfbb9d | 1111 | } |
a7a533cd | 1112 | |
159b042f JG |
1113 | status = process_attr_tracker_inclusion_set_remove_value( |
1114 | tracker, value); | |
1115 | if (status != PROCESS_ATTR_TRACKER_STATUS_OK) { | |
1116 | switch (status) { | |
1117 | case PROCESS_ATTR_TRACKER_STATUS_MISSING: | |
1118 | ret_code = LTTNG_ERR_PROCESS_ATTR_MISSING; | |
1119 | break; | |
1120 | case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY: | |
1121 | ret_code = LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY; | |
1122 | break; | |
1123 | case PROCESS_ATTR_TRACKER_STATUS_ERROR: | |
1124 | default: | |
1125 | ret_code = LTTNG_ERR_UNK; | |
1126 | break; | |
1127 | } | |
a7a533cd JR |
1128 | goto end; |
1129 | } | |
1130 | ||
159b042f JG |
1131 | DBG("Kernel track %s %d for session id %" PRIu64, |
1132 | lttng_process_attr_to_string(process_attr), | |
1133 | integral_value, session->id); | |
1134 | if (process_attr == LTTNG_PROCESS_ATTR_PROCESS_ID) { | |
1135 | /* | |
1136 | * Maintain a special case for the process ID process attribute | |
1137 | * tracker as it was the only supported attribute prior to 2.12. | |
1138 | */ | |
1139 | ret = kernctl_untrack_pid(session->fd, integral_value); | |
1140 | } else { | |
1141 | ret = kernctl_untrack_id( | |
1142 | session->fd, process_attr, integral_value); | |
1143 | } | |
1144 | if (ret == 0) { | |
1145 | ret_code = LTTNG_OK; | |
1146 | goto end; | |
1147 | } | |
1148 | kernel_wait_quiescent(); | |
1149 | ||
1150 | /* kern-ctl error handling */ | |
1151 | switch (-ret) { | |
1152 | case 0: | |
1153 | ret_code = LTTNG_OK; | |
1154 | break; | |
1155 | case EINVAL: | |
1156 | ret_code = LTTNG_ERR_INVALID; | |
1157 | break; | |
1158 | case ENOMEM: | |
1159 | ret_code = LTTNG_ERR_NOMEM; | |
1160 | break; | |
1161 | case ENOENT: | |
1162 | ret_code = LTTNG_ERR_PROCESS_ATTR_MISSING; | |
1163 | break; | |
1164 | default: | |
1165 | ret_code = LTTNG_ERR_UNK; | |
1166 | break; | |
1167 | } | |
1168 | ||
1169 | /* Attempt to add the value to the tracker. */ | |
1170 | status = process_attr_tracker_inclusion_set_add_value( | |
1171 | tracker, value); | |
1172 | if (status != PROCESS_ATTR_TRACKER_STATUS_OK) { | |
1173 | ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error", | |
1174 | lttng_process_attr_to_string(process_attr), | |
1175 | integral_value); | |
1176 | } | |
a7a533cd | 1177 | end: |
159b042f | 1178 | return ret_code; |
a5dfbb9d MD |
1179 | } |
1180 | ||
aaf26714 | 1181 | /* |
050349bb DG |
1182 | * Create kernel metadata, open from the kernel tracer and add it to the |
1183 | * kernel session. | |
aaf26714 | 1184 | */ |
a4b92340 | 1185 | int kernel_open_metadata(struct ltt_kernel_session *session) |
aaf26714 DG |
1186 | { |
1187 | int ret; | |
74024a21 | 1188 | struct ltt_kernel_metadata *lkm = NULL; |
aaf26714 | 1189 | |
a0377dfe | 1190 | LTTNG_ASSERT(session); |
0525e9ae | 1191 | |
54012638 | 1192 | /* Allocate kernel metadata */ |
a4b92340 | 1193 | lkm = trace_kernel_create_metadata(); |
54012638 | 1194 | if (lkm == NULL) { |
aaf26714 DG |
1195 | goto error; |
1196 | } | |
1197 | ||
54012638 | 1198 | /* Kernel tracer metadata creation */ |
f3ed775e | 1199 | ret = kernctl_open_metadata(session->fd, &lkm->conf->attr); |
aaf26714 | 1200 | if (ret < 0) { |
74024a21 | 1201 | goto error_open; |
aaf26714 DG |
1202 | } |
1203 | ||
8c0faa1d | 1204 | lkm->fd = ret; |
d40f0359 | 1205 | lkm->key = ++next_kernel_channel_key; |
7b395890 DG |
1206 | /* Prevent fd duplication after execlp() */ |
1207 | ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC); | |
1208 | if (ret < 0) { | |
df0f840b | 1209 | PERROR("fcntl session fd"); |
7b395890 DG |
1210 | } |
1211 | ||
aaf26714 | 1212 | session->metadata = lkm; |
8c0faa1d | 1213 | |
00e2e675 | 1214 | DBG("Kernel metadata opened (fd: %d)", lkm->fd); |
8c0faa1d DG |
1215 | |
1216 | return 0; | |
1217 | ||
74024a21 DG |
1218 | error_open: |
1219 | trace_kernel_destroy_metadata(lkm); | |
8c0faa1d | 1220 | error: |
54012638 | 1221 | return -1; |
8c0faa1d DG |
1222 | } |
1223 | ||
1224 | /* | |
050349bb | 1225 | * Start tracing session. |
8c0faa1d DG |
1226 | */ |
1227 | int kernel_start_session(struct ltt_kernel_session *session) | |
1228 | { | |
1229 | int ret; | |
1230 | ||
a0377dfe | 1231 | LTTNG_ASSERT(session); |
0525e9ae | 1232 | |
8c0faa1d DG |
1233 | ret = kernctl_start_session(session->fd); |
1234 | if (ret < 0) { | |
df0f840b | 1235 | PERROR("ioctl start session"); |
8c0faa1d DG |
1236 | goto error; |
1237 | } | |
1238 | ||
1239 | DBG("Kernel session started"); | |
1240 | ||
1241 | return 0; | |
1242 | ||
1243 | error: | |
1244 | return ret; | |
1245 | } | |
1246 | ||
f3ed775e | 1247 | /* |
050349bb | 1248 | * Make a kernel wait to make sure in-flight probe have completed. |
f3ed775e | 1249 | */ |
7d268848 | 1250 | void kernel_wait_quiescent(void) |
f3ed775e DG |
1251 | { |
1252 | int ret; | |
7d268848 | 1253 | int fd = kernel_tracer_fd; |
f3ed775e DG |
1254 | |
1255 | DBG("Kernel quiescent wait on %d", fd); | |
1256 | ||
1257 | ret = kernctl_wait_quiescent(fd); | |
1258 | if (ret < 0) { | |
df0f840b | 1259 | PERROR("wait quiescent ioctl"); |
f3ed775e DG |
1260 | ERR("Kernel quiescent wait failed"); |
1261 | } | |
1262 | } | |
1263 | ||
1264 | /* | |
f3ed775e DG |
1265 | * Force flush buffer of metadata. |
1266 | */ | |
1267 | int kernel_metadata_flush_buffer(int fd) | |
1268 | { | |
1269 | int ret; | |
1270 | ||
169d2cb7 DG |
1271 | DBG("Kernel flushing metadata buffer on fd %d", fd); |
1272 | ||
f3ed775e DG |
1273 | ret = kernctl_buffer_flush(fd); |
1274 | if (ret < 0) { | |
00e2e675 | 1275 | ERR("Fail to flush metadata buffers %d (ret: %d)", fd, ret); |
f3ed775e DG |
1276 | } |
1277 | ||
1278 | return 0; | |
1279 | } | |
1280 | ||
1281 | /* | |
050349bb | 1282 | * Force flush buffer for channel. |
f3ed775e DG |
1283 | */ |
1284 | int kernel_flush_buffer(struct ltt_kernel_channel *channel) | |
1285 | { | |
1286 | int ret; | |
1287 | struct ltt_kernel_stream *stream; | |
1288 | ||
a0377dfe | 1289 | LTTNG_ASSERT(channel); |
0525e9ae | 1290 | |
f3ed775e DG |
1291 | DBG("Flush buffer for channel %s", channel->channel->name); |
1292 | ||
1293 | cds_list_for_each_entry(stream, &channel->stream_list.head, list) { | |
1294 | DBG("Flushing channel stream %d", stream->fd); | |
1295 | ret = kernctl_buffer_flush(stream->fd); | |
1296 | if (ret < 0) { | |
df0f840b | 1297 | PERROR("ioctl"); |
f3ed775e DG |
1298 | ERR("Fail to flush buffer for stream %d (ret: %d)", |
1299 | stream->fd, ret); | |
1300 | } | |
1301 | } | |
1302 | ||
1303 | return 0; | |
1304 | } | |
1305 | ||
8c0faa1d | 1306 | /* |
050349bb | 1307 | * Stop tracing session. |
8c0faa1d DG |
1308 | */ |
1309 | int kernel_stop_session(struct ltt_kernel_session *session) | |
1310 | { | |
1311 | int ret; | |
1312 | ||
a0377dfe | 1313 | LTTNG_ASSERT(session); |
0525e9ae | 1314 | |
8c0faa1d DG |
1315 | ret = kernctl_stop_session(session->fd); |
1316 | if (ret < 0) { | |
1317 | goto error; | |
1318 | } | |
1319 | ||
1320 | DBG("Kernel session stopped"); | |
1321 | ||
1322 | return 0; | |
1323 | ||
1324 | error: | |
1325 | return ret; | |
1326 | } | |
1327 | ||
1328 | /* | |
050349bb DG |
1329 | * Open stream of channel, register it to the kernel tracer and add it |
1330 | * to the stream list of the channel. | |
8c0faa1d | 1331 | * |
1cfb4b98 MD |
1332 | * Note: given that the streams may appear in random order wrt CPU |
1333 | * number (e.g. cpu hotplug), the index value of the stream number in | |
1334 | * the stream name is not necessarily linked to the CPU number. | |
1335 | * | |
050349bb | 1336 | * Return the number of created stream. Else, a negative value. |
8c0faa1d | 1337 | */ |
f3ed775e | 1338 | int kernel_open_channel_stream(struct ltt_kernel_channel *channel) |
8c0faa1d | 1339 | { |
1cfb4b98 | 1340 | int ret; |
8c0faa1d DG |
1341 | struct ltt_kernel_stream *lks; |
1342 | ||
a0377dfe | 1343 | LTTNG_ASSERT(channel); |
0525e9ae | 1344 | |
5a47c6a2 | 1345 | while ((ret = kernctl_create_stream(channel->fd)) >= 0) { |
1cfb4b98 MD |
1346 | lks = trace_kernel_create_stream(channel->channel->name, |
1347 | channel->stream_count); | |
8c0faa1d | 1348 | if (lks == NULL) { |
799e2c4f MD |
1349 | ret = close(ret); |
1350 | if (ret) { | |
1351 | PERROR("close"); | |
1352 | } | |
8c0faa1d DG |
1353 | goto error; |
1354 | } | |
1355 | ||
1356 | lks->fd = ret; | |
7b395890 DG |
1357 | /* Prevent fd duplication after execlp() */ |
1358 | ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC); | |
1359 | if (ret < 0) { | |
df0f840b | 1360 | PERROR("fcntl session fd"); |
7b395890 DG |
1361 | } |
1362 | ||
1624d5b7 JD |
1363 | lks->tracefile_size = channel->channel->attr.tracefile_size; |
1364 | lks->tracefile_count = channel->channel->attr.tracefile_count; | |
1365 | ||
1cfb4b98 | 1366 | /* Add stream to channel stream list */ |
8c0faa1d DG |
1367 | cds_list_add(&lks->list, &channel->stream_list.head); |
1368 | channel->stream_count++; | |
8c0faa1d | 1369 | |
00e2e675 DG |
1370 | DBG("Kernel stream %s created (fd: %d, state: %d)", lks->name, lks->fd, |
1371 | lks->state); | |
54012638 | 1372 | } |
8c0faa1d DG |
1373 | |
1374 | return channel->stream_count; | |
1375 | ||
1376 | error: | |
54012638 | 1377 | return -1; |
8c0faa1d DG |
1378 | } |
1379 | ||
1380 | /* | |
050349bb | 1381 | * Open the metadata stream and set it to the kernel session. |
8c0faa1d | 1382 | */ |
f3ed775e | 1383 | int kernel_open_metadata_stream(struct ltt_kernel_session *session) |
8c0faa1d DG |
1384 | { |
1385 | int ret; | |
1386 | ||
a0377dfe | 1387 | LTTNG_ASSERT(session); |
0525e9ae | 1388 | |
8c0faa1d DG |
1389 | ret = kernctl_create_stream(session->metadata->fd); |
1390 | if (ret < 0) { | |
df0f840b | 1391 | PERROR("kernel create metadata stream"); |
8c0faa1d DG |
1392 | goto error; |
1393 | } | |
1394 | ||
1395 | DBG("Kernel metadata stream created (fd: %d)", ret); | |
1396 | session->metadata_stream_fd = ret; | |
7b395890 DG |
1397 | /* Prevent fd duplication after execlp() */ |
1398 | ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC); | |
1399 | if (ret < 0) { | |
df0f840b | 1400 | PERROR("fcntl session fd"); |
7b395890 | 1401 | } |
aaf26714 DG |
1402 | |
1403 | return 0; | |
1404 | ||
1405 | error: | |
54012638 | 1406 | return -1; |
aaf26714 | 1407 | } |
2ef84c95 DG |
1408 | |
1409 | /* | |
9f19cc17 | 1410 | * Get the event list from the kernel tracer and return the number of elements. |
2ef84c95 | 1411 | */ |
7d268848 | 1412 | ssize_t kernel_list_events(struct lttng_event **events) |
2ef84c95 | 1413 | { |
53efb85a | 1414 | int fd, ret; |
9f19cc17 DG |
1415 | char *event; |
1416 | size_t nbmem, count = 0; | |
2ef84c95 | 1417 | FILE *fp; |
9f19cc17 | 1418 | struct lttng_event *elist; |
2ef84c95 | 1419 | |
a0377dfe | 1420 | LTTNG_ASSERT(events); |
0525e9ae | 1421 | |
7d268848 | 1422 | fd = kernctl_tracepoint_list(kernel_tracer_fd); |
2ef84c95 | 1423 | if (fd < 0) { |
df0f840b | 1424 | PERROR("kernel tracepoint list"); |
2ef84c95 DG |
1425 | goto error; |
1426 | } | |
1427 | ||
1428 | fp = fdopen(fd, "r"); | |
1429 | if (fp == NULL) { | |
df0f840b | 1430 | PERROR("kernel tracepoint list fdopen"); |
61b73b12 | 1431 | goto error_fp; |
2ef84c95 DG |
1432 | } |
1433 | ||
1434 | /* | |
1435 | * Init memory size counter | |
1436 | * See kernel-ctl.h for explanation of this value | |
1437 | */ | |
6725fe19 | 1438 | nbmem = KERNEL_EVENT_INIT_LIST_SIZE; |
7966af57 | 1439 | elist = (lttng_event *) zmalloc(sizeof(struct lttng_event) * nbmem); |
3b870559 MD |
1440 | if (elist == NULL) { |
1441 | PERROR("alloc list events"); | |
1442 | count = -ENOMEM; | |
1443 | goto end; | |
1444 | } | |
2ef84c95 | 1445 | |
53efb85a | 1446 | while (fscanf(fp, "event { name = %m[^;]; };\n", &event) == 1) { |
6725fe19 | 1447 | if (count >= nbmem) { |
3b870559 | 1448 | struct lttng_event *new_elist; |
53efb85a | 1449 | size_t new_nbmem; |
3b870559 | 1450 | |
53efb85a MD |
1451 | new_nbmem = nbmem << 1; |
1452 | DBG("Reallocating event list from %zu to %zu bytes", | |
1453 | nbmem, new_nbmem); | |
7966af57 | 1454 | new_elist = (lttng_event *) realloc(elist, new_nbmem * sizeof(struct lttng_event)); |
3b870559 | 1455 | if (new_elist == NULL) { |
df0f840b | 1456 | PERROR("realloc list events"); |
3b870559 MD |
1457 | free(event); |
1458 | free(elist); | |
61b73b12 MD |
1459 | count = -ENOMEM; |
1460 | goto end; | |
2ef84c95 | 1461 | } |
53efb85a MD |
1462 | /* Zero the new memory */ |
1463 | memset(new_elist + nbmem, 0, | |
1464 | (new_nbmem - nbmem) * sizeof(struct lttng_event)); | |
1465 | nbmem = new_nbmem; | |
3b870559 | 1466 | elist = new_elist; |
2ef84c95 | 1467 | } |
99497cd0 MD |
1468 | strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN); |
1469 | elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; | |
67b9d018 | 1470 | elist[count].enabled = -1; |
9f19cc17 | 1471 | count++; |
3b870559 | 1472 | free(event); |
2ef84c95 DG |
1473 | } |
1474 | ||
9f19cc17 | 1475 | *events = elist; |
ced2f820 | 1476 | DBG("Kernel list events done (%zu events)", count); |
61b73b12 | 1477 | end: |
799e2c4f MD |
1478 | ret = fclose(fp); /* closes both fp and fd */ |
1479 | if (ret) { | |
1480 | PERROR("fclose"); | |
1481 | } | |
9f19cc17 | 1482 | return count; |
2ef84c95 | 1483 | |
61b73b12 | 1484 | error_fp: |
799e2c4f MD |
1485 | ret = close(fd); |
1486 | if (ret) { | |
1487 | PERROR("close"); | |
1488 | } | |
2ef84c95 DG |
1489 | error: |
1490 | return -1; | |
1491 | } | |
096102bd DG |
1492 | |
1493 | /* | |
1494 | * Get kernel version and validate it. | |
1495 | */ | |
b8e2fb80 FD |
1496 | int kernel_validate_version(struct lttng_kernel_abi_tracer_version *version, |
1497 | struct lttng_kernel_abi_tracer_abi_version *abi_version) | |
096102bd DG |
1498 | { |
1499 | int ret; | |
096102bd | 1500 | |
7d268848 | 1501 | ret = kernctl_tracer_version(kernel_tracer_fd, version); |
096102bd | 1502 | if (ret < 0) { |
521dd134 | 1503 | ERR("Failed to retrieve the lttng-modules version"); |
096102bd DG |
1504 | goto error; |
1505 | } | |
1506 | ||
1507 | /* Validate version */ | |
88076e89 | 1508 | if (version->major != VERSION_MAJOR) { |
c052142c | 1509 | ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)", |
88076e89 | 1510 | version->major, VERSION_MAJOR); |
096102bd | 1511 | goto error_version; |
096102bd | 1512 | } |
7d268848 | 1513 | ret = kernctl_tracer_abi_version(kernel_tracer_fd, abi_version); |
c052142c | 1514 | if (ret < 0) { |
521dd134 | 1515 | ERR("Failed to retrieve lttng-modules ABI version"); |
c052142c MD |
1516 | goto error; |
1517 | } | |
b8e2fb80 | 1518 | if (abi_version->major != LTTNG_KERNEL_ABI_MAJOR_VERSION) { |
521dd134 | 1519 | ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)", |
88076e89 | 1520 | abi_version->major, abi_version->minor, |
b8e2fb80 | 1521 | LTTNG_KERNEL_ABI_MAJOR_VERSION); |
c052142c MD |
1522 | goto error; |
1523 | } | |
1524 | DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)", | |
88076e89 JD |
1525 | version->major, version->minor, |
1526 | abi_version->major, abi_version->minor); | |
096102bd DG |
1527 | return 0; |
1528 | ||
1529 | error_version: | |
096102bd DG |
1530 | ret = -1; |
1531 | ||
1532 | error: | |
521dd134 | 1533 | ERR("Kernel tracer version check failed; kernel tracing will not be available"); |
096102bd DG |
1534 | return ret; |
1535 | } | |
335a95b7 MD |
1536 | |
1537 | /* | |
1538 | * Kernel work-arounds called at the start of sessiond main(). | |
1539 | */ | |
1540 | int init_kernel_workarounds(void) | |
1541 | { | |
8936c33a | 1542 | int ret; |
335a95b7 MD |
1543 | FILE *fp; |
1544 | ||
1545 | /* | |
1546 | * boot_id needs to be read once before being used concurrently | |
1547 | * to deal with a Linux kernel race. A fix is proposed for | |
1548 | * upstream, but the work-around is needed for older kernels. | |
1549 | */ | |
1550 | fp = fopen("/proc/sys/kernel/random/boot_id", "r"); | |
1551 | if (!fp) { | |
1552 | goto end_boot_id; | |
1553 | } | |
1554 | while (!feof(fp)) { | |
1555 | char buf[37] = ""; | |
1556 | ||
8936c33a DG |
1557 | ret = fread(buf, 1, sizeof(buf), fp); |
1558 | if (ret < 0) { | |
1559 | /* Ignore error, we don't really care */ | |
1560 | } | |
335a95b7 | 1561 | } |
799e2c4f MD |
1562 | ret = fclose(fp); |
1563 | if (ret) { | |
1564 | PERROR("fclose"); | |
1565 | } | |
335a95b7 | 1566 | end_boot_id: |
335a95b7 MD |
1567 | return 0; |
1568 | } | |
2f77fc4b DG |
1569 | |
1570 | /* | |
d070c424 | 1571 | * Teardown of a kernel session, keeping data required by destroy notifiers. |
2f77fc4b DG |
1572 | */ |
1573 | void kernel_destroy_session(struct ltt_kernel_session *ksess) | |
1574 | { | |
82b69413 JG |
1575 | struct lttng_trace_chunk *trace_chunk; |
1576 | ||
2f77fc4b DG |
1577 | if (ksess == NULL) { |
1578 | DBG3("No kernel session when tearing down session"); | |
1579 | return; | |
1580 | } | |
1581 | ||
1582 | DBG("Tearing down kernel session"); | |
82b69413 | 1583 | trace_chunk = ksess->current_trace_chunk; |
2f77fc4b | 1584 | |
07b86b52 | 1585 | /* |
15dc512a DG |
1586 | * Destroy channels on the consumer if at least one FD has been sent and we |
1587 | * are in no output mode because the streams are in *no* monitor mode so we | |
1588 | * have to send a command to clean them up or else they leaked. | |
07b86b52 | 1589 | */ |
15dc512a | 1590 | if (!ksess->output_traces && ksess->consumer_fds_sent) { |
07b86b52 JD |
1591 | int ret; |
1592 | struct consumer_socket *socket; | |
1593 | struct lttng_ht_iter iter; | |
1594 | ||
1595 | /* For each consumer socket. */ | |
d069d577 | 1596 | rcu_read_lock(); |
07b86b52 JD |
1597 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter, |
1598 | socket, node.node) { | |
1599 | struct ltt_kernel_channel *chan; | |
1600 | ||
1601 | /* For each channel, ask the consumer to destroy it. */ | |
1602 | cds_list_for_each_entry(chan, &ksess->channel_list.head, list) { | |
1603 | ret = kernel_consumer_destroy_channel(socket, chan); | |
1604 | if (ret < 0) { | |
1605 | /* Consumer is probably dead. Use next socket. */ | |
1606 | continue; | |
1607 | } | |
1608 | } | |
1609 | } | |
d069d577 | 1610 | rcu_read_unlock(); |
07b86b52 JD |
1611 | } |
1612 | ||
2f77fc4b DG |
1613 | /* Close any relayd session */ |
1614 | consumer_output_send_destroy_relayd(ksess->consumer); | |
1615 | ||
1616 | trace_kernel_destroy_session(ksess); | |
82b69413 | 1617 | lttng_trace_chunk_put(trace_chunk); |
2f77fc4b | 1618 | } |
fb5f35b6 | 1619 | |
d070c424 MD |
1620 | /* Teardown of data required by destroy notifiers. */ |
1621 | void kernel_free_session(struct ltt_kernel_session *ksess) | |
1622 | { | |
1623 | if (ksess == NULL) { | |
1624 | return; | |
1625 | } | |
1626 | trace_kernel_free_session(ksess); | |
1627 | } | |
1628 | ||
fb5f35b6 DG |
1629 | /* |
1630 | * Destroy a kernel channel object. It does not do anything on the tracer side. | |
1631 | */ | |
1632 | void kernel_destroy_channel(struct ltt_kernel_channel *kchan) | |
1633 | { | |
1634 | struct ltt_kernel_session *ksess = NULL; | |
1635 | ||
a0377dfe FD |
1636 | LTTNG_ASSERT(kchan); |
1637 | LTTNG_ASSERT(kchan->channel); | |
fb5f35b6 DG |
1638 | |
1639 | DBG3("Kernel destroy channel %s", kchan->channel->name); | |
1640 | ||
1641 | /* Update channel count of associated session. */ | |
1642 | if (kchan->session) { | |
1643 | /* Keep pointer reference so we can update it after the destroy. */ | |
1644 | ksess = kchan->session; | |
1645 | } | |
1646 | ||
1647 | trace_kernel_destroy_channel(kchan); | |
1648 | ||
1649 | /* | |
1650 | * At this point the kernel channel is not visible anymore. This is safe | |
1651 | * since in order to work on a visible kernel session, the tracing session | |
1652 | * lock (ltt_session.lock) MUST be acquired. | |
1653 | */ | |
1654 | if (ksess) { | |
1655 | ksess->channel_count--; | |
1656 | } | |
1657 | } | |
6dc3064a DG |
1658 | |
1659 | /* | |
1660 | * Take a snapshot for a given kernel session. | |
1661 | * | |
9a654598 | 1662 | * Return LTTNG_OK on success or else return a LTTNG_ERR code. |
6dc3064a | 1663 | */ |
fb9a95c4 JG |
1664 | enum lttng_error_code kernel_snapshot_record( |
1665 | struct ltt_kernel_session *ksess, | |
348a81dc | 1666 | const struct consumer_output *output, int wait, |
d07ceecd | 1667 | uint64_t nb_packets_per_stream) |
6dc3064a | 1668 | { |
2a06df8d | 1669 | int err, ret, saved_metadata_fd; |
9a654598 | 1670 | enum lttng_error_code status = LTTNG_OK; |
6dc3064a DG |
1671 | struct consumer_socket *socket; |
1672 | struct lttng_ht_iter iter; | |
1673 | struct ltt_kernel_metadata *saved_metadata; | |
3b967712 | 1674 | char *trace_path = NULL; |
5da88b0f | 1675 | size_t consumer_path_offset = 0; |
6dc3064a | 1676 | |
a0377dfe FD |
1677 | LTTNG_ASSERT(ksess); |
1678 | LTTNG_ASSERT(ksess->consumer); | |
1679 | LTTNG_ASSERT(output); | |
6dc3064a DG |
1680 | |
1681 | DBG("Kernel snapshot record started"); | |
1682 | ||
1683 | /* Save current metadata since the following calls will change it. */ | |
1684 | saved_metadata = ksess->metadata; | |
1685 | saved_metadata_fd = ksess->metadata_stream_fd; | |
1686 | ||
1687 | rcu_read_lock(); | |
1688 | ||
1689 | ret = kernel_open_metadata(ksess); | |
1690 | if (ret < 0) { | |
9a654598 | 1691 | status = LTTNG_ERR_KERN_META_FAIL; |
6dc3064a DG |
1692 | goto error; |
1693 | } | |
1694 | ||
1695 | ret = kernel_open_metadata_stream(ksess); | |
1696 | if (ret < 0) { | |
9a654598 | 1697 | status = LTTNG_ERR_KERN_META_FAIL; |
6dc3064a DG |
1698 | goto error_open_stream; |
1699 | } | |
1700 | ||
3b967712 | 1701 | trace_path = setup_channel_trace_path(ksess->consumer, |
bd666153 | 1702 | "", &consumer_path_offset); |
3b967712 MD |
1703 | if (!trace_path) { |
1704 | status = LTTNG_ERR_INVALID; | |
1705 | goto error; | |
1706 | } | |
6dc3064a | 1707 | /* Send metadata to consumer and snapshot everything. */ |
348a81dc | 1708 | cds_lfht_for_each_entry(output->socks->ht, &iter.iter, |
6dc3064a | 1709 | socket, node.node) { |
6dc3064a | 1710 | struct ltt_kernel_channel *chan; |
6dc3064a | 1711 | |
6dc3064a DG |
1712 | pthread_mutex_lock(socket->lock); |
1713 | /* This stream must not be monitored by the consumer. */ | |
07b86b52 | 1714 | ret = kernel_consumer_add_metadata(socket, ksess, 0); |
6dc3064a | 1715 | pthread_mutex_unlock(socket->lock); |
6dc3064a | 1716 | if (ret < 0) { |
0ed78e50 | 1717 | status = LTTNG_ERR_KERN_META_FAIL; |
6dc3064a DG |
1718 | goto error_consumer; |
1719 | } | |
1720 | ||
1721 | /* For each channel, ask the consumer to snapshot it. */ | |
1722 | cds_list_for_each_entry(chan, &ksess->channel_list.head, list) { | |
9a654598 | 1723 | status = consumer_snapshot_channel(socket, chan->key, output, 0, |
5c786ded | 1724 | ksess->uid, ksess->gid, |
5da88b0f | 1725 | &trace_path[consumer_path_offset], wait, |
d2956687 | 1726 | nb_packets_per_stream); |
9a654598 | 1727 | if (status != LTTNG_OK) { |
2a06df8d DG |
1728 | (void) kernel_consumer_destroy_metadata(socket, |
1729 | ksess->metadata); | |
6dc3064a DG |
1730 | goto error_consumer; |
1731 | } | |
1732 | } | |
1733 | ||
1734 | /* Snapshot metadata, */ | |
9a654598 | 1735 | status = consumer_snapshot_channel(socket, ksess->metadata->key, output, |
5da88b0f MD |
1736 | 1, ksess->uid, ksess->gid, &trace_path[consumer_path_offset], |
1737 | wait, 0); | |
9a654598 | 1738 | if (status != LTTNG_OK) { |
6dc3064a DG |
1739 | goto error_consumer; |
1740 | } | |
07b86b52 JD |
1741 | |
1742 | /* | |
1743 | * The metadata snapshot is done, ask the consumer to destroy it since | |
1744 | * it's not monitored on the consumer side. | |
1745 | */ | |
1746 | (void) kernel_consumer_destroy_metadata(socket, ksess->metadata); | |
6dc3064a DG |
1747 | } |
1748 | ||
1749 | error_consumer: | |
1750 | /* Close newly opened metadata stream. It's now on the consumer side. */ | |
2a06df8d DG |
1751 | err = close(ksess->metadata_stream_fd); |
1752 | if (err < 0) { | |
6dc3064a DG |
1753 | PERROR("close snapshot kernel"); |
1754 | } | |
1755 | ||
1756 | error_open_stream: | |
1757 | trace_kernel_destroy_metadata(ksess->metadata); | |
1758 | error: | |
1759 | /* Restore metadata state.*/ | |
1760 | ksess->metadata = saved_metadata; | |
1761 | ksess->metadata_stream_fd = saved_metadata_fd; | |
6dc3064a | 1762 | rcu_read_unlock(); |
3b967712 | 1763 | free(trace_path); |
9a654598 | 1764 | return status; |
6dc3064a | 1765 | } |
834978fd DG |
1766 | |
1767 | /* | |
1768 | * Get the syscall mask array from the kernel tracer. | |
1769 | * | |
1770 | * Return 0 on success else a negative value. In both case, syscall_mask should | |
1771 | * be freed. | |
1772 | */ | |
1773 | int kernel_syscall_mask(int chan_fd, char **syscall_mask, uint32_t *nr_bits) | |
1774 | { | |
a0377dfe FD |
1775 | LTTNG_ASSERT(syscall_mask); |
1776 | LTTNG_ASSERT(nr_bits); | |
834978fd DG |
1777 | |
1778 | return kernctl_syscall_mask(chan_fd, syscall_mask, nr_bits); | |
1779 | } | |
6e21424e | 1780 | |
959e3c66 JR |
1781 | static |
1782 | int kernel_tracer_abi_greater_or_equal(unsigned int major, unsigned int minor) | |
6e21424e | 1783 | { |
959e3c66 | 1784 | int ret; |
b8e2fb80 | 1785 | struct lttng_kernel_abi_tracer_abi_version abi; |
6e21424e | 1786 | |
7d268848 | 1787 | ret = kernctl_tracer_abi_version(kernel_tracer_fd, &abi); |
6e21424e JR |
1788 | if (ret < 0) { |
1789 | ERR("Failed to retrieve lttng-modules ABI version"); | |
1790 | goto error; | |
1791 | } | |
1792 | ||
959e3c66 JR |
1793 | ret = abi.major > major || (abi.major == major && abi.minor >= minor); |
1794 | error: | |
1795 | return ret; | |
1796 | } | |
1797 | ||
1798 | /* | |
1799 | * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi | |
1800 | * version number. | |
1801 | * | |
1802 | * Return 1 on success, 0 when feature is not supported, negative value in case | |
1803 | * of errors. | |
1804 | */ | |
1805 | int kernel_supports_ring_buffer_snapshot_sample_positions(void) | |
1806 | { | |
6e21424e JR |
1807 | /* |
1808 | * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3 | |
1809 | */ | |
959e3c66 | 1810 | return kernel_tracer_abi_greater_or_equal(2, 3); |
6e21424e | 1811 | } |
5c408ad8 | 1812 | |
a40a503f MD |
1813 | /* |
1814 | * Check for the support of the packet sequence number via abi version number. | |
1815 | * | |
1816 | * Return 1 on success, 0 when feature is not supported, negative value in case | |
1817 | * of errors. | |
1818 | */ | |
1819 | int kernel_supports_ring_buffer_packet_sequence_number(void) | |
1820 | { | |
a40a503f | 1821 | /* |
3029bc92 JG |
1822 | * Packet sequence number was introduced in LTTng 2.8, |
1823 | * lttng-modules ABI 2.1. | |
a40a503f | 1824 | */ |
959e3c66 JR |
1825 | return kernel_tracer_abi_greater_or_equal(2, 1); |
1826 | } | |
1827 | ||
1828 | /* | |
1829 | * Check for the support of event notifiers via abi version number. | |
1830 | * | |
1831 | * Return 1 on success, 0 when feature is not supported, negative value in case | |
1832 | * of errors. | |
1833 | */ | |
1834 | int kernel_supports_event_notifiers(void) | |
1835 | { | |
1836 | /* | |
1837 | * Event notifiers were introduced in LTTng 2.13, lttng-modules ABI 2.6. | |
1838 | */ | |
1839 | return kernel_tracer_abi_greater_or_equal(2, 6); | |
a40a503f MD |
1840 | } |
1841 | ||
5c408ad8 JD |
1842 | /* |
1843 | * Rotate a kernel session. | |
1844 | * | |
d5a1b7aa | 1845 | * Return LTTNG_OK on success or else an LTTng error code. |
5c408ad8 | 1846 | */ |
d5a1b7aa | 1847 | enum lttng_error_code kernel_rotate_session(struct ltt_session *session) |
5c408ad8 JD |
1848 | { |
1849 | int ret; | |
d5a1b7aa | 1850 | enum lttng_error_code status = LTTNG_OK; |
5c408ad8 JD |
1851 | struct consumer_socket *socket; |
1852 | struct lttng_ht_iter iter; | |
1853 | struct ltt_kernel_session *ksess = session->kernel_session; | |
1854 | ||
a0377dfe FD |
1855 | LTTNG_ASSERT(ksess); |
1856 | LTTNG_ASSERT(ksess->consumer); | |
5c408ad8 JD |
1857 | |
1858 | DBG("Rotate kernel session %s started (session %" PRIu64 ")", | |
1859 | session->name, session->id); | |
1860 | ||
1861 | rcu_read_lock(); | |
1862 | ||
1863 | /* | |
1864 | * Note that this loop will end after one iteration given that there is | |
1865 | * only one kernel consumer. | |
1866 | */ | |
1867 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter, | |
1868 | socket, node.node) { | |
1869 | struct ltt_kernel_channel *chan; | |
1870 | ||
b8a1b0bd | 1871 | /* For each channel, ask the consumer to rotate it. */ |
5c408ad8 | 1872 | cds_list_for_each_entry(chan, &ksess->channel_list.head, list) { |
92816cc3 JG |
1873 | DBG("Rotate kernel channel %" PRIu64 ", session %s", |
1874 | chan->key, session->name); | |
5c408ad8 JD |
1875 | ret = consumer_rotate_channel(socket, chan->key, |
1876 | ksess->uid, ksess->gid, ksess->consumer, | |
d2956687 | 1877 | /* is_metadata_channel */ false); |
5c408ad8 | 1878 | if (ret < 0) { |
7e0de219 | 1879 | status = LTTNG_ERR_ROTATION_FAIL_CONSUMER; |
5c408ad8 JD |
1880 | goto error; |
1881 | } | |
1882 | } | |
1883 | ||
1884 | /* | |
1885 | * Rotate the metadata channel. | |
1886 | */ | |
22a1b931 | 1887 | ret = consumer_rotate_channel(socket, ksess->metadata->key, |
5c408ad8 | 1888 | ksess->uid, ksess->gid, ksess->consumer, |
d2956687 | 1889 | /* is_metadata_channel */ true); |
5c408ad8 | 1890 | if (ret < 0) { |
7e0de219 | 1891 | status = LTTNG_ERR_ROTATION_FAIL_CONSUMER; |
5c408ad8 JD |
1892 | goto error; |
1893 | } | |
1894 | } | |
1895 | ||
5c408ad8 JD |
1896 | error: |
1897 | rcu_read_unlock(); | |
d5a1b7aa | 1898 | return status; |
5c408ad8 | 1899 | } |
d2956687 JG |
1900 | |
1901 | enum lttng_error_code kernel_create_channel_subdirectories( | |
1902 | const struct ltt_kernel_session *ksess) | |
1903 | { | |
1904 | enum lttng_error_code ret = LTTNG_OK; | |
1905 | enum lttng_trace_chunk_status chunk_status; | |
1906 | ||
1907 | rcu_read_lock(); | |
a0377dfe | 1908 | LTTNG_ASSERT(ksess->current_trace_chunk); |
d2956687 JG |
1909 | |
1910 | /* | |
1911 | * Create the index subdirectory which will take care | |
1912 | * of implicitly creating the channel's path. | |
1913 | */ | |
1914 | chunk_status = lttng_trace_chunk_create_subdirectory( | |
1915 | ksess->current_trace_chunk, | |
1916 | DEFAULT_KERNEL_TRACE_DIR "/" DEFAULT_INDEX_DIR); | |
1917 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
1918 | ret = LTTNG_ERR_CREATE_DIR_FAIL; | |
1919 | goto error; | |
1920 | } | |
1921 | error: | |
1922 | rcu_read_unlock(); | |
1923 | return ret; | |
1924 | } | |
7d268848 MD |
1925 | |
1926 | /* | |
1927 | * Setup necessary data for kernel tracer action. | |
1928 | */ | |
7d268848 MD |
1929 | int init_kernel_tracer(void) |
1930 | { | |
1931 | int ret; | |
1932 | bool is_root = !getuid(); | |
1933 | ||
1934 | /* Modprobe lttng kernel modules */ | |
1935 | ret = modprobe_lttng_control(); | |
1936 | if (ret < 0) { | |
1937 | goto error; | |
1938 | } | |
1939 | ||
1940 | /* Open debugfs lttng */ | |
1941 | kernel_tracer_fd = open(module_proc_lttng, O_RDWR); | |
1942 | if (kernel_tracer_fd < 0) { | |
1943 | DBG("Failed to open %s", module_proc_lttng); | |
1944 | goto error_open; | |
1945 | } | |
1946 | ||
1947 | /* Validate kernel version */ | |
412d7227 SM |
1948 | ret = kernel_validate_version(&the_kernel_tracer_version, |
1949 | &the_kernel_tracer_abi_version); | |
7d268848 MD |
1950 | if (ret < 0) { |
1951 | goto error_version; | |
1952 | } | |
1953 | ||
1954 | ret = modprobe_lttng_data(); | |
1955 | if (ret < 0) { | |
1956 | goto error_modules; | |
1957 | } | |
1958 | ||
1959 | ret = kernel_supports_ring_buffer_snapshot_sample_positions(); | |
1960 | if (ret < 0) { | |
1961 | goto error_modules; | |
1962 | } | |
7d268848 MD |
1963 | if (ret < 1) { |
1964 | WARN("Kernel tracer does not support buffer monitoring. " | |
1965 | "The monitoring timer of channels in the kernel domain " | |
1966 | "will be set to 0 (disabled)."); | |
1967 | } | |
1968 | ||
352b58f5 JR |
1969 | ret = kernel_supports_event_notifiers(); |
1970 | if (ret < 0) { | |
1971 | ERR("Failed to check for kernel tracer event notifier support"); | |
1972 | goto error_modules; | |
1973 | } | |
1974 | ret = kernel_create_event_notifier_group(&kernel_tracer_event_notifier_group_fd); | |
1975 | if (ret < 0) { | |
1976 | /* This is not fatal. */ | |
1977 | WARN("Failed to create kernel event notifier group"); | |
1978 | kernel_tracer_event_notifier_group_fd = -1; | |
1979 | } else { | |
90aa04a1 FD |
1980 | enum event_notifier_error_accounting_status error_accounting_status; |
1981 | enum lttng_error_code error_code_ret = | |
352b58f5 JR |
1982 | kernel_create_event_notifier_group_notification_fd( |
1983 | &kernel_tracer_event_notifier_group_notification_fd); | |
1984 | ||
1985 | if (error_code_ret != LTTNG_OK) { | |
1986 | goto error_modules; | |
1987 | } | |
1988 | ||
90aa04a1 FD |
1989 | error_accounting_status = event_notifier_error_accounting_register_kernel( |
1990 | kernel_tracer_event_notifier_group_fd); | |
1991 | if (error_accounting_status != EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK) { | |
1992 | ERR("Failed to initialize event notifier error accounting for kernel tracer"); | |
1993 | error_code_ret = LTTNG_ERR_EVENT_NOTIFIER_ERROR_ACCOUNTING; | |
1994 | goto error_modules; | |
1995 | } | |
1996 | ||
352b58f5 JR |
1997 | kernel_token_to_event_notifier_rule_ht = cds_lfht_new( |
1998 | DEFAULT_HT_SIZE, 1, 0, | |
1999 | CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, | |
2000 | NULL); | |
2001 | if (!kernel_token_to_event_notifier_rule_ht) { | |
2002 | goto error_token_ht; | |
2003 | } | |
2004 | } | |
2005 | ||
2006 | DBG("Kernel tracer initialized: kernel tracer fd = %d, event notifier group fd = %d, event notifier group notification fd = %d", | |
2007 | kernel_tracer_fd, kernel_tracer_event_notifier_group_fd, | |
2008 | kernel_tracer_event_notifier_group_notification_fd); | |
7d268848 MD |
2009 | |
2010 | ret = syscall_init_table(kernel_tracer_fd); | |
2011 | if (ret < 0) { | |
2012 | ERR("Unable to populate syscall table. Syscall tracing won't " | |
2013 | "work for this session daemon."); | |
2014 | } | |
352b58f5 | 2015 | |
7d268848 MD |
2016 | return 0; |
2017 | ||
2018 | error_version: | |
2019 | modprobe_remove_lttng_control(); | |
2020 | ret = close(kernel_tracer_fd); | |
2021 | if (ret) { | |
352b58f5 JR |
2022 | PERROR("Failed to close kernel tracer file descriptor: fd = %d", |
2023 | kernel_tracer_fd); | |
7d268848 | 2024 | } |
6dcf80d3 | 2025 | |
7d268848 MD |
2026 | kernel_tracer_fd = -1; |
2027 | return LTTNG_ERR_KERN_VERSION; | |
2028 | ||
352b58f5 JR |
2029 | |
2030 | error_token_ht: | |
2031 | ret = close(kernel_tracer_event_notifier_group_notification_fd); | |
2032 | if (ret) { | |
2033 | PERROR("Failed to close kernel tracer event notifier group notification file descriptor: fd = %d", | |
2034 | kernel_tracer_event_notifier_group_notification_fd); | |
2035 | } | |
2036 | ||
6dcf80d3 JG |
2037 | kernel_tracer_event_notifier_group_notification_fd = -1; |
2038 | ||
7d268848 | 2039 | error_modules: |
352b58f5 JR |
2040 | ret = close(kernel_tracer_event_notifier_group_fd); |
2041 | if (ret) { | |
2042 | PERROR("Failed to close kernel tracer event notifier group file descriptor: fd = %d", | |
2043 | kernel_tracer_event_notifier_group_fd); | |
2044 | } | |
2045 | ||
6dcf80d3 JG |
2046 | kernel_tracer_event_notifier_group_fd = -1; |
2047 | ||
7d268848 MD |
2048 | ret = close(kernel_tracer_fd); |
2049 | if (ret) { | |
352b58f5 JR |
2050 | PERROR("Failed to close kernel tracer file descriptor: fd = %d", |
2051 | kernel_tracer_fd); | |
7d268848 MD |
2052 | } |
2053 | ||
6dcf80d3 JG |
2054 | kernel_tracer_fd = -1; |
2055 | ||
7d268848 MD |
2056 | error_open: |
2057 | modprobe_remove_lttng_control(); | |
2058 | ||
2059 | error: | |
2060 | WARN("No kernel tracer available"); | |
2061 | kernel_tracer_fd = -1; | |
2062 | if (!is_root) { | |
2063 | return LTTNG_ERR_NEED_ROOT_SESSIOND; | |
2064 | } else { | |
2065 | return LTTNG_ERR_KERN_NA; | |
2066 | } | |
2067 | } | |
2068 | ||
7d268848 MD |
2069 | void cleanup_kernel_tracer(void) |
2070 | { | |
352b58f5 JR |
2071 | DBG2("Closing kernel event notifier group notification file descriptor"); |
2072 | if (kernel_tracer_event_notifier_group_notification_fd >= 0) { | |
b69a1b40 | 2073 | int ret = notification_thread_command_remove_tracer_event_source( |
412d7227 | 2074 | the_notification_thread_handle, |
352b58f5 JR |
2075 | kernel_tracer_event_notifier_group_notification_fd); |
2076 | if (ret != LTTNG_OK) { | |
2077 | ERR("Failed to remove kernel event notifier notification from notification thread"); | |
2078 | } | |
2079 | ||
2080 | ret = close(kernel_tracer_event_notifier_group_notification_fd); | |
2081 | if (ret) { | |
2082 | PERROR("Failed to close kernel event notifier group notification file descriptor: fd = %d", | |
2083 | kernel_tracer_event_notifier_group_notification_fd); | |
2084 | } | |
2085 | ||
2086 | kernel_tracer_event_notifier_group_notification_fd = -1; | |
2087 | } | |
2088 | ||
2089 | if (kernel_token_to_event_notifier_rule_ht) { | |
b69a1b40 JG |
2090 | const int ret = cds_lfht_destroy( |
2091 | kernel_token_to_event_notifier_rule_ht, NULL); | |
a0377dfe | 2092 | LTTNG_ASSERT(ret == 0); |
352b58f5 JR |
2093 | } |
2094 | ||
2095 | DBG2("Closing kernel event notifier group file descriptor"); | |
2096 | if (kernel_tracer_event_notifier_group_fd >= 0) { | |
b69a1b40 JG |
2097 | const int ret = close(kernel_tracer_event_notifier_group_fd); |
2098 | ||
352b58f5 JR |
2099 | if (ret) { |
2100 | PERROR("Failed to close kernel event notifier group file descriptor: fd = %d", | |
2101 | kernel_tracer_event_notifier_group_fd); | |
2102 | } | |
2103 | ||
2104 | kernel_tracer_event_notifier_group_fd = -1; | |
2105 | } | |
7d268848 MD |
2106 | |
2107 | DBG2("Closing kernel fd"); | |
2108 | if (kernel_tracer_fd >= 0) { | |
b69a1b40 JG |
2109 | const int ret = close(kernel_tracer_fd); |
2110 | ||
7d268848 | 2111 | if (ret) { |
352b58f5 JR |
2112 | PERROR("Failed to close kernel tracer file descriptor: fd = %d", |
2113 | kernel_tracer_fd); | |
7d268848 | 2114 | } |
352b58f5 | 2115 | |
7d268848 MD |
2116 | kernel_tracer_fd = -1; |
2117 | } | |
352b58f5 | 2118 | |
7d268848 MD |
2119 | free(syscall_table); |
2120 | } | |
2121 | ||
7d268848 MD |
2122 | bool kernel_tracer_is_initialized(void) |
2123 | { | |
2124 | return kernel_tracer_fd >= 0; | |
2125 | } | |
bca212a2 MD |
2126 | |
2127 | /* | |
2128 | * Clear a kernel session. | |
2129 | * | |
2130 | * Return LTTNG_OK on success or else an LTTng error code. | |
2131 | */ | |
2132 | enum lttng_error_code kernel_clear_session(struct ltt_session *session) | |
2133 | { | |
2134 | int ret; | |
2135 | enum lttng_error_code status = LTTNG_OK; | |
2136 | struct consumer_socket *socket; | |
2137 | struct lttng_ht_iter iter; | |
2138 | struct ltt_kernel_session *ksess = session->kernel_session; | |
2139 | ||
a0377dfe FD |
2140 | LTTNG_ASSERT(ksess); |
2141 | LTTNG_ASSERT(ksess->consumer); | |
bca212a2 MD |
2142 | |
2143 | DBG("Clear kernel session %s (session %" PRIu64 ")", | |
2144 | session->name, session->id); | |
2145 | ||
2146 | rcu_read_lock(); | |
2147 | ||
2148 | if (ksess->active) { | |
2149 | ERR("Expecting inactive session %s (%" PRIu64 ")", session->name, session->id); | |
2150 | status = LTTNG_ERR_FATAL; | |
2151 | goto end; | |
2152 | } | |
2153 | ||
2154 | /* | |
2155 | * Note that this loop will end after one iteration given that there is | |
2156 | * only one kernel consumer. | |
2157 | */ | |
2158 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter, | |
2159 | socket, node.node) { | |
2160 | struct ltt_kernel_channel *chan; | |
2161 | ||
2162 | /* For each channel, ask the consumer to clear it. */ | |
2163 | cds_list_for_each_entry(chan, &ksess->channel_list.head, list) { | |
2164 | DBG("Clear kernel channel %" PRIu64 ", session %s", | |
2165 | chan->key, session->name); | |
2166 | ret = consumer_clear_channel(socket, chan->key); | |
2167 | if (ret < 0) { | |
2168 | goto error; | |
2169 | } | |
2170 | } | |
2171 | ||
2172 | if (!ksess->metadata) { | |
2173 | /* | |
2174 | * Nothing to do for the metadata. | |
2175 | * This is a snapshot session. | |
2176 | * The metadata is genererated on the fly. | |
2177 | */ | |
2178 | continue; | |
2179 | } | |
2180 | ||
2181 | /* | |
2182 | * Clear the metadata channel. | |
2183 | * Metadata channel is not cleared per se but we still need to | |
2184 | * perform a rotation operation on it behind the scene. | |
2185 | */ | |
2186 | ret = consumer_clear_channel(socket, ksess->metadata->key); | |
2187 | if (ret < 0) { | |
2188 | goto error; | |
2189 | } | |
2190 | } | |
2191 | ||
2192 | goto end; | |
2193 | error: | |
2194 | switch (-ret) { | |
2195 | case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED: | |
2196 | status = LTTNG_ERR_CLEAR_RELAY_DISALLOWED; | |
2197 | break; | |
2198 | default: | |
2199 | status = LTTNG_ERR_CLEAR_FAIL_CONSUMER; | |
2200 | break; | |
2201 | } | |
2202 | end: | |
2203 | rcu_read_unlock(); | |
2204 | return status; | |
2205 | } | |
352b58f5 JR |
2206 | |
2207 | enum lttng_error_code kernel_create_event_notifier_group_notification_fd( | |
2208 | int *event_notifier_group_notification_fd) | |
2209 | { | |
2210 | int local_fd = -1, ret; | |
2211 | enum lttng_error_code error_code_ret; | |
2212 | ||
a0377dfe | 2213 | LTTNG_ASSERT(event_notifier_group_notification_fd); |
352b58f5 JR |
2214 | |
2215 | ret = kernctl_create_event_notifier_group_notification_fd( | |
2216 | kernel_tracer_event_notifier_group_fd); | |
2217 | if (ret < 0) { | |
2218 | PERROR("Failed to create kernel event notifier group notification file descriptor"); | |
2219 | error_code_ret = LTTNG_ERR_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD; | |
2220 | goto error; | |
2221 | } | |
2222 | ||
2223 | local_fd = ret; | |
2224 | ||
2225 | /* Prevent fd duplication after execlp(). */ | |
2226 | ret = fcntl(local_fd, F_SETFD, FD_CLOEXEC); | |
2227 | if (ret < 0) { | |
2228 | PERROR("Failed to set FD_CLOEXEC on kernel event notifier group notification file descriptor: fd = %d", | |
2229 | local_fd); | |
2230 | error_code_ret = LTTNG_ERR_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD; | |
2231 | goto error; | |
2232 | } | |
2233 | ||
2234 | DBG("Created kernel notifier group notification file descriptor: fd = %d", | |
2235 | local_fd); | |
2236 | error_code_ret = LTTNG_OK; | |
2237 | *event_notifier_group_notification_fd = local_fd; | |
2238 | local_fd = -1; | |
2239 | ||
2240 | error: | |
2241 | if (local_fd >= 0) { | |
2242 | ret = close(local_fd); | |
2243 | if (ret) { | |
2244 | PERROR("Failed to close kernel event notifier group notification file descriptor: fd = %d", | |
2245 | local_fd); | |
2246 | } | |
2247 | } | |
2248 | ||
2249 | return error_code_ret; | |
2250 | } | |
2251 | ||
2252 | enum lttng_error_code kernel_destroy_event_notifier_group_notification_fd( | |
2253 | int event_notifier_group_notification_fd) | |
2254 | { | |
2255 | enum lttng_error_code ret_code = LTTNG_OK; | |
2256 | ||
2257 | DBG("Closing event notifier group notification file descriptor: fd = %d", | |
2258 | event_notifier_group_notification_fd); | |
2259 | if (event_notifier_group_notification_fd >= 0) { | |
2260 | const int ret = close(event_notifier_group_notification_fd); | |
2261 | if (ret) { | |
2262 | PERROR("Failed to close event notifier group notification file descriptor: fd = %d", | |
2263 | event_notifier_group_notification_fd); | |
2264 | } | |
2265 | } | |
2266 | ||
2267 | return ret_code; | |
2268 | } | |
2269 | ||
2270 | static | |
2271 | unsigned long hash_trigger(const struct lttng_trigger *trigger) | |
2272 | { | |
2273 | const struct lttng_condition *condition = | |
2274 | lttng_trigger_get_const_condition(trigger); | |
2275 | ||
2276 | return lttng_condition_hash(condition); | |
2277 | } | |
2278 | ||
2279 | static | |
2280 | int match_trigger(struct cds_lfht_node *node, const void *key) | |
2281 | { | |
2282 | const struct ltt_kernel_event_notifier_rule *event_notifier_rule; | |
7966af57 | 2283 | const struct lttng_trigger *trigger = (lttng_trigger *) key; |
352b58f5 JR |
2284 | |
2285 | event_notifier_rule = caa_container_of(node, | |
2286 | const struct ltt_kernel_event_notifier_rule, ht_node); | |
2287 | ||
2288 | return lttng_trigger_is_equal(trigger, event_notifier_rule->trigger); | |
2289 | } | |
2290 | ||
2291 | static enum lttng_error_code kernel_create_event_notifier_rule( | |
2292 | struct lttng_trigger *trigger, | |
2293 | const struct lttng_credentials *creds, uint64_t token) | |
2294 | { | |
2295 | int err, fd, ret = 0; | |
2296 | enum lttng_error_code error_code_ret; | |
2297 | enum lttng_condition_status condition_status; | |
2298 | enum lttng_condition_type condition_type; | |
2299 | enum lttng_event_rule_type event_rule_type; | |
2300 | struct ltt_kernel_event_notifier_rule *event_notifier_rule; | |
b8e2fb80 | 2301 | struct lttng_kernel_abi_event_notifier kernel_event_notifier = {}; |
bbd6675c | 2302 | unsigned int capture_bytecode_count = 0, i; |
352b58f5 JR |
2303 | const struct lttng_condition *condition = NULL; |
2304 | const struct lttng_event_rule *event_rule = NULL; | |
bbd6675c | 2305 | enum lttng_condition_status cond_status; |
352b58f5 | 2306 | |
a0377dfe | 2307 | LTTNG_ASSERT(trigger); |
352b58f5 JR |
2308 | |
2309 | condition = lttng_trigger_get_const_condition(trigger); | |
a0377dfe | 2310 | LTTNG_ASSERT(condition); |
352b58f5 JR |
2311 | |
2312 | condition_type = lttng_condition_get_type(condition); | |
a0377dfe | 2313 | LTTNG_ASSERT(condition_type == LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES); |
352b58f5 JR |
2314 | |
2315 | /* Does not acquire a reference. */ | |
8dbb86b8 | 2316 | condition_status = lttng_condition_event_rule_matches_get_rule( |
352b58f5 | 2317 | condition, &event_rule); |
a0377dfe FD |
2318 | LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK); |
2319 | LTTNG_ASSERT(event_rule); | |
352b58f5 JR |
2320 | |
2321 | event_rule_type = lttng_event_rule_get_type(event_rule); | |
a0377dfe | 2322 | LTTNG_ASSERT(event_rule_type != LTTNG_EVENT_RULE_TYPE_UNKNOWN); |
352b58f5 JR |
2323 | |
2324 | error_code_ret = trace_kernel_create_event_notifier_rule(trigger, token, | |
8dbb86b8 JR |
2325 | lttng_condition_event_rule_matches_get_error_counter_index( |
2326 | condition), | |
352b58f5 JR |
2327 | &event_notifier_rule); |
2328 | if (error_code_ret != LTTNG_OK) { | |
2329 | goto error; | |
2330 | } | |
2331 | ||
2332 | error_code_ret = trace_kernel_init_event_notifier_from_event_rule( | |
2333 | event_rule, &kernel_event_notifier); | |
2334 | if (error_code_ret != LTTNG_OK) { | |
b5e3b044 | 2335 | goto free_event; |
352b58f5 JR |
2336 | } |
2337 | ||
2338 | kernel_event_notifier.event.token = event_notifier_rule->token; | |
90aa04a1 | 2339 | kernel_event_notifier.error_counter_idx = |
8dbb86b8 JR |
2340 | lttng_condition_event_rule_matches_get_error_counter_index( |
2341 | condition); | |
352b58f5 JR |
2342 | |
2343 | fd = kernctl_create_event_notifier( | |
2344 | kernel_tracer_event_notifier_group_fd, | |
2345 | &kernel_event_notifier); | |
2346 | if (fd < 0) { | |
2347 | switch (-fd) { | |
2348 | case EEXIST: | |
2349 | error_code_ret = LTTNG_ERR_KERN_EVENT_EXIST; | |
2350 | break; | |
2351 | case ENOSYS: | |
2352 | WARN("Failed to create kernel event notifier: not notifier type not implemented"); | |
2353 | error_code_ret = LTTNG_ERR_KERN_EVENT_ENOSYS; | |
2354 | break; | |
2355 | case ENOENT: | |
2356 | WARN("Failed to create kernel event notifier: not found: name = '%s'", | |
2357 | kernel_event_notifier.event.name); | |
2358 | error_code_ret = LTTNG_ERR_KERN_ENABLE_FAIL; | |
2359 | break; | |
2360 | default: | |
2361 | PERROR("Failed to create kernel event notifier: error code = %d, name = '%s'", | |
2362 | fd, kernel_event_notifier.event.name); | |
2363 | error_code_ret = LTTNG_ERR_KERN_ENABLE_FAIL; | |
2364 | } | |
2365 | goto free_event; | |
2366 | } | |
2367 | ||
2368 | event_notifier_rule->fd = fd; | |
2369 | /* Prevent fd duplication after execlp(). */ | |
2370 | err = fcntl(event_notifier_rule->fd, F_SETFD, FD_CLOEXEC); | |
2371 | if (err < 0) { | |
2372 | PERROR("Failed to set FD_CLOEXEC on kernel event notifier file descriptor: fd = %d", | |
2373 | fd); | |
2374 | error_code_ret = LTTNG_ERR_FATAL; | |
2375 | goto set_cloexec_error; | |
2376 | } | |
2377 | ||
2378 | if (event_notifier_rule->filter) { | |
2379 | err = kernctl_filter(event_notifier_rule->fd, event_notifier_rule->filter); | |
2380 | if (err < 0) { | |
2381 | switch (-err) { | |
2382 | case ENOMEM: | |
2383 | error_code_ret = LTTNG_ERR_FILTER_NOMEM; | |
2384 | break; | |
2385 | default: | |
2386 | error_code_ret = LTTNG_ERR_FILTER_INVAL; | |
2387 | break; | |
2388 | } | |
2389 | goto filter_error; | |
2390 | } | |
2391 | } | |
2392 | ||
2393 | if (lttng_event_rule_get_type(event_rule) == | |
46fd07ac | 2394 | LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE) { |
352b58f5 JR |
2395 | ret = userspace_probe_event_rule_add_callsites( |
2396 | event_rule, creds, event_notifier_rule->fd); | |
2397 | if (ret) { | |
2398 | error_code_ret = LTTNG_ERR_KERN_ENABLE_FAIL; | |
2399 | goto add_callsite_error; | |
2400 | } | |
2401 | } | |
2402 | ||
d602bd6a | 2403 | /* Set the capture bytecode if any. */ |
8dbb86b8 | 2404 | cond_status = lttng_condition_event_rule_matches_get_capture_descriptor_count( |
bbd6675c | 2405 | condition, &capture_bytecode_count); |
a0377dfe | 2406 | LTTNG_ASSERT(cond_status == LTTNG_CONDITION_STATUS_OK); |
bbd6675c JR |
2407 | |
2408 | for (i = 0; i < capture_bytecode_count; i++) { | |
2409 | const struct lttng_bytecode *capture_bytecode = | |
8dbb86b8 | 2410 | lttng_condition_event_rule_matches_get_capture_bytecode_at_index( |
bbd6675c JR |
2411 | condition, i); |
2412 | ||
2413 | if (capture_bytecode == NULL) { | |
2414 | ERR("Unexpected NULL capture bytecode on condition"); | |
2415 | error_code_ret = LTTNG_ERR_KERN_ENABLE_FAIL; | |
e8b1187d | 2416 | goto capture_error; |
bbd6675c JR |
2417 | } |
2418 | ||
2419 | ret = kernctl_capture(event_notifier_rule->fd, capture_bytecode); | |
2420 | if (ret < 0) { | |
2421 | ERR("Failed to set capture bytecode on event notifier rule fd: fd = %d", | |
2422 | event_notifier_rule->fd); | |
2423 | error_code_ret = LTTNG_ERR_KERN_ENABLE_FAIL; | |
e8b1187d | 2424 | goto capture_error; |
bbd6675c JR |
2425 | } |
2426 | } | |
2427 | ||
352b58f5 JR |
2428 | err = kernctl_enable(event_notifier_rule->fd); |
2429 | if (err < 0) { | |
2430 | switch (-err) { | |
2431 | case EEXIST: | |
2432 | error_code_ret = LTTNG_ERR_KERN_EVENT_EXIST; | |
2433 | break; | |
2434 | default: | |
2435 | PERROR("enable kernel event notifier"); | |
2436 | error_code_ret = LTTNG_ERR_KERN_ENABLE_FAIL; | |
2437 | break; | |
2438 | } | |
2439 | goto enable_error; | |
2440 | } | |
2441 | ||
2442 | /* Add trigger to kernel token mapping in the hash table. */ | |
2443 | rcu_read_lock(); | |
2444 | cds_lfht_add(kernel_token_to_event_notifier_rule_ht, hash_trigger(trigger), | |
2445 | &event_notifier_rule->ht_node); | |
2446 | rcu_read_unlock(); | |
2447 | ||
2448 | DBG("Created kernel event notifier: name = '%s', fd = %d", | |
2449 | kernel_event_notifier.event.name, | |
2450 | event_notifier_rule->fd); | |
2451 | ||
2452 | return LTTNG_OK; | |
2453 | ||
e8b1187d | 2454 | capture_error: |
352b58f5 JR |
2455 | add_callsite_error: |
2456 | enable_error: | |
2457 | set_cloexec_error: | |
2458 | filter_error: | |
2459 | { | |
2460 | const int close_ret = close(event_notifier_rule->fd); | |
2461 | ||
2462 | if (close_ret) { | |
2463 | PERROR("Failed to close kernel event notifier file descriptor: fd = %d", | |
2464 | event_notifier_rule->fd); | |
2465 | } | |
2466 | } | |
2467 | free_event: | |
2468 | free(event_notifier_rule); | |
2469 | error: | |
2470 | return error_code_ret; | |
2471 | } | |
2472 | ||
2473 | enum lttng_error_code kernel_register_event_notifier( | |
2474 | struct lttng_trigger *trigger, | |
2475 | const struct lttng_credentials *cmd_creds) | |
2476 | { | |
2477 | enum lttng_error_code ret; | |
2478 | enum lttng_condition_status status; | |
2479 | enum lttng_domain_type domain_type; | |
2480 | const struct lttng_event_rule *event_rule; | |
2481 | const struct lttng_condition *const condition = | |
2482 | lttng_trigger_get_const_condition(trigger); | |
2483 | const uint64_t token = lttng_trigger_get_tracer_token(trigger); | |
2484 | ||
a0377dfe | 2485 | LTTNG_ASSERT(condition); |
352b58f5 JR |
2486 | |
2487 | /* Does not acquire a reference to the event rule. */ | |
8dbb86b8 | 2488 | status = lttng_condition_event_rule_matches_get_rule( |
352b58f5 | 2489 | condition, &event_rule); |
a0377dfe | 2490 | LTTNG_ASSERT(status == LTTNG_CONDITION_STATUS_OK); |
352b58f5 JR |
2491 | |
2492 | domain_type = lttng_event_rule_get_domain_type(event_rule); | |
a0377dfe | 2493 | LTTNG_ASSERT(domain_type == LTTNG_DOMAIN_KERNEL); |
352b58f5 JR |
2494 | |
2495 | ret = kernel_create_event_notifier_rule(trigger, cmd_creds, token); | |
2496 | if (ret != LTTNG_OK) { | |
124dda43 | 2497 | ERR("Failed to create kernel event notifier rule"); |
352b58f5 JR |
2498 | } |
2499 | ||
2500 | return ret; | |
2501 | } | |
2502 | ||
2503 | enum lttng_error_code kernel_unregister_event_notifier( | |
2504 | const struct lttng_trigger *trigger) | |
2505 | { | |
2506 | struct ltt_kernel_event_notifier_rule *token_event_rule_element; | |
2507 | struct cds_lfht_node *node; | |
2508 | struct cds_lfht_iter iter; | |
2509 | enum lttng_error_code error_code_ret; | |
2510 | int ret; | |
2511 | ||
2512 | rcu_read_lock(); | |
2513 | ||
2514 | cds_lfht_lookup(kernel_token_to_event_notifier_rule_ht, | |
2515 | hash_trigger(trigger), match_trigger, trigger, &iter); | |
2516 | ||
2517 | node = cds_lfht_iter_get_node(&iter); | |
2518 | if (!node) { | |
2519 | error_code_ret = LTTNG_ERR_TRIGGER_NOT_FOUND; | |
2520 | goto error; | |
2521 | } | |
2522 | ||
2523 | token_event_rule_element = caa_container_of(node, | |
2524 | struct ltt_kernel_event_notifier_rule, ht_node); | |
2525 | ||
2526 | ret = kernel_disable_event_notifier_rule(token_event_rule_element); | |
2527 | if (ret) { | |
2528 | error_code_ret = LTTNG_ERR_FATAL; | |
2529 | goto error; | |
2530 | } | |
2531 | ||
2532 | trace_kernel_destroy_event_notifier_rule(token_event_rule_element); | |
2533 | error_code_ret = LTTNG_OK; | |
2534 | ||
2535 | error: | |
2536 | rcu_read_unlock(); | |
2537 | ||
2538 | return error_code_ret; | |
2539 | } | |
2540 | ||
2541 | int kernel_get_notification_fd(void) | |
2542 | { | |
2543 | return kernel_tracer_event_notifier_group_notification_fd; | |
2544 | } |