Commit | Line | Data |
---|---|---|
20fe2104 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 | ||
8c0faa1d | 19 | #define _GNU_SOURCE |
20fe2104 DG |
20 | #include <errno.h> |
21 | #include <stdlib.h> | |
22 | #include <stdio.h> | |
f34daff7 | 23 | #include <string.h> |
8c0faa1d | 24 | #include <unistd.h> |
20fe2104 | 25 | |
8c0faa1d | 26 | #include "lttngerr.h" |
20fe2104 DG |
27 | #include "ltt-sessiond.h" |
28 | #include "libkernelctl.h" | |
29 | #include "kernel-ctl.h" | |
20fe2104 DG |
30 | |
31 | /* | |
32 | * kernel_create_session | |
33 | * | |
54012638 DG |
34 | * Create a new kernel session, register it to the kernel tracer and add it to |
35 | * the session daemon session. | |
20fe2104 | 36 | */ |
8c0faa1d | 37 | int kernel_create_session(struct ltt_session *session, int tracer_fd) |
20fe2104 DG |
38 | { |
39 | int ret; | |
40 | struct ltt_kernel_session *lks; | |
41 | ||
54012638 DG |
42 | /* Allocate data structure */ |
43 | lks = trace_create_kernel_session(); | |
20fe2104 | 44 | if (lks == NULL) { |
54012638 | 45 | ret = -1; |
20fe2104 DG |
46 | goto error; |
47 | } | |
48 | ||
54012638 | 49 | /* Kernel tracer session creation */ |
20fe2104 DG |
50 | ret = kernctl_create_session(tracer_fd); |
51 | if (ret < 0) { | |
54012638 | 52 | perror("ioctl kernel create session"); |
20fe2104 DG |
53 | goto error; |
54 | } | |
55 | ||
20fe2104 | 56 | lks->fd = ret; |
8c0faa1d DG |
57 | session->kernel_session = lks; |
58 | session->kern_session_count++; | |
8c0faa1d DG |
59 | |
60 | DBG("Kernel session created (fd: %d)", lks->fd); | |
20fe2104 DG |
61 | |
62 | return 0; | |
63 | ||
64 | error: | |
65 | return ret; | |
66 | } | |
67 | ||
68 | /* | |
69 | * kernel_create_channel | |
70 | * | |
54012638 DG |
71 | * Create a kernel channel, register it to the kernel tracer and add it to the |
72 | * kernel session. | |
20fe2104 | 73 | */ |
8c0faa1d | 74 | int kernel_create_channel(struct ltt_kernel_session *session) |
20fe2104 DG |
75 | { |
76 | int ret; | |
77 | struct ltt_kernel_channel *lkc; | |
20fe2104 | 78 | |
54012638 DG |
79 | /* Allocate kernel channel */ |
80 | lkc = trace_create_kernel_channel(); | |
81 | if (lkc == NULL) { | |
20fe2104 DG |
82 | goto error; |
83 | } | |
84 | ||
54012638 DG |
85 | /* Kernel tracer channel creation */ |
86 | ret = kernctl_create_channel(session->fd, lkc->channel); | |
20fe2104 | 87 | if (ret < 0) { |
54012638 | 88 | perror("ioctl kernel create channel"); |
20fe2104 DG |
89 | goto error; |
90 | } | |
91 | ||
54012638 | 92 | /* Setup the channel fd */ |
20fe2104 | 93 | lkc->fd = ret; |
54012638 | 94 | /* Add channel to session */ |
8c0faa1d DG |
95 | cds_list_add(&lkc->list, &session->channel_list.head); |
96 | session->channel_count++; | |
20fe2104 | 97 | |
54012638 | 98 | DBG("Kernel channel created (fd: %d and path: %s)", lkc->fd, lkc->pathname); |
20fe2104 DG |
99 | |
100 | return 0; | |
101 | ||
102 | error: | |
54012638 | 103 | return -1; |
20fe2104 | 104 | } |
f34daff7 DG |
105 | |
106 | /* | |
107 | * kernel_enable_event | |
108 | * | |
54012638 DG |
109 | * Create a kernel event, enable it to the kernel tracer and add it to the |
110 | * channel event list of the kernel session. | |
f34daff7 | 111 | */ |
8c0faa1d | 112 | int kernel_enable_event(struct ltt_kernel_session *session, char *name) |
f34daff7 DG |
113 | { |
114 | int ret; | |
8c0faa1d | 115 | struct ltt_kernel_channel *chan; |
f34daff7 | 116 | struct ltt_kernel_event *event; |
f34daff7 | 117 | |
54012638 DG |
118 | event = trace_create_kernel_event(name, LTTNG_KERNEL_TRACEPOINTS); |
119 | if (event == NULL) { | |
f34daff7 DG |
120 | goto error; |
121 | } | |
122 | ||
8c0faa1d | 123 | cds_list_for_each_entry(chan, &session->channel_list.head, list) { |
54012638 | 124 | ret = kernctl_create_event(chan->fd, event->event); |
8c0faa1d | 125 | if (ret < 0) { |
33a2b854 | 126 | ERR("Unable to enable event %s", name); |
8c0faa1d DG |
127 | goto error; |
128 | } | |
f34daff7 | 129 | |
8c0faa1d DG |
130 | event->fd = ret; |
131 | /* Add event to event list */ | |
132 | cds_list_add(&event->list, &chan->events_list.head); | |
133 | DBG("Event %s enabled (fd: %d)", name, event->fd); | |
134 | } | |
f34daff7 DG |
135 | |
136 | return 0; | |
137 | ||
138 | error: | |
54012638 | 139 | return -1; |
f34daff7 | 140 | } |
aaf26714 DG |
141 | |
142 | /* | |
143 | * kernel_open_metadata | |
144 | * | |
54012638 DG |
145 | * Create kernel metadata, open from the kernel tracer and add it to the |
146 | * kernel session. | |
aaf26714 DG |
147 | */ |
148 | int kernel_open_metadata(struct ltt_kernel_session *session) | |
149 | { | |
150 | int ret; | |
151 | struct ltt_kernel_metadata *lkm; | |
aaf26714 | 152 | |
54012638 DG |
153 | /* Allocate kernel metadata */ |
154 | lkm = trace_create_kernel_metadata(); | |
155 | if (lkm == NULL) { | |
aaf26714 DG |
156 | goto error; |
157 | } | |
158 | ||
54012638 DG |
159 | /* Kernel tracer metadata creation */ |
160 | ret = kernctl_open_metadata(session->fd, lkm->conf); | |
aaf26714 DG |
161 | if (ret < 0) { |
162 | goto error; | |
163 | } | |
164 | ||
8c0faa1d | 165 | lkm->fd = ret; |
aaf26714 | 166 | session->metadata = lkm; |
8c0faa1d | 167 | |
54012638 | 168 | DBG("Kernel metadata opened (fd: %d and path: %s)", lkm->fd, lkm->pathname); |
8c0faa1d DG |
169 | |
170 | return 0; | |
171 | ||
172 | error: | |
54012638 | 173 | return -1; |
8c0faa1d DG |
174 | } |
175 | ||
176 | /* | |
177 | * kernel_start_session | |
178 | * | |
179 | * Start tracing session. | |
180 | */ | |
181 | int kernel_start_session(struct ltt_kernel_session *session) | |
182 | { | |
183 | int ret; | |
184 | ||
185 | ret = kernctl_start_session(session->fd); | |
186 | if (ret < 0) { | |
187 | goto error; | |
188 | } | |
189 | ||
190 | DBG("Kernel session started"); | |
191 | ||
192 | return 0; | |
193 | ||
194 | error: | |
195 | return ret; | |
196 | } | |
197 | ||
198 | /* | |
199 | * kernel_stop_session | |
200 | * | |
201 | * Stop tracing session. | |
202 | */ | |
203 | int kernel_stop_session(struct ltt_kernel_session *session) | |
204 | { | |
205 | int ret; | |
206 | ||
207 | ret = kernctl_stop_session(session->fd); | |
208 | if (ret < 0) { | |
209 | goto error; | |
210 | } | |
211 | ||
212 | DBG("Kernel session stopped"); | |
213 | ||
214 | return 0; | |
215 | ||
216 | error: | |
217 | return ret; | |
218 | } | |
219 | ||
220 | /* | |
221 | * kernel_create_channel_stream | |
222 | * | |
54012638 DG |
223 | * Create a stream for a channel, register it to the kernel tracer and add it |
224 | * to the stream list of the channel. | |
8c0faa1d DG |
225 | * |
226 | * Return the number of created stream. Else, a negative value. | |
227 | */ | |
228 | int kernel_create_channel_stream(struct ltt_kernel_channel *channel) | |
229 | { | |
230 | int ret; | |
231 | struct ltt_kernel_stream *lks; | |
232 | ||
233 | while ((ret = kernctl_create_stream(channel->fd)) > 0) { | |
54012638 | 234 | lks = trace_create_kernel_stream(); |
8c0faa1d | 235 | if (lks == NULL) { |
54012638 | 236 | close(ret); |
8c0faa1d DG |
237 | goto error; |
238 | } | |
239 | ||
240 | lks->fd = ret; | |
241 | ret = asprintf(&lks->pathname, "%s/trace_%d", | |
242 | channel->pathname, channel->stream_count); | |
243 | if (ret < 0) { | |
244 | perror("asprintf kernel create stream"); | |
245 | goto error; | |
246 | } | |
8c0faa1d | 247 | |
54012638 | 248 | /* Add stream to channe stream list */ |
8c0faa1d DG |
249 | cds_list_add(&lks->list, &channel->stream_list.head); |
250 | channel->stream_count++; | |
8c0faa1d | 251 | |
54012638 DG |
252 | DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)", |
253 | channel->stream_count, lks->fd, lks->state, lks->pathname); | |
254 | } | |
8c0faa1d DG |
255 | |
256 | return channel->stream_count; | |
257 | ||
258 | error: | |
54012638 | 259 | return -1; |
8c0faa1d DG |
260 | } |
261 | ||
262 | /* | |
263 | * kernel_create_metadata_stream | |
264 | * | |
54012638 | 265 | * Create the metadata stream and set it to the kernel session. |
8c0faa1d DG |
266 | */ |
267 | int kernel_create_metadata_stream(struct ltt_kernel_session *session) | |
268 | { | |
269 | int ret; | |
270 | ||
271 | ret = kernctl_create_stream(session->metadata->fd); | |
272 | if (ret < 0) { | |
273 | perror("kernel create metadata stream"); | |
8c0faa1d DG |
274 | goto error; |
275 | } | |
276 | ||
277 | DBG("Kernel metadata stream created (fd: %d)", ret); | |
278 | session->metadata_stream_fd = ret; | |
aaf26714 DG |
279 | |
280 | return 0; | |
281 | ||
282 | error: | |
54012638 | 283 | return -1; |
aaf26714 | 284 | } |
2ef84c95 DG |
285 | |
286 | /* | |
287 | * kernel_list_events | |
288 | * | |
289 | * Get the event list from the kernel tracer and return that list in the CTF | |
290 | * format. | |
291 | */ | |
292 | ssize_t kernel_list_events(int tracer_fd, char **list) | |
293 | { | |
294 | int fd; | |
295 | char *buf, *line = NULL; | |
296 | size_t nb, nbmem, total = 0; | |
297 | ssize_t size; | |
298 | FILE *fp; | |
299 | ||
300 | fd = kernctl_tracepoint_list(tracer_fd); | |
301 | if (fd < 0) { | |
302 | perror("kernel tracepoint list"); | |
303 | goto error; | |
304 | } | |
305 | ||
306 | fp = fdopen(fd, "r"); | |
307 | if (fp == NULL) { | |
308 | perror("kernel tracepoint list fdopen"); | |
309 | goto error; | |
310 | } | |
311 | ||
312 | /* | |
313 | * Init memory size counter | |
314 | * See kernel-ctl.h for explanation of this value | |
315 | */ | |
316 | nbmem = KERNEL_EVENT_LIST_SIZE; | |
317 | buf = malloc(nbmem); | |
318 | ||
319 | while ((size = getline(&line, &nb, fp)) != -1) { | |
320 | if (total + size > nbmem) { | |
321 | DBG("Reallocating event list from %ld to %ld bytes", nbmem, | |
322 | total + size + KERNEL_EVENT_LIST_SIZE); | |
323 | /* Adding the default size again */ | |
324 | nbmem = total + size + KERNEL_EVENT_LIST_SIZE; | |
325 | buf = realloc(buf, nbmem); | |
326 | if (buf == NULL) { | |
327 | perror("realloc list events"); | |
328 | goto error; | |
329 | } | |
330 | } | |
331 | memcpy(buf + total, line, size); | |
332 | total += size; | |
333 | } | |
334 | ||
335 | *list = buf; | |
336 | ||
337 | DBG("Kernel list events done"); | |
338 | ||
339 | return total; | |
340 | ||
341 | error: | |
342 | return -1; | |
343 | } | |
344 |