29432de49a0355b16863ab69189929a6c37719f3
[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 modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _LGPL_SOURCE
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23
24 #include <common/common.h>
25 #include <common/defaults.h>
26
27 #include "consumer.h"
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 assert(session);
39 assert(name);
40
41 /*
42 * If we receive an empty string for channel name, it means the
43 * default channel name is requested.
44 */
45 if (name[0] == '\0')
46 name = DEFAULT_CHANNEL_NAME;
47
48 DBG("Trying to find channel %s", name);
49
50 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
51 if (strcmp(name, chan->channel->name) == 0) {
52 DBG("Found channel by name %s", name);
53 return chan;
54 }
55 }
56
57 return NULL;
58 }
59
60 /*
61 * Find the event for the given channel.
62 */
63 struct ltt_kernel_event *trace_kernel_find_event(
64 char *name, struct ltt_kernel_channel *channel,
65 enum lttng_event_type type,
66 struct lttng_filter_bytecode *filter)
67 {
68 struct ltt_kernel_event *ev;
69 int found = 0;
70
71 assert(name);
72 assert(channel);
73
74 cds_list_for_each_entry(ev, &channel->events_list.head, list) {
75 if (type != LTTNG_EVENT_ALL && ev->type != type) {
76 continue;
77 }
78 if (strcmp(name, ev->event->name)) {
79 continue;
80 }
81 if ((ev->filter && !filter) || (!ev->filter && filter)) {
82 continue;
83 }
84 if (ev->filter && filter) {
85 if (ev->filter->len != filter->len ||
86 memcmp(ev->filter->data, filter->data,
87 filter->len) != 0) {
88 continue;
89 }
90 }
91 found = 1;
92 break;
93 }
94 if (found) {
95 DBG("Found event %s for channel %s", name,
96 channel->channel->name);
97 return ev;
98 } else {
99 return NULL;
100 }
101 }
102
103 /*
104 * Find the event name for the given channel.
105 */
106 struct ltt_kernel_event *trace_kernel_get_event_by_name(
107 char *name, struct ltt_kernel_channel *channel,
108 enum lttng_event_type type)
109 {
110 struct ltt_kernel_event *ev;
111 int found = 0;
112
113 assert(name);
114 assert(channel);
115
116 cds_list_for_each_entry(ev, &channel->events_list.head, list) {
117 if (type != LTTNG_EVENT_ALL && ev->type != type) {
118 continue;
119 }
120 if (strcmp(name, ev->event->name)) {
121 continue;
122 }
123 found = 1;
124 break;
125 }
126 if (found) {
127 DBG("Found event %s for channel %s", name,
128 channel->channel->name);
129 return ev;
130 } else {
131 return NULL;
132 }
133 }
134
135 /*
136 * Allocate and initialize a kernel session data structure.
137 *
138 * Return pointer to structure or NULL.
139 */
140 struct ltt_kernel_session *trace_kernel_create_session(void)
141 {
142 struct ltt_kernel_session *lks = NULL;
143
144 /* Allocate a new ltt kernel session */
145 lks = zmalloc(sizeof(struct ltt_kernel_session));
146 if (lks == NULL) {
147 PERROR("create kernel session zmalloc");
148 goto alloc_error;
149 }
150
151 /* Init data structure */
152 lks->fd = -1;
153 lks->metadata_stream_fd = -1;
154 lks->channel_count = 0;
155 lks->stream_count_global = 0;
156 lks->metadata = NULL;
157 CDS_INIT_LIST_HEAD(&lks->channel_list.head);
158
159 lks->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
160 if (lks->consumer == NULL) {
161 goto error;
162 }
163
164 return lks;
165
166 error:
167 free(lks);
168
169 alloc_error:
170 return NULL;
171 }
172
173 /*
174 * Allocate and initialize a kernel channel data structure.
175 *
176 * Return pointer to structure or NULL.
177 */
178 struct ltt_kernel_channel *trace_kernel_create_channel(
179 struct lttng_channel *chan)
180 {
181 struct ltt_kernel_channel *lkc;
182
183 assert(chan);
184
185 lkc = zmalloc(sizeof(struct ltt_kernel_channel));
186 if (lkc == NULL) {
187 PERROR("ltt_kernel_channel zmalloc");
188 goto error;
189 }
190
191 lkc->channel = zmalloc(sizeof(struct lttng_channel));
192 if (lkc->channel == NULL) {
193 PERROR("lttng_channel zmalloc");
194 free(lkc);
195 goto error;
196 }
197 memcpy(lkc->channel, chan, sizeof(struct lttng_channel));
198
199 /*
200 * If we receive an empty string for channel name, it means the
201 * default channel name is requested.
202 */
203 if (chan->name[0] == '\0') {
204 strncpy(lkc->channel->name, DEFAULT_CHANNEL_NAME,
205 sizeof(lkc->channel->name));
206 }
207 lkc->channel->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
208
209 lkc->fd = -1;
210 lkc->stream_count = 0;
211 lkc->event_count = 0;
212 lkc->enabled = 1;
213 /* Init linked list */
214 CDS_INIT_LIST_HEAD(&lkc->events_list.head);
215 CDS_INIT_LIST_HEAD(&lkc->stream_list.head);
216 CDS_INIT_LIST_HEAD(&lkc->ctx_list);
217
218 return lkc;
219
220 error:
221 return NULL;
222 }
223
224 /*
225 * Allocate and init a kernel context object.
226 *
227 * Return the allocated object or NULL on error.
228 */
229 struct ltt_kernel_context *trace_kernel_create_context(
230 struct lttng_kernel_context *ctx)
231 {
232 struct ltt_kernel_context *kctx;
233
234 kctx = zmalloc(sizeof(*kctx));
235 if (!kctx) {
236 PERROR("zmalloc kernel context");
237 goto error;
238 }
239
240 if (ctx) {
241 memcpy(&kctx->ctx, ctx, sizeof(kctx->ctx));
242 }
243
244 CDS_INIT_LIST_HEAD(&kctx->list);
245
246 error:
247 return kctx;
248 }
249
250 /*
251 * Allocate and initialize a kernel event. Set name and event type.
252 * We own filter_expression, and filter.
253 *
254 * Return pointer to structure or NULL.
255 */
256 struct ltt_kernel_event *trace_kernel_create_event(struct lttng_event *ev,
257 char *filter_expression, struct lttng_filter_bytecode *filter)
258 {
259 struct ltt_kernel_event *lke;
260 struct lttng_kernel_event *attr;
261
262 assert(ev);
263
264 lke = zmalloc(sizeof(struct ltt_kernel_event));
265 attr = zmalloc(sizeof(struct lttng_kernel_event));
266 if (lke == NULL || attr == NULL) {
267 PERROR("kernel event zmalloc");
268 goto error;
269 }
270
271 switch (ev->type) {
272 case LTTNG_EVENT_PROBE:
273 attr->instrumentation = LTTNG_KERNEL_KPROBE;
274 attr->u.kprobe.addr = ev->attr.probe.addr;
275 attr->u.kprobe.offset = ev->attr.probe.offset;
276 strncpy(attr->u.kprobe.symbol_name,
277 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
278 attr->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
279 break;
280 case LTTNG_EVENT_FUNCTION:
281 attr->instrumentation = LTTNG_KERNEL_KRETPROBE;
282 attr->u.kretprobe.addr = ev->attr.probe.addr;
283 attr->u.kretprobe.offset = ev->attr.probe.offset;
284 strncpy(attr->u.kretprobe.symbol_name,
285 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
286 attr->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
287 break;
288 case LTTNG_EVENT_FUNCTION_ENTRY:
289 attr->instrumentation = LTTNG_KERNEL_FUNCTION;
290 strncpy(attr->u.ftrace.symbol_name,
291 ev->attr.ftrace.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
292 attr->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
293 break;
294 case LTTNG_EVENT_TRACEPOINT:
295 attr->instrumentation = LTTNG_KERNEL_TRACEPOINT;
296 break;
297 case LTTNG_EVENT_SYSCALL:
298 attr->instrumentation = LTTNG_KERNEL_SYSCALL;
299 break;
300 case LTTNG_EVENT_ALL:
301 attr->instrumentation = LTTNG_KERNEL_ALL;
302 break;
303 default:
304 ERR("Unknown kernel instrumentation type (%d)", ev->type);
305 goto error;
306 }
307
308 /* Copy event name */
309 strncpy(attr->name, ev->name, LTTNG_KERNEL_SYM_NAME_LEN);
310 attr->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
311
312 /* Setting up a kernel event */
313 lke->fd = -1;
314 lke->event = attr;
315 lke->enabled = 1;
316 lke->filter_expression = filter_expression;
317 lke->filter = filter;
318
319 return lke;
320
321 error:
322 free(filter_expression);
323 free(filter);
324 free(lke);
325 free(attr);
326 return NULL;
327 }
328
329 /*
330 * Allocate and initialize a kernel metadata.
331 *
332 * Return pointer to structure or NULL.
333 */
334 struct ltt_kernel_metadata *trace_kernel_create_metadata(void)
335 {
336 struct ltt_kernel_metadata *lkm;
337 struct lttng_channel *chan;
338
339 lkm = zmalloc(sizeof(struct ltt_kernel_metadata));
340 chan = zmalloc(sizeof(struct lttng_channel));
341 if (lkm == NULL || chan == NULL) {
342 PERROR("kernel metadata zmalloc");
343 goto error;
344 }
345
346 /* Set default attributes */
347 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
348 chan->attr.subbuf_size = default_get_metadata_subbuf_size();
349 chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
350 chan->attr.switch_timer_interval = DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
351 chan->attr.read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
352 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
353
354 /* Init metadata */
355 lkm->fd = -1;
356 lkm->conf = chan;
357
358 return lkm;
359
360 error:
361 free(lkm);
362 free(chan);
363 return NULL;
364 }
365
366 /*
367 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
368 * default.
369 *
370 * Return pointer to structure or NULL.
371 */
372 struct ltt_kernel_stream *trace_kernel_create_stream(const char *name,
373 unsigned int count)
374 {
375 int ret;
376 struct ltt_kernel_stream *lks;
377
378 assert(name);
379
380 lks = zmalloc(sizeof(struct ltt_kernel_stream));
381 if (lks == NULL) {
382 PERROR("kernel stream zmalloc");
383 goto error;
384 }
385
386 /* Set name */
387 ret = snprintf(lks->name, sizeof(lks->name), "%s_%u", name, count);
388 if (ret < 0) {
389 PERROR("snprintf stream name");
390 goto error;
391 }
392 lks->name[sizeof(lks->name) - 1] = '\0';
393
394 /* Init stream */
395 lks->fd = -1;
396 lks->state = 0;
397 lks->cpu = count;
398
399 return lks;
400
401 error:
402 return NULL;
403 }
404
405 /*
406 * Cleanup kernel stream structure.
407 */
408 void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream)
409 {
410 assert(stream);
411
412 DBG("[trace] Closing stream fd %d", stream->fd);
413 /* Close kernel fd */
414 if (stream->fd >= 0) {
415 int ret;
416
417 ret = close(stream->fd);
418 if (ret) {
419 PERROR("close");
420 }
421 }
422 /* Remove from stream list */
423 cds_list_del(&stream->list);
424
425 free(stream);
426 }
427
428 /*
429 * Cleanup kernel event structure.
430 */
431 void trace_kernel_destroy_event(struct ltt_kernel_event *event)
432 {
433 assert(event);
434
435 if (event->fd >= 0) {
436 int ret;
437
438 DBG("[trace] Closing event fd %d", event->fd);
439 /* Close kernel fd */
440 ret = close(event->fd);
441 if (ret) {
442 PERROR("close");
443 }
444 } else {
445 DBG("[trace] Tearing down event (no associated fd)");
446 }
447
448 /* Remove from event list */
449 cds_list_del(&event->list);
450
451 free(event->filter_expression);
452 free(event->filter);
453
454 free(event->event);
455 free(event);
456 }
457
458 /*
459 * Cleanup kernel context structure.
460 */
461 void trace_kernel_destroy_context(struct ltt_kernel_context *ctx)
462 {
463 assert(ctx);
464
465 cds_list_del(&ctx->list);
466 free(ctx);
467 }
468
469 /*
470 * Cleanup kernel channel structure.
471 */
472 void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel)
473 {
474 struct ltt_kernel_stream *stream, *stmp;
475 struct ltt_kernel_event *event, *etmp;
476 struct ltt_kernel_context *ctx, *ctmp;
477 int ret;
478
479 assert(channel);
480
481 DBG("[trace] Closing channel fd %d", channel->fd);
482 /* Close kernel fd */
483 if (channel->fd >= 0) {
484 ret = close(channel->fd);
485 if (ret) {
486 PERROR("close");
487 }
488 }
489
490 /* For each stream in the channel list */
491 cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) {
492 trace_kernel_destroy_stream(stream);
493 }
494
495 /* For each event in the channel list */
496 cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) {
497 trace_kernel_destroy_event(event);
498 }
499
500 /* For each context in the channel list */
501 cds_list_for_each_entry_safe(ctx, ctmp, &channel->ctx_list, list) {
502 trace_kernel_destroy_context(ctx);
503 }
504
505 /* Remove from channel list */
506 cds_list_del(&channel->list);
507
508 free(channel->channel);
509 free(channel);
510 }
511
512 /*
513 * Cleanup kernel metadata structure.
514 */
515 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata)
516 {
517 assert(metadata);
518
519 DBG("[trace] Closing metadata fd %d", metadata->fd);
520 /* Close kernel fd */
521 if (metadata->fd >= 0) {
522 int ret;
523
524 ret = close(metadata->fd);
525 if (ret) {
526 PERROR("close");
527 }
528 }
529
530 free(metadata->conf);
531 free(metadata);
532 }
533
534 /*
535 * Cleanup kernel session structure
536 *
537 * Should *NOT* be called with RCU read-side lock held.
538 */
539 void trace_kernel_destroy_session(struct ltt_kernel_session *session)
540 {
541 struct ltt_kernel_channel *channel, *ctmp;
542 int ret;
543
544 assert(session);
545
546 DBG("[trace] Closing session fd %d", session->fd);
547 /* Close kernel fds */
548 if (session->fd >= 0) {
549 ret = close(session->fd);
550 if (ret) {
551 PERROR("close");
552 }
553 }
554
555 if (session->metadata_stream_fd >= 0) {
556 DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd);
557 ret = close(session->metadata_stream_fd);
558 if (ret) {
559 PERROR("close");
560 }
561 }
562
563 if (session->metadata != NULL) {
564 trace_kernel_destroy_metadata(session->metadata);
565 }
566
567 cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) {
568 trace_kernel_destroy_channel(channel);
569 }
570
571 /* Wipe consumer output object */
572 consumer_output_put(session->consumer);
573
574 free(session);
575 }
This page took 0.042805 seconds and 4 git commands to generate.