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