Add enable kernel event using the enable ioctl
[lttng-tools.git] / ltt-sessiond / trace.c
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>
23 #include <unistd.h>
24 #include <urcu/list.h>
25
26 #include "lttngerr.h"
27 #include "ltt-sessiond.h"
28 #include "trace.h"
29
30 /*
31 * get_kernel_channel_by_name
32 *
33 * Find the channel name for the given kernel session.
34 */
35 struct 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
52 error:
53 return NULL;
54 }
55
56 /*
57 * get_kernel_event_by_name
58 *
59 * Find the event name for the given channel.
60 */
61 struct 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
79 error:
80 return NULL;
81 }
82
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 */
90 struct 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
111 error:
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 */
122 struct ltt_kernel_channel *trace_create_kernel_channel(struct lttng_channel *chan)
123 {
124 int ret;
125 struct ltt_kernel_channel *lkc;
126
127 lkc = malloc(sizeof(struct ltt_kernel_channel));
128 if (lkc == NULL) {
129 perror("ltt_kernel_channel malloc");
130 goto error;
131 }
132
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));
139
140 lkc->fd = 0;
141 lkc->stream_count = 0;
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 */
146 ret = asprintf(&lkc->pathname, "%s", DEFAULT_TRACE_OUTPUT);
147 if (ret < 0) {
148 perror("asprintf kernel create channel");
149 goto error;
150 }
151
152 return lkc;
153
154 error:
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 */
165 struct ltt_kernel_event *trace_create_kernel_event(struct lttng_event *ev)
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
177 switch (ev->type) {
178 case LTTNG_EVENT_KPROBES:
179 attr->instrumentation = LTTNG_KERNEL_KPROBES;
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;
190 case LTTNG_EVENT_TRACEPOINTS:
191 attr->instrumentation = LTTNG_KERNEL_TRACEPOINTS;
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
201 /* Setting up a kernel event */
202 lke->fd = 0;
203 lke->event = attr;
204
205 return lke;
206
207 error:
208 return NULL;
209 }
210
211 /*
212 * trace_create_kernel_metadata
213 *
214 * Allocate and initialize a kernel metadata.
215 *
216 * Return pointer to structure or NULL.
217 */
218 struct ltt_kernel_metadata *trace_create_kernel_metadata(void)
219 {
220 int ret;
221 struct ltt_kernel_metadata *lkm;
222 struct lttng_channel *chan;
223
224 lkm = malloc(sizeof(struct ltt_kernel_metadata));
225 chan = malloc(sizeof(struct lttng_channel));
226 if (lkm == NULL || chan == NULL) {
227 perror("kernel metadata malloc");
228 goto error;
229 }
230
231 /* Set default attributes */
232 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
233 chan->attr.subbuf_size = DEFAULT_CHANNEL_SUBBUF_SIZE;
234 chan->attr.num_subbuf = DEFAULT_CHANNEL_SUBBUF_NUM;
235 chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
236 chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
237
238 /* Init metadata */
239 lkm->fd = 0;
240 lkm->conf = chan;
241 /* Set default metadata path */
242 ret = asprintf(&lkm->pathname, "%s/metadata", DEFAULT_TRACE_OUTPUT);
243 if (ret < 0) {
244 perror("asprintf kernel metadata");
245 goto error;
246 }
247
248 return lkm;
249
250 error:
251 return NULL;
252 }
253
254 /*
255 * trace_create_kernel_stream
256 *
257 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
258 * default.
259 *
260 * Return pointer to structure or NULL.
261 */
262 struct ltt_kernel_stream *trace_create_kernel_stream(void)
263 {
264 struct ltt_kernel_stream *lks;
265
266 lks = malloc(sizeof(struct ltt_kernel_stream));
267 if (lks == NULL) {
268 perror("kernel stream malloc");
269 goto error;
270 }
271
272 /* Init stream */
273 lks->fd = 0;
274 lks->pathname = NULL;
275 lks->state = 0;
276
277 return lks;
278
279 error:
280 return NULL;
281 }
282
283 void trace_destroy_kernel_stream(struct ltt_kernel_stream *stream)
284 {
285 DBG("[trace] Closing stream fd %d", stream->fd);
286 /* Close kernel fd */
287 close(stream->fd);
288 free(stream->pathname);
289
290 /* Remove from stream list */
291 cds_list_del(&stream->list);
292 free(stream);
293 }
294
295 void trace_destroy_kernel_event(struct ltt_kernel_event *event)
296 {
297 DBG("[trace] Closing event fd %d", event->fd);
298 /* Close kernel fd */
299 close(event->fd);
300 /* Free attributes */
301 free(event->event);
302
303 /* Remove from event list */
304 cds_list_del(&event->list);
305 free(event);
306 }
307
308 void trace_destroy_kernel_channel(struct ltt_kernel_channel *channel)
309 {
310 struct ltt_kernel_stream *stream;
311 struct ltt_kernel_event *event;
312
313 DBG("[trace] Closing channel fd %d", channel->fd);
314 /* Close kernel fd */
315 close(channel->fd);
316 free(channel->pathname);
317 /* Free attributes structure */
318 free(channel->channel);
319
320 /* For each stream in the channel list */
321 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
322 trace_destroy_kernel_stream(stream);
323 }
324
325 /* For each event in the channel list */
326 cds_list_for_each_entry(event, &channel->events_list.head, list) {
327 trace_destroy_kernel_event(event);
328 }
329
330 /* Remove from channel list */
331 cds_list_del(&channel->list);
332 free(channel);
333 }
334
335 void trace_destroy_kernel_metadata(struct ltt_kernel_metadata *metadata)
336 {
337 DBG("[trace] Closing metadata fd %d", metadata->fd);
338 /* Close kernel fd */
339 close(metadata->fd);
340 /* Free attributes */
341 free(metadata->conf);
342
343 free(metadata);
344 }
345
346 void trace_destroy_kernel_session(struct ltt_kernel_session *session)
347 {
348 struct ltt_kernel_channel *channel;
349
350 DBG("[trace] Closing session fd %d", session->fd);
351 /* Close kernel fds */
352 close(session->fd);
353 if (session->metadata_stream_fd != 0) {
354 DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd);
355 close(session->metadata_stream_fd);
356 }
357
358 trace_destroy_kernel_metadata(session->metadata);
359
360 cds_list_for_each_entry(channel, &session->channel_list.head, list) {
361 trace_destroy_kernel_channel(channel);
362 }
363
364 free(session);
365 }
This page took 0.036882 seconds and 4 git commands to generate.