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