Fix: Close socket handle on error
[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 <lttng/event.h>
25 #include <lttng/lttng-error.h>
26 #include <lttng/userspace-probe.h>
27 #include <lttng/userspace-probe-internal.h>
28
29 #include <common/common.h>
30 #include <common/defaults.h>
31 #include <common/trace-chunk.h>
32
33 #include "consumer.h"
34 #include "trace-kernel.h"
35 #include "lttng-sessiond.h"
36 #include "notification-thread-commands.h"
37
38 /*
39 * Find the channel name for the given kernel session.
40 */
41 struct ltt_kernel_channel *trace_kernel_get_channel_by_name(
42 const char *name, struct ltt_kernel_session *session)
43 {
44 struct ltt_kernel_channel *chan;
45
46 assert(session);
47 assert(name);
48
49 /*
50 * If we receive an empty string for channel name, it means the
51 * default channel name is requested.
52 */
53 if (name[0] == '\0')
54 name = DEFAULT_CHANNEL_NAME;
55
56 DBG("Trying to find channel %s", name);
57
58 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
59 if (strcmp(name, chan->channel->name) == 0) {
60 DBG("Found channel by name %s", name);
61 return chan;
62 }
63 }
64
65 return NULL;
66 }
67
68 /*
69 * Find the event for the given channel.
70 */
71 struct ltt_kernel_event *trace_kernel_find_event(
72 char *name, struct ltt_kernel_channel *channel,
73 enum lttng_event_type type,
74 struct lttng_filter_bytecode *filter)
75 {
76 struct ltt_kernel_event *ev;
77 int found = 0;
78
79 assert(name);
80 assert(channel);
81
82 cds_list_for_each_entry(ev, &channel->events_list.head, list) {
83 if (type != LTTNG_EVENT_ALL && ev->type != type) {
84 continue;
85 }
86 if (strcmp(name, ev->event->name)) {
87 continue;
88 }
89 if ((ev->filter && !filter) || (!ev->filter && filter)) {
90 continue;
91 }
92 if (ev->filter && filter) {
93 if (ev->filter->len != filter->len ||
94 memcmp(ev->filter->data, filter->data,
95 filter->len) != 0) {
96 continue;
97 }
98 }
99 found = 1;
100 break;
101 }
102 if (found) {
103 DBG("Found event %s for channel %s", name,
104 channel->channel->name);
105 return ev;
106 } else {
107 return NULL;
108 }
109 }
110
111 /*
112 * Find the event name for the given channel.
113 */
114 struct ltt_kernel_event *trace_kernel_get_event_by_name(
115 char *name, struct ltt_kernel_channel *channel,
116 enum lttng_event_type type)
117 {
118 struct ltt_kernel_event *ev;
119 int found = 0;
120
121 assert(name);
122 assert(channel);
123
124 cds_list_for_each_entry(ev, &channel->events_list.head, list) {
125 if (type != LTTNG_EVENT_ALL && ev->type != type) {
126 continue;
127 }
128 if (strcmp(name, ev->event->name)) {
129 continue;
130 }
131 found = 1;
132 break;
133 }
134 if (found) {
135 DBG("Found event %s for channel %s", name,
136 channel->channel->name);
137 return ev;
138 } else {
139 return NULL;
140 }
141 }
142
143 /*
144 * Allocate and initialize a kernel session data structure.
145 *
146 * Return pointer to structure or NULL.
147 */
148 struct ltt_kernel_session *trace_kernel_create_session(void)
149 {
150 struct ltt_kernel_session *lks = NULL;
151
152 /* Allocate a new ltt kernel session */
153 lks = zmalloc(sizeof(struct ltt_kernel_session));
154 if (lks == NULL) {
155 PERROR("create kernel session zmalloc");
156 goto alloc_error;
157 }
158
159 /* Init data structure */
160 lks->fd = -1;
161 lks->metadata_stream_fd = -1;
162 lks->channel_count = 0;
163 lks->stream_count_global = 0;
164 lks->metadata = NULL;
165 CDS_INIT_LIST_HEAD(&lks->channel_list.head);
166
167 lks->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
168 if (lks->consumer == NULL) {
169 goto error;
170 }
171
172 return lks;
173
174 error:
175 free(lks);
176
177 alloc_error:
178 return NULL;
179 }
180
181 /*
182 * Allocate and initialize a kernel channel data structure.
183 *
184 * Return pointer to structure or NULL.
185 */
186 struct ltt_kernel_channel *trace_kernel_create_channel(
187 struct lttng_channel *chan)
188 {
189 struct ltt_kernel_channel *lkc;
190 struct lttng_channel_extended *extended = NULL;
191
192 assert(chan);
193
194 lkc = zmalloc(sizeof(struct ltt_kernel_channel));
195 if (lkc == NULL) {
196 PERROR("ltt_kernel_channel zmalloc");
197 goto error;
198 }
199
200 lkc->channel = zmalloc(sizeof(struct lttng_channel));
201 if (lkc->channel == NULL) {
202 PERROR("lttng_channel zmalloc");
203 goto error;
204 }
205
206 extended = zmalloc(sizeof(struct lttng_channel_extended));
207 if (!extended) {
208 PERROR("lttng_channel_channel zmalloc");
209 goto error;
210 }
211 memcpy(lkc->channel, chan, sizeof(struct lttng_channel));
212 memcpy(extended, chan->attr.extended.ptr, sizeof(struct lttng_channel_extended));
213 lkc->channel->attr.extended.ptr = extended;
214 extended = NULL;
215
216 /*
217 * If we receive an empty string for channel name, it means the
218 * default channel name is requested.
219 */
220 if (chan->name[0] == '\0') {
221 strncpy(lkc->channel->name, DEFAULT_CHANNEL_NAME,
222 sizeof(lkc->channel->name));
223 }
224 lkc->channel->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
225
226 lkc->fd = -1;
227 lkc->stream_count = 0;
228 lkc->event_count = 0;
229 lkc->enabled = 1;
230 lkc->published_to_notification_thread = false;
231 /* Init linked list */
232 CDS_INIT_LIST_HEAD(&lkc->events_list.head);
233 CDS_INIT_LIST_HEAD(&lkc->stream_list.head);
234 CDS_INIT_LIST_HEAD(&lkc->ctx_list);
235
236 return lkc;
237
238 error:
239 if (lkc) {
240 free(lkc->channel);
241 }
242 free(extended);
243 free(lkc);
244 return NULL;
245 }
246
247 /*
248 * Allocate and init a kernel context object.
249 *
250 * Return the allocated object or NULL on error.
251 */
252 struct ltt_kernel_context *trace_kernel_create_context(
253 struct lttng_kernel_context *ctx)
254 {
255 struct ltt_kernel_context *kctx;
256
257 kctx = zmalloc(sizeof(*kctx));
258 if (!kctx) {
259 PERROR("zmalloc kernel context");
260 goto error;
261 }
262
263 if (ctx) {
264 memcpy(&kctx->ctx, ctx, sizeof(kctx->ctx));
265 }
266 error:
267 return kctx;
268 }
269
270 /*
271 * Allocate and init a kernel context object from an existing kernel context
272 * object.
273 *
274 * Return the allocated object or NULL on error.
275 */
276 struct ltt_kernel_context *trace_kernel_copy_context(
277 struct ltt_kernel_context *kctx)
278 {
279 struct ltt_kernel_context *kctx_copy;
280
281 assert(kctx);
282 kctx_copy = zmalloc(sizeof(*kctx_copy));
283 if (!kctx_copy) {
284 PERROR("zmalloc ltt_kernel_context");
285 goto error;
286 }
287
288 memcpy(kctx_copy, kctx, sizeof(*kctx_copy));
289 memset(&kctx_copy->list, 0, sizeof(kctx_copy->list));
290
291 error:
292 return kctx_copy;
293 }
294
295 /*
296 * Allocate and initialize a kernel event. Set name and event type.
297 * We own filter_expression, and filter.
298 *
299 * Return pointer to structure or NULL.
300 */
301 enum lttng_error_code trace_kernel_create_event(
302 struct lttng_event *ev, char *filter_expression,
303 struct lttng_filter_bytecode *filter,
304 struct ltt_kernel_event **kernel_event)
305 {
306 enum lttng_error_code ret;
307 struct lttng_kernel_event *attr;
308 struct ltt_kernel_event *local_kernel_event;
309 struct lttng_userspace_probe_location *userspace_probe_location = NULL;
310
311 assert(ev);
312
313 local_kernel_event = zmalloc(sizeof(struct ltt_kernel_event));
314 attr = zmalloc(sizeof(struct lttng_kernel_event));
315 if (local_kernel_event == NULL || attr == NULL) {
316 PERROR("kernel event zmalloc");
317 ret = LTTNG_ERR_NOMEM;
318 goto error;
319 }
320
321 switch (ev->type) {
322 case LTTNG_EVENT_PROBE:
323 attr->instrumentation = LTTNG_KERNEL_KPROBE;
324 attr->u.kprobe.addr = ev->attr.probe.addr;
325 attr->u.kprobe.offset = ev->attr.probe.offset;
326 strncpy(attr->u.kprobe.symbol_name,
327 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
328 attr->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
329 break;
330 case LTTNG_EVENT_USERSPACE_PROBE:
331 {
332 const struct lttng_userspace_probe_location* location = NULL;
333 const struct lttng_userspace_probe_location_lookup_method *lookup = NULL;
334
335 location = lttng_event_get_userspace_probe_location(ev);
336 if (!location) {
337 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
338 goto error;
339 }
340
341 /*
342 * From this point on, the specific term 'uprobe' is used
343 * instead of the generic 'userspace probe' because it's the
344 * technology used at the moment for this instrumentation.
345 * LTTng currently implements userspace probes using uprobes.
346 * In the interactions with the kernel tracer, we use the
347 * uprobe term.
348 */
349 attr->instrumentation = LTTNG_KERNEL_UPROBE;
350
351 /*
352 * Only the elf lookup method is supported at the moment.
353 */
354 lookup = lttng_userspace_probe_location_get_lookup_method(
355 location);
356 if (!lookup) {
357 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
358 goto error;
359 }
360
361 /*
362 * From the kernel tracer's perspective, all userspace probe
363 * event types are all the same: a file and an offset.
364 */
365 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup)) {
366 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF:
367 /* Get the file descriptor on the target binary. */
368 attr->u.uprobe.fd =
369 lttng_userspace_probe_location_function_get_binary_fd(location);
370
371 /*
372 * Save a reference to the probe location used during
373 * the listing of events. Close its FD since it won't
374 * be needed for listing.
375 */
376 userspace_probe_location =
377 lttng_userspace_probe_location_copy(location);
378 ret = lttng_userspace_probe_location_function_set_binary_fd(
379 userspace_probe_location, -1);
380 if (ret) {
381 goto error;
382 }
383 break;
384 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT:
385 /* Get the file descriptor on the target binary. */
386 attr->u.uprobe.fd =
387 lttng_userspace_probe_location_tracepoint_get_binary_fd(location);
388
389 /*
390 * Save a reference to the probe location used during the listing of
391 * events. Close its FD since it won't be needed for listing.
392 */
393 userspace_probe_location =
394 lttng_userspace_probe_location_copy(location);
395 ret = lttng_userspace_probe_location_tracepoint_set_binary_fd(
396 userspace_probe_location, -1);
397 if (ret) {
398 goto error;
399 }
400 break;
401 default:
402 DBG("Unsupported lookup method type");
403 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
404 goto error;
405 }
406 break;
407 }
408 case LTTNG_EVENT_FUNCTION:
409 attr->instrumentation = LTTNG_KERNEL_KRETPROBE;
410 attr->u.kretprobe.addr = ev->attr.probe.addr;
411 attr->u.kretprobe.offset = ev->attr.probe.offset;
412 strncpy(attr->u.kretprobe.symbol_name,
413 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
414 attr->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
415 break;
416 case LTTNG_EVENT_FUNCTION_ENTRY:
417 attr->instrumentation = LTTNG_KERNEL_FUNCTION;
418 strncpy(attr->u.ftrace.symbol_name,
419 ev->attr.ftrace.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
420 attr->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
421 break;
422 case LTTNG_EVENT_TRACEPOINT:
423 attr->instrumentation = LTTNG_KERNEL_TRACEPOINT;
424 break;
425 case LTTNG_EVENT_SYSCALL:
426 attr->instrumentation = LTTNG_KERNEL_SYSCALL;
427 break;
428 case LTTNG_EVENT_ALL:
429 attr->instrumentation = LTTNG_KERNEL_ALL;
430 break;
431 default:
432 ERR("Unknown kernel instrumentation type (%d)", ev->type);
433 ret = LTTNG_ERR_INVALID;
434 goto error;
435 }
436
437 /* Copy event name */
438 strncpy(attr->name, ev->name, LTTNG_KERNEL_SYM_NAME_LEN);
439 attr->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
440
441 /* Setting up a kernel event */
442 local_kernel_event->fd = -1;
443 local_kernel_event->event = attr;
444 local_kernel_event->enabled = 1;
445 local_kernel_event->filter_expression = filter_expression;
446 local_kernel_event->filter = filter;
447 local_kernel_event->userspace_probe_location = userspace_probe_location;
448
449 *kernel_event = local_kernel_event;
450
451 return LTTNG_OK;
452
453 error:
454 free(filter_expression);
455 free(filter);
456 free(local_kernel_event);
457 free(attr);
458 return ret;
459 }
460
461 /*
462 * Allocate and initialize a kernel metadata.
463 *
464 * Return pointer to structure or NULL.
465 */
466 struct ltt_kernel_metadata *trace_kernel_create_metadata(void)
467 {
468 struct ltt_kernel_metadata *lkm;
469 struct lttng_channel *chan;
470
471 lkm = zmalloc(sizeof(struct ltt_kernel_metadata));
472 chan = zmalloc(sizeof(struct lttng_channel));
473 if (lkm == NULL || chan == NULL) {
474 PERROR("kernel metadata zmalloc");
475 goto error;
476 }
477
478 /* Set default attributes */
479 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
480 chan->attr.subbuf_size = default_get_metadata_subbuf_size();
481 chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
482 chan->attr.switch_timer_interval = DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
483 chan->attr.read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
484 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
485
486 /* Init metadata */
487 lkm->fd = -1;
488 lkm->conf = chan;
489
490 return lkm;
491
492 error:
493 free(lkm);
494 free(chan);
495 return NULL;
496 }
497
498 /*
499 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
500 * default.
501 *
502 * Return pointer to structure or NULL.
503 */
504 struct ltt_kernel_stream *trace_kernel_create_stream(const char *name,
505 unsigned int count)
506 {
507 int ret;
508 struct ltt_kernel_stream *lks;
509
510 assert(name);
511
512 lks = zmalloc(sizeof(struct ltt_kernel_stream));
513 if (lks == NULL) {
514 PERROR("kernel stream zmalloc");
515 goto error;
516 }
517
518 /* Set name */
519 ret = snprintf(lks->name, sizeof(lks->name), "%s_%u", name, count);
520 if (ret < 0) {
521 PERROR("snprintf stream name");
522 goto error;
523 }
524 lks->name[sizeof(lks->name) - 1] = '\0';
525
526 /* Init stream */
527 lks->fd = -1;
528 lks->state = 0;
529 lks->cpu = count;
530
531 return lks;
532
533 error:
534 return NULL;
535 }
536
537 /*
538 * Cleanup kernel stream structure.
539 */
540 void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream)
541 {
542 assert(stream);
543
544 DBG("[trace] Closing stream fd %d", stream->fd);
545 /* Close kernel fd */
546 if (stream->fd >= 0) {
547 int ret;
548
549 ret = close(stream->fd);
550 if (ret) {
551 PERROR("close");
552 }
553 }
554 /* Remove from stream list */
555 cds_list_del(&stream->list);
556
557 free(stream);
558 }
559
560 /*
561 * Cleanup kernel event structure.
562 */
563 void trace_kernel_destroy_event(struct ltt_kernel_event *event)
564 {
565 assert(event);
566
567 if (event->fd >= 0) {
568 int ret;
569
570 DBG("[trace] Closing event fd %d", event->fd);
571 /* Close kernel fd */
572 ret = close(event->fd);
573 if (ret) {
574 PERROR("close");
575 }
576 } else {
577 DBG("[trace] Tearing down event (no associated fd)");
578 }
579
580 /* Remove from event list */
581 cds_list_del(&event->list);
582
583 free(event->filter_expression);
584 free(event->filter);
585
586 free(event->event);
587 free(event);
588 }
589
590 /*
591 * Cleanup kernel context structure.
592 */
593 void trace_kernel_destroy_context(struct ltt_kernel_context *ctx)
594 {
595 assert(ctx);
596
597 if (ctx->in_list) {
598 cds_list_del(&ctx->list);
599 }
600 free(ctx);
601 }
602
603 /*
604 * Cleanup kernel channel structure.
605 */
606 void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel)
607 {
608 struct ltt_kernel_stream *stream, *stmp;
609 struct ltt_kernel_event *event, *etmp;
610 struct ltt_kernel_context *ctx, *ctmp;
611 int ret;
612 enum lttng_error_code status;
613
614 assert(channel);
615
616 DBG("[trace] Closing channel fd %d", channel->fd);
617 /* Close kernel fd */
618 if (channel->fd >= 0) {
619 ret = close(channel->fd);
620 if (ret) {
621 PERROR("close");
622 }
623 }
624
625 /* For each stream in the channel list */
626 cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) {
627 trace_kernel_destroy_stream(stream);
628 }
629
630 /* For each event in the channel list */
631 cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) {
632 trace_kernel_destroy_event(event);
633 }
634
635 /* For each context in the channel list */
636 cds_list_for_each_entry_safe(ctx, ctmp, &channel->ctx_list, list) {
637 trace_kernel_destroy_context(ctx);
638 }
639
640 /* Remove from channel list */
641 cds_list_del(&channel->list);
642
643 if (notification_thread_handle
644 && channel->published_to_notification_thread) {
645 status = notification_thread_command_remove_channel(
646 notification_thread_handle,
647 channel->key, LTTNG_DOMAIN_KERNEL);
648 assert(status == LTTNG_OK);
649 }
650 free(channel->channel->attr.extended.ptr);
651 free(channel->channel);
652 free(channel);
653 }
654
655 /*
656 * Cleanup kernel metadata structure.
657 */
658 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata)
659 {
660 assert(metadata);
661
662 DBG("[trace] Closing metadata fd %d", metadata->fd);
663 /* Close kernel fd */
664 if (metadata->fd >= 0) {
665 int ret;
666
667 ret = close(metadata->fd);
668 if (ret) {
669 PERROR("close");
670 }
671 }
672
673 free(metadata->conf);
674 free(metadata);
675 }
676
677 /*
678 * Cleanup kernel session structure
679 *
680 * Should *NOT* be called with RCU read-side lock held.
681 */
682 void trace_kernel_destroy_session(struct ltt_kernel_session *session)
683 {
684 struct ltt_kernel_channel *channel, *ctmp;
685 int ret;
686
687 assert(session);
688
689 DBG("[trace] Closing session fd %d", session->fd);
690 /* Close kernel fds */
691 if (session->fd >= 0) {
692 ret = close(session->fd);
693 if (ret) {
694 PERROR("close");
695 }
696 }
697
698 if (session->metadata_stream_fd >= 0) {
699 DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd);
700 ret = close(session->metadata_stream_fd);
701 if (ret) {
702 PERROR("close");
703 }
704 }
705
706 if (session->metadata != NULL) {
707 trace_kernel_destroy_metadata(session->metadata);
708 }
709
710 cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) {
711 trace_kernel_destroy_channel(channel);
712 }
713 }
714
715 /* Free elements needed by destroy notifiers. */
716 void trace_kernel_free_session(struct ltt_kernel_session *session)
717 {
718 /* Wipe consumer output object */
719 consumer_output_put(session->consumer);
720
721 free(session);
722 }
This page took 0.057898 seconds and 4 git commands to generate.