sessiond: clean-up: typo in ust-app.c comment
[lttng-tools.git] / src / bin / lttng-sessiond / trace-kernel.c
CommitLineData
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
dcabc190
FD
24#include <lttng/event.h>
25#include <lttng/lttng-error.h>
26#include <lttng/userspace-probe.h>
27#include <lttng/userspace-probe-internal.h>
28
990570ed
DG
29#include <common/common.h>
30#include <common/defaults.h>
82b69413 31#include <common/trace-chunk.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 41struct 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 */
71struct ltt_kernel_event *trace_kernel_find_event(
72 char *name, struct ltt_kernel_channel *channel,
73 enum lttng_event_type type,
74 struct lttng_filter_bytecode *filter)
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 114struct 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 148struct 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
00e2e675
DG
167 lks->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
168 if (lks->consumer == NULL) {
169 goto error;
170 }
171
54012638
DG
172 return lks;
173
174error:
a4b92340
DG
175 free(lks);
176
177alloc_error:
54012638
DG
178 return NULL;
179}
180
181/*
050349bb 182 * Allocate and initialize a kernel channel data structure.
54012638 183 *
050349bb 184 * Return pointer to structure or NULL.
54012638 185 */
00e2e675 186struct ltt_kernel_channel *trace_kernel_create_channel(
fdd9eb17 187 struct lttng_channel *chan)
54012638 188{
54012638 189 struct ltt_kernel_channel *lkc;
61a5b6b1 190 struct lttng_channel_extended *extended = NULL;
54012638 191
0525e9ae
DG
192 assert(chan);
193
ba7f0ae5 194 lkc = zmalloc(sizeof(struct ltt_kernel_channel));
f3ed775e 195 if (lkc == NULL) {
df0f840b 196 PERROR("ltt_kernel_channel zmalloc");
54012638
DG
197 goto error;
198 }
199
ba7f0ae5 200 lkc->channel = zmalloc(sizeof(struct lttng_channel));
f3ed775e 201 if (lkc->channel == NULL) {
df0f840b 202 PERROR("lttng_channel zmalloc");
e9404c27
JG
203 goto error;
204 }
205
206 extended = zmalloc(sizeof(struct lttng_channel_extended));
207 if (!extended) {
208 PERROR("lttng_channel_channel zmalloc");
f3ed775e
DG
209 goto error;
210 }
211 memcpy(lkc->channel, chan, sizeof(struct lttng_channel));
e9404c27
JG
212 memcpy(extended, chan->attr.extended.ptr, sizeof(struct lttng_channel_extended));
213 lkc->channel->attr.extended.ptr = extended;
214 extended = NULL;
54012638 215
85076754
MD
216 /*
217 * If we receive an empty string for channel name, it means the
218 * default channel name is requested.
219 */
220 if (chan->name[0] == '\0') {
221 strncpy(lkc->channel->name, DEFAULT_CHANNEL_NAME,
222 sizeof(lkc->channel->name));
223 }
bd722d76 224 lkc->channel->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
85076754 225
03550b58 226 lkc->fd = -1;
54012638 227 lkc->stream_count = 0;
cbbbb275 228 lkc->event_count = 0;
d36b8583 229 lkc->enabled = 1;
753873bf 230 lkc->published_to_notification_thread = false;
54012638
DG
231 /* Init linked list */
232 CDS_INIT_LIST_HEAD(&lkc->events_list.head);
233 CDS_INIT_LIST_HEAD(&lkc->stream_list.head);
645328ae 234 CDS_INIT_LIST_HEAD(&lkc->ctx_list);
54012638
DG
235
236 return lkc;
237
238error:
e9404c27
JG
239 if (lkc) {
240 free(lkc->channel);
241 }
242 free(extended);
243 free(lkc);
54012638
DG
244 return NULL;
245}
246
645328ae
DG
247/*
248 * Allocate and init a kernel context object.
249 *
250 * Return the allocated object or NULL on error.
251 */
252struct ltt_kernel_context *trace_kernel_create_context(
253 struct lttng_kernel_context *ctx)
254{
255 struct ltt_kernel_context *kctx;
256
257 kctx = zmalloc(sizeof(*kctx));
258 if (!kctx) {
259 PERROR("zmalloc kernel context");
260 goto error;
261 }
262
263 if (ctx) {
264 memcpy(&kctx->ctx, ctx, sizeof(kctx->ctx));
265 }
df3c77c8
JG
266error:
267 return kctx;
268}
645328ae 269
df3c77c8
JG
270/*
271 * Allocate and init a kernel context object from an existing kernel context
272 * object.
273 *
274 * Return the allocated object or NULL on error.
275 */
276struct ltt_kernel_context *trace_kernel_copy_context(
277 struct ltt_kernel_context *kctx)
278{
279 struct ltt_kernel_context *kctx_copy;
280
281 assert(kctx);
282 kctx_copy = zmalloc(sizeof(*kctx_copy));
283 if (!kctx_copy) {
284 PERROR("zmalloc ltt_kernel_context");
285 goto error;
286 }
287
288 memcpy(kctx_copy, kctx, sizeof(*kctx_copy));
289 memset(&kctx_copy->list, 0, sizeof(kctx_copy->list));
7b9445b3 290
645328ae 291error:
df3c77c8 292 return kctx_copy;
645328ae
DG
293}
294
54012638 295/*
050349bb 296 * Allocate and initialize a kernel event. Set name and event type.
a969e101 297 * We own filter_expression, and filter.
54012638 298 *
050349bb 299 * Return pointer to structure or NULL.
54012638 300 */
71a3bb01
FD
301enum lttng_error_code trace_kernel_create_event(
302 struct lttng_event *ev, char *filter_expression,
303 struct lttng_filter_bytecode *filter,
304 struct ltt_kernel_event **kernel_event)
54012638 305{
71a3bb01 306 enum lttng_error_code ret;
54012638 307 struct lttng_kernel_event *attr;
71a3bb01 308 struct ltt_kernel_event *local_kernel_event;
dcabc190 309 struct lttng_userspace_probe_location *userspace_probe_location = NULL;
54012638 310
0525e9ae
DG
311 assert(ev);
312
71a3bb01 313 local_kernel_event = zmalloc(sizeof(struct ltt_kernel_event));
ba7f0ae5 314 attr = zmalloc(sizeof(struct lttng_kernel_event));
71a3bb01 315 if (local_kernel_event == NULL || attr == NULL) {
df0f840b 316 PERROR("kernel event zmalloc");
71a3bb01 317 ret = LTTNG_ERR_NOMEM;
54012638
DG
318 goto error;
319 }
320
f3ed775e 321 switch (ev->type) {
7d29a247 322 case LTTNG_EVENT_PROBE:
e6ddca71 323 attr->instrumentation = LTTNG_KERNEL_KPROBE;
7d29a247
DG
324 attr->u.kprobe.addr = ev->attr.probe.addr;
325 attr->u.kprobe.offset = ev->attr.probe.offset;
f3ed775e 326 strncpy(attr->u.kprobe.symbol_name,
dbbb3ec5
DG
327 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
328 attr->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
f3ed775e 329 break;
dcabc190
FD
330 case LTTNG_EVENT_USERSPACE_PROBE:
331 {
87597c2c
JG
332 const struct lttng_userspace_probe_location* location = NULL;
333 const struct lttng_userspace_probe_location_lookup_method *lookup = NULL;
dcabc190
FD
334
335 location = lttng_event_get_userspace_probe_location(ev);
336 if (!location) {
337 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
338 goto error;
339 }
340
341 /*
342 * From this point on, the specific term 'uprobe' is used
343 * instead of the generic 'userspace probe' because it's the
344 * technology used at the moment for this instrumentation.
345 * LTTng currently implements userspace probes using uprobes.
346 * In the interactions with the kernel tracer, we use the
347 * uprobe term.
348 */
349 attr->instrumentation = LTTNG_KERNEL_UPROBE;
350
351 /*
352 * Only the elf lookup method is supported at the moment.
353 */
354 lookup = lttng_userspace_probe_location_get_lookup_method(
355 location);
356 if (!lookup) {
357 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
358 goto error;
359 }
360
361 /*
362 * From the kernel tracer's perspective, all userspace probe
363 * event types are all the same: a file and an offset.
364 */
365 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup)) {
366 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF:
367 /* Get the file descriptor on the target binary. */
368 attr->u.uprobe.fd =
369 lttng_userspace_probe_location_function_get_binary_fd(location);
370
371 /*
372 * Save a reference to the probe location used during
373 * the listing of events. Close its FD since it won't
374 * be needed for listing.
375 */
376 userspace_probe_location =
377 lttng_userspace_probe_location_copy(location);
378 ret = lttng_userspace_probe_location_function_set_binary_fd(
379 userspace_probe_location, -1);
380 if (ret) {
381 goto error;
382 }
383 break;
384 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT:
385 /* Get the file descriptor on the target binary. */
386 attr->u.uprobe.fd =
387 lttng_userspace_probe_location_tracepoint_get_binary_fd(location);
388
389 /*
390 * Save a reference to the probe location used during the listing of
391 * events. Close its FD since it won't be needed for listing.
392 */
393 userspace_probe_location =
394 lttng_userspace_probe_location_copy(location);
395 ret = lttng_userspace_probe_location_tracepoint_set_binary_fd(
396 userspace_probe_location, -1);
397 if (ret) {
398 goto error;
399 }
400 break;
401 default:
402 DBG("Unsupported lookup method type");
403 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
404 goto error;
405 }
406 break;
407 }
f3ed775e 408 case LTTNG_EVENT_FUNCTION:
8f0d098b
MD
409 attr->instrumentation = LTTNG_KERNEL_KRETPROBE;
410 attr->u.kretprobe.addr = ev->attr.probe.addr;
411 attr->u.kretprobe.offset = ev->attr.probe.offset;
8f0d098b 412 strncpy(attr->u.kretprobe.symbol_name,
dbbb3ec5
DG
413 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
414 attr->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
8f0d098b
MD
415 break;
416 case LTTNG_EVENT_FUNCTION_ENTRY:
f3ed775e
DG
417 attr->instrumentation = LTTNG_KERNEL_FUNCTION;
418 strncpy(attr->u.ftrace.symbol_name,
dbbb3ec5
DG
419 ev->attr.ftrace.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
420 attr->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
f3ed775e 421 break;
e6ddca71
DG
422 case LTTNG_EVENT_TRACEPOINT:
423 attr->instrumentation = LTTNG_KERNEL_TRACEPOINT;
f3ed775e 424 break;
a54bd42d
MD
425 case LTTNG_EVENT_SYSCALL:
426 attr->instrumentation = LTTNG_KERNEL_SYSCALL;
0133c199 427 break;
7a3d1328
MD
428 case LTTNG_EVENT_ALL:
429 attr->instrumentation = LTTNG_KERNEL_ALL;
430 break;
f3ed775e
DG
431 default:
432 ERR("Unknown kernel instrumentation type (%d)", ev->type);
71a3bb01 433 ret = LTTNG_ERR_INVALID;
f3ed775e
DG
434 goto error;
435 }
436
437 /* Copy event name */
dbbb3ec5
DG
438 strncpy(attr->name, ev->name, LTTNG_KERNEL_SYM_NAME_LEN);
439 attr->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
f3ed775e 440
54012638 441 /* Setting up a kernel event */
71a3bb01
FD
442 local_kernel_event->fd = -1;
443 local_kernel_event->event = attr;
444 local_kernel_event->enabled = 1;
445 local_kernel_event->filter_expression = filter_expression;
446 local_kernel_event->filter = filter;
dcabc190 447 local_kernel_event->userspace_probe_location = userspace_probe_location;
54012638 448
71a3bb01
FD
449 *kernel_event = local_kernel_event;
450
451 return LTTNG_OK;
54012638
DG
452
453error:
a969e101
MD
454 free(filter_expression);
455 free(filter);
71a3bb01 456 free(local_kernel_event);
a2c0da86 457 free(attr);
71a3bb01 458 return ret;
54012638
DG
459}
460
461/*
050349bb 462 * Allocate and initialize a kernel metadata.
54012638 463 *
050349bb 464 * Return pointer to structure or NULL.
54012638 465 */
a4b92340 466struct ltt_kernel_metadata *trace_kernel_create_metadata(void)
54012638 467{
54012638 468 struct ltt_kernel_metadata *lkm;
f3ed775e 469 struct lttng_channel *chan;
54012638 470
ba7f0ae5
DG
471 lkm = zmalloc(sizeof(struct ltt_kernel_metadata));
472 chan = zmalloc(sizeof(struct lttng_channel));
f3ed775e 473 if (lkm == NULL || chan == NULL) {
df0f840b 474 PERROR("kernel metadata zmalloc");
54012638
DG
475 goto error;
476 }
477
478 /* Set default attributes */
f3ed775e 479 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
3e230f92 480 chan->attr.subbuf_size = default_get_metadata_subbuf_size();
b389abbe 481 chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
6bb9e85f
MD
482 chan->attr.switch_timer_interval = DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
483 chan->attr.read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
7d452e12 484 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
54012638
DG
485
486 /* Init metadata */
03550b58 487 lkm->fd = -1;
f3ed775e 488 lkm->conf = chan;
54012638
DG
489
490 return lkm;
491
492error:
a2c0da86
MD
493 free(lkm);
494 free(chan);
54012638
DG
495 return NULL;
496}
497
498/*
050349bb
DG
499 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
500 * default.
54012638 501 *
050349bb 502 * Return pointer to structure or NULL.
54012638 503 */
00e2e675
DG
504struct ltt_kernel_stream *trace_kernel_create_stream(const char *name,
505 unsigned int count)
54012638 506{
00e2e675 507 int ret;
54012638
DG
508 struct ltt_kernel_stream *lks;
509
0525e9ae
DG
510 assert(name);
511
ba7f0ae5 512 lks = zmalloc(sizeof(struct ltt_kernel_stream));
54012638 513 if (lks == NULL) {
df0f840b 514 PERROR("kernel stream zmalloc");
54012638
DG
515 goto error;
516 }
517
00e2e675 518 /* Set name */
535b8ff4 519 ret = snprintf(lks->name, sizeof(lks->name), "%s_%u", name, count);
00e2e675
DG
520 if (ret < 0) {
521 PERROR("snprintf stream name");
522 goto error;
523 }
524 lks->name[sizeof(lks->name) - 1] = '\0';
525
54012638 526 /* Init stream */
03550b58 527 lks->fd = -1;
54012638 528 lks->state = 0;
ffe60014 529 lks->cpu = count;
54012638
DG
530
531 return lks;
532
533error:
534 return NULL;
535}
c363b55d 536
050349bb
DG
537/*
538 * Cleanup kernel stream structure.
539 */
62499ad6 540void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream)
c363b55d 541{
0525e9ae
DG
542 assert(stream);
543
33a2b854 544 DBG("[trace] Closing stream fd %d", stream->fd);
c363b55d 545 /* Close kernel fd */
03550b58 546 if (stream->fd >= 0) {
c617c0c6
MD
547 int ret;
548
03550b58
MD
549 ret = close(stream->fd);
550 if (ret) {
551 PERROR("close");
552 }
799e2c4f 553 }
c363b55d
DG
554 /* Remove from stream list */
555 cds_list_del(&stream->list);
f9815039 556
c363b55d
DG
557 free(stream);
558}
559
050349bb
DG
560/*
561 * Cleanup kernel event structure.
562 */
62499ad6 563void trace_kernel_destroy_event(struct ltt_kernel_event *event)
c363b55d 564{
0525e9ae
DG
565 assert(event);
566
87eb4ab8 567 if (event->fd >= 0) {
c617c0c6
MD
568 int ret;
569
87eb4ab8
MD
570 DBG("[trace] Closing event fd %d", event->fd);
571 /* Close kernel fd */
799e2c4f
MD
572 ret = close(event->fd);
573 if (ret) {
574 PERROR("close");
575 }
87eb4ab8
MD
576 } else {
577 DBG("[trace] Tearing down event (no associated fd)");
578 }
c363b55d
DG
579
580 /* Remove from event list */
581 cds_list_del(&event->list);
f9815039 582
00a62084
MD
583 free(event->filter_expression);
584 free(event->filter);
585
f9815039 586 free(event->event);
c363b55d
DG
587 free(event);
588}
589
645328ae
DG
590/*
591 * Cleanup kernel context structure.
592 */
593void trace_kernel_destroy_context(struct ltt_kernel_context *ctx)
594{
595 assert(ctx);
596
ba985c3a
JG
597 if (ctx->in_list) {
598 cds_list_del(&ctx->list);
599 }
645328ae
DG
600 free(ctx);
601}
602
050349bb
DG
603/*
604 * Cleanup kernel channel structure.
605 */
62499ad6 606void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel)
c363b55d 607{
af9737e9
DG
608 struct ltt_kernel_stream *stream, *stmp;
609 struct ltt_kernel_event *event, *etmp;
645328ae 610 struct ltt_kernel_context *ctx, *ctmp;
799e2c4f 611 int ret;
e9404c27 612 enum lttng_error_code status;
c363b55d 613
0525e9ae
DG
614 assert(channel);
615
33a2b854 616 DBG("[trace] Closing channel fd %d", channel->fd);
c363b55d 617 /* Close kernel fd */
03550b58
MD
618 if (channel->fd >= 0) {
619 ret = close(channel->fd);
620 if (ret) {
621 PERROR("close");
622 }
799e2c4f 623 }
c363b55d
DG
624
625 /* For each stream in the channel list */
af9737e9 626 cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) {
62499ad6 627 trace_kernel_destroy_stream(stream);
c363b55d
DG
628 }
629
630 /* For each event in the channel list */
af9737e9 631 cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) {
62499ad6 632 trace_kernel_destroy_event(event);
c363b55d
DG
633 }
634
645328ae
DG
635 /* For each context in the channel list */
636 cds_list_for_each_entry_safe(ctx, ctmp, &channel->ctx_list, list) {
637 trace_kernel_destroy_context(ctx);
638 }
639
c363b55d
DG
640 /* Remove from channel list */
641 cds_list_del(&channel->list);
f9815039 642
753873bf
JR
643 if (notification_thread_handle
644 && channel->published_to_notification_thread) {
63aaa3dc
JG
645 status = notification_thread_command_remove_channel(
646 notification_thread_handle,
e1f3997a 647 channel->key, LTTNG_DOMAIN_KERNEL);
63aaa3dc
JG
648 assert(status == LTTNG_OK);
649 }
e9404c27 650 free(channel->channel->attr.extended.ptr);
f9815039 651 free(channel->channel);
c363b55d
DG
652 free(channel);
653}
654
050349bb
DG
655/*
656 * Cleanup kernel metadata structure.
657 */
62499ad6 658void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata)
c363b55d 659{
0525e9ae
DG
660 assert(metadata);
661
33a2b854 662 DBG("[trace] Closing metadata fd %d", metadata->fd);
c363b55d 663 /* Close kernel fd */
03550b58 664 if (metadata->fd >= 0) {
c617c0c6
MD
665 int ret;
666
03550b58
MD
667 ret = close(metadata->fd);
668 if (ret) {
669 PERROR("close");
670 }
799e2c4f 671 }
c363b55d 672
f9815039 673 free(metadata->conf);
c363b55d
DG
674 free(metadata);
675}
676
050349bb 677/*
62499ad6 678 * Cleanup kernel session structure
36b588ed
MD
679 *
680 * Should *NOT* be called with RCU read-side lock held.
050349bb 681 */
62499ad6 682void trace_kernel_destroy_session(struct ltt_kernel_session *session)
c363b55d 683{
af9737e9 684 struct ltt_kernel_channel *channel, *ctmp;
799e2c4f 685 int ret;
c363b55d 686
0525e9ae
DG
687 assert(session);
688
33a2b854 689 DBG("[trace] Closing session fd %d", session->fd);
c363b55d 690 /* Close kernel fds */
03550b58
MD
691 if (session->fd >= 0) {
692 ret = close(session->fd);
693 if (ret) {
694 PERROR("close");
695 }
799e2c4f 696 }
f9815039 697
03550b58 698 if (session->metadata_stream_fd >= 0) {
70dc1c34 699 DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd);
799e2c4f
MD
700 ret = close(session->metadata_stream_fd);
701 if (ret) {
702 PERROR("close");
703 }
70dc1c34 704 }
c363b55d 705
d36b8583 706 if (session->metadata != NULL) {
62499ad6 707 trace_kernel_destroy_metadata(session->metadata);
d36b8583 708 }
c363b55d 709
af9737e9 710 cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) {
62499ad6 711 trace_kernel_destroy_channel(channel);
c363b55d 712 }
d070c424 713}
c363b55d 714
d070c424
MD
715/* Free elements needed by destroy notifiers. */
716void trace_kernel_free_session(struct ltt_kernel_session *session)
717{
00e2e675 718 /* Wipe consumer output object */
6addfa37 719 consumer_output_put(session->consumer);
00e2e675 720
c363b55d
DG
721 free(session);
722}
This page took 0.096643 seconds and 4 git commands to generate.