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>
34 #include "trace-kernel.h"
35 #include "lttng-sessiond.h"
36 #include "notification-thread-commands.h"
39 * Find the channel name for the given kernel session.
41 struct ltt_kernel_channel
*trace_kernel_get_channel_by_name(
42 const char *name
, struct ltt_kernel_session
*session
)
44 struct ltt_kernel_channel
*chan
;
50 * If we receive an empty string for channel name, it means the
51 * default channel name is requested.
54 name
= DEFAULT_CHANNEL_NAME
;
56 DBG("Trying to find channel %s", name
);
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
);
69 * Find the event for the given channel.
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
)
76 struct ltt_kernel_event
*ev
;
82 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
83 if (type
!= LTTNG_EVENT_ALL
&& ev
->type
!= type
) {
86 if (strcmp(name
, ev
->event
->name
)) {
89 if ((ev
->filter
&& !filter
) || (!ev
->filter
&& filter
)) {
92 if (ev
->filter
&& filter
) {
93 if (ev
->filter
->len
!= filter
->len
||
94 memcmp(ev
->filter
->data
, filter
->data
,
103 DBG("Found event %s for channel %s", name
,
104 channel
->channel
->name
);
112 * Find the event name for the given channel.
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
)
118 struct ltt_kernel_event
*ev
;
124 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
125 if (type
!= LTTNG_EVENT_ALL
&& ev
->type
!= type
) {
128 if (strcmp(name
, ev
->event
->name
)) {
135 DBG("Found event %s for channel %s", name
,
136 channel
->channel
->name
);
144 * Allocate and initialize a kernel session data structure.
146 * Return pointer to structure or NULL.
148 struct ltt_kernel_session
*trace_kernel_create_session(void)
150 struct ltt_kernel_session
*lks
= NULL
;
152 /* Allocate a new ltt kernel session */
153 lks
= zmalloc(sizeof(struct ltt_kernel_session
));
155 PERROR("create kernel session zmalloc");
159 /* Init data structure */
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
);
167 lks
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
168 if (lks
->consumer
== NULL
) {
182 * Allocate and initialize a kernel channel data structure.
184 * Return pointer to structure or NULL.
186 struct ltt_kernel_channel
*trace_kernel_create_channel(
187 struct lttng_channel
*chan
)
189 struct ltt_kernel_channel
*lkc
;
190 struct lttng_channel_extended
*extended
= NULL
;
194 lkc
= zmalloc(sizeof(struct ltt_kernel_channel
));
196 PERROR("ltt_kernel_channel zmalloc");
200 lkc
->channel
= zmalloc(sizeof(struct lttng_channel
));
201 if (lkc
->channel
== NULL
) {
202 PERROR("lttng_channel zmalloc");
206 extended
= zmalloc(sizeof(struct lttng_channel_extended
));
208 PERROR("lttng_channel_channel zmalloc");
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
;
217 * If we receive an empty string for channel name, it means the
218 * default channel name is requested.
220 if (chan
->name
[0] == '\0') {
221 strncpy(lkc
->channel
->name
, DEFAULT_CHANNEL_NAME
,
222 sizeof(lkc
->channel
->name
));
224 lkc
->channel
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
227 lkc
->stream_count
= 0;
228 lkc
->event_count
= 0;
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
);
248 * Allocate and init a kernel context object.
250 * Return the allocated object or NULL on error.
252 struct ltt_kernel_context
*trace_kernel_create_context(
253 struct lttng_kernel_context
*ctx
)
255 struct ltt_kernel_context
*kctx
;
257 kctx
= zmalloc(sizeof(*kctx
));
259 PERROR("zmalloc kernel context");
264 memcpy(&kctx
->ctx
, ctx
, sizeof(kctx
->ctx
));
271 * Allocate and init a kernel context object from an existing kernel context
274 * Return the allocated object or NULL on error.
276 struct ltt_kernel_context
*trace_kernel_copy_context(
277 struct ltt_kernel_context
*kctx
)
279 struct ltt_kernel_context
*kctx_copy
;
282 kctx_copy
= zmalloc(sizeof(*kctx_copy
));
284 PERROR("zmalloc ltt_kernel_context");
288 memcpy(kctx_copy
, kctx
, sizeof(*kctx_copy
));
289 memset(&kctx_copy
->list
, 0, sizeof(kctx_copy
->list
));
296 * Allocate and initialize a kernel event. Set name and event type.
297 * We own filter_expression, and filter.
299 * Return pointer to structure or NULL.
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
)
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
;
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
;
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';
330 case LTTNG_EVENT_USERSPACE_PROBE
:
332 const struct lttng_userspace_probe_location
* location
= NULL
;
333 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
335 location
= lttng_event_get_userspace_probe_location(ev
);
337 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
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
349 attr
->instrumentation
= LTTNG_KERNEL_UPROBE
;
352 * Only the elf lookup method is supported at the moment.
354 lookup
= lttng_userspace_probe_location_get_lookup_method(
357 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
362 * From the kernel tracer's perspective, all userspace probe
363 * event types are all the same: a file and an offset.
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. */
369 lttng_userspace_probe_location_function_get_binary_fd(location
);
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.
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);
384 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
385 /* Get the file descriptor on the target binary. */
387 lttng_userspace_probe_location_tracepoint_get_binary_fd(location
);
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.
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);
402 DBG("Unsupported lookup method type");
403 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
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';
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';
422 case LTTNG_EVENT_TRACEPOINT
:
423 attr
->instrumentation
= LTTNG_KERNEL_TRACEPOINT
;
425 case LTTNG_EVENT_SYSCALL
:
426 attr
->instrumentation
= LTTNG_KERNEL_SYSCALL
;
428 case LTTNG_EVENT_ALL
:
429 attr
->instrumentation
= LTTNG_KERNEL_ALL
;
432 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
433 ret
= LTTNG_ERR_INVALID
;
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';
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
;
449 *kernel_event
= local_kernel_event
;
454 free(filter_expression
);
456 free(local_kernel_event
);
462 * Allocate and initialize a kernel metadata.
464 * Return pointer to structure or NULL.
466 struct ltt_kernel_metadata
*trace_kernel_create_metadata(void)
468 struct ltt_kernel_metadata
*lkm
;
469 struct lttng_channel
*chan
;
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");
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
;
499 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
502 * Return pointer to structure or NULL.
504 struct ltt_kernel_stream
*trace_kernel_create_stream(const char *name
,
508 struct ltt_kernel_stream
*lks
;
512 lks
= zmalloc(sizeof(struct ltt_kernel_stream
));
514 PERROR("kernel stream zmalloc");
519 ret
= snprintf(lks
->name
, sizeof(lks
->name
), "%s_%u", name
, count
);
521 PERROR("snprintf stream name");
524 lks
->name
[sizeof(lks
->name
) - 1] = '\0';
538 * Cleanup kernel stream structure.
540 void trace_kernel_destroy_stream(struct ltt_kernel_stream
*stream
)
544 DBG("[trace] Closing stream fd %d", stream
->fd
);
545 /* Close kernel fd */
546 if (stream
->fd
>= 0) {
549 ret
= close(stream
->fd
);
554 /* Remove from stream list */
555 cds_list_del(&stream
->list
);
561 * Cleanup kernel event structure.
563 void trace_kernel_destroy_event(struct ltt_kernel_event
*event
)
567 if (event
->fd
>= 0) {
570 DBG("[trace] Closing event fd %d", event
->fd
);
571 /* Close kernel fd */
572 ret
= close(event
->fd
);
577 DBG("[trace] Tearing down event (no associated fd)");
580 /* Remove from event list */
581 cds_list_del(&event
->list
);
583 free(event
->filter_expression
);
591 * Cleanup kernel context structure.
593 void trace_kernel_destroy_context(struct ltt_kernel_context
*ctx
)
598 cds_list_del(&ctx
->list
);
604 * Cleanup kernel channel structure.
606 void trace_kernel_destroy_channel(struct ltt_kernel_channel
*channel
)
608 struct ltt_kernel_stream
*stream
, *stmp
;
609 struct ltt_kernel_event
*event
, *etmp
;
610 struct ltt_kernel_context
*ctx
, *ctmp
;
612 enum lttng_error_code status
;
616 DBG("[trace] Closing channel fd %d", channel
->fd
);
617 /* Close kernel fd */
618 if (channel
->fd
>= 0) {
619 ret
= close(channel
->fd
);
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
);
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
);
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
);
640 /* Remove from channel list */
641 cds_list_del(&channel
->list
);
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
);
650 free(channel
->channel
->attr
.extended
.ptr
);
651 free(channel
->channel
);
656 * Cleanup kernel metadata structure.
658 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata
*metadata
)
662 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
663 /* Close kernel fd */
664 if (metadata
->fd
>= 0) {
667 ret
= close(metadata
->fd
);
673 free(metadata
->conf
);
678 * Cleanup kernel session structure
680 * Should *NOT* be called with RCU read-side lock held.
682 void trace_kernel_destroy_session(struct ltt_kernel_session
*session
)
684 struct ltt_kernel_channel
*channel
, *ctmp
;
689 DBG("[trace] Closing session fd %d", session
->fd
);
690 /* Close kernel fds */
691 if (session
->fd
>= 0) {
692 ret
= close(session
->fd
);
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
);
706 if (session
->metadata
!= NULL
) {
707 trace_kernel_destroy_metadata(session
->metadata
);
710 cds_list_for_each_entry_safe(channel
, ctmp
, &session
->channel_list
.head
, list
) {
711 trace_kernel_destroy_channel(channel
);
715 /* Free elements needed by destroy notifiers. */
716 void trace_kernel_free_session(struct ltt_kernel_session
*session
)
718 /* Wipe consumer output object */
719 consumer_output_put(session
->consumer
);