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