kernel: Add `struct lttng_kernel_syscall` to ABI
[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;
c863e5a6
FD
437 attr->u.syscall.abi = LTTNG_KERNEL_SYSCALL_ABI_ALL;
438 attr->u.syscall.entryexit = LTTNG_KERNEL_SYSCALL_ENTRYEXIT;
439 attr->u.syscall.match = LTTNG_KERNEL_SYSCALL_MATCH_NAME;
0133c199 440 break;
7a3d1328
MD
441 case LTTNG_EVENT_ALL:
442 attr->instrumentation = LTTNG_KERNEL_ALL;
443 break;
f3ed775e
DG
444 default:
445 ERR("Unknown kernel instrumentation type (%d)", ev->type);
71a3bb01 446 ret = LTTNG_ERR_INVALID;
f3ed775e
DG
447 goto error;
448 }
449
450 /* Copy event name */
dbbb3ec5
DG
451 strncpy(attr->name, ev->name, LTTNG_KERNEL_SYM_NAME_LEN);
452 attr->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
f3ed775e 453
54012638 454 /* Setting up a kernel event */
71a3bb01
FD
455 local_kernel_event->fd = -1;
456 local_kernel_event->event = attr;
457 local_kernel_event->enabled = 1;
458 local_kernel_event->filter_expression = filter_expression;
459 local_kernel_event->filter = filter;
dcabc190 460 local_kernel_event->userspace_probe_location = userspace_probe_location;
54012638 461
71a3bb01
FD
462 *kernel_event = local_kernel_event;
463
464 return LTTNG_OK;
54012638
DG
465
466error:
a969e101
MD
467 free(filter_expression);
468 free(filter);
71a3bb01 469 free(local_kernel_event);
a2c0da86 470 free(attr);
71a3bb01 471 return ret;
54012638
DG
472}
473
474/*
050349bb 475 * Allocate and initialize a kernel metadata.
54012638 476 *
050349bb 477 * Return pointer to structure or NULL.
54012638 478 */
a4b92340 479struct ltt_kernel_metadata *trace_kernel_create_metadata(void)
54012638 480{
d42266a4 481 int ret;
54012638 482 struct ltt_kernel_metadata *lkm;
f3ed775e 483 struct lttng_channel *chan;
54012638 484
ba7f0ae5
DG
485 lkm = zmalloc(sizeof(struct ltt_kernel_metadata));
486 chan = zmalloc(sizeof(struct lttng_channel));
f3ed775e 487 if (lkm == NULL || chan == NULL) {
df0f840b 488 PERROR("kernel metadata zmalloc");
54012638
DG
489 goto error;
490 }
491
d42266a4
JG
492 ret = lttng_strncpy(
493 chan->name, DEFAULT_METADATA_NAME, sizeof(chan->name));
494 if (ret) {
495 ERR("Failed to initialize metadata channel name to `%s`",
496 DEFAULT_METADATA_NAME);
497 goto error;
498 }
499
54012638 500 /* Set default attributes */
d42266a4 501 chan->attr.overwrite = DEFAULT_METADATA_OVERWRITE;
3e230f92 502 chan->attr.subbuf_size = default_get_metadata_subbuf_size();
b389abbe 503 chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
d42266a4
JG
504 chan->attr.switch_timer_interval = DEFAULT_METADATA_SWITCH_TIMER;
505 chan->attr.read_timer_interval = DEFAULT_METADATA_READ_TIMER;;
506
507
508 /*
509 * The metadata channel of kernel sessions must use the "mmap"
510 * back-end since the consumer daemon accumulates complete
511 * metadata units before sending them to the relay daemon in
512 * live mode. The consumer daemon also needs to extract the contents
513 * of the metadata cache when computing a rotation position.
514 *
515 * In both cases, it is not possible to rely on the splice
516 * back-end as the consumer daemon may need to accumulate more
517 * content than can be backed by the ring buffer's underlying
518 * pages.
519 */
520 chan->attr.output = LTTNG_EVENT_MMAP;
521 chan->attr.tracefile_size = 0;
522 chan->attr.tracefile_count = 0;
523 chan->attr.live_timer_interval = 0;
54012638
DG
524
525 /* Init metadata */
03550b58 526 lkm->fd = -1;
f3ed775e 527 lkm->conf = chan;
54012638
DG
528
529 return lkm;
530
531error:
a2c0da86
MD
532 free(lkm);
533 free(chan);
54012638
DG
534 return NULL;
535}
536
537/*
050349bb
DG
538 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
539 * default.
54012638 540 *
050349bb 541 * Return pointer to structure or NULL.
54012638 542 */
00e2e675
DG
543struct ltt_kernel_stream *trace_kernel_create_stream(const char *name,
544 unsigned int count)
54012638 545{
00e2e675 546 int ret;
54012638
DG
547 struct ltt_kernel_stream *lks;
548
0525e9ae
DG
549 assert(name);
550
ba7f0ae5 551 lks = zmalloc(sizeof(struct ltt_kernel_stream));
54012638 552 if (lks == NULL) {
df0f840b 553 PERROR("kernel stream zmalloc");
54012638
DG
554 goto error;
555 }
556
00e2e675 557 /* Set name */
535b8ff4 558 ret = snprintf(lks->name, sizeof(lks->name), "%s_%u", name, count);
00e2e675
DG
559 if (ret < 0) {
560 PERROR("snprintf stream name");
561 goto error;
562 }
563 lks->name[sizeof(lks->name) - 1] = '\0';
564
54012638 565 /* Init stream */
03550b58 566 lks->fd = -1;
54012638 567 lks->state = 0;
ffe60014 568 lks->cpu = count;
54012638
DG
569
570 return lks;
571
572error:
573 return NULL;
574}
c363b55d 575
050349bb
DG
576/*
577 * Cleanup kernel stream structure.
578 */
62499ad6 579void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream)
c363b55d 580{
0525e9ae
DG
581 assert(stream);
582
33a2b854 583 DBG("[trace] Closing stream fd %d", stream->fd);
c363b55d 584 /* Close kernel fd */
03550b58 585 if (stream->fd >= 0) {
c617c0c6
MD
586 int ret;
587
03550b58
MD
588 ret = close(stream->fd);
589 if (ret) {
590 PERROR("close");
591 }
799e2c4f 592 }
c363b55d
DG
593 /* Remove from stream list */
594 cds_list_del(&stream->list);
f9815039 595
c363b55d
DG
596 free(stream);
597}
598
050349bb
DG
599/*
600 * Cleanup kernel event structure.
601 */
62499ad6 602void trace_kernel_destroy_event(struct ltt_kernel_event *event)
c363b55d 603{
0525e9ae
DG
604 assert(event);
605
87eb4ab8 606 if (event->fd >= 0) {
c617c0c6
MD
607 int ret;
608
87eb4ab8
MD
609 DBG("[trace] Closing event fd %d", event->fd);
610 /* Close kernel fd */
799e2c4f
MD
611 ret = close(event->fd);
612 if (ret) {
613 PERROR("close");
614 }
87eb4ab8
MD
615 } else {
616 DBG("[trace] Tearing down event (no associated fd)");
617 }
c363b55d
DG
618
619 /* Remove from event list */
620 cds_list_del(&event->list);
f9815039 621
00a62084
MD
622 free(event->filter_expression);
623 free(event->filter);
624
f9815039 625 free(event->event);
c363b55d
DG
626 free(event);
627}
628
645328ae
DG
629/*
630 * Cleanup kernel context structure.
631 */
632void trace_kernel_destroy_context(struct ltt_kernel_context *ctx)
633{
634 assert(ctx);
635
ba985c3a
JG
636 if (ctx->in_list) {
637 cds_list_del(&ctx->list);
638 }
645328ae
DG
639 free(ctx);
640}
641
050349bb
DG
642/*
643 * Cleanup kernel channel structure.
644 */
62499ad6 645void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel)
c363b55d 646{
af9737e9
DG
647 struct ltt_kernel_stream *stream, *stmp;
648 struct ltt_kernel_event *event, *etmp;
645328ae 649 struct ltt_kernel_context *ctx, *ctmp;
799e2c4f 650 int ret;
e9404c27 651 enum lttng_error_code status;
c363b55d 652
0525e9ae
DG
653 assert(channel);
654
33a2b854 655 DBG("[trace] Closing channel fd %d", channel->fd);
c363b55d 656 /* Close kernel fd */
03550b58
MD
657 if (channel->fd >= 0) {
658 ret = close(channel->fd);
659 if (ret) {
660 PERROR("close");
661 }
799e2c4f 662 }
c363b55d
DG
663
664 /* For each stream in the channel list */
af9737e9 665 cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) {
62499ad6 666 trace_kernel_destroy_stream(stream);
c363b55d
DG
667 }
668
669 /* For each event in the channel list */
af9737e9 670 cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) {
62499ad6 671 trace_kernel_destroy_event(event);
c363b55d
DG
672 }
673
645328ae
DG
674 /* For each context in the channel list */
675 cds_list_for_each_entry_safe(ctx, ctmp, &channel->ctx_list, list) {
676 trace_kernel_destroy_context(ctx);
677 }
678
c363b55d
DG
679 /* Remove from channel list */
680 cds_list_del(&channel->list);
f9815039 681
753873bf
JR
682 if (notification_thread_handle
683 && channel->published_to_notification_thread) {
63aaa3dc
JG
684 status = notification_thread_command_remove_channel(
685 notification_thread_handle,
e1f3997a 686 channel->key, LTTNG_DOMAIN_KERNEL);
63aaa3dc
JG
687 assert(status == LTTNG_OK);
688 }
e9404c27 689 free(channel->channel->attr.extended.ptr);
f9815039 690 free(channel->channel);
c363b55d
DG
691 free(channel);
692}
693
050349bb
DG
694/*
695 * Cleanup kernel metadata structure.
696 */
62499ad6 697void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata)
c363b55d 698{
0525e9ae
DG
699 assert(metadata);
700
33a2b854 701 DBG("[trace] Closing metadata fd %d", metadata->fd);
c363b55d 702 /* Close kernel fd */
03550b58 703 if (metadata->fd >= 0) {
c617c0c6
MD
704 int ret;
705
03550b58
MD
706 ret = close(metadata->fd);
707 if (ret) {
708 PERROR("close");
709 }
799e2c4f 710 }
c363b55d 711
f9815039 712 free(metadata->conf);
c363b55d
DG
713 free(metadata);
714}
715
050349bb 716/*
62499ad6 717 * Cleanup kernel session structure
36b588ed
MD
718 *
719 * Should *NOT* be called with RCU read-side lock held.
050349bb 720 */
62499ad6 721void trace_kernel_destroy_session(struct ltt_kernel_session *session)
c363b55d 722{
af9737e9 723 struct ltt_kernel_channel *channel, *ctmp;
799e2c4f 724 int ret;
c363b55d 725
0525e9ae
DG
726 assert(session);
727
33a2b854 728 DBG("[trace] Closing session fd %d", session->fd);
c363b55d 729 /* Close kernel fds */
03550b58
MD
730 if (session->fd >= 0) {
731 ret = close(session->fd);
732 if (ret) {
733 PERROR("close");
734 }
799e2c4f 735 }
f9815039 736
03550b58 737 if (session->metadata_stream_fd >= 0) {
70dc1c34 738 DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd);
799e2c4f
MD
739 ret = close(session->metadata_stream_fd);
740 if (ret) {
741 PERROR("close");
742 }
70dc1c34 743 }
c363b55d 744
d36b8583 745 if (session->metadata != NULL) {
62499ad6 746 trace_kernel_destroy_metadata(session->metadata);
d36b8583 747 }
c363b55d 748
af9737e9 749 cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) {
62499ad6 750 trace_kernel_destroy_channel(channel);
c363b55d 751 }
d070c424 752}
c363b55d 753
d070c424
MD
754/* Free elements needed by destroy notifiers. */
755void trace_kernel_free_session(struct ltt_kernel_session *session)
756{
00e2e675 757 /* Wipe consumer output object */
6addfa37 758 consumer_output_put(session->consumer);
00e2e675 759
159b042f
JG
760 process_attr_tracker_destroy(session->tracker_pid);
761 process_attr_tracker_destroy(session->tracker_vpid);
762 process_attr_tracker_destroy(session->tracker_uid);
763 process_attr_tracker_destroy(session->tracker_vuid);
764 process_attr_tracker_destroy(session->tracker_gid);
765 process_attr_tracker_destroy(session->tracker_vgid);
55c9e7ca 766
c363b55d
DG
767 free(session);
768}
This page took 0.099927 seconds and 4 git commands to generate.