Port: Remove _GNU_SOURCE, defined in config.h
[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 }
243
7b9445b3
DG
244 CDS_INIT_LIST_HEAD(&kctx->list);
245
645328ae
DG
246error:
247 return kctx;
248}
249
54012638 250/*
050349bb 251 * Allocate and initialize a kernel event. Set name and event type.
a969e101 252 * We own filter_expression, and filter.
54012638 253 *
050349bb 254 * Return pointer to structure or NULL.
54012638 255 */
00a62084
MD
256struct ltt_kernel_event *trace_kernel_create_event(struct lttng_event *ev,
257 char *filter_expression, struct lttng_filter_bytecode *filter)
54012638
DG
258{
259 struct ltt_kernel_event *lke;
260 struct lttng_kernel_event *attr;
261
0525e9ae
DG
262 assert(ev);
263
ba7f0ae5
DG
264 lke = zmalloc(sizeof(struct ltt_kernel_event));
265 attr = zmalloc(sizeof(struct lttng_kernel_event));
54012638 266 if (lke == NULL || attr == NULL) {
df0f840b 267 PERROR("kernel event zmalloc");
54012638
DG
268 goto error;
269 }
270
f3ed775e 271 switch (ev->type) {
7d29a247 272 case LTTNG_EVENT_PROBE:
e6ddca71 273 attr->instrumentation = LTTNG_KERNEL_KPROBE;
7d29a247
DG
274 attr->u.kprobe.addr = ev->attr.probe.addr;
275 attr->u.kprobe.offset = ev->attr.probe.offset;
f3ed775e 276 strncpy(attr->u.kprobe.symbol_name,
dbbb3ec5
DG
277 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
278 attr->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
f3ed775e
DG
279 break;
280 case LTTNG_EVENT_FUNCTION:
8f0d098b
MD
281 attr->instrumentation = LTTNG_KERNEL_KRETPROBE;
282 attr->u.kretprobe.addr = ev->attr.probe.addr;
283 attr->u.kretprobe.offset = ev->attr.probe.offset;
8f0d098b 284 strncpy(attr->u.kretprobe.symbol_name,
dbbb3ec5
DG
285 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
286 attr->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
8f0d098b
MD
287 break;
288 case LTTNG_EVENT_FUNCTION_ENTRY:
f3ed775e
DG
289 attr->instrumentation = LTTNG_KERNEL_FUNCTION;
290 strncpy(attr->u.ftrace.symbol_name,
dbbb3ec5
DG
291 ev->attr.ftrace.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
292 attr->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
f3ed775e 293 break;
e6ddca71
DG
294 case LTTNG_EVENT_TRACEPOINT:
295 attr->instrumentation = LTTNG_KERNEL_TRACEPOINT;
f3ed775e 296 break;
a54bd42d
MD
297 case LTTNG_EVENT_SYSCALL:
298 attr->instrumentation = LTTNG_KERNEL_SYSCALL;
0133c199 299 break;
7a3d1328
MD
300 case LTTNG_EVENT_ALL:
301 attr->instrumentation = LTTNG_KERNEL_ALL;
302 break;
f3ed775e
DG
303 default:
304 ERR("Unknown kernel instrumentation type (%d)", ev->type);
305 goto error;
306 }
307
308 /* Copy event name */
dbbb3ec5
DG
309 strncpy(attr->name, ev->name, LTTNG_KERNEL_SYM_NAME_LEN);
310 attr->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
f3ed775e 311
54012638 312 /* Setting up a kernel event */
03550b58 313 lke->fd = -1;
54012638 314 lke->event = attr;
d36b8583 315 lke->enabled = 1;
00a62084
MD
316 lke->filter_expression = filter_expression;
317 lke->filter = filter;
54012638
DG
318
319 return lke;
320
321error:
a969e101
MD
322 free(filter_expression);
323 free(filter);
a2c0da86
MD
324 free(lke);
325 free(attr);
54012638
DG
326 return NULL;
327}
328
329/*
050349bb 330 * Allocate and initialize a kernel metadata.
54012638 331 *
050349bb 332 * Return pointer to structure or NULL.
54012638 333 */
a4b92340 334struct ltt_kernel_metadata *trace_kernel_create_metadata(void)
54012638 335{
54012638 336 struct ltt_kernel_metadata *lkm;
f3ed775e 337 struct lttng_channel *chan;
54012638 338
ba7f0ae5
DG
339 lkm = zmalloc(sizeof(struct ltt_kernel_metadata));
340 chan = zmalloc(sizeof(struct lttng_channel));
f3ed775e 341 if (lkm == NULL || chan == NULL) {
df0f840b 342 PERROR("kernel metadata zmalloc");
54012638
DG
343 goto error;
344 }
345
346 /* Set default attributes */
f3ed775e 347 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
3e230f92 348 chan->attr.subbuf_size = default_get_metadata_subbuf_size();
b389abbe 349 chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
6bb9e85f
MD
350 chan->attr.switch_timer_interval = DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
351 chan->attr.read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
7d452e12 352 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
54012638
DG
353
354 /* Init metadata */
03550b58 355 lkm->fd = -1;
f3ed775e 356 lkm->conf = chan;
54012638
DG
357
358 return lkm;
359
360error:
a2c0da86
MD
361 free(lkm);
362 free(chan);
54012638
DG
363 return NULL;
364}
365
366/*
050349bb
DG
367 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
368 * default.
54012638 369 *
050349bb 370 * Return pointer to structure or NULL.
54012638 371 */
00e2e675
DG
372struct ltt_kernel_stream *trace_kernel_create_stream(const char *name,
373 unsigned int count)
54012638 374{
00e2e675 375 int ret;
54012638
DG
376 struct ltt_kernel_stream *lks;
377
0525e9ae
DG
378 assert(name);
379
ba7f0ae5 380 lks = zmalloc(sizeof(struct ltt_kernel_stream));
54012638 381 if (lks == NULL) {
df0f840b 382 PERROR("kernel stream zmalloc");
54012638
DG
383 goto error;
384 }
385
00e2e675 386 /* Set name */
535b8ff4 387 ret = snprintf(lks->name, sizeof(lks->name), "%s_%u", name, count);
00e2e675
DG
388 if (ret < 0) {
389 PERROR("snprintf stream name");
390 goto error;
391 }
392 lks->name[sizeof(lks->name) - 1] = '\0';
393
54012638 394 /* Init stream */
03550b58 395 lks->fd = -1;
54012638 396 lks->state = 0;
ffe60014 397 lks->cpu = count;
54012638
DG
398
399 return lks;
400
401error:
402 return NULL;
403}
c363b55d 404
050349bb
DG
405/*
406 * Cleanup kernel stream structure.
407 */
62499ad6 408void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream)
c363b55d 409{
0525e9ae
DG
410 assert(stream);
411
33a2b854 412 DBG("[trace] Closing stream fd %d", stream->fd);
c363b55d 413 /* Close kernel fd */
03550b58 414 if (stream->fd >= 0) {
c617c0c6
MD
415 int ret;
416
03550b58
MD
417 ret = close(stream->fd);
418 if (ret) {
419 PERROR("close");
420 }
799e2c4f 421 }
c363b55d
DG
422 /* Remove from stream list */
423 cds_list_del(&stream->list);
f9815039 424
c363b55d
DG
425 free(stream);
426}
427
050349bb
DG
428/*
429 * Cleanup kernel event structure.
430 */
62499ad6 431void trace_kernel_destroy_event(struct ltt_kernel_event *event)
c363b55d 432{
0525e9ae
DG
433 assert(event);
434
87eb4ab8 435 if (event->fd >= 0) {
c617c0c6
MD
436 int ret;
437
87eb4ab8
MD
438 DBG("[trace] Closing event fd %d", event->fd);
439 /* Close kernel fd */
799e2c4f
MD
440 ret = close(event->fd);
441 if (ret) {
442 PERROR("close");
443 }
87eb4ab8
MD
444 } else {
445 DBG("[trace] Tearing down event (no associated fd)");
446 }
c363b55d
DG
447
448 /* Remove from event list */
449 cds_list_del(&event->list);
f9815039 450
00a62084
MD
451 free(event->filter_expression);
452 free(event->filter);
453
f9815039 454 free(event->event);
c363b55d
DG
455 free(event);
456}
457
645328ae
DG
458/*
459 * Cleanup kernel context structure.
460 */
461void trace_kernel_destroy_context(struct ltt_kernel_context *ctx)
462{
463 assert(ctx);
464
465 cds_list_del(&ctx->list);
466 free(ctx);
467}
468
050349bb
DG
469/*
470 * Cleanup kernel channel structure.
471 */
62499ad6 472void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel)
c363b55d 473{
af9737e9
DG
474 struct ltt_kernel_stream *stream, *stmp;
475 struct ltt_kernel_event *event, *etmp;
645328ae 476 struct ltt_kernel_context *ctx, *ctmp;
799e2c4f 477 int ret;
c363b55d 478
0525e9ae
DG
479 assert(channel);
480
33a2b854 481 DBG("[trace] Closing channel fd %d", channel->fd);
c363b55d 482 /* Close kernel fd */
03550b58
MD
483 if (channel->fd >= 0) {
484 ret = close(channel->fd);
485 if (ret) {
486 PERROR("close");
487 }
799e2c4f 488 }
c363b55d
DG
489
490 /* For each stream in the channel list */
af9737e9 491 cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) {
62499ad6 492 trace_kernel_destroy_stream(stream);
c363b55d
DG
493 }
494
495 /* For each event in the channel list */
af9737e9 496 cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) {
62499ad6 497 trace_kernel_destroy_event(event);
c363b55d
DG
498 }
499
645328ae
DG
500 /* For each context in the channel list */
501 cds_list_for_each_entry_safe(ctx, ctmp, &channel->ctx_list, list) {
502 trace_kernel_destroy_context(ctx);
503 }
504
c363b55d
DG
505 /* Remove from channel list */
506 cds_list_del(&channel->list);
f9815039 507
f9815039 508 free(channel->channel);
c363b55d
DG
509 free(channel);
510}
511
050349bb
DG
512/*
513 * Cleanup kernel metadata structure.
514 */
62499ad6 515void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata)
c363b55d 516{
0525e9ae
DG
517 assert(metadata);
518
33a2b854 519 DBG("[trace] Closing metadata fd %d", metadata->fd);
c363b55d 520 /* Close kernel fd */
03550b58 521 if (metadata->fd >= 0) {
c617c0c6
MD
522 int ret;
523
03550b58
MD
524 ret = close(metadata->fd);
525 if (ret) {
526 PERROR("close");
527 }
799e2c4f 528 }
c363b55d 529
f9815039 530 free(metadata->conf);
c363b55d
DG
531 free(metadata);
532}
533
050349bb 534/*
62499ad6 535 * Cleanup kernel session structure
36b588ed
MD
536 *
537 * Should *NOT* be called with RCU read-side lock held.
050349bb 538 */
62499ad6 539void trace_kernel_destroy_session(struct ltt_kernel_session *session)
c363b55d 540{
af9737e9 541 struct ltt_kernel_channel *channel, *ctmp;
799e2c4f 542 int ret;
c363b55d 543
0525e9ae
DG
544 assert(session);
545
33a2b854 546 DBG("[trace] Closing session fd %d", session->fd);
c363b55d 547 /* Close kernel fds */
03550b58
MD
548 if (session->fd >= 0) {
549 ret = close(session->fd);
550 if (ret) {
551 PERROR("close");
552 }
799e2c4f 553 }
f9815039 554
03550b58 555 if (session->metadata_stream_fd >= 0) {
70dc1c34 556 DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd);
799e2c4f
MD
557 ret = close(session->metadata_stream_fd);
558 if (ret) {
559 PERROR("close");
560 }
70dc1c34 561 }
c363b55d 562
d36b8583 563 if (session->metadata != NULL) {
62499ad6 564 trace_kernel_destroy_metadata(session->metadata);
d36b8583 565 }
c363b55d 566
af9737e9 567 cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) {
62499ad6 568 trace_kernel_destroy_channel(channel);
c363b55d
DG
569 }
570
00e2e675 571 /* Wipe consumer output object */
6addfa37 572 consumer_output_put(session->consumer);
00e2e675 573
c363b55d
DG
574 free(session);
575}
This page took 0.084034 seconds and 5 git commands to generate.