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