Add benchmark directory to git ignore
[lttng-tools.git] / ltt-sessiond / trace-kernel.c
CommitLineData
54012638
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
82a3637f
DG
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
54012638
DG
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19#define _GNU_SOURCE
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
c363b55d 23#include <unistd.h>
54012638 24
1e307fab
DG
25#include <lttngerr.h>
26
62499ad6 27#include "trace-kernel.h"
54012638 28
19e70852 29/*
050349bb 30 * Find the channel name for the given kernel session.
19e70852 31 */
62499ad6 32struct ltt_kernel_channel *trace_kernel_get_channel_by_name(
19e70852
DG
33 char *name, struct ltt_kernel_session *session)
34{
35 struct ltt_kernel_channel *chan;
36
37 if (session == NULL) {
38 ERR("Undefine session");
39 goto error;
40 }
41
42 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
43 if (strcmp(name, chan->channel->name) == 0) {
44 DBG("Found channel by name %s", name);
45 return chan;
46 }
47 }
48
49error:
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
61 if (channel == NULL) {
62 ERR("Undefine channel");
63 goto error;
64 }
65
66 cds_list_for_each_entry(ev, &channel->events_list.head, list) {
67 if (strcmp(name, ev->event->name) == 0) {
68 DBG("Found event by name %s for channel %s", name,
69 channel->channel->name);
70 return ev;
71 }
72 }
73
74error:
75 return NULL;
76}
77
54012638 78/*
050349bb 79 * Allocate and initialize a kernel session data structure.
54012638 80 *
050349bb 81 * Return pointer to structure or NULL.
54012638 82 */
f9815039 83struct ltt_kernel_session *trace_kernel_create_session(char *path)
54012638 84{
f9815039 85 int ret;
54012638
DG
86 struct ltt_kernel_session *lks;
87
88 /* Allocate a new ltt kernel session */
89 lks = malloc(sizeof(struct ltt_kernel_session));
90 if (lks == NULL) {
91 perror("create kernel session malloc");
92 goto error;
93 }
94
95 /* Init data structure */
96 lks->fd = 0;
97 lks->metadata_stream_fd = 0;
98 lks->channel_count = 0;
99 lks->stream_count_global = 0;
100 lks->metadata = NULL;
d9800920 101 lks->consumer_fd = 0;
54012638
DG
102 CDS_INIT_LIST_HEAD(&lks->channel_list.head);
103
f9815039
DG
104 /* Set session path */
105 ret = asprintf(&lks->trace_path, "%s/kernel", path);
106 if (ret < 0) {
107 perror("asprintf kernel traces path");
108 goto error;
109 }
110
54012638
DG
111 return lks;
112
113error:
114 return NULL;
115}
116
117/*
050349bb 118 * Allocate and initialize a kernel channel data structure.
54012638 119 *
050349bb 120 * Return pointer to structure or NULL.
54012638 121 */
62499ad6 122struct ltt_kernel_channel *trace_kernel_create_channel(struct lttng_channel *chan, char *path)
54012638
DG
123{
124 int ret;
125 struct ltt_kernel_channel *lkc;
54012638
DG
126
127 lkc = malloc(sizeof(struct ltt_kernel_channel));
f3ed775e
DG
128 if (lkc == NULL) {
129 perror("ltt_kernel_channel malloc");
54012638
DG
130 goto error;
131 }
132
f3ed775e
DG
133 lkc->channel = malloc(sizeof(struct lttng_channel));
134 if (lkc->channel == NULL) {
135 perror("lttng_channel malloc");
136 goto error;
137 }
138 memcpy(lkc->channel, chan, sizeof(struct lttng_channel));
54012638
DG
139
140 lkc->fd = 0;
141 lkc->stream_count = 0;
cbbbb275 142 lkc->event_count = 0;
d36b8583 143 lkc->enabled = 1;
e75cda3a 144 lkc->ctx = NULL;
54012638
DG
145 /* Init linked list */
146 CDS_INIT_LIST_HEAD(&lkc->events_list.head);
147 CDS_INIT_LIST_HEAD(&lkc->stream_list.head);
148 /* Set default trace output path */
b082db07 149 ret = asprintf(&lkc->pathname, "%s", path);
54012638
DG
150 if (ret < 0) {
151 perror("asprintf kernel create channel");
152 goto error;
153 }
154
155 return lkc;
156
157error:
158 return NULL;
159}
160
161/*
050349bb 162 * Allocate and initialize a kernel event. Set name and event type.
54012638 163 *
050349bb 164 * Return pointer to structure or NULL.
54012638 165 */
62499ad6 166struct ltt_kernel_event *trace_kernel_create_event(struct lttng_event *ev)
54012638
DG
167{
168 struct ltt_kernel_event *lke;
169 struct lttng_kernel_event *attr;
170
171 lke = malloc(sizeof(struct ltt_kernel_event));
172 attr = malloc(sizeof(struct lttng_kernel_event));
173 if (lke == NULL || attr == NULL) {
174 perror("kernel event malloc");
175 goto error;
176 }
177
f3ed775e 178 switch (ev->type) {
7d29a247 179 case LTTNG_EVENT_PROBE:
e6ddca71 180 attr->instrumentation = LTTNG_KERNEL_KPROBE;
7d29a247
DG
181 attr->u.kprobe.addr = ev->attr.probe.addr;
182 attr->u.kprobe.offset = ev->attr.probe.offset;
f3ed775e 183 strncpy(attr->u.kprobe.symbol_name,
7d29a247 184 ev->attr.probe.symbol_name, LTTNG_SYM_NAME_LEN);
99497cd0 185 attr->u.kprobe.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
f3ed775e
DG
186 break;
187 case LTTNG_EVENT_FUNCTION:
8f0d098b
MD
188 attr->instrumentation = LTTNG_KERNEL_KRETPROBE;
189 attr->u.kretprobe.addr = ev->attr.probe.addr;
190 attr->u.kretprobe.offset = ev->attr.probe.offset;
191 attr->u.kretprobe.offset = ev->attr.probe.offset;
192 strncpy(attr->u.kretprobe.symbol_name,
193 ev->attr.probe.symbol_name, LTTNG_SYM_NAME_LEN);
99497cd0 194 attr->u.kretprobe.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
8f0d098b
MD
195 break;
196 case LTTNG_EVENT_FUNCTION_ENTRY:
f3ed775e
DG
197 attr->instrumentation = LTTNG_KERNEL_FUNCTION;
198 strncpy(attr->u.ftrace.symbol_name,
199 ev->attr.ftrace.symbol_name, LTTNG_SYM_NAME_LEN);
99497cd0 200 attr->u.ftrace.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
f3ed775e 201 break;
e6ddca71
DG
202 case LTTNG_EVENT_TRACEPOINT:
203 attr->instrumentation = LTTNG_KERNEL_TRACEPOINT;
f3ed775e
DG
204 break;
205 default:
206 ERR("Unknown kernel instrumentation type (%d)", ev->type);
207 goto error;
208 }
209
210 /* Copy event name */
211 strncpy(attr->name, ev->name, LTTNG_SYM_NAME_LEN);
99497cd0 212 attr->name[LTTNG_SYM_NAME_LEN - 1] = '\0';
f3ed775e 213
54012638
DG
214 /* Setting up a kernel event */
215 lke->fd = 0;
216 lke->event = attr;
d36b8583 217 lke->enabled = 1;
e75cda3a 218 lke->ctx = NULL;
54012638
DG
219
220 return lke;
221
222error:
223 return NULL;
224}
225
226/*
050349bb 227 * Allocate and initialize a kernel metadata.
54012638 228 *
050349bb 229 * Return pointer to structure or NULL.
54012638 230 */
62499ad6 231struct ltt_kernel_metadata *trace_kernel_create_metadata(char *path)
54012638
DG
232{
233 int ret;
234 struct ltt_kernel_metadata *lkm;
f3ed775e 235 struct lttng_channel *chan;
54012638
DG
236
237 lkm = malloc(sizeof(struct ltt_kernel_metadata));
f3ed775e
DG
238 chan = malloc(sizeof(struct lttng_channel));
239 if (lkm == NULL || chan == NULL) {
54012638
DG
240 perror("kernel metadata malloc");
241 goto error;
242 }
243
244 /* Set default attributes */
f3ed775e 245 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
b389abbe
MD
246 chan->attr.subbuf_size = DEFAULT_METADATA_SUBBUF_SIZE;
247 chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
f3ed775e
DG
248 chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
249 chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
7d452e12 250 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
54012638
DG
251
252 /* Init metadata */
253 lkm->fd = 0;
f3ed775e 254 lkm->conf = chan;
54012638 255 /* Set default metadata path */
58a97671 256 ret = asprintf(&lkm->pathname, "%s/metadata", path);
54012638
DG
257 if (ret < 0) {
258 perror("asprintf kernel metadata");
259 goto error;
260 }
261
262 return lkm;
263
264error:
265 return NULL;
266}
267
268/*
050349bb
DG
269 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
270 * default.
54012638 271 *
050349bb 272 * Return pointer to structure or NULL.
54012638 273 */
62499ad6 274struct ltt_kernel_stream *trace_kernel_create_stream(void)
54012638
DG
275{
276 struct ltt_kernel_stream *lks;
277
278 lks = malloc(sizeof(struct ltt_kernel_stream));
279 if (lks == NULL) {
280 perror("kernel stream malloc");
281 goto error;
282 }
283
284 /* Init stream */
285 lks->fd = 0;
286 lks->pathname = NULL;
287 lks->state = 0;
288
289 return lks;
290
291error:
292 return NULL;
293}
c363b55d 294
050349bb
DG
295/*
296 * Cleanup kernel stream structure.
297 */
62499ad6 298void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream)
c363b55d 299{
33a2b854 300 DBG("[trace] Closing stream fd %d", stream->fd);
c363b55d
DG
301 /* Close kernel fd */
302 close(stream->fd);
c363b55d
DG
303 /* Remove from stream list */
304 cds_list_del(&stream->list);
f9815039
DG
305
306 free(stream->pathname);
c363b55d
DG
307 free(stream);
308}
309
050349bb
DG
310/*
311 * Cleanup kernel event structure.
312 */
62499ad6 313void trace_kernel_destroy_event(struct ltt_kernel_event *event)
c363b55d 314{
33a2b854 315 DBG("[trace] Closing event fd %d", event->fd);
c363b55d
DG
316 /* Close kernel fd */
317 close(event->fd);
c363b55d
DG
318
319 /* Remove from event list */
320 cds_list_del(&event->list);
f9815039
DG
321
322 free(event->event);
323 free(event->ctx);
c363b55d
DG
324 free(event);
325}
326
050349bb
DG
327/*
328 * Cleanup kernel channel structure.
329 */
62499ad6 330void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel)
c363b55d 331{
af9737e9
DG
332 struct ltt_kernel_stream *stream, *stmp;
333 struct ltt_kernel_event *event, *etmp;
c363b55d 334
33a2b854 335 DBG("[trace] Closing channel fd %d", channel->fd);
c363b55d
DG
336 /* Close kernel fd */
337 close(channel->fd);
c363b55d
DG
338
339 /* For each stream in the channel list */
af9737e9 340 cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) {
62499ad6 341 trace_kernel_destroy_stream(stream);
c363b55d
DG
342 }
343
344 /* For each event in the channel list */
af9737e9 345 cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) {
62499ad6 346 trace_kernel_destroy_event(event);
c363b55d
DG
347 }
348
349 /* Remove from channel list */
350 cds_list_del(&channel->list);
f9815039
DG
351
352 free(channel->pathname);
353 free(channel->channel);
354 free(channel->ctx);
c363b55d
DG
355 free(channel);
356}
357
050349bb
DG
358/*
359 * Cleanup kernel metadata structure.
360 */
62499ad6 361void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata)
c363b55d 362{
33a2b854 363 DBG("[trace] Closing metadata fd %d", metadata->fd);
c363b55d
DG
364 /* Close kernel fd */
365 close(metadata->fd);
c363b55d 366
f9815039
DG
367 free(metadata->conf);
368 free(metadata->pathname);
c363b55d
DG
369 free(metadata);
370}
371
050349bb 372/*
62499ad6 373 * Cleanup kernel session structure
050349bb 374 */
62499ad6 375void trace_kernel_destroy_session(struct ltt_kernel_session *session)
c363b55d 376{
af9737e9 377 struct ltt_kernel_channel *channel, *ctmp;
c363b55d 378
33a2b854 379 DBG("[trace] Closing session fd %d", session->fd);
c363b55d
DG
380 /* Close kernel fds */
381 close(session->fd);
f9815039 382
70dc1c34
DG
383 if (session->metadata_stream_fd != 0) {
384 DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd);
385 close(session->metadata_stream_fd);
386 }
c363b55d 387
d36b8583 388 if (session->metadata != NULL) {
62499ad6 389 trace_kernel_destroy_metadata(session->metadata);
d36b8583 390 }
c363b55d 391
af9737e9 392 cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) {
62499ad6 393 trace_kernel_destroy_channel(channel);
c363b55d
DG
394 }
395
f9815039 396 free(session->trace_path);
c363b55d
DG
397 free(session);
398}
This page took 0.041996 seconds and 4 git commands to generate.