Cleanup comments and bad indent
[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 * Find the channel name for the given kernel session.
31 */
32 struct 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
49 error:
50 return NULL;
51 }
52
53 /*
54 * Find the event name for the given channel.
55 */
56 struct 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
74 error:
75 return NULL;
76 }
77
78 /*
79 * Allocate and initialize a kernel session data structure.
80 *
81 * Return pointer to structure or NULL.
82 */
83 struct 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;
100 lks->consumer_fd = 0;
101 CDS_INIT_LIST_HEAD(&lks->channel_list.head);
102
103 return lks;
104
105 error:
106 return NULL;
107 }
108
109 /*
110 * Allocate and initialize a kernel channel data structure.
111 *
112 * Return pointer to structure or NULL.
113 */
114 struct ltt_kernel_channel *trace_create_kernel_channel(struct lttng_channel *chan, char *path)
115 {
116 int ret;
117 struct ltt_kernel_channel *lkc;
118
119 lkc = malloc(sizeof(struct ltt_kernel_channel));
120 if (lkc == NULL) {
121 perror("ltt_kernel_channel malloc");
122 goto error;
123 }
124
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));
131
132 lkc->fd = 0;
133 lkc->stream_count = 0;
134 lkc->event_count = 0;
135 lkc->enabled = 1;
136 lkc->ctx = NULL;
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 */
141 ret = asprintf(&lkc->pathname, "%s", path);
142 if (ret < 0) {
143 perror("asprintf kernel create channel");
144 goto error;
145 }
146
147 return lkc;
148
149 error:
150 return NULL;
151 }
152
153 /*
154 * Allocate and initialize a kernel event. Set name and event type.
155 *
156 * Return pointer to structure or NULL.
157 */
158 struct ltt_kernel_event *trace_create_kernel_event(struct lttng_event *ev)
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
170 switch (ev->type) {
171 case LTTNG_EVENT_PROBE:
172 attr->instrumentation = LTTNG_KERNEL_KPROBE;
173 attr->u.kprobe.addr = ev->attr.probe.addr;
174 attr->u.kprobe.offset = ev->attr.probe.offset;
175 strncpy(attr->u.kprobe.symbol_name,
176 ev->attr.probe.symbol_name, LTTNG_SYM_NAME_LEN);
177 attr->u.kprobe.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
178 break;
179 case LTTNG_EVENT_FUNCTION:
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);
186 attr->u.kretprobe.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
187 break;
188 case LTTNG_EVENT_FUNCTION_ENTRY:
189 attr->instrumentation = LTTNG_KERNEL_FUNCTION;
190 strncpy(attr->u.ftrace.symbol_name,
191 ev->attr.ftrace.symbol_name, LTTNG_SYM_NAME_LEN);
192 attr->u.ftrace.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
193 break;
194 case LTTNG_EVENT_TRACEPOINT:
195 attr->instrumentation = LTTNG_KERNEL_TRACEPOINT;
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);
204 attr->name[LTTNG_SYM_NAME_LEN - 1] = '\0';
205
206 /* Setting up a kernel event */
207 lke->fd = 0;
208 lke->event = attr;
209 lke->enabled = 1;
210 lke->ctx = NULL;
211
212 return lke;
213
214 error:
215 return NULL;
216 }
217
218 /*
219 * Allocate and initialize a kernel metadata.
220 *
221 * Return pointer to structure or NULL.
222 */
223 struct ltt_kernel_metadata *trace_create_kernel_metadata(char *path)
224 {
225 int ret;
226 struct ltt_kernel_metadata *lkm;
227 struct lttng_channel *chan;
228
229 lkm = malloc(sizeof(struct ltt_kernel_metadata));
230 chan = malloc(sizeof(struct lttng_channel));
231 if (lkm == NULL || chan == NULL) {
232 perror("kernel metadata malloc");
233 goto error;
234 }
235
236 /* Set default attributes */
237 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
238 chan->attr.subbuf_size = DEFAULT_METADATA_SUBBUF_SIZE;
239 chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
240 chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
241 chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
242 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
243
244 /* Init metadata */
245 lkm->fd = 0;
246 lkm->conf = chan;
247 /* Set default metadata path */
248 ret = asprintf(&lkm->pathname, "%s/metadata", path);
249 if (ret < 0) {
250 perror("asprintf kernel metadata");
251 goto error;
252 }
253
254 return lkm;
255
256 error:
257 return NULL;
258 }
259
260 /*
261 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
262 * default.
263 *
264 * Return pointer to structure or NULL.
265 */
266 struct 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
283 error:
284 return NULL;
285 }
286
287 /*
288 * Cleanup kernel stream structure.
289 */
290 void trace_destroy_kernel_stream(struct ltt_kernel_stream *stream)
291 {
292 DBG("[trace] Closing stream fd %d", stream->fd);
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
302 /*
303 * Cleanup kernel event structure.
304 */
305 void trace_destroy_kernel_event(struct ltt_kernel_event *event)
306 {
307 DBG("[trace] Closing event fd %d", event->fd);
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
318 /*
319 * Cleanup kernel channel structure.
320 */
321 void trace_destroy_kernel_channel(struct ltt_kernel_channel *channel)
322 {
323 struct ltt_kernel_stream *stream, *stmp;
324 struct ltt_kernel_event *event, *etmp;
325
326 DBG("[trace] Closing channel fd %d", channel->fd);
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 */
334 cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) {
335 trace_destroy_kernel_stream(stream);
336 }
337
338 /* For each event in the channel list */
339 cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) {
340 trace_destroy_kernel_event(event);
341 }
342
343 /* Remove from channel list */
344 cds_list_del(&channel->list);
345 free(channel);
346 }
347
348 /*
349 * Cleanup kernel metadata structure.
350 */
351 void trace_destroy_kernel_metadata(struct ltt_kernel_metadata *metadata)
352 {
353 DBG("[trace] Closing metadata fd %d", metadata->fd);
354 /* Close kernel fd */
355 close(metadata->fd);
356 /* Free attributes */
357 free(metadata->conf);
358
359 free(metadata);
360 }
361
362 /*
363 * Cleanup kernel session structure
364 */
365 void trace_destroy_kernel_session(struct ltt_kernel_session *session)
366 {
367 struct ltt_kernel_channel *channel, *ctmp;
368
369 DBG("[trace] Closing session fd %d", session->fd);
370 /* Close kernel fds */
371 close(session->fd);
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 }
376
377 if (session->metadata != NULL) {
378 trace_destroy_kernel_metadata(session->metadata);
379 }
380
381 cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) {
382 trace_destroy_kernel_channel(channel);
383 }
384
385 free(session);
386 }
This page took 0.055017 seconds and 4 git commands to generate.