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