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>
24 #include "trace-kernel.h"
25 #include "lttng-sessiond.h"
26 #include "notification-thread-commands.h"
29 * Find the channel name for the given kernel session.
31 struct ltt_kernel_channel
*trace_kernel_get_channel_by_name(
32 const char *name
, struct ltt_kernel_session
*session
)
34 struct ltt_kernel_channel
*chan
;
40 * If we receive an empty string for channel name, it means the
41 * default channel name is requested.
44 name
= DEFAULT_CHANNEL_NAME
;
46 DBG("Trying to find channel %s", name
);
48 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
49 if (strcmp(name
, chan
->channel
->name
) == 0) {
50 DBG("Found channel by name %s", name
);
59 * Find the event for the given channel.
61 struct ltt_kernel_event
*trace_kernel_find_event(
62 char *name
, struct ltt_kernel_channel
*channel
,
63 enum lttng_event_type type
,
64 struct lttng_filter_bytecode
*filter
)
66 struct ltt_kernel_event
*ev
;
72 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
73 if (type
!= LTTNG_EVENT_ALL
&& ev
->type
!= type
) {
76 if (strcmp(name
, ev
->event
->name
)) {
79 if ((ev
->filter
&& !filter
) || (!ev
->filter
&& filter
)) {
82 if (ev
->filter
&& filter
) {
83 if (ev
->filter
->len
!= filter
->len
||
84 memcmp(ev
->filter
->data
, filter
->data
,
93 DBG("Found event %s for channel %s", name
,
94 channel
->channel
->name
);
102 * Find the event name for the given channel.
104 struct ltt_kernel_event
*trace_kernel_get_event_by_name(
105 char *name
, struct ltt_kernel_channel
*channel
,
106 enum lttng_event_type type
)
108 struct ltt_kernel_event
*ev
;
114 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
115 if (type
!= LTTNG_EVENT_ALL
&& ev
->type
!= type
) {
118 if (strcmp(name
, ev
->event
->name
)) {
125 DBG("Found event %s for channel %s", name
,
126 channel
->channel
->name
);
134 * Allocate and initialize a kernel session data structure.
136 * Return pointer to structure or NULL.
138 struct ltt_kernel_session
*trace_kernel_create_session(void)
140 struct ltt_kernel_session
*lks
= NULL
;
142 /* Allocate a new ltt kernel session */
143 lks
= zmalloc(sizeof(struct ltt_kernel_session
));
145 PERROR("create kernel session zmalloc");
149 /* Init data structure */
151 lks
->metadata_stream_fd
= -1;
152 lks
->channel_count
= 0;
153 lks
->stream_count_global
= 0;
154 lks
->metadata
= NULL
;
155 CDS_INIT_LIST_HEAD(&lks
->channel_list
.head
);
157 lks
->tracker_list_pid
= lttng_tracker_list_create();
158 if (!lks
->tracker_list_pid
) {
161 lks
->tracker_list_vpid
= lttng_tracker_list_create();
162 if (!lks
->tracker_list_vpid
) {
165 lks
->tracker_list_uid
= lttng_tracker_list_create();
166 if (!lks
->tracker_list_uid
) {
169 lks
->tracker_list_vuid
= lttng_tracker_list_create();
170 if (!lks
->tracker_list_vuid
) {
173 lks
->tracker_list_gid
= lttng_tracker_list_create();
174 if (!lks
->tracker_list_gid
) {
177 lks
->tracker_list_vgid
= lttng_tracker_list_create();
178 if (!lks
->tracker_list_vgid
) {
181 lks
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
182 if (lks
->consumer
== NULL
) {
189 lttng_tracker_list_destroy(lks
->tracker_list_pid
);
190 lttng_tracker_list_destroy(lks
->tracker_list_vpid
);
191 lttng_tracker_list_destroy(lks
->tracker_list_uid
);
192 lttng_tracker_list_destroy(lks
->tracker_list_vuid
);
193 lttng_tracker_list_destroy(lks
->tracker_list_gid
);
194 lttng_tracker_list_destroy(lks
->tracker_list_vgid
);
202 * Allocate and initialize a kernel channel data structure.
204 * Return pointer to structure or NULL.
206 struct ltt_kernel_channel
*trace_kernel_create_channel(
207 struct lttng_channel
*chan
)
209 struct ltt_kernel_channel
*lkc
;
210 struct lttng_channel_extended
*extended
= NULL
;
214 lkc
= zmalloc(sizeof(struct ltt_kernel_channel
));
216 PERROR("ltt_kernel_channel zmalloc");
220 lkc
->channel
= zmalloc(sizeof(struct lttng_channel
));
221 if (lkc
->channel
== NULL
) {
222 PERROR("lttng_channel zmalloc");
226 extended
= zmalloc(sizeof(struct lttng_channel_extended
));
228 PERROR("lttng_channel_channel zmalloc");
231 memcpy(lkc
->channel
, chan
, sizeof(struct lttng_channel
));
232 memcpy(extended
, chan
->attr
.extended
.ptr
, sizeof(struct lttng_channel_extended
));
233 lkc
->channel
->attr
.extended
.ptr
= extended
;
237 * If we receive an empty string for channel name, it means the
238 * default channel name is requested.
240 if (chan
->name
[0] == '\0') {
241 strncpy(lkc
->channel
->name
, DEFAULT_CHANNEL_NAME
,
242 sizeof(lkc
->channel
->name
));
244 lkc
->channel
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
247 lkc
->stream_count
= 0;
248 lkc
->event_count
= 0;
250 lkc
->published_to_notification_thread
= false;
251 /* Init linked list */
252 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
253 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
254 CDS_INIT_LIST_HEAD(&lkc
->ctx_list
);
268 * Allocate and init a kernel context object.
270 * Return the allocated object or NULL on error.
272 struct ltt_kernel_context
*trace_kernel_create_context(
273 struct lttng_kernel_context
*ctx
)
275 struct ltt_kernel_context
*kctx
;
277 kctx
= zmalloc(sizeof(*kctx
));
279 PERROR("zmalloc kernel context");
284 memcpy(&kctx
->ctx
, ctx
, sizeof(kctx
->ctx
));
291 * Allocate and init a kernel context object from an existing kernel context
294 * Return the allocated object or NULL on error.
296 struct ltt_kernel_context
*trace_kernel_copy_context(
297 struct ltt_kernel_context
*kctx
)
299 struct ltt_kernel_context
*kctx_copy
;
302 kctx_copy
= zmalloc(sizeof(*kctx_copy
));
304 PERROR("zmalloc ltt_kernel_context");
308 memcpy(kctx_copy
, kctx
, sizeof(*kctx_copy
));
309 memset(&kctx_copy
->list
, 0, sizeof(kctx_copy
->list
));
316 * Allocate and initialize a kernel event. Set name and event type.
317 * We own filter_expression, and filter.
319 * Return pointer to structure or NULL.
321 enum lttng_error_code
trace_kernel_create_event(
322 struct lttng_event
*ev
, char *filter_expression
,
323 struct lttng_filter_bytecode
*filter
,
324 struct ltt_kernel_event
**kernel_event
)
326 enum lttng_error_code ret
;
327 struct lttng_kernel_event
*attr
;
328 struct ltt_kernel_event
*local_kernel_event
;
329 struct lttng_userspace_probe_location
*userspace_probe_location
= NULL
;
333 local_kernel_event
= zmalloc(sizeof(struct ltt_kernel_event
));
334 attr
= zmalloc(sizeof(struct lttng_kernel_event
));
335 if (local_kernel_event
== NULL
|| attr
== NULL
) {
336 PERROR("kernel event zmalloc");
337 ret
= LTTNG_ERR_NOMEM
;
342 case LTTNG_EVENT_PROBE
:
343 attr
->instrumentation
= LTTNG_KERNEL_KPROBE
;
344 attr
->u
.kprobe
.addr
= ev
->attr
.probe
.addr
;
345 attr
->u
.kprobe
.offset
= ev
->attr
.probe
.offset
;
346 strncpy(attr
->u
.kprobe
.symbol_name
,
347 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
348 attr
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
350 case LTTNG_EVENT_USERSPACE_PROBE
:
352 const struct lttng_userspace_probe_location
* location
= NULL
;
353 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
355 location
= lttng_event_get_userspace_probe_location(ev
);
357 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
362 * From this point on, the specific term 'uprobe' is used
363 * instead of the generic 'userspace probe' because it's the
364 * technology used at the moment for this instrumentation.
365 * LTTng currently implements userspace probes using uprobes.
366 * In the interactions with the kernel tracer, we use the
369 attr
->instrumentation
= LTTNG_KERNEL_UPROBE
;
372 * Only the elf lookup method is supported at the moment.
374 lookup
= lttng_userspace_probe_location_get_lookup_method(
377 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
382 * From the kernel tracer's perspective, all userspace probe
383 * event types are all the same: a file and an offset.
385 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
386 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
387 /* Get the file descriptor on the target binary. */
389 lttng_userspace_probe_location_function_get_binary_fd(location
);
392 * Save a reference to the probe location used during
393 * the listing of events. Close its FD since it won't
394 * be needed for listing.
396 userspace_probe_location
=
397 lttng_userspace_probe_location_copy(location
);
398 ret
= lttng_userspace_probe_location_function_set_binary_fd(
399 userspace_probe_location
, -1);
404 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
405 /* Get the file descriptor on the target binary. */
407 lttng_userspace_probe_location_tracepoint_get_binary_fd(location
);
410 * Save a reference to the probe location used during the listing of
411 * events. Close its FD since it won't be needed for listing.
413 userspace_probe_location
=
414 lttng_userspace_probe_location_copy(location
);
415 ret
= lttng_userspace_probe_location_tracepoint_set_binary_fd(
416 userspace_probe_location
, -1);
422 DBG("Unsupported lookup method type");
423 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
428 case LTTNG_EVENT_FUNCTION
:
429 attr
->instrumentation
= LTTNG_KERNEL_KRETPROBE
;
430 attr
->u
.kretprobe
.addr
= ev
->attr
.probe
.addr
;
431 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
432 strncpy(attr
->u
.kretprobe
.symbol_name
,
433 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
434 attr
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
436 case LTTNG_EVENT_FUNCTION_ENTRY
:
437 attr
->instrumentation
= LTTNG_KERNEL_FUNCTION
;
438 strncpy(attr
->u
.ftrace
.symbol_name
,
439 ev
->attr
.ftrace
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
440 attr
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
442 case LTTNG_EVENT_TRACEPOINT
:
443 attr
->instrumentation
= LTTNG_KERNEL_TRACEPOINT
;
445 case LTTNG_EVENT_SYSCALL
:
446 attr
->instrumentation
= LTTNG_KERNEL_SYSCALL
;
448 case LTTNG_EVENT_ALL
:
449 attr
->instrumentation
= LTTNG_KERNEL_ALL
;
452 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
453 ret
= LTTNG_ERR_INVALID
;
457 /* Copy event name */
458 strncpy(attr
->name
, ev
->name
, LTTNG_KERNEL_SYM_NAME_LEN
);
459 attr
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
461 /* Setting up a kernel event */
462 local_kernel_event
->fd
= -1;
463 local_kernel_event
->event
= attr
;
464 local_kernel_event
->enabled
= 1;
465 local_kernel_event
->filter_expression
= filter_expression
;
466 local_kernel_event
->filter
= filter
;
467 local_kernel_event
->userspace_probe_location
= userspace_probe_location
;
469 *kernel_event
= local_kernel_event
;
474 free(filter_expression
);
476 free(local_kernel_event
);
482 * Allocate and initialize a kernel metadata.
484 * Return pointer to structure or NULL.
486 struct ltt_kernel_metadata
*trace_kernel_create_metadata(void)
488 struct ltt_kernel_metadata
*lkm
;
489 struct lttng_channel
*chan
;
491 lkm
= zmalloc(sizeof(struct ltt_kernel_metadata
));
492 chan
= zmalloc(sizeof(struct lttng_channel
));
493 if (lkm
== NULL
|| chan
== NULL
) {
494 PERROR("kernel metadata zmalloc");
498 /* Set default attributes */
499 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
500 chan
->attr
.subbuf_size
= default_get_metadata_subbuf_size();
501 chan
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
502 chan
->attr
.switch_timer_interval
= DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
503 chan
->attr
.read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
504 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
519 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
522 * Return pointer to structure or NULL.
524 struct ltt_kernel_stream
*trace_kernel_create_stream(const char *name
,
528 struct ltt_kernel_stream
*lks
;
532 lks
= zmalloc(sizeof(struct ltt_kernel_stream
));
534 PERROR("kernel stream zmalloc");
539 ret
= snprintf(lks
->name
, sizeof(lks
->name
), "%s_%u", name
, count
);
541 PERROR("snprintf stream name");
544 lks
->name
[sizeof(lks
->name
) - 1] = '\0';
558 * Cleanup kernel stream structure.
560 void trace_kernel_destroy_stream(struct ltt_kernel_stream
*stream
)
564 DBG("[trace] Closing stream fd %d", stream
->fd
);
565 /* Close kernel fd */
566 if (stream
->fd
>= 0) {
569 ret
= close(stream
->fd
);
574 /* Remove from stream list */
575 cds_list_del(&stream
->list
);
581 * Cleanup kernel event structure.
583 void trace_kernel_destroy_event(struct ltt_kernel_event
*event
)
587 if (event
->fd
>= 0) {
590 DBG("[trace] Closing event fd %d", event
->fd
);
591 /* Close kernel fd */
592 ret
= close(event
->fd
);
597 DBG("[trace] Tearing down event (no associated fd)");
600 /* Remove from event list */
601 cds_list_del(&event
->list
);
603 free(event
->filter_expression
);
611 * Cleanup kernel context structure.
613 void trace_kernel_destroy_context(struct ltt_kernel_context
*ctx
)
618 cds_list_del(&ctx
->list
);
624 * Cleanup kernel channel structure.
626 void trace_kernel_destroy_channel(struct ltt_kernel_channel
*channel
)
628 struct ltt_kernel_stream
*stream
, *stmp
;
629 struct ltt_kernel_event
*event
, *etmp
;
630 struct ltt_kernel_context
*ctx
, *ctmp
;
632 enum lttng_error_code status
;
636 DBG("[trace] Closing channel fd %d", channel
->fd
);
637 /* Close kernel fd */
638 if (channel
->fd
>= 0) {
639 ret
= close(channel
->fd
);
645 /* For each stream in the channel list */
646 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->stream_list
.head
, list
) {
647 trace_kernel_destroy_stream(stream
);
650 /* For each event in the channel list */
651 cds_list_for_each_entry_safe(event
, etmp
, &channel
->events_list
.head
, list
) {
652 trace_kernel_destroy_event(event
);
655 /* For each context in the channel list */
656 cds_list_for_each_entry_safe(ctx
, ctmp
, &channel
->ctx_list
, list
) {
657 trace_kernel_destroy_context(ctx
);
660 /* Remove from channel list */
661 cds_list_del(&channel
->list
);
663 if (notification_thread_handle
664 && channel
->published_to_notification_thread
) {
665 status
= notification_thread_command_remove_channel(
666 notification_thread_handle
,
667 channel
->key
, LTTNG_DOMAIN_KERNEL
);
668 assert(status
== LTTNG_OK
);
670 free(channel
->channel
->attr
.extended
.ptr
);
671 free(channel
->channel
);
676 * Cleanup kernel metadata structure.
678 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata
*metadata
)
682 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
683 /* Close kernel fd */
684 if (metadata
->fd
>= 0) {
687 ret
= close(metadata
->fd
);
693 free(metadata
->conf
);
698 * Cleanup kernel session structure
700 * Should *NOT* be called with RCU read-side lock held.
702 void trace_kernel_destroy_session(struct ltt_kernel_session
*session
)
704 struct ltt_kernel_channel
*channel
, *ctmp
;
709 DBG("[trace] Closing session fd %d", session
->fd
);
710 /* Close kernel fds */
711 if (session
->fd
>= 0) {
712 ret
= close(session
->fd
);
718 if (session
->metadata_stream_fd
>= 0) {
719 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
720 ret
= close(session
->metadata_stream_fd
);
726 if (session
->metadata
!= NULL
) {
727 trace_kernel_destroy_metadata(session
->metadata
);
730 cds_list_for_each_entry_safe(channel
, ctmp
, &session
->channel_list
.head
, list
) {
731 trace_kernel_destroy_channel(channel
);
735 /* Free elements needed by destroy notifiers. */
736 void trace_kernel_free_session(struct ltt_kernel_session
*session
)
738 /* Wipe consumer output object */
739 consumer_output_put(session
->consumer
);
741 lttng_tracker_list_destroy(session
->tracker_list_pid
);
742 lttng_tracker_list_destroy(session
->tracker_list_vpid
);
743 lttng_tracker_list_destroy(session
->tracker_list_uid
);
744 lttng_tracker_list_destroy(session
->tracker_list_vuid
);
745 lttng_tracker_list_destroy(session
->tracker_list_gid
);
746 lttng_tracker_list_destroy(session
->tracker_list_vgid
);