Cleanup comments and bad indent
[lttng-tools.git] / ltt-sessiond / trace.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
DG
24#include <urcu/list.h>
25
33a2b854 26#include "lttngerr.h"
54012638
DG
27#include "trace.h"
28
19e70852 29/*
050349bb 30 * Find the channel name for the given kernel session.
19e70852
DG
31 */
32struct ltt_kernel_channel *get_kernel_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
49error:
50 return NULL;
51}
52
53/*
050349bb 54 * Find the event name for the given channel.
19e70852
DG
55 */
56struct ltt_kernel_event *get_kernel_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
74error:
75 return NULL;
76}
77
54012638 78/*
050349bb 79 * Allocate and initialize a kernel session data structure.
54012638 80 *
050349bb 81 * Return pointer to structure or NULL.
54012638
DG
82 */
83struct ltt_kernel_session *trace_create_kernel_session(void)
84{
85 struct ltt_kernel_session *lks;
86
87 /* Allocate a new ltt kernel session */
88 lks = malloc(sizeof(struct ltt_kernel_session));
89 if (lks == NULL) {
90 perror("create kernel session malloc");
91 goto error;
92 }
93
94 /* Init data structure */
95 lks->fd = 0;
96 lks->metadata_stream_fd = 0;
97 lks->channel_count = 0;
98 lks->stream_count_global = 0;
99 lks->metadata = NULL;
d9800920 100 lks->consumer_fd = 0;
54012638
DG
101 CDS_INIT_LIST_HEAD(&lks->channel_list.head);
102
103 return lks;
104
105error:
106 return NULL;
107}
108
109/*
050349bb 110 * Allocate and initialize a kernel channel data structure.
54012638 111 *
050349bb 112 * Return pointer to structure or NULL.
54012638 113 */
b082db07 114struct ltt_kernel_channel *trace_create_kernel_channel(struct lttng_channel *chan, char *path)
54012638
DG
115{
116 int ret;
117 struct ltt_kernel_channel *lkc;
54012638
DG
118
119 lkc = malloc(sizeof(struct ltt_kernel_channel));
f3ed775e
DG
120 if (lkc == NULL) {
121 perror("ltt_kernel_channel malloc");
54012638
DG
122 goto error;
123 }
124
f3ed775e
DG
125 lkc->channel = malloc(sizeof(struct lttng_channel));
126 if (lkc->channel == NULL) {
127 perror("lttng_channel malloc");
128 goto error;
129 }
130 memcpy(lkc->channel, chan, sizeof(struct lttng_channel));
54012638
DG
131
132 lkc->fd = 0;
133 lkc->stream_count = 0;
cbbbb275 134 lkc->event_count = 0;
d36b8583 135 lkc->enabled = 1;
e75cda3a 136 lkc->ctx = NULL;
54012638
DG
137 /* Init linked list */
138 CDS_INIT_LIST_HEAD(&lkc->events_list.head);
139 CDS_INIT_LIST_HEAD(&lkc->stream_list.head);
140 /* Set default trace output path */
b082db07 141 ret = asprintf(&lkc->pathname, "%s", path);
54012638
DG
142 if (ret < 0) {
143 perror("asprintf kernel create channel");
144 goto error;
145 }
146
147 return lkc;
148
149error:
150 return NULL;
151}
152
153/*
050349bb 154 * Allocate and initialize a kernel event. Set name and event type.
54012638 155 *
050349bb 156 * Return pointer to structure or NULL.
54012638 157 */
f3ed775e 158struct ltt_kernel_event *trace_create_kernel_event(struct lttng_event *ev)
54012638
DG
159{
160 struct ltt_kernel_event *lke;
161 struct lttng_kernel_event *attr;
162
163 lke = malloc(sizeof(struct ltt_kernel_event));
164 attr = malloc(sizeof(struct lttng_kernel_event));
165 if (lke == NULL || attr == NULL) {
166 perror("kernel event malloc");
167 goto error;
168 }
169
f3ed775e 170 switch (ev->type) {
7d29a247 171 case LTTNG_EVENT_PROBE:
e6ddca71 172 attr->instrumentation = LTTNG_KERNEL_KPROBE;
7d29a247
DG
173 attr->u.kprobe.addr = ev->attr.probe.addr;
174 attr->u.kprobe.offset = ev->attr.probe.offset;
f3ed775e 175 strncpy(attr->u.kprobe.symbol_name,
7d29a247 176 ev->attr.probe.symbol_name, LTTNG_SYM_NAME_LEN);
99497cd0 177 attr->u.kprobe.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
f3ed775e
DG
178 break;
179 case LTTNG_EVENT_FUNCTION:
8f0d098b
MD
180 attr->instrumentation = LTTNG_KERNEL_KRETPROBE;
181 attr->u.kretprobe.addr = ev->attr.probe.addr;
182 attr->u.kretprobe.offset = ev->attr.probe.offset;
183 attr->u.kretprobe.offset = ev->attr.probe.offset;
184 strncpy(attr->u.kretprobe.symbol_name,
185 ev->attr.probe.symbol_name, LTTNG_SYM_NAME_LEN);
99497cd0 186 attr->u.kretprobe.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
8f0d098b
MD
187 break;
188 case LTTNG_EVENT_FUNCTION_ENTRY:
f3ed775e
DG
189 attr->instrumentation = LTTNG_KERNEL_FUNCTION;
190 strncpy(attr->u.ftrace.symbol_name,
191 ev->attr.ftrace.symbol_name, LTTNG_SYM_NAME_LEN);
99497cd0 192 attr->u.ftrace.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
f3ed775e 193 break;
e6ddca71
DG
194 case LTTNG_EVENT_TRACEPOINT:
195 attr->instrumentation = LTTNG_KERNEL_TRACEPOINT;
f3ed775e
DG
196 break;
197 default:
198 ERR("Unknown kernel instrumentation type (%d)", ev->type);
199 goto error;
200 }
201
202 /* Copy event name */
203 strncpy(attr->name, ev->name, LTTNG_SYM_NAME_LEN);
99497cd0 204 attr->name[LTTNG_SYM_NAME_LEN - 1] = '\0';
f3ed775e 205
54012638
DG
206 /* Setting up a kernel event */
207 lke->fd = 0;
208 lke->event = attr;
d36b8583 209 lke->enabled = 1;
e75cda3a 210 lke->ctx = NULL;
54012638
DG
211
212 return lke;
213
214error:
215 return NULL;
216}
217
218/*
050349bb 219 * Allocate and initialize a kernel metadata.
54012638 220 *
050349bb 221 * Return pointer to structure or NULL.
54012638 222 */
58a97671 223struct ltt_kernel_metadata *trace_create_kernel_metadata(char *path)
54012638
DG
224{
225 int ret;
226 struct ltt_kernel_metadata *lkm;
f3ed775e 227 struct lttng_channel *chan;
54012638
DG
228
229 lkm = malloc(sizeof(struct ltt_kernel_metadata));
f3ed775e
DG
230 chan = malloc(sizeof(struct lttng_channel));
231 if (lkm == NULL || chan == NULL) {
54012638
DG
232 perror("kernel metadata malloc");
233 goto error;
234 }
235
236 /* Set default attributes */
f3ed775e 237 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
b389abbe
MD
238 chan->attr.subbuf_size = DEFAULT_METADATA_SUBBUF_SIZE;
239 chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
f3ed775e
DG
240 chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
241 chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
7d452e12 242 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
54012638
DG
243
244 /* Init metadata */
245 lkm->fd = 0;
f3ed775e 246 lkm->conf = chan;
54012638 247 /* Set default metadata path */
58a97671 248 ret = asprintf(&lkm->pathname, "%s/metadata", path);
54012638
DG
249 if (ret < 0) {
250 perror("asprintf kernel metadata");
251 goto error;
252 }
253
254 return lkm;
255
256error:
257 return NULL;
258}
259
260/*
050349bb
DG
261 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
262 * default.
54012638 263 *
050349bb 264 * Return pointer to structure or NULL.
54012638
DG
265 */
266struct ltt_kernel_stream *trace_create_kernel_stream(void)
267{
268 struct ltt_kernel_stream *lks;
269
270 lks = malloc(sizeof(struct ltt_kernel_stream));
271 if (lks == NULL) {
272 perror("kernel stream malloc");
273 goto error;
274 }
275
276 /* Init stream */
277 lks->fd = 0;
278 lks->pathname = NULL;
279 lks->state = 0;
280
281 return lks;
282
283error:
284 return NULL;
285}
c363b55d 286
050349bb
DG
287/*
288 * Cleanup kernel stream structure.
289 */
c363b55d
DG
290void trace_destroy_kernel_stream(struct ltt_kernel_stream *stream)
291{
33a2b854 292 DBG("[trace] Closing stream fd %d", stream->fd);
c363b55d
DG
293 /* Close kernel fd */
294 close(stream->fd);
295 free(stream->pathname);
296
297 /* Remove from stream list */
298 cds_list_del(&stream->list);
299 free(stream);
300}
301
050349bb
DG
302/*
303 * Cleanup kernel event structure.
304 */
c363b55d
DG
305void trace_destroy_kernel_event(struct ltt_kernel_event *event)
306{
33a2b854 307 DBG("[trace] Closing event fd %d", event->fd);
c363b55d
DG
308 /* Close kernel fd */
309 close(event->fd);
310 /* Free attributes */
311 free(event->event);
312
313 /* Remove from event list */
314 cds_list_del(&event->list);
315 free(event);
316}
317
050349bb
DG
318/*
319 * Cleanup kernel channel structure.
320 */
c363b55d
DG
321void trace_destroy_kernel_channel(struct ltt_kernel_channel *channel)
322{
af9737e9
DG
323 struct ltt_kernel_stream *stream, *stmp;
324 struct ltt_kernel_event *event, *etmp;
c363b55d 325
33a2b854 326 DBG("[trace] Closing channel fd %d", channel->fd);
c363b55d
DG
327 /* Close kernel fd */
328 close(channel->fd);
329 free(channel->pathname);
330 /* Free attributes structure */
331 free(channel->channel);
332
333 /* For each stream in the channel list */
af9737e9 334 cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) {
c363b55d
DG
335 trace_destroy_kernel_stream(stream);
336 }
337
338 /* For each event in the channel list */
af9737e9 339 cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) {
c363b55d
DG
340 trace_destroy_kernel_event(event);
341 }
342
343 /* Remove from channel list */
344 cds_list_del(&channel->list);
345 free(channel);
346}
347
050349bb
DG
348/*
349 * Cleanup kernel metadata structure.
350 */
c363b55d
DG
351void trace_destroy_kernel_metadata(struct ltt_kernel_metadata *metadata)
352{
33a2b854 353 DBG("[trace] Closing metadata fd %d", metadata->fd);
c363b55d
DG
354 /* Close kernel fd */
355 close(metadata->fd);
356 /* Free attributes */
357 free(metadata->conf);
358
359 free(metadata);
360}
361
050349bb
DG
362/*
363 * Cleanup kernel session structure
364 */
c363b55d
DG
365void trace_destroy_kernel_session(struct ltt_kernel_session *session)
366{
af9737e9 367 struct ltt_kernel_channel *channel, *ctmp;
c363b55d 368
33a2b854 369 DBG("[trace] Closing session fd %d", session->fd);
c363b55d
DG
370 /* Close kernel fds */
371 close(session->fd);
70dc1c34
DG
372 if (session->metadata_stream_fd != 0) {
373 DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd);
374 close(session->metadata_stream_fd);
375 }
c363b55d 376
d36b8583
DG
377 if (session->metadata != NULL) {
378 trace_destroy_kernel_metadata(session->metadata);
379 }
c363b55d 380
af9737e9 381 cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) {
c363b55d
DG
382 trace_destroy_kernel_channel(channel);
383 }
384
385 free(session);
386}
This page took 0.039307 seconds and 4 git commands to generate.