'\" t
.\" Title: perf-dlfilter
.\" Author: [FIXME: author] [see http://www.docbook.org/tdg5/en/html/author]
.\" Generator: DocBook XSL Stylesheets vsnapshot
.\" Date: 2023-08-15
.\" Manual: perf Manual
.\" Source: perf
.\" Language: English
.\"
.TH "PERF\-DLFILTER" "1" "2023\-08\-15" "perf" "perf Manual"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.\" http://bugs.debian.org/507673
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\" -----------------------------------------------------------------
.\" * set default formatting
.\" -----------------------------------------------------------------
.\" disable hyphenation
.nh
.\" disable justification (adjust text to left margin only)
.ad l
.\" -----------------------------------------------------------------
.\" * MAIN CONTENT STARTS HERE *
.\" -----------------------------------------------------------------
.SH "NAME"
perf-dlfilter \- Filter sample events using a dynamically loaded shared object file
.SH "SYNOPSIS"
.sp
.nf
\fIperf script\fR [\-\-dlfilter file\&.so ] [ \-\-dlarg arg ]\&...
.fi
.SH "DESCRIPTION"
.sp
This option is used to process data through a custom filter provided by a dynamically loaded shared object file\&. Arguments can be passed using \-\-dlarg and retrieved using perf_dlfilter_fns\&.args()\&.
.sp
If \fIfile\&.so\fR does not contain "/", then it will be found either in the current directory, or perf tools exec path which is ~/libexec/perf\-core/dlfilters for a local build and install (refer perf \-\-exec\-path), or the dynamic linker paths\&.
.SH "API"
.sp
The API for filtering consists of the following:
.sp
.if n \{\
.RS 4
.\}
.nf
#include
struct perf_dlfilter_fns perf_dlfilter_fns;
int start(void **data, void *ctx);
int stop(void *data, void *ctx);
int filter_event(void *data, const struct perf_dlfilter_sample *sample, void *ctx);
int filter_event_early(void *data, const struct perf_dlfilter_sample *sample, void *ctx);
const char *filter_description(const char **long_description);
.fi
.if n \{\
.RE
.\}
.sp
If implemented, \fIstart\fR will be called at the beginning, before any calls to \fIfilter_event\fR or \fIfilter_event_early\fR\&. Return 0 to indicate success, or return a negative error code\&. \fI*data\fR can be assigned for use by other functions\&. \fIctx\fR is needed for calls to perf_dlfilter_fns, but most perf_dlfilter_fns are not valid when called from \fIstart\fR\&.
.sp
If implemented, \fIstop\fR will be called at the end, after any calls to \fIfilter_event\fR or \fIfilter_event_early\fR\&. Return 0 to indicate success, or return a negative error code\&. \fIdata\fR is set by \fIstart\fR\&. \fIctx\fR is needed for calls to perf_dlfilter_fns, but most perf_dlfilter_fns are not valid when called from \fIstop\fR\&.
.sp
If implemented, \fIfilter_event\fR will be called for each sample event\&. Return 0 to keep the sample event, 1 to filter it out, or return a negative error code\&. \fIdata\fR is set by \fIstart\fR\&. \fIctx\fR is needed for calls to \fIperf_dlfilter_fns\fR\&.
.sp
\fIfilter_event_early\fR is the same as \fIfilter_event\fR except it is called before internal filtering\&.
.sp
If implemented, \fIfilter_description\fR should return a one\-line description of the filter, and optionally a longer description\&.
.sp
Do not assume the \fIsample\fR argument is valid (dereferenceable) after \fIfilter_event\fR and \fIfilter_event_early\fR return\&.
.sp
Do not assume data referenced by pointers in struct perf_dlfilter_sample is valid (dereferenceable) after \fIfilter_event\fR and \fIfilter_event_early\fR return\&.
.SS "The perf_dlfilter_sample structure"
.sp
\fIfilter_event\fR and \fIfilter_event_early\fR are passed a perf_dlfilter_sample structure, which contains the following fields:
.sp
.if n \{\
.RS 4
.\}
.nf
/*
* perf sample event information (as per perf script and )
*/
struct perf_dlfilter_sample {
__u32 size; /* Size of this structure (for compatibility checking) */
__u16 ins_lat; /* Refer PERF_SAMPLE_WEIGHT_TYPE in */
__u16 p_stage_cyc; /* Refer PERF_SAMPLE_WEIGHT_TYPE in */
__u64 ip;
__s32 pid;
__s32 tid;
__u64 time;
__u64 addr;
__u64 id;
__u64 stream_id;
__u64 period;
__u64 weight; /* Refer PERF_SAMPLE_WEIGHT_TYPE in */
__u64 transaction; /* Refer PERF_SAMPLE_TRANSACTION in */
__u64 insn_cnt; /* For instructions\-per\-cycle (IPC) */
__u64 cyc_cnt; /* For instructions\-per\-cycle (IPC) */
__s32 cpu;
__u32 flags; /* Refer PERF_DLFILTER_FLAG_* above */
__u64 data_src; /* Refer PERF_SAMPLE_DATA_SRC in */
__u64 phys_addr; /* Refer PERF_SAMPLE_PHYS_ADDR in */
__u64 data_page_size; /* Refer PERF_SAMPLE_DATA_PAGE_SIZE in */
__u64 code_page_size; /* Refer PERF_SAMPLE_CODE_PAGE_SIZE in */
__u64 cgroup; /* Refer PERF_SAMPLE_CGROUP in */
__u8 cpumode; /* Refer CPUMODE_MASK etc in */
__u8 addr_correlates_sym; /* True => resolve_addr() can be called */
__u16 misc; /* Refer perf_event_header in */
__u32 raw_size; /* Refer PERF_SAMPLE_RAW in */
const void *raw_data; /* Refer PERF_SAMPLE_RAW in */
__u64 brstack_nr; /* Number of brstack entries */
const struct perf_branch_entry *brstack; /* Refer */
__u64 raw_callchain_nr; /* Number of raw_callchain entries */
const __u64 *raw_callchain; /* Refer */
const char *event;
__s32 machine_pid;
__s32 vcpu;
};
.fi
.if n \{\
.RE
.\}
.sp
Note: \fImachine_pid\fR and \fIvcpu\fR are not original members, but were added together later\&. \fIsize\fR can be used to determine their presence at run time\&. PERF_DLFILTER_HAS_MACHINE_PID will be defined if they are present at compile time\&. For example:
.sp
.if n \{\
.RS 4
.\}
.nf
#include
#include
#include
static inline bool have_machine_pid(const struct perf_dlfilter_sample *sample)
{
#ifdef PERF_DLFILTER_HAS_MACHINE_PID
return sample\->size >= offsetof(struct perf_dlfilter_sample, vcpu) + sizeof(sample\->vcpu);
#else
return false;
#endif
}
.fi
.if n \{\
.RE
.\}
.SS "The perf_dlfilter_fns structure"
.sp
The \fIperf_dlfilter_fns\fR structure is populated with function pointers when the file is loaded\&. The functions can be called by \fIfilter_event\fR or \fIfilter_event_early\fR\&.
.sp
.if n \{\
.RS 4
.\}
.nf
struct perf_dlfilter_fns {
const struct perf_dlfilter_al *(*resolve_ip)(void *ctx);
const struct perf_dlfilter_al *(*resolve_addr)(void *ctx);
char **(*args)(void *ctx, int *dlargc);
__s32 (*resolve_address)(void *ctx, __u64 address, struct perf_dlfilter_al *al);
const __u8 *(*insn)(void *ctx, __u32 *length);
const char *(*srcline)(void *ctx, __u32 *line_number);
struct perf_event_attr *(*attr)(void *ctx);
__s32 (*object_code)(void *ctx, __u64 ip, void *buf, __u32 len);
void (*al_cleanup)(void *ctx, struct perf_dlfilter_al *al);
void *(*reserved[119])(void *);
};
.fi
.if n \{\
.RE
.\}
.sp
\fIresolve_ip\fR returns information about ip\&.
.sp
\fIresolve_addr\fR returns information about addr (if addr_correlates_sym)\&.
.sp
\fIargs\fR returns arguments from \-\-dlarg options\&.
.sp
\fIresolve_address\fR provides information about \fIaddress\fR\&. al\(->size must be set before calling\&. Returns 0 on success, \-1 otherwise\&. Call al_cleanup() (if present, see below) when \fIal\fR data is no longer needed\&.
.sp
\fIinsn\fR returns instruction bytes and length\&.
.sp
\fIsrcline\fR return source file name and line number\&.
.sp
\fIattr\fR returns perf_event_attr, refer \&.
.sp
\fIobject_code\fR reads object code and returns the number of bytes read\&.
.sp
\fIal_cleanup\fR must be called (if present, so check perf_dlfilter_fns\&.al_cleanup != NULL) after resolve_address() to free any associated resources\&.
.sp
Do not assume pointers obtained via perf_dlfilter_fns are valid (dereferenceable) after \fIfilter_event\fR and \fIfilter_event_early\fR return\&.
.SS "The perf_dlfilter_al structure"
.sp
The \fIperf_dlfilter_al\fR structure contains information about an address\&.
.sp
.if n \{\
.RS 4
.\}
.nf
/*
* Address location (as per perf script)
*/
struct perf_dlfilter_al {
__u32 size; /* Size of this structure (for compatibility checking) */
__u32 symoff;
const char *sym;
__u64 addr; /* Mapped address (from dso) */
__u64 sym_start;
__u64 sym_end;
const char *dso;
__u8 sym_binding; /* STB_LOCAL, STB_GLOBAL or STB_WEAK, refer */
__u8 is_64_bit; /* Only valid if dso is not NULL */
__u8 is_kernel_ip; /* True if in kernel space */
__u32 buildid_size;
__u8 *buildid;
/* Below members are only populated by resolve_ip() */
__u8 filtered; /* true if this sample event will be filtered out */
const char *comm;
void *priv; /* Private data\&. Do not change */
};
.fi
.if n \{\
.RE
.\}
.sp
Do not assume data referenced by pointers in struct perf_dlfilter_al is valid (dereferenceable) after \fIfilter_event\fR and \fIfilter_event_early\fR return\&.
.SS "perf_dlfilter_sample flags"
.sp
The \fIflags\fR member of \fIperf_dlfilter_sample\fR corresponds with the flags field of perf script\&. The bits of the flags are as follows:
.sp
.if n \{\
.RS 4
.\}
.nf
/* Definitions for perf_dlfilter_sample flags */
enum {
PERF_DLFILTER_FLAG_BRANCH = 1ULL << 0,
PERF_DLFILTER_FLAG_CALL = 1ULL << 1,
PERF_DLFILTER_FLAG_RETURN = 1ULL << 2,
PERF_DLFILTER_FLAG_CONDITIONAL = 1ULL << 3,
PERF_DLFILTER_FLAG_SYSCALLRET = 1ULL << 4,
PERF_DLFILTER_FLAG_ASYNC = 1ULL << 5,
PERF_DLFILTER_FLAG_INTERRUPT = 1ULL << 6,
PERF_DLFILTER_FLAG_TX_ABORT = 1ULL << 7,
PERF_DLFILTER_FLAG_TRACE_BEGIN = 1ULL << 8,
PERF_DLFILTER_FLAG_TRACE_END = 1ULL << 9,
PERF_DLFILTER_FLAG_IN_TX = 1ULL << 10,
PERF_DLFILTER_FLAG_VMENTRY = 1ULL << 11,
PERF_DLFILTER_FLAG_VMEXIT = 1ULL << 12,
};
.fi
.if n \{\
.RE
.\}
.SH "EXAMPLE"
.sp
Filter out everything except branches from "foo" to "bar":
.sp
.if n \{\
.RS 4
.\}
.nf
#include
#include
struct perf_dlfilter_fns perf_dlfilter_fns;
int filter_event(void *data, const struct perf_dlfilter_sample *sample, void *ctx)
{
const struct perf_dlfilter_al *al;
const struct perf_dlfilter_al *addr_al;
if (!sample\->ip || !sample\->addr_correlates_sym)
return 1;
al = perf_dlfilter_fns\&.resolve_ip(ctx);
if (!al || !al\->sym || strcmp(al\->sym, "foo"))
return 1;
addr_al = perf_dlfilter_fns\&.resolve_addr(ctx);
if (!addr_al || !addr_al\->sym || strcmp(addr_al\->sym, "bar"))
return 1;
return 0;
}
.fi
.if n \{\
.RE
.\}
.sp
To build the shared object, assuming perf has been installed for the local user i\&.e\&. perf_dlfilter\&.h is in ~/include/perf :
.sp
.if n \{\
.RS 4
.\}
.nf
gcc \-c \-I ~/include \-fpic dlfilter\-example\&.c
gcc \-shared \-o dlfilter\-example\&.so dlfilter\-example\&.o
.fi
.if n \{\
.RE
.\}
.sp
To use the filter with perf script:
.sp
.if n \{\
.RS 4
.\}
.nf
perf script \-\-dlfilter dlfilter\-example\&.so
.fi
.if n \{\
.RE
.\}
.SH "NOTES"
.sp
The dlfilter \&.so file will be dependent on shared libraries\&. If those change, it may be necessary to rebuild the \&.so\&. Also there may be unexpected results if the \&.so uses different versions of the shared libraries that perf uses\&. Versions can be checked using the ldd command\&.
.SH "SEE ALSO"
.sp
\fBperf-script\fR(1)