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>
33 #include "trace-kernel.h"
34 #include "lttng-sessiond.h"
35 #include "notification-thread-commands.h"
38 * Find the channel name for the given kernel session.
40 struct ltt_kernel_channel
*trace_kernel_get_channel_by_name(
41 char *name
, struct ltt_kernel_session
*session
)
43 struct ltt_kernel_channel
*chan
;
49 * If we receive an empty string for channel name, it means the
50 * default channel name is requested.
53 name
= DEFAULT_CHANNEL_NAME
;
55 DBG("Trying to find channel %s", name
);
57 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
58 if (strcmp(name
, chan
->channel
->name
) == 0) {
59 DBG("Found channel by name %s", name
);
68 * Find the event for the given channel.
70 struct ltt_kernel_event
*trace_kernel_find_event(
71 char *name
, struct ltt_kernel_channel
*channel
,
72 enum lttng_event_type type
,
73 struct lttng_filter_bytecode
*filter
)
75 struct ltt_kernel_event
*ev
;
81 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
82 if (type
!= LTTNG_EVENT_ALL
&& ev
->type
!= type
) {
85 if (strcmp(name
, ev
->event
->name
)) {
88 if ((ev
->filter
&& !filter
) || (!ev
->filter
&& filter
)) {
91 if (ev
->filter
&& filter
) {
92 if (ev
->filter
->len
!= filter
->len
||
93 memcmp(ev
->filter
->data
, filter
->data
,
102 DBG("Found event %s for channel %s", name
,
103 channel
->channel
->name
);
111 * Find the event name for the given channel.
113 struct ltt_kernel_event
*trace_kernel_get_event_by_name(
114 char *name
, struct ltt_kernel_channel
*channel
,
115 enum lttng_event_type type
)
117 struct ltt_kernel_event
*ev
;
123 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
124 if (type
!= LTTNG_EVENT_ALL
&& ev
->type
!= type
) {
127 if (strcmp(name
, ev
->event
->name
)) {
134 DBG("Found event %s for channel %s", name
,
135 channel
->channel
->name
);
143 * Allocate and initialize a kernel session data structure.
145 * Return pointer to structure or NULL.
147 struct ltt_kernel_session
*trace_kernel_create_session(void)
149 struct ltt_kernel_session
*lks
= NULL
;
151 /* Allocate a new ltt kernel session */
152 lks
= zmalloc(sizeof(struct ltt_kernel_session
));
154 PERROR("create kernel session zmalloc");
158 /* Init data structure */
160 lks
->metadata_stream_fd
= -1;
161 lks
->channel_count
= 0;
162 lks
->stream_count_global
= 0;
163 lks
->metadata
= NULL
;
164 CDS_INIT_LIST_HEAD(&lks
->channel_list
.head
);
166 lks
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
167 if (lks
->consumer
== NULL
) {
181 * Allocate and initialize a kernel channel data structure.
183 * Return pointer to structure or NULL.
185 struct ltt_kernel_channel
*trace_kernel_create_channel(
186 struct lttng_channel
*chan
)
188 struct ltt_kernel_channel
*lkc
;
189 struct lttng_channel_extended
*extended
= NULL
;
193 lkc
= zmalloc(sizeof(struct ltt_kernel_channel
));
195 PERROR("ltt_kernel_channel zmalloc");
199 lkc
->channel
= zmalloc(sizeof(struct lttng_channel
));
200 if (lkc
->channel
== NULL
) {
201 PERROR("lttng_channel zmalloc");
205 extended
= zmalloc(sizeof(struct lttng_channel_extended
));
207 PERROR("lttng_channel_channel zmalloc");
210 memcpy(lkc
->channel
, chan
, sizeof(struct lttng_channel
));
211 memcpy(extended
, chan
->attr
.extended
.ptr
, sizeof(struct lttng_channel_extended
));
212 lkc
->channel
->attr
.extended
.ptr
= extended
;
216 * If we receive an empty string for channel name, it means the
217 * default channel name is requested.
219 if (chan
->name
[0] == '\0') {
220 strncpy(lkc
->channel
->name
, DEFAULT_CHANNEL_NAME
,
221 sizeof(lkc
->channel
->name
));
223 lkc
->channel
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
226 lkc
->stream_count
= 0;
227 lkc
->event_count
= 0;
229 lkc
->published_to_notification_thread
= false;
230 /* Init linked list */
231 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
232 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
233 CDS_INIT_LIST_HEAD(&lkc
->ctx_list
);
247 * Allocate and init a kernel context object.
249 * Return the allocated object or NULL on error.
251 struct ltt_kernel_context
*trace_kernel_create_context(
252 struct lttng_kernel_context
*ctx
)
254 struct ltt_kernel_context
*kctx
;
256 kctx
= zmalloc(sizeof(*kctx
));
258 PERROR("zmalloc kernel context");
263 memcpy(&kctx
->ctx
, ctx
, sizeof(kctx
->ctx
));
270 * Allocate and init a kernel context object from an existing kernel context
273 * Return the allocated object or NULL on error.
275 struct ltt_kernel_context
*trace_kernel_copy_context(
276 struct ltt_kernel_context
*kctx
)
278 struct ltt_kernel_context
*kctx_copy
;
281 kctx_copy
= zmalloc(sizeof(*kctx_copy
));
283 PERROR("zmalloc ltt_kernel_context");
287 memcpy(kctx_copy
, kctx
, sizeof(*kctx_copy
));
288 memset(&kctx_copy
->list
, 0, sizeof(kctx_copy
->list
));
295 * Allocate and initialize a kernel event. Set name and event type.
296 * We own filter_expression, and filter.
298 * Return pointer to structure or NULL.
300 enum lttng_error_code
trace_kernel_create_event(
301 struct lttng_event
*ev
, char *filter_expression
,
302 struct lttng_filter_bytecode
*filter
,
303 struct ltt_kernel_event
**kernel_event
)
305 enum lttng_error_code ret
;
306 struct lttng_kernel_event
*attr
;
307 struct ltt_kernel_event
*local_kernel_event
;
308 struct lttng_userspace_probe_location
*userspace_probe_location
= NULL
;
312 local_kernel_event
= zmalloc(sizeof(struct ltt_kernel_event
));
313 attr
= zmalloc(sizeof(struct lttng_kernel_event
));
314 if (local_kernel_event
== NULL
|| attr
== NULL
) {
315 PERROR("kernel event zmalloc");
316 ret
= LTTNG_ERR_NOMEM
;
321 case LTTNG_EVENT_PROBE
:
322 attr
->instrumentation
= LTTNG_KERNEL_KPROBE
;
323 attr
->u
.kprobe
.addr
= ev
->attr
.probe
.addr
;
324 attr
->u
.kprobe
.offset
= ev
->attr
.probe
.offset
;
325 strncpy(attr
->u
.kprobe
.symbol_name
,
326 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
327 attr
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
329 case LTTNG_EVENT_USERSPACE_PROBE
:
331 const struct lttng_userspace_probe_location
* location
= NULL
;
332 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
334 location
= lttng_event_get_userspace_probe_location(ev
);
336 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
341 * From this point on, the specific term 'uprobe' is used
342 * instead of the generic 'userspace probe' because it's the
343 * technology used at the moment for this instrumentation.
344 * LTTng currently implements userspace probes using uprobes.
345 * In the interactions with the kernel tracer, we use the
348 attr
->instrumentation
= LTTNG_KERNEL_UPROBE
;
351 * Only the elf lookup method is supported at the moment.
353 lookup
= lttng_userspace_probe_location_get_lookup_method(
356 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
361 * From the kernel tracer's perspective, all userspace probe
362 * event types are all the same: a file and an offset.
364 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
365 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
366 /* Get the file descriptor on the target binary. */
368 lttng_userspace_probe_location_function_get_binary_fd(location
);
371 * Save a reference to the probe location used during
372 * the listing of events. Close its FD since it won't
373 * be needed for listing.
375 userspace_probe_location
=
376 lttng_userspace_probe_location_copy(location
);
377 ret
= lttng_userspace_probe_location_function_set_binary_fd(
378 userspace_probe_location
, -1);
383 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
384 /* Get the file descriptor on the target binary. */
386 lttng_userspace_probe_location_tracepoint_get_binary_fd(location
);
389 * Save a reference to the probe location used during the listing of
390 * events. Close its FD since it won't be needed for listing.
392 userspace_probe_location
=
393 lttng_userspace_probe_location_copy(location
);
394 ret
= lttng_userspace_probe_location_tracepoint_set_binary_fd(
395 userspace_probe_location
, -1);
401 DBG("Unsupported lookup method type");
402 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
407 case LTTNG_EVENT_FUNCTION
:
408 attr
->instrumentation
= LTTNG_KERNEL_KRETPROBE
;
409 attr
->u
.kretprobe
.addr
= ev
->attr
.probe
.addr
;
410 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
411 strncpy(attr
->u
.kretprobe
.symbol_name
,
412 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
413 attr
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
415 case LTTNG_EVENT_FUNCTION_ENTRY
:
416 attr
->instrumentation
= LTTNG_KERNEL_FUNCTION
;
417 strncpy(attr
->u
.ftrace
.symbol_name
,
418 ev
->attr
.ftrace
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
419 attr
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
421 case LTTNG_EVENT_TRACEPOINT
:
422 attr
->instrumentation
= LTTNG_KERNEL_TRACEPOINT
;
424 case LTTNG_EVENT_SYSCALL
:
425 attr
->instrumentation
= LTTNG_KERNEL_SYSCALL
;
427 case LTTNG_EVENT_ALL
:
428 attr
->instrumentation
= LTTNG_KERNEL_ALL
;
431 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
432 ret
= LTTNG_ERR_INVALID
;
436 /* Copy event name */
437 strncpy(attr
->name
, ev
->name
, LTTNG_KERNEL_SYM_NAME_LEN
);
438 attr
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
440 /* Setting up a kernel event */
441 local_kernel_event
->fd
= -1;
442 local_kernel_event
->event
= attr
;
443 local_kernel_event
->enabled
= 1;
444 local_kernel_event
->filter_expression
= filter_expression
;
445 local_kernel_event
->filter
= filter
;
446 local_kernel_event
->userspace_probe_location
= userspace_probe_location
;
448 *kernel_event
= local_kernel_event
;
453 free(filter_expression
);
455 free(local_kernel_event
);
461 * Allocate and initialize a kernel metadata.
463 * Return pointer to structure or NULL.
465 struct ltt_kernel_metadata
*trace_kernel_create_metadata(void)
467 struct ltt_kernel_metadata
*lkm
;
468 struct lttng_channel
*chan
;
470 lkm
= zmalloc(sizeof(struct ltt_kernel_metadata
));
471 chan
= zmalloc(sizeof(struct lttng_channel
));
472 if (lkm
== NULL
|| chan
== NULL
) {
473 PERROR("kernel metadata zmalloc");
477 /* Set default attributes */
478 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
479 chan
->attr
.subbuf_size
= default_get_metadata_subbuf_size();
480 chan
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
481 chan
->attr
.switch_timer_interval
= DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
482 chan
->attr
.read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
483 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
498 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
501 * Return pointer to structure or NULL.
503 struct ltt_kernel_stream
*trace_kernel_create_stream(const char *name
,
507 struct ltt_kernel_stream
*lks
;
511 lks
= zmalloc(sizeof(struct ltt_kernel_stream
));
513 PERROR("kernel stream zmalloc");
518 ret
= snprintf(lks
->name
, sizeof(lks
->name
), "%s_%u", name
, count
);
520 PERROR("snprintf stream name");
523 lks
->name
[sizeof(lks
->name
) - 1] = '\0';
537 * Cleanup kernel stream structure.
539 void trace_kernel_destroy_stream(struct ltt_kernel_stream
*stream
)
543 DBG("[trace] Closing stream fd %d", stream
->fd
);
544 /* Close kernel fd */
545 if (stream
->fd
>= 0) {
548 ret
= close(stream
->fd
);
553 /* Remove from stream list */
554 cds_list_del(&stream
->list
);
560 * Cleanup kernel event structure.
562 void trace_kernel_destroy_event(struct ltt_kernel_event
*event
)
566 if (event
->fd
>= 0) {
569 DBG("[trace] Closing event fd %d", event
->fd
);
570 /* Close kernel fd */
571 ret
= close(event
->fd
);
576 DBG("[trace] Tearing down event (no associated fd)");
579 /* Remove from event list */
580 cds_list_del(&event
->list
);
582 free(event
->filter_expression
);
590 * Cleanup kernel context structure.
592 void trace_kernel_destroy_context(struct ltt_kernel_context
*ctx
)
597 cds_list_del(&ctx
->list
);
603 * Cleanup kernel channel structure.
605 void trace_kernel_destroy_channel(struct ltt_kernel_channel
*channel
)
607 struct ltt_kernel_stream
*stream
, *stmp
;
608 struct ltt_kernel_event
*event
, *etmp
;
609 struct ltt_kernel_context
*ctx
, *ctmp
;
611 enum lttng_error_code status
;
615 DBG("[trace] Closing channel fd %d", channel
->fd
);
616 /* Close kernel fd */
617 if (channel
->fd
>= 0) {
618 ret
= close(channel
->fd
);
624 /* For each stream in the channel list */
625 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->stream_list
.head
, list
) {
626 trace_kernel_destroy_stream(stream
);
629 /* For each event in the channel list */
630 cds_list_for_each_entry_safe(event
, etmp
, &channel
->events_list
.head
, list
) {
631 trace_kernel_destroy_event(event
);
634 /* For each context in the channel list */
635 cds_list_for_each_entry_safe(ctx
, ctmp
, &channel
->ctx_list
, list
) {
636 trace_kernel_destroy_context(ctx
);
639 /* Remove from channel list */
640 cds_list_del(&channel
->list
);
642 if (notification_thread_handle
643 && channel
->published_to_notification_thread
) {
644 status
= notification_thread_command_remove_channel(
645 notification_thread_handle
,
646 channel
->key
, LTTNG_DOMAIN_KERNEL
);
647 assert(status
== LTTNG_OK
);
649 free(channel
->channel
->attr
.extended
.ptr
);
650 free(channel
->channel
);
655 * Cleanup kernel metadata structure.
657 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata
*metadata
)
661 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
662 /* Close kernel fd */
663 if (metadata
->fd
>= 0) {
666 ret
= close(metadata
->fd
);
672 free(metadata
->conf
);
677 * Cleanup kernel session structure
679 * Should *NOT* be called with RCU read-side lock held.
681 void trace_kernel_destroy_session(struct ltt_kernel_session
*session
)
683 struct ltt_kernel_channel
*channel
, *ctmp
;
688 DBG("[trace] Closing session fd %d", session
->fd
);
689 /* Close kernel fds */
690 if (session
->fd
>= 0) {
691 ret
= close(session
->fd
);
697 if (session
->metadata_stream_fd
>= 0) {
698 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
699 ret
= close(session
->metadata_stream_fd
);
705 if (session
->metadata
!= NULL
) {
706 trace_kernel_destroy_metadata(session
->metadata
);
709 cds_list_for_each_entry_safe(channel
, ctmp
, &session
->channel_list
.head
, list
) {
710 trace_kernel_destroy_channel(channel
);
713 /* Wipe consumer output object */
714 consumer_output_put(session
->consumer
);