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
;
437 attr
->u
.syscall
.abi
= LTTNG_KERNEL_SYSCALL_ABI_ALL
;
438 attr
->u
.syscall
.entryexit
= LTTNG_KERNEL_SYSCALL_ENTRYEXIT
;
439 attr
->u
.syscall
.match
= LTTNG_KERNEL_SYSCALL_MATCH_NAME
;
441 case LTTNG_EVENT_ALL
:
442 attr
->instrumentation
= LTTNG_KERNEL_ALL
;
445 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
446 ret
= LTTNG_ERR_INVALID
;
450 /* Copy event name */
451 strncpy(attr
->name
, ev
->name
, LTTNG_KERNEL_SYM_NAME_LEN
);
452 attr
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
454 /* Setting up a kernel event */
455 local_kernel_event
->fd
= -1;
456 local_kernel_event
->event
= attr
;
457 local_kernel_event
->enabled
= 1;
458 local_kernel_event
->filter_expression
= filter_expression
;
459 local_kernel_event
->filter
= filter
;
460 local_kernel_event
->userspace_probe_location
= userspace_probe_location
;
462 *kernel_event
= local_kernel_event
;
467 free(filter_expression
);
469 free(local_kernel_event
);
475 * Allocate and initialize a kernel metadata.
477 * Return pointer to structure or NULL.
479 struct ltt_kernel_metadata
*trace_kernel_create_metadata(void)
482 struct ltt_kernel_metadata
*lkm
;
483 struct lttng_channel
*chan
;
485 lkm
= zmalloc(sizeof(struct ltt_kernel_metadata
));
486 chan
= zmalloc(sizeof(struct lttng_channel
));
487 if (lkm
== NULL
|| chan
== NULL
) {
488 PERROR("kernel metadata zmalloc");
493 chan
->name
, DEFAULT_METADATA_NAME
, sizeof(chan
->name
));
495 ERR("Failed to initialize metadata channel name to `%s`",
496 DEFAULT_METADATA_NAME
);
500 /* Set default attributes */
501 chan
->attr
.overwrite
= DEFAULT_METADATA_OVERWRITE
;
502 chan
->attr
.subbuf_size
= default_get_metadata_subbuf_size();
503 chan
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
504 chan
->attr
.switch_timer_interval
= DEFAULT_METADATA_SWITCH_TIMER
;
505 chan
->attr
.read_timer_interval
= DEFAULT_METADATA_READ_TIMER
;;
509 * The metadata channel of kernel sessions must use the "mmap"
510 * back-end since the consumer daemon accumulates complete
511 * metadata units before sending them to the relay daemon in
512 * live mode. The consumer daemon also needs to extract the contents
513 * of the metadata cache when computing a rotation position.
515 * In both cases, it is not possible to rely on the splice
516 * back-end as the consumer daemon may need to accumulate more
517 * content than can be backed by the ring buffer's underlying
520 chan
->attr
.output
= LTTNG_EVENT_MMAP
;
521 chan
->attr
.tracefile_size
= 0;
522 chan
->attr
.tracefile_count
= 0;
523 chan
->attr
.live_timer_interval
= 0;
538 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
541 * Return pointer to structure or NULL.
543 struct ltt_kernel_stream
*trace_kernel_create_stream(const char *name
,
547 struct ltt_kernel_stream
*lks
;
551 lks
= zmalloc(sizeof(struct ltt_kernel_stream
));
553 PERROR("kernel stream zmalloc");
558 ret
= snprintf(lks
->name
, sizeof(lks
->name
), "%s_%u", name
, count
);
560 PERROR("snprintf stream name");
563 lks
->name
[sizeof(lks
->name
) - 1] = '\0';
577 * Cleanup kernel stream structure.
579 void trace_kernel_destroy_stream(struct ltt_kernel_stream
*stream
)
583 DBG("[trace] Closing stream fd %d", stream
->fd
);
584 /* Close kernel fd */
585 if (stream
->fd
>= 0) {
588 ret
= close(stream
->fd
);
593 /* Remove from stream list */
594 cds_list_del(&stream
->list
);
600 * Cleanup kernel event structure.
602 void trace_kernel_destroy_event(struct ltt_kernel_event
*event
)
606 if (event
->fd
>= 0) {
609 DBG("[trace] Closing event fd %d", event
->fd
);
610 /* Close kernel fd */
611 ret
= close(event
->fd
);
616 DBG("[trace] Tearing down event (no associated fd)");
619 /* Remove from event list */
620 cds_list_del(&event
->list
);
622 free(event
->filter_expression
);
630 * Cleanup kernel context structure.
632 void trace_kernel_destroy_context(struct ltt_kernel_context
*ctx
)
637 cds_list_del(&ctx
->list
);
643 * Cleanup kernel channel structure.
645 void trace_kernel_destroy_channel(struct ltt_kernel_channel
*channel
)
647 struct ltt_kernel_stream
*stream
, *stmp
;
648 struct ltt_kernel_event
*event
, *etmp
;
649 struct ltt_kernel_context
*ctx
, *ctmp
;
651 enum lttng_error_code status
;
655 DBG("[trace] Closing channel fd %d", channel
->fd
);
656 /* Close kernel fd */
657 if (channel
->fd
>= 0) {
658 ret
= close(channel
->fd
);
664 /* For each stream in the channel list */
665 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->stream_list
.head
, list
) {
666 trace_kernel_destroy_stream(stream
);
669 /* For each event in the channel list */
670 cds_list_for_each_entry_safe(event
, etmp
, &channel
->events_list
.head
, list
) {
671 trace_kernel_destroy_event(event
);
674 /* For each context in the channel list */
675 cds_list_for_each_entry_safe(ctx
, ctmp
, &channel
->ctx_list
, list
) {
676 trace_kernel_destroy_context(ctx
);
679 /* Remove from channel list */
680 cds_list_del(&channel
->list
);
682 if (notification_thread_handle
683 && channel
->published_to_notification_thread
) {
684 status
= notification_thread_command_remove_channel(
685 notification_thread_handle
,
686 channel
->key
, LTTNG_DOMAIN_KERNEL
);
687 assert(status
== LTTNG_OK
);
689 free(channel
->channel
->attr
.extended
.ptr
);
690 free(channel
->channel
);
695 * Cleanup kernel metadata structure.
697 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata
*metadata
)
701 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
702 /* Close kernel fd */
703 if (metadata
->fd
>= 0) {
706 ret
= close(metadata
->fd
);
712 free(metadata
->conf
);
717 * Cleanup kernel session structure
719 * Should *NOT* be called with RCU read-side lock held.
721 void trace_kernel_destroy_session(struct ltt_kernel_session
*session
)
723 struct ltt_kernel_channel
*channel
, *ctmp
;
728 DBG("[trace] Closing session fd %d", session
->fd
);
729 /* Close kernel fds */
730 if (session
->fd
>= 0) {
731 ret
= close(session
->fd
);
737 if (session
->metadata_stream_fd
>= 0) {
738 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
739 ret
= close(session
->metadata_stream_fd
);
745 if (session
->metadata
!= NULL
) {
746 trace_kernel_destroy_metadata(session
->metadata
);
749 cds_list_for_each_entry_safe(channel
, ctmp
, &session
->channel_list
.head
, list
) {
750 trace_kernel_destroy_channel(channel
);
754 /* Free elements needed by destroy notifiers. */
755 void trace_kernel_free_session(struct ltt_kernel_session
*session
)
757 /* Wipe consumer output object */
758 consumer_output_put(session
->consumer
);
760 process_attr_tracker_destroy(session
->tracker_pid
);
761 process_attr_tracker_destroy(session
->tracker_vpid
);
762 process_attr_tracker_destroy(session
->tracker_uid
);
763 process_attr_tracker_destroy(session
->tracker_vuid
);
764 process_attr_tracker_destroy(session
->tracker_gid
);
765 process_attr_tracker_destroy(session
->tracker_vgid
);