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