Clean-up: sessiond kernel: change spaces to tabs
[lttng-tools.git] / src / bin / lttng-sessiond / trace-kernel.c
CommitLineData
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>
16#include <lttng/userspace-probe.h>
17#include <lttng/userspace-probe-internal.h>
18
990570ed
DG
19#include <common/common.h>
20#include <common/defaults.h>
82b69413 21#include <common/trace-chunk.h>
d42266a4 22#include <common/macros.h>
1e307fab 23
00e2e675 24#include "consumer.h"
62499ad6 25#include "trace-kernel.h"
e9404c27
JG
26#include "lttng-sessiond.h"
27#include "notification-thread-commands.h"
54012638 28
19e70852 29/*
050349bb 30 * Find the channel name for the given kernel session.
19e70852 31 */
62499ad6 32struct ltt_kernel_channel *trace_kernel_get_channel_by_name(
df4f5a87 33 const char *name, struct ltt_kernel_session *session)
19e70852
DG
34{
35 struct ltt_kernel_channel *chan;
36
0525e9ae
DG
37 assert(session);
38 assert(name);
19e70852 39
85076754
MD
40 /*
41 * If we receive an empty string for channel name, it means the
42 * default channel name is requested.
43 */
44 if (name[0] == '\0')
45 name = DEFAULT_CHANNEL_NAME;
46
54d01ffb
DG
47 DBG("Trying to find channel %s", name);
48
19e70852
DG
49 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
50 if (strcmp(name, chan->channel->name) == 0) {
51 DBG("Found channel by name %s", name);
52 return chan;
53 }
54 }
55
19e70852
DG
56 return NULL;
57}
58
00a62084
MD
59/*
60 * Find the event for the given channel.
61 */
62struct ltt_kernel_event *trace_kernel_find_event(
63 char *name, struct ltt_kernel_channel *channel,
64 enum lttng_event_type type,
65 struct lttng_filter_bytecode *filter)
66{
67 struct ltt_kernel_event *ev;
68 int found = 0;
69
70 assert(name);
71 assert(channel);
72
73 cds_list_for_each_entry(ev, &channel->events_list.head, list) {
74 if (type != LTTNG_EVENT_ALL && ev->type != type) {
75 continue;
76 }
77 if (strcmp(name, ev->event->name)) {
78 continue;
79 }
80 if ((ev->filter && !filter) || (!ev->filter && filter)) {
81 continue;
82 }
83 if (ev->filter && filter) {
84 if (ev->filter->len != filter->len ||
85 memcmp(ev->filter->data, filter->data,
86 filter->len) != 0) {
87 continue;
88 }
89 }
90 found = 1;
91 break;
92 }
93 if (found) {
94 DBG("Found event %s for channel %s", name,
95 channel->channel->name);
96 return ev;
97 } else {
98 return NULL;
99 }
100}
101
19e70852 102/*
050349bb 103 * Find the event name for the given channel.
19e70852 104 */
62499ad6 105struct ltt_kernel_event *trace_kernel_get_event_by_name(
d0ae4ea8
MD
106 char *name, struct ltt_kernel_channel *channel,
107 enum lttng_event_type type)
19e70852
DG
108{
109 struct ltt_kernel_event *ev;
00a62084 110 int found = 0;
19e70852 111
0525e9ae
DG
112 assert(name);
113 assert(channel);
19e70852
DG
114
115 cds_list_for_each_entry(ev, &channel->events_list.head, list) {
00a62084 116 if (type != LTTNG_EVENT_ALL && ev->type != type) {
d0ae4ea8 117 continue;
19e70852 118 }
00a62084
MD
119 if (strcmp(name, ev->event->name)) {
120 continue;
121 }
122 found = 1;
123 break;
124 }
125 if (found) {
126 DBG("Found event %s for channel %s", name,
127 channel->channel->name);
128 return ev;
129 } else {
130 return NULL;
19e70852 131 }
19e70852
DG
132}
133
54012638 134/*
050349bb 135 * Allocate and initialize a kernel session data structure.
54012638 136 *
050349bb 137 * Return pointer to structure or NULL.
54012638 138 */
dec56f6c 139struct ltt_kernel_session *trace_kernel_create_session(void)
54012638 140{
a4b92340 141 struct ltt_kernel_session *lks = NULL;
54012638
DG
142
143 /* Allocate a new ltt kernel session */
ba7f0ae5 144 lks = zmalloc(sizeof(struct ltt_kernel_session));
54012638 145 if (lks == NULL) {
df0f840b 146 PERROR("create kernel session zmalloc");
a4b92340 147 goto alloc_error;
54012638
DG
148 }
149
150 /* Init data structure */
03550b58
MD
151 lks->fd = -1;
152 lks->metadata_stream_fd = -1;
54012638
DG
153 lks->channel_count = 0;
154 lks->stream_count_global = 0;
155 lks->metadata = NULL;
156 CDS_INIT_LIST_HEAD(&lks->channel_list.head);
157
159b042f
JG
158 lks->tracker_pid = process_attr_tracker_create();
159 if (!lks->tracker_pid) {
55c9e7ca
JR
160 goto error;
161 }
159b042f
JG
162 lks->tracker_vpid = process_attr_tracker_create();
163 if (!lks->tracker_vpid) {
55c9e7ca
JR
164 goto error;
165 }
159b042f
JG
166 lks->tracker_uid = process_attr_tracker_create();
167 if (!lks->tracker_uid) {
55c9e7ca
JR
168 goto error;
169 }
159b042f
JG
170 lks->tracker_vuid = process_attr_tracker_create();
171 if (!lks->tracker_vuid) {
55c9e7ca
JR
172 goto error;
173 }
159b042f
JG
174 lks->tracker_gid = process_attr_tracker_create();
175 if (!lks->tracker_gid) {
55c9e7ca
JR
176 goto error;
177 }
159b042f
JG
178 lks->tracker_vgid = process_attr_tracker_create();
179 if (!lks->tracker_vgid) {
55c9e7ca
JR
180 goto error;
181 }
00e2e675
DG
182 lks->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
183 if (lks->consumer == NULL) {
184 goto error;
185 }
186
54012638
DG
187 return lks;
188
189error:
159b042f
JG
190 process_attr_tracker_destroy(lks->tracker_pid);
191 process_attr_tracker_destroy(lks->tracker_vpid);
192 process_attr_tracker_destroy(lks->tracker_uid);
193 process_attr_tracker_destroy(lks->tracker_vuid);
194 process_attr_tracker_destroy(lks->tracker_gid);
195 process_attr_tracker_destroy(lks->tracker_vgid);
a4b92340
DG
196 free(lks);
197
198alloc_error:
54012638
DG
199 return NULL;
200}
201
202/*
050349bb 203 * Allocate and initialize a kernel channel data structure.
54012638 204 *
050349bb 205 * Return pointer to structure or NULL.
54012638 206 */
00e2e675 207struct ltt_kernel_channel *trace_kernel_create_channel(
fdd9eb17 208 struct lttng_channel *chan)
54012638 209{
54012638 210 struct ltt_kernel_channel *lkc;
61a5b6b1 211 struct lttng_channel_extended *extended = NULL;
54012638 212
0525e9ae
DG
213 assert(chan);
214
ba7f0ae5 215 lkc = zmalloc(sizeof(struct ltt_kernel_channel));
f3ed775e 216 if (lkc == NULL) {
df0f840b 217 PERROR("ltt_kernel_channel zmalloc");
54012638
DG
218 goto error;
219 }
220
ba7f0ae5 221 lkc->channel = zmalloc(sizeof(struct lttng_channel));
f3ed775e 222 if (lkc->channel == NULL) {
df0f840b 223 PERROR("lttng_channel zmalloc");
e9404c27
JG
224 goto error;
225 }
226
227 extended = zmalloc(sizeof(struct lttng_channel_extended));
228 if (!extended) {
229 PERROR("lttng_channel_channel zmalloc");
f3ed775e
DG
230 goto error;
231 }
232 memcpy(lkc->channel, chan, sizeof(struct lttng_channel));
e9404c27
JG
233 memcpy(extended, chan->attr.extended.ptr, sizeof(struct lttng_channel_extended));
234 lkc->channel->attr.extended.ptr = extended;
235 extended = NULL;
54012638 236
85076754
MD
237 /*
238 * If we receive an empty string for channel name, it means the
239 * default channel name is requested.
240 */
241 if (chan->name[0] == '\0') {
242 strncpy(lkc->channel->name, DEFAULT_CHANNEL_NAME,
243 sizeof(lkc->channel->name));
244 }
bd722d76 245 lkc->channel->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
85076754 246
03550b58 247 lkc->fd = -1;
54012638 248 lkc->stream_count = 0;
cbbbb275 249 lkc->event_count = 0;
d36b8583 250 lkc->enabled = 1;
753873bf 251 lkc->published_to_notification_thread = false;
54012638
DG
252 /* Init linked list */
253 CDS_INIT_LIST_HEAD(&lkc->events_list.head);
254 CDS_INIT_LIST_HEAD(&lkc->stream_list.head);
645328ae 255 CDS_INIT_LIST_HEAD(&lkc->ctx_list);
54012638
DG
256
257 return lkc;
258
259error:
e9404c27
JG
260 if (lkc) {
261 free(lkc->channel);
262 }
263 free(extended);
264 free(lkc);
54012638
DG
265 return NULL;
266}
267
645328ae
DG
268/*
269 * Allocate and init a kernel context object.
270 *
271 * Return the allocated object or NULL on error.
272 */
273struct ltt_kernel_context *trace_kernel_create_context(
274 struct lttng_kernel_context *ctx)
275{
276 struct ltt_kernel_context *kctx;
277
278 kctx = zmalloc(sizeof(*kctx));
279 if (!kctx) {
280 PERROR("zmalloc kernel context");
281 goto error;
282 }
283
284 if (ctx) {
285 memcpy(&kctx->ctx, ctx, sizeof(kctx->ctx));
286 }
df3c77c8
JG
287error:
288 return kctx;
289}
645328ae 290
df3c77c8
JG
291/*
292 * Allocate and init a kernel context object from an existing kernel context
293 * object.
294 *
295 * Return the allocated object or NULL on error.
296 */
297struct ltt_kernel_context *trace_kernel_copy_context(
298 struct ltt_kernel_context *kctx)
299{
300 struct ltt_kernel_context *kctx_copy;
301
302 assert(kctx);
303 kctx_copy = zmalloc(sizeof(*kctx_copy));
304 if (!kctx_copy) {
305 PERROR("zmalloc ltt_kernel_context");
306 goto error;
307 }
308
309 memcpy(kctx_copy, kctx, sizeof(*kctx_copy));
310 memset(&kctx_copy->list, 0, sizeof(kctx_copy->list));
7b9445b3 311
645328ae 312error:
df3c77c8 313 return kctx_copy;
645328ae
DG
314}
315
54012638 316/*
050349bb 317 * Allocate and initialize a kernel event. Set name and event type.
a969e101 318 * We own filter_expression, and filter.
54012638 319 *
050349bb 320 * Return pointer to structure or NULL.
54012638 321 */
71a3bb01
FD
322enum lttng_error_code trace_kernel_create_event(
323 struct lttng_event *ev, char *filter_expression,
324 struct lttng_filter_bytecode *filter,
325 struct ltt_kernel_event **kernel_event)
54012638 326{
71a3bb01 327 enum lttng_error_code ret;
54012638 328 struct lttng_kernel_event *attr;
71a3bb01 329 struct ltt_kernel_event *local_kernel_event;
dcabc190 330 struct lttng_userspace_probe_location *userspace_probe_location = NULL;
54012638 331
0525e9ae
DG
332 assert(ev);
333
71a3bb01 334 local_kernel_event = zmalloc(sizeof(struct ltt_kernel_event));
ba7f0ae5 335 attr = zmalloc(sizeof(struct lttng_kernel_event));
71a3bb01 336 if (local_kernel_event == NULL || attr == NULL) {
df0f840b 337 PERROR("kernel event zmalloc");
71a3bb01 338 ret = LTTNG_ERR_NOMEM;
54012638
DG
339 goto error;
340 }
341
f3ed775e 342 switch (ev->type) {
7d29a247 343 case LTTNG_EVENT_PROBE:
e6ddca71 344 attr->instrumentation = LTTNG_KERNEL_KPROBE;
7d29a247
DG
345 attr->u.kprobe.addr = ev->attr.probe.addr;
346 attr->u.kprobe.offset = ev->attr.probe.offset;
f3ed775e 347 strncpy(attr->u.kprobe.symbol_name,
dbbb3ec5
DG
348 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
349 attr->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
f3ed775e 350 break;
dcabc190
FD
351 case LTTNG_EVENT_USERSPACE_PROBE:
352 {
87597c2c
JG
353 const struct lttng_userspace_probe_location* location = NULL;
354 const struct lttng_userspace_probe_location_lookup_method *lookup = NULL;
dcabc190
FD
355
356 location = lttng_event_get_userspace_probe_location(ev);
357 if (!location) {
358 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
359 goto error;
360 }
361
362 /*
363 * From this point on, the specific term 'uprobe' is used
364 * instead of the generic 'userspace probe' because it's the
365 * technology used at the moment for this instrumentation.
366 * LTTng currently implements userspace probes using uprobes.
367 * In the interactions with the kernel tracer, we use the
368 * uprobe term.
369 */
370 attr->instrumentation = LTTNG_KERNEL_UPROBE;
371
372 /*
373 * Only the elf lookup method is supported at the moment.
374 */
375 lookup = lttng_userspace_probe_location_get_lookup_method(
376 location);
377 if (!lookup) {
378 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
379 goto error;
380 }
381
382 /*
383 * From the kernel tracer's perspective, all userspace probe
384 * event types are all the same: a file and an offset.
385 */
386 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup)) {
387 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF:
388 /* Get the file descriptor on the target binary. */
389 attr->u.uprobe.fd =
390 lttng_userspace_probe_location_function_get_binary_fd(location);
391
392 /*
393 * Save a reference to the probe location used during
e368fb43 394 * the listing of events.
dcabc190
FD
395 */
396 userspace_probe_location =
397 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 =
402 lttng_userspace_probe_location_tracepoint_get_binary_fd(location);
403
404 /*
405 * Save a reference to the probe location used during the listing of
e368fb43 406 * events.
dcabc190
FD
407 */
408 userspace_probe_location =
409 lttng_userspace_probe_location_copy(location);
dcabc190
FD
410 break;
411 default:
412 DBG("Unsupported lookup method type");
413 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
414 goto error;
415 }
416 break;
417 }
f3ed775e 418 case LTTNG_EVENT_FUNCTION:
8f0d098b
MD
419 attr->instrumentation = LTTNG_KERNEL_KRETPROBE;
420 attr->u.kretprobe.addr = ev->attr.probe.addr;
421 attr->u.kretprobe.offset = ev->attr.probe.offset;
8f0d098b 422 strncpy(attr->u.kretprobe.symbol_name,
dbbb3ec5
DG
423 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
424 attr->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
8f0d098b
MD
425 break;
426 case LTTNG_EVENT_FUNCTION_ENTRY:
f3ed775e
DG
427 attr->instrumentation = LTTNG_KERNEL_FUNCTION;
428 strncpy(attr->u.ftrace.symbol_name,
dbbb3ec5
DG
429 ev->attr.ftrace.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
430 attr->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
f3ed775e 431 break;
e6ddca71
DG
432 case LTTNG_EVENT_TRACEPOINT:
433 attr->instrumentation = LTTNG_KERNEL_TRACEPOINT;
f3ed775e 434 break;
a54bd42d
MD
435 case LTTNG_EVENT_SYSCALL:
436 attr->instrumentation = LTTNG_KERNEL_SYSCALL;
0133c199 437 break;
7a3d1328
MD
438 case LTTNG_EVENT_ALL:
439 attr->instrumentation = LTTNG_KERNEL_ALL;
440 break;
f3ed775e
DG
441 default:
442 ERR("Unknown kernel instrumentation type (%d)", ev->type);
71a3bb01 443 ret = LTTNG_ERR_INVALID;
f3ed775e
DG
444 goto error;
445 }
446
447 /* Copy event name */
dbbb3ec5
DG
448 strncpy(attr->name, ev->name, LTTNG_KERNEL_SYM_NAME_LEN);
449 attr->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
f3ed775e 450
54012638 451 /* Setting up a kernel event */
71a3bb01
FD
452 local_kernel_event->fd = -1;
453 local_kernel_event->event = attr;
454 local_kernel_event->enabled = 1;
455 local_kernel_event->filter_expression = filter_expression;
456 local_kernel_event->filter = filter;
dcabc190 457 local_kernel_event->userspace_probe_location = userspace_probe_location;
54012638 458
71a3bb01
FD
459 *kernel_event = local_kernel_event;
460
461 return LTTNG_OK;
54012638
DG
462
463error:
a969e101
MD
464 free(filter_expression);
465 free(filter);
71a3bb01 466 free(local_kernel_event);
a2c0da86 467 free(attr);
71a3bb01 468 return ret;
54012638
DG
469}
470
471/*
050349bb 472 * Allocate and initialize a kernel metadata.
54012638 473 *
050349bb 474 * Return pointer to structure or NULL.
54012638 475 */
a4b92340 476struct ltt_kernel_metadata *trace_kernel_create_metadata(void)
54012638 477{
d42266a4 478 int ret;
54012638 479 struct ltt_kernel_metadata *lkm;
f3ed775e 480 struct lttng_channel *chan;
54012638 481
ba7f0ae5
DG
482 lkm = zmalloc(sizeof(struct ltt_kernel_metadata));
483 chan = zmalloc(sizeof(struct lttng_channel));
f3ed775e 484 if (lkm == NULL || chan == NULL) {
df0f840b 485 PERROR("kernel metadata zmalloc");
54012638
DG
486 goto error;
487 }
488
d42266a4
JG
489 ret = lttng_strncpy(
490 chan->name, DEFAULT_METADATA_NAME, sizeof(chan->name));
491 if (ret) {
492 ERR("Failed to initialize metadata channel name to `%s`",
493 DEFAULT_METADATA_NAME);
494 goto error;
495 }
496
54012638 497 /* Set default attributes */
d42266a4 498 chan->attr.overwrite = DEFAULT_METADATA_OVERWRITE;
3e230f92 499 chan->attr.subbuf_size = default_get_metadata_subbuf_size();
b389abbe 500 chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
d42266a4
JG
501 chan->attr.switch_timer_interval = DEFAULT_METADATA_SWITCH_TIMER;
502 chan->attr.read_timer_interval = DEFAULT_METADATA_READ_TIMER;;
503
504
505 /*
506 * The metadata channel of kernel sessions must use the "mmap"
507 * back-end since the consumer daemon accumulates complete
508 * metadata units before sending them to the relay daemon in
509 * live mode. The consumer daemon also needs to extract the contents
510 * of the metadata cache when computing a rotation position.
511 *
512 * In both cases, it is not possible to rely on the splice
513 * back-end as the consumer daemon may need to accumulate more
514 * content than can be backed by the ring buffer's underlying
515 * pages.
516 */
517 chan->attr.output = LTTNG_EVENT_MMAP;
518 chan->attr.tracefile_size = 0;
519 chan->attr.tracefile_count = 0;
520 chan->attr.live_timer_interval = 0;
54012638
DG
521
522 /* Init metadata */
03550b58 523 lkm->fd = -1;
f3ed775e 524 lkm->conf = chan;
54012638
DG
525
526 return lkm;
527
528error:
a2c0da86
MD
529 free(lkm);
530 free(chan);
54012638
DG
531 return NULL;
532}
533
534/*
050349bb
DG
535 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
536 * default.
54012638 537 *
050349bb 538 * Return pointer to structure or NULL.
54012638 539 */
00e2e675
DG
540struct ltt_kernel_stream *trace_kernel_create_stream(const char *name,
541 unsigned int count)
54012638 542{
00e2e675 543 int ret;
54012638
DG
544 struct ltt_kernel_stream *lks;
545
0525e9ae
DG
546 assert(name);
547
ba7f0ae5 548 lks = zmalloc(sizeof(struct ltt_kernel_stream));
54012638 549 if (lks == NULL) {
df0f840b 550 PERROR("kernel stream zmalloc");
54012638
DG
551 goto error;
552 }
553
00e2e675 554 /* Set name */
535b8ff4 555 ret = snprintf(lks->name, sizeof(lks->name), "%s_%u", name, count);
00e2e675
DG
556 if (ret < 0) {
557 PERROR("snprintf stream name");
558 goto error;
559 }
560 lks->name[sizeof(lks->name) - 1] = '\0';
561
54012638 562 /* Init stream */
03550b58 563 lks->fd = -1;
54012638 564 lks->state = 0;
ffe60014 565 lks->cpu = count;
54012638
DG
566
567 return lks;
568
569error:
570 return NULL;
571}
c363b55d 572
050349bb
DG
573/*
574 * Cleanup kernel stream structure.
575 */
62499ad6 576void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream)
c363b55d 577{
0525e9ae
DG
578 assert(stream);
579
33a2b854 580 DBG("[trace] Closing stream fd %d", stream->fd);
c363b55d 581 /* Close kernel fd */
03550b58 582 if (stream->fd >= 0) {
c617c0c6
MD
583 int ret;
584
03550b58
MD
585 ret = close(stream->fd);
586 if (ret) {
587 PERROR("close");
588 }
799e2c4f 589 }
c363b55d
DG
590 /* Remove from stream list */
591 cds_list_del(&stream->list);
f9815039 592
c363b55d
DG
593 free(stream);
594}
595
050349bb
DG
596/*
597 * Cleanup kernel event structure.
598 */
62499ad6 599void trace_kernel_destroy_event(struct ltt_kernel_event *event)
c363b55d 600{
0525e9ae
DG
601 assert(event);
602
87eb4ab8 603 if (event->fd >= 0) {
c617c0c6
MD
604 int ret;
605
87eb4ab8
MD
606 DBG("[trace] Closing event fd %d", event->fd);
607 /* Close kernel fd */
799e2c4f
MD
608 ret = close(event->fd);
609 if (ret) {
610 PERROR("close");
611 }
87eb4ab8
MD
612 } else {
613 DBG("[trace] Tearing down event (no associated fd)");
614 }
c363b55d
DG
615
616 /* Remove from event list */
617 cds_list_del(&event->list);
f9815039 618
00a62084
MD
619 free(event->filter_expression);
620 free(event->filter);
621
f9815039 622 free(event->event);
c363b55d
DG
623 free(event);
624}
625
645328ae
DG
626/*
627 * Cleanup kernel context structure.
628 */
629void trace_kernel_destroy_context(struct ltt_kernel_context *ctx)
630{
631 assert(ctx);
632
ba985c3a
JG
633 if (ctx->in_list) {
634 cds_list_del(&ctx->list);
635 }
645328ae
DG
636 free(ctx);
637}
638
050349bb
DG
639/*
640 * Cleanup kernel channel structure.
641 */
62499ad6 642void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel)
c363b55d 643{
af9737e9
DG
644 struct ltt_kernel_stream *stream, *stmp;
645 struct ltt_kernel_event *event, *etmp;
645328ae 646 struct ltt_kernel_context *ctx, *ctmp;
799e2c4f 647 int ret;
e9404c27 648 enum lttng_error_code status;
c363b55d 649
0525e9ae
DG
650 assert(channel);
651
33a2b854 652 DBG("[trace] Closing channel fd %d", channel->fd);
c363b55d 653 /* Close kernel fd */
03550b58
MD
654 if (channel->fd >= 0) {
655 ret = close(channel->fd);
656 if (ret) {
657 PERROR("close");
658 }
799e2c4f 659 }
c363b55d
DG
660
661 /* For each stream in the channel list */
af9737e9 662 cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) {
62499ad6 663 trace_kernel_destroy_stream(stream);
c363b55d
DG
664 }
665
666 /* For each event in the channel list */
af9737e9 667 cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) {
62499ad6 668 trace_kernel_destroy_event(event);
c363b55d
DG
669 }
670
645328ae
DG
671 /* For each context in the channel list */
672 cds_list_for_each_entry_safe(ctx, ctmp, &channel->ctx_list, list) {
673 trace_kernel_destroy_context(ctx);
674 }
675
c363b55d
DG
676 /* Remove from channel list */
677 cds_list_del(&channel->list);
f9815039 678
753873bf
JR
679 if (notification_thread_handle
680 && channel->published_to_notification_thread) {
63aaa3dc
JG
681 status = notification_thread_command_remove_channel(
682 notification_thread_handle,
e1f3997a 683 channel->key, LTTNG_DOMAIN_KERNEL);
63aaa3dc
JG
684 assert(status == LTTNG_OK);
685 }
e9404c27 686 free(channel->channel->attr.extended.ptr);
f9815039 687 free(channel->channel);
c363b55d
DG
688 free(channel);
689}
690
050349bb
DG
691/*
692 * Cleanup kernel metadata structure.
693 */
62499ad6 694void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata)
c363b55d 695{
0525e9ae
DG
696 assert(metadata);
697
33a2b854 698 DBG("[trace] Closing metadata fd %d", metadata->fd);
c363b55d 699 /* Close kernel fd */
03550b58 700 if (metadata->fd >= 0) {
c617c0c6
MD
701 int ret;
702
03550b58
MD
703 ret = close(metadata->fd);
704 if (ret) {
705 PERROR("close");
706 }
799e2c4f 707 }
c363b55d 708
f9815039 709 free(metadata->conf);
c363b55d
DG
710 free(metadata);
711}
712
050349bb 713/*
62499ad6 714 * Cleanup kernel session structure
36b588ed
MD
715 *
716 * Should *NOT* be called with RCU read-side lock held.
050349bb 717 */
62499ad6 718void trace_kernel_destroy_session(struct ltt_kernel_session *session)
c363b55d 719{
af9737e9 720 struct ltt_kernel_channel *channel, *ctmp;
799e2c4f 721 int ret;
c363b55d 722
0525e9ae
DG
723 assert(session);
724
33a2b854 725 DBG("[trace] Closing session fd %d", session->fd);
c363b55d 726 /* Close kernel fds */
03550b58
MD
727 if (session->fd >= 0) {
728 ret = close(session->fd);
729 if (ret) {
730 PERROR("close");
731 }
799e2c4f 732 }
f9815039 733
03550b58 734 if (session->metadata_stream_fd >= 0) {
70dc1c34 735 DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd);
799e2c4f
MD
736 ret = close(session->metadata_stream_fd);
737 if (ret) {
738 PERROR("close");
739 }
70dc1c34 740 }
c363b55d 741
d36b8583 742 if (session->metadata != NULL) {
62499ad6 743 trace_kernel_destroy_metadata(session->metadata);
d36b8583 744 }
c363b55d 745
af9737e9 746 cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) {
62499ad6 747 trace_kernel_destroy_channel(channel);
c363b55d 748 }
d070c424 749}
c363b55d 750
d070c424
MD
751/* Free elements needed by destroy notifiers. */
752void trace_kernel_free_session(struct ltt_kernel_session *session)
753{
00e2e675 754 /* Wipe consumer output object */
6addfa37 755 consumer_output_put(session->consumer);
00e2e675 756
159b042f
JG
757 process_attr_tracker_destroy(session->tracker_pid);
758 process_attr_tracker_destroy(session->tracker_vpid);
759 process_attr_tracker_destroy(session->tracker_uid);
760 process_attr_tracker_destroy(session->tracker_vuid);
761 process_attr_tracker_destroy(session->tracker_gid);
762 process_attr_tracker_destroy(session->tracker_vgid);
55c9e7ca 763
c363b55d
DG
764 free(session);
765}
This page took 0.126178 seconds and 5 git commands to generate.