Split and remove lttng-share header file
[lttng-tools.git] / src / bin / lttng-sessiond / trace-kernel.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
25 #include <common/lttngerr.h>
26 #include <common/common.h>
27 #include <common/defaults.h>
28
29 #include "trace-kernel.h"
30
31 /*
32 * Find the channel name for the given kernel session.
33 */
34 struct ltt_kernel_channel *trace_kernel_get_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 DBG("Trying to find channel %s", name);
45
46 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
47 if (strcmp(name, chan->channel->name) == 0) {
48 DBG("Found channel by name %s", name);
49 return chan;
50 }
51 }
52
53 error:
54 return NULL;
55 }
56
57 /*
58 * Find the event name for the given channel.
59 */
60 struct ltt_kernel_event *trace_kernel_get_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 * Allocate and initialize a kernel session data structure.
84 *
85 * Return pointer to structure or NULL.
86 */
87 struct ltt_kernel_session *trace_kernel_create_session(char *path)
88 {
89 int ret;
90 struct ltt_kernel_session *lks;
91
92 /* Allocate a new ltt kernel session */
93 lks = zmalloc(sizeof(struct ltt_kernel_session));
94 if (lks == NULL) {
95 perror("create kernel session zmalloc");
96 goto error;
97 }
98
99 /* Init data structure */
100 lks->fd = 0;
101 lks->metadata_stream_fd = 0;
102 lks->channel_count = 0;
103 lks->stream_count_global = 0;
104 lks->metadata = NULL;
105 lks->consumer_fd = 0;
106 CDS_INIT_LIST_HEAD(&lks->channel_list.head);
107
108 /* Set session path */
109 ret = asprintf(&lks->trace_path, "%s/kernel", path);
110 if (ret < 0) {
111 perror("asprintf kernel traces path");
112 goto error;
113 }
114
115 return lks;
116
117 error:
118 return NULL;
119 }
120
121 /*
122 * Allocate and initialize a kernel channel data structure.
123 *
124 * Return pointer to structure or NULL.
125 */
126 struct ltt_kernel_channel *trace_kernel_create_channel(struct lttng_channel *chan, char *path)
127 {
128 int ret;
129 struct ltt_kernel_channel *lkc;
130
131 lkc = zmalloc(sizeof(struct ltt_kernel_channel));
132 if (lkc == NULL) {
133 perror("ltt_kernel_channel zmalloc");
134 goto error;
135 }
136
137 lkc->channel = zmalloc(sizeof(struct lttng_channel));
138 if (lkc->channel == NULL) {
139 perror("lttng_channel zmalloc");
140 goto error;
141 }
142 memcpy(lkc->channel, chan, sizeof(struct lttng_channel));
143
144 lkc->fd = 0;
145 lkc->stream_count = 0;
146 lkc->event_count = 0;
147 lkc->enabled = 1;
148 lkc->ctx = NULL;
149 /* Init linked list */
150 CDS_INIT_LIST_HEAD(&lkc->events_list.head);
151 CDS_INIT_LIST_HEAD(&lkc->stream_list.head);
152 /* Set default trace output path */
153 ret = asprintf(&lkc->pathname, "%s", path);
154 if (ret < 0) {
155 perror("asprintf kernel create channel");
156 goto error;
157 }
158
159 return lkc;
160
161 error:
162 return NULL;
163 }
164
165 /*
166 * Allocate and initialize a kernel event. Set name and event type.
167 *
168 * Return pointer to structure or NULL.
169 */
170 struct ltt_kernel_event *trace_kernel_create_event(struct lttng_event *ev)
171 {
172 struct ltt_kernel_event *lke;
173 struct lttng_kernel_event *attr;
174
175 lke = zmalloc(sizeof(struct ltt_kernel_event));
176 attr = zmalloc(sizeof(struct lttng_kernel_event));
177 if (lke == NULL || attr == NULL) {
178 perror("kernel event zmalloc");
179 goto error;
180 }
181
182 switch (ev->type) {
183 case LTTNG_EVENT_PROBE:
184 attr->instrumentation = LTTNG_KERNEL_KPROBE;
185 attr->u.kprobe.addr = ev->attr.probe.addr;
186 attr->u.kprobe.offset = ev->attr.probe.offset;
187 strncpy(attr->u.kprobe.symbol_name,
188 ev->attr.probe.symbol_name, LTTNG_SYM_NAME_LEN);
189 attr->u.kprobe.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
190 break;
191 case LTTNG_EVENT_FUNCTION:
192 attr->instrumentation = LTTNG_KERNEL_KRETPROBE;
193 attr->u.kretprobe.addr = ev->attr.probe.addr;
194 attr->u.kretprobe.offset = ev->attr.probe.offset;
195 attr->u.kretprobe.offset = ev->attr.probe.offset;
196 strncpy(attr->u.kretprobe.symbol_name,
197 ev->attr.probe.symbol_name, LTTNG_SYM_NAME_LEN);
198 attr->u.kretprobe.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
199 break;
200 case LTTNG_EVENT_FUNCTION_ENTRY:
201 attr->instrumentation = LTTNG_KERNEL_FUNCTION;
202 strncpy(attr->u.ftrace.symbol_name,
203 ev->attr.ftrace.symbol_name, LTTNG_SYM_NAME_LEN);
204 attr->u.ftrace.symbol_name[LTTNG_SYM_NAME_LEN - 1] = '\0';
205 break;
206 case LTTNG_EVENT_TRACEPOINT:
207 attr->instrumentation = LTTNG_KERNEL_TRACEPOINT;
208 break;
209 case LTTNG_EVENT_SYSCALL:
210 attr->instrumentation = LTTNG_KERNEL_SYSCALL;
211 break;
212 case LTTNG_EVENT_ALL:
213 attr->instrumentation = LTTNG_KERNEL_ALL;
214 break;
215 default:
216 ERR("Unknown kernel instrumentation type (%d)", ev->type);
217 goto error;
218 }
219
220 /* Copy event name */
221 strncpy(attr->name, ev->name, LTTNG_SYM_NAME_LEN);
222 attr->name[LTTNG_SYM_NAME_LEN - 1] = '\0';
223
224 /* Setting up a kernel event */
225 lke->fd = 0;
226 lke->event = attr;
227 lke->enabled = 1;
228 lke->ctx = NULL;
229
230 return lke;
231
232 error:
233 return NULL;
234 }
235
236 /*
237 * Allocate and initialize a kernel metadata.
238 *
239 * Return pointer to structure or NULL.
240 */
241 struct ltt_kernel_metadata *trace_kernel_create_metadata(char *path)
242 {
243 int ret;
244 struct ltt_kernel_metadata *lkm;
245 struct lttng_channel *chan;
246
247 lkm = zmalloc(sizeof(struct ltt_kernel_metadata));
248 chan = zmalloc(sizeof(struct lttng_channel));
249 if (lkm == NULL || chan == NULL) {
250 perror("kernel metadata zmalloc");
251 goto error;
252 }
253
254 /* Set default attributes */
255 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
256 chan->attr.subbuf_size = DEFAULT_METADATA_SUBBUF_SIZE;
257 chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
258 chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
259 chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
260 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
261
262 /* Init metadata */
263 lkm->fd = 0;
264 lkm->conf = chan;
265 /* Set default metadata path */
266 ret = asprintf(&lkm->pathname, "%s/metadata", path);
267 if (ret < 0) {
268 perror("asprintf kernel metadata");
269 goto error;
270 }
271
272 return lkm;
273
274 error:
275 return NULL;
276 }
277
278 /*
279 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
280 * default.
281 *
282 * Return pointer to structure or NULL.
283 */
284 struct ltt_kernel_stream *trace_kernel_create_stream(void)
285 {
286 struct ltt_kernel_stream *lks;
287
288 lks = zmalloc(sizeof(struct ltt_kernel_stream));
289 if (lks == NULL) {
290 perror("kernel stream zmalloc");
291 goto error;
292 }
293
294 /* Init stream */
295 lks->fd = 0;
296 lks->pathname = NULL;
297 lks->state = 0;
298
299 return lks;
300
301 error:
302 return NULL;
303 }
304
305 /*
306 * Cleanup kernel stream structure.
307 */
308 void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream)
309 {
310 DBG("[trace] Closing stream fd %d", stream->fd);
311 /* Close kernel fd */
312 close(stream->fd);
313 /* Remove from stream list */
314 cds_list_del(&stream->list);
315
316 free(stream->pathname);
317 free(stream);
318 }
319
320 /*
321 * Cleanup kernel event structure.
322 */
323 void trace_kernel_destroy_event(struct ltt_kernel_event *event)
324 {
325 DBG("[trace] Closing event fd %d", event->fd);
326 /* Close kernel fd */
327 close(event->fd);
328
329 /* Remove from event list */
330 cds_list_del(&event->list);
331
332 free(event->event);
333 free(event->ctx);
334 free(event);
335 }
336
337 /*
338 * Cleanup kernel channel structure.
339 */
340 void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel)
341 {
342 struct ltt_kernel_stream *stream, *stmp;
343 struct ltt_kernel_event *event, *etmp;
344
345 DBG("[trace] Closing channel fd %d", channel->fd);
346 /* Close kernel fd */
347 close(channel->fd);
348
349 /* For each stream in the channel list */
350 cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) {
351 trace_kernel_destroy_stream(stream);
352 }
353
354 /* For each event in the channel list */
355 cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) {
356 trace_kernel_destroy_event(event);
357 }
358
359 /* Remove from channel list */
360 cds_list_del(&channel->list);
361
362 free(channel->pathname);
363 free(channel->channel);
364 free(channel->ctx);
365 free(channel);
366 }
367
368 /*
369 * Cleanup kernel metadata structure.
370 */
371 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata)
372 {
373 DBG("[trace] Closing metadata fd %d", metadata->fd);
374 /* Close kernel fd */
375 close(metadata->fd);
376
377 free(metadata->conf);
378 free(metadata->pathname);
379 free(metadata);
380 }
381
382 /*
383 * Cleanup kernel session structure
384 */
385 void trace_kernel_destroy_session(struct ltt_kernel_session *session)
386 {
387 struct ltt_kernel_channel *channel, *ctmp;
388
389 DBG("[trace] Closing session fd %d", session->fd);
390 /* Close kernel fds */
391 close(session->fd);
392
393 if (session->metadata_stream_fd != 0) {
394 DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd);
395 close(session->metadata_stream_fd);
396 }
397
398 if (session->metadata != NULL) {
399 trace_kernel_destroy_metadata(session->metadata);
400 }
401
402 cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) {
403 trace_kernel_destroy_channel(channel);
404 }
405
406 free(session->trace_path);
407 free(session);
408 }
This page took 0.036793 seconds and 4 git commands to generate.