2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
4 * SPDX-License-Identifier: GPL-2.0-only
14 #include <lttng/event.h>
15 #include <lttng/lttng-error.h>
16 #include <lttng/userspace-probe.h>
17 #include <lttng/userspace-probe-internal.h>
19 #include <common/common.h>
20 #include <common/defaults.h>
21 #include <common/trace-chunk.h>
22 #include <common/macros.h>
25 #include "trace-kernel.h"
26 #include "lttng-sessiond.h"
27 #include "notification-thread-commands.h"
30 * Find the channel name for the given kernel session.
32 struct ltt_kernel_channel
*trace_kernel_get_channel_by_name(
33 const char *name
, struct ltt_kernel_session
*session
)
35 struct ltt_kernel_channel
*chan
;
41 * If we receive an empty string for channel name, it means the
42 * default channel name is requested.
45 name
= DEFAULT_CHANNEL_NAME
;
47 DBG("Trying to find channel %s", name
);
49 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
50 if (strcmp(name
, chan
->channel
->name
) == 0) {
51 DBG("Found channel by name %s", name
);
60 * Find the event for the given channel.
62 struct ltt_kernel_event
*trace_kernel_find_event(
63 char *name
, struct ltt_kernel_channel
*channel
,
64 enum lttng_event_type type
,
65 struct lttng_filter_bytecode
*filter
)
67 struct ltt_kernel_event
*ev
;
73 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
74 if (type
!= LTTNG_EVENT_ALL
&& ev
->type
!= type
) {
77 if (strcmp(name
, ev
->event
->name
)) {
80 if ((ev
->filter
&& !filter
) || (!ev
->filter
&& filter
)) {
83 if (ev
->filter
&& filter
) {
84 if (ev
->filter
->len
!= filter
->len
||
85 memcmp(ev
->filter
->data
, filter
->data
,
94 DBG("Found event %s for channel %s", name
,
95 channel
->channel
->name
);
103 * Find the event name for the given channel.
105 struct ltt_kernel_event
*trace_kernel_get_event_by_name(
106 char *name
, struct ltt_kernel_channel
*channel
,
107 enum lttng_event_type type
)
109 struct ltt_kernel_event
*ev
;
115 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
116 if (type
!= LTTNG_EVENT_ALL
&& ev
->type
!= type
) {
119 if (strcmp(name
, ev
->event
->name
)) {
126 DBG("Found event %s for channel %s", name
,
127 channel
->channel
->name
);
135 * Allocate and initialize a kernel session data structure.
137 * Return pointer to structure or NULL.
139 struct ltt_kernel_session
*trace_kernel_create_session(void)
141 struct ltt_kernel_session
*lks
= NULL
;
143 /* Allocate a new ltt kernel session */
144 lks
= zmalloc(sizeof(struct ltt_kernel_session
));
146 PERROR("create kernel session zmalloc");
150 /* Init data structure */
152 lks
->metadata_stream_fd
= -1;
153 lks
->channel_count
= 0;
154 lks
->stream_count_global
= 0;
155 lks
->metadata
= NULL
;
156 CDS_INIT_LIST_HEAD(&lks
->channel_list
.head
);
158 lks
->tracker_pid
= process_attr_tracker_create();
159 if (!lks
->tracker_pid
) {
162 lks
->tracker_vpid
= process_attr_tracker_create();
163 if (!lks
->tracker_vpid
) {
166 lks
->tracker_uid
= process_attr_tracker_create();
167 if (!lks
->tracker_uid
) {
170 lks
->tracker_vuid
= process_attr_tracker_create();
171 if (!lks
->tracker_vuid
) {
174 lks
->tracker_gid
= process_attr_tracker_create();
175 if (!lks
->tracker_gid
) {
178 lks
->tracker_vgid
= process_attr_tracker_create();
179 if (!lks
->tracker_vgid
) {
182 lks
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
183 if (lks
->consumer
== NULL
) {
190 process_attr_tracker_destroy(lks
->tracker_pid
);
191 process_attr_tracker_destroy(lks
->tracker_vpid
);
192 process_attr_tracker_destroy(lks
->tracker_uid
);
193 process_attr_tracker_destroy(lks
->tracker_vuid
);
194 process_attr_tracker_destroy(lks
->tracker_gid
);
195 process_attr_tracker_destroy(lks
->tracker_vgid
);
203 * Allocate and initialize a kernel channel data structure.
205 * Return pointer to structure or NULL.
207 struct ltt_kernel_channel
*trace_kernel_create_channel(
208 struct lttng_channel
*chan
)
210 struct ltt_kernel_channel
*lkc
;
211 struct lttng_channel_extended
*extended
= NULL
;
215 lkc
= zmalloc(sizeof(struct ltt_kernel_channel
));
217 PERROR("ltt_kernel_channel zmalloc");
221 lkc
->channel
= zmalloc(sizeof(struct lttng_channel
));
222 if (lkc
->channel
== NULL
) {
223 PERROR("lttng_channel zmalloc");
227 extended
= zmalloc(sizeof(struct lttng_channel_extended
));
229 PERROR("lttng_channel_channel zmalloc");
232 memcpy(lkc
->channel
, chan
, sizeof(struct lttng_channel
));
233 memcpy(extended
, chan
->attr
.extended
.ptr
, sizeof(struct lttng_channel_extended
));
234 lkc
->channel
->attr
.extended
.ptr
= extended
;
238 * If we receive an empty string for channel name, it means the
239 * default channel name is requested.
241 if (chan
->name
[0] == '\0') {
242 strncpy(lkc
->channel
->name
, DEFAULT_CHANNEL_NAME
,
243 sizeof(lkc
->channel
->name
));
245 lkc
->channel
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
248 lkc
->stream_count
= 0;
249 lkc
->event_count
= 0;
251 lkc
->published_to_notification_thread
= false;
252 /* Init linked list */
253 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
254 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
255 CDS_INIT_LIST_HEAD(&lkc
->ctx_list
);
269 * Allocate and init a kernel context object.
271 * Return the allocated object or NULL on error.
273 struct ltt_kernel_context
*trace_kernel_create_context(
274 struct lttng_kernel_context
*ctx
)
276 struct ltt_kernel_context
*kctx
;
278 kctx
= zmalloc(sizeof(*kctx
));
280 PERROR("zmalloc kernel context");
285 memcpy(&kctx
->ctx
, ctx
, sizeof(kctx
->ctx
));
292 * Allocate and init a kernel context object from an existing kernel context
295 * Return the allocated object or NULL on error.
297 struct ltt_kernel_context
*trace_kernel_copy_context(
298 struct ltt_kernel_context
*kctx
)
300 struct ltt_kernel_context
*kctx_copy
;
303 kctx_copy
= zmalloc(sizeof(*kctx_copy
));
305 PERROR("zmalloc ltt_kernel_context");
309 memcpy(kctx_copy
, kctx
, sizeof(*kctx_copy
));
310 memset(&kctx_copy
->list
, 0, sizeof(kctx_copy
->list
));
317 * Allocate and initialize a kernel event. Set name and event type.
318 * We own filter_expression, and filter.
320 * Return pointer to structure or NULL.
322 enum lttng_error_code
trace_kernel_create_event(
323 struct lttng_event
*ev
, char *filter_expression
,
324 struct lttng_filter_bytecode
*filter
,
325 struct ltt_kernel_event
**kernel_event
)
327 enum lttng_error_code ret
;
328 struct lttng_kernel_event
*attr
;
329 struct ltt_kernel_event
*local_kernel_event
;
330 struct lttng_userspace_probe_location
*userspace_probe_location
= NULL
;
334 local_kernel_event
= zmalloc(sizeof(struct ltt_kernel_event
));
335 attr
= zmalloc(sizeof(struct lttng_kernel_event
));
336 if (local_kernel_event
== NULL
|| attr
== NULL
) {
337 PERROR("kernel event zmalloc");
338 ret
= LTTNG_ERR_NOMEM
;
343 case LTTNG_EVENT_PROBE
:
344 attr
->instrumentation
= LTTNG_KERNEL_KPROBE
;
345 attr
->u
.kprobe
.addr
= ev
->attr
.probe
.addr
;
346 attr
->u
.kprobe
.offset
= ev
->attr
.probe
.offset
;
347 strncpy(attr
->u
.kprobe
.symbol_name
,
348 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
349 attr
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
351 case LTTNG_EVENT_USERSPACE_PROBE
:
353 const struct lttng_userspace_probe_location
* location
= NULL
;
354 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
356 location
= lttng_event_get_userspace_probe_location(ev
);
358 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
363 * From this point on, the specific term 'uprobe' is used
364 * instead of the generic 'userspace probe' because it's the
365 * technology used at the moment for this instrumentation.
366 * LTTng currently implements userspace probes using uprobes.
367 * In the interactions with the kernel tracer, we use the
370 attr
->instrumentation
= LTTNG_KERNEL_UPROBE
;
373 * Only the elf lookup method is supported at the moment.
375 lookup
= lttng_userspace_probe_location_get_lookup_method(
378 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
383 * From the kernel tracer's perspective, all userspace probe
384 * event types are all the same: a file and an offset.
386 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
387 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
388 /* Get the file descriptor on the target binary. */
390 lttng_userspace_probe_location_function_get_binary_fd(location
);
393 * Save a reference to the probe location used during
394 * the listing of events.
396 userspace_probe_location
=
397 lttng_userspace_probe_location_copy(location
);
399 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
400 /* Get the file descriptor on the target binary. */
402 lttng_userspace_probe_location_tracepoint_get_binary_fd(location
);
405 * Save a reference to the probe location used during the listing of
408 userspace_probe_location
=
409 lttng_userspace_probe_location_copy(location
);
412 DBG("Unsupported lookup method type");
413 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
418 case LTTNG_EVENT_FUNCTION
:
419 attr
->instrumentation
= LTTNG_KERNEL_KRETPROBE
;
420 attr
->u
.kretprobe
.addr
= ev
->attr
.probe
.addr
;
421 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
422 strncpy(attr
->u
.kretprobe
.symbol_name
,
423 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
424 attr
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
426 case LTTNG_EVENT_FUNCTION_ENTRY
:
427 attr
->instrumentation
= LTTNG_KERNEL_FUNCTION
;
428 strncpy(attr
->u
.ftrace
.symbol_name
,
429 ev
->attr
.ftrace
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
430 attr
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
432 case LTTNG_EVENT_TRACEPOINT
:
433 attr
->instrumentation
= LTTNG_KERNEL_TRACEPOINT
;
435 case LTTNG_EVENT_SYSCALL
:
436 attr
->instrumentation
= LTTNG_KERNEL_SYSCALL
;
438 case LTTNG_EVENT_ALL
:
439 attr
->instrumentation
= LTTNG_KERNEL_ALL
;
442 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
443 ret
= LTTNG_ERR_INVALID
;
447 /* Copy event name */
448 strncpy(attr
->name
, ev
->name
, LTTNG_KERNEL_SYM_NAME_LEN
);
449 attr
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
451 /* Setting up a kernel event */
452 local_kernel_event
->fd
= -1;
453 local_kernel_event
->event
= attr
;
454 local_kernel_event
->enabled
= 1;
455 local_kernel_event
->filter_expression
= filter_expression
;
456 local_kernel_event
->filter
= filter
;
457 local_kernel_event
->userspace_probe_location
= userspace_probe_location
;
459 *kernel_event
= local_kernel_event
;
464 free(filter_expression
);
466 free(local_kernel_event
);
472 * Allocate and initialize a kernel metadata.
474 * Return pointer to structure or NULL.
476 struct ltt_kernel_metadata
*trace_kernel_create_metadata(void)
479 struct ltt_kernel_metadata
*lkm
;
480 struct lttng_channel
*chan
;
482 lkm
= zmalloc(sizeof(struct ltt_kernel_metadata
));
483 chan
= zmalloc(sizeof(struct lttng_channel
));
484 if (lkm
== NULL
|| chan
== NULL
) {
485 PERROR("kernel metadata zmalloc");
490 chan
->name
, DEFAULT_METADATA_NAME
, sizeof(chan
->name
));
492 ERR("Failed to initialize metadata channel name to `%s`",
493 DEFAULT_METADATA_NAME
);
497 /* Set default attributes */
498 chan
->attr
.overwrite
= DEFAULT_METADATA_OVERWRITE
;
499 chan
->attr
.subbuf_size
= default_get_metadata_subbuf_size();
500 chan
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
501 chan
->attr
.switch_timer_interval
= DEFAULT_METADATA_SWITCH_TIMER
;
502 chan
->attr
.read_timer_interval
= DEFAULT_METADATA_READ_TIMER
;;
506 * The metadata channel of kernel sessions must use the "mmap"
507 * back-end since the consumer daemon accumulates complete
508 * metadata units before sending them to the relay daemon in
509 * live mode. The consumer daemon also needs to extract the contents
510 * of the metadata cache when computing a rotation position.
512 * In both cases, it is not possible to rely on the splice
513 * back-end as the consumer daemon may need to accumulate more
514 * content than can be backed by the ring buffer's underlying
517 chan
->attr
.output
= LTTNG_EVENT_MMAP
;
518 chan
->attr
.tracefile_size
= 0;
519 chan
->attr
.tracefile_count
= 0;
520 chan
->attr
.live_timer_interval
= 0;
535 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
538 * Return pointer to structure or NULL.
540 struct ltt_kernel_stream
*trace_kernel_create_stream(const char *name
,
544 struct ltt_kernel_stream
*lks
;
548 lks
= zmalloc(sizeof(struct ltt_kernel_stream
));
550 PERROR("kernel stream zmalloc");
555 ret
= snprintf(lks
->name
, sizeof(lks
->name
), "%s_%u", name
, count
);
557 PERROR("snprintf stream name");
560 lks
->name
[sizeof(lks
->name
) - 1] = '\0';
574 * Cleanup kernel stream structure.
576 void trace_kernel_destroy_stream(struct ltt_kernel_stream
*stream
)
580 DBG("[trace] Closing stream fd %d", stream
->fd
);
581 /* Close kernel fd */
582 if (stream
->fd
>= 0) {
585 ret
= close(stream
->fd
);
590 /* Remove from stream list */
591 cds_list_del(&stream
->list
);
597 * Cleanup kernel event structure.
599 void trace_kernel_destroy_event(struct ltt_kernel_event
*event
)
603 if (event
->fd
>= 0) {
606 DBG("[trace] Closing event fd %d", event
->fd
);
607 /* Close kernel fd */
608 ret
= close(event
->fd
);
613 DBG("[trace] Tearing down event (no associated fd)");
616 /* Remove from event list */
617 cds_list_del(&event
->list
);
619 free(event
->filter_expression
);
627 * Cleanup kernel context structure.
629 void trace_kernel_destroy_context(struct ltt_kernel_context
*ctx
)
634 cds_list_del(&ctx
->list
);
640 * Cleanup kernel channel structure.
642 void trace_kernel_destroy_channel(struct ltt_kernel_channel
*channel
)
644 struct ltt_kernel_stream
*stream
, *stmp
;
645 struct ltt_kernel_event
*event
, *etmp
;
646 struct ltt_kernel_context
*ctx
, *ctmp
;
648 enum lttng_error_code status
;
652 DBG("[trace] Closing channel fd %d", channel
->fd
);
653 /* Close kernel fd */
654 if (channel
->fd
>= 0) {
655 ret
= close(channel
->fd
);
661 /* For each stream in the channel list */
662 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->stream_list
.head
, list
) {
663 trace_kernel_destroy_stream(stream
);
666 /* For each event in the channel list */
667 cds_list_for_each_entry_safe(event
, etmp
, &channel
->events_list
.head
, list
) {
668 trace_kernel_destroy_event(event
);
671 /* For each context in the channel list */
672 cds_list_for_each_entry_safe(ctx
, ctmp
, &channel
->ctx_list
, list
) {
673 trace_kernel_destroy_context(ctx
);
676 /* Remove from channel list */
677 cds_list_del(&channel
->list
);
679 if (notification_thread_handle
680 && channel
->published_to_notification_thread
) {
681 status
= notification_thread_command_remove_channel(
682 notification_thread_handle
,
683 channel
->key
, LTTNG_DOMAIN_KERNEL
);
684 assert(status
== LTTNG_OK
);
686 free(channel
->channel
->attr
.extended
.ptr
);
687 free(channel
->channel
);
692 * Cleanup kernel metadata structure.
694 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata
*metadata
)
698 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
699 /* Close kernel fd */
700 if (metadata
->fd
>= 0) {
703 ret
= close(metadata
->fd
);
709 free(metadata
->conf
);
714 * Cleanup kernel session structure
716 * Should *NOT* be called with RCU read-side lock held.
718 void trace_kernel_destroy_session(struct ltt_kernel_session
*session
)
720 struct ltt_kernel_channel
*channel
, *ctmp
;
725 DBG("[trace] Closing session fd %d", session
->fd
);
726 /* Close kernel fds */
727 if (session
->fd
>= 0) {
728 ret
= close(session
->fd
);
734 if (session
->metadata_stream_fd
>= 0) {
735 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
736 ret
= close(session
->metadata_stream_fd
);
742 if (session
->metadata
!= NULL
) {
743 trace_kernel_destroy_metadata(session
->metadata
);
746 cds_list_for_each_entry_safe(channel
, ctmp
, &session
->channel_list
.head
, list
) {
747 trace_kernel_destroy_channel(channel
);
751 /* Free elements needed by destroy notifiers. */
752 void trace_kernel_free_session(struct ltt_kernel_session
*session
)
754 /* Wipe consumer output object */
755 consumer_output_put(session
->consumer
);
757 process_attr_tracker_destroy(session
->tracker_pid
);
758 process_attr_tracker_destroy(session
->tracker_vpid
);
759 process_attr_tracker_destroy(session
->tracker_uid
);
760 process_attr_tracker_destroy(session
->tracker_vuid
);
761 process_attr_tracker_destroy(session
->tracker_gid
);
762 process_attr_tracker_destroy(session
->tracker_vgid
);