Fix: test all close return values in sessiond
[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 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
82a3637f
DG
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
54012638
DG
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 24
990570ed
DG
25#include <common/common.h>
26#include <common/defaults.h>
1e307fab 27
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;
54012638
DG
89 struct ltt_kernel_session *lks;
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");
54012638
DG
95 goto error;
96 }
97
98 /* Init data structure */
99 lks->fd = 0;
100 lks->metadata_stream_fd = 0;
101 lks->channel_count = 0;
102 lks->stream_count_global = 0;
103 lks->metadata = NULL;
d9800920 104 lks->consumer_fd = 0;
54012638
DG
105 CDS_INIT_LIST_HEAD(&lks->channel_list.head);
106
f9815039
DG
107 /* Set session path */
108 ret = asprintf(&lks->trace_path, "%s/kernel", path);
109 if (ret < 0) {
df0f840b 110 PERROR("asprintf kernel traces path");
f9815039
DG
111 goto error;
112 }
113
54012638
DG
114 return lks;
115
116error:
117 return NULL;
118}
119
120/*
050349bb 121 * Allocate and initialize a kernel channel data structure.
54012638 122 *
050349bb 123 * Return pointer to structure or NULL.
54012638 124 */
62499ad6 125struct ltt_kernel_channel *trace_kernel_create_channel(struct lttng_channel *chan, char *path)
54012638
DG
126{
127 int ret;
128 struct ltt_kernel_channel *lkc;
54012638 129
ba7f0ae5 130 lkc = zmalloc(sizeof(struct ltt_kernel_channel));
f3ed775e 131 if (lkc == NULL) {
df0f840b 132 PERROR("ltt_kernel_channel zmalloc");
54012638
DG
133 goto error;
134 }
135
ba7f0ae5 136 lkc->channel = zmalloc(sizeof(struct lttng_channel));
f3ed775e 137 if (lkc->channel == NULL) {
df0f840b 138 PERROR("lttng_channel zmalloc");
f3ed775e
DG
139 goto error;
140 }
141 memcpy(lkc->channel, chan, sizeof(struct lttng_channel));
54012638
DG
142
143 lkc->fd = 0;
144 lkc->stream_count = 0;
cbbbb275 145 lkc->event_count = 0;
d36b8583 146 lkc->enabled = 1;
e75cda3a 147 lkc->ctx = NULL;
54012638
DG
148 /* Init linked list */
149 CDS_INIT_LIST_HEAD(&lkc->events_list.head);
150 CDS_INIT_LIST_HEAD(&lkc->stream_list.head);
151 /* Set default trace output path */
b082db07 152 ret = asprintf(&lkc->pathname, "%s", path);
54012638 153 if (ret < 0) {
df0f840b 154 PERROR("asprintf kernel create channel");
54012638
DG
155 goto error;
156 }
157
158 return lkc;
159
160error:
161 return NULL;
162}
163
164/*
050349bb 165 * Allocate and initialize a kernel event. Set name and event type.
54012638 166 *
050349bb 167 * Return pointer to structure or NULL.
54012638 168 */
62499ad6 169struct ltt_kernel_event *trace_kernel_create_event(struct lttng_event *ev)
54012638
DG
170{
171 struct ltt_kernel_event *lke;
172 struct lttng_kernel_event *attr;
173
ba7f0ae5
DG
174 lke = zmalloc(sizeof(struct ltt_kernel_event));
175 attr = zmalloc(sizeof(struct lttng_kernel_event));
54012638 176 if (lke == NULL || attr == NULL) {
df0f840b 177 PERROR("kernel event zmalloc");
54012638
DG
178 goto error;
179 }
180
f3ed775e 181 switch (ev->type) {
7d29a247 182 case LTTNG_EVENT_PROBE:
e6ddca71 183 attr->instrumentation = LTTNG_KERNEL_KPROBE;
7d29a247
DG
184 attr->u.kprobe.addr = ev->attr.probe.addr;
185 attr->u.kprobe.offset = ev->attr.probe.offset;
f3ed775e 186 strncpy(attr->u.kprobe.symbol_name,
dbbb3ec5
DG
187 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
188 attr->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
f3ed775e
DG
189 break;
190 case LTTNG_EVENT_FUNCTION:
8f0d098b
MD
191 attr->instrumentation = LTTNG_KERNEL_KRETPROBE;
192 attr->u.kretprobe.addr = ev->attr.probe.addr;
193 attr->u.kretprobe.offset = ev->attr.probe.offset;
194 attr->u.kretprobe.offset = ev->attr.probe.offset;
195 strncpy(attr->u.kretprobe.symbol_name,
dbbb3ec5
DG
196 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
197 attr->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
8f0d098b
MD
198 break;
199 case LTTNG_EVENT_FUNCTION_ENTRY:
f3ed775e
DG
200 attr->instrumentation = LTTNG_KERNEL_FUNCTION;
201 strncpy(attr->u.ftrace.symbol_name,
dbbb3ec5
DG
202 ev->attr.ftrace.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
203 attr->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
f3ed775e 204 break;
e6ddca71
DG
205 case LTTNG_EVENT_TRACEPOINT:
206 attr->instrumentation = LTTNG_KERNEL_TRACEPOINT;
f3ed775e 207 break;
a54bd42d
MD
208 case LTTNG_EVENT_SYSCALL:
209 attr->instrumentation = LTTNG_KERNEL_SYSCALL;
0133c199 210 break;
7a3d1328
MD
211 case LTTNG_EVENT_ALL:
212 attr->instrumentation = LTTNG_KERNEL_ALL;
213 break;
f3ed775e
DG
214 default:
215 ERR("Unknown kernel instrumentation type (%d)", ev->type);
216 goto error;
217 }
218
219 /* Copy event name */
dbbb3ec5
DG
220 strncpy(attr->name, ev->name, LTTNG_KERNEL_SYM_NAME_LEN);
221 attr->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
f3ed775e 222
54012638
DG
223 /* Setting up a kernel event */
224 lke->fd = 0;
225 lke->event = attr;
d36b8583 226 lke->enabled = 1;
e75cda3a 227 lke->ctx = NULL;
54012638
DG
228
229 return lke;
230
231error:
232 return NULL;
233}
234
235/*
050349bb 236 * Allocate and initialize a kernel metadata.
54012638 237 *
050349bb 238 * Return pointer to structure or NULL.
54012638 239 */
62499ad6 240struct ltt_kernel_metadata *trace_kernel_create_metadata(char *path)
54012638
DG
241{
242 int ret;
243 struct ltt_kernel_metadata *lkm;
f3ed775e 244 struct lttng_channel *chan;
54012638 245
ba7f0ae5
DG
246 lkm = zmalloc(sizeof(struct ltt_kernel_metadata));
247 chan = zmalloc(sizeof(struct lttng_channel));
f3ed775e 248 if (lkm == NULL || chan == NULL) {
df0f840b 249 PERROR("kernel metadata zmalloc");
54012638
DG
250 goto error;
251 }
252
253 /* Set default attributes */
f3ed775e 254 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
b389abbe
MD
255 chan->attr.subbuf_size = DEFAULT_METADATA_SUBBUF_SIZE;
256 chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
f3ed775e
DG
257 chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
258 chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
7d452e12 259 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
54012638
DG
260
261 /* Init metadata */
262 lkm->fd = 0;
f3ed775e 263 lkm->conf = chan;
54012638 264 /* Set default metadata path */
58a97671 265 ret = asprintf(&lkm->pathname, "%s/metadata", path);
54012638 266 if (ret < 0) {
df0f840b 267 PERROR("asprintf kernel metadata");
54012638
DG
268 goto error;
269 }
270
271 return lkm;
272
273error:
274 return NULL;
275}
276
277/*
050349bb
DG
278 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
279 * default.
54012638 280 *
050349bb 281 * Return pointer to structure or NULL.
54012638 282 */
62499ad6 283struct ltt_kernel_stream *trace_kernel_create_stream(void)
54012638
DG
284{
285 struct ltt_kernel_stream *lks;
286
ba7f0ae5 287 lks = zmalloc(sizeof(struct ltt_kernel_stream));
54012638 288 if (lks == NULL) {
df0f840b 289 PERROR("kernel stream zmalloc");
54012638
DG
290 goto error;
291 }
292
293 /* Init stream */
294 lks->fd = 0;
295 lks->pathname = NULL;
296 lks->state = 0;
297
298 return lks;
299
300error:
301 return NULL;
302}
c363b55d 303
050349bb
DG
304/*
305 * Cleanup kernel stream structure.
306 */
62499ad6 307void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream)
c363b55d 308{
799e2c4f
MD
309 int ret;
310
33a2b854 311 DBG("[trace] Closing stream fd %d", stream->fd);
c363b55d 312 /* Close kernel fd */
799e2c4f
MD
313 ret = close(stream->fd);
314 if (ret) {
315 PERROR("close");
316 }
c363b55d
DG
317 /* Remove from stream list */
318 cds_list_del(&stream->list);
f9815039
DG
319
320 free(stream->pathname);
c363b55d
DG
321 free(stream);
322}
323
050349bb
DG
324/*
325 * Cleanup kernel event structure.
326 */
62499ad6 327void trace_kernel_destroy_event(struct ltt_kernel_event *event)
c363b55d 328{
799e2c4f
MD
329 int ret;
330
87eb4ab8
MD
331 if (event->fd >= 0) {
332 DBG("[trace] Closing event fd %d", event->fd);
333 /* Close kernel fd */
799e2c4f
MD
334 ret = close(event->fd);
335 if (ret) {
336 PERROR("close");
337 }
87eb4ab8
MD
338 } else {
339 DBG("[trace] Tearing down event (no associated fd)");
340 }
c363b55d
DG
341
342 /* Remove from event list */
343 cds_list_del(&event->list);
f9815039
DG
344
345 free(event->event);
346 free(event->ctx);
c363b55d
DG
347 free(event);
348}
349
050349bb
DG
350/*
351 * Cleanup kernel channel structure.
352 */
62499ad6 353void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel)
c363b55d 354{
af9737e9
DG
355 struct ltt_kernel_stream *stream, *stmp;
356 struct ltt_kernel_event *event, *etmp;
799e2c4f 357 int ret;
c363b55d 358
33a2b854 359 DBG("[trace] Closing channel fd %d", channel->fd);
c363b55d 360 /* Close kernel fd */
799e2c4f
MD
361 ret = close(channel->fd);
362 if (ret) {
363 PERROR("close");
364 }
c363b55d
DG
365
366 /* For each stream in the channel list */
af9737e9 367 cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) {
62499ad6 368 trace_kernel_destroy_stream(stream);
c363b55d
DG
369 }
370
371 /* For each event in the channel list */
af9737e9 372 cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) {
62499ad6 373 trace_kernel_destroy_event(event);
c363b55d
DG
374 }
375
376 /* Remove from channel list */
377 cds_list_del(&channel->list);
f9815039
DG
378
379 free(channel->pathname);
380 free(channel->channel);
381 free(channel->ctx);
c363b55d
DG
382 free(channel);
383}
384
050349bb
DG
385/*
386 * Cleanup kernel metadata structure.
387 */
62499ad6 388void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata)
c363b55d 389{
799e2c4f
MD
390 int ret;
391
33a2b854 392 DBG("[trace] Closing metadata fd %d", metadata->fd);
c363b55d 393 /* Close kernel fd */
799e2c4f
MD
394 ret = close(metadata->fd);
395 if (ret) {
396 PERROR("close");
397 }
c363b55d 398
f9815039
DG
399 free(metadata->conf);
400 free(metadata->pathname);
c363b55d
DG
401 free(metadata);
402}
403
050349bb 404/*
62499ad6 405 * Cleanup kernel session structure
050349bb 406 */
62499ad6 407void trace_kernel_destroy_session(struct ltt_kernel_session *session)
c363b55d 408{
af9737e9 409 struct ltt_kernel_channel *channel, *ctmp;
799e2c4f 410 int ret;
c363b55d 411
33a2b854 412 DBG("[trace] Closing session fd %d", session->fd);
c363b55d 413 /* Close kernel fds */
799e2c4f
MD
414 ret = close(session->fd);
415 if (ret) {
416 PERROR("close");
417 }
f9815039 418
70dc1c34
DG
419 if (session->metadata_stream_fd != 0) {
420 DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd);
799e2c4f
MD
421 ret = close(session->metadata_stream_fd);
422 if (ret) {
423 PERROR("close");
424 }
70dc1c34 425 }
c363b55d 426
d36b8583 427 if (session->metadata != NULL) {
62499ad6 428 trace_kernel_destroy_metadata(session->metadata);
d36b8583 429 }
c363b55d 430
af9737e9 431 cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) {
62499ad6 432 trace_kernel_destroy_channel(channel);
c363b55d
DG
433 }
434
f9815039 435 free(session->trace_path);
c363b55d
DG
436 free(session);
437}
This page took 0.053504 seconds and 4 git commands to generate.