Fix: various memleaks
[lttng-tools.git] / src / bin / lttng-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 modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23
24 #include <common/common.h>
25 #include <common/defaults.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 DBG("Trying to find channel %s", name);
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
51 error:
52 return NULL;
53 }
54
55 /*
56 * Find the event name for the given channel.
57 */
58 struct ltt_kernel_event *trace_kernel_get_event_by_name(
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
76 error:
77 return NULL;
78 }
79
80 /*
81 * Allocate and initialize a kernel session data structure.
82 *
83 * Return pointer to structure or NULL.
84 */
85 struct ltt_kernel_session *trace_kernel_create_session(char *path)
86 {
87 int ret;
88 struct ltt_kernel_session *lks;
89
90 /* Allocate a new ltt kernel session */
91 lks = zmalloc(sizeof(struct ltt_kernel_session));
92 if (lks == NULL) {
93 PERROR("create kernel session zmalloc");
94 goto error;
95 }
96
97 /* Init data structure */
98 lks->fd = -1;
99 lks->metadata_stream_fd = -1;
100 lks->channel_count = 0;
101 lks->stream_count_global = 0;
102 lks->metadata = NULL;
103 lks->consumer_fd = -1;
104 CDS_INIT_LIST_HEAD(&lks->channel_list.head);
105
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
113 return lks;
114
115 error:
116 return NULL;
117 }
118
119 /*
120 * Allocate and initialize a kernel channel data structure.
121 *
122 * Return pointer to structure or NULL.
123 */
124 struct ltt_kernel_channel *trace_kernel_create_channel(struct lttng_channel *chan, char *path)
125 {
126 int ret;
127 struct ltt_kernel_channel *lkc;
128
129 lkc = zmalloc(sizeof(struct ltt_kernel_channel));
130 if (lkc == NULL) {
131 PERROR("ltt_kernel_channel zmalloc");
132 goto error;
133 }
134
135 lkc->channel = zmalloc(sizeof(struct lttng_channel));
136 if (lkc->channel == NULL) {
137 PERROR("lttng_channel zmalloc");
138 goto error;
139 }
140 memcpy(lkc->channel, chan, sizeof(struct lttng_channel));
141
142 lkc->fd = -1;
143 lkc->stream_count = 0;
144 lkc->event_count = 0;
145 lkc->enabled = 1;
146 lkc->ctx = NULL;
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 */
151 ret = asprintf(&lkc->pathname, "%s", path);
152 if (ret < 0) {
153 PERROR("asprintf kernel create channel");
154 goto error;
155 }
156
157 return lkc;
158
159 error:
160 return NULL;
161 }
162
163 /*
164 * Allocate and initialize a kernel event. Set name and event type.
165 *
166 * Return pointer to structure or NULL.
167 */
168 struct ltt_kernel_event *trace_kernel_create_event(struct lttng_event *ev)
169 {
170 struct ltt_kernel_event *lke;
171 struct lttng_kernel_event *attr;
172
173 lke = zmalloc(sizeof(struct ltt_kernel_event));
174 attr = zmalloc(sizeof(struct lttng_kernel_event));
175 if (lke == NULL || attr == NULL) {
176 PERROR("kernel event zmalloc");
177 goto error;
178 }
179
180 switch (ev->type) {
181 case LTTNG_EVENT_PROBE:
182 attr->instrumentation = LTTNG_KERNEL_KPROBE;
183 attr->u.kprobe.addr = ev->attr.probe.addr;
184 attr->u.kprobe.offset = ev->attr.probe.offset;
185 strncpy(attr->u.kprobe.symbol_name,
186 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
187 attr->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
188 break;
189 case LTTNG_EVENT_FUNCTION:
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_KERNEL_SYM_NAME_LEN);
196 attr->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
197 break;
198 case LTTNG_EVENT_FUNCTION_ENTRY:
199 attr->instrumentation = LTTNG_KERNEL_FUNCTION;
200 strncpy(attr->u.ftrace.symbol_name,
201 ev->attr.ftrace.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
202 attr->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
203 break;
204 case LTTNG_EVENT_TRACEPOINT:
205 attr->instrumentation = LTTNG_KERNEL_TRACEPOINT;
206 break;
207 case LTTNG_EVENT_SYSCALL:
208 attr->instrumentation = LTTNG_KERNEL_SYSCALL;
209 break;
210 case LTTNG_EVENT_ALL:
211 attr->instrumentation = LTTNG_KERNEL_ALL;
212 break;
213 default:
214 ERR("Unknown kernel instrumentation type (%d)", ev->type);
215 goto error;
216 }
217
218 /* Copy event name */
219 strncpy(attr->name, ev->name, LTTNG_KERNEL_SYM_NAME_LEN);
220 attr->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
221
222 /* Setting up a kernel event */
223 lke->fd = -1;
224 lke->event = attr;
225 lke->enabled = 1;
226 lke->ctx = NULL;
227
228 return lke;
229
230 error:
231 free(lke);
232 free(attr);
233 return NULL;
234 }
235
236 /*
237 * Allocate and initialize a kernel metadata.
238 *
239 * Return pointer to structure or NULL.
240 */
241 struct ltt_kernel_metadata *trace_kernel_create_metadata(char *path)
242 {
243 int ret;
244 struct ltt_kernel_metadata *lkm;
245 struct lttng_channel *chan;
246
247 lkm = zmalloc(sizeof(struct ltt_kernel_metadata));
248 chan = zmalloc(sizeof(struct lttng_channel));
249 if (lkm == NULL || chan == NULL) {
250 PERROR("kernel metadata zmalloc");
251 goto error;
252 }
253
254 /* Set default attributes */
255 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
256 chan->attr.subbuf_size = DEFAULT_METADATA_SUBBUF_SIZE;
257 chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
258 chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
259 chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
260 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
261
262 /* Init metadata */
263 lkm->fd = -1;
264 lkm->conf = chan;
265 /* Set default metadata path */
266 ret = asprintf(&lkm->pathname, "%s/metadata", path);
267 if (ret < 0) {
268 PERROR("asprintf kernel metadata");
269 goto error;
270 }
271
272 return lkm;
273
274 error:
275 free(lkm);
276 free(chan);
277 return NULL;
278 }
279
280 /*
281 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
282 * default.
283 *
284 * Return pointer to structure or NULL.
285 */
286 struct ltt_kernel_stream *trace_kernel_create_stream(void)
287 {
288 struct ltt_kernel_stream *lks;
289
290 lks = zmalloc(sizeof(struct ltt_kernel_stream));
291 if (lks == NULL) {
292 PERROR("kernel stream zmalloc");
293 goto error;
294 }
295
296 /* Init stream */
297 lks->fd = -1;
298 lks->pathname = NULL;
299 lks->state = 0;
300
301 return lks;
302
303 error:
304 return NULL;
305 }
306
307 /*
308 * Cleanup kernel stream structure.
309 */
310 void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream)
311 {
312 int ret;
313
314 DBG("[trace] Closing stream fd %d", stream->fd);
315 /* Close kernel fd */
316 if (stream->fd >= 0) {
317 ret = close(stream->fd);
318 if (ret) {
319 PERROR("close");
320 }
321 }
322 /* Remove from stream list */
323 cds_list_del(&stream->list);
324
325 free(stream->pathname);
326 free(stream);
327 }
328
329 /*
330 * Cleanup kernel event structure.
331 */
332 void trace_kernel_destroy_event(struct ltt_kernel_event *event)
333 {
334 int ret;
335
336 if (event->fd >= 0) {
337 DBG("[trace] Closing event fd %d", event->fd);
338 /* Close kernel fd */
339 ret = close(event->fd);
340 if (ret) {
341 PERROR("close");
342 }
343 } else {
344 DBG("[trace] Tearing down event (no associated fd)");
345 }
346
347 /* Remove from event list */
348 cds_list_del(&event->list);
349
350 free(event->event);
351 free(event->ctx);
352 free(event);
353 }
354
355 /*
356 * Cleanup kernel channel structure.
357 */
358 void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel)
359 {
360 struct ltt_kernel_stream *stream, *stmp;
361 struct ltt_kernel_event *event, *etmp;
362 int ret;
363
364 DBG("[trace] Closing channel fd %d", channel->fd);
365 /* Close kernel fd */
366 if (channel->fd >= 0) {
367 ret = close(channel->fd);
368 if (ret) {
369 PERROR("close");
370 }
371 }
372
373 /* For each stream in the channel list */
374 cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) {
375 trace_kernel_destroy_stream(stream);
376 }
377
378 /* For each event in the channel list */
379 cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) {
380 trace_kernel_destroy_event(event);
381 }
382
383 /* Remove from channel list */
384 cds_list_del(&channel->list);
385
386 free(channel->pathname);
387 free(channel->channel);
388 free(channel->ctx);
389 free(channel);
390 }
391
392 /*
393 * Cleanup kernel metadata structure.
394 */
395 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata)
396 {
397 int ret;
398
399 DBG("[trace] Closing metadata fd %d", metadata->fd);
400 /* Close kernel fd */
401 if (metadata->fd >= 0) {
402 ret = close(metadata->fd);
403 if (ret) {
404 PERROR("close");
405 }
406 }
407
408 free(metadata->conf);
409 free(metadata->pathname);
410 free(metadata);
411 }
412
413 /*
414 * Cleanup kernel session structure
415 */
416 void trace_kernel_destroy_session(struct ltt_kernel_session *session)
417 {
418 struct ltt_kernel_channel *channel, *ctmp;
419 int ret;
420
421 DBG("[trace] Closing session fd %d", session->fd);
422 /* Close kernel fds */
423 if (session->fd >= 0) {
424 ret = close(session->fd);
425 if (ret) {
426 PERROR("close");
427 }
428 }
429
430 if (session->metadata_stream_fd >= 0) {
431 DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd);
432 ret = close(session->metadata_stream_fd);
433 if (ret) {
434 PERROR("close");
435 }
436 }
437
438 if (session->metadata != NULL) {
439 trace_kernel_destroy_metadata(session->metadata);
440 }
441
442 cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) {
443 trace_kernel_destroy_channel(channel);
444 }
445
446 free(session->trace_path);
447 free(session);
448 }
This page took 0.039112 seconds and 4 git commands to generate.