Fix tracing events data structure
[lttng-tools.git] / ltt-sessiond / trace.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
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
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
DG
24#include <urcu/list.h>
25
33a2b854 26#include "lttngerr.h"
54012638
DG
27#include "trace.h"
28
19e70852
DG
29/*
30 * get_kernel_channel_by_name
31 *
32 * Find the channel name for the given kernel session.
33 */
34struct ltt_kernel_channel *get_kernel_channel_by_name(
35 char *name, struct ltt_kernel_session *session)
36{
37 struct ltt_kernel_channel *chan;
38
39 if (session == NULL) {
40 ERR("Undefine session");
41 goto error;
42 }
43
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/*
56 * get_kernel_event_by_name
57 *
58 * Find the event name for the given channel.
59 */
60struct ltt_kernel_event *get_kernel_event_by_name(
61 char *name, struct ltt_kernel_channel *channel)
62{
63 struct ltt_kernel_event *ev;
64
65 if (channel == NULL) {
66 ERR("Undefine channel");
67 goto error;
68 }
69
70 cds_list_for_each_entry(ev, &channel->events_list.head, list) {
71 if (strcmp(name, ev->event->name) == 0) {
72 DBG("Found event by name %s for channel %s", name,
73 channel->channel->name);
74 return ev;
75 }
76 }
77
78error:
79 return NULL;
80}
81
54012638
DG
82/*
83 * trace_create_kernel_session
84 *
85 * Allocate and initialize a kernel session data structure.
86 *
87 * Return pointer to structure or NULL.
88 */
89struct ltt_kernel_session *trace_create_kernel_session(void)
90{
91 struct ltt_kernel_session *lks;
92
93 /* Allocate a new ltt kernel session */
94 lks = malloc(sizeof(struct ltt_kernel_session));
95 if (lks == NULL) {
96 perror("create kernel session malloc");
97 goto error;
98 }
99
100 /* Init data structure */
101 lks->fd = 0;
102 lks->metadata_stream_fd = 0;
103 lks->channel_count = 0;
104 lks->stream_count_global = 0;
105 lks->metadata = NULL;
106 CDS_INIT_LIST_HEAD(&lks->channel_list.head);
107
108 return lks;
109
110error:
111 return NULL;
112}
113
114/*
115 * trace_create_kernel_channel
116 *
117 * Allocate and initialize a kernel channel data structure.
118 *
119 * Return pointer to structure or NULL.
120 */
b082db07 121struct ltt_kernel_channel *trace_create_kernel_channel(struct lttng_channel *chan, char *path)
54012638
DG
122{
123 int ret;
124 struct ltt_kernel_channel *lkc;
54012638
DG
125
126 lkc = malloc(sizeof(struct ltt_kernel_channel));
f3ed775e
DG
127 if (lkc == NULL) {
128 perror("ltt_kernel_channel malloc");
54012638
DG
129 goto error;
130 }
131
f3ed775e
DG
132 lkc->channel = malloc(sizeof(struct lttng_channel));
133 if (lkc->channel == NULL) {
134 perror("lttng_channel malloc");
135 goto error;
136 }
137 memcpy(lkc->channel, chan, sizeof(struct lttng_channel));
54012638
DG
138
139 lkc->fd = 0;
140 lkc->stream_count = 0;
d36b8583 141 lkc->enabled = 1;
54012638
DG
142 /* Init linked list */
143 CDS_INIT_LIST_HEAD(&lkc->events_list.head);
144 CDS_INIT_LIST_HEAD(&lkc->stream_list.head);
145 /* Set default trace output path */
b082db07 146 ret = asprintf(&lkc->pathname, "%s", path);
54012638
DG
147 if (ret < 0) {
148 perror("asprintf kernel create channel");
149 goto error;
150 }
151
152 return lkc;
153
154error:
155 return NULL;
156}
157
158/*
159 * trace_create_kernel_event
160 *
161 * Allocate and initialize a kernel event. Set name and event type.
162 *
163 * Return pointer to structure or NULL.
164 */
f3ed775e 165struct ltt_kernel_event *trace_create_kernel_event(struct lttng_event *ev)
54012638
DG
166{
167 struct ltt_kernel_event *lke;
168 struct lttng_kernel_event *attr;
169
170 lke = malloc(sizeof(struct ltt_kernel_event));
171 attr = malloc(sizeof(struct lttng_kernel_event));
172 if (lke == NULL || attr == NULL) {
173 perror("kernel event malloc");
174 goto error;
175 }
176
f3ed775e 177 switch (ev->type) {
e6ddca71
DG
178 case LTTNG_EVENT_KPROBE:
179 attr->instrumentation = LTTNG_KERNEL_KPROBE;
f3ed775e
DG
180 attr->u.kprobe.addr = ev->attr.kprobe.addr;
181 attr->u.kprobe.offset = ev->attr.kprobe.offset;
182 strncpy(attr->u.kprobe.symbol_name,
183 ev->attr.kprobe.symbol_name, LTTNG_SYM_NAME_LEN);
184 break;
185 case LTTNG_EVENT_FUNCTION:
186 attr->instrumentation = LTTNG_KERNEL_FUNCTION;
187 strncpy(attr->u.ftrace.symbol_name,
188 ev->attr.ftrace.symbol_name, LTTNG_SYM_NAME_LEN);
189 break;
e6ddca71
DG
190 case LTTNG_EVENT_TRACEPOINT:
191 attr->instrumentation = LTTNG_KERNEL_TRACEPOINT;
f3ed775e
DG
192 break;
193 default:
194 ERR("Unknown kernel instrumentation type (%d)", ev->type);
195 goto error;
196 }
197
198 /* Copy event name */
199 strncpy(attr->name, ev->name, LTTNG_SYM_NAME_LEN);
200
54012638
DG
201 /* Setting up a kernel event */
202 lke->fd = 0;
203 lke->event = attr;
d36b8583 204 lke->enabled = 1;
54012638
DG
205
206 return lke;
207
208error:
209 return NULL;
210}
211
212/*
213 * trace_create_kernel_metadata
214 *
215 * Allocate and initialize a kernel metadata.
216 *
217 * Return pointer to structure or NULL.
218 */
58a97671 219struct ltt_kernel_metadata *trace_create_kernel_metadata(char *path)
54012638
DG
220{
221 int ret;
222 struct ltt_kernel_metadata *lkm;
f3ed775e 223 struct lttng_channel *chan;
54012638
DG
224
225 lkm = malloc(sizeof(struct ltt_kernel_metadata));
f3ed775e
DG
226 chan = malloc(sizeof(struct lttng_channel));
227 if (lkm == NULL || chan == NULL) {
54012638
DG
228 perror("kernel metadata malloc");
229 goto error;
230 }
231
232 /* Set default attributes */
f3ed775e
DG
233 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
234 chan->attr.subbuf_size = DEFAULT_CHANNEL_SUBBUF_SIZE;
235 chan->attr.num_subbuf = DEFAULT_CHANNEL_SUBBUF_NUM;
236 chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
237 chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
7d452e12 238 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
54012638
DG
239
240 /* Init metadata */
241 lkm->fd = 0;
f3ed775e 242 lkm->conf = chan;
54012638 243 /* Set default metadata path */
58a97671 244 ret = asprintf(&lkm->pathname, "%s/metadata", path);
54012638
DG
245 if (ret < 0) {
246 perror("asprintf kernel metadata");
247 goto error;
248 }
249
250 return lkm;
251
252error:
253 return NULL;
254}
255
256/*
257 * trace_create_kernel_stream
258 *
259 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
260 * default.
261 *
262 * Return pointer to structure or NULL.
263 */
264struct ltt_kernel_stream *trace_create_kernel_stream(void)
265{
266 struct ltt_kernel_stream *lks;
267
268 lks = malloc(sizeof(struct ltt_kernel_stream));
269 if (lks == NULL) {
270 perror("kernel stream malloc");
271 goto error;
272 }
273
274 /* Init stream */
275 lks->fd = 0;
276 lks->pathname = NULL;
277 lks->state = 0;
278
279 return lks;
280
281error:
282 return NULL;
283}
c363b55d
DG
284
285void trace_destroy_kernel_stream(struct ltt_kernel_stream *stream)
286{
33a2b854 287 DBG("[trace] Closing stream fd %d", stream->fd);
c363b55d
DG
288 /* Close kernel fd */
289 close(stream->fd);
290 free(stream->pathname);
291
292 /* Remove from stream list */
293 cds_list_del(&stream->list);
294 free(stream);
295}
296
297void trace_destroy_kernel_event(struct ltt_kernel_event *event)
298{
33a2b854 299 DBG("[trace] Closing event fd %d", event->fd);
c363b55d
DG
300 /* Close kernel fd */
301 close(event->fd);
302 /* Free attributes */
303 free(event->event);
304
305 /* Remove from event list */
306 cds_list_del(&event->list);
307 free(event);
308}
309
310void trace_destroy_kernel_channel(struct ltt_kernel_channel *channel)
311{
312 struct ltt_kernel_stream *stream;
313 struct ltt_kernel_event *event;
314
33a2b854 315 DBG("[trace] Closing channel fd %d", channel->fd);
c363b55d
DG
316 /* Close kernel fd */
317 close(channel->fd);
318 free(channel->pathname);
319 /* Free attributes structure */
320 free(channel->channel);
321
322 /* For each stream in the channel list */
323 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
324 trace_destroy_kernel_stream(stream);
325 }
326
327 /* For each event in the channel list */
328 cds_list_for_each_entry(event, &channel->events_list.head, list) {
329 trace_destroy_kernel_event(event);
330 }
331
332 /* Remove from channel list */
333 cds_list_del(&channel->list);
334 free(channel);
335}
336
337void trace_destroy_kernel_metadata(struct ltt_kernel_metadata *metadata)
338{
33a2b854 339 DBG("[trace] Closing metadata fd %d", metadata->fd);
c363b55d
DG
340 /* Close kernel fd */
341 close(metadata->fd);
342 /* Free attributes */
343 free(metadata->conf);
344
345 free(metadata);
346}
347
348void trace_destroy_kernel_session(struct ltt_kernel_session *session)
349{
350 struct ltt_kernel_channel *channel;
351
33a2b854 352 DBG("[trace] Closing session fd %d", session->fd);
c363b55d
DG
353 /* Close kernel fds */
354 close(session->fd);
70dc1c34
DG
355 if (session->metadata_stream_fd != 0) {
356 DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd);
357 close(session->metadata_stream_fd);
358 }
c363b55d 359
d36b8583
DG
360 if (session->metadata != NULL) {
361 trace_destroy_kernel_metadata(session->metadata);
362 }
c363b55d
DG
363
364 cds_list_for_each_entry(channel, &session->channel_list.head, list) {
365 trace_destroy_kernel_channel(channel);
366 }
367
368 free(session);
369}
This page took 0.036716 seconds and 4 git commands to generate.