1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Test v2 API for perf --dlfilter shared object 4 * Copyright (c) 2023, Intel Corporation. 5 */ 6 #include <stdio.h> 7 #include <stdlib.h> 8 #include <string.h> 9 #include <stdbool.h> 10 11 /* 12 * Copy v2 API instead of including current API 13 */ 14 #include <linux/perf_event.h> 15 #include <linux/types.h> 16 17 /* 18 * The following macro can be used to determine if this header defines 19 * perf_dlfilter_sample machine_pid and vcpu. 20 */ 21 #define PERF_DLFILTER_HAS_MACHINE_PID 22 23 /* Definitions for perf_dlfilter_sample flags */ 24 enum { 25 PERF_DLFILTER_FLAG_BRANCH = 1ULL << 0, 26 PERF_DLFILTER_FLAG_CALL = 1ULL << 1, 27 PERF_DLFILTER_FLAG_RETURN = 1ULL << 2, 28 PERF_DLFILTER_FLAG_CONDITIONAL = 1ULL << 3, 29 PERF_DLFILTER_FLAG_SYSCALLRET = 1ULL << 4, 30 PERF_DLFILTER_FLAG_ASYNC = 1ULL << 5, 31 PERF_DLFILTER_FLAG_INTERRUPT = 1ULL << 6, 32 PERF_DLFILTER_FLAG_TX_ABORT = 1ULL << 7, 33 PERF_DLFILTER_FLAG_TRACE_BEGIN = 1ULL << 8, 34 PERF_DLFILTER_FLAG_TRACE_END = 1ULL << 9, 35 PERF_DLFILTER_FLAG_IN_TX = 1ULL << 10, 36 PERF_DLFILTER_FLAG_VMENTRY = 1ULL << 11, 37 PERF_DLFILTER_FLAG_VMEXIT = 1ULL << 12, 38 }; 39 40 /* 41 * perf sample event information (as per perf script and <linux/perf_event.h>) 42 */ 43 struct perf_dlfilter_sample { 44 __u32 size; /* Size of this structure (for compatibility checking) */ 45 __u16 ins_lat; /* Refer PERF_SAMPLE_WEIGHT_TYPE in <linux/perf_event.h> */ 46 __u16 p_stage_cyc; /* Refer PERF_SAMPLE_WEIGHT_TYPE in <linux/perf_event.h> */ 47 __u64 ip; 48 __s32 pid; 49 __s32 tid; 50 __u64 time; 51 __u64 addr; 52 __u64 id; 53 __u64 stream_id; 54 __u64 period; 55 __u64 weight; /* Refer PERF_SAMPLE_WEIGHT_TYPE in <linux/perf_event.h> */ 56 __u64 transaction; /* Refer PERF_SAMPLE_TRANSACTION in <linux/perf_event.h> */ 57 __u64 insn_cnt; /* For instructions-per-cycle (IPC) */ 58 __u64 cyc_cnt; /* For instructions-per-cycle (IPC) */ 59 __s32 cpu; 60 __u32 flags; /* Refer PERF_DLFILTER_FLAG_* above */ 61 __u64 data_src; /* Refer PERF_SAMPLE_DATA_SRC in <linux/perf_event.h> */ 62 __u64 phys_addr; /* Refer PERF_SAMPLE_PHYS_ADDR in <linux/perf_event.h> */ 63 __u64 data_page_size; /* Refer PERF_SAMPLE_DATA_PAGE_SIZE in <linux/perf_event.h> */ 64 __u64 code_page_size; /* Refer PERF_SAMPLE_CODE_PAGE_SIZE in <linux/perf_event.h> */ 65 __u64 cgroup; /* Refer PERF_SAMPLE_CGROUP in <linux/perf_event.h> */ 66 __u8 cpumode; /* Refer CPUMODE_MASK etc in <linux/perf_event.h> */ 67 __u8 addr_correlates_sym; /* True => resolve_addr() can be called */ 68 __u16 misc; /* Refer perf_event_header in <linux/perf_event.h> */ 69 __u32 raw_size; /* Refer PERF_SAMPLE_RAW in <linux/perf_event.h> */ 70 const void *raw_data; /* Refer PERF_SAMPLE_RAW in <linux/perf_event.h> */ 71 __u64 brstack_nr; /* Number of brstack entries */ 72 const struct perf_branch_entry *brstack; /* Refer <linux/perf_event.h> */ 73 __u64 raw_callchain_nr; /* Number of raw_callchain entries */ 74 const __u64 *raw_callchain; /* Refer <linux/perf_event.h> */ 75 const char *event; 76 __s32 machine_pid; 77 __s32 vcpu; 78 }; 79 80 /* 81 * Address location (as per perf script) 82 */ 83 struct perf_dlfilter_al { 84 __u32 size; /* Size of this structure (for compatibility checking) */ 85 __u32 symoff; 86 const char *sym; 87 __u64 addr; /* Mapped address (from dso) */ 88 __u64 sym_start; 89 __u64 sym_end; 90 const char *dso; 91 __u8 sym_binding; /* STB_LOCAL, STB_GLOBAL or STB_WEAK, refer <elf.h> */ 92 __u8 is_64_bit; /* Only valid if dso is not NULL */ 93 __u8 is_kernel_ip; /* True if in kernel space */ 94 __u32 buildid_size; 95 __u8 *buildid; 96 /* Below members are only populated by resolve_ip() */ 97 __u8 filtered; /* True if this sample event will be filtered out */ 98 const char *comm; 99 void *priv; /* Private data (v2 API) */ 100 }; 101 102 struct perf_dlfilter_fns { 103 /* Return information about ip */ 104 const struct perf_dlfilter_al *(*resolve_ip)(void *ctx); 105 /* Return information about addr (if addr_correlates_sym) */ 106 const struct perf_dlfilter_al *(*resolve_addr)(void *ctx); 107 /* Return arguments from --dlarg option */ 108 char **(*args)(void *ctx, int *dlargc); 109 /* 110 * Return information about address (al->size must be set before 111 * calling). Returns 0 on success, -1 otherwise. Call al_cleanup() 112 * when 'al' data is no longer needed. 113 */ 114 __s32 (*resolve_address)(void *ctx, __u64 address, struct perf_dlfilter_al *al); 115 /* Return instruction bytes and length */ 116 const __u8 *(*insn)(void *ctx, __u32 *length); 117 /* Return source file name and line number */ 118 const char *(*srcline)(void *ctx, __u32 *line_number); 119 /* Return perf_event_attr, refer <linux/perf_event.h> */ 120 struct perf_event_attr *(*attr)(void *ctx); 121 /* Read object code, return numbers of bytes read */ 122 __s32 (*object_code)(void *ctx, __u64 ip, void *buf, __u32 len); 123 /* 124 * If present (i.e. must check al_cleanup != NULL), call after 125 * resolve_address() to free any associated resources. (v2 API) 126 */ 127 void (*al_cleanup)(void *ctx, struct perf_dlfilter_al *al); 128 /* Reserved */ 129 void *(*reserved[119])(void *); 130 }; 131 132 struct perf_dlfilter_fns perf_dlfilter_fns; 133 134 static int verbose; 135 136 #define pr_debug(fmt, ...) do { \ 137 if (verbose > 0) \ 138 fprintf(stderr, fmt, ##__VA_ARGS__); \ 139 } while (0) 140 141 static int test_fail(const char *msg) 142 { 143 pr_debug("%s\n", msg); 144 return -1; 145 } 146 147 #define CHECK(x) do { \ 148 if (!(x)) \ 149 return test_fail("Check '" #x "' failed\n"); \ 150 } while (0) 151 152 struct filter_data { 153 __u64 ip; 154 __u64 addr; 155 int do_early; 156 int early_filter_cnt; 157 int filter_cnt; 158 }; 159 160 static struct filter_data *filt_dat; 161 162 int start(void **data, void *ctx) 163 { 164 int dlargc; 165 char **dlargv; 166 struct filter_data *d; 167 static bool called; 168 169 verbose = 1; 170 171 CHECK(!filt_dat && !called); 172 called = true; 173 174 d = calloc(1, sizeof(*d)); 175 if (!d) 176 test_fail("Failed to allocate memory"); 177 filt_dat = d; 178 *data = d; 179 180 dlargv = perf_dlfilter_fns.args(ctx, &dlargc); 181 182 CHECK(dlargc == 6); 183 CHECK(!strcmp(dlargv[0], "first")); 184 verbose = strtol(dlargv[1], NULL, 0); 185 d->ip = strtoull(dlargv[2], NULL, 0); 186 d->addr = strtoull(dlargv[3], NULL, 0); 187 d->do_early = strtol(dlargv[4], NULL, 0); 188 CHECK(!strcmp(dlargv[5], "last")); 189 190 pr_debug("%s API\n", __func__); 191 192 return 0; 193 } 194 195 #define CHECK_SAMPLE(x) do { \ 196 if (sample->x != expected.x) \ 197 return test_fail("'" #x "' not expected value\n"); \ 198 } while (0) 199 200 static int check_sample(struct filter_data *d, const struct perf_dlfilter_sample *sample) 201 { 202 struct perf_dlfilter_sample expected = { 203 .ip = d->ip, 204 .pid = 12345, 205 .tid = 12346, 206 .time = 1234567890, 207 .addr = d->addr, 208 .id = 99, 209 .stream_id = 101, 210 .period = 543212345, 211 .cpu = 31, 212 .cpumode = PERF_RECORD_MISC_USER, 213 .addr_correlates_sym = 1, 214 .misc = PERF_RECORD_MISC_USER, 215 }; 216 217 CHECK(sample->size >= sizeof(struct perf_dlfilter_sample)); 218 219 CHECK_SAMPLE(ip); 220 CHECK_SAMPLE(pid); 221 CHECK_SAMPLE(tid); 222 CHECK_SAMPLE(time); 223 CHECK_SAMPLE(addr); 224 CHECK_SAMPLE(id); 225 CHECK_SAMPLE(stream_id); 226 CHECK_SAMPLE(period); 227 CHECK_SAMPLE(cpu); 228 CHECK_SAMPLE(cpumode); 229 CHECK_SAMPLE(addr_correlates_sym); 230 CHECK_SAMPLE(misc); 231 232 CHECK(!sample->raw_data); 233 CHECK_SAMPLE(brstack_nr); 234 CHECK(!sample->brstack); 235 CHECK_SAMPLE(raw_callchain_nr); 236 CHECK(!sample->raw_callchain); 237 238 #define EVENT_NAME "branches:" 239 CHECK(!strncmp(sample->event, EVENT_NAME, strlen(EVENT_NAME))); 240 241 return 0; 242 } 243 244 static int check_al(void *ctx) 245 { 246 const struct perf_dlfilter_al *al; 247 248 al = perf_dlfilter_fns.resolve_ip(ctx); 249 if (!al) 250 return test_fail("resolve_ip() failed"); 251 252 CHECK(al->sym && !strcmp("foo", al->sym)); 253 CHECK(!al->symoff); 254 255 return 0; 256 } 257 258 static int check_addr_al(void *ctx) 259 { 260 const struct perf_dlfilter_al *addr_al; 261 262 addr_al = perf_dlfilter_fns.resolve_addr(ctx); 263 if (!addr_al) 264 return test_fail("resolve_addr() failed"); 265 266 CHECK(addr_al->sym && !strcmp("bar", addr_al->sym)); 267 CHECK(!addr_al->symoff); 268 269 return 0; 270 } 271 272 static int check_address_al(void *ctx, const struct perf_dlfilter_sample *sample) 273 { 274 struct perf_dlfilter_al address_al; 275 const struct perf_dlfilter_al *al; 276 277 al = perf_dlfilter_fns.resolve_ip(ctx); 278 if (!al) 279 return test_fail("resolve_ip() failed"); 280 281 address_al.size = sizeof(address_al); 282 if (perf_dlfilter_fns.resolve_address(ctx, sample->ip, &address_al)) 283 return test_fail("resolve_address() failed"); 284 285 CHECK(address_al.sym && al->sym); 286 CHECK(!strcmp(address_al.sym, al->sym)); 287 CHECK(address_al.addr == al->addr); 288 CHECK(address_al.sym_start == al->sym_start); 289 CHECK(address_al.sym_end == al->sym_end); 290 CHECK(address_al.dso && al->dso); 291 CHECK(!strcmp(address_al.dso, al->dso)); 292 293 /* al_cleanup() is v2 API so may not be present */ 294 if (perf_dlfilter_fns.al_cleanup) 295 perf_dlfilter_fns.al_cleanup(ctx, &address_al); 296 297 return 0; 298 } 299 300 static int check_attr(void *ctx) 301 { 302 struct perf_event_attr *attr = perf_dlfilter_fns.attr(ctx); 303 304 CHECK(attr); 305 CHECK(attr->type == PERF_TYPE_HARDWARE); 306 CHECK(attr->config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS); 307 308 return 0; 309 } 310 311 static int check_object_code(void *ctx, const struct perf_dlfilter_sample *sample) 312 { 313 __u8 buf[15]; 314 315 CHECK(perf_dlfilter_fns.object_code(ctx, sample->ip, buf, sizeof(buf)) > 0); 316 317 return 0; 318 } 319 320 static int do_checks(void *data, const struct perf_dlfilter_sample *sample, void *ctx, bool early) 321 { 322 struct filter_data *d = data; 323 324 CHECK(data && filt_dat == data); 325 326 if (early) { 327 CHECK(!d->early_filter_cnt); 328 d->early_filter_cnt += 1; 329 } else { 330 CHECK(!d->filter_cnt); 331 CHECK(d->early_filter_cnt); 332 CHECK(d->do_early != 2); 333 d->filter_cnt += 1; 334 } 335 336 if (check_sample(data, sample)) 337 return -1; 338 339 if (check_attr(ctx)) 340 return -1; 341 342 if (early && !d->do_early) 343 return 0; 344 345 if (check_al(ctx) || check_addr_al(ctx) || check_address_al(ctx, sample) || 346 check_object_code(ctx, sample)) 347 return -1; 348 349 if (early) 350 return d->do_early == 2; 351 352 return 1; 353 } 354 355 int filter_event_early(void *data, const struct perf_dlfilter_sample *sample, void *ctx) 356 { 357 pr_debug("%s API\n", __func__); 358 359 return do_checks(data, sample, ctx, true); 360 } 361 362 int filter_event(void *data, const struct perf_dlfilter_sample *sample, void *ctx) 363 { 364 pr_debug("%s API\n", __func__); 365 366 return do_checks(data, sample, ctx, false); 367 } 368 369 int stop(void *data, void *ctx) 370 { 371 static bool called; 372 373 pr_debug("%s API\n", __func__); 374 375 CHECK(data && filt_dat == data && !called); 376 called = true; 377 378 free(data); 379 filt_dat = NULL; 380 return 0; 381 } 382 383 const char *filter_description(const char **long_description) 384 { 385 *long_description = "Filter used by the 'dlfilter C API' perf test"; 386 return "dlfilter to test v2 C API"; 387 } 388
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.