Add benchmark directory to git ignore
[lttng-tools.git] / ltt-sessiond / trace-kernel.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; only version 2
7 * of the License.
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
25 #include <lttngerr.h>
26
27 #include "trace-kernel.h"
28
29 /*
30 * Find the channel name for the given kernel session.
31 */
32 struct ltt_kernel_channel *trace_kernel_get_channel_by_name(
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
49 error:
50 return NULL;
51 }
52
53 /*
54 * Find the event name for the given channel.
55 */
56 struct ltt_kernel_event *trace_kernel_get_event_by_name(
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
74 error:
75 return NULL;
76 }
77
78 /*
79 * Allocate and initialize a kernel session data structure.
80 *
81 * Return pointer to structure or NULL.
82 */
83 struct ltt_kernel_session *trace_kernel_create_session(char *path)
84 {
85 int ret;
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;
101 lks->consumer_fd = 0;
102 CDS_INIT_LIST_HEAD(&lks->channel_list.head);
103
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
111 return lks;
112
113 error:
114 return NULL;
115 }
116
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_kernel_create_channel(struct lttng_channel *chan, char *path)
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 lkc->event_count = 0;
143 lkc->enabled = 1;
144 lkc->ctx = NULL;
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 */
149 ret = asprintf(&lkc->pathname, "%s", path);
150 if (ret < 0) {
151 perror("asprintf kernel create channel");
152 goto error;
153 }
154
155 return lkc;
156
157 error:
158 return NULL;
159 }
160
161 /*
162 * Allocate and initialize a kernel event. Set name and event type.
163 *
164 * Return pointer to structure or NULL.
165 */
166 struct ltt_kernel_event *trace_kernel_create_event(struct lttng_event *ev)
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
178 switch (ev->type) {
179 case LTTNG_EVENT_PROBE:
180 attr->instrumentation = LTTNG_KERNEL_KPROBE;
181 attr->u.kprobe.addr = ev->attr.probe.addr;
182 attr->u.kprobe.offset = ev->attr.probe.offset;
183 strncpy(attr->u.kprobe.symbol_name,
184 ev->attr.probe.symbol_name, LTTNG_SYM_NAME_LEN);
185 attr->u.kprobe.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
186 break;
187 case LTTNG_EVENT_FUNCTION:
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);
194 attr->u.kretprobe.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
195 break;
196 case LTTNG_EVENT_FUNCTION_ENTRY:
197 attr->instrumentation = LTTNG_KERNEL_FUNCTION;
198 strncpy(attr->u.ftrace.symbol_name,
199 ev->attr.ftrace.symbol_name, LTTNG_SYM_NAME_LEN);
200 attr->u.ftrace.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
201 break;
202 case LTTNG_EVENT_TRACEPOINT:
203 attr->instrumentation = LTTNG_KERNEL_TRACEPOINT;
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);
212 attr->name[LTTNG_SYM_NAME_LEN - 1] = '\0';
213
214 /* Setting up a kernel event */
215 lke->fd = 0;
216 lke->event = attr;
217 lke->enabled = 1;
218 lke->ctx = NULL;
219
220 return lke;
221
222 error:
223 return NULL;
224 }
225
226 /*
227 * Allocate and initialize a kernel metadata.
228 *
229 * Return pointer to structure or NULL.
230 */
231 struct ltt_kernel_metadata *trace_kernel_create_metadata(char *path)
232 {
233 int ret;
234 struct ltt_kernel_metadata *lkm;
235 struct lttng_channel *chan;
236
237 lkm = malloc(sizeof(struct ltt_kernel_metadata));
238 chan = malloc(sizeof(struct lttng_channel));
239 if (lkm == NULL || chan == NULL) {
240 perror("kernel metadata malloc");
241 goto error;
242 }
243
244 /* Set default attributes */
245 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
246 chan->attr.subbuf_size = DEFAULT_METADATA_SUBBUF_SIZE;
247 chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
248 chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
249 chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
250 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
251
252 /* Init metadata */
253 lkm->fd = 0;
254 lkm->conf = chan;
255 /* Set default metadata path */
256 ret = asprintf(&lkm->pathname, "%s/metadata", path);
257 if (ret < 0) {
258 perror("asprintf kernel metadata");
259 goto error;
260 }
261
262 return lkm;
263
264 error:
265 return NULL;
266 }
267
268 /*
269 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
270 * default.
271 *
272 * Return pointer to structure or NULL.
273 */
274 struct ltt_kernel_stream *trace_kernel_create_stream(void)
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
291 error:
292 return NULL;
293 }
294
295 /*
296 * Cleanup kernel stream structure.
297 */
298 void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream)
299 {
300 DBG("[trace] Closing stream fd %d", stream->fd);
301 /* Close kernel fd */
302 close(stream->fd);
303 /* Remove from stream list */
304 cds_list_del(&stream->list);
305
306 free(stream->pathname);
307 free(stream);
308 }
309
310 /*
311 * Cleanup kernel event structure.
312 */
313 void trace_kernel_destroy_event(struct ltt_kernel_event *event)
314 {
315 DBG("[trace] Closing event fd %d", event->fd);
316 /* Close kernel fd */
317 close(event->fd);
318
319 /* Remove from event list */
320 cds_list_del(&event->list);
321
322 free(event->event);
323 free(event->ctx);
324 free(event);
325 }
326
327 /*
328 * Cleanup kernel channel structure.
329 */
330 void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel)
331 {
332 struct ltt_kernel_stream *stream, *stmp;
333 struct ltt_kernel_event *event, *etmp;
334
335 DBG("[trace] Closing channel fd %d", channel->fd);
336 /* Close kernel fd */
337 close(channel->fd);
338
339 /* For each stream in the channel list */
340 cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) {
341 trace_kernel_destroy_stream(stream);
342 }
343
344 /* For each event in the channel list */
345 cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) {
346 trace_kernel_destroy_event(event);
347 }
348
349 /* Remove from channel list */
350 cds_list_del(&channel->list);
351
352 free(channel->pathname);
353 free(channel->channel);
354 free(channel->ctx);
355 free(channel);
356 }
357
358 /*
359 * Cleanup kernel metadata structure.
360 */
361 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata)
362 {
363 DBG("[trace] Closing metadata fd %d", metadata->fd);
364 /* Close kernel fd */
365 close(metadata->fd);
366
367 free(metadata->conf);
368 free(metadata->pathname);
369 free(metadata);
370 }
371
372 /*
373 * Cleanup kernel session structure
374 */
375 void trace_kernel_destroy_session(struct ltt_kernel_session *session)
376 {
377 struct ltt_kernel_channel *channel, *ctmp;
378
379 DBG("[trace] Closing session fd %d", session->fd);
380 /* Close kernel fds */
381 close(session->fd);
382
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 }
387
388 if (session->metadata != NULL) {
389 trace_kernel_destroy_metadata(session->metadata);
390 }
391
392 cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) {
393 trace_kernel_destroy_channel(channel);
394 }
395
396 free(session->trace_path);
397 free(session);
398 }
This page took 0.036107 seconds and 4 git commands to generate.