Permit custom consumer registration to a session
[lttng-tools.git] / ltt-sessiond / trace.c
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
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
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>
23 #include <unistd.h>
24 #include <urcu/list.h>
25
26 #include "lttngerr.h"
27 #include "trace.h"
28
29 /*
30 * get_kernel_channel_by_name
31 *
32 * Find the channel name for the given kernel session.
33 */
34 struct 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
51 error:
52 return NULL;
53 }
54
55 /*
56 * get_kernel_event_by_name
57 *
58 * Find the event name for the given channel.
59 */
60 struct 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
78 error:
79 return NULL;
80 }
81
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 */
89 struct 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 lks->consumer_fd = 0;
107 CDS_INIT_LIST_HEAD(&lks->channel_list.head);
108
109 return lks;
110
111 error:
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 */
122 struct ltt_kernel_channel *trace_create_kernel_channel(struct lttng_channel *chan, char *path)
123 {
124 int ret;
125 struct ltt_kernel_channel *lkc;
126
127 lkc = malloc(sizeof(struct ltt_kernel_channel));
128 if (lkc == NULL) {
129 perror("ltt_kernel_channel malloc");
130 goto error;
131 }
132
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));
139
140 lkc->fd = 0;
141 lkc->stream_count = 0;
142 lkc->event_count = 0;
143 lkc->enabled = 1;
144 lkc->ctx = NULL;
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 */
149 ret = asprintf(&lkc->pathname, "%s", path);
150 if (ret < 0) {
151 perror("asprintf kernel create channel");
152 goto error;
153 }
154
155 return lkc;
156
157 error:
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 */
168 struct ltt_kernel_event *trace_create_kernel_event(struct lttng_event *ev)
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
180 switch (ev->type) {
181 case LTTNG_EVENT_PROBE:
182 attr->instrumentation = LTTNG_KERNEL_KPROBE;
183 attr->u.kprobe.addr = ev->attr.probe.addr;
184 attr->u.kprobe.offset = ev->attr.probe.offset;
185 strncpy(attr->u.kprobe.symbol_name,
186 ev->attr.probe.symbol_name, LTTNG_SYM_NAME_LEN);
187 attr->u.kprobe.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
188 break;
189 case LTTNG_EVENT_FUNCTION:
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);
196 attr->u.kretprobe.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
197 break;
198 case LTTNG_EVENT_FUNCTION_ENTRY:
199 attr->instrumentation = LTTNG_KERNEL_FUNCTION;
200 strncpy(attr->u.ftrace.symbol_name,
201 ev->attr.ftrace.symbol_name, LTTNG_SYM_NAME_LEN);
202 attr->u.ftrace.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
203 break;
204 case LTTNG_EVENT_TRACEPOINT:
205 attr->instrumentation = LTTNG_KERNEL_TRACEPOINT;
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);
214 attr->name[LTTNG_SYM_NAME_LEN - 1] = '\0';
215
216 /* Setting up a kernel event */
217 lke->fd = 0;
218 lke->event = attr;
219 lke->enabled = 1;
220 lke->ctx = NULL;
221
222 return lke;
223
224 error:
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 */
235 struct ltt_kernel_metadata *trace_create_kernel_metadata(char *path)
236 {
237 int ret;
238 struct ltt_kernel_metadata *lkm;
239 struct lttng_channel *chan;
240
241 lkm = malloc(sizeof(struct ltt_kernel_metadata));
242 chan = malloc(sizeof(struct lttng_channel));
243 if (lkm == NULL || chan == NULL) {
244 perror("kernel metadata malloc");
245 goto error;
246 }
247
248 /* Set default attributes */
249 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
250 chan->attr.subbuf_size = DEFAULT_CHANNEL_SUBBUF_SIZE;
251 chan->attr.num_subbuf = DEFAULT_CHANNEL_SUBBUF_NUM;
252 chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
253 chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
254 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
255
256 /* Init metadata */
257 lkm->fd = 0;
258 lkm->conf = chan;
259 /* Set default metadata path */
260 ret = asprintf(&lkm->pathname, "%s/metadata", path);
261 if (ret < 0) {
262 perror("asprintf kernel metadata");
263 goto error;
264 }
265
266 return lkm;
267
268 error:
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 */
280 struct 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
297 error:
298 return NULL;
299 }
300
301 void trace_destroy_kernel_stream(struct ltt_kernel_stream *stream)
302 {
303 DBG("[trace] Closing stream fd %d", stream->fd);
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
313 void trace_destroy_kernel_event(struct ltt_kernel_event *event)
314 {
315 DBG("[trace] Closing event fd %d", event->fd);
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
326 void trace_destroy_kernel_channel(struct ltt_kernel_channel *channel)
327 {
328 struct ltt_kernel_stream *stream, *stmp;
329 struct ltt_kernel_event *event, *etmp;
330
331 DBG("[trace] Closing channel fd %d", channel->fd);
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 */
339 cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) {
340 trace_destroy_kernel_stream(stream);
341 }
342
343 /* For each event in the channel list */
344 cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) {
345 trace_destroy_kernel_event(event);
346 }
347
348 /* Remove from channel list */
349 cds_list_del(&channel->list);
350 free(channel);
351 }
352
353 void trace_destroy_kernel_metadata(struct ltt_kernel_metadata *metadata)
354 {
355 DBG("[trace] Closing metadata fd %d", metadata->fd);
356 /* Close kernel fd */
357 close(metadata->fd);
358 /* Free attributes */
359 free(metadata->conf);
360
361 free(metadata);
362 }
363
364 void trace_destroy_kernel_session(struct ltt_kernel_session *session)
365 {
366 struct ltt_kernel_channel *channel, *ctmp;
367
368 DBG("[trace] Closing session fd %d", session->fd);
369 /* Close kernel fds */
370 close(session->fd);
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 }
375
376 if (session->metadata != NULL) {
377 trace_destroy_kernel_metadata(session->metadata);
378 }
379
380 cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) {
381 trace_destroy_kernel_channel(channel);
382 }
383
384 free(session);
385 }
This page took 0.037175 seconds and 5 git commands to generate.