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