Commit | Line | Data |
---|---|---|
54012638 | 1 | /* |
21cf9b6b | 2 | * Copyright (C) 2011 EfficiOS Inc. |
54012638 | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
54012638 | 5 | * |
54012638 DG |
6 | */ |
7 | ||
6c1c0768 | 8 | #define _LGPL_SOURCE |
28ab034a JG |
9 | #include "consumer.hpp" |
10 | #include "lttng-sessiond.hpp" | |
11 | #include "notification-thread-commands.hpp" | |
12 | #include "trace-kernel.hpp" | |
13 | ||
14 | #include <common/common.hpp> | |
15 | #include <common/defaults.hpp> | |
16 | #include <common/macros.hpp> | |
17 | #include <common/trace-chunk.hpp> | |
54012638 | 18 | |
c9e313bc | 19 | #include <lttng/event-rule/event-rule-internal.hpp> |
28ab034a | 20 | #include <lttng/event-rule/event-rule.h> |
c9e313bc | 21 | #include <lttng/event-rule/kernel-kprobe-internal.hpp> |
28ab034a | 22 | #include <lttng/event-rule/kernel-kprobe.h> |
c9e313bc | 23 | #include <lttng/event-rule/kernel-syscall-internal.hpp> |
28ab034a | 24 | #include <lttng/event-rule/kernel-syscall.h> |
c9e313bc | 25 | #include <lttng/event-rule/kernel-tracepoint-internal.hpp> |
28ab034a | 26 | #include <lttng/event-rule/kernel-tracepoint.h> |
c9e313bc | 27 | #include <lttng/event-rule/kernel-uprobe-internal.hpp> |
28ab034a JG |
28 | #include <lttng/event-rule/kernel-uprobe.h> |
29 | #include <lttng/event.h> | |
30 | #include <lttng/kernel-probe.h> | |
31 | #include <lttng/lttng-error.h> | |
32 | #include <lttng/userspace-probe-internal.hpp> | |
33 | #include <lttng/userspace-probe.h> | |
1e307fab | 34 | |
28ab034a JG |
35 | #include <stdio.h> |
36 | #include <stdlib.h> | |
37 | #include <string.h> | |
38 | #include <unistd.h> | |
54012638 | 39 | |
19e70852 | 40 | /* |
050349bb | 41 | * Find the channel name for the given kernel session. |
19e70852 | 42 | */ |
28ab034a JG |
43 | struct ltt_kernel_channel *trace_kernel_get_channel_by_name(const char *name, |
44 | struct ltt_kernel_session *session) | |
19e70852 DG |
45 | { |
46 | struct ltt_kernel_channel *chan; | |
47 | ||
a0377dfe FD |
48 | LTTNG_ASSERT(session); |
49 | LTTNG_ASSERT(name); | |
19e70852 | 50 | |
85076754 MD |
51 | /* |
52 | * If we receive an empty string for channel name, it means the | |
53 | * default channel name is requested. | |
54 | */ | |
55 | if (name[0] == '\0') | |
56 | name = DEFAULT_CHANNEL_NAME; | |
57 | ||
54d01ffb DG |
58 | DBG("Trying to find channel %s", name); |
59 | ||
28ab034a | 60 | cds_list_for_each_entry (chan, &session->channel_list.head, list) { |
19e70852 DG |
61 | if (strcmp(name, chan->channel->name) == 0) { |
62 | DBG("Found channel by name %s", name); | |
63 | return chan; | |
64 | } | |
65 | } | |
66 | ||
cd9adb8b | 67 | return nullptr; |
19e70852 DG |
68 | } |
69 | ||
00a62084 MD |
70 | /* |
71 | * Find the event for the given channel. | |
72 | */ | |
28ab034a JG |
73 | struct ltt_kernel_event *trace_kernel_find_event(char *name, |
74 | struct ltt_kernel_channel *channel, | |
75 | enum lttng_event_type type, | |
76 | struct lttng_bytecode *filter) | |
00a62084 MD |
77 | { |
78 | struct ltt_kernel_event *ev; | |
79 | int found = 0; | |
80 | ||
a0377dfe FD |
81 | LTTNG_ASSERT(name); |
82 | LTTNG_ASSERT(channel); | |
00a62084 | 83 | |
28ab034a | 84 | cds_list_for_each_entry (ev, &channel->events_list.head, list) { |
00a62084 MD |
85 | if (type != LTTNG_EVENT_ALL && ev->type != type) { |
86 | continue; | |
87 | } | |
5c7248cd | 88 | if (strcmp(name, ev->event->name) != 0) { |
00a62084 MD |
89 | continue; |
90 | } | |
91 | if ((ev->filter && !filter) || (!ev->filter && filter)) { | |
92 | continue; | |
93 | } | |
94 | if (ev->filter && filter) { | |
95 | if (ev->filter->len != filter->len || | |
28ab034a | 96 | memcmp(ev->filter->data, filter->data, filter->len) != 0) { |
00a62084 MD |
97 | continue; |
98 | } | |
99 | } | |
100 | found = 1; | |
101 | break; | |
102 | } | |
103 | if (found) { | |
28ab034a | 104 | DBG("Found event %s for channel %s", name, channel->channel->name); |
00a62084 MD |
105 | return ev; |
106 | } else { | |
cd9adb8b | 107 | return nullptr; |
00a62084 MD |
108 | } |
109 | } | |
110 | ||
19e70852 | 111 | /* |
050349bb | 112 | * Find the event name for the given channel. |
19e70852 | 113 | */ |
28ab034a JG |
114 | struct ltt_kernel_event *trace_kernel_get_event_by_name(char *name, |
115 | struct ltt_kernel_channel *channel, | |
116 | enum lttng_event_type type) | |
19e70852 DG |
117 | { |
118 | struct ltt_kernel_event *ev; | |
00a62084 | 119 | int found = 0; |
19e70852 | 120 | |
a0377dfe FD |
121 | LTTNG_ASSERT(name); |
122 | LTTNG_ASSERT(channel); | |
19e70852 | 123 | |
28ab034a | 124 | cds_list_for_each_entry (ev, &channel->events_list.head, list) { |
00a62084 | 125 | if (type != LTTNG_EVENT_ALL && ev->type != type) { |
d0ae4ea8 | 126 | continue; |
19e70852 | 127 | } |
5c7248cd | 128 | if (strcmp(name, ev->event->name) != 0) { |
00a62084 MD |
129 | continue; |
130 | } | |
131 | found = 1; | |
132 | break; | |
133 | } | |
134 | if (found) { | |
28ab034a | 135 | DBG("Found event %s for channel %s", name, channel->channel->name); |
00a62084 MD |
136 | return ev; |
137 | } else { | |
cd9adb8b | 138 | return nullptr; |
19e70852 | 139 | } |
19e70852 DG |
140 | } |
141 | ||
54012638 | 142 | /* |
050349bb | 143 | * Allocate and initialize a kernel session data structure. |
54012638 | 144 | * |
050349bb | 145 | * Return pointer to structure or NULL. |
54012638 | 146 | */ |
cd9adb8b | 147 | struct ltt_kernel_session *trace_kernel_create_session() |
54012638 | 148 | { |
cd9adb8b | 149 | struct ltt_kernel_session *lks = nullptr; |
54012638 DG |
150 | |
151 | /* Allocate a new ltt kernel session */ | |
64803277 | 152 | lks = zmalloc<ltt_kernel_session>(); |
cd9adb8b | 153 | if (lks == nullptr) { |
df0f840b | 154 | PERROR("create kernel session zmalloc"); |
a4b92340 | 155 | goto alloc_error; |
54012638 DG |
156 | } |
157 | ||
158 | /* Init data structure */ | |
03550b58 MD |
159 | lks->fd = -1; |
160 | lks->metadata_stream_fd = -1; | |
54012638 DG |
161 | lks->channel_count = 0; |
162 | lks->stream_count_global = 0; | |
cd9adb8b | 163 | lks->metadata = nullptr; |
54012638 DG |
164 | CDS_INIT_LIST_HEAD(&lks->channel_list.head); |
165 | ||
159b042f JG |
166 | lks->tracker_pid = process_attr_tracker_create(); |
167 | if (!lks->tracker_pid) { | |
55c9e7ca JR |
168 | goto error; |
169 | } | |
159b042f JG |
170 | lks->tracker_vpid = process_attr_tracker_create(); |
171 | if (!lks->tracker_vpid) { | |
55c9e7ca JR |
172 | goto error; |
173 | } | |
159b042f JG |
174 | lks->tracker_uid = process_attr_tracker_create(); |
175 | if (!lks->tracker_uid) { | |
55c9e7ca JR |
176 | goto error; |
177 | } | |
159b042f JG |
178 | lks->tracker_vuid = process_attr_tracker_create(); |
179 | if (!lks->tracker_vuid) { | |
55c9e7ca JR |
180 | goto error; |
181 | } | |
159b042f JG |
182 | lks->tracker_gid = process_attr_tracker_create(); |
183 | if (!lks->tracker_gid) { | |
55c9e7ca JR |
184 | goto error; |
185 | } | |
159b042f JG |
186 | lks->tracker_vgid = process_attr_tracker_create(); |
187 | if (!lks->tracker_vgid) { | |
55c9e7ca JR |
188 | goto error; |
189 | } | |
00e2e675 | 190 | lks->consumer = consumer_create_output(CONSUMER_DST_LOCAL); |
cd9adb8b | 191 | if (lks->consumer == nullptr) { |
00e2e675 DG |
192 | goto error; |
193 | } | |
194 | ||
54012638 DG |
195 | return lks; |
196 | ||
197 | error: | |
159b042f JG |
198 | process_attr_tracker_destroy(lks->tracker_pid); |
199 | process_attr_tracker_destroy(lks->tracker_vpid); | |
200 | process_attr_tracker_destroy(lks->tracker_uid); | |
201 | process_attr_tracker_destroy(lks->tracker_vuid); | |
202 | process_attr_tracker_destroy(lks->tracker_gid); | |
203 | process_attr_tracker_destroy(lks->tracker_vgid); | |
a4b92340 DG |
204 | free(lks); |
205 | ||
206 | alloc_error: | |
cd9adb8b | 207 | return nullptr; |
54012638 DG |
208 | } |
209 | ||
210 | /* | |
050349bb | 211 | * Allocate and initialize a kernel channel data structure. |
54012638 | 212 | * |
050349bb | 213 | * Return pointer to structure or NULL. |
54012638 | 214 | */ |
28ab034a | 215 | struct ltt_kernel_channel *trace_kernel_create_channel(struct lttng_channel *chan) |
54012638 | 216 | { |
54012638 | 217 | struct ltt_kernel_channel *lkc; |
cd9adb8b | 218 | struct lttng_channel_extended *extended = nullptr; |
54012638 | 219 | |
a0377dfe | 220 | LTTNG_ASSERT(chan); |
0525e9ae | 221 | |
64803277 | 222 | lkc = zmalloc<ltt_kernel_channel>(); |
cd9adb8b | 223 | if (lkc == nullptr) { |
df0f840b | 224 | PERROR("ltt_kernel_channel zmalloc"); |
54012638 DG |
225 | goto error; |
226 | } | |
227 | ||
64803277 | 228 | lkc->channel = zmalloc<lttng_channel>(); |
cd9adb8b | 229 | if (lkc->channel == nullptr) { |
df0f840b | 230 | PERROR("lttng_channel zmalloc"); |
e9404c27 JG |
231 | goto error; |
232 | } | |
233 | ||
64803277 | 234 | extended = zmalloc<lttng_channel_extended>(); |
e9404c27 JG |
235 | if (!extended) { |
236 | PERROR("lttng_channel_channel zmalloc"); | |
f3ed775e DG |
237 | goto error; |
238 | } | |
239 | memcpy(lkc->channel, chan, sizeof(struct lttng_channel)); | |
e9404c27 JG |
240 | memcpy(extended, chan->attr.extended.ptr, sizeof(struct lttng_channel_extended)); |
241 | lkc->channel->attr.extended.ptr = extended; | |
cd9adb8b | 242 | extended = nullptr; |
54012638 | 243 | |
85076754 MD |
244 | /* |
245 | * If we receive an empty string for channel name, it means the | |
246 | * default channel name is requested. | |
247 | */ | |
248 | if (chan->name[0] == '\0') { | |
28ab034a | 249 | strncpy(lkc->channel->name, DEFAULT_CHANNEL_NAME, sizeof(lkc->channel->name)); |
85076754 | 250 | } |
b8e2fb80 | 251 | lkc->channel->name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0'; |
85076754 | 252 | |
03550b58 | 253 | lkc->fd = -1; |
54012638 | 254 | lkc->stream_count = 0; |
cbbbb275 | 255 | lkc->event_count = 0; |
d36b8583 | 256 | lkc->enabled = 1; |
753873bf | 257 | lkc->published_to_notification_thread = false; |
54012638 DG |
258 | /* Init linked list */ |
259 | CDS_INIT_LIST_HEAD(&lkc->events_list.head); | |
260 | CDS_INIT_LIST_HEAD(&lkc->stream_list.head); | |
645328ae | 261 | CDS_INIT_LIST_HEAD(&lkc->ctx_list); |
54012638 DG |
262 | |
263 | return lkc; | |
264 | ||
265 | error: | |
e9404c27 JG |
266 | if (lkc) { |
267 | free(lkc->channel); | |
268 | } | |
269 | free(extended); | |
270 | free(lkc); | |
cd9adb8b | 271 | return nullptr; |
54012638 DG |
272 | } |
273 | ||
645328ae DG |
274 | /* |
275 | * Allocate and init a kernel context object. | |
276 | * | |
277 | * Return the allocated object or NULL on error. | |
278 | */ | |
28ab034a | 279 | struct ltt_kernel_context *trace_kernel_create_context(struct lttng_kernel_abi_context *ctx) |
645328ae DG |
280 | { |
281 | struct ltt_kernel_context *kctx; | |
282 | ||
64803277 | 283 | kctx = zmalloc<ltt_kernel_context>(); |
645328ae DG |
284 | if (!kctx) { |
285 | PERROR("zmalloc kernel context"); | |
286 | goto error; | |
287 | } | |
288 | ||
289 | if (ctx) { | |
290 | memcpy(&kctx->ctx, ctx, sizeof(kctx->ctx)); | |
291 | } | |
df3c77c8 JG |
292 | error: |
293 | return kctx; | |
294 | } | |
645328ae | 295 | |
df3c77c8 JG |
296 | /* |
297 | * Allocate and init a kernel context object from an existing kernel context | |
298 | * object. | |
299 | * | |
300 | * Return the allocated object or NULL on error. | |
301 | */ | |
28ab034a | 302 | struct ltt_kernel_context *trace_kernel_copy_context(struct ltt_kernel_context *kctx) |
df3c77c8 JG |
303 | { |
304 | struct ltt_kernel_context *kctx_copy; | |
305 | ||
a0377dfe | 306 | LTTNG_ASSERT(kctx); |
64803277 | 307 | kctx_copy = zmalloc<ltt_kernel_context>(); |
df3c77c8 JG |
308 | if (!kctx_copy) { |
309 | PERROR("zmalloc ltt_kernel_context"); | |
310 | goto error; | |
311 | } | |
312 | ||
313 | memcpy(kctx_copy, kctx, sizeof(*kctx_copy)); | |
314 | memset(&kctx_copy->list, 0, sizeof(kctx_copy->list)); | |
7b9445b3 | 315 | |
645328ae | 316 | error: |
df3c77c8 | 317 | return kctx_copy; |
645328ae DG |
318 | } |
319 | ||
54012638 | 320 | /* |
050349bb | 321 | * Allocate and initialize a kernel event. Set name and event type. |
a969e101 | 322 | * We own filter_expression, and filter. |
54012638 | 323 | * |
050349bb | 324 | * Return pointer to structure or NULL. |
54012638 | 325 | */ |
28ab034a JG |
326 | enum lttng_error_code trace_kernel_create_event(struct lttng_event *ev, |
327 | char *filter_expression, | |
328 | struct lttng_bytecode *filter, | |
329 | struct ltt_kernel_event **kernel_event) | |
54012638 | 330 | { |
71a3bb01 | 331 | enum lttng_error_code ret; |
b8e2fb80 | 332 | struct lttng_kernel_abi_event *attr; |
71a3bb01 | 333 | struct ltt_kernel_event *local_kernel_event; |
cd9adb8b | 334 | struct lttng_userspace_probe_location *userspace_probe_location = nullptr; |
54012638 | 335 | |
a0377dfe | 336 | LTTNG_ASSERT(ev); |
0525e9ae | 337 | |
64803277 SM |
338 | local_kernel_event = zmalloc<ltt_kernel_event>(); |
339 | attr = zmalloc<lttng_kernel_abi_event>(); | |
cd9adb8b | 340 | if (local_kernel_event == nullptr || attr == nullptr) { |
df0f840b | 341 | PERROR("kernel event zmalloc"); |
71a3bb01 | 342 | ret = LTTNG_ERR_NOMEM; |
54012638 DG |
343 | goto error; |
344 | } | |
345 | ||
f3ed775e | 346 | switch (ev->type) { |
7d29a247 | 347 | case LTTNG_EVENT_PROBE: |
b8e2fb80 | 348 | attr->instrumentation = LTTNG_KERNEL_ABI_KPROBE; |
7d29a247 DG |
349 | attr->u.kprobe.addr = ev->attr.probe.addr; |
350 | attr->u.kprobe.offset = ev->attr.probe.offset; | |
f3ed775e | 351 | strncpy(attr->u.kprobe.symbol_name, |
28ab034a JG |
352 | ev->attr.probe.symbol_name, |
353 | LTTNG_KERNEL_ABI_SYM_NAME_LEN); | |
b8e2fb80 | 354 | attr->u.kprobe.symbol_name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0'; |
f3ed775e | 355 | break; |
dcabc190 FD |
356 | case LTTNG_EVENT_USERSPACE_PROBE: |
357 | { | |
cd9adb8b JG |
358 | const struct lttng_userspace_probe_location *location = nullptr; |
359 | const struct lttng_userspace_probe_location_lookup_method *lookup = nullptr; | |
dcabc190 FD |
360 | |
361 | location = lttng_event_get_userspace_probe_location(ev); | |
362 | if (!location) { | |
363 | ret = LTTNG_ERR_PROBE_LOCATION_INVAL; | |
364 | goto error; | |
365 | } | |
366 | ||
367 | /* | |
368 | * From this point on, the specific term 'uprobe' is used | |
369 | * instead of the generic 'userspace probe' because it's the | |
370 | * technology used at the moment for this instrumentation. | |
371 | * LTTng currently implements userspace probes using uprobes. | |
372 | * In the interactions with the kernel tracer, we use the | |
373 | * uprobe term. | |
374 | */ | |
b8e2fb80 | 375 | attr->instrumentation = LTTNG_KERNEL_ABI_UPROBE; |
dcabc190 | 376 | |
28ab034a | 377 | lookup = lttng_userspace_probe_location_get_lookup_method(location); |
dcabc190 FD |
378 | if (!lookup) { |
379 | ret = LTTNG_ERR_PROBE_LOCATION_INVAL; | |
380 | goto error; | |
381 | } | |
382 | ||
383 | /* | |
384 | * From the kernel tracer's perspective, all userspace probe | |
385 | * event types are all the same: a file and an offset. | |
386 | */ | |
387 | switch (lttng_userspace_probe_location_lookup_method_get_type(lookup)) { | |
388 | case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF: | |
389 | /* Get the file descriptor on the target binary. */ | |
390 | attr->u.uprobe.fd = | |
28ab034a | 391 | lttng_userspace_probe_location_function_get_binary_fd(location); |
dcabc190 FD |
392 | |
393 | /* | |
394 | * Save a reference to the probe location used during | |
e368fb43 | 395 | * the listing of events. |
dcabc190 | 396 | */ |
28ab034a | 397 | userspace_probe_location = lttng_userspace_probe_location_copy(location); |
dcabc190 FD |
398 | break; |
399 | case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT: | |
400 | /* Get the file descriptor on the target binary. */ | |
401 | attr->u.uprobe.fd = | |
28ab034a | 402 | lttng_userspace_probe_location_tracepoint_get_binary_fd(location); |
dcabc190 FD |
403 | |
404 | /* | |
405 | * Save a reference to the probe location used during the listing of | |
e368fb43 | 406 | * events. |
dcabc190 | 407 | */ |
28ab034a | 408 | userspace_probe_location = lttng_userspace_probe_location_copy(location); |
dcabc190 FD |
409 | break; |
410 | default: | |
411 | DBG("Unsupported lookup method type"); | |
412 | ret = LTTNG_ERR_PROBE_LOCATION_INVAL; | |
413 | goto error; | |
414 | } | |
415 | break; | |
416 | } | |
f3ed775e | 417 | case LTTNG_EVENT_FUNCTION: |
b8e2fb80 | 418 | attr->instrumentation = LTTNG_KERNEL_ABI_KRETPROBE; |
8f0d098b MD |
419 | attr->u.kretprobe.addr = ev->attr.probe.addr; |
420 | attr->u.kretprobe.offset = ev->attr.probe.offset; | |
8f0d098b | 421 | strncpy(attr->u.kretprobe.symbol_name, |
28ab034a JG |
422 | ev->attr.probe.symbol_name, |
423 | LTTNG_KERNEL_ABI_SYM_NAME_LEN); | |
b8e2fb80 | 424 | attr->u.kretprobe.symbol_name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0'; |
8f0d098b MD |
425 | break; |
426 | case LTTNG_EVENT_FUNCTION_ENTRY: | |
b8e2fb80 | 427 | attr->instrumentation = LTTNG_KERNEL_ABI_FUNCTION; |
f3ed775e | 428 | strncpy(attr->u.ftrace.symbol_name, |
28ab034a JG |
429 | ev->attr.ftrace.symbol_name, |
430 | LTTNG_KERNEL_ABI_SYM_NAME_LEN); | |
b8e2fb80 | 431 | attr->u.ftrace.symbol_name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0'; |
f3ed775e | 432 | break; |
e6ddca71 | 433 | case LTTNG_EVENT_TRACEPOINT: |
b8e2fb80 | 434 | attr->instrumentation = LTTNG_KERNEL_ABI_TRACEPOINT; |
f3ed775e | 435 | break; |
a54bd42d | 436 | case LTTNG_EVENT_SYSCALL: |
b8e2fb80 FD |
437 | attr->instrumentation = LTTNG_KERNEL_ABI_SYSCALL; |
438 | attr->u.syscall.abi = LTTNG_KERNEL_ABI_SYSCALL_ABI_ALL; | |
439 | attr->u.syscall.entryexit = LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT; | |
440 | attr->u.syscall.match = LTTNG_KERNEL_ABI_SYSCALL_MATCH_NAME; | |
0133c199 | 441 | break; |
7a3d1328 | 442 | case LTTNG_EVENT_ALL: |
b8e2fb80 | 443 | attr->instrumentation = LTTNG_KERNEL_ABI_ALL; |
7a3d1328 | 444 | break; |
f3ed775e DG |
445 | default: |
446 | ERR("Unknown kernel instrumentation type (%d)", ev->type); | |
71a3bb01 | 447 | ret = LTTNG_ERR_INVALID; |
f3ed775e DG |
448 | goto error; |
449 | } | |
450 | ||
451 | /* Copy event name */ | |
b8e2fb80 FD |
452 | strncpy(attr->name, ev->name, LTTNG_KERNEL_ABI_SYM_NAME_LEN); |
453 | attr->name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0'; | |
f3ed775e | 454 | |
54012638 | 455 | /* Setting up a kernel event */ |
71a3bb01 FD |
456 | local_kernel_event->fd = -1; |
457 | local_kernel_event->event = attr; | |
458 | local_kernel_event->enabled = 1; | |
459 | local_kernel_event->filter_expression = filter_expression; | |
460 | local_kernel_event->filter = filter; | |
dcabc190 | 461 | local_kernel_event->userspace_probe_location = userspace_probe_location; |
54012638 | 462 | |
71a3bb01 FD |
463 | *kernel_event = local_kernel_event; |
464 | ||
465 | return LTTNG_OK; | |
54012638 DG |
466 | |
467 | error: | |
a969e101 MD |
468 | free(filter_expression); |
469 | free(filter); | |
71a3bb01 | 470 | free(local_kernel_event); |
a2c0da86 | 471 | free(attr); |
71a3bb01 | 472 | return ret; |
54012638 DG |
473 | } |
474 | ||
352b58f5 JR |
475 | /* |
476 | * Allocate and initialize a kernel token event rule. | |
477 | * | |
478 | * Return pointer to structure or NULL. | |
479 | */ | |
28ab034a JG |
480 | enum lttng_error_code |
481 | trace_kernel_create_event_notifier_rule(struct lttng_trigger *trigger, | |
482 | uint64_t token, | |
483 | uint64_t error_counter_index, | |
484 | struct ltt_kernel_event_notifier_rule **event_notifier_rule) | |
352b58f5 JR |
485 | { |
486 | enum lttng_error_code ret = LTTNG_OK; | |
487 | enum lttng_condition_type condition_type; | |
488 | enum lttng_event_rule_type event_rule_type; | |
489 | enum lttng_condition_status condition_status; | |
490 | struct ltt_kernel_event_notifier_rule *local_kernel_token_event_rule; | |
cd9adb8b JG |
491 | const struct lttng_condition *condition = nullptr; |
492 | const struct lttng_event_rule *event_rule = nullptr; | |
352b58f5 | 493 | |
a0377dfe | 494 | LTTNG_ASSERT(event_notifier_rule); |
352b58f5 | 495 | |
7c1f6da2 | 496 | condition = lttng_trigger_get_const_condition(trigger); |
a0377dfe | 497 | LTTNG_ASSERT(condition); |
352b58f5 JR |
498 | |
499 | condition_type = lttng_condition_get_type(condition); | |
a0377dfe | 500 | LTTNG_ASSERT(condition_type == LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES); |
352b58f5 | 501 | |
28ab034a | 502 | condition_status = lttng_condition_event_rule_matches_get_rule(condition, &event_rule); |
a0377dfe FD |
503 | LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK); |
504 | LTTNG_ASSERT(event_rule); | |
352b58f5 JR |
505 | |
506 | event_rule_type = lttng_event_rule_get_type(event_rule); | |
a0377dfe | 507 | LTTNG_ASSERT(event_rule_type != LTTNG_EVENT_RULE_TYPE_UNKNOWN); |
352b58f5 | 508 | |
28ab034a | 509 | local_kernel_token_event_rule = zmalloc<ltt_kernel_event_notifier_rule>(); |
cd9adb8b | 510 | if (local_kernel_token_event_rule == nullptr) { |
352b58f5 JR |
511 | PERROR("Failed to allocate ltt_kernel_token_event_rule structure"); |
512 | ret = LTTNG_ERR_NOMEM; | |
513 | goto error; | |
514 | } | |
515 | ||
516 | local_kernel_token_event_rule->fd = -1; | |
517 | local_kernel_token_event_rule->enabled = 1; | |
518 | local_kernel_token_event_rule->token = token; | |
90aa04a1 | 519 | local_kernel_token_event_rule->error_counter_index = error_counter_index; |
352b58f5 JR |
520 | |
521 | /* Get the reference of the event rule. */ | |
522 | lttng_trigger_get(trigger); | |
523 | ||
524 | local_kernel_token_event_rule->trigger = trigger; | |
525 | /* The event rule still owns the filter and bytecode. */ | |
28ab034a | 526 | local_kernel_token_event_rule->filter = lttng_event_rule_get_filter_bytecode(event_rule); |
352b58f5 JR |
527 | |
528 | DBG3("Created kernel event notifier rule: token = %" PRIu64, | |
28ab034a | 529 | local_kernel_token_event_rule->token); |
352b58f5 JR |
530 | error: |
531 | *event_notifier_rule = local_kernel_token_event_rule; | |
532 | return ret; | |
533 | } | |
534 | ||
535 | /* | |
536 | * Initialize a kernel trigger from an event rule. | |
537 | */ | |
538 | enum lttng_error_code trace_kernel_init_event_notifier_from_event_rule( | |
28ab034a JG |
539 | const struct lttng_event_rule *rule, |
540 | struct lttng_kernel_abi_event_notifier *kernel_event_notifier) | |
352b58f5 | 541 | { |
366a83b3 | 542 | enum lttng_error_code ret_code; |
352b58f5 | 543 | const char *name; |
366a83b3 | 544 | int strncpy_ret; |
352b58f5 JR |
545 | |
546 | switch (lttng_event_rule_get_type(rule)) { | |
85522de5 | 547 | case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE: |
352b58f5 JR |
548 | { |
549 | uint64_t address = 0, offset = 0; | |
cd9adb8b JG |
550 | const char *symbol_name = nullptr; |
551 | const struct lttng_kernel_probe_location *location = nullptr; | |
352b58f5 JR |
552 | enum lttng_kernel_probe_location_status k_status; |
553 | enum lttng_event_rule_status status; | |
554 | ||
85522de5 | 555 | status = lttng_event_rule_kernel_kprobe_get_location(rule, &location); |
352b58f5 | 556 | if (status != LTTNG_EVENT_RULE_STATUS_OK) { |
366a83b3 | 557 | ret_code = LTTNG_ERR_PROBE_LOCATION_INVAL; |
352b58f5 JR |
558 | goto error; |
559 | } | |
560 | ||
561 | switch (lttng_kernel_probe_location_get_type(location)) { | |
562 | case LTTNG_KERNEL_PROBE_LOCATION_TYPE_ADDRESS: | |
563 | { | |
28ab034a JG |
564 | k_status = |
565 | lttng_kernel_probe_location_address_get_address(location, &address); | |
a0377dfe | 566 | LTTNG_ASSERT(k_status == LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK); |
352b58f5 JR |
567 | break; |
568 | } | |
569 | case LTTNG_KERNEL_PROBE_LOCATION_TYPE_SYMBOL_OFFSET: | |
570 | { | |
28ab034a | 571 | k_status = lttng_kernel_probe_location_symbol_get_offset(location, &offset); |
a0377dfe | 572 | LTTNG_ASSERT(k_status == LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK); |
28ab034a | 573 | symbol_name = lttng_kernel_probe_location_symbol_get_name(location); |
352b58f5 JR |
574 | break; |
575 | } | |
576 | default: | |
577 | abort(); | |
578 | } | |
579 | ||
b8e2fb80 | 580 | kernel_event_notifier->event.instrumentation = LTTNG_KERNEL_ABI_KPROBE; |
352b58f5 JR |
581 | kernel_event_notifier->event.u.kprobe.addr = address; |
582 | kernel_event_notifier->event.u.kprobe.offset = offset; | |
583 | if (symbol_name) { | |
366a83b3 | 584 | strncpy_ret = lttng_strncpy( |
28ab034a JG |
585 | kernel_event_notifier->event.u.kprobe.symbol_name, |
586 | symbol_name, | |
587 | sizeof(kernel_event_notifier->event.u.kprobe.symbol_name)); | |
352b58f5 | 588 | |
366a83b3 JG |
589 | if (strncpy_ret) { |
590 | ret_code = LTTNG_ERR_INVALID; | |
352b58f5 JR |
591 | goto error; |
592 | } | |
593 | } | |
594 | ||
28ab034a JG |
595 | kernel_event_notifier->event.u.kprobe |
596 | .symbol_name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0'; | |
f2791161 | 597 | |
85522de5 | 598 | status = lttng_event_rule_kernel_kprobe_get_event_name(rule, &name); |
a0377dfe | 599 | LTTNG_ASSERT(status == LTTNG_EVENT_RULE_STATUS_OK); |
366a83b3 | 600 | ret_code = LTTNG_OK; |
352b58f5 JR |
601 | break; |
602 | } | |
46fd07ac | 603 | case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE: |
352b58f5 | 604 | { |
cd9adb8b JG |
605 | const struct lttng_userspace_probe_location *location = nullptr; |
606 | const struct lttng_userspace_probe_location_lookup_method *lookup = nullptr; | |
352b58f5 JR |
607 | enum lttng_event_rule_status status; |
608 | ||
46fd07ac | 609 | status = lttng_event_rule_kernel_uprobe_get_location(rule, &location); |
352b58f5 | 610 | if (status != LTTNG_EVENT_RULE_STATUS_OK) { |
366a83b3 | 611 | ret_code = LTTNG_ERR_PROBE_LOCATION_INVAL; |
352b58f5 JR |
612 | goto error; |
613 | } | |
614 | ||
b8e2fb80 | 615 | kernel_event_notifier->event.instrumentation = LTTNG_KERNEL_ABI_UPROBE; |
352b58f5 | 616 | |
28ab034a | 617 | lookup = lttng_userspace_probe_location_get_lookup_method(location); |
352b58f5 | 618 | if (!lookup) { |
366a83b3 | 619 | ret_code = LTTNG_ERR_PROBE_LOCATION_INVAL; |
352b58f5 JR |
620 | goto error; |
621 | } | |
622 | ||
623 | /* | |
624 | * From the kernel tracer's perspective, all userspace probe | |
625 | * event types are all the same: a file and an offset. | |
626 | */ | |
627 | switch (lttng_userspace_probe_location_lookup_method_get_type(lookup)) { | |
628 | case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF: | |
629 | /* Get the file descriptor on the target binary. */ | |
630 | kernel_event_notifier->event.u.uprobe.fd = | |
28ab034a | 631 | lttng_userspace_probe_location_function_get_binary_fd(location); |
352b58f5 JR |
632 | |
633 | break; | |
634 | case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT: | |
635 | /* Get the file descriptor on the target binary. */ | |
636 | kernel_event_notifier->event.u.uprobe.fd = | |
28ab034a | 637 | lttng_userspace_probe_location_tracepoint_get_binary_fd(location); |
352b58f5 JR |
638 | break; |
639 | default: | |
640 | abort(); | |
641 | } | |
642 | ||
28ab034a | 643 | status = lttng_event_rule_kernel_uprobe_get_event_name(rule, &name); |
a0377dfe | 644 | LTTNG_ASSERT(status == LTTNG_EVENT_RULE_STATUS_OK); |
366a83b3 | 645 | ret_code = LTTNG_OK; |
352b58f5 JR |
646 | break; |
647 | } | |
695f7044 | 648 | case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT: |
352b58f5 | 649 | { |
352b58f5 | 650 | const enum lttng_event_rule_status status = |
28ab034a | 651 | lttng_event_rule_kernel_tracepoint_get_name_pattern(rule, &name); |
352b58f5 | 652 | |
a0377dfe | 653 | LTTNG_ASSERT(status == LTTNG_EVENT_RULE_STATUS_OK); |
28ab034a | 654 | kernel_event_notifier->event.instrumentation = LTTNG_KERNEL_ABI_TRACEPOINT; |
352b58f5 | 655 | |
366a83b3 | 656 | ret_code = LTTNG_OK; |
352b58f5 JR |
657 | break; |
658 | } | |
4f7da553 | 659 | case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL: |
352b58f5 JR |
660 | { |
661 | const enum lttng_event_rule_status status = | |
28ab034a JG |
662 | lttng_event_rule_kernel_syscall_get_name_pattern(rule, &name); |
663 | const enum lttng_event_rule_kernel_syscall_emission_site emission_site = | |
4f7da553 | 664 | lttng_event_rule_kernel_syscall_get_emission_site(rule); |
b8e2fb80 | 665 | enum lttng_kernel_abi_syscall_entryexit entryexit; |
352b58f5 | 666 | |
a0377dfe | 667 | LTTNG_ASSERT(status == LTTNG_EVENT_RULE_STATUS_OK); |
28ab034a JG |
668 | LTTNG_ASSERT(emission_site != |
669 | LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_UNKNOWN); | |
57739a6b | 670 | |
28ab034a | 671 | switch (emission_site) { |
4f7da553 | 672 | case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY: |
b8e2fb80 | 673 | entryexit = LTTNG_KERNEL_ABI_SYSCALL_ENTRY; |
57739a6b | 674 | break; |
4f7da553 | 675 | case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_EXIT: |
b8e2fb80 | 676 | entryexit = LTTNG_KERNEL_ABI_SYSCALL_EXIT; |
57739a6b | 677 | break; |
4f7da553 | 678 | case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY_EXIT: |
b8e2fb80 | 679 | entryexit = LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT; |
57739a6b JR |
680 | break; |
681 | default: | |
682 | abort(); | |
683 | break; | |
684 | } | |
352b58f5 | 685 | |
28ab034a JG |
686 | kernel_event_notifier->event.instrumentation = LTTNG_KERNEL_ABI_SYSCALL; |
687 | kernel_event_notifier->event.u.syscall.abi = LTTNG_KERNEL_ABI_SYSCALL_ABI_ALL; | |
688 | kernel_event_notifier->event.u.syscall.entryexit = entryexit; | |
689 | kernel_event_notifier->event.u.syscall.match = LTTNG_KERNEL_ABI_SYSCALL_MATCH_NAME; | |
366a83b3 | 690 | ret_code = LTTNG_OK; |
352b58f5 JR |
691 | break; |
692 | } | |
352b58f5 JR |
693 | default: |
694 | abort(); | |
695 | break; | |
696 | } | |
697 | ||
28ab034a JG |
698 | strncpy_ret = lttng_strncpy( |
699 | kernel_event_notifier->event.name, name, LTTNG_KERNEL_ABI_SYM_NAME_LEN); | |
366a83b3 JG |
700 | if (strncpy_ret) { |
701 | ret_code = LTTNG_ERR_INVALID; | |
352b58f5 JR |
702 | goto error; |
703 | } | |
704 | ||
705 | error: | |
366a83b3 | 706 | return ret_code; |
352b58f5 | 707 | } |
54012638 | 708 | /* |
050349bb | 709 | * Allocate and initialize a kernel metadata. |
54012638 | 710 | * |
050349bb | 711 | * Return pointer to structure or NULL. |
54012638 | 712 | */ |
cd9adb8b | 713 | struct ltt_kernel_metadata *trace_kernel_create_metadata() |
54012638 | 714 | { |
d42266a4 | 715 | int ret; |
54012638 | 716 | struct ltt_kernel_metadata *lkm; |
f3ed775e | 717 | struct lttng_channel *chan; |
54012638 | 718 | |
64803277 SM |
719 | lkm = zmalloc<ltt_kernel_metadata>(); |
720 | chan = zmalloc<lttng_channel>(); | |
cd9adb8b | 721 | if (lkm == nullptr || chan == nullptr) { |
df0f840b | 722 | PERROR("kernel metadata zmalloc"); |
54012638 DG |
723 | goto error; |
724 | } | |
725 | ||
28ab034a | 726 | ret = lttng_strncpy(chan->name, DEFAULT_METADATA_NAME, sizeof(chan->name)); |
d42266a4 | 727 | if (ret) { |
28ab034a | 728 | ERR("Failed to initialize metadata channel name to `%s`", DEFAULT_METADATA_NAME); |
d42266a4 JG |
729 | goto error; |
730 | } | |
731 | ||
54012638 | 732 | /* Set default attributes */ |
d42266a4 | 733 | chan->attr.overwrite = DEFAULT_METADATA_OVERWRITE; |
3e230f92 | 734 | chan->attr.subbuf_size = default_get_metadata_subbuf_size(); |
b389abbe | 735 | chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM; |
d42266a4 | 736 | chan->attr.switch_timer_interval = DEFAULT_METADATA_SWITCH_TIMER; |
28ab034a JG |
737 | chan->attr.read_timer_interval = DEFAULT_METADATA_READ_TIMER; |
738 | ; | |
d42266a4 JG |
739 | |
740 | /* | |
741 | * The metadata channel of kernel sessions must use the "mmap" | |
742 | * back-end since the consumer daemon accumulates complete | |
743 | * metadata units before sending them to the relay daemon in | |
744 | * live mode. The consumer daemon also needs to extract the contents | |
745 | * of the metadata cache when computing a rotation position. | |
746 | * | |
747 | * In both cases, it is not possible to rely on the splice | |
748 | * back-end as the consumer daemon may need to accumulate more | |
749 | * content than can be backed by the ring buffer's underlying | |
750 | * pages. | |
751 | */ | |
752 | chan->attr.output = LTTNG_EVENT_MMAP; | |
753 | chan->attr.tracefile_size = 0; | |
754 | chan->attr.tracefile_count = 0; | |
755 | chan->attr.live_timer_interval = 0; | |
54012638 DG |
756 | |
757 | /* Init metadata */ | |
03550b58 | 758 | lkm->fd = -1; |
f3ed775e | 759 | lkm->conf = chan; |
54012638 DG |
760 | |
761 | return lkm; | |
762 | ||
763 | error: | |
a2c0da86 MD |
764 | free(lkm); |
765 | free(chan); | |
cd9adb8b | 766 | return nullptr; |
54012638 DG |
767 | } |
768 | ||
769 | /* | |
050349bb DG |
770 | * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by |
771 | * default. | |
54012638 | 772 | * |
050349bb | 773 | * Return pointer to structure or NULL. |
54012638 | 774 | */ |
28ab034a | 775 | struct ltt_kernel_stream *trace_kernel_create_stream(const char *name, unsigned int count) |
54012638 | 776 | { |
00e2e675 | 777 | int ret; |
54012638 DG |
778 | struct ltt_kernel_stream *lks; |
779 | ||
a0377dfe | 780 | LTTNG_ASSERT(name); |
0525e9ae | 781 | |
64803277 | 782 | lks = zmalloc<ltt_kernel_stream>(); |
cd9adb8b | 783 | if (lks == nullptr) { |
df0f840b | 784 | PERROR("kernel stream zmalloc"); |
54012638 DG |
785 | goto error; |
786 | } | |
787 | ||
00e2e675 | 788 | /* Set name */ |
535b8ff4 | 789 | ret = snprintf(lks->name, sizeof(lks->name), "%s_%u", name, count); |
00e2e675 DG |
790 | if (ret < 0) { |
791 | PERROR("snprintf stream name"); | |
792 | goto error; | |
793 | } | |
794 | lks->name[sizeof(lks->name) - 1] = '\0'; | |
795 | ||
54012638 | 796 | /* Init stream */ |
03550b58 | 797 | lks->fd = -1; |
54012638 | 798 | lks->state = 0; |
ffe60014 | 799 | lks->cpu = count; |
54012638 DG |
800 | |
801 | return lks; | |
802 | ||
803 | error: | |
cd9adb8b | 804 | return nullptr; |
54012638 | 805 | } |
c363b55d | 806 | |
050349bb DG |
807 | /* |
808 | * Cleanup kernel stream structure. | |
809 | */ | |
62499ad6 | 810 | void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream) |
c363b55d | 811 | { |
a0377dfe | 812 | LTTNG_ASSERT(stream); |
0525e9ae | 813 | |
33a2b854 | 814 | DBG("[trace] Closing stream fd %d", stream->fd); |
c363b55d | 815 | /* Close kernel fd */ |
03550b58 | 816 | if (stream->fd >= 0) { |
c617c0c6 MD |
817 | int ret; |
818 | ||
03550b58 MD |
819 | ret = close(stream->fd); |
820 | if (ret) { | |
821 | PERROR("close"); | |
822 | } | |
799e2c4f | 823 | } |
c363b55d DG |
824 | /* Remove from stream list */ |
825 | cds_list_del(&stream->list); | |
f9815039 | 826 | |
c363b55d DG |
827 | free(stream); |
828 | } | |
829 | ||
050349bb DG |
830 | /* |
831 | * Cleanup kernel event structure. | |
832 | */ | |
62499ad6 | 833 | void trace_kernel_destroy_event(struct ltt_kernel_event *event) |
c363b55d | 834 | { |
a0377dfe | 835 | LTTNG_ASSERT(event); |
0525e9ae | 836 | |
87eb4ab8 | 837 | if (event->fd >= 0) { |
c617c0c6 MD |
838 | int ret; |
839 | ||
87eb4ab8 MD |
840 | DBG("[trace] Closing event fd %d", event->fd); |
841 | /* Close kernel fd */ | |
799e2c4f MD |
842 | ret = close(event->fd); |
843 | if (ret) { | |
844 | PERROR("close"); | |
845 | } | |
87eb4ab8 | 846 | } else { |
352b58f5 | 847 | DBG("[trace] Tearing down event (no associated file descriptor)"); |
87eb4ab8 | 848 | } |
c363b55d DG |
849 | |
850 | /* Remove from event list */ | |
851 | cds_list_del(&event->list); | |
f9815039 | 852 | |
00a62084 MD |
853 | free(event->filter_expression); |
854 | free(event->filter); | |
855 | ||
f9815039 | 856 | free(event->event); |
c363b55d DG |
857 | free(event); |
858 | } | |
859 | ||
352b58f5 JR |
860 | /* |
861 | * Cleanup kernel event structure. | |
862 | */ | |
863 | static void free_token_event_rule_rcu(struct rcu_head *rcu_node) | |
864 | { | |
28ab034a JG |
865 | struct ltt_kernel_event_notifier_rule *rule = |
866 | caa_container_of(rcu_node, struct ltt_kernel_event_notifier_rule, rcu_node); | |
352b58f5 JR |
867 | |
868 | free(rule); | |
869 | } | |
870 | ||
28ab034a | 871 | void trace_kernel_destroy_event_notifier_rule(struct ltt_kernel_event_notifier_rule *event) |
352b58f5 | 872 | { |
a0377dfe | 873 | LTTNG_ASSERT(event); |
352b58f5 JR |
874 | |
875 | if (event->fd >= 0) { | |
876 | const int ret = close(event->fd); | |
877 | ||
28ab034a | 878 | DBG("Closing kernel event notifier rule file descriptor: fd = %d", event->fd); |
352b58f5 JR |
879 | if (ret) { |
880 | PERROR("Failed to close kernel event notifier file descriptor: fd = %d", | |
28ab034a | 881 | event->fd); |
352b58f5 JR |
882 | } |
883 | } else { | |
884 | DBG("Destroying kernel event notifier rule (no associated file descriptor)"); | |
885 | } | |
886 | ||
887 | lttng_trigger_put(event->trigger); | |
888 | call_rcu(&event->rcu_node, free_token_event_rule_rcu); | |
889 | } | |
645328ae DG |
890 | /* |
891 | * Cleanup kernel context structure. | |
892 | */ | |
893 | void trace_kernel_destroy_context(struct ltt_kernel_context *ctx) | |
894 | { | |
a0377dfe | 895 | LTTNG_ASSERT(ctx); |
645328ae | 896 | |
ba985c3a JG |
897 | if (ctx->in_list) { |
898 | cds_list_del(&ctx->list); | |
899 | } | |
645328ae DG |
900 | free(ctx); |
901 | } | |
902 | ||
050349bb DG |
903 | /* |
904 | * Cleanup kernel channel structure. | |
905 | */ | |
62499ad6 | 906 | void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel) |
c363b55d | 907 | { |
af9737e9 DG |
908 | struct ltt_kernel_stream *stream, *stmp; |
909 | struct ltt_kernel_event *event, *etmp; | |
645328ae | 910 | struct ltt_kernel_context *ctx, *ctmp; |
799e2c4f | 911 | int ret; |
e9404c27 | 912 | enum lttng_error_code status; |
c363b55d | 913 | |
a0377dfe | 914 | LTTNG_ASSERT(channel); |
0525e9ae | 915 | |
33a2b854 | 916 | DBG("[trace] Closing channel fd %d", channel->fd); |
c363b55d | 917 | /* Close kernel fd */ |
03550b58 MD |
918 | if (channel->fd >= 0) { |
919 | ret = close(channel->fd); | |
920 | if (ret) { | |
921 | PERROR("close"); | |
922 | } | |
799e2c4f | 923 | } |
c363b55d DG |
924 | |
925 | /* For each stream in the channel list */ | |
28ab034a | 926 | cds_list_for_each_entry_safe (stream, stmp, &channel->stream_list.head, list) { |
62499ad6 | 927 | trace_kernel_destroy_stream(stream); |
c363b55d DG |
928 | } |
929 | ||
930 | /* For each event in the channel list */ | |
28ab034a | 931 | cds_list_for_each_entry_safe (event, etmp, &channel->events_list.head, list) { |
62499ad6 | 932 | trace_kernel_destroy_event(event); |
c363b55d DG |
933 | } |
934 | ||
645328ae | 935 | /* For each context in the channel list */ |
28ab034a | 936 | cds_list_for_each_entry_safe (ctx, ctmp, &channel->ctx_list, list) { |
645328ae DG |
937 | trace_kernel_destroy_context(ctx); |
938 | } | |
939 | ||
c363b55d DG |
940 | /* Remove from channel list */ |
941 | cds_list_del(&channel->list); | |
f9815039 | 942 | |
28ab034a | 943 | if (the_notification_thread_handle && channel->published_to_notification_thread) { |
63aaa3dc | 944 | status = notification_thread_command_remove_channel( |
28ab034a | 945 | the_notification_thread_handle, channel->key, LTTNG_DOMAIN_KERNEL); |
a0377dfe | 946 | LTTNG_ASSERT(status == LTTNG_OK); |
63aaa3dc | 947 | } |
e9404c27 | 948 | free(channel->channel->attr.extended.ptr); |
f9815039 | 949 | free(channel->channel); |
c363b55d DG |
950 | free(channel); |
951 | } | |
952 | ||
050349bb DG |
953 | /* |
954 | * Cleanup kernel metadata structure. | |
955 | */ | |
62499ad6 | 956 | void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata) |
c363b55d | 957 | { |
a0377dfe | 958 | LTTNG_ASSERT(metadata); |
0525e9ae | 959 | |
33a2b854 | 960 | DBG("[trace] Closing metadata fd %d", metadata->fd); |
c363b55d | 961 | /* Close kernel fd */ |
03550b58 | 962 | if (metadata->fd >= 0) { |
c617c0c6 MD |
963 | int ret; |
964 | ||
03550b58 MD |
965 | ret = close(metadata->fd); |
966 | if (ret) { | |
967 | PERROR("close"); | |
968 | } | |
799e2c4f | 969 | } |
c363b55d | 970 | |
f9815039 | 971 | free(metadata->conf); |
c363b55d DG |
972 | free(metadata); |
973 | } | |
974 | ||
050349bb | 975 | /* |
62499ad6 | 976 | * Cleanup kernel session structure |
050349bb | 977 | */ |
62499ad6 | 978 | void trace_kernel_destroy_session(struct ltt_kernel_session *session) |
c363b55d | 979 | { |
af9737e9 | 980 | struct ltt_kernel_channel *channel, *ctmp; |
799e2c4f | 981 | int ret; |
c363b55d | 982 | |
a0377dfe | 983 | LTTNG_ASSERT(session); |
0525e9ae | 984 | |
33a2b854 | 985 | DBG("[trace] Closing session fd %d", session->fd); |
c363b55d | 986 | /* Close kernel fds */ |
03550b58 MD |
987 | if (session->fd >= 0) { |
988 | ret = close(session->fd); | |
989 | if (ret) { | |
990 | PERROR("close"); | |
991 | } | |
799e2c4f | 992 | } |
f9815039 | 993 | |
03550b58 | 994 | if (session->metadata_stream_fd >= 0) { |
70dc1c34 | 995 | DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd); |
799e2c4f MD |
996 | ret = close(session->metadata_stream_fd); |
997 | if (ret) { | |
998 | PERROR("close"); | |
999 | } | |
70dc1c34 | 1000 | } |
c363b55d | 1001 | |
cd9adb8b | 1002 | if (session->metadata != nullptr) { |
62499ad6 | 1003 | trace_kernel_destroy_metadata(session->metadata); |
d36b8583 | 1004 | } |
c363b55d | 1005 | |
28ab034a | 1006 | cds_list_for_each_entry_safe (channel, ctmp, &session->channel_list.head, list) { |
62499ad6 | 1007 | trace_kernel_destroy_channel(channel); |
c363b55d | 1008 | } |
d070c424 | 1009 | } |
c363b55d | 1010 | |
d070c424 MD |
1011 | /* Free elements needed by destroy notifiers. */ |
1012 | void trace_kernel_free_session(struct ltt_kernel_session *session) | |
1013 | { | |
00e2e675 | 1014 | /* Wipe consumer output object */ |
6addfa37 | 1015 | consumer_output_put(session->consumer); |
00e2e675 | 1016 | |
159b042f JG |
1017 | process_attr_tracker_destroy(session->tracker_pid); |
1018 | process_attr_tracker_destroy(session->tracker_vpid); | |
1019 | process_attr_tracker_destroy(session->tracker_uid); | |
1020 | process_attr_tracker_destroy(session->tracker_vuid); | |
1021 | process_attr_tracker_destroy(session->tracker_gid); | |
1022 | process_attr_tracker_destroy(session->tracker_vgid); | |
55c9e7ca | 1023 | |
c363b55d DG |
1024 | free(session); |
1025 | } |