2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
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.
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.
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.
24 #include <lttng/event.h>
25 #include <lttng/lttng-error.h>
26 #include <lttng/userspace-probe.h>
27 #include <lttng/userspace-probe-internal.h>
29 #include <common/common.h>
30 #include <common/defaults.h>
31 #include <common/trace-chunk.h>
32 #include <common/macros.h>
35 #include "trace-kernel.h"
36 #include "lttng-sessiond.h"
37 #include "notification-thread-commands.h"
40 * Find the channel name for the given kernel session.
42 struct ltt_kernel_channel
*trace_kernel_get_channel_by_name(
43 const char *name
, struct ltt_kernel_session
*session
)
45 struct ltt_kernel_channel
*chan
;
51 * If we receive an empty string for channel name, it means the
52 * default channel name is requested.
55 name
= DEFAULT_CHANNEL_NAME
;
57 DBG("Trying to find channel %s", name
);
59 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
60 if (strcmp(name
, chan
->channel
->name
) == 0) {
61 DBG("Found channel by name %s", name
);
70 * Find the event for the given channel.
72 struct ltt_kernel_event
*trace_kernel_find_event(
73 char *name
, struct ltt_kernel_channel
*channel
,
74 enum lttng_event_type type
,
75 struct lttng_filter_bytecode
*filter
)
77 struct ltt_kernel_event
*ev
;
83 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
84 if (type
!= LTTNG_EVENT_ALL
&& ev
->type
!= type
) {
87 if (strcmp(name
, ev
->event
->name
)) {
90 if ((ev
->filter
&& !filter
) || (!ev
->filter
&& filter
)) {
93 if (ev
->filter
&& filter
) {
94 if (ev
->filter
->len
!= filter
->len
||
95 memcmp(ev
->filter
->data
, filter
->data
,
104 DBG("Found event %s for channel %s", name
,
105 channel
->channel
->name
);
113 * Find the event name for the given channel.
115 struct ltt_kernel_event
*trace_kernel_get_event_by_name(
116 char *name
, struct ltt_kernel_channel
*channel
,
117 enum lttng_event_type type
)
119 struct ltt_kernel_event
*ev
;
125 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
126 if (type
!= LTTNG_EVENT_ALL
&& ev
->type
!= type
) {
129 if (strcmp(name
, ev
->event
->name
)) {
136 DBG("Found event %s for channel %s", name
,
137 channel
->channel
->name
);
145 * Allocate and initialize a kernel session data structure.
147 * Return pointer to structure or NULL.
149 struct ltt_kernel_session
*trace_kernel_create_session(void)
151 struct ltt_kernel_session
*lks
= NULL
;
153 /* Allocate a new ltt kernel session */
154 lks
= zmalloc(sizeof(struct ltt_kernel_session
));
156 PERROR("create kernel session zmalloc");
160 /* Init data structure */
162 lks
->metadata_stream_fd
= -1;
163 lks
->channel_count
= 0;
164 lks
->stream_count_global
= 0;
165 lks
->metadata
= NULL
;
166 CDS_INIT_LIST_HEAD(&lks
->channel_list
.head
);
168 lks
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
169 if (lks
->consumer
== NULL
) {
183 * Allocate and initialize a kernel channel data structure.
185 * Return pointer to structure or NULL.
187 struct ltt_kernel_channel
*trace_kernel_create_channel(
188 struct lttng_channel
*chan
)
190 struct ltt_kernel_channel
*lkc
;
191 struct lttng_channel_extended
*extended
= NULL
;
195 lkc
= zmalloc(sizeof(struct ltt_kernel_channel
));
197 PERROR("ltt_kernel_channel zmalloc");
201 lkc
->channel
= zmalloc(sizeof(struct lttng_channel
));
202 if (lkc
->channel
== NULL
) {
203 PERROR("lttng_channel zmalloc");
207 extended
= zmalloc(sizeof(struct lttng_channel_extended
));
209 PERROR("lttng_channel_channel zmalloc");
212 memcpy(lkc
->channel
, chan
, sizeof(struct lttng_channel
));
213 memcpy(extended
, chan
->attr
.extended
.ptr
, sizeof(struct lttng_channel_extended
));
214 lkc
->channel
->attr
.extended
.ptr
= extended
;
218 * If we receive an empty string for channel name, it means the
219 * default channel name is requested.
221 if (chan
->name
[0] == '\0') {
222 strncpy(lkc
->channel
->name
, DEFAULT_CHANNEL_NAME
,
223 sizeof(lkc
->channel
->name
));
225 lkc
->channel
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
228 lkc
->stream_count
= 0;
229 lkc
->event_count
= 0;
231 lkc
->published_to_notification_thread
= false;
232 /* Init linked list */
233 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
234 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
235 CDS_INIT_LIST_HEAD(&lkc
->ctx_list
);
249 * Allocate and init a kernel context object.
251 * Return the allocated object or NULL on error.
253 struct ltt_kernel_context
*trace_kernel_create_context(
254 struct lttng_kernel_context
*ctx
)
256 struct ltt_kernel_context
*kctx
;
258 kctx
= zmalloc(sizeof(*kctx
));
260 PERROR("zmalloc kernel context");
265 memcpy(&kctx
->ctx
, ctx
, sizeof(kctx
->ctx
));
272 * Allocate and init a kernel context object from an existing kernel context
275 * Return the allocated object or NULL on error.
277 struct ltt_kernel_context
*trace_kernel_copy_context(
278 struct ltt_kernel_context
*kctx
)
280 struct ltt_kernel_context
*kctx_copy
;
283 kctx_copy
= zmalloc(sizeof(*kctx_copy
));
285 PERROR("zmalloc ltt_kernel_context");
289 memcpy(kctx_copy
, kctx
, sizeof(*kctx_copy
));
290 memset(&kctx_copy
->list
, 0, sizeof(kctx_copy
->list
));
297 * Allocate and initialize a kernel event. Set name and event type.
298 * We own filter_expression, and filter.
300 * Return pointer to structure or NULL.
302 enum lttng_error_code
trace_kernel_create_event(
303 struct lttng_event
*ev
, char *filter_expression
,
304 struct lttng_filter_bytecode
*filter
,
305 struct ltt_kernel_event
**kernel_event
)
307 enum lttng_error_code ret
;
308 struct lttng_kernel_event
*attr
;
309 struct ltt_kernel_event
*local_kernel_event
;
310 struct lttng_userspace_probe_location
*userspace_probe_location
= NULL
;
314 local_kernel_event
= zmalloc(sizeof(struct ltt_kernel_event
));
315 attr
= zmalloc(sizeof(struct lttng_kernel_event
));
316 if (local_kernel_event
== NULL
|| attr
== NULL
) {
317 PERROR("kernel event zmalloc");
318 ret
= LTTNG_ERR_NOMEM
;
323 case LTTNG_EVENT_PROBE
:
324 attr
->instrumentation
= LTTNG_KERNEL_KPROBE
;
325 attr
->u
.kprobe
.addr
= ev
->attr
.probe
.addr
;
326 attr
->u
.kprobe
.offset
= ev
->attr
.probe
.offset
;
327 strncpy(attr
->u
.kprobe
.symbol_name
,
328 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
329 attr
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
331 case LTTNG_EVENT_USERSPACE_PROBE
:
333 const struct lttng_userspace_probe_location
* location
= NULL
;
334 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
336 location
= lttng_event_get_userspace_probe_location(ev
);
338 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
343 * From this point on, the specific term 'uprobe' is used
344 * instead of the generic 'userspace probe' because it's the
345 * technology used at the moment for this instrumentation.
346 * LTTng currently implements userspace probes using uprobes.
347 * In the interactions with the kernel tracer, we use the
350 attr
->instrumentation
= LTTNG_KERNEL_UPROBE
;
353 * Only the elf lookup method is supported at the moment.
355 lookup
= lttng_userspace_probe_location_get_lookup_method(
358 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
363 * From the kernel tracer's perspective, all userspace probe
364 * event types are all the same: a file and an offset.
366 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
367 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
368 /* Get the file descriptor on the target binary. */
370 lttng_userspace_probe_location_function_get_binary_fd(location
);
373 * Save a reference to the probe location used during
374 * the listing of events. Close its FD since it won't
375 * be needed for listing.
377 userspace_probe_location
=
378 lttng_userspace_probe_location_copy(location
);
379 ret
= lttng_userspace_probe_location_function_set_binary_fd(
380 userspace_probe_location
, -1);
385 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
386 /* Get the file descriptor on the target binary. */
388 lttng_userspace_probe_location_tracepoint_get_binary_fd(location
);
391 * Save a reference to the probe location used during the listing of
392 * events. Close its FD since it won't be needed for listing.
394 userspace_probe_location
=
395 lttng_userspace_probe_location_copy(location
);
396 ret
= lttng_userspace_probe_location_tracepoint_set_binary_fd(
397 userspace_probe_location
, -1);
403 DBG("Unsupported lookup method type");
404 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
409 case LTTNG_EVENT_FUNCTION
:
410 attr
->instrumentation
= LTTNG_KERNEL_KRETPROBE
;
411 attr
->u
.kretprobe
.addr
= ev
->attr
.probe
.addr
;
412 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
413 strncpy(attr
->u
.kretprobe
.symbol_name
,
414 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
415 attr
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
417 case LTTNG_EVENT_FUNCTION_ENTRY
:
418 attr
->instrumentation
= LTTNG_KERNEL_FUNCTION
;
419 strncpy(attr
->u
.ftrace
.symbol_name
,
420 ev
->attr
.ftrace
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
421 attr
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
423 case LTTNG_EVENT_TRACEPOINT
:
424 attr
->instrumentation
= LTTNG_KERNEL_TRACEPOINT
;
426 case LTTNG_EVENT_SYSCALL
:
427 attr
->instrumentation
= LTTNG_KERNEL_SYSCALL
;
429 case LTTNG_EVENT_ALL
:
430 attr
->instrumentation
= LTTNG_KERNEL_ALL
;
433 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
434 ret
= LTTNG_ERR_INVALID
;
438 /* Copy event name */
439 strncpy(attr
->name
, ev
->name
, LTTNG_KERNEL_SYM_NAME_LEN
);
440 attr
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
442 /* Setting up a kernel event */
443 local_kernel_event
->fd
= -1;
444 local_kernel_event
->event
= attr
;
445 local_kernel_event
->enabled
= 1;
446 local_kernel_event
->filter_expression
= filter_expression
;
447 local_kernel_event
->filter
= filter
;
448 local_kernel_event
->userspace_probe_location
= userspace_probe_location
;
450 *kernel_event
= local_kernel_event
;
455 free(filter_expression
);
457 free(local_kernel_event
);
463 * Allocate and initialize a kernel metadata.
465 * Return pointer to structure or NULL.
467 struct ltt_kernel_metadata
*trace_kernel_create_metadata(void)
470 struct ltt_kernel_metadata
*lkm
;
471 struct lttng_channel
*chan
;
473 lkm
= zmalloc(sizeof(struct ltt_kernel_metadata
));
474 chan
= zmalloc(sizeof(struct lttng_channel
));
475 if (lkm
== NULL
|| chan
== NULL
) {
476 PERROR("kernel metadata zmalloc");
481 chan
->name
, DEFAULT_METADATA_NAME
, sizeof(chan
->name
));
483 ERR("Failed to initialize metadata channel name to `%s`",
484 DEFAULT_METADATA_NAME
);
488 /* Set default attributes */
489 chan
->attr
.overwrite
= DEFAULT_METADATA_OVERWRITE
;
490 chan
->attr
.subbuf_size
= default_get_metadata_subbuf_size();
491 chan
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
492 chan
->attr
.switch_timer_interval
= DEFAULT_METADATA_SWITCH_TIMER
;
493 chan
->attr
.read_timer_interval
= DEFAULT_METADATA_READ_TIMER
;;
497 * The metadata channel of kernel sessions must use the "mmap"
498 * back-end since the consumer daemon accumulates complete
499 * metadata units before sending them to the relay daemon in
500 * live mode. The consumer daemon also needs to extract the contents
501 * of the metadata cache when computing a rotation position.
503 * In both cases, it is not possible to rely on the splice
504 * back-end as the consumer daemon may need to accumulate more
505 * content than can be backed by the ring buffer's underlying
508 chan
->attr
.output
= LTTNG_EVENT_MMAP
;
509 chan
->attr
.tracefile_size
= 0;
510 chan
->attr
.tracefile_count
= 0;
511 chan
->attr
.live_timer_interval
= 0;
526 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
529 * Return pointer to structure or NULL.
531 struct ltt_kernel_stream
*trace_kernel_create_stream(const char *name
,
535 struct ltt_kernel_stream
*lks
;
539 lks
= zmalloc(sizeof(struct ltt_kernel_stream
));
541 PERROR("kernel stream zmalloc");
546 ret
= snprintf(lks
->name
, sizeof(lks
->name
), "%s_%u", name
, count
);
548 PERROR("snprintf stream name");
551 lks
->name
[sizeof(lks
->name
) - 1] = '\0';
565 * Cleanup kernel stream structure.
567 void trace_kernel_destroy_stream(struct ltt_kernel_stream
*stream
)
571 DBG("[trace] Closing stream fd %d", stream
->fd
);
572 /* Close kernel fd */
573 if (stream
->fd
>= 0) {
576 ret
= close(stream
->fd
);
581 /* Remove from stream list */
582 cds_list_del(&stream
->list
);
588 * Cleanup kernel event structure.
590 void trace_kernel_destroy_event(struct ltt_kernel_event
*event
)
594 if (event
->fd
>= 0) {
597 DBG("[trace] Closing event fd %d", event
->fd
);
598 /* Close kernel fd */
599 ret
= close(event
->fd
);
604 DBG("[trace] Tearing down event (no associated fd)");
607 /* Remove from event list */
608 cds_list_del(&event
->list
);
610 free(event
->filter_expression
);
618 * Cleanup kernel context structure.
620 void trace_kernel_destroy_context(struct ltt_kernel_context
*ctx
)
625 cds_list_del(&ctx
->list
);
631 * Cleanup kernel channel structure.
633 void trace_kernel_destroy_channel(struct ltt_kernel_channel
*channel
)
635 struct ltt_kernel_stream
*stream
, *stmp
;
636 struct ltt_kernel_event
*event
, *etmp
;
637 struct ltt_kernel_context
*ctx
, *ctmp
;
639 enum lttng_error_code status
;
643 DBG("[trace] Closing channel fd %d", channel
->fd
);
644 /* Close kernel fd */
645 if (channel
->fd
>= 0) {
646 ret
= close(channel
->fd
);
652 /* For each stream in the channel list */
653 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->stream_list
.head
, list
) {
654 trace_kernel_destroy_stream(stream
);
657 /* For each event in the channel list */
658 cds_list_for_each_entry_safe(event
, etmp
, &channel
->events_list
.head
, list
) {
659 trace_kernel_destroy_event(event
);
662 /* For each context in the channel list */
663 cds_list_for_each_entry_safe(ctx
, ctmp
, &channel
->ctx_list
, list
) {
664 trace_kernel_destroy_context(ctx
);
667 /* Remove from channel list */
668 cds_list_del(&channel
->list
);
670 if (notification_thread_handle
671 && channel
->published_to_notification_thread
) {
672 status
= notification_thread_command_remove_channel(
673 notification_thread_handle
,
674 channel
->key
, LTTNG_DOMAIN_KERNEL
);
675 assert(status
== LTTNG_OK
);
677 free(channel
->channel
->attr
.extended
.ptr
);
678 free(channel
->channel
);
683 * Cleanup kernel metadata structure.
685 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata
*metadata
)
689 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
690 /* Close kernel fd */
691 if (metadata
->fd
>= 0) {
694 ret
= close(metadata
->fd
);
700 free(metadata
->conf
);
705 * Cleanup kernel session structure
707 * Should *NOT* be called with RCU read-side lock held.
709 void trace_kernel_destroy_session(struct ltt_kernel_session
*session
)
711 struct ltt_kernel_channel
*channel
, *ctmp
;
716 DBG("[trace] Closing session fd %d", session
->fd
);
717 /* Close kernel fds */
718 if (session
->fd
>= 0) {
719 ret
= close(session
->fd
);
725 if (session
->metadata_stream_fd
>= 0) {
726 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
727 ret
= close(session
->metadata_stream_fd
);
733 if (session
->metadata
!= NULL
) {
734 trace_kernel_destroy_metadata(session
->metadata
);
737 cds_list_for_each_entry_safe(channel
, ctmp
, &session
->channel_list
.head
, list
) {
738 trace_kernel_destroy_channel(channel
);
742 /* Free elements needed by destroy notifiers. */
743 void trace_kernel_free_session(struct ltt_kernel_session
*session
)
745 /* Wipe consumer output object */
746 consumer_output_put(session
->consumer
);