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