Add kernel trace destroy functions
[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; either version 2
7 * of the License, or (at your option) any later version.
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 "ltt-sessiond.h"
27 #include "trace.h"
28
29 /*
30 * trace_create_kernel_session
31 *
32 * Allocate and initialize a kernel session data structure.
33 *
34 * Return pointer to structure or NULL.
35 */
36 struct ltt_kernel_session *trace_create_kernel_session(void)
37 {
38 struct ltt_kernel_session *lks;
39
40 /* Allocate a new ltt kernel session */
41 lks = malloc(sizeof(struct ltt_kernel_session));
42 if (lks == NULL) {
43 perror("create kernel session malloc");
44 goto error;
45 }
46
47 /* Init data structure */
48 lks->fd = 0;
49 lks->metadata_stream_fd = 0;
50 lks->channel_count = 0;
51 lks->stream_count_global = 0;
52 lks->metadata = NULL;
53 CDS_INIT_LIST_HEAD(&lks->channel_list.head);
54
55 return lks;
56
57 error:
58 return NULL;
59 }
60
61 /*
62 * trace_create_kernel_channel
63 *
64 * Allocate and initialize a kernel channel data structure.
65 *
66 * Return pointer to structure or NULL.
67 */
68 struct ltt_kernel_channel *trace_create_kernel_channel(void)
69 {
70 int ret;
71 struct ltt_kernel_channel *lkc;
72 struct lttng_kernel_channel *chan;
73
74 lkc = malloc(sizeof(struct ltt_kernel_channel));
75 chan = malloc(sizeof(struct lttng_kernel_channel));
76 if (lkc == NULL || chan == NULL) {
77 perror("kernel channel malloc");
78 goto error;
79 }
80
81 /* Default value to channel */
82 chan->overwrite = DEFAULT_KERNEL_OVERWRITE;
83 chan->subbuf_size = DEFAULT_KERNEL_SUBBUF_SIZE;
84 chan->num_subbuf = DEFAULT_KERNEL_SUBBUF_NUM;
85 chan->switch_timer_interval = DEFAULT_KERNEL_SWITCH_TIMER;
86 chan->read_timer_interval = DEFAULT_KERNEL_READ_TIMER;
87
88 lkc->fd = 0;
89 lkc->stream_count = 0;
90 lkc->channel = chan;
91 /* Init linked list */
92 CDS_INIT_LIST_HEAD(&lkc->events_list.head);
93 CDS_INIT_LIST_HEAD(&lkc->stream_list.head);
94 /* Set default trace output path */
95 ret = asprintf(&lkc->pathname, "%s", DEFAULT_TRACE_OUTPUT);
96 if (ret < 0) {
97 perror("asprintf kernel create channel");
98 goto error;
99 }
100
101 return lkc;
102
103 error:
104 return NULL;
105 }
106
107 /*
108 * trace_create_kernel_event
109 *
110 * Allocate and initialize a kernel event. Set name and event type.
111 *
112 * Return pointer to structure or NULL.
113 */
114 struct ltt_kernel_event *trace_create_kernel_event(char *name,
115 enum lttng_kernel_instrumentation type)
116 {
117 struct ltt_kernel_event *lke;
118 struct lttng_kernel_event *attr;
119
120 lke = malloc(sizeof(struct ltt_kernel_event));
121 attr = malloc(sizeof(struct lttng_kernel_event));
122 if (lke == NULL || attr == NULL) {
123 perror("kernel event malloc");
124 goto error;
125 }
126
127 /* Init event attribute */
128 attr->instrumentation = type;
129 strncpy(attr->name, name, LTTNG_SYM_NAME_LEN);
130 /* Setting up a kernel event */
131 lke->fd = 0;
132 lke->event = attr;
133
134 return lke;
135
136 error:
137 return NULL;
138 }
139
140 /*
141 * trace_create_kernel_metadata
142 *
143 * Allocate and initialize a kernel metadata.
144 *
145 * Return pointer to structure or NULL.
146 */
147 struct ltt_kernel_metadata *trace_create_kernel_metadata(void)
148 {
149 int ret;
150 struct ltt_kernel_metadata *lkm;
151 struct lttng_kernel_channel *attr;
152
153 lkm = malloc(sizeof(struct ltt_kernel_metadata));
154 attr = malloc(sizeof(struct lttng_kernel_channel));
155 if (lkm == NULL || attr == NULL) {
156 perror("kernel metadata malloc");
157 goto error;
158 }
159
160 /* Set default attributes */
161 attr->overwrite = DEFAULT_KERNEL_OVERWRITE;
162 attr->subbuf_size = DEFAULT_KERNEL_SUBBUF_SIZE;
163 attr->num_subbuf = DEFAULT_KERNEL_SUBBUF_NUM;
164 attr->switch_timer_interval = DEFAULT_KERNEL_SWITCH_TIMER;
165 attr->read_timer_interval = DEFAULT_KERNEL_READ_TIMER;
166
167 /* Init metadata */
168 lkm->fd = 0;
169 lkm->conf = attr;
170 /* Set default metadata path */
171 ret = asprintf(&lkm->pathname, "%s/metadata", DEFAULT_TRACE_OUTPUT);
172 if (ret < 0) {
173 perror("asprintf kernel metadata");
174 goto error;
175 }
176
177 return lkm;
178
179 error:
180 return NULL;
181 }
182
183 /*
184 * trace_create_kernel_stream
185 *
186 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
187 * default.
188 *
189 * Return pointer to structure or NULL.
190 */
191 struct ltt_kernel_stream *trace_create_kernel_stream(void)
192 {
193 struct ltt_kernel_stream *lks;
194
195 lks = malloc(sizeof(struct ltt_kernel_stream));
196 if (lks == NULL) {
197 perror("kernel stream malloc");
198 goto error;
199 }
200
201 /* Init stream */
202 lks->fd = 0;
203 lks->pathname = NULL;
204 lks->state = 0;
205
206 return lks;
207
208 error:
209 return NULL;
210 }
211
212 void trace_destroy_kernel_stream(struct ltt_kernel_stream *stream)
213 {
214 /* Close kernel fd */
215 close(stream->fd);
216 free(stream->pathname);
217
218 /* Remove from stream list */
219 cds_list_del(&stream->list);
220 free(stream);
221 }
222
223 void trace_destroy_kernel_event(struct ltt_kernel_event *event)
224 {
225 /* Close kernel fd */
226 close(event->fd);
227 /* Free attributes */
228 free(event->event);
229
230 /* Remove from event list */
231 cds_list_del(&event->list);
232 free(event);
233 }
234
235 void trace_destroy_kernel_channel(struct ltt_kernel_channel *channel)
236 {
237 struct ltt_kernel_stream *stream;
238 struct ltt_kernel_event *event;
239
240 /* Close kernel fd */
241 close(channel->fd);
242 free(channel->pathname);
243 /* Free attributes structure */
244 free(channel->channel);
245
246 /* For each stream in the channel list */
247 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
248 trace_destroy_kernel_stream(stream);
249 }
250
251 /* For each event in the channel list */
252 cds_list_for_each_entry(event, &channel->events_list.head, list) {
253 trace_destroy_kernel_event(event);
254 }
255
256 /* Remove from channel list */
257 cds_list_del(&channel->list);
258 free(channel);
259 }
260
261 void trace_destroy_kernel_metadata(struct ltt_kernel_metadata *metadata)
262 {
263 /* Close kernel fd */
264 close(metadata->fd);
265 /* Free attributes */
266 free(metadata->conf);
267
268 free(metadata);
269 }
270
271 void trace_destroy_kernel_session(struct ltt_kernel_session *session)
272 {
273 struct ltt_kernel_channel *channel;
274
275 /* Close kernel fds */
276 close(session->fd);
277 close(session->metadata_stream_fd);
278
279 trace_destroy_kernel_metadata(session->metadata);
280
281 cds_list_for_each_entry(channel, &session->channel_list.head, list) {
282 trace_destroy_kernel_channel(channel);
283 }
284
285 free(session);
286 }
This page took 0.035925 seconds and 5 git commands to generate.