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