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