Fix: memory leak in add relayd socket error path
[lttng-tools.git] / src / bin / lttng-sessiond / trace-kernel.c
CommitLineData
54012638
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
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.
54012638
DG
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 *
d14d33bf
AM
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.
54012638
DG
16 */
17
18#define _GNU_SOURCE
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
c363b55d 22#include <unistd.h>
54012638 23
990570ed
DG
24#include <common/common.h>
25#include <common/defaults.h>
1e307fab 26
00e2e675 27#include "consumer.h"
62499ad6 28#include "trace-kernel.h"
54012638 29
19e70852 30/*
050349bb 31 * Find the channel name for the given kernel session.
19e70852 32 */
62499ad6 33struct ltt_kernel_channel *trace_kernel_get_channel_by_name(
19e70852
DG
34 char *name, struct ltt_kernel_session *session)
35{
36 struct ltt_kernel_channel *chan;
37
38 if (session == NULL) {
39 ERR("Undefine session");
40 goto error;
41 }
42
54d01ffb
DG
43 DBG("Trying to find channel %s", name);
44
19e70852
DG
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/*
050349bb 57 * Find the event name for the given channel.
19e70852 58 */
62499ad6 59struct ltt_kernel_event *trace_kernel_get_event_by_name(
19e70852
DG
60 char *name, struct ltt_kernel_channel *channel)
61{
62 struct ltt_kernel_event *ev;
63
64 if (channel == NULL) {
65 ERR("Undefine channel");
66 goto error;
67 }
68
69 cds_list_for_each_entry(ev, &channel->events_list.head, list) {
70 if (strcmp(name, ev->event->name) == 0) {
71 DBG("Found event by name %s for channel %s", name,
72 channel->channel->name);
73 return ev;
74 }
75 }
76
77error:
78 return NULL;
79}
80
54012638 81/*
050349bb 82 * Allocate and initialize a kernel session data structure.
54012638 83 *
050349bb 84 * Return pointer to structure or NULL.
54012638 85 */
f9815039 86struct ltt_kernel_session *trace_kernel_create_session(char *path)
54012638 87{
f9815039 88 int ret;
a4b92340 89 struct ltt_kernel_session *lks = NULL;
54012638
DG
90
91 /* Allocate a new ltt kernel session */
ba7f0ae5 92 lks = zmalloc(sizeof(struct ltt_kernel_session));
54012638 93 if (lks == NULL) {
df0f840b 94 PERROR("create kernel session zmalloc");
a4b92340 95 goto alloc_error;
54012638
DG
96 }
97
98 /* Init data structure */
03550b58
MD
99 lks->fd = -1;
100 lks->metadata_stream_fd = -1;
54012638
DG
101 lks->channel_count = 0;
102 lks->stream_count_global = 0;
103 lks->metadata = NULL;
104 CDS_INIT_LIST_HEAD(&lks->channel_list.head);
105
00e2e675
DG
106 lks->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
107 if (lks->consumer == NULL) {
108 goto error;
109 }
110
111 /*
112 * The tmp_consumer stays NULL until a set_consumer_uri command is
113 * executed. At this point, the consumer should be nullify until an
114 * enable_consumer command. This assignment is symbolic since we've zmalloc
115 * the struct.
116 */
117 lks->tmp_consumer = NULL;
118
a4b92340
DG
119 if (path && strlen(path) > 0) {
120 /* Use the default consumer output which is the tracing session path. */
121 ret = snprintf(lks->consumer->dst.trace_path, PATH_MAX,
122 "%s" DEFAULT_KERNEL_TRACE_DIR, path);
123 if (ret < 0) {
124 PERROR("snprintf consumer trace path");
125 goto error;
126 }
00e2e675 127
a4b92340
DG
128 /* Set session path */
129 ret = asprintf(&lks->trace_path, "%s" DEFAULT_KERNEL_TRACE_DIR, path);
130 if (ret < 0) {
131 PERROR("asprintf kernel traces path");
132 goto error;
133 }
f9815039
DG
134 }
135
54012638
DG
136 return lks;
137
138error:
a4b92340
DG
139 free(lks);
140
141alloc_error:
54012638
DG
142 return NULL;
143}
144
145/*
050349bb 146 * Allocate and initialize a kernel channel data structure.
54012638 147 *
050349bb 148 * Return pointer to structure or NULL.
54012638 149 */
00e2e675
DG
150struct ltt_kernel_channel *trace_kernel_create_channel(
151 struct lttng_channel *chan, char *path)
54012638 152{
54012638 153 struct ltt_kernel_channel *lkc;
54012638 154
ba7f0ae5 155 lkc = zmalloc(sizeof(struct ltt_kernel_channel));
f3ed775e 156 if (lkc == NULL) {
df0f840b 157 PERROR("ltt_kernel_channel zmalloc");
54012638
DG
158 goto error;
159 }
160
ba7f0ae5 161 lkc->channel = zmalloc(sizeof(struct lttng_channel));
f3ed775e 162 if (lkc->channel == NULL) {
df0f840b 163 PERROR("lttng_channel zmalloc");
f3ed775e
DG
164 goto error;
165 }
166 memcpy(lkc->channel, chan, sizeof(struct lttng_channel));
54012638 167
03550b58 168 lkc->fd = -1;
54012638 169 lkc->stream_count = 0;
cbbbb275 170 lkc->event_count = 0;
d36b8583 171 lkc->enabled = 1;
e75cda3a 172 lkc->ctx = NULL;
54012638
DG
173 /* Init linked list */
174 CDS_INIT_LIST_HEAD(&lkc->events_list.head);
175 CDS_INIT_LIST_HEAD(&lkc->stream_list.head);
54012638
DG
176
177 return lkc;
178
179error:
180 return NULL;
181}
182
183/*
050349bb 184 * Allocate and initialize a kernel event. Set name and event type.
54012638 185 *
050349bb 186 * Return pointer to structure or NULL.
54012638 187 */
62499ad6 188struct ltt_kernel_event *trace_kernel_create_event(struct lttng_event *ev)
54012638
DG
189{
190 struct ltt_kernel_event *lke;
191 struct lttng_kernel_event *attr;
192
ba7f0ae5
DG
193 lke = zmalloc(sizeof(struct ltt_kernel_event));
194 attr = zmalloc(sizeof(struct lttng_kernel_event));
54012638 195 if (lke == NULL || attr == NULL) {
df0f840b 196 PERROR("kernel event zmalloc");
54012638
DG
197 goto error;
198 }
199
f3ed775e 200 switch (ev->type) {
7d29a247 201 case LTTNG_EVENT_PROBE:
e6ddca71 202 attr->instrumentation = LTTNG_KERNEL_KPROBE;
7d29a247
DG
203 attr->u.kprobe.addr = ev->attr.probe.addr;
204 attr->u.kprobe.offset = ev->attr.probe.offset;
f3ed775e 205 strncpy(attr->u.kprobe.symbol_name,
dbbb3ec5
DG
206 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
207 attr->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
f3ed775e
DG
208 break;
209 case LTTNG_EVENT_FUNCTION:
8f0d098b
MD
210 attr->instrumentation = LTTNG_KERNEL_KRETPROBE;
211 attr->u.kretprobe.addr = ev->attr.probe.addr;
212 attr->u.kretprobe.offset = ev->attr.probe.offset;
213 attr->u.kretprobe.offset = ev->attr.probe.offset;
214 strncpy(attr->u.kretprobe.symbol_name,
dbbb3ec5
DG
215 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
216 attr->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
8f0d098b
MD
217 break;
218 case LTTNG_EVENT_FUNCTION_ENTRY:
f3ed775e
DG
219 attr->instrumentation = LTTNG_KERNEL_FUNCTION;
220 strncpy(attr->u.ftrace.symbol_name,
dbbb3ec5
DG
221 ev->attr.ftrace.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
222 attr->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
f3ed775e 223 break;
e6ddca71
DG
224 case LTTNG_EVENT_TRACEPOINT:
225 attr->instrumentation = LTTNG_KERNEL_TRACEPOINT;
f3ed775e 226 break;
a54bd42d
MD
227 case LTTNG_EVENT_SYSCALL:
228 attr->instrumentation = LTTNG_KERNEL_SYSCALL;
0133c199 229 break;
7a3d1328
MD
230 case LTTNG_EVENT_ALL:
231 attr->instrumentation = LTTNG_KERNEL_ALL;
232 break;
f3ed775e
DG
233 default:
234 ERR("Unknown kernel instrumentation type (%d)", ev->type);
235 goto error;
236 }
237
238 /* Copy event name */
dbbb3ec5
DG
239 strncpy(attr->name, ev->name, LTTNG_KERNEL_SYM_NAME_LEN);
240 attr->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
f3ed775e 241
54012638 242 /* Setting up a kernel event */
03550b58 243 lke->fd = -1;
54012638 244 lke->event = attr;
d36b8583 245 lke->enabled = 1;
54012638
DG
246
247 return lke;
248
249error:
a2c0da86
MD
250 free(lke);
251 free(attr);
54012638
DG
252 return NULL;
253}
254
255/*
050349bb 256 * Allocate and initialize a kernel metadata.
54012638 257 *
050349bb 258 * Return pointer to structure or NULL.
54012638 259 */
a4b92340 260struct ltt_kernel_metadata *trace_kernel_create_metadata(void)
54012638 261{
54012638 262 struct ltt_kernel_metadata *lkm;
f3ed775e 263 struct lttng_channel *chan;
54012638 264
ba7f0ae5
DG
265 lkm = zmalloc(sizeof(struct ltt_kernel_metadata));
266 chan = zmalloc(sizeof(struct lttng_channel));
f3ed775e 267 if (lkm == NULL || chan == NULL) {
df0f840b 268 PERROR("kernel metadata zmalloc");
54012638
DG
269 goto error;
270 }
271
272 /* Set default attributes */
f3ed775e 273 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
3e230f92 274 chan->attr.subbuf_size = default_get_metadata_subbuf_size();
b389abbe 275 chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
f3ed775e
DG
276 chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
277 chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
7d452e12 278 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
54012638
DG
279
280 /* Init metadata */
03550b58 281 lkm->fd = -1;
f3ed775e 282 lkm->conf = chan;
54012638
DG
283
284 return lkm;
285
286error:
a2c0da86
MD
287 free(lkm);
288 free(chan);
54012638
DG
289 return NULL;
290}
291
292/*
050349bb
DG
293 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
294 * default.
54012638 295 *
050349bb 296 * Return pointer to structure or NULL.
54012638 297 */
00e2e675
DG
298struct ltt_kernel_stream *trace_kernel_create_stream(const char *name,
299 unsigned int count)
54012638 300{
00e2e675 301 int ret;
54012638
DG
302 struct ltt_kernel_stream *lks;
303
ba7f0ae5 304 lks = zmalloc(sizeof(struct ltt_kernel_stream));
54012638 305 if (lks == NULL) {
df0f840b 306 PERROR("kernel stream zmalloc");
54012638
DG
307 goto error;
308 }
309
00e2e675
DG
310 /* Set name */
311 ret = snprintf(lks->name, sizeof(lks->name), "%s_%d", name, count);
312 if (ret < 0) {
313 PERROR("snprintf stream name");
314 goto error;
315 }
316 lks->name[sizeof(lks->name) - 1] = '\0';
317
54012638 318 /* Init stream */
03550b58 319 lks->fd = -1;
54012638
DG
320 lks->state = 0;
321
322 return lks;
323
324error:
325 return NULL;
326}
c363b55d 327
050349bb
DG
328/*
329 * Cleanup kernel stream structure.
330 */
62499ad6 331void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream)
c363b55d 332{
799e2c4f
MD
333 int ret;
334
33a2b854 335 DBG("[trace] Closing stream fd %d", stream->fd);
c363b55d 336 /* Close kernel fd */
03550b58
MD
337 if (stream->fd >= 0) {
338 ret = close(stream->fd);
339 if (ret) {
340 PERROR("close");
341 }
799e2c4f 342 }
c363b55d
DG
343 /* Remove from stream list */
344 cds_list_del(&stream->list);
f9815039 345
c363b55d
DG
346 free(stream);
347}
348
050349bb
DG
349/*
350 * Cleanup kernel event structure.
351 */
62499ad6 352void trace_kernel_destroy_event(struct ltt_kernel_event *event)
c363b55d 353{
799e2c4f
MD
354 int ret;
355
87eb4ab8
MD
356 if (event->fd >= 0) {
357 DBG("[trace] Closing event fd %d", event->fd);
358 /* Close kernel fd */
799e2c4f
MD
359 ret = close(event->fd);
360 if (ret) {
361 PERROR("close");
362 }
87eb4ab8
MD
363 } else {
364 DBG("[trace] Tearing down event (no associated fd)");
365 }
c363b55d
DG
366
367 /* Remove from event list */
368 cds_list_del(&event->list);
f9815039
DG
369
370 free(event->event);
c363b55d
DG
371 free(event);
372}
373
050349bb
DG
374/*
375 * Cleanup kernel channel structure.
376 */
62499ad6 377void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel)
c363b55d 378{
af9737e9
DG
379 struct ltt_kernel_stream *stream, *stmp;
380 struct ltt_kernel_event *event, *etmp;
799e2c4f 381 int ret;
c363b55d 382
33a2b854 383 DBG("[trace] Closing channel fd %d", channel->fd);
c363b55d 384 /* Close kernel fd */
03550b58
MD
385 if (channel->fd >= 0) {
386 ret = close(channel->fd);
387 if (ret) {
388 PERROR("close");
389 }
799e2c4f 390 }
c363b55d
DG
391
392 /* For each stream in the channel list */
af9737e9 393 cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) {
62499ad6 394 trace_kernel_destroy_stream(stream);
c363b55d
DG
395 }
396
397 /* For each event in the channel list */
af9737e9 398 cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) {
62499ad6 399 trace_kernel_destroy_event(event);
c363b55d
DG
400 }
401
402 /* Remove from channel list */
403 cds_list_del(&channel->list);
f9815039 404
f9815039
DG
405 free(channel->channel);
406 free(channel->ctx);
c363b55d
DG
407 free(channel);
408}
409
050349bb
DG
410/*
411 * Cleanup kernel metadata structure.
412 */
62499ad6 413void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata)
c363b55d 414{
799e2c4f
MD
415 int ret;
416
33a2b854 417 DBG("[trace] Closing metadata fd %d", metadata->fd);
c363b55d 418 /* Close kernel fd */
03550b58
MD
419 if (metadata->fd >= 0) {
420 ret = close(metadata->fd);
421 if (ret) {
422 PERROR("close");
423 }
799e2c4f 424 }
c363b55d 425
f9815039 426 free(metadata->conf);
c363b55d
DG
427 free(metadata);
428}
429
050349bb 430/*
62499ad6 431 * Cleanup kernel session structure
050349bb 432 */
62499ad6 433void trace_kernel_destroy_session(struct ltt_kernel_session *session)
c363b55d 434{
af9737e9 435 struct ltt_kernel_channel *channel, *ctmp;
799e2c4f 436 int ret;
c363b55d 437
33a2b854 438 DBG("[trace] Closing session fd %d", session->fd);
c363b55d 439 /* Close kernel fds */
03550b58
MD
440 if (session->fd >= 0) {
441 ret = close(session->fd);
442 if (ret) {
443 PERROR("close");
444 }
799e2c4f 445 }
f9815039 446
03550b58 447 if (session->metadata_stream_fd >= 0) {
70dc1c34 448 DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd);
799e2c4f
MD
449 ret = close(session->metadata_stream_fd);
450 if (ret) {
451 PERROR("close");
452 }
70dc1c34 453 }
c363b55d 454
d36b8583 455 if (session->metadata != NULL) {
62499ad6 456 trace_kernel_destroy_metadata(session->metadata);
d36b8583 457 }
c363b55d 458
af9737e9 459 cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) {
62499ad6 460 trace_kernel_destroy_channel(channel);
c363b55d
DG
461 }
462
00e2e675
DG
463 /* Wipe consumer output object */
464 consumer_destroy_output(session->consumer);
465 consumer_destroy_output(session->tmp_consumer);
466
f9815039 467 free(session->trace_path);
c363b55d
DG
468 free(session);
469}
This page took 0.055666 seconds and 4 git commands to generate.