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