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
->tracker_list_pid
= lttng_tracker_list_create();
168 if (!lks
->tracker_list_pid
) {
171 lks
->tracker_list_vpid
= lttng_tracker_list_create();
172 if (!lks
->tracker_list_vpid
) {
175 lks
->tracker_list_uid
= lttng_tracker_list_create();
176 if (!lks
->tracker_list_uid
) {
179 lks
->tracker_list_vuid
= lttng_tracker_list_create();
180 if (!lks
->tracker_list_vuid
) {
183 lks
->tracker_list_gid
= lttng_tracker_list_create();
184 if (!lks
->tracker_list_gid
) {
187 lks
->tracker_list_vgid
= lttng_tracker_list_create();
188 if (!lks
->tracker_list_vgid
) {
191 lks
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
192 if (lks
->consumer
== NULL
) {
199 lttng_tracker_list_destroy(lks
->tracker_list_pid
);
200 lttng_tracker_list_destroy(lks
->tracker_list_vpid
);
201 lttng_tracker_list_destroy(lks
->tracker_list_uid
);
202 lttng_tracker_list_destroy(lks
->tracker_list_vuid
);
203 lttng_tracker_list_destroy(lks
->tracker_list_gid
);
204 lttng_tracker_list_destroy(lks
->tracker_list_vgid
);
212 * Allocate and initialize a kernel channel data structure.
214 * Return pointer to structure or NULL.
216 struct ltt_kernel_channel
*trace_kernel_create_channel(
217 struct lttng_channel
*chan
)
219 struct ltt_kernel_channel
*lkc
;
220 struct lttng_channel_extended
*extended
= NULL
;
224 lkc
= zmalloc(sizeof(struct ltt_kernel_channel
));
226 PERROR("ltt_kernel_channel zmalloc");
230 lkc
->channel
= zmalloc(sizeof(struct lttng_channel
));
231 if (lkc
->channel
== NULL
) {
232 PERROR("lttng_channel zmalloc");
236 extended
= zmalloc(sizeof(struct lttng_channel_extended
));
238 PERROR("lttng_channel_channel zmalloc");
241 memcpy(lkc
->channel
, chan
, sizeof(struct lttng_channel
));
242 memcpy(extended
, chan
->attr
.extended
.ptr
, sizeof(struct lttng_channel_extended
));
243 lkc
->channel
->attr
.extended
.ptr
= extended
;
247 * If we receive an empty string for channel name, it means the
248 * default channel name is requested.
250 if (chan
->name
[0] == '\0') {
251 strncpy(lkc
->channel
->name
, DEFAULT_CHANNEL_NAME
,
252 sizeof(lkc
->channel
->name
));
254 lkc
->channel
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
257 lkc
->stream_count
= 0;
258 lkc
->event_count
= 0;
260 lkc
->published_to_notification_thread
= false;
261 /* Init linked list */
262 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
263 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
264 CDS_INIT_LIST_HEAD(&lkc
->ctx_list
);
278 * Allocate and init a kernel context object.
280 * Return the allocated object or NULL on error.
282 struct ltt_kernel_context
*trace_kernel_create_context(
283 struct lttng_kernel_context
*ctx
)
285 struct ltt_kernel_context
*kctx
;
287 kctx
= zmalloc(sizeof(*kctx
));
289 PERROR("zmalloc kernel context");
294 memcpy(&kctx
->ctx
, ctx
, sizeof(kctx
->ctx
));
301 * Allocate and init a kernel context object from an existing kernel context
304 * Return the allocated object or NULL on error.
306 struct ltt_kernel_context
*trace_kernel_copy_context(
307 struct ltt_kernel_context
*kctx
)
309 struct ltt_kernel_context
*kctx_copy
;
312 kctx_copy
= zmalloc(sizeof(*kctx_copy
));
314 PERROR("zmalloc ltt_kernel_context");
318 memcpy(kctx_copy
, kctx
, sizeof(*kctx_copy
));
319 memset(&kctx_copy
->list
, 0, sizeof(kctx_copy
->list
));
326 * Allocate and initialize a kernel event. Set name and event type.
327 * We own filter_expression, and filter.
329 * Return pointer to structure or NULL.
331 enum lttng_error_code
trace_kernel_create_event(
332 struct lttng_event
*ev
, char *filter_expression
,
333 struct lttng_filter_bytecode
*filter
,
334 struct ltt_kernel_event
**kernel_event
)
336 enum lttng_error_code ret
;
337 struct lttng_kernel_event
*attr
;
338 struct ltt_kernel_event
*local_kernel_event
;
339 struct lttng_userspace_probe_location
*userspace_probe_location
= NULL
;
343 local_kernel_event
= zmalloc(sizeof(struct ltt_kernel_event
));
344 attr
= zmalloc(sizeof(struct lttng_kernel_event
));
345 if (local_kernel_event
== NULL
|| attr
== NULL
) {
346 PERROR("kernel event zmalloc");
347 ret
= LTTNG_ERR_NOMEM
;
352 case LTTNG_EVENT_PROBE
:
353 attr
->instrumentation
= LTTNG_KERNEL_KPROBE
;
354 attr
->u
.kprobe
.addr
= ev
->attr
.probe
.addr
;
355 attr
->u
.kprobe
.offset
= ev
->attr
.probe
.offset
;
356 strncpy(attr
->u
.kprobe
.symbol_name
,
357 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
358 attr
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
360 case LTTNG_EVENT_USERSPACE_PROBE
:
362 const struct lttng_userspace_probe_location
* location
= NULL
;
363 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
365 location
= lttng_event_get_userspace_probe_location(ev
);
367 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
372 * From this point on, the specific term 'uprobe' is used
373 * instead of the generic 'userspace probe' because it's the
374 * technology used at the moment for this instrumentation.
375 * LTTng currently implements userspace probes using uprobes.
376 * In the interactions with the kernel tracer, we use the
379 attr
->instrumentation
= LTTNG_KERNEL_UPROBE
;
382 * Only the elf lookup method is supported at the moment.
384 lookup
= lttng_userspace_probe_location_get_lookup_method(
387 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
392 * From the kernel tracer's perspective, all userspace probe
393 * event types are all the same: a file and an offset.
395 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
396 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
397 /* Get the file descriptor on the target binary. */
399 lttng_userspace_probe_location_function_get_binary_fd(location
);
402 * Save a reference to the probe location used during
403 * the listing of events. Close its FD since it won't
404 * be needed for listing.
406 userspace_probe_location
=
407 lttng_userspace_probe_location_copy(location
);
408 ret
= lttng_userspace_probe_location_function_set_binary_fd(
409 userspace_probe_location
, -1);
414 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
415 /* Get the file descriptor on the target binary. */
417 lttng_userspace_probe_location_tracepoint_get_binary_fd(location
);
420 * Save a reference to the probe location used during the listing of
421 * events. Close its FD since it won't be needed for listing.
423 userspace_probe_location
=
424 lttng_userspace_probe_location_copy(location
);
425 ret
= lttng_userspace_probe_location_tracepoint_set_binary_fd(
426 userspace_probe_location
, -1);
432 DBG("Unsupported lookup method type");
433 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
438 case LTTNG_EVENT_FUNCTION
:
439 attr
->instrumentation
= LTTNG_KERNEL_KRETPROBE
;
440 attr
->u
.kretprobe
.addr
= ev
->attr
.probe
.addr
;
441 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
442 strncpy(attr
->u
.kretprobe
.symbol_name
,
443 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
444 attr
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
446 case LTTNG_EVENT_FUNCTION_ENTRY
:
447 attr
->instrumentation
= LTTNG_KERNEL_FUNCTION
;
448 strncpy(attr
->u
.ftrace
.symbol_name
,
449 ev
->attr
.ftrace
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
450 attr
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
452 case LTTNG_EVENT_TRACEPOINT
:
453 attr
->instrumentation
= LTTNG_KERNEL_TRACEPOINT
;
455 case LTTNG_EVENT_SYSCALL
:
456 attr
->instrumentation
= LTTNG_KERNEL_SYSCALL
;
458 case LTTNG_EVENT_ALL
:
459 attr
->instrumentation
= LTTNG_KERNEL_ALL
;
462 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
463 ret
= LTTNG_ERR_INVALID
;
467 /* Copy event name */
468 strncpy(attr
->name
, ev
->name
, LTTNG_KERNEL_SYM_NAME_LEN
);
469 attr
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
471 /* Setting up a kernel event */
472 local_kernel_event
->fd
= -1;
473 local_kernel_event
->event
= attr
;
474 local_kernel_event
->enabled
= 1;
475 local_kernel_event
->filter_expression
= filter_expression
;
476 local_kernel_event
->filter
= filter
;
477 local_kernel_event
->userspace_probe_location
= userspace_probe_location
;
479 *kernel_event
= local_kernel_event
;
484 free(filter_expression
);
486 free(local_kernel_event
);
492 * Allocate and initialize a kernel metadata.
494 * Return pointer to structure or NULL.
496 struct ltt_kernel_metadata
*trace_kernel_create_metadata(void)
498 struct ltt_kernel_metadata
*lkm
;
499 struct lttng_channel
*chan
;
501 lkm
= zmalloc(sizeof(struct ltt_kernel_metadata
));
502 chan
= zmalloc(sizeof(struct lttng_channel
));
503 if (lkm
== NULL
|| chan
== NULL
) {
504 PERROR("kernel metadata zmalloc");
508 /* Set default attributes */
509 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
510 chan
->attr
.subbuf_size
= default_get_metadata_subbuf_size();
511 chan
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
512 chan
->attr
.switch_timer_interval
= DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
513 chan
->attr
.read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
514 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
529 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
532 * Return pointer to structure or NULL.
534 struct ltt_kernel_stream
*trace_kernel_create_stream(const char *name
,
538 struct ltt_kernel_stream
*lks
;
542 lks
= zmalloc(sizeof(struct ltt_kernel_stream
));
544 PERROR("kernel stream zmalloc");
549 ret
= snprintf(lks
->name
, sizeof(lks
->name
), "%s_%u", name
, count
);
551 PERROR("snprintf stream name");
554 lks
->name
[sizeof(lks
->name
) - 1] = '\0';
568 * Cleanup kernel stream structure.
570 void trace_kernel_destroy_stream(struct ltt_kernel_stream
*stream
)
574 DBG("[trace] Closing stream fd %d", stream
->fd
);
575 /* Close kernel fd */
576 if (stream
->fd
>= 0) {
579 ret
= close(stream
->fd
);
584 /* Remove from stream list */
585 cds_list_del(&stream
->list
);
591 * Cleanup kernel event structure.
593 void trace_kernel_destroy_event(struct ltt_kernel_event
*event
)
597 if (event
->fd
>= 0) {
600 DBG("[trace] Closing event fd %d", event
->fd
);
601 /* Close kernel fd */
602 ret
= close(event
->fd
);
607 DBG("[trace] Tearing down event (no associated fd)");
610 /* Remove from event list */
611 cds_list_del(&event
->list
);
613 free(event
->filter_expression
);
621 * Cleanup kernel context structure.
623 void trace_kernel_destroy_context(struct ltt_kernel_context
*ctx
)
628 cds_list_del(&ctx
->list
);
634 * Cleanup kernel channel structure.
636 void trace_kernel_destroy_channel(struct ltt_kernel_channel
*channel
)
638 struct ltt_kernel_stream
*stream
, *stmp
;
639 struct ltt_kernel_event
*event
, *etmp
;
640 struct ltt_kernel_context
*ctx
, *ctmp
;
642 enum lttng_error_code status
;
646 DBG("[trace] Closing channel fd %d", channel
->fd
);
647 /* Close kernel fd */
648 if (channel
->fd
>= 0) {
649 ret
= close(channel
->fd
);
655 /* For each stream in the channel list */
656 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->stream_list
.head
, list
) {
657 trace_kernel_destroy_stream(stream
);
660 /* For each event in the channel list */
661 cds_list_for_each_entry_safe(event
, etmp
, &channel
->events_list
.head
, list
) {
662 trace_kernel_destroy_event(event
);
665 /* For each context in the channel list */
666 cds_list_for_each_entry_safe(ctx
, ctmp
, &channel
->ctx_list
, list
) {
667 trace_kernel_destroy_context(ctx
);
670 /* Remove from channel list */
671 cds_list_del(&channel
->list
);
673 if (notification_thread_handle
674 && channel
->published_to_notification_thread
) {
675 status
= notification_thread_command_remove_channel(
676 notification_thread_handle
,
677 channel
->key
, LTTNG_DOMAIN_KERNEL
);
678 assert(status
== LTTNG_OK
);
680 free(channel
->channel
->attr
.extended
.ptr
);
681 free(channel
->channel
);
686 * Cleanup kernel metadata structure.
688 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata
*metadata
)
692 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
693 /* Close kernel fd */
694 if (metadata
->fd
>= 0) {
697 ret
= close(metadata
->fd
);
703 free(metadata
->conf
);
708 * Cleanup kernel session structure
710 * Should *NOT* be called with RCU read-side lock held.
712 void trace_kernel_destroy_session(struct ltt_kernel_session
*session
)
714 struct ltt_kernel_channel
*channel
, *ctmp
;
719 DBG("[trace] Closing session fd %d", session
->fd
);
720 /* Close kernel fds */
721 if (session
->fd
>= 0) {
722 ret
= close(session
->fd
);
728 if (session
->metadata_stream_fd
>= 0) {
729 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
730 ret
= close(session
->metadata_stream_fd
);
736 if (session
->metadata
!= NULL
) {
737 trace_kernel_destroy_metadata(session
->metadata
);
740 cds_list_for_each_entry_safe(channel
, ctmp
, &session
->channel_list
.head
, list
) {
741 trace_kernel_destroy_channel(channel
);
745 /* Free elements needed by destroy notifiers. */
746 void trace_kernel_free_session(struct ltt_kernel_session
*session
)
748 /* Wipe consumer output object */
749 consumer_output_put(session
->consumer
);
751 lttng_tracker_list_destroy(session
->tracker_list_pid
);
752 lttng_tracker_list_destroy(session
->tracker_list_vpid
);
753 lttng_tracker_list_destroy(session
->tracker_list_uid
);
754 lttng_tracker_list_destroy(session
->tracker_list_vuid
);
755 lttng_tracker_list_destroy(session
->tracker_list_gid
);
756 lttng_tracker_list_destroy(session
->tracker_list_vgid
);