Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * dlfilter-test-api-v0.c: test original (v0) API for perf --dlfilter shared object
0004  * Copyright (c) 2021, Intel Corporation.
0005  */
0006 #include <stdio.h>
0007 #include <stdlib.h>
0008 #include <string.h>
0009 #include <stdbool.h>
0010 
0011 /*
0012  * Copy original (v0) API instead of including current API
0013  */
0014 #include <linux/perf_event.h>
0015 #include <linux/types.h>
0016 
0017 /* Definitions for perf_dlfilter_sample flags */
0018 enum {
0019     PERF_DLFILTER_FLAG_BRANCH   = 1ULL << 0,
0020     PERF_DLFILTER_FLAG_CALL     = 1ULL << 1,
0021     PERF_DLFILTER_FLAG_RETURN   = 1ULL << 2,
0022     PERF_DLFILTER_FLAG_CONDITIONAL  = 1ULL << 3,
0023     PERF_DLFILTER_FLAG_SYSCALLRET   = 1ULL << 4,
0024     PERF_DLFILTER_FLAG_ASYNC    = 1ULL << 5,
0025     PERF_DLFILTER_FLAG_INTERRUPT    = 1ULL << 6,
0026     PERF_DLFILTER_FLAG_TX_ABORT = 1ULL << 7,
0027     PERF_DLFILTER_FLAG_TRACE_BEGIN  = 1ULL << 8,
0028     PERF_DLFILTER_FLAG_TRACE_END    = 1ULL << 9,
0029     PERF_DLFILTER_FLAG_IN_TX    = 1ULL << 10,
0030     PERF_DLFILTER_FLAG_VMENTRY  = 1ULL << 11,
0031     PERF_DLFILTER_FLAG_VMEXIT   = 1ULL << 12,
0032 };
0033 
0034 /*
0035  * perf sample event information (as per perf script and <linux/perf_event.h>)
0036  */
0037 struct perf_dlfilter_sample {
0038     __u32 size; /* Size of this structure (for compatibility checking) */
0039     __u16 ins_lat;      /* Refer PERF_SAMPLE_WEIGHT_TYPE in <linux/perf_event.h> */
0040     __u16 p_stage_cyc;  /* Refer PERF_SAMPLE_WEIGHT_TYPE in <linux/perf_event.h> */
0041     __u64 ip;
0042     __s32 pid;
0043     __s32 tid;
0044     __u64 time;
0045     __u64 addr;
0046     __u64 id;
0047     __u64 stream_id;
0048     __u64 period;
0049     __u64 weight;       /* Refer PERF_SAMPLE_WEIGHT_TYPE in <linux/perf_event.h> */
0050     __u64 transaction;  /* Refer PERF_SAMPLE_TRANSACTION in <linux/perf_event.h> */
0051     __u64 insn_cnt; /* For instructions-per-cycle (IPC) */
0052     __u64 cyc_cnt;      /* For instructions-per-cycle (IPC) */
0053     __s32 cpu;
0054     __u32 flags;        /* Refer PERF_DLFILTER_FLAG_* above */
0055     __u64 data_src;     /* Refer PERF_SAMPLE_DATA_SRC in <linux/perf_event.h> */
0056     __u64 phys_addr;    /* Refer PERF_SAMPLE_PHYS_ADDR in <linux/perf_event.h> */
0057     __u64 data_page_size;   /* Refer PERF_SAMPLE_DATA_PAGE_SIZE in <linux/perf_event.h> */
0058     __u64 code_page_size;   /* Refer PERF_SAMPLE_CODE_PAGE_SIZE in <linux/perf_event.h> */
0059     __u64 cgroup;       /* Refer PERF_SAMPLE_CGROUP in <linux/perf_event.h> */
0060     __u8  cpumode;      /* Refer CPUMODE_MASK etc in <linux/perf_event.h> */
0061     __u8  addr_correlates_sym; /* True => resolve_addr() can be called */
0062     __u16 misc;     /* Refer perf_event_header in <linux/perf_event.h> */
0063     __u32 raw_size;     /* Refer PERF_SAMPLE_RAW in <linux/perf_event.h> */
0064     const void *raw_data;   /* Refer PERF_SAMPLE_RAW in <linux/perf_event.h> */
0065     __u64 brstack_nr;   /* Number of brstack entries */
0066     const struct perf_branch_entry *brstack; /* Refer <linux/perf_event.h> */
0067     __u64 raw_callchain_nr; /* Number of raw_callchain entries */
0068     const __u64 *raw_callchain; /* Refer <linux/perf_event.h> */
0069     const char *event;
0070 };
0071 
0072 /*
0073  * Address location (as per perf script)
0074  */
0075 struct perf_dlfilter_al {
0076     __u32 size; /* Size of this structure (for compatibility checking) */
0077     __u32 symoff;
0078     const char *sym;
0079     __u64 addr; /* Mapped address (from dso) */
0080     __u64 sym_start;
0081     __u64 sym_end;
0082     const char *dso;
0083     __u8  sym_binding; /* STB_LOCAL, STB_GLOBAL or STB_WEAK, refer <elf.h> */
0084     __u8  is_64_bit; /* Only valid if dso is not NULL */
0085     __u8  is_kernel_ip; /* True if in kernel space */
0086     __u32 buildid_size;
0087     __u8 *buildid;
0088     /* Below members are only populated by resolve_ip() */
0089     __u8 filtered; /* True if this sample event will be filtered out */
0090     const char *comm;
0091 };
0092 
0093 struct perf_dlfilter_fns {
0094     /* Return information about ip */
0095     const struct perf_dlfilter_al *(*resolve_ip)(void *ctx);
0096     /* Return information about addr (if addr_correlates_sym) */
0097     const struct perf_dlfilter_al *(*resolve_addr)(void *ctx);
0098     /* Return arguments from --dlarg option */
0099     char **(*args)(void *ctx, int *dlargc);
0100     /*
0101      * Return information about address (al->size must be set before
0102      * calling). Returns 0 on success, -1 otherwise.
0103      */
0104     __s32 (*resolve_address)(void *ctx, __u64 address, struct perf_dlfilter_al *al);
0105     /* Return instruction bytes and length */
0106     const __u8 *(*insn)(void *ctx, __u32 *length);
0107     /* Return source file name and line number */
0108     const char *(*srcline)(void *ctx, __u32 *line_number);
0109     /* Return perf_event_attr, refer <linux/perf_event.h> */
0110     struct perf_event_attr *(*attr)(void *ctx);
0111     /* Read object code, return numbers of bytes read */
0112     __s32 (*object_code)(void *ctx, __u64 ip, void *buf, __u32 len);
0113     /* Reserved */
0114     void *(*reserved[120])(void *);
0115 };
0116 
0117 struct perf_dlfilter_fns perf_dlfilter_fns;
0118 
0119 static int verbose;
0120 
0121 #define pr_debug(fmt, ...) do { \
0122         if (verbose) \
0123             fprintf(stderr, fmt, ##__VA_ARGS__); \
0124     } while (0)
0125 
0126 static int test_fail(const char *msg)
0127 {
0128     pr_debug("%s\n", msg);
0129     return -1;
0130 }
0131 
0132 #define CHECK(x) do { \
0133         if (!(x)) \
0134             return test_fail("Check '" #x "' failed\n"); \
0135     } while (0)
0136 
0137 struct filter_data {
0138     __u64 ip;
0139     __u64 addr;
0140     int do_early;
0141     int early_filter_cnt;
0142     int filter_cnt;
0143 };
0144 
0145 static struct filter_data *filt_dat;
0146 
0147 int start(void **data, void *ctx)
0148 {
0149     int dlargc;
0150     char **dlargv;
0151     struct filter_data *d;
0152     static bool called;
0153 
0154     verbose = 1;
0155 
0156     CHECK(!filt_dat && !called);
0157     called = true;
0158 
0159     d = calloc(1, sizeof(*d));
0160     if (!d)
0161         test_fail("Failed to allocate memory");
0162     filt_dat = d;
0163     *data = d;
0164 
0165     dlargv = perf_dlfilter_fns.args(ctx, &dlargc);
0166 
0167     CHECK(dlargc == 6);
0168     CHECK(!strcmp(dlargv[0], "first"));
0169     verbose = strtol(dlargv[1], NULL, 0);
0170     d->ip = strtoull(dlargv[2], NULL, 0);
0171     d->addr = strtoull(dlargv[3], NULL, 0);
0172     d->do_early = strtol(dlargv[4], NULL, 0);
0173     CHECK(!strcmp(dlargv[5], "last"));
0174 
0175     pr_debug("%s API\n", __func__);
0176 
0177     return 0;
0178 }
0179 
0180 #define CHECK_SAMPLE(x) do { \
0181         if (sample->x != expected.x) \
0182             return test_fail("'" #x "' not expected value\n"); \
0183     } while (0)
0184 
0185 static int check_sample(struct filter_data *d, const struct perf_dlfilter_sample *sample)
0186 {
0187     struct perf_dlfilter_sample expected = {
0188         .ip     = d->ip,
0189         .pid        = 12345,
0190         .tid        = 12346,
0191         .time       = 1234567890,
0192         .addr       = d->addr,
0193         .id     = 99,
0194         .stream_id  = 101,
0195         .period     = 543212345,
0196         .cpu        = 31,
0197         .cpumode    = PERF_RECORD_MISC_USER,
0198         .addr_correlates_sym = 1,
0199         .misc       = PERF_RECORD_MISC_USER,
0200     };
0201 
0202     CHECK(sample->size >= sizeof(struct perf_dlfilter_sample));
0203 
0204     CHECK_SAMPLE(ip);
0205     CHECK_SAMPLE(pid);
0206     CHECK_SAMPLE(tid);
0207     CHECK_SAMPLE(time);
0208     CHECK_SAMPLE(addr);
0209     CHECK_SAMPLE(id);
0210     CHECK_SAMPLE(stream_id);
0211     CHECK_SAMPLE(period);
0212     CHECK_SAMPLE(cpu);
0213     CHECK_SAMPLE(cpumode);
0214     CHECK_SAMPLE(addr_correlates_sym);
0215     CHECK_SAMPLE(misc);
0216 
0217     CHECK(!sample->raw_data);
0218     CHECK_SAMPLE(brstack_nr);
0219     CHECK(!sample->brstack);
0220     CHECK_SAMPLE(raw_callchain_nr);
0221     CHECK(!sample->raw_callchain);
0222 
0223 #define EVENT_NAME "branches:"
0224     CHECK(!strncmp(sample->event, EVENT_NAME, strlen(EVENT_NAME)));
0225 
0226     return 0;
0227 }
0228 
0229 static int check_al(void *ctx)
0230 {
0231     const struct perf_dlfilter_al *al;
0232 
0233     al = perf_dlfilter_fns.resolve_ip(ctx);
0234     if (!al)
0235         return test_fail("resolve_ip() failed");
0236 
0237     CHECK(al->sym && !strcmp("foo", al->sym));
0238     CHECK(!al->symoff);
0239 
0240     return 0;
0241 }
0242 
0243 static int check_addr_al(void *ctx)
0244 {
0245     const struct perf_dlfilter_al *addr_al;
0246 
0247     addr_al = perf_dlfilter_fns.resolve_addr(ctx);
0248     if (!addr_al)
0249         return test_fail("resolve_addr() failed");
0250 
0251     CHECK(addr_al->sym && !strcmp("bar", addr_al->sym));
0252     CHECK(!addr_al->symoff);
0253 
0254     return 0;
0255 }
0256 
0257 static int check_attr(void *ctx)
0258 {
0259     struct perf_event_attr *attr = perf_dlfilter_fns.attr(ctx);
0260 
0261     CHECK(attr);
0262     CHECK(attr->type == PERF_TYPE_HARDWARE);
0263     CHECK(attr->config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS);
0264 
0265     return 0;
0266 }
0267 
0268 static int do_checks(void *data, const struct perf_dlfilter_sample *sample, void *ctx, bool early)
0269 {
0270     struct filter_data *d = data;
0271 
0272     CHECK(data && filt_dat == data);
0273 
0274     if (early) {
0275         CHECK(!d->early_filter_cnt);
0276         d->early_filter_cnt += 1;
0277     } else {
0278         CHECK(!d->filter_cnt);
0279         CHECK(d->early_filter_cnt);
0280         CHECK(d->do_early != 2);
0281         d->filter_cnt += 1;
0282     }
0283 
0284     if (check_sample(data, sample))
0285         return -1;
0286 
0287     if (check_attr(ctx))
0288         return -1;
0289 
0290     if (early && !d->do_early)
0291         return 0;
0292 
0293     if (check_al(ctx) || check_addr_al(ctx))
0294         return -1;
0295 
0296     if (early)
0297         return d->do_early == 2;
0298 
0299     return 1;
0300 }
0301 
0302 int filter_event_early(void *data, const struct perf_dlfilter_sample *sample, void *ctx)
0303 {
0304     pr_debug("%s API\n", __func__);
0305 
0306     return do_checks(data, sample, ctx, true);
0307 }
0308 
0309 int filter_event(void *data, const struct perf_dlfilter_sample *sample, void *ctx)
0310 {
0311     pr_debug("%s API\n", __func__);
0312 
0313     return do_checks(data, sample, ctx, false);
0314 }
0315 
0316 int stop(void *data, void *ctx)
0317 {
0318     static bool called;
0319 
0320     pr_debug("%s API\n", __func__);
0321 
0322     CHECK(data && filt_dat == data && !called);
0323     called = true;
0324 
0325     free(data);
0326     filt_dat = NULL;
0327     return 0;
0328 }
0329 
0330 const char *filter_description(const char **long_description)
0331 {
0332     *long_description = "Filter used by the 'dlfilter C API' perf test";
0333     return "dlfilter to test v0 C API";
0334 }