Fix channel name in all kernel event test
[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
54d01ffb
DG
42 DBG("Trying to find channel %s", name);
43
19e70852
DG
44 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
45 if (strcmp(name, chan->channel->name) == 0) {
46 DBG("Found channel by name %s", name);
47 return chan;
48 }
49 }
50
51error:
52 return NULL;
53}
54
55/*
050349bb 56 * Find the event name for the given channel.
19e70852 57 */
62499ad6 58struct ltt_kernel_event *trace_kernel_get_event_by_name(
19e70852
DG
59 char *name, struct ltt_kernel_channel *channel)
60{
61 struct ltt_kernel_event *ev;
62
63 if (channel == NULL) {
64 ERR("Undefine channel");
65 goto error;
66 }
67
68 cds_list_for_each_entry(ev, &channel->events_list.head, list) {
69 if (strcmp(name, ev->event->name) == 0) {
70 DBG("Found event by name %s for channel %s", name,
71 channel->channel->name);
72 return ev;
73 }
74 }
75
76error:
77 return NULL;
78}
79
54012638 80/*
050349bb 81 * Allocate and initialize a kernel session data structure.
54012638 82 *
050349bb 83 * Return pointer to structure or NULL.
54012638 84 */
f9815039 85struct ltt_kernel_session *trace_kernel_create_session(char *path)
54012638 86{
f9815039 87 int ret;
54012638
DG
88 struct ltt_kernel_session *lks;
89
90 /* Allocate a new ltt kernel session */
91 lks = malloc(sizeof(struct ltt_kernel_session));
92 if (lks == NULL) {
93 perror("create kernel session malloc");
94 goto error;
95 }
96
97 /* Init data structure */
98 lks->fd = 0;
99 lks->metadata_stream_fd = 0;
100 lks->channel_count = 0;
101 lks->stream_count_global = 0;
102 lks->metadata = NULL;
d9800920 103 lks->consumer_fd = 0;
54012638
DG
104 CDS_INIT_LIST_HEAD(&lks->channel_list.head);
105
f9815039
DG
106 /* Set session path */
107 ret = asprintf(&lks->trace_path, "%s/kernel", path);
108 if (ret < 0) {
109 perror("asprintf kernel traces path");
110 goto error;
111 }
112
54012638
DG
113 return lks;
114
115error:
116 return NULL;
117}
118
119/*
050349bb 120 * Allocate and initialize a kernel channel data structure.
54012638 121 *
050349bb 122 * Return pointer to structure or NULL.
54012638 123 */
62499ad6 124struct ltt_kernel_channel *trace_kernel_create_channel(struct lttng_channel *chan, char *path)
54012638
DG
125{
126 int ret;
127 struct ltt_kernel_channel *lkc;
54012638
DG
128
129 lkc = malloc(sizeof(struct ltt_kernel_channel));
f3ed775e
DG
130 if (lkc == NULL) {
131 perror("ltt_kernel_channel malloc");
54012638
DG
132 goto error;
133 }
134
f3ed775e
DG
135 lkc->channel = malloc(sizeof(struct lttng_channel));
136 if (lkc->channel == NULL) {
137 perror("lttng_channel malloc");
138 goto error;
139 }
140 memcpy(lkc->channel, chan, sizeof(struct lttng_channel));
54012638
DG
141
142 lkc->fd = 0;
143 lkc->stream_count = 0;
cbbbb275 144 lkc->event_count = 0;
d36b8583 145 lkc->enabled = 1;
e75cda3a 146 lkc->ctx = NULL;
54012638
DG
147 /* Init linked list */
148 CDS_INIT_LIST_HEAD(&lkc->events_list.head);
149 CDS_INIT_LIST_HEAD(&lkc->stream_list.head);
150 /* Set default trace output path */
b082db07 151 ret = asprintf(&lkc->pathname, "%s", path);
54012638
DG
152 if (ret < 0) {
153 perror("asprintf kernel create channel");
154 goto error;
155 }
156
157 return lkc;
158
159error:
160 return NULL;
161}
162
163/*
050349bb 164 * Allocate and initialize a kernel event. Set name and event type.
54012638 165 *
050349bb 166 * Return pointer to structure or NULL.
54012638 167 */
62499ad6 168struct ltt_kernel_event *trace_kernel_create_event(struct lttng_event *ev)
54012638
DG
169{
170 struct ltt_kernel_event *lke;
171 struct lttng_kernel_event *attr;
172
173 lke = malloc(sizeof(struct ltt_kernel_event));
174 attr = malloc(sizeof(struct lttng_kernel_event));
175 if (lke == NULL || attr == NULL) {
176 perror("kernel event malloc");
177 goto error;
178 }
179
f3ed775e 180 switch (ev->type) {
7d29a247 181 case LTTNG_EVENT_PROBE:
e6ddca71 182 attr->instrumentation = LTTNG_KERNEL_KPROBE;
7d29a247
DG
183 attr->u.kprobe.addr = ev->attr.probe.addr;
184 attr->u.kprobe.offset = ev->attr.probe.offset;
f3ed775e 185 strncpy(attr->u.kprobe.symbol_name,
7d29a247 186 ev->attr.probe.symbol_name, LTTNG_SYM_NAME_LEN);
99497cd0 187 attr->u.kprobe.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
f3ed775e
DG
188 break;
189 case LTTNG_EVENT_FUNCTION:
8f0d098b
MD
190 attr->instrumentation = LTTNG_KERNEL_KRETPROBE;
191 attr->u.kretprobe.addr = ev->attr.probe.addr;
192 attr->u.kretprobe.offset = ev->attr.probe.offset;
193 attr->u.kretprobe.offset = ev->attr.probe.offset;
194 strncpy(attr->u.kretprobe.symbol_name,
195 ev->attr.probe.symbol_name, LTTNG_SYM_NAME_LEN);
99497cd0 196 attr->u.kretprobe.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
8f0d098b
MD
197 break;
198 case LTTNG_EVENT_FUNCTION_ENTRY:
f3ed775e
DG
199 attr->instrumentation = LTTNG_KERNEL_FUNCTION;
200 strncpy(attr->u.ftrace.symbol_name,
201 ev->attr.ftrace.symbol_name, LTTNG_SYM_NAME_LEN);
99497cd0 202 attr->u.ftrace.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
f3ed775e 203 break;
e6ddca71
DG
204 case LTTNG_EVENT_TRACEPOINT:
205 attr->instrumentation = LTTNG_KERNEL_TRACEPOINT;
f3ed775e 206 break;
a54bd42d
MD
207 case LTTNG_EVENT_SYSCALL:
208 attr->instrumentation = LTTNG_KERNEL_SYSCALL;
0133c199 209 break;
f3ed775e
DG
210 default:
211 ERR("Unknown kernel instrumentation type (%d)", ev->type);
212 goto error;
213 }
214
215 /* Copy event name */
216 strncpy(attr->name, ev->name, LTTNG_SYM_NAME_LEN);
99497cd0 217 attr->name[LTTNG_SYM_NAME_LEN - 1] = '\0';
f3ed775e 218
54012638
DG
219 /* Setting up a kernel event */
220 lke->fd = 0;
221 lke->event = attr;
d36b8583 222 lke->enabled = 1;
e75cda3a 223 lke->ctx = NULL;
54012638
DG
224
225 return lke;
226
227error:
228 return NULL;
229}
230
231/*
050349bb 232 * Allocate and initialize a kernel metadata.
54012638 233 *
050349bb 234 * Return pointer to structure or NULL.
54012638 235 */
62499ad6 236struct ltt_kernel_metadata *trace_kernel_create_metadata(char *path)
54012638
DG
237{
238 int ret;
239 struct ltt_kernel_metadata *lkm;
f3ed775e 240 struct lttng_channel *chan;
54012638
DG
241
242 lkm = malloc(sizeof(struct ltt_kernel_metadata));
f3ed775e
DG
243 chan = malloc(sizeof(struct lttng_channel));
244 if (lkm == NULL || chan == NULL) {
54012638
DG
245 perror("kernel metadata malloc");
246 goto error;
247 }
248
249 /* Set default attributes */
f3ed775e 250 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
b389abbe
MD
251 chan->attr.subbuf_size = DEFAULT_METADATA_SUBBUF_SIZE;
252 chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
f3ed775e
DG
253 chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
254 chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
7d452e12 255 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
54012638
DG
256
257 /* Init metadata */
258 lkm->fd = 0;
f3ed775e 259 lkm->conf = chan;
54012638 260 /* Set default metadata path */
58a97671 261 ret = asprintf(&lkm->pathname, "%s/metadata", path);
54012638
DG
262 if (ret < 0) {
263 perror("asprintf kernel metadata");
264 goto error;
265 }
266
267 return lkm;
268
269error:
270 return NULL;
271}
272
273/*
050349bb
DG
274 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
275 * default.
54012638 276 *
050349bb 277 * Return pointer to structure or NULL.
54012638 278 */
62499ad6 279struct ltt_kernel_stream *trace_kernel_create_stream(void)
54012638
DG
280{
281 struct ltt_kernel_stream *lks;
282
283 lks = malloc(sizeof(struct ltt_kernel_stream));
284 if (lks == NULL) {
285 perror("kernel stream malloc");
286 goto error;
287 }
288
289 /* Init stream */
290 lks->fd = 0;
291 lks->pathname = NULL;
292 lks->state = 0;
293
294 return lks;
295
296error:
297 return NULL;
298}
c363b55d 299
050349bb
DG
300/*
301 * Cleanup kernel stream structure.
302 */
62499ad6 303void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream)
c363b55d 304{
33a2b854 305 DBG("[trace] Closing stream fd %d", stream->fd);
c363b55d
DG
306 /* Close kernel fd */
307 close(stream->fd);
c363b55d
DG
308 /* Remove from stream list */
309 cds_list_del(&stream->list);
f9815039
DG
310
311 free(stream->pathname);
c363b55d
DG
312 free(stream);
313}
314
050349bb
DG
315/*
316 * Cleanup kernel event structure.
317 */
62499ad6 318void trace_kernel_destroy_event(struct ltt_kernel_event *event)
c363b55d 319{
33a2b854 320 DBG("[trace] Closing event fd %d", event->fd);
c363b55d
DG
321 /* Close kernel fd */
322 close(event->fd);
c363b55d
DG
323
324 /* Remove from event list */
325 cds_list_del(&event->list);
f9815039
DG
326
327 free(event->event);
328 free(event->ctx);
c363b55d
DG
329 free(event);
330}
331
050349bb
DG
332/*
333 * Cleanup kernel channel structure.
334 */
62499ad6 335void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel)
c363b55d 336{
af9737e9
DG
337 struct ltt_kernel_stream *stream, *stmp;
338 struct ltt_kernel_event *event, *etmp;
c363b55d 339
33a2b854 340 DBG("[trace] Closing channel fd %d", channel->fd);
c363b55d
DG
341 /* Close kernel fd */
342 close(channel->fd);
c363b55d
DG
343
344 /* For each stream in the channel list */
af9737e9 345 cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) {
62499ad6 346 trace_kernel_destroy_stream(stream);
c363b55d
DG
347 }
348
349 /* For each event in the channel list */
af9737e9 350 cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) {
62499ad6 351 trace_kernel_destroy_event(event);
c363b55d
DG
352 }
353
354 /* Remove from channel list */
355 cds_list_del(&channel->list);
f9815039
DG
356
357 free(channel->pathname);
358 free(channel->channel);
359 free(channel->ctx);
c363b55d
DG
360 free(channel);
361}
362
050349bb
DG
363/*
364 * Cleanup kernel metadata structure.
365 */
62499ad6 366void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata)
c363b55d 367{
33a2b854 368 DBG("[trace] Closing metadata fd %d", metadata->fd);
c363b55d
DG
369 /* Close kernel fd */
370 close(metadata->fd);
c363b55d 371
f9815039
DG
372 free(metadata->conf);
373 free(metadata->pathname);
c363b55d
DG
374 free(metadata);
375}
376
050349bb 377/*
62499ad6 378 * Cleanup kernel session structure
050349bb 379 */
62499ad6 380void trace_kernel_destroy_session(struct ltt_kernel_session *session)
c363b55d 381{
af9737e9 382 struct ltt_kernel_channel *channel, *ctmp;
c363b55d 383
33a2b854 384 DBG("[trace] Closing session fd %d", session->fd);
c363b55d
DG
385 /* Close kernel fds */
386 close(session->fd);
f9815039 387
70dc1c34
DG
388 if (session->metadata_stream_fd != 0) {
389 DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd);
390 close(session->metadata_stream_fd);
391 }
c363b55d 392
d36b8583 393 if (session->metadata != NULL) {
62499ad6 394 trace_kernel_destroy_metadata(session->metadata);
d36b8583 395 }
c363b55d 396
af9737e9 397 cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) {
62499ad6 398 trace_kernel_destroy_channel(channel);
c363b55d
DG
399 }
400
f9815039 401 free(session->trace_path);
c363b55d
DG
402 free(session);
403}
This page took 0.0441 seconds and 4 git commands to generate.