Commit | Line | Data |
---|---|---|
54012638 DG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * | |
d14d33bf AM |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License, version 2 only, | |
6 | * as published by the Free Software Foundation. | |
54012638 DG |
7 | * |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | * | |
d14d33bf AM |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., | |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
54012638 DG |
16 | */ |
17 | ||
6c1c0768 | 18 | #define _LGPL_SOURCE |
54012638 DG |
19 | #include <stdio.h> |
20 | #include <stdlib.h> | |
21 | #include <string.h> | |
c363b55d | 22 | #include <unistd.h> |
54012638 | 23 | |
990570ed DG |
24 | #include <common/common.h> |
25 | #include <common/defaults.h> | |
1e307fab | 26 | |
00e2e675 | 27 | #include "consumer.h" |
62499ad6 | 28 | #include "trace-kernel.h" |
e9404c27 JG |
29 | #include "lttng-sessiond.h" |
30 | #include "notification-thread-commands.h" | |
54012638 | 31 | |
19e70852 | 32 | /* |
050349bb | 33 | * Find the channel name for the given kernel session. |
19e70852 | 34 | */ |
62499ad6 | 35 | struct ltt_kernel_channel *trace_kernel_get_channel_by_name( |
19e70852 DG |
36 | char *name, struct ltt_kernel_session *session) |
37 | { | |
38 | struct ltt_kernel_channel *chan; | |
39 | ||
0525e9ae DG |
40 | assert(session); |
41 | assert(name); | |
19e70852 | 42 | |
85076754 MD |
43 | /* |
44 | * If we receive an empty string for channel name, it means the | |
45 | * default channel name is requested. | |
46 | */ | |
47 | if (name[0] == '\0') | |
48 | name = DEFAULT_CHANNEL_NAME; | |
49 | ||
54d01ffb DG |
50 | DBG("Trying to find channel %s", name); |
51 | ||
19e70852 DG |
52 | cds_list_for_each_entry(chan, &session->channel_list.head, list) { |
53 | if (strcmp(name, chan->channel->name) == 0) { | |
54 | DBG("Found channel by name %s", name); | |
55 | return chan; | |
56 | } | |
57 | } | |
58 | ||
19e70852 DG |
59 | return NULL; |
60 | } | |
61 | ||
00a62084 MD |
62 | /* |
63 | * Find the event for the given channel. | |
64 | */ | |
65 | struct ltt_kernel_event *trace_kernel_find_event( | |
66 | char *name, struct ltt_kernel_channel *channel, | |
67 | enum lttng_event_type type, | |
68 | struct lttng_filter_bytecode *filter) | |
69 | { | |
70 | struct ltt_kernel_event *ev; | |
71 | int found = 0; | |
72 | ||
73 | assert(name); | |
74 | assert(channel); | |
75 | ||
76 | cds_list_for_each_entry(ev, &channel->events_list.head, list) { | |
77 | if (type != LTTNG_EVENT_ALL && ev->type != type) { | |
78 | continue; | |
79 | } | |
80 | if (strcmp(name, ev->event->name)) { | |
81 | continue; | |
82 | } | |
83 | if ((ev->filter && !filter) || (!ev->filter && filter)) { | |
84 | continue; | |
85 | } | |
86 | if (ev->filter && filter) { | |
87 | if (ev->filter->len != filter->len || | |
88 | memcmp(ev->filter->data, filter->data, | |
89 | filter->len) != 0) { | |
90 | continue; | |
91 | } | |
92 | } | |
93 | found = 1; | |
94 | break; | |
95 | } | |
96 | if (found) { | |
97 | DBG("Found event %s for channel %s", name, | |
98 | channel->channel->name); | |
99 | return ev; | |
100 | } else { | |
101 | return NULL; | |
102 | } | |
103 | } | |
104 | ||
19e70852 | 105 | /* |
050349bb | 106 | * Find the event name for the given channel. |
19e70852 | 107 | */ |
62499ad6 | 108 | struct ltt_kernel_event *trace_kernel_get_event_by_name( |
d0ae4ea8 MD |
109 | char *name, struct ltt_kernel_channel *channel, |
110 | enum lttng_event_type type) | |
19e70852 DG |
111 | { |
112 | struct ltt_kernel_event *ev; | |
00a62084 | 113 | int found = 0; |
19e70852 | 114 | |
0525e9ae DG |
115 | assert(name); |
116 | assert(channel); | |
19e70852 DG |
117 | |
118 | cds_list_for_each_entry(ev, &channel->events_list.head, list) { | |
00a62084 | 119 | if (type != LTTNG_EVENT_ALL && ev->type != type) { |
d0ae4ea8 | 120 | continue; |
19e70852 | 121 | } |
00a62084 MD |
122 | if (strcmp(name, ev->event->name)) { |
123 | continue; | |
124 | } | |
125 | found = 1; | |
126 | break; | |
127 | } | |
128 | if (found) { | |
129 | DBG("Found event %s for channel %s", name, | |
130 | channel->channel->name); | |
131 | return ev; | |
132 | } else { | |
133 | return NULL; | |
19e70852 | 134 | } |
19e70852 DG |
135 | } |
136 | ||
54012638 | 137 | /* |
050349bb | 138 | * Allocate and initialize a kernel session data structure. |
54012638 | 139 | * |
050349bb | 140 | * Return pointer to structure or NULL. |
54012638 | 141 | */ |
dec56f6c | 142 | struct ltt_kernel_session *trace_kernel_create_session(void) |
54012638 | 143 | { |
a4b92340 | 144 | struct ltt_kernel_session *lks = NULL; |
54012638 DG |
145 | |
146 | /* Allocate a new ltt kernel session */ | |
ba7f0ae5 | 147 | lks = zmalloc(sizeof(struct ltt_kernel_session)); |
54012638 | 148 | if (lks == NULL) { |
df0f840b | 149 | PERROR("create kernel session zmalloc"); |
a4b92340 | 150 | goto alloc_error; |
54012638 DG |
151 | } |
152 | ||
153 | /* Init data structure */ | |
03550b58 MD |
154 | lks->fd = -1; |
155 | lks->metadata_stream_fd = -1; | |
54012638 DG |
156 | lks->channel_count = 0; |
157 | lks->stream_count_global = 0; | |
158 | lks->metadata = NULL; | |
159 | CDS_INIT_LIST_HEAD(&lks->channel_list.head); | |
160 | ||
00e2e675 DG |
161 | lks->consumer = consumer_create_output(CONSUMER_DST_LOCAL); |
162 | if (lks->consumer == NULL) { | |
163 | goto error; | |
164 | } | |
165 | ||
54012638 DG |
166 | return lks; |
167 | ||
168 | error: | |
a4b92340 DG |
169 | free(lks); |
170 | ||
171 | alloc_error: | |
54012638 DG |
172 | return NULL; |
173 | } | |
174 | ||
175 | /* | |
050349bb | 176 | * Allocate and initialize a kernel channel data structure. |
54012638 | 177 | * |
050349bb | 178 | * Return pointer to structure or NULL. |
54012638 | 179 | */ |
00e2e675 | 180 | struct ltt_kernel_channel *trace_kernel_create_channel( |
fdd9eb17 | 181 | struct lttng_channel *chan) |
54012638 | 182 | { |
54012638 | 183 | struct ltt_kernel_channel *lkc; |
61a5b6b1 | 184 | struct lttng_channel_extended *extended = NULL; |
54012638 | 185 | |
0525e9ae DG |
186 | assert(chan); |
187 | ||
ba7f0ae5 | 188 | lkc = zmalloc(sizeof(struct ltt_kernel_channel)); |
f3ed775e | 189 | if (lkc == NULL) { |
df0f840b | 190 | PERROR("ltt_kernel_channel zmalloc"); |
54012638 DG |
191 | goto error; |
192 | } | |
193 | ||
ba7f0ae5 | 194 | lkc->channel = zmalloc(sizeof(struct lttng_channel)); |
f3ed775e | 195 | if (lkc->channel == NULL) { |
df0f840b | 196 | PERROR("lttng_channel zmalloc"); |
e9404c27 JG |
197 | goto error; |
198 | } | |
199 | ||
200 | extended = zmalloc(sizeof(struct lttng_channel_extended)); | |
201 | if (!extended) { | |
202 | PERROR("lttng_channel_channel zmalloc"); | |
f3ed775e DG |
203 | goto error; |
204 | } | |
205 | memcpy(lkc->channel, chan, sizeof(struct lttng_channel)); | |
e9404c27 JG |
206 | memcpy(extended, chan->attr.extended.ptr, sizeof(struct lttng_channel_extended)); |
207 | lkc->channel->attr.extended.ptr = extended; | |
208 | extended = NULL; | |
54012638 | 209 | |
85076754 MD |
210 | /* |
211 | * If we receive an empty string for channel name, it means the | |
212 | * default channel name is requested. | |
213 | */ | |
214 | if (chan->name[0] == '\0') { | |
215 | strncpy(lkc->channel->name, DEFAULT_CHANNEL_NAME, | |
216 | sizeof(lkc->channel->name)); | |
217 | } | |
bd722d76 | 218 | lkc->channel->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
85076754 | 219 | |
03550b58 | 220 | lkc->fd = -1; |
54012638 | 221 | lkc->stream_count = 0; |
cbbbb275 | 222 | lkc->event_count = 0; |
d36b8583 | 223 | lkc->enabled = 1; |
753873bf | 224 | lkc->published_to_notification_thread = false; |
54012638 DG |
225 | /* Init linked list */ |
226 | CDS_INIT_LIST_HEAD(&lkc->events_list.head); | |
227 | CDS_INIT_LIST_HEAD(&lkc->stream_list.head); | |
645328ae | 228 | CDS_INIT_LIST_HEAD(&lkc->ctx_list); |
54012638 DG |
229 | |
230 | return lkc; | |
231 | ||
232 | error: | |
e9404c27 JG |
233 | if (lkc) { |
234 | free(lkc->channel); | |
235 | } | |
236 | free(extended); | |
237 | free(lkc); | |
54012638 DG |
238 | return NULL; |
239 | } | |
240 | ||
645328ae DG |
241 | /* |
242 | * Allocate and init a kernel context object. | |
243 | * | |
244 | * Return the allocated object or NULL on error. | |
245 | */ | |
246 | struct ltt_kernel_context *trace_kernel_create_context( | |
247 | struct lttng_kernel_context *ctx) | |
248 | { | |
249 | struct ltt_kernel_context *kctx; | |
250 | ||
251 | kctx = zmalloc(sizeof(*kctx)); | |
252 | if (!kctx) { | |
253 | PERROR("zmalloc kernel context"); | |
254 | goto error; | |
255 | } | |
256 | ||
257 | if (ctx) { | |
258 | memcpy(&kctx->ctx, ctx, sizeof(kctx->ctx)); | |
259 | } | |
df3c77c8 JG |
260 | error: |
261 | return kctx; | |
262 | } | |
645328ae | 263 | |
df3c77c8 JG |
264 | /* |
265 | * Allocate and init a kernel context object from an existing kernel context | |
266 | * object. | |
267 | * | |
268 | * Return the allocated object or NULL on error. | |
269 | */ | |
270 | struct ltt_kernel_context *trace_kernel_copy_context( | |
271 | struct ltt_kernel_context *kctx) | |
272 | { | |
273 | struct ltt_kernel_context *kctx_copy; | |
274 | ||
275 | assert(kctx); | |
276 | kctx_copy = zmalloc(sizeof(*kctx_copy)); | |
277 | if (!kctx_copy) { | |
278 | PERROR("zmalloc ltt_kernel_context"); | |
279 | goto error; | |
280 | } | |
281 | ||
282 | memcpy(kctx_copy, kctx, sizeof(*kctx_copy)); | |
283 | memset(&kctx_copy->list, 0, sizeof(kctx_copy->list)); | |
7b9445b3 | 284 | |
645328ae | 285 | error: |
df3c77c8 | 286 | return kctx_copy; |
645328ae DG |
287 | } |
288 | ||
54012638 | 289 | /* |
050349bb | 290 | * Allocate and initialize a kernel event. Set name and event type. |
a969e101 | 291 | * We own filter_expression, and filter. |
54012638 | 292 | * |
050349bb | 293 | * Return pointer to structure or NULL. |
54012638 | 294 | */ |
71a3bb01 FD |
295 | enum lttng_error_code trace_kernel_create_event( |
296 | struct lttng_event *ev, char *filter_expression, | |
297 | struct lttng_filter_bytecode *filter, | |
298 | struct ltt_kernel_event **kernel_event) | |
54012638 | 299 | { |
71a3bb01 | 300 | enum lttng_error_code ret; |
54012638 | 301 | struct lttng_kernel_event *attr; |
71a3bb01 | 302 | struct ltt_kernel_event *local_kernel_event; |
54012638 | 303 | |
0525e9ae DG |
304 | assert(ev); |
305 | ||
71a3bb01 | 306 | local_kernel_event = zmalloc(sizeof(struct ltt_kernel_event)); |
ba7f0ae5 | 307 | attr = zmalloc(sizeof(struct lttng_kernel_event)); |
71a3bb01 | 308 | if (local_kernel_event == NULL || attr == NULL) { |
df0f840b | 309 | PERROR("kernel event zmalloc"); |
71a3bb01 | 310 | ret = LTTNG_ERR_NOMEM; |
54012638 DG |
311 | goto error; |
312 | } | |
313 | ||
f3ed775e | 314 | switch (ev->type) { |
7d29a247 | 315 | case LTTNG_EVENT_PROBE: |
e6ddca71 | 316 | attr->instrumentation = LTTNG_KERNEL_KPROBE; |
7d29a247 DG |
317 | attr->u.kprobe.addr = ev->attr.probe.addr; |
318 | attr->u.kprobe.offset = ev->attr.probe.offset; | |
f3ed775e | 319 | strncpy(attr->u.kprobe.symbol_name, |
dbbb3ec5 DG |
320 | ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN); |
321 | attr->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; | |
f3ed775e DG |
322 | break; |
323 | case LTTNG_EVENT_FUNCTION: | |
8f0d098b MD |
324 | attr->instrumentation = LTTNG_KERNEL_KRETPROBE; |
325 | attr->u.kretprobe.addr = ev->attr.probe.addr; | |
326 | attr->u.kretprobe.offset = ev->attr.probe.offset; | |
8f0d098b | 327 | strncpy(attr->u.kretprobe.symbol_name, |
dbbb3ec5 DG |
328 | ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN); |
329 | attr->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; | |
8f0d098b MD |
330 | break; |
331 | case LTTNG_EVENT_FUNCTION_ENTRY: | |
f3ed775e DG |
332 | attr->instrumentation = LTTNG_KERNEL_FUNCTION; |
333 | strncpy(attr->u.ftrace.symbol_name, | |
dbbb3ec5 DG |
334 | ev->attr.ftrace.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN); |
335 | attr->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; | |
f3ed775e | 336 | break; |
e6ddca71 DG |
337 | case LTTNG_EVENT_TRACEPOINT: |
338 | attr->instrumentation = LTTNG_KERNEL_TRACEPOINT; | |
f3ed775e | 339 | break; |
a54bd42d MD |
340 | case LTTNG_EVENT_SYSCALL: |
341 | attr->instrumentation = LTTNG_KERNEL_SYSCALL; | |
0133c199 | 342 | break; |
7a3d1328 MD |
343 | case LTTNG_EVENT_ALL: |
344 | attr->instrumentation = LTTNG_KERNEL_ALL; | |
345 | break; | |
f3ed775e DG |
346 | default: |
347 | ERR("Unknown kernel instrumentation type (%d)", ev->type); | |
71a3bb01 | 348 | ret = LTTNG_ERR_INVALID; |
f3ed775e DG |
349 | goto error; |
350 | } | |
351 | ||
352 | /* Copy event name */ | |
dbbb3ec5 DG |
353 | strncpy(attr->name, ev->name, LTTNG_KERNEL_SYM_NAME_LEN); |
354 | attr->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; | |
f3ed775e | 355 | |
54012638 | 356 | /* Setting up a kernel event */ |
71a3bb01 FD |
357 | local_kernel_event->fd = -1; |
358 | local_kernel_event->event = attr; | |
359 | local_kernel_event->enabled = 1; | |
360 | local_kernel_event->filter_expression = filter_expression; | |
361 | local_kernel_event->filter = filter; | |
54012638 | 362 | |
71a3bb01 FD |
363 | *kernel_event = local_kernel_event; |
364 | ||
365 | return LTTNG_OK; | |
54012638 DG |
366 | |
367 | error: | |
a969e101 MD |
368 | free(filter_expression); |
369 | free(filter); | |
71a3bb01 | 370 | free(local_kernel_event); |
a2c0da86 | 371 | free(attr); |
71a3bb01 | 372 | return ret; |
54012638 DG |
373 | } |
374 | ||
375 | /* | |
050349bb | 376 | * Allocate and initialize a kernel metadata. |
54012638 | 377 | * |
050349bb | 378 | * Return pointer to structure or NULL. |
54012638 | 379 | */ |
a4b92340 | 380 | struct ltt_kernel_metadata *trace_kernel_create_metadata(void) |
54012638 | 381 | { |
54012638 | 382 | struct ltt_kernel_metadata *lkm; |
f3ed775e | 383 | struct lttng_channel *chan; |
54012638 | 384 | |
ba7f0ae5 DG |
385 | lkm = zmalloc(sizeof(struct ltt_kernel_metadata)); |
386 | chan = zmalloc(sizeof(struct lttng_channel)); | |
f3ed775e | 387 | if (lkm == NULL || chan == NULL) { |
df0f840b | 388 | PERROR("kernel metadata zmalloc"); |
54012638 DG |
389 | goto error; |
390 | } | |
391 | ||
392 | /* Set default attributes */ | |
f3ed775e | 393 | chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE; |
3e230f92 | 394 | chan->attr.subbuf_size = default_get_metadata_subbuf_size(); |
b389abbe | 395 | chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM; |
6bb9e85f MD |
396 | chan->attr.switch_timer_interval = DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER; |
397 | chan->attr.read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER; | |
7d452e12 | 398 | chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT; |
54012638 DG |
399 | |
400 | /* Init metadata */ | |
03550b58 | 401 | lkm->fd = -1; |
f3ed775e | 402 | lkm->conf = chan; |
54012638 DG |
403 | |
404 | return lkm; | |
405 | ||
406 | error: | |
a2c0da86 MD |
407 | free(lkm); |
408 | free(chan); | |
54012638 DG |
409 | return NULL; |
410 | } | |
411 | ||
412 | /* | |
050349bb DG |
413 | * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by |
414 | * default. | |
54012638 | 415 | * |
050349bb | 416 | * Return pointer to structure or NULL. |
54012638 | 417 | */ |
00e2e675 DG |
418 | struct ltt_kernel_stream *trace_kernel_create_stream(const char *name, |
419 | unsigned int count) | |
54012638 | 420 | { |
00e2e675 | 421 | int ret; |
54012638 DG |
422 | struct ltt_kernel_stream *lks; |
423 | ||
0525e9ae DG |
424 | assert(name); |
425 | ||
ba7f0ae5 | 426 | lks = zmalloc(sizeof(struct ltt_kernel_stream)); |
54012638 | 427 | if (lks == NULL) { |
df0f840b | 428 | PERROR("kernel stream zmalloc"); |
54012638 DG |
429 | goto error; |
430 | } | |
431 | ||
00e2e675 | 432 | /* Set name */ |
535b8ff4 | 433 | ret = snprintf(lks->name, sizeof(lks->name), "%s_%u", name, count); |
00e2e675 DG |
434 | if (ret < 0) { |
435 | PERROR("snprintf stream name"); | |
436 | goto error; | |
437 | } | |
438 | lks->name[sizeof(lks->name) - 1] = '\0'; | |
439 | ||
54012638 | 440 | /* Init stream */ |
03550b58 | 441 | lks->fd = -1; |
54012638 | 442 | lks->state = 0; |
ffe60014 | 443 | lks->cpu = count; |
54012638 DG |
444 | |
445 | return lks; | |
446 | ||
447 | error: | |
448 | return NULL; | |
449 | } | |
c363b55d | 450 | |
050349bb DG |
451 | /* |
452 | * Cleanup kernel stream structure. | |
453 | */ | |
62499ad6 | 454 | void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream) |
c363b55d | 455 | { |
0525e9ae DG |
456 | assert(stream); |
457 | ||
33a2b854 | 458 | DBG("[trace] Closing stream fd %d", stream->fd); |
c363b55d | 459 | /* Close kernel fd */ |
03550b58 | 460 | if (stream->fd >= 0) { |
c617c0c6 MD |
461 | int ret; |
462 | ||
03550b58 MD |
463 | ret = close(stream->fd); |
464 | if (ret) { | |
465 | PERROR("close"); | |
466 | } | |
799e2c4f | 467 | } |
c363b55d DG |
468 | /* Remove from stream list */ |
469 | cds_list_del(&stream->list); | |
f9815039 | 470 | |
c363b55d DG |
471 | free(stream); |
472 | } | |
473 | ||
050349bb DG |
474 | /* |
475 | * Cleanup kernel event structure. | |
476 | */ | |
62499ad6 | 477 | void trace_kernel_destroy_event(struct ltt_kernel_event *event) |
c363b55d | 478 | { |
0525e9ae DG |
479 | assert(event); |
480 | ||
87eb4ab8 | 481 | if (event->fd >= 0) { |
c617c0c6 MD |
482 | int ret; |
483 | ||
87eb4ab8 MD |
484 | DBG("[trace] Closing event fd %d", event->fd); |
485 | /* Close kernel fd */ | |
799e2c4f MD |
486 | ret = close(event->fd); |
487 | if (ret) { | |
488 | PERROR("close"); | |
489 | } | |
87eb4ab8 MD |
490 | } else { |
491 | DBG("[trace] Tearing down event (no associated fd)"); | |
492 | } | |
c363b55d DG |
493 | |
494 | /* Remove from event list */ | |
495 | cds_list_del(&event->list); | |
f9815039 | 496 | |
00a62084 MD |
497 | free(event->filter_expression); |
498 | free(event->filter); | |
499 | ||
f9815039 | 500 | free(event->event); |
c363b55d DG |
501 | free(event); |
502 | } | |
503 | ||
645328ae DG |
504 | /* |
505 | * Cleanup kernel context structure. | |
506 | */ | |
507 | void trace_kernel_destroy_context(struct ltt_kernel_context *ctx) | |
508 | { | |
509 | assert(ctx); | |
510 | ||
ba985c3a JG |
511 | if (ctx->in_list) { |
512 | cds_list_del(&ctx->list); | |
513 | } | |
645328ae DG |
514 | free(ctx); |
515 | } | |
516 | ||
050349bb DG |
517 | /* |
518 | * Cleanup kernel channel structure. | |
519 | */ | |
62499ad6 | 520 | void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel) |
c363b55d | 521 | { |
af9737e9 DG |
522 | struct ltt_kernel_stream *stream, *stmp; |
523 | struct ltt_kernel_event *event, *etmp; | |
645328ae | 524 | struct ltt_kernel_context *ctx, *ctmp; |
799e2c4f | 525 | int ret; |
e9404c27 | 526 | enum lttng_error_code status; |
c363b55d | 527 | |
0525e9ae DG |
528 | assert(channel); |
529 | ||
33a2b854 | 530 | DBG("[trace] Closing channel fd %d", channel->fd); |
c363b55d | 531 | /* Close kernel fd */ |
03550b58 MD |
532 | if (channel->fd >= 0) { |
533 | ret = close(channel->fd); | |
534 | if (ret) { | |
535 | PERROR("close"); | |
536 | } | |
799e2c4f | 537 | } |
c363b55d DG |
538 | |
539 | /* For each stream in the channel list */ | |
af9737e9 | 540 | cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) { |
62499ad6 | 541 | trace_kernel_destroy_stream(stream); |
c363b55d DG |
542 | } |
543 | ||
544 | /* For each event in the channel list */ | |
af9737e9 | 545 | cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) { |
62499ad6 | 546 | trace_kernel_destroy_event(event); |
c363b55d DG |
547 | } |
548 | ||
645328ae DG |
549 | /* For each context in the channel list */ |
550 | cds_list_for_each_entry_safe(ctx, ctmp, &channel->ctx_list, list) { | |
551 | trace_kernel_destroy_context(ctx); | |
552 | } | |
553 | ||
c363b55d DG |
554 | /* Remove from channel list */ |
555 | cds_list_del(&channel->list); | |
f9815039 | 556 | |
753873bf JR |
557 | if (notification_thread_handle |
558 | && channel->published_to_notification_thread) { | |
63aaa3dc JG |
559 | status = notification_thread_command_remove_channel( |
560 | notification_thread_handle, | |
e1f3997a | 561 | channel->key, LTTNG_DOMAIN_KERNEL); |
63aaa3dc JG |
562 | assert(status == LTTNG_OK); |
563 | } | |
e9404c27 | 564 | free(channel->channel->attr.extended.ptr); |
f9815039 | 565 | free(channel->channel); |
c363b55d DG |
566 | free(channel); |
567 | } | |
568 | ||
050349bb DG |
569 | /* |
570 | * Cleanup kernel metadata structure. | |
571 | */ | |
62499ad6 | 572 | void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata) |
c363b55d | 573 | { |
0525e9ae DG |
574 | assert(metadata); |
575 | ||
33a2b854 | 576 | DBG("[trace] Closing metadata fd %d", metadata->fd); |
c363b55d | 577 | /* Close kernel fd */ |
03550b58 | 578 | if (metadata->fd >= 0) { |
c617c0c6 MD |
579 | int ret; |
580 | ||
03550b58 MD |
581 | ret = close(metadata->fd); |
582 | if (ret) { | |
583 | PERROR("close"); | |
584 | } | |
799e2c4f | 585 | } |
c363b55d | 586 | |
f9815039 | 587 | free(metadata->conf); |
c363b55d DG |
588 | free(metadata); |
589 | } | |
590 | ||
050349bb | 591 | /* |
62499ad6 | 592 | * Cleanup kernel session structure |
36b588ed MD |
593 | * |
594 | * Should *NOT* be called with RCU read-side lock held. | |
050349bb | 595 | */ |
62499ad6 | 596 | void trace_kernel_destroy_session(struct ltt_kernel_session *session) |
c363b55d | 597 | { |
af9737e9 | 598 | struct ltt_kernel_channel *channel, *ctmp; |
799e2c4f | 599 | int ret; |
c363b55d | 600 | |
0525e9ae DG |
601 | assert(session); |
602 | ||
33a2b854 | 603 | DBG("[trace] Closing session fd %d", session->fd); |
c363b55d | 604 | /* Close kernel fds */ |
03550b58 MD |
605 | if (session->fd >= 0) { |
606 | ret = close(session->fd); | |
607 | if (ret) { | |
608 | PERROR("close"); | |
609 | } | |
799e2c4f | 610 | } |
f9815039 | 611 | |
03550b58 | 612 | if (session->metadata_stream_fd >= 0) { |
70dc1c34 | 613 | DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd); |
799e2c4f MD |
614 | ret = close(session->metadata_stream_fd); |
615 | if (ret) { | |
616 | PERROR("close"); | |
617 | } | |
70dc1c34 | 618 | } |
c363b55d | 619 | |
d36b8583 | 620 | if (session->metadata != NULL) { |
62499ad6 | 621 | trace_kernel_destroy_metadata(session->metadata); |
d36b8583 | 622 | } |
c363b55d | 623 | |
af9737e9 | 624 | cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) { |
62499ad6 | 625 | trace_kernel_destroy_channel(channel); |
c363b55d DG |
626 | } |
627 | ||
00e2e675 | 628 | /* Wipe consumer output object */ |
6addfa37 | 629 | consumer_output_put(session->consumer); |
00e2e675 | 630 | |
c363b55d DG |
631 | free(session); |
632 | } |