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