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