1 /* 2 * builtin-trace.c 3 * 4 * Builtin 'trace' command: 5 * 6 * Display a continuously updated trace of any workload, CPU, specific PID, 7 * system wide, etc. Default format is loosely strace like, but any other 8 * event may be specified using --event. 9 * 10 * Copyright (C) 2012, 2013, 2014, 2015 Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> 11 * 12 * Initially based on the 'trace' prototype by Thomas Gleixner: 13 * 14 * http://lwn.net/Articles/415728/ ("Announcing a new utility: 'trace'") 15 */ 16 17 #include <traceevent/event-parse.h> 18 #include <api/fs/tracing_path.h> 19 #include <bpf/bpf.h> 20 #include "util/bpf_map.h" 21 #include "util/rlimit.h" 22 #include "builtin.h" 23 #include "util/cgroup.h" 24 #include "util/color.h" 25 #include "util/config.h" 26 #include "util/debug.h" 27 #include "util/env.h" 28 #include "util/event.h" 29 #include "util/evlist.h" 30 #include "util/evswitch.h" 31 #include <subcmd/exec-cmd.h> 32 #include "util/machine.h" 33 #include "util/map.h" 34 #include "util/symbol.h" 35 #include "util/path.h" 36 #include "util/session.h" 37 #include "util/thread.h" 38 #include <subcmd/parse-options.h> 39 #include "util/strlist.h" 40 #include "util/intlist.h" 41 #include "util/thread_map.h" 42 #include "util/stat.h" 43 #include "trace/beauty/beauty.h" 44 #include "trace-event.h" 45 #include "util/parse-events.h" 46 #include "util/bpf-loader.h" 47 #include "callchain.h" 48 #include "print_binary.h" 49 #include "string2.h" 50 #include "syscalltbl.h" 51 #include "rb_resort.h" 52 53 #include <errno.h> 54 #include <inttypes.h> 55 #include <poll.h> 56 #include <signal.h> 57 #include <stdlib.h> 58 #include <string.h> 59 #include <linux/err.h> 60 #include <linux/filter.h> 61 #include <linux/kernel.h> 62 #include <linux/random.h> 63 #include <linux/stringify.h> 64 #include <linux/time64.h> 65 #include <linux/zalloc.h> 66 #include <fcntl.h> 67 #include <sys/sysmacros.h> 68 69 #include <linux/ctype.h> 70 71 #ifndef O_CLOEXEC 72 # define O_CLOEXEC 02000000 73 #endif 74 75 #ifndef F_LINUX_SPECIFIC_BASE 76 # define F_LINUX_SPECIFIC_BASE 1024 77 #endif 78 79 struct trace { 80 struct perf_tool tool; 81 struct syscalltbl *sctbl; 82 struct { 83 struct syscall *table; 84 struct bpf_map *map; 85 struct { // per syscall BPF_MAP_TYPE_PROG_ARRAY 86 struct bpf_map *sys_enter, 87 *sys_exit; 88 } prog_array; 89 struct { 90 struct evsel *sys_enter, 91 *sys_exit, 92 *augmented; 93 } events; 94 struct bpf_program *unaugmented_prog; 95 } syscalls; 96 struct { 97 struct bpf_map *map; 98 } dump; 99 struct record_opts opts; 100 struct evlist *evlist; 101 struct machine *host; 102 struct thread *current; 103 struct bpf_object *bpf_obj; 104 struct cgroup *cgroup; 105 u64 base_time; 106 FILE *output; 107 unsigned long nr_events; 108 unsigned long nr_events_printed; 109 unsigned long max_events; 110 struct evswitch evswitch; 111 struct strlist *ev_qualifier; 112 struct { 113 size_t nr; 114 int *entries; 115 } ev_qualifier_ids; 116 struct { 117 size_t nr; 118 pid_t *entries; 119 struct bpf_map *map; 120 } filter_pids; 121 double duration_filter; 122 double runtime_ms; 123 struct { 124 u64 vfs_getname, 125 proc_getname; 126 } stats; 127 unsigned int max_stack; 128 unsigned int min_stack; 129 int raw_augmented_syscalls_args_size; 130 bool raw_augmented_syscalls; 131 bool fd_path_disabled; 132 bool sort_events; 133 bool not_ev_qualifier; 134 bool live; 135 bool full_time; 136 bool sched; 137 bool multiple_threads; 138 bool summary; 139 bool summary_only; 140 bool failure_only; 141 bool show_comm; 142 bool print_sample; 143 bool show_tool_stats; 144 bool trace_syscalls; 145 bool kernel_syscallchains; 146 s16 args_alignment; 147 bool show_tstamp; 148 bool show_duration; 149 bool show_zeros; 150 bool show_arg_names; 151 bool show_string_prefix; 152 bool force; 153 bool vfs_getname; 154 int trace_pgfaults; 155 struct { 156 struct ordered_events data; 157 u64 last; 158 } oe; 159 }; 160 161 struct tp_field { 162 int offset; 163 union { 164 u64 (*integer)(struct tp_field *field, struct perf_sample *sample); 165 void *(*pointer)(struct tp_field *field, struct perf_sample *sample); 166 }; 167 }; 168 169 #define TP_UINT_FIELD(bits) \ 170 static u64 tp_field__u##bits(struct tp_field *field, struct perf_sample *sample) \ 171 { \ 172 u##bits value; \ 173 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \ 174 return value; \ 175 } 176 177 TP_UINT_FIELD(8); 178 TP_UINT_FIELD(16); 179 TP_UINT_FIELD(32); 180 TP_UINT_FIELD(64); 181 182 #define TP_UINT_FIELD__SWAPPED(bits) \ 183 static u64 tp_field__swapped_u##bits(struct tp_field *field, struct perf_sample *sample) \ 184 { \ 185 u##bits value; \ 186 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \ 187 return bswap_##bits(value);\ 188 } 189 190 TP_UINT_FIELD__SWAPPED(16); 191 TP_UINT_FIELD__SWAPPED(32); 192 TP_UINT_FIELD__SWAPPED(64); 193 194 static int __tp_field__init_uint(struct tp_field *field, int size, int offset, bool needs_swap) 195 { 196 field->offset = offset; 197 198 switch (size) { 199 case 1: 200 field->integer = tp_field__u8; 201 break; 202 case 2: 203 field->integer = needs_swap ? tp_field__swapped_u16 : tp_field__u16; 204 break; 205 case 4: 206 field->integer = needs_swap ? tp_field__swapped_u32 : tp_field__u32; 207 break; 208 case 8: 209 field->integer = needs_swap ? tp_field__swapped_u64 : tp_field__u64; 210 break; 211 default: 212 return -1; 213 } 214 215 return 0; 216 } 217 218 static int tp_field__init_uint(struct tp_field *field, struct tep_format_field *format_field, bool needs_swap) 219 { 220 return __tp_field__init_uint(field, format_field->size, format_field->offset, needs_swap); 221 } 222 223 static void *tp_field__ptr(struct tp_field *field, struct perf_sample *sample) 224 { 225 return sample->raw_data + field->offset; 226 } 227 228 static int __tp_field__init_ptr(struct tp_field *field, int offset) 229 { 230 field->offset = offset; 231 field->pointer = tp_field__ptr; 232 return 0; 233 } 234 235 static int tp_field__init_ptr(struct tp_field *field, struct tep_format_field *format_field) 236 { 237 return __tp_field__init_ptr(field, format_field->offset); 238 } 239 240 struct syscall_tp { 241 struct tp_field id; 242 union { 243 struct tp_field args, ret; 244 }; 245 }; 246 247 static int perf_evsel__init_tp_uint_field(struct evsel *evsel, 248 struct tp_field *field, 249 const char *name) 250 { 251 struct tep_format_field *format_field = perf_evsel__field(evsel, name); 252 253 if (format_field == NULL) 254 return -1; 255 256 return tp_field__init_uint(field, format_field, evsel->needs_swap); 257 } 258 259 #define perf_evsel__init_sc_tp_uint_field(evsel, name) \ 260 ({ struct syscall_tp *sc = evsel->priv;\ 261 perf_evsel__init_tp_uint_field(evsel, &sc->name, #name); }) 262 263 static int perf_evsel__init_tp_ptr_field(struct evsel *evsel, 264 struct tp_field *field, 265 const char *name) 266 { 267 struct tep_format_field *format_field = perf_evsel__field(evsel, name); 268 269 if (format_field == NULL) 270 return -1; 271 272 return tp_field__init_ptr(field, format_field); 273 } 274 275 #define perf_evsel__init_sc_tp_ptr_field(evsel, name) \ 276 ({ struct syscall_tp *sc = evsel->priv;\ 277 perf_evsel__init_tp_ptr_field(evsel, &sc->name, #name); }) 278 279 static void evsel__delete_priv(struct evsel *evsel) 280 { 281 zfree(&evsel->priv); 282 evsel__delete(evsel); 283 } 284 285 static int perf_evsel__init_syscall_tp(struct evsel *evsel) 286 { 287 struct syscall_tp *sc = evsel->priv = malloc(sizeof(struct syscall_tp)); 288 289 if (evsel->priv != NULL) { 290 if (perf_evsel__init_tp_uint_field(evsel, &sc->id, "__syscall_nr") && 291 perf_evsel__init_tp_uint_field(evsel, &sc->id, "nr")) 292 goto out_delete; 293 return 0; 294 } 295 296 return -ENOMEM; 297 out_delete: 298 zfree(&evsel->priv); 299 return -ENOENT; 300 } 301 302 static int perf_evsel__init_augmented_syscall_tp(struct evsel *evsel, struct evsel *tp) 303 { 304 struct syscall_tp *sc = evsel->priv = malloc(sizeof(struct syscall_tp)); 305 306 if (evsel->priv != NULL) { 307 struct tep_format_field *syscall_id = perf_evsel__field(tp, "id"); 308 if (syscall_id == NULL) 309 syscall_id = perf_evsel__field(tp, "__syscall_nr"); 310 if (syscall_id == NULL) 311 goto out_delete; 312 if (__tp_field__init_uint(&sc->id, syscall_id->size, syscall_id->offset, evsel->needs_swap)) 313 goto out_delete; 314 315 return 0; 316 } 317 318 return -ENOMEM; 319 out_delete: 320 zfree(&evsel->priv); 321 return -EINVAL; 322 } 323 324 static int perf_evsel__init_augmented_syscall_tp_args(struct evsel *evsel) 325 { 326 struct syscall_tp *sc = evsel->priv; 327 328 return __tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64)); 329 } 330 331 static int perf_evsel__init_augmented_syscall_tp_ret(struct evsel *evsel) 332 { 333 struct syscall_tp *sc = evsel->priv; 334 335 return __tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap); 336 } 337 338 static int perf_evsel__init_raw_syscall_tp(struct evsel *evsel, void *handler) 339 { 340 evsel->priv = malloc(sizeof(struct syscall_tp)); 341 if (evsel->priv != NULL) { 342 if (perf_evsel__init_sc_tp_uint_field(evsel, id)) 343 goto out_delete; 344 345 evsel->handler = handler; 346 return 0; 347 } 348 349 return -ENOMEM; 350 351 out_delete: 352 zfree(&evsel->priv); 353 return -ENOENT; 354 } 355 356 static struct evsel *perf_evsel__raw_syscall_newtp(const char *direction, void *handler) 357 { 358 struct evsel *evsel = perf_evsel__newtp("raw_syscalls", direction); 359 360 /* older kernel (e.g., RHEL6) use syscalls:{enter,exit} */ 361 if (IS_ERR(evsel)) 362 evsel = perf_evsel__newtp("syscalls", direction); 363 364 if (IS_ERR(evsel)) 365 return NULL; 366 367 if (perf_evsel__init_raw_syscall_tp(evsel, handler)) 368 goto out_delete; 369 370 return evsel; 371 372 out_delete: 373 evsel__delete_priv(evsel); 374 return NULL; 375 } 376 377 #define perf_evsel__sc_tp_uint(evsel, name, sample) \ 378 ({ struct syscall_tp *fields = evsel->priv; \ 379 fields->name.integer(&fields->name, sample); }) 380 381 #define perf_evsel__sc_tp_ptr(evsel, name, sample) \ 382 ({ struct syscall_tp *fields = evsel->priv; \ 383 fields->name.pointer(&fields->name, sample); }) 384 385 size_t strarray__scnprintf(struct strarray *sa, char *bf, size_t size, const char *intfmt, bool show_prefix, int val) 386 { 387 int idx = val - sa->offset; 388 389 if (idx < 0 || idx >= sa->nr_entries || sa->entries[idx] == NULL) { 390 size_t printed = scnprintf(bf, size, intfmt, val); 391 if (show_prefix) 392 printed += scnprintf(bf + printed, size - printed, " /* %s??? */", sa->prefix); 393 return printed; 394 } 395 396 return scnprintf(bf, size, "%s%s", show_prefix ? sa->prefix : "", sa->entries[idx]); 397 } 398 399 static size_t __syscall_arg__scnprintf_strarray(char *bf, size_t size, 400 const char *intfmt, 401 struct syscall_arg *arg) 402 { 403 return strarray__scnprintf(arg->parm, bf, size, intfmt, arg->show_string_prefix, arg->val); 404 } 405 406 static size_t syscall_arg__scnprintf_strarray(char *bf, size_t size, 407 struct syscall_arg *arg) 408 { 409 return __syscall_arg__scnprintf_strarray(bf, size, "%d", arg); 410 } 411 412 #define SCA_STRARRAY syscall_arg__scnprintf_strarray 413 414 size_t syscall_arg__scnprintf_strarray_flags(char *bf, size_t size, struct syscall_arg *arg) 415 { 416 return strarray__scnprintf_flags(arg->parm, bf, size, arg->show_string_prefix, arg->val); 417 } 418 419 size_t strarrays__scnprintf(struct strarrays *sas, char *bf, size_t size, const char *intfmt, bool show_prefix, int val) 420 { 421 size_t printed; 422 int i; 423 424 for (i = 0; i < sas->nr_entries; ++i) { 425 struct strarray *sa = sas->entries[i]; 426 int idx = val - sa->offset; 427 428 if (idx >= 0 && idx < sa->nr_entries) { 429 if (sa->entries[idx] == NULL) 430 break; 431 return scnprintf(bf, size, "%s%s", show_prefix ? sa->prefix : "", sa->entries[idx]); 432 } 433 } 434 435 printed = scnprintf(bf, size, intfmt, val); 436 if (show_prefix) 437 printed += scnprintf(bf + printed, size - printed, " /* %s??? */", sas->entries[0]->prefix); 438 return printed; 439 } 440 441 size_t syscall_arg__scnprintf_strarrays(char *bf, size_t size, 442 struct syscall_arg *arg) 443 { 444 return strarrays__scnprintf(arg->parm, bf, size, "%d", arg->show_string_prefix, arg->val); 445 } 446 447 #ifndef AT_FDCWD 448 #define AT_FDCWD -100 449 #endif 450 451 static size_t syscall_arg__scnprintf_fd_at(char *bf, size_t size, 452 struct syscall_arg *arg) 453 { 454 int fd = arg->val; 455 const char *prefix = "AT_FD"; 456 457 if (fd == AT_FDCWD) 458 return scnprintf(bf, size, "%s%s", arg->show_string_prefix ? prefix : "", "CWD"); 459 460 return syscall_arg__scnprintf_fd(bf, size, arg); 461 } 462 463 #define SCA_FDAT syscall_arg__scnprintf_fd_at 464 465 static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size, 466 struct syscall_arg *arg); 467 468 #define SCA_CLOSE_FD syscall_arg__scnprintf_close_fd 469 470 size_t syscall_arg__scnprintf_hex(char *bf, size_t size, struct syscall_arg *arg) 471 { 472 return scnprintf(bf, size, "%#lx", arg->val); 473 } 474 475 size_t syscall_arg__scnprintf_ptr(char *bf, size_t size, struct syscall_arg *arg) 476 { 477 if (arg->val == 0) 478 return scnprintf(bf, size, "NULL"); 479 return syscall_arg__scnprintf_hex(bf, size, arg); 480 } 481 482 size_t syscall_arg__scnprintf_int(char *bf, size_t size, struct syscall_arg *arg) 483 { 484 return scnprintf(bf, size, "%d", arg->val); 485 } 486 487 size_t syscall_arg__scnprintf_long(char *bf, size_t size, struct syscall_arg *arg) 488 { 489 return scnprintf(bf, size, "%ld", arg->val); 490 } 491 492 static const char *bpf_cmd[] = { 493 "MAP_CREATE", "MAP_LOOKUP_ELEM", "MAP_UPDATE_ELEM", "MAP_DELETE_ELEM", 494 "MAP_GET_NEXT_KEY", "PROG_LOAD", 495 }; 496 static DEFINE_STRARRAY(bpf_cmd, "BPF_"); 497 498 static const char *fsmount_flags[] = { 499 [1] = "CLOEXEC", 500 }; 501 static DEFINE_STRARRAY(fsmount_flags, "FSMOUNT_"); 502 503 #include "trace/beauty/generated/fsconfig_arrays.c" 504 505 static DEFINE_STRARRAY(fsconfig_cmds, "FSCONFIG_"); 506 507 static const char *epoll_ctl_ops[] = { "ADD", "DEL", "MOD", }; 508 static DEFINE_STRARRAY_OFFSET(epoll_ctl_ops, "EPOLL_CTL_", 1); 509 510 static const char *itimers[] = { "REAL", "VIRTUAL", "PROF", }; 511 static DEFINE_STRARRAY(itimers, "ITIMER_"); 512 513 static const char *keyctl_options[] = { 514 "GET_KEYRING_ID", "JOIN_SESSION_KEYRING", "UPDATE", "REVOKE", "CHOWN", 515 "SETPERM", "DESCRIBE", "CLEAR", "LINK", "UNLINK", "SEARCH", "READ", 516 "INSTANTIATE", "NEGATE", "SET_REQKEY_KEYRING", "SET_TIMEOUT", 517 "ASSUME_AUTHORITY", "GET_SECURITY", "SESSION_TO_PARENT", "REJECT", 518 "INSTANTIATE_IOV", "INVALIDATE", "GET_PERSISTENT", 519 }; 520 static DEFINE_STRARRAY(keyctl_options, "KEYCTL_"); 521 522 static const char *whences[] = { "SET", "CUR", "END", 523 #ifdef SEEK_DATA 524 "DATA", 525 #endif 526 #ifdef SEEK_HOLE 527 "HOLE", 528 #endif 529 }; 530 static DEFINE_STRARRAY(whences, "SEEK_"); 531 532 static const char *fcntl_cmds[] = { 533 "DUPFD", "GETFD", "SETFD", "GETFL", "SETFL", "GETLK", "SETLK", 534 "SETLKW", "SETOWN", "GETOWN", "SETSIG", "GETSIG", "GETLK64", 535 "SETLK64", "SETLKW64", "SETOWN_EX", "GETOWN_EX", 536 "GETOWNER_UIDS", 537 }; 538 static DEFINE_STRARRAY(fcntl_cmds, "F_"); 539 540 static const char *fcntl_linux_specific_cmds[] = { 541 "SETLEASE", "GETLEASE", "NOTIFY", [5] = "CANCELLK", "DUPFD_CLOEXEC", 542 "SETPIPE_SZ", "GETPIPE_SZ", "ADD_SEALS", "GET_SEALS", 543 "GET_RW_HINT", "SET_RW_HINT", "GET_FILE_RW_HINT", "SET_FILE_RW_HINT", 544 }; 545 546 static DEFINE_STRARRAY_OFFSET(fcntl_linux_specific_cmds, "F_", F_LINUX_SPECIFIC_BASE); 547 548 static struct strarray *fcntl_cmds_arrays[] = { 549 &strarray__fcntl_cmds, 550 &strarray__fcntl_linux_specific_cmds, 551 }; 552 553 static DEFINE_STRARRAYS(fcntl_cmds_arrays); 554 555 static const char *rlimit_resources[] = { 556 "CPU", "FSIZE", "DATA", "STACK", "CORE", "RSS", "NPROC", "NOFILE", 557 "MEMLOCK", "AS", "LOCKS", "SIGPENDING", "MSGQUEUE", "NICE", "RTPRIO", 558 "RTTIME", 559 }; 560 static DEFINE_STRARRAY(rlimit_resources, "RLIMIT_"); 561 562 static const char *sighow[] = { "BLOCK", "UNBLOCK", "SETMASK", }; 563 static DEFINE_STRARRAY(sighow, "SIG_"); 564 565 static const char *clockid[] = { 566 "REALTIME", "MONOTONIC", "PROCESS_CPUTIME_ID", "THREAD_CPUTIME_ID", 567 "MONOTONIC_RAW", "REALTIME_COARSE", "MONOTONIC_COARSE", "BOOTTIME", 568 "REALTIME_ALARM", "BOOTTIME_ALARM", "SGI_CYCLE", "TAI" 569 }; 570 static DEFINE_STRARRAY(clockid, "CLOCK_"); 571 572 static size_t syscall_arg__scnprintf_access_mode(char *bf, size_t size, 573 struct syscall_arg *arg) 574 { 575 bool show_prefix = arg->show_string_prefix; 576 const char *suffix = "_OK"; 577 size_t printed = 0; 578 int mode = arg->val; 579 580 if (mode == F_OK) /* 0 */ 581 return scnprintf(bf, size, "F%s", show_prefix ? suffix : ""); 582 #define P_MODE(n) \ 583 if (mode & n##_OK) { \ 584 printed += scnprintf(bf + printed, size - printed, "%s%s", #n, show_prefix ? suffix : ""); \ 585 mode &= ~n##_OK; \ 586 } 587 588 P_MODE(R); 589 P_MODE(W); 590 P_MODE(X); 591 #undef P_MODE 592 593 if (mode) 594 printed += scnprintf(bf + printed, size - printed, "|%#x", mode); 595 596 return printed; 597 } 598 599 #define SCA_ACCMODE syscall_arg__scnprintf_access_mode 600 601 static size_t syscall_arg__scnprintf_filename(char *bf, size_t size, 602 struct syscall_arg *arg); 603 604 #define SCA_FILENAME syscall_arg__scnprintf_filename 605 606 static size_t syscall_arg__scnprintf_pipe_flags(char *bf, size_t size, 607 struct syscall_arg *arg) 608 { 609 bool show_prefix = arg->show_string_prefix; 610 const char *prefix = "O_"; 611 int printed = 0, flags = arg->val; 612 613 #define P_FLAG(n) \ 614 if (flags & O_##n) { \ 615 printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", #n); \ 616 flags &= ~O_##n; \ 617 } 618 619 P_FLAG(CLOEXEC); 620 P_FLAG(NONBLOCK); 621 #undef P_FLAG 622 623 if (flags) 624 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags); 625 626 return printed; 627 } 628 629 #define SCA_PIPE_FLAGS syscall_arg__scnprintf_pipe_flags 630 631 #ifndef GRND_NONBLOCK 632 #define GRND_NONBLOCK 0x0001 633 #endif 634 #ifndef GRND_RANDOM 635 #define GRND_RANDOM 0x0002 636 #endif 637 638 static size_t syscall_arg__scnprintf_getrandom_flags(char *bf, size_t size, 639 struct syscall_arg *arg) 640 { 641 bool show_prefix = arg->show_string_prefix; 642 const char *prefix = "GRND_"; 643 int printed = 0, flags = arg->val; 644 645 #define P_FLAG(n) \ 646 if (flags & GRND_##n) { \ 647 printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", #n); \ 648 flags &= ~GRND_##n; \ 649 } 650 651 P_FLAG(RANDOM); 652 P_FLAG(NONBLOCK); 653 #undef P_FLAG 654 655 if (flags) 656 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags); 657 658 return printed; 659 } 660 661 #define SCA_GETRANDOM_FLAGS syscall_arg__scnprintf_getrandom_flags 662 663 #define STRARRAY(name, array) \ 664 { .scnprintf = SCA_STRARRAY, \ 665 .parm = &strarray__##array, } 666 667 #define STRARRAY_FLAGS(name, array) \ 668 { .scnprintf = SCA_STRARRAY_FLAGS, \ 669 .parm = &strarray__##array, } 670 671 #include "trace/beauty/arch_errno_names.c" 672 #include "trace/beauty/eventfd.c" 673 #include "trace/beauty/futex_op.c" 674 #include "trace/beauty/futex_val3.c" 675 #include "trace/beauty/mmap.c" 676 #include "trace/beauty/mode_t.c" 677 #include "trace/beauty/msg_flags.c" 678 #include "trace/beauty/open_flags.c" 679 #include "trace/beauty/perf_event_open.c" 680 #include "trace/beauty/pid.c" 681 #include "trace/beauty/sched_policy.c" 682 #include "trace/beauty/seccomp.c" 683 #include "trace/beauty/signum.c" 684 #include "trace/beauty/socket_type.c" 685 #include "trace/beauty/waitid_options.c" 686 687 struct syscall_arg_fmt { 688 size_t (*scnprintf)(char *bf, size_t size, struct syscall_arg *arg); 689 unsigned long (*mask_val)(struct syscall_arg *arg, unsigned long val); 690 void *parm; 691 const char *name; 692 bool show_zero; 693 }; 694 695 static struct syscall_fmt { 696 const char *name; 697 const char *alias; 698 struct { 699 const char *sys_enter, 700 *sys_exit; 701 } bpf_prog_name; 702 struct syscall_arg_fmt arg[6]; 703 u8 nr_args; 704 bool errpid; 705 bool timeout; 706 bool hexret; 707 } syscall_fmts[] = { 708 { .name = "access", 709 .arg = { [1] = { .scnprintf = SCA_ACCMODE, /* mode */ }, }, }, 710 { .name = "arch_prctl", 711 .arg = { [0] = { .scnprintf = SCA_X86_ARCH_PRCTL_CODE, /* code */ }, 712 [1] = { .scnprintf = SCA_PTR, /* arg2 */ }, }, }, 713 { .name = "bind", 714 .arg = { [0] = { .scnprintf = SCA_INT, /* fd */ }, 715 [1] = { .scnprintf = SCA_SOCKADDR, /* umyaddr */ }, 716 [2] = { .scnprintf = SCA_INT, /* addrlen */ }, }, }, 717 { .name = "bpf", 718 .arg = { [0] = STRARRAY(cmd, bpf_cmd), }, }, 719 { .name = "brk", .hexret = true, 720 .arg = { [0] = { .scnprintf = SCA_PTR, /* brk */ }, }, }, 721 { .name = "clock_gettime", 722 .arg = { [0] = STRARRAY(clk_id, clockid), }, }, 723 { .name = "clone", .errpid = true, .nr_args = 5, 724 .arg = { [0] = { .name = "flags", .scnprintf = SCA_CLONE_FLAGS, }, 725 [1] = { .name = "child_stack", .scnprintf = SCA_HEX, }, 726 [2] = { .name = "parent_tidptr", .scnprintf = SCA_HEX, }, 727 [3] = { .name = "child_tidptr", .scnprintf = SCA_HEX, }, 728 [4] = { .name = "tls", .scnprintf = SCA_HEX, }, }, }, 729 { .name = "close", 730 .arg = { [0] = { .scnprintf = SCA_CLOSE_FD, /* fd */ }, }, }, 731 { .name = "connect", 732 .arg = { [0] = { .scnprintf = SCA_INT, /* fd */ }, 733 [1] = { .scnprintf = SCA_SOCKADDR, /* servaddr */ }, 734 [2] = { .scnprintf = SCA_INT, /* addrlen */ }, }, }, 735 { .name = "epoll_ctl", 736 .arg = { [1] = STRARRAY(op, epoll_ctl_ops), }, }, 737 { .name = "eventfd2", 738 .arg = { [1] = { .scnprintf = SCA_EFD_FLAGS, /* flags */ }, }, }, 739 { .name = "fchmodat", 740 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, }, 741 { .name = "fchownat", 742 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, }, 743 { .name = "fcntl", 744 .arg = { [1] = { .scnprintf = SCA_FCNTL_CMD, /* cmd */ 745 .parm = &strarrays__fcntl_cmds_arrays, 746 .show_zero = true, }, 747 [2] = { .scnprintf = SCA_FCNTL_ARG, /* arg */ }, }, }, 748 { .name = "flock", 749 .arg = { [1] = { .scnprintf = SCA_FLOCK, /* cmd */ }, }, }, 750 { .name = "fsconfig", 751 .arg = { [1] = STRARRAY(cmd, fsconfig_cmds), }, }, 752 { .name = "fsmount", 753 .arg = { [1] = STRARRAY_FLAGS(flags, fsmount_flags), 754 [2] = { .scnprintf = SCA_FSMOUNT_ATTR_FLAGS, /* attr_flags */ }, }, }, 755 { .name = "fspick", 756 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, 757 [1] = { .scnprintf = SCA_FILENAME, /* path */ }, 758 [2] = { .scnprintf = SCA_FSPICK_FLAGS, /* flags */ }, }, }, 759 { .name = "fstat", .alias = "newfstat", }, 760 { .name = "fstatat", .alias = "newfstatat", }, 761 { .name = "futex", 762 .arg = { [1] = { .scnprintf = SCA_FUTEX_OP, /* op */ }, 763 [5] = { .scnprintf = SCA_FUTEX_VAL3, /* val3 */ }, }, }, 764 { .name = "futimesat", 765 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, }, 766 { .name = "getitimer", 767 .arg = { [0] = STRARRAY(which, itimers), }, }, 768 { .name = "getpid", .errpid = true, }, 769 { .name = "getpgid", .errpid = true, }, 770 { .name = "getppid", .errpid = true, }, 771 { .name = "getrandom", 772 .arg = { [2] = { .scnprintf = SCA_GETRANDOM_FLAGS, /* flags */ }, }, }, 773 { .name = "getrlimit", 774 .arg = { [0] = STRARRAY(resource, rlimit_resources), }, }, 775 { .name = "gettid", .errpid = true, }, 776 { .name = "ioctl", 777 .arg = { 778 #if defined(__i386__) || defined(__x86_64__) 779 /* 780 * FIXME: Make this available to all arches. 781 */ 782 [1] = { .scnprintf = SCA_IOCTL_CMD, /* cmd */ }, 783 [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, }, 784 #else 785 [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, }, 786 #endif 787 { .name = "kcmp", .nr_args = 5, 788 .arg = { [0] = { .name = "pid1", .scnprintf = SCA_PID, }, 789 [1] = { .name = "pid2", .scnprintf = SCA_PID, }, 790 [2] = { .name = "type", .scnprintf = SCA_KCMP_TYPE, }, 791 [3] = { .name = "idx1", .scnprintf = SCA_KCMP_IDX, }, 792 [4] = { .name = "idx2", .scnprintf = SCA_KCMP_IDX, }, }, }, 793 { .name = "keyctl", 794 .arg = { [0] = STRARRAY(option, keyctl_options), }, }, 795 { .name = "kill", 796 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, }, 797 { .name = "linkat", 798 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, }, 799 { .name = "lseek", 800 .arg = { [2] = STRARRAY(whence, whences), }, }, 801 { .name = "lstat", .alias = "newlstat", }, 802 { .name = "madvise", 803 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ }, 804 [2] = { .scnprintf = SCA_MADV_BHV, /* behavior */ }, }, }, 805 { .name = "mkdirat", 806 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, }, 807 { .name = "mknodat", 808 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, }, 809 { .name = "mmap", .hexret = true, 810 /* The standard mmap maps to old_mmap on s390x */ 811 #if defined(__s390x__) 812 .alias = "old_mmap", 813 #endif 814 .arg = { [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ }, 815 [3] = { .scnprintf = SCA_MMAP_FLAGS, /* flags */ }, 816 [5] = { .scnprintf = SCA_HEX, /* offset */ }, }, }, 817 { .name = "mount", 818 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* dev_name */ }, 819 [3] = { .scnprintf = SCA_MOUNT_FLAGS, /* flags */ 820 .mask_val = SCAMV_MOUNT_FLAGS, /* flags */ }, }, }, 821 { .name = "move_mount", 822 .arg = { [0] = { .scnprintf = SCA_FDAT, /* from_dfd */ }, 823 [1] = { .scnprintf = SCA_FILENAME, /* from_pathname */ }, 824 [2] = { .scnprintf = SCA_FDAT, /* to_dfd */ }, 825 [3] = { .scnprintf = SCA_FILENAME, /* to_pathname */ }, 826 [4] = { .scnprintf = SCA_MOVE_MOUNT_FLAGS, /* flags */ }, }, }, 827 { .name = "mprotect", 828 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ }, 829 [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ }, }, }, 830 { .name = "mq_unlink", 831 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* u_name */ }, }, }, 832 { .name = "mremap", .hexret = true, 833 .arg = { [3] = { .scnprintf = SCA_MREMAP_FLAGS, /* flags */ }, }, }, 834 { .name = "name_to_handle_at", 835 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, }, 836 { .name = "newfstatat", 837 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, }, 838 { .name = "open", 839 .arg = { [1] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, }, 840 { .name = "open_by_handle_at", 841 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, 842 [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, }, 843 { .name = "openat", 844 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, 845 [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, }, 846 { .name = "perf_event_open", 847 .arg = { [2] = { .scnprintf = SCA_INT, /* cpu */ }, 848 [3] = { .scnprintf = SCA_FD, /* group_fd */ }, 849 [4] = { .scnprintf = SCA_PERF_FLAGS, /* flags */ }, }, }, 850 { .name = "pipe2", 851 .arg = { [1] = { .scnprintf = SCA_PIPE_FLAGS, /* flags */ }, }, }, 852 { .name = "pkey_alloc", 853 .arg = { [1] = { .scnprintf = SCA_PKEY_ALLOC_ACCESS_RIGHTS, /* access_rights */ }, }, }, 854 { .name = "pkey_free", 855 .arg = { [0] = { .scnprintf = SCA_INT, /* key */ }, }, }, 856 { .name = "pkey_mprotect", 857 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ }, 858 [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ }, 859 [3] = { .scnprintf = SCA_INT, /* pkey */ }, }, }, 860 { .name = "poll", .timeout = true, }, 861 { .name = "ppoll", .timeout = true, }, 862 { .name = "prctl", 863 .arg = { [0] = { .scnprintf = SCA_PRCTL_OPTION, /* option */ }, 864 [1] = { .scnprintf = SCA_PRCTL_ARG2, /* arg2 */ }, 865 [2] = { .scnprintf = SCA_PRCTL_ARG3, /* arg3 */ }, }, }, 866 { .name = "pread", .alias = "pread64", }, 867 { .name = "preadv", .alias = "pread", }, 868 { .name = "prlimit64", 869 .arg = { [1] = STRARRAY(resource, rlimit_resources), }, }, 870 { .name = "pwrite", .alias = "pwrite64", }, 871 { .name = "readlinkat", 872 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, }, 873 { .name = "recvfrom", 874 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, }, 875 { .name = "recvmmsg", 876 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, }, 877 { .name = "recvmsg", 878 .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, }, 879 { .name = "renameat", 880 .arg = { [0] = { .scnprintf = SCA_FDAT, /* olddirfd */ }, 881 [2] = { .scnprintf = SCA_FDAT, /* newdirfd */ }, }, }, 882 { .name = "renameat2", 883 .arg = { [0] = { .scnprintf = SCA_FDAT, /* olddirfd */ }, 884 [2] = { .scnprintf = SCA_FDAT, /* newdirfd */ }, 885 [4] = { .scnprintf = SCA_RENAMEAT2_FLAGS, /* flags */ }, }, }, 886 { .name = "rt_sigaction", 887 .arg = { [0] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, }, 888 { .name = "rt_sigprocmask", 889 .arg = { [0] = STRARRAY(how, sighow), }, }, 890 { .name = "rt_sigqueueinfo", 891 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, }, 892 { .name = "rt_tgsigqueueinfo", 893 .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, }, 894 { .name = "sched_setscheduler", 895 .arg = { [1] = { .scnprintf = SCA_SCHED_POLICY, /* policy */ }, }, }, 896 { .name = "seccomp", 897 .arg = { [0] = { .scnprintf = SCA_SECCOMP_OP, /* op */ }, 898 [1] = { .scnprintf = SCA_SECCOMP_FLAGS, /* flags */ }, }, }, 899 { .name = "select", .timeout = true, }, 900 { .name = "sendfile", .alias = "sendfile64", }, 901 { .name = "sendmmsg", 902 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, }, 903 { .name = "sendmsg", 904 .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, }, 905 { .name = "sendto", 906 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, 907 [4] = { .scnprintf = SCA_SOCKADDR, /* addr */ }, }, }, 908 { .name = "set_tid_address", .errpid = true, }, 909 { .name = "setitimer", 910 .arg = { [0] = STRARRAY(which, itimers), }, }, 911 { .name = "setrlimit", 912 .arg = { [0] = STRARRAY(resource, rlimit_resources), }, }, 913 { .name = "socket", 914 .arg = { [0] = STRARRAY(family, socket_families), 915 [1] = { .scnprintf = SCA_SK_TYPE, /* type */ }, 916 [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, }, 917 { .name = "socketpair", 918 .arg = { [0] = STRARRAY(family, socket_families), 919 [1] = { .scnprintf = SCA_SK_TYPE, /* type */ }, 920 [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, }, 921 { .name = "stat", .alias = "newstat", }, 922 { .name = "statx", 923 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fdat */ }, 924 [2] = { .scnprintf = SCA_STATX_FLAGS, /* flags */ } , 925 [3] = { .scnprintf = SCA_STATX_MASK, /* mask */ }, }, }, 926 { .name = "swapoff", 927 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, }, 928 { .name = "swapon", 929 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, }, 930 { .name = "symlinkat", 931 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, }, 932 { .name = "sync_file_range", 933 .arg = { [3] = { .scnprintf = SCA_SYNC_FILE_RANGE_FLAGS, /* flags */ }, }, }, 934 { .name = "tgkill", 935 .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, }, 936 { .name = "tkill", 937 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, }, 938 { .name = "umount2", .alias = "umount", 939 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* name */ }, }, }, 940 { .name = "uname", .alias = "newuname", }, 941 { .name = "unlinkat", 942 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, }, 943 { .name = "utimensat", 944 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dirfd */ }, }, }, 945 { .name = "wait4", .errpid = true, 946 .arg = { [2] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, }, 947 { .name = "waitid", .errpid = true, 948 .arg = { [3] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, }, 949 }; 950 951 static int syscall_fmt__cmp(const void *name, const void *fmtp) 952 { 953 const struct syscall_fmt *fmt = fmtp; 954 return strcmp(name, fmt->name); 955 } 956 957 static struct syscall_fmt *syscall_fmt__find(const char *name) 958 { 959 const int nmemb = ARRAY_SIZE(syscall_fmts); 960 return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp); 961 } 962 963 static struct syscall_fmt *syscall_fmt__find_by_alias(const char *alias) 964 { 965 int i, nmemb = ARRAY_SIZE(syscall_fmts); 966 967 for (i = 0; i < nmemb; ++i) { 968 if (syscall_fmts[i].alias && strcmp(syscall_fmts[i].alias, alias) == 0) 969 return &syscall_fmts[i]; 970 } 971 972 return NULL; 973 } 974 975 /* 976 * is_exit: is this "exit" or "exit_group"? 977 * is_open: is this "open" or "openat"? To associate the fd returned in sys_exit with the pathname in sys_enter. 978 * args_size: sum of the sizes of the syscall arguments, anything after that is augmented stuff: pathname for openat, etc. 979 * nonexistent: Just a hole in the syscall table, syscall id not allocated 980 */ 981 struct syscall { 982 struct tep_event *tp_format; 983 int nr_args; 984 int args_size; 985 struct { 986 struct bpf_program *sys_enter, 987 *sys_exit; 988 } bpf_prog; 989 bool is_exit; 990 bool is_open; 991 bool nonexistent; 992 struct tep_format_field *args; 993 const char *name; 994 struct syscall_fmt *fmt; 995 struct syscall_arg_fmt *arg_fmt; 996 }; 997 998 /* 999 * Must match what is in the BPF program: 1000 * 1001 * tools/perf/examples/bpf/augmented_raw_syscalls.c 1002 */ 1003 struct bpf_map_syscall_entry { 1004 bool enabled; 1005 u16 string_args_len[6]; 1006 }; 1007 1008 /* 1009 * We need to have this 'calculated' boolean because in some cases we really 1010 * don't know what is the duration of a syscall, for instance, when we start 1011 * a session and some threads are waiting for a syscall to finish, say 'poll', 1012 * in which case all we can do is to print "( ? ) for duration and for the 1013 * start timestamp. 1014 */ 1015 static size_t fprintf_duration(unsigned long t, bool calculated, FILE *fp) 1016 { 1017 double duration = (double)t / NSEC_PER_MSEC; 1018 size_t printed = fprintf(fp, "("); 1019 1020 if (!calculated) 1021 printed += fprintf(fp, " "); 1022 else if (duration >= 1.0) 1023 printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration); 1024 else if (duration >= 0.01) 1025 printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration); 1026 else 1027 printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration); 1028 return printed + fprintf(fp, "): "); 1029 } 1030 1031 /** 1032 * filename.ptr: The filename char pointer that will be vfs_getname'd 1033 * filename.entry_str_pos: Where to insert the string translated from 1034 * filename.ptr by the vfs_getname tracepoint/kprobe. 1035 * ret_scnprintf: syscall args may set this to a different syscall return 1036 * formatter, for instance, fcntl may return fds, file flags, etc. 1037 */ 1038 struct thread_trace { 1039 u64 entry_time; 1040 bool entry_pending; 1041 unsigned long nr_events; 1042 unsigned long pfmaj, pfmin; 1043 char *entry_str; 1044 double runtime_ms; 1045 size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg); 1046 struct { 1047 unsigned long ptr; 1048 short int entry_str_pos; 1049 bool pending_open; 1050 unsigned int namelen; 1051 char *name; 1052 } filename; 1053 struct { 1054 int max; 1055 struct file *table; 1056 } files; 1057 1058 struct intlist *syscall_stats; 1059 }; 1060 1061 static struct thread_trace *thread_trace__new(void) 1062 { 1063 struct thread_trace *ttrace = zalloc(sizeof(struct thread_trace)); 1064 1065 if (ttrace) { 1066 ttrace->files.max = -1; 1067 ttrace->syscall_stats = intlist__new(NULL); 1068 } 1069 1070 return ttrace; 1071 } 1072 1073 static struct thread_trace *thread__trace(struct thread *thread, FILE *fp) 1074 { 1075 struct thread_trace *ttrace; 1076 1077 if (thread == NULL) 1078 goto fail; 1079 1080 if (thread__priv(thread) == NULL) 1081 thread__set_priv(thread, thread_trace__new()); 1082 1083 if (thread__priv(thread) == NULL) 1084 goto fail; 1085 1086 ttrace = thread__priv(thread); 1087 ++ttrace->nr_events; 1088 1089 return ttrace; 1090 fail: 1091 color_fprintf(fp, PERF_COLOR_RED, 1092 "WARNING: not enough memory, dropping samples!\n"); 1093 return NULL; 1094 } 1095 1096 1097 void syscall_arg__set_ret_scnprintf(struct syscall_arg *arg, 1098 size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg)) 1099 { 1100 struct thread_trace *ttrace = thread__priv(arg->thread); 1101 1102 ttrace->ret_scnprintf = ret_scnprintf; 1103 } 1104 1105 #define TRACE_PFMAJ (1 << 0) 1106 #define TRACE_PFMIN (1 << 1) 1107 1108 static const size_t trace__entry_str_size = 2048; 1109 1110 static struct file *thread_trace__files_entry(struct thread_trace *ttrace, int fd) 1111 { 1112 if (fd < 0) 1113 return NULL; 1114 1115 if (fd > ttrace->files.max) { 1116 struct file *nfiles = realloc(ttrace->files.table, (fd + 1) * sizeof(struct file)); 1117 1118 if (nfiles == NULL) 1119 return NULL; 1120 1121 if (ttrace->files.max != -1) { 1122 memset(nfiles + ttrace->files.max + 1, 0, 1123 (fd - ttrace->files.max) * sizeof(struct file)); 1124 } else { 1125 memset(nfiles, 0, (fd + 1) * sizeof(struct file)); 1126 } 1127 1128 ttrace->files.table = nfiles; 1129 ttrace->files.max = fd; 1130 } 1131 1132 return ttrace->files.table + fd; 1133 } 1134 1135 struct file *thread__files_entry(struct thread *thread, int fd) 1136 { 1137 return thread_trace__files_entry(thread__priv(thread), fd); 1138 } 1139 1140 static int trace__set_fd_pathname(struct thread *thread, int fd, const char *pathname) 1141 { 1142 struct thread_trace *ttrace = thread__priv(thread); 1143 struct file *file = thread_trace__files_entry(ttrace, fd); 1144 1145 if (file != NULL) { 1146 struct stat st; 1147 if (stat(pathname, &st) == 0) 1148 file->dev_maj = major(st.st_rdev); 1149 file->pathname = strdup(pathname); 1150 if (file->pathname) 1151 return 0; 1152 } 1153 1154 return -1; 1155 } 1156 1157 static int thread__read_fd_path(struct thread *thread, int fd) 1158 { 1159 char linkname[PATH_MAX], pathname[PATH_MAX]; 1160 struct stat st; 1161 int ret; 1162 1163 if (thread->pid_ == thread->tid) { 1164 scnprintf(linkname, sizeof(linkname), 1165 "/proc/%d/fd/%d", thread->pid_, fd); 1166 } else { 1167 scnprintf(linkname, sizeof(linkname), 1168 "/proc/%d/task/%d/fd/%d", thread->pid_, thread->tid, fd); 1169 } 1170 1171 if (lstat(linkname, &st) < 0 || st.st_size + 1 > (off_t)sizeof(pathname)) 1172 return -1; 1173 1174 ret = readlink(linkname, pathname, sizeof(pathname)); 1175 1176 if (ret < 0 || ret > st.st_size) 1177 return -1; 1178 1179 pathname[ret] = '\0'; 1180 return trace__set_fd_pathname(thread, fd, pathname); 1181 } 1182 1183 static const char *thread__fd_path(struct thread *thread, int fd, 1184 struct trace *trace) 1185 { 1186 struct thread_trace *ttrace = thread__priv(thread); 1187 1188 if (ttrace == NULL || trace->fd_path_disabled) 1189 return NULL; 1190 1191 if (fd < 0) 1192 return NULL; 1193 1194 if ((fd > ttrace->files.max || ttrace->files.table[fd].pathname == NULL)) { 1195 if (!trace->live) 1196 return NULL; 1197 ++trace->stats.proc_getname; 1198 if (thread__read_fd_path(thread, fd)) 1199 return NULL; 1200 } 1201 1202 return ttrace->files.table[fd].pathname; 1203 } 1204 1205 size_t syscall_arg__scnprintf_fd(char *bf, size_t size, struct syscall_arg *arg) 1206 { 1207 int fd = arg->val; 1208 size_t printed = scnprintf(bf, size, "%d", fd); 1209 const char *path = thread__fd_path(arg->thread, fd, arg->trace); 1210 1211 if (path) 1212 printed += scnprintf(bf + printed, size - printed, "<%s>", path); 1213 1214 return printed; 1215 } 1216 1217 size_t pid__scnprintf_fd(struct trace *trace, pid_t pid, int fd, char *bf, size_t size) 1218 { 1219 size_t printed = scnprintf(bf, size, "%d", fd); 1220 struct thread *thread = machine__find_thread(trace->host, pid, pid); 1221 1222 if (thread) { 1223 const char *path = thread__fd_path(thread, fd, trace); 1224 1225 if (path) 1226 printed += scnprintf(bf + printed, size - printed, "<%s>", path); 1227 1228 thread__put(thread); 1229 } 1230 1231 return printed; 1232 } 1233 1234 static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size, 1235 struct syscall_arg *arg) 1236 { 1237 int fd = arg->val; 1238 size_t printed = syscall_arg__scnprintf_fd(bf, size, arg); 1239 struct thread_trace *ttrace = thread__priv(arg->thread); 1240 1241 if (ttrace && fd >= 0 && fd <= ttrace->files.max) 1242 zfree(&ttrace->files.table[fd].pathname); 1243 1244 return printed; 1245 } 1246 1247 static void thread__set_filename_pos(struct thread *thread, const char *bf, 1248 unsigned long ptr) 1249 { 1250 struct thread_trace *ttrace = thread__priv(thread); 1251 1252 ttrace->filename.ptr = ptr; 1253 ttrace->filename.entry_str_pos = bf - ttrace->entry_str; 1254 } 1255 1256 static size_t syscall_arg__scnprintf_augmented_string(struct syscall_arg *arg, char *bf, size_t size) 1257 { 1258 struct augmented_arg *augmented_arg = arg->augmented.args; 1259 size_t printed = scnprintf(bf, size, "\"%.*s\"", augmented_arg->size, augmented_arg->value); 1260 /* 1261 * So that the next arg with a payload can consume its augmented arg, i.e. for rename* syscalls 1262 * we would have two strings, each prefixed by its size. 1263 */ 1264 int consumed = sizeof(*augmented_arg) + augmented_arg->size; 1265 1266 arg->augmented.args = ((void *)arg->augmented.args) + consumed; 1267 arg->augmented.size -= consumed; 1268 1269 return printed; 1270 } 1271 1272 static size_t syscall_arg__scnprintf_filename(char *bf, size_t size, 1273 struct syscall_arg *arg) 1274 { 1275 unsigned long ptr = arg->val; 1276 1277 if (arg->augmented.args) 1278 return syscall_arg__scnprintf_augmented_string(arg, bf, size); 1279 1280 if (!arg->trace->vfs_getname) 1281 return scnprintf(bf, size, "%#x", ptr); 1282 1283 thread__set_filename_pos(arg->thread, bf, ptr); 1284 return 0; 1285 } 1286 1287 static bool trace__filter_duration(struct trace *trace, double t) 1288 { 1289 return t < (trace->duration_filter * NSEC_PER_MSEC); 1290 } 1291 1292 static size_t __trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp) 1293 { 1294 double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC; 1295 1296 return fprintf(fp, "%10.3f ", ts); 1297 } 1298 1299 /* 1300 * We're handling tstamp=0 as an undefined tstamp, i.e. like when we are 1301 * using ttrace->entry_time for a thread that receives a sys_exit without 1302 * first having received a sys_enter ("poll" issued before tracing session 1303 * starts, lost sys_enter exit due to ring buffer overflow). 1304 */ 1305 static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp) 1306 { 1307 if (tstamp > 0) 1308 return __trace__fprintf_tstamp(trace, tstamp, fp); 1309 1310 return fprintf(fp, " ? "); 1311 } 1312 1313 static bool done = false; 1314 static bool interrupted = false; 1315 1316 static void sig_handler(int sig) 1317 { 1318 done = true; 1319 interrupted = sig == SIGINT; 1320 } 1321 1322 static size_t trace__fprintf_comm_tid(struct trace *trace, struct thread *thread, FILE *fp) 1323 { 1324 size_t printed = 0; 1325 1326 if (trace->multiple_threads) { 1327 if (trace->show_comm) 1328 printed += fprintf(fp, "%.14s/", thread__comm_str(thread)); 1329 printed += fprintf(fp, "%d ", thread->tid); 1330 } 1331 1332 return printed; 1333 } 1334 1335 static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread, 1336 u64 duration, bool duration_calculated, u64 tstamp, FILE *fp) 1337 { 1338 size_t printed = 0; 1339 1340 if (trace->show_tstamp) 1341 printed = trace__fprintf_tstamp(trace, tstamp, fp); 1342 if (trace->show_duration) 1343 printed += fprintf_duration(duration, duration_calculated, fp); 1344 return printed + trace__fprintf_comm_tid(trace, thread, fp); 1345 } 1346 1347 static int trace__process_event(struct trace *trace, struct machine *machine, 1348 union perf_event *event, struct perf_sample *sample) 1349 { 1350 int ret = 0; 1351 1352 switch (event->header.type) { 1353 case PERF_RECORD_LOST: 1354 color_fprintf(trace->output, PERF_COLOR_RED, 1355 "LOST %" PRIu64 " events!\n", event->lost.lost); 1356 ret = machine__process_lost_event(machine, event, sample); 1357 break; 1358 default: 1359 ret = machine__process_event(machine, event, sample); 1360 break; 1361 } 1362 1363 return ret; 1364 } 1365 1366 static int trace__tool_process(struct perf_tool *tool, 1367 union perf_event *event, 1368 struct perf_sample *sample, 1369 struct machine *machine) 1370 { 1371 struct trace *trace = container_of(tool, struct trace, tool); 1372 return trace__process_event(trace, machine, event, sample); 1373 } 1374 1375 static char *trace__machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp) 1376 { 1377 struct machine *machine = vmachine; 1378 1379 if (machine->kptr_restrict_warned) 1380 return NULL; 1381 1382 if (symbol_conf.kptr_restrict) { 1383 pr_warning("Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n" 1384 "Check /proc/sys/kernel/kptr_restrict.\n\n" 1385 "Kernel samples will not be resolved.\n"); 1386 machine->kptr_restrict_warned = true; 1387 return NULL; 1388 } 1389 1390 return machine__resolve_kernel_addr(vmachine, addrp, modp); 1391 } 1392 1393 static int trace__symbols_init(struct trace *trace, struct evlist *evlist) 1394 { 1395 int err = symbol__init(NULL); 1396 1397 if (err) 1398 return err; 1399 1400 trace->host = machine__new_host(); 1401 if (trace->host == NULL) 1402 return -ENOMEM; 1403 1404 err = trace_event__register_resolver(trace->host, trace__machine__resolve_kernel_addr); 1405 if (err < 0) 1406 goto out; 1407 1408 err = __machine__synthesize_threads(trace->host, &trace->tool, &trace->opts.target, 1409 evlist->core.threads, trace__tool_process, false, 1410 1); 1411 out: 1412 if (err) 1413 symbol__exit(); 1414 1415 return err; 1416 } 1417 1418 static void trace__symbols__exit(struct trace *trace) 1419 { 1420 machine__exit(trace->host); 1421 trace->host = NULL; 1422 1423 symbol__exit(); 1424 } 1425 1426 static int syscall__alloc_arg_fmts(struct syscall *sc, int nr_args) 1427 { 1428 int idx; 1429 1430 if (nr_args == 6 && sc->fmt && sc->fmt->nr_args != 0) 1431 nr_args = sc->fmt->nr_args; 1432 1433 sc->arg_fmt = calloc(nr_args, sizeof(*sc->arg_fmt)); 1434 if (sc->arg_fmt == NULL) 1435 return -1; 1436 1437 for (idx = 0; idx < nr_args; ++idx) { 1438 if (sc->fmt) 1439 sc->arg_fmt[idx] = sc->fmt->arg[idx]; 1440 } 1441 1442 sc->nr_args = nr_args; 1443 return 0; 1444 } 1445 1446 static int syscall__set_arg_fmts(struct syscall *sc) 1447 { 1448 struct tep_format_field *field, *last_field = NULL; 1449 int idx = 0, len; 1450 1451 for (field = sc->args; field; field = field->next, ++idx) { 1452 last_field = field; 1453 1454 if (sc->fmt && sc->fmt->arg[idx].scnprintf) 1455 continue; 1456 1457 len = strlen(field->name); 1458 1459 if (strcmp(field->type, "const char *") == 0 && 1460 ((len >= 4 && strcmp(field->name + len - 4, "name") == 0) || 1461 strstr(field->name, "path") != NULL)) 1462 sc->arg_fmt[idx].scnprintf = SCA_FILENAME; 1463 else if ((field->flags & TEP_FIELD_IS_POINTER) || strstr(field->name, "addr")) 1464 sc->arg_fmt[idx].scnprintf = SCA_PTR; 1465 else if (strcmp(field->type, "pid_t") == 0) 1466 sc->arg_fmt[idx].scnprintf = SCA_PID; 1467 else if (strcmp(field->type, "umode_t") == 0) 1468 sc->arg_fmt[idx].scnprintf = SCA_MODE_T; 1469 else if ((strcmp(field->type, "int") == 0 || 1470 strcmp(field->type, "unsigned int") == 0 || 1471 strcmp(field->type, "long") == 0) && 1472 len >= 2 && strcmp(field->name + len - 2, "fd") == 0) { 1473 /* 1474 * /sys/kernel/tracing/events/syscalls/sys_enter* 1475 * egrep 'field:.*fd;' .../format|sed -r 's/.*field:([a-z ]+) [a-z_]*fd.+/\1/g'|sort|uniq -c 1476 * 65 int 1477 * 23 unsigned int 1478 * 7 unsigned long 1479 */ 1480 sc->arg_fmt[idx].scnprintf = SCA_FD; 1481 } 1482 } 1483 1484 if (last_field) 1485 sc->args_size = last_field->offset + last_field->size; 1486 1487 return 0; 1488 } 1489 1490 static int trace__read_syscall_info(struct trace *trace, int id) 1491 { 1492 char tp_name[128]; 1493 struct syscall *sc; 1494 const char *name = syscalltbl__name(trace->sctbl, id); 1495 1496 if (trace->syscalls.table == NULL) { 1497 trace->syscalls.table = calloc(trace->sctbl->syscalls.max_id + 1, sizeof(*sc)); 1498 if (trace->syscalls.table == NULL) 1499 return -ENOMEM; 1500 } 1501 1502 sc = trace->syscalls.table + id; 1503 if (sc->nonexistent) 1504 return 0; 1505 1506 if (name == NULL) { 1507 sc->nonexistent = true; 1508 return 0; 1509 } 1510 1511 sc->name = name; 1512 sc->fmt = syscall_fmt__find(sc->name); 1513 1514 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name); 1515 sc->tp_format = trace_event__tp_format("syscalls", tp_name); 1516 1517 if (IS_ERR(sc->tp_format) && sc->fmt && sc->fmt->alias) { 1518 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias); 1519 sc->tp_format = trace_event__tp_format("syscalls", tp_name); 1520 } 1521 1522 if (syscall__alloc_arg_fmts(sc, IS_ERR(sc->tp_format) ? 6 : sc->tp_format->format.nr_fields)) 1523 return -ENOMEM; 1524 1525 if (IS_ERR(sc->tp_format)) 1526 return PTR_ERR(sc->tp_format); 1527 1528 sc->args = sc->tp_format->format.fields; 1529 /* 1530 * We need to check and discard the first variable '__syscall_nr' 1531 * or 'nr' that mean the syscall number. It is needless here. 1532 * So drop '__syscall_nr' or 'nr' field but does not exist on older kernels. 1533 */ 1534 if (sc->args && (!strcmp(sc->args->name, "__syscall_nr") || !strcmp(sc->args->name, "nr"))) { 1535 sc->args = sc->args->next; 1536 --sc->nr_args; 1537 } 1538 1539 sc->is_exit = !strcmp(name, "exit_group") || !strcmp(name, "exit"); 1540 sc->is_open = !strcmp(name, "open") || !strcmp(name, "openat"); 1541 1542 return syscall__set_arg_fmts(sc); 1543 } 1544 1545 static int intcmp(const void *a, const void *b) 1546 { 1547 const int *one = a, *another = b; 1548 1549 return *one - *another; 1550 } 1551 1552 static int trace__validate_ev_qualifier(struct trace *trace) 1553 { 1554 int err = 0; 1555 bool printed_invalid_prefix = false; 1556 struct str_node *pos; 1557 size_t nr_used = 0, nr_allocated = strlist__nr_entries(trace->ev_qualifier); 1558 1559 trace->ev_qualifier_ids.entries = malloc(nr_allocated * 1560 sizeof(trace->ev_qualifier_ids.entries[0])); 1561 1562 if (trace->ev_qualifier_ids.entries == NULL) { 1563 fputs("Error:\tNot enough memory for allocating events qualifier ids\n", 1564 trace->output); 1565 err = -EINVAL; 1566 goto out; 1567 } 1568 1569 strlist__for_each_entry(pos, trace->ev_qualifier) { 1570 const char *sc = pos->s; 1571 int id = syscalltbl__id(trace->sctbl, sc), match_next = -1; 1572 1573 if (id < 0) { 1574 id = syscalltbl__strglobmatch_first(trace->sctbl, sc, &match_next); 1575 if (id >= 0) 1576 goto matches; 1577 1578 if (!printed_invalid_prefix) { 1579 pr_debug("Skipping unknown syscalls: "); 1580 printed_invalid_prefix = true; 1581 } else { 1582 pr_debug(", "); 1583 } 1584 1585 pr_debug("%s", sc); 1586 continue; 1587 } 1588 matches: 1589 trace->ev_qualifier_ids.entries[nr_used++] = id; 1590 if (match_next == -1) 1591 continue; 1592 1593 while (1) { 1594 id = syscalltbl__strglobmatch_next(trace->sctbl, sc, &match_next); 1595 if (id < 0) 1596 break; 1597 if (nr_allocated == nr_used) { 1598 void *entries; 1599 1600 nr_allocated += 8; 1601 entries = realloc(trace->ev_qualifier_ids.entries, 1602 nr_allocated * sizeof(trace->ev_qualifier_ids.entries[0])); 1603 if (entries == NULL) { 1604 err = -ENOMEM; 1605 fputs("\nError:\t Not enough memory for parsing\n", trace->output); 1606 goto out_free; 1607 } 1608 trace->ev_qualifier_ids.entries = entries; 1609 } 1610 trace->ev_qualifier_ids.entries[nr_used++] = id; 1611 } 1612 } 1613 1614 trace->ev_qualifier_ids.nr = nr_used; 1615 qsort(trace->ev_qualifier_ids.entries, nr_used, sizeof(int), intcmp); 1616 out: 1617 if (printed_invalid_prefix) 1618 pr_debug("\n"); 1619 return err; 1620 out_free: 1621 zfree(&trace->ev_qualifier_ids.entries); 1622 trace->ev_qualifier_ids.nr = 0; 1623 goto out; 1624 } 1625 1626 static __maybe_unused bool trace__syscall_enabled(struct trace *trace, int id) 1627 { 1628 bool in_ev_qualifier; 1629 1630 if (trace->ev_qualifier_ids.nr == 0) 1631 return true; 1632 1633 in_ev_qualifier = bsearch(&id, trace->ev_qualifier_ids.entries, 1634 trace->ev_qualifier_ids.nr, sizeof(int), intcmp) != NULL; 1635 1636 if (in_ev_qualifier) 1637 return !trace->not_ev_qualifier; 1638 1639 return trace->not_ev_qualifier; 1640 } 1641 1642 /* 1643 * args is to be interpreted as a series of longs but we need to handle 1644 * 8-byte unaligned accesses. args points to raw_data within the event 1645 * and raw_data is guaranteed to be 8-byte unaligned because it is 1646 * preceded by raw_size which is a u32. So we need to copy args to a temp 1647 * variable to read it. Most notably this avoids extended load instructions 1648 * on unaligned addresses 1649 */ 1650 unsigned long syscall_arg__val(struct syscall_arg *arg, u8 idx) 1651 { 1652 unsigned long val; 1653 unsigned char *p = arg->args + sizeof(unsigned long) * idx; 1654 1655 memcpy(&val, p, sizeof(val)); 1656 return val; 1657 } 1658 1659 static size_t syscall__scnprintf_name(struct syscall *sc, char *bf, size_t size, 1660 struct syscall_arg *arg) 1661 { 1662 if (sc->arg_fmt && sc->arg_fmt[arg->idx].name) 1663 return scnprintf(bf, size, "%s: ", sc->arg_fmt[arg->idx].name); 1664 1665 return scnprintf(bf, size, "arg%d: ", arg->idx); 1666 } 1667 1668 /* 1669 * Check if the value is in fact zero, i.e. mask whatever needs masking, such 1670 * as mount 'flags' argument that needs ignoring some magic flag, see comment 1671 * in tools/perf/trace/beauty/mount_flags.c 1672 */ 1673 static unsigned long syscall__mask_val(struct syscall *sc, struct syscall_arg *arg, unsigned long val) 1674 { 1675 if (sc->arg_fmt && sc->arg_fmt[arg->idx].mask_val) 1676 return sc->arg_fmt[arg->idx].mask_val(arg, val); 1677 1678 return val; 1679 } 1680 1681 static size_t syscall__scnprintf_val(struct syscall *sc, char *bf, size_t size, 1682 struct syscall_arg *arg, unsigned long val) 1683 { 1684 if (sc->arg_fmt && sc->arg_fmt[arg->idx].scnprintf) { 1685 arg->val = val; 1686 if (sc->arg_fmt[arg->idx].parm) 1687 arg->parm = sc->arg_fmt[arg->idx].parm; 1688 return sc->arg_fmt[arg->idx].scnprintf(bf, size, arg); 1689 } 1690 return scnprintf(bf, size, "%ld", val); 1691 } 1692 1693 static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size, 1694 unsigned char *args, void *augmented_args, int augmented_args_size, 1695 struct trace *trace, struct thread *thread) 1696 { 1697 size_t printed = 0; 1698 unsigned long val; 1699 u8 bit = 1; 1700 struct syscall_arg arg = { 1701 .args = args, 1702 .augmented = { 1703 .size = augmented_args_size, 1704 .args = augmented_args, 1705 }, 1706 .idx = 0, 1707 .mask = 0, 1708 .trace = trace, 1709 .thread = thread, 1710 .show_string_prefix = trace->show_string_prefix, 1711 }; 1712 struct thread_trace *ttrace = thread__priv(thread); 1713 1714 /* 1715 * Things like fcntl will set this in its 'cmd' formatter to pick the 1716 * right formatter for the return value (an fd? file flags?), which is 1717 * not needed for syscalls that always return a given type, say an fd. 1718 */ 1719 ttrace->ret_scnprintf = NULL; 1720 1721 if (sc->args != NULL) { 1722 struct tep_format_field *field; 1723 1724 for (field = sc->args; field; 1725 field = field->next, ++arg.idx, bit <<= 1) { 1726 if (arg.mask & bit) 1727 continue; 1728 1729 val = syscall_arg__val(&arg, arg.idx); 1730 /* 1731 * Some syscall args need some mask, most don't and 1732 * return val untouched. 1733 */ 1734 val = syscall__mask_val(sc, &arg, val); 1735 1736 /* 1737 * Suppress this argument if its value is zero and 1738 * and we don't have a string associated in an 1739 * strarray for it. 1740 */ 1741 if (val == 0 && 1742 !trace->show_zeros && 1743 !(sc->arg_fmt && 1744 (sc->arg_fmt[arg.idx].show_zero || 1745 sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAY || 1746 sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAYS) && 1747 sc->arg_fmt[arg.idx].parm)) 1748 continue; 1749 1750 printed += scnprintf(bf + printed, size - printed, "%s", printed ? ", " : ""); 1751 1752 if (trace->show_arg_names) 1753 printed += scnprintf(bf + printed, size - printed, "%s: ", field->name); 1754 1755 printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val); 1756 } 1757 } else if (IS_ERR(sc->tp_format)) { 1758 /* 1759 * If we managed to read the tracepoint /format file, then we 1760 * may end up not having any args, like with gettid(), so only 1761 * print the raw args when we didn't manage to read it. 1762 */ 1763 while (arg.idx < sc->nr_args) { 1764 if (arg.mask & bit) 1765 goto next_arg; 1766 val = syscall_arg__val(&arg, arg.idx); 1767 if (printed) 1768 printed += scnprintf(bf + printed, size - printed, ", "); 1769 printed += syscall__scnprintf_name(sc, bf + printed, size - printed, &arg); 1770 printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val); 1771 next_arg: 1772 ++arg.idx; 1773 bit <<= 1; 1774 } 1775 } 1776 1777 return printed; 1778 } 1779 1780 typedef int (*tracepoint_handler)(struct trace *trace, struct evsel *evsel, 1781 union perf_event *event, 1782 struct perf_sample *sample); 1783 1784 static struct syscall *trace__syscall_info(struct trace *trace, 1785 struct evsel *evsel, int id) 1786 { 1787 int err = 0; 1788 1789 if (id < 0) { 1790 1791 /* 1792 * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried 1793 * before that, leaving at a higher verbosity level till that is 1794 * explained. Reproduced with plain ftrace with: 1795 * 1796 * echo 1 > /t/events/raw_syscalls/sys_exit/enable 1797 * grep "NR -1 " /t/trace_pipe 1798 * 1799 * After generating some load on the machine. 1800 */ 1801 if (verbose > 1) { 1802 static u64 n; 1803 fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n", 1804 id, perf_evsel__name(evsel), ++n); 1805 } 1806 return NULL; 1807 } 1808 1809 err = -EINVAL; 1810 1811 if (id > trace->sctbl->syscalls.max_id) 1812 goto out_cant_read; 1813 1814 if ((trace->syscalls.table == NULL || trace->syscalls.table[id].name == NULL) && 1815 (err = trace__read_syscall_info(trace, id)) != 0) 1816 goto out_cant_read; 1817 1818 if (trace->syscalls.table[id].name == NULL) { 1819 if (trace->syscalls.table[id].nonexistent) 1820 return NULL; 1821 goto out_cant_read; 1822 } 1823 1824 return &trace->syscalls.table[id]; 1825 1826 out_cant_read: 1827 if (verbose > 0) { 1828 char sbuf[STRERR_BUFSIZE]; 1829 fprintf(trace->output, "Problems reading syscall %d: %d (%s)", id, -err, str_error_r(-err, sbuf, sizeof(sbuf))); 1830 if (id <= trace->sctbl->syscalls.max_id && trace->syscalls.table[id].name != NULL) 1831 fprintf(trace->output, "(%s)", trace->syscalls.table[id].name); 1832 fputs(" information\n", trace->output); 1833 } 1834 return NULL; 1835 } 1836 1837 static void thread__update_stats(struct thread_trace *ttrace, 1838 int id, struct perf_sample *sample) 1839 { 1840 struct int_node *inode; 1841 struct stats *stats; 1842 u64 duration = 0; 1843 1844 inode = intlist__findnew(ttrace->syscall_stats, id); 1845 if (inode == NULL) 1846 return; 1847 1848 stats = inode->priv; 1849 if (stats == NULL) { 1850 stats = malloc(sizeof(struct stats)); 1851 if (stats == NULL) 1852 return; 1853 init_stats(stats); 1854 inode->priv = stats; 1855 } 1856 1857 if (ttrace->entry_time && sample->time > ttrace->entry_time) 1858 duration = sample->time - ttrace->entry_time; 1859 1860 update_stats(stats, duration); 1861 } 1862 1863 static int trace__printf_interrupted_entry(struct trace *trace) 1864 { 1865 struct thread_trace *ttrace; 1866 size_t printed; 1867 int len; 1868 1869 if (trace->failure_only || trace->current == NULL) 1870 return 0; 1871 1872 ttrace = thread__priv(trace->current); 1873 1874 if (!ttrace->entry_pending) 1875 return 0; 1876 1877 printed = trace__fprintf_entry_head(trace, trace->current, 0, false, ttrace->entry_time, trace->output); 1878 printed += len = fprintf(trace->output, "%s)", ttrace->entry_str); 1879 1880 if (len < trace->args_alignment - 4) 1881 printed += fprintf(trace->output, "%-*s", trace->args_alignment - 4 - len, " "); 1882 1883 printed += fprintf(trace->output, " ...\n"); 1884 1885 ttrace->entry_pending = false; 1886 ++trace->nr_events_printed; 1887 1888 return printed; 1889 } 1890 1891 static int trace__fprintf_sample(struct trace *trace, struct evsel *evsel, 1892 struct perf_sample *sample, struct thread *thread) 1893 { 1894 int printed = 0; 1895 1896 if (trace->print_sample) { 1897 double ts = (double)sample->time / NSEC_PER_MSEC; 1898 1899 printed += fprintf(trace->output, "%22s %10.3f %s %d/%d [%d]\n", 1900 perf_evsel__name(evsel), ts, 1901 thread__comm_str(thread), 1902 sample->pid, sample->tid, sample->cpu); 1903 } 1904 1905 return printed; 1906 } 1907 1908 static void *syscall__augmented_args(struct syscall *sc, struct perf_sample *sample, int *augmented_args_size, int raw_augmented_args_size) 1909 { 1910 void *augmented_args = NULL; 1911 /* 1912 * For now with BPF raw_augmented we hook into raw_syscalls:sys_enter 1913 * and there we get all 6 syscall args plus the tracepoint common fields 1914 * that gets calculated at the start and the syscall_nr (another long). 1915 * So we check if that is the case and if so don't look after the 1916 * sc->args_size but always after the full raw_syscalls:sys_enter payload, 1917 * which is fixed. 1918 * 1919 * We'll revisit this later to pass s->args_size to the BPF augmenter 1920 * (now tools/perf/examples/bpf/augmented_raw_syscalls.c, so that it 1921 * copies only what we need for each syscall, like what happens when we 1922 * use syscalls:sys_enter_NAME, so that we reduce the kernel/userspace 1923 * traffic to just what is needed for each syscall. 1924 */ 1925 int args_size = raw_augmented_args_size ?: sc->args_size; 1926 1927 *augmented_args_size = sample->raw_size - args_size; 1928 if (*augmented_args_size > 0) 1929 augmented_args = sample->raw_data + args_size; 1930 1931 return augmented_args; 1932 } 1933 1934 static int trace__sys_enter(struct trace *trace, struct evsel *evsel, 1935 union perf_event *event __maybe_unused, 1936 struct perf_sample *sample) 1937 { 1938 char *msg; 1939 void *args; 1940 int printed = 0; 1941 struct thread *thread; 1942 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1; 1943 int augmented_args_size = 0; 1944 void *augmented_args = NULL; 1945 struct syscall *sc = trace__syscall_info(trace, evsel, id); 1946 struct thread_trace *ttrace; 1947 1948 if (sc == NULL) 1949 return -1; 1950 1951 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid); 1952 ttrace = thread__trace(thread, trace->output); 1953 if (ttrace == NULL) 1954 goto out_put; 1955 1956 trace__fprintf_sample(trace, evsel, sample, thread); 1957 1958 args = perf_evsel__sc_tp_ptr(evsel, args, sample); 1959 1960 if (ttrace->entry_str == NULL) { 1961 ttrace->entry_str = malloc(trace__entry_str_size); 1962 if (!ttrace->entry_str) 1963 goto out_put; 1964 } 1965 1966 if (!(trace->duration_filter || trace->summary_only || trace->min_stack)) 1967 trace__printf_interrupted_entry(trace); 1968 /* 1969 * If this is raw_syscalls.sys_enter, then it always comes with the 6 possible 1970 * arguments, even if the syscall being handled, say "openat", uses only 4 arguments 1971 * this breaks syscall__augmented_args() check for augmented args, as we calculate 1972 * syscall->args_size using each syscalls:sys_enter_NAME tracefs format file, 1973 * so when handling, say the openat syscall, we end up getting 6 args for the 1974 * raw_syscalls:sys_enter event, when we expected just 4, we end up mistakenly 1975 * thinking that the extra 2 u64 args are the augmented filename, so just check 1976 * here and avoid using augmented syscalls when the evsel is the raw_syscalls one. 1977 */ 1978 if (evsel != trace->syscalls.events.sys_enter) 1979 augmented_args = syscall__augmented_args(sc, sample, &augmented_args_size, trace->raw_augmented_syscalls_args_size); 1980 ttrace->entry_time = sample->time; 1981 msg = ttrace->entry_str; 1982 printed += scnprintf(msg + printed, trace__entry_str_size - printed, "%s(", sc->name); 1983 1984 printed += syscall__scnprintf_args(sc, msg + printed, trace__entry_str_size - printed, 1985 args, augmented_args, augmented_args_size, trace, thread); 1986 1987 if (sc->is_exit) { 1988 if (!(trace->duration_filter || trace->summary_only || trace->failure_only || trace->min_stack)) { 1989 int alignment = 0; 1990 1991 trace__fprintf_entry_head(trace, thread, 0, false, ttrace->entry_time, trace->output); 1992 printed = fprintf(trace->output, "%s)", ttrace->entry_str); 1993 if (trace->args_alignment > printed) 1994 alignment = trace->args_alignment - printed; 1995 fprintf(trace->output, "%*s= ?\n", alignment, " "); 1996 } 1997 } else { 1998 ttrace->entry_pending = true; 1999 /* See trace__vfs_getname & trace__sys_exit */ 2000 ttrace->filename.pending_open = false; 2001 } 2002 2003 if (trace->current != thread) { 2004 thread__put(trace->current); 2005 trace->current = thread__get(thread); 2006 } 2007 err = 0; 2008 out_put: 2009 thread__put(thread); 2010 return err; 2011 } 2012 2013 static int trace__fprintf_sys_enter(struct trace *trace, struct evsel *evsel, 2014 struct perf_sample *sample) 2015 { 2016 struct thread_trace *ttrace; 2017 struct thread *thread; 2018 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1; 2019 struct syscall *sc = trace__syscall_info(trace, evsel, id); 2020 char msg[1024]; 2021 void *args, *augmented_args = NULL; 2022 int augmented_args_size; 2023 2024 if (sc == NULL) 2025 return -1; 2026 2027 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid); 2028 ttrace = thread__trace(thread, trace->output); 2029 /* 2030 * We need to get ttrace just to make sure it is there when syscall__scnprintf_args() 2031 * and the rest of the beautifiers accessing it via struct syscall_arg touches it. 2032 */ 2033 if (ttrace == NULL) 2034 goto out_put; 2035 2036 args = perf_evsel__sc_tp_ptr(evsel, args, sample); 2037 augmented_args = syscall__augmented_args(sc, sample, &augmented_args_size, trace->raw_augmented_syscalls_args_size); 2038 syscall__scnprintf_args(sc, msg, sizeof(msg), args, augmented_args, augmented_args_size, trace, thread); 2039 fprintf(trace->output, "%s", msg); 2040 err = 0; 2041 out_put: 2042 thread__put(thread); 2043 return err; 2044 } 2045 2046 static int trace__resolve_callchain(struct trace *trace, struct evsel *evsel, 2047 struct perf_sample *sample, 2048 struct callchain_cursor *cursor) 2049 { 2050 struct addr_location al; 2051 int max_stack = evsel->core.attr.sample_max_stack ? 2052 evsel->core.attr.sample_max_stack : 2053 trace->max_stack; 2054 int err; 2055 2056 if (machine__resolve(trace->host, &al, sample) < 0) 2057 return -1; 2058 2059 err = thread__resolve_callchain(al.thread, cursor, evsel, sample, NULL, NULL, max_stack); 2060 addr_location__put(&al); 2061 return err; 2062 } 2063 2064 static int trace__fprintf_callchain(struct trace *trace, struct perf_sample *sample) 2065 { 2066 /* TODO: user-configurable print_opts */ 2067 const unsigned int print_opts = EVSEL__PRINT_SYM | 2068 EVSEL__PRINT_DSO | 2069 EVSEL__PRINT_UNKNOWN_AS_ADDR; 2070 2071 return sample__fprintf_callchain(sample, 38, print_opts, &callchain_cursor, trace->output); 2072 } 2073 2074 static const char *errno_to_name(struct evsel *evsel, int err) 2075 { 2076 struct perf_env *env = perf_evsel__env(evsel); 2077 const char *arch_name = perf_env__arch(env); 2078 2079 return arch_syscalls__strerrno(arch_name, err); 2080 } 2081 2082 static int trace__sys_exit(struct trace *trace, struct evsel *evsel, 2083 union perf_event *event __maybe_unused, 2084 struct perf_sample *sample) 2085 { 2086 long ret; 2087 u64 duration = 0; 2088 bool duration_calculated = false; 2089 struct thread *thread; 2090 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1, callchain_ret = 0, printed = 0; 2091 int alignment = trace->args_alignment; 2092 struct syscall *sc = trace__syscall_info(trace, evsel, id); 2093 struct thread_trace *ttrace; 2094 2095 if (sc == NULL) 2096 return -1; 2097 2098 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid); 2099 ttrace = thread__trace(thread, trace->output); 2100 if (ttrace == NULL) 2101 goto out_put; 2102 2103 trace__fprintf_sample(trace, evsel, sample, thread); 2104 2105 if (trace->summary) 2106 thread__update_stats(ttrace, id, sample); 2107 2108 ret = perf_evsel__sc_tp_uint(evsel, ret, sample); 2109 2110 if (!trace->fd_path_disabled && sc->is_open && ret >= 0 && ttrace->filename.pending_open) { 2111 trace__set_fd_pathname(thread, ret, ttrace->filename.name); 2112 ttrace->filename.pending_open = false; 2113 ++trace->stats.vfs_getname; 2114 } 2115 2116 if (ttrace->entry_time) { 2117 duration = sample->time - ttrace->entry_time; 2118 if (trace__filter_duration(trace, duration)) 2119 goto out; 2120 duration_calculated = true; 2121 } else if (trace->duration_filter) 2122 goto out; 2123 2124 if (sample->callchain) { 2125 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor); 2126 if (callchain_ret == 0) { 2127 if (callchain_cursor.nr < trace->min_stack) 2128 goto out; 2129 callchain_ret = 1; 2130 } 2131 } 2132 2133 if (trace->summary_only || (ret >= 0 && trace->failure_only)) 2134 goto out; 2135 2136 trace__fprintf_entry_head(trace, thread, duration, duration_calculated, ttrace->entry_time, trace->output); 2137 2138 if (ttrace->entry_pending) { 2139 printed = fprintf(trace->output, "%s", ttrace->entry_str); 2140 } else { 2141 printed += fprintf(trace->output, " ... ["); 2142 color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued"); 2143 printed += 9; 2144 printed += fprintf(trace->output, "]: %s()", sc->name); 2145 } 2146 2147 printed++; /* the closing ')' */ 2148 2149 if (alignment > printed) 2150 alignment -= printed; 2151 else 2152 alignment = 0; 2153 2154 fprintf(trace->output, ")%*s= ", alignment, " "); 2155 2156 if (sc->fmt == NULL) { 2157 if (ret < 0) 2158 goto errno_print; 2159 signed_print: 2160 fprintf(trace->output, "%ld", ret); 2161 } else if (ret < 0) { 2162 errno_print: { 2163 char bf[STRERR_BUFSIZE]; 2164 const char *emsg = str_error_r(-ret, bf, sizeof(bf)), 2165 *e = errno_to_name(evsel, -ret); 2166 2167 fprintf(trace->output, "-1 %s (%s)", e, emsg); 2168 } 2169 } else if (ret == 0 && sc->fmt->timeout) 2170 fprintf(trace->output, "0 (Timeout)"); 2171 else if (ttrace->ret_scnprintf) { 2172 char bf[1024]; 2173 struct syscall_arg arg = { 2174 .val = ret, 2175 .thread = thread, 2176 .trace = trace, 2177 }; 2178 ttrace->ret_scnprintf(bf, sizeof(bf), &arg); 2179 ttrace->ret_scnprintf = NULL; 2180 fprintf(trace->output, "%s", bf); 2181 } else if (sc->fmt->hexret) 2182 fprintf(trace->output, "%#lx", ret); 2183 else if (sc->fmt->errpid) { 2184 struct thread *child = machine__find_thread(trace->host, ret, ret); 2185 2186 if (child != NULL) { 2187 fprintf(trace->output, "%ld", ret); 2188 if (child->comm_set) 2189 fprintf(trace->output, " (%s)", thread__comm_str(child)); 2190 thread__put(child); 2191 } 2192 } else 2193 goto signed_print; 2194 2195 fputc('\n', trace->output); 2196 2197 /* 2198 * We only consider an 'event' for the sake of --max-events a non-filtered 2199 * sys_enter + sys_exit and other tracepoint events. 2200 */ 2201 if (++trace->nr_events_printed == trace->max_events && trace->max_events != ULONG_MAX) 2202 interrupted = true; 2203 2204 if (callchain_ret > 0) 2205 trace__fprintf_callchain(trace, sample); 2206 else if (callchain_ret < 0) 2207 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel)); 2208 out: 2209 ttrace->entry_pending = false; 2210 err = 0; 2211 out_put: 2212 thread__put(thread); 2213 return err; 2214 } 2215 2216 static int trace__vfs_getname(struct trace *trace, struct evsel *evsel, 2217 union perf_event *event __maybe_unused, 2218 struct perf_sample *sample) 2219 { 2220 struct thread *thread = machine__findnew_thread(trace->host, sample->pid, sample->tid); 2221 struct thread_trace *ttrace; 2222 size_t filename_len, entry_str_len, to_move; 2223 ssize_t remaining_space; 2224 char *pos; 2225 const char *filename = perf_evsel__rawptr(evsel, sample, "pathname"); 2226 2227 if (!thread) 2228 goto out; 2229 2230 ttrace = thread__priv(thread); 2231 if (!ttrace) 2232 goto out_put; 2233 2234 filename_len = strlen(filename); 2235 if (filename_len == 0) 2236 goto out_put; 2237 2238 if (ttrace->filename.namelen < filename_len) { 2239 char *f = realloc(ttrace->filename.name, filename_len + 1); 2240 2241 if (f == NULL) 2242 goto out_put; 2243 2244 ttrace->filename.namelen = filename_len; 2245 ttrace->filename.name = f; 2246 } 2247 2248 strcpy(ttrace->filename.name, filename); 2249 ttrace->filename.pending_open = true; 2250 2251 if (!ttrace->filename.ptr) 2252 goto out_put; 2253 2254 entry_str_len = strlen(ttrace->entry_str); 2255 remaining_space = trace__entry_str_size - entry_str_len - 1; /* \0 */ 2256 if (remaining_space <= 0) 2257 goto out_put; 2258 2259 if (filename_len > (size_t)remaining_space) { 2260 filename += filename_len - remaining_space; 2261 filename_len = remaining_space; 2262 } 2263 2264 to_move = entry_str_len - ttrace->filename.entry_str_pos + 1; /* \0 */ 2265 pos = ttrace->entry_str + ttrace->filename.entry_str_pos; 2266 memmove(pos + filename_len, pos, to_move); 2267 memcpy(pos, filename, filename_len); 2268 2269 ttrace->filename.ptr = 0; 2270 ttrace->filename.entry_str_pos = 0; 2271 out_put: 2272 thread__put(thread); 2273 out: 2274 return 0; 2275 } 2276 2277 static int trace__sched_stat_runtime(struct trace *trace, struct evsel *evsel, 2278 union perf_event *event __maybe_unused, 2279 struct perf_sample *sample) 2280 { 2281 u64 runtime = perf_evsel__intval(evsel, sample, "runtime"); 2282 double runtime_ms = (double)runtime / NSEC_PER_MSEC; 2283 struct thread *thread = machine__findnew_thread(trace->host, 2284 sample->pid, 2285 sample->tid); 2286 struct thread_trace *ttrace = thread__trace(thread, trace->output); 2287 2288 if (ttrace == NULL) 2289 goto out_dump; 2290 2291 ttrace->runtime_ms += runtime_ms; 2292 trace->runtime_ms += runtime_ms; 2293 out_put: 2294 thread__put(thread); 2295 return 0; 2296 2297 out_dump: 2298 fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n", 2299 evsel->name, 2300 perf_evsel__strval(evsel, sample, "comm"), 2301 (pid_t)perf_evsel__intval(evsel, sample, "pid"), 2302 runtime, 2303 perf_evsel__intval(evsel, sample, "vruntime")); 2304 goto out_put; 2305 } 2306 2307 static int bpf_output__printer(enum binary_printer_ops op, 2308 unsigned int val, void *extra __maybe_unused, FILE *fp) 2309 { 2310 unsigned char ch = (unsigned char)val; 2311 2312 switch (op) { 2313 case BINARY_PRINT_CHAR_DATA: 2314 return fprintf(fp, "%c", isprint(ch) ? ch : '.'); 2315 case BINARY_PRINT_DATA_BEGIN: 2316 case BINARY_PRINT_LINE_BEGIN: 2317 case BINARY_PRINT_ADDR: 2318 case BINARY_PRINT_NUM_DATA: 2319 case BINARY_PRINT_NUM_PAD: 2320 case BINARY_PRINT_SEP: 2321 case BINARY_PRINT_CHAR_PAD: 2322 case BINARY_PRINT_LINE_END: 2323 case BINARY_PRINT_DATA_END: 2324 default: 2325 break; 2326 } 2327 2328 return 0; 2329 } 2330 2331 static void bpf_output__fprintf(struct trace *trace, 2332 struct perf_sample *sample) 2333 { 2334 binary__fprintf(sample->raw_data, sample->raw_size, 8, 2335 bpf_output__printer, NULL, trace->output); 2336 ++trace->nr_events_printed; 2337 } 2338 2339 static int trace__event_handler(struct trace *trace, struct evsel *evsel, 2340 union perf_event *event __maybe_unused, 2341 struct perf_sample *sample) 2342 { 2343 struct thread *thread; 2344 int callchain_ret = 0; 2345 /* 2346 * Check if we called perf_evsel__disable(evsel) due to, for instance, 2347 * this event's max_events having been hit and this is an entry coming 2348 * from the ring buffer that we should discard, since the max events 2349 * have already been considered/printed. 2350 */ 2351 if (evsel->disabled) 2352 return 0; 2353 2354 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid); 2355 2356 if (sample->callchain) { 2357 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor); 2358 if (callchain_ret == 0) { 2359 if (callchain_cursor.nr < trace->min_stack) 2360 goto out; 2361 callchain_ret = 1; 2362 } 2363 } 2364 2365 trace__printf_interrupted_entry(trace); 2366 trace__fprintf_tstamp(trace, sample->time, trace->output); 2367 2368 if (trace->trace_syscalls && trace->show_duration) 2369 fprintf(trace->output, "( ): "); 2370 2371 if (thread) 2372 trace__fprintf_comm_tid(trace, thread, trace->output); 2373 2374 if (evsel == trace->syscalls.events.augmented) { 2375 int id = perf_evsel__sc_tp_uint(evsel, id, sample); 2376 struct syscall *sc = trace__syscall_info(trace, evsel, id); 2377 2378 if (sc) { 2379 fprintf(trace->output, "%s(", sc->name); 2380 trace__fprintf_sys_enter(trace, evsel, sample); 2381 fputc(')', trace->output); 2382 goto newline; 2383 } 2384 2385 /* 2386 * XXX: Not having the associated syscall info or not finding/adding 2387 * the thread should never happen, but if it does... 2388 * fall thru and print it as a bpf_output event. 2389 */ 2390 } 2391 2392 fprintf(trace->output, "%s:", evsel->name); 2393 2394 if (perf_evsel__is_bpf_output(evsel)) { 2395 bpf_output__fprintf(trace, sample); 2396 } else if (evsel->tp_format) { 2397 if (strncmp(evsel->tp_format->name, "sys_enter_", 10) || 2398 trace__fprintf_sys_enter(trace, evsel, sample)) { 2399 event_format__fprintf(evsel->tp_format, sample->cpu, 2400 sample->raw_data, sample->raw_size, 2401 trace->output); 2402 ++trace->nr_events_printed; 2403 2404 if (evsel->max_events != ULONG_MAX && ++evsel->nr_events_printed == evsel->max_events) { 2405 evsel__disable(evsel); 2406 evsel__close(evsel); 2407 } 2408 } 2409 } 2410 2411 newline: 2412 fprintf(trace->output, "\n"); 2413 2414 if (callchain_ret > 0) 2415 trace__fprintf_callchain(trace, sample); 2416 else if (callchain_ret < 0) 2417 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel)); 2418 out: 2419 thread__put(thread); 2420 return 0; 2421 } 2422 2423 static void print_location(FILE *f, struct perf_sample *sample, 2424 struct addr_location *al, 2425 bool print_dso, bool print_sym) 2426 { 2427 2428 if ((verbose > 0 || print_dso) && al->map) 2429 fprintf(f, "%s@", al->map->dso->long_name); 2430 2431 if ((verbose > 0 || print_sym) && al->sym) 2432 fprintf(f, "%s+0x%" PRIx64, al->sym->name, 2433 al->addr - al->sym->start); 2434 else if (al->map) 2435 fprintf(f, "0x%" PRIx64, al->addr); 2436 else 2437 fprintf(f, "0x%" PRIx64, sample->addr); 2438 } 2439 2440 static int trace__pgfault(struct trace *trace, 2441 struct evsel *evsel, 2442 union perf_event *event __maybe_unused, 2443 struct perf_sample *sample) 2444 { 2445 struct thread *thread; 2446 struct addr_location al; 2447 char map_type = 'd'; 2448 struct thread_trace *ttrace; 2449 int err = -1; 2450 int callchain_ret = 0; 2451 2452 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid); 2453 2454 if (sample->callchain) { 2455 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor); 2456 if (callchain_ret == 0) { 2457 if (callchain_cursor.nr < trace->min_stack) 2458 goto out_put; 2459 callchain_ret = 1; 2460 } 2461 } 2462 2463 ttrace = thread__trace(thread, trace->output); 2464 if (ttrace == NULL) 2465 goto out_put; 2466 2467 if (evsel->core.attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ) 2468 ttrace->pfmaj++; 2469 else 2470 ttrace->pfmin++; 2471 2472 if (trace->summary_only) 2473 goto out; 2474 2475 thread__find_symbol(thread, sample->cpumode, sample->ip, &al); 2476 2477 trace__fprintf_entry_head(trace, thread, 0, true, sample->time, trace->output); 2478 2479 fprintf(trace->output, "%sfault [", 2480 evsel->core.attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ? 2481 "maj" : "min"); 2482 2483 print_location(trace->output, sample, &al, false, true); 2484 2485 fprintf(trace->output, "] => "); 2486 2487 thread__find_symbol(thread, sample->cpumode, sample->addr, &al); 2488 2489 if (!al.map) { 2490 thread__find_symbol(thread, sample->cpumode, sample->addr, &al); 2491 2492 if (al.map) 2493 map_type = 'x'; 2494 else 2495 map_type = '?'; 2496 } 2497 2498 print_location(trace->output, sample, &al, true, false); 2499 2500 fprintf(trace->output, " (%c%c)\n", map_type, al.level); 2501 2502 if (callchain_ret > 0) 2503 trace__fprintf_callchain(trace, sample); 2504 else if (callchain_ret < 0) 2505 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel)); 2506 2507 ++trace->nr_events_printed; 2508 out: 2509 err = 0; 2510 out_put: 2511 thread__put(thread); 2512 return err; 2513 } 2514 2515 static void trace__set_base_time(struct trace *trace, 2516 struct evsel *evsel, 2517 struct perf_sample *sample) 2518 { 2519 /* 2520 * BPF events were not setting PERF_SAMPLE_TIME, so be more robust 2521 * and don't use sample->time unconditionally, we may end up having 2522 * some other event in the future without PERF_SAMPLE_TIME for good 2523 * reason, i.e. we may not be interested in its timestamps, just in 2524 * it taking place, picking some piece of information when it 2525 * appears in our event stream (vfs_getname comes to mind). 2526 */ 2527 if (trace->base_time == 0 && !trace->full_time && 2528 (evsel->core.attr.sample_type & PERF_SAMPLE_TIME)) 2529 trace->base_time = sample->time; 2530 } 2531 2532 static int trace__process_sample(struct perf_tool *tool, 2533 union perf_event *event, 2534 struct perf_sample *sample, 2535 struct evsel *evsel, 2536 struct machine *machine __maybe_unused) 2537 { 2538 struct trace *trace = container_of(tool, struct trace, tool); 2539 struct thread *thread; 2540 int err = 0; 2541 2542 tracepoint_handler handler = evsel->handler; 2543 2544 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid); 2545 if (thread && thread__is_filtered(thread)) 2546 goto out; 2547 2548 trace__set_base_time(trace, evsel, sample); 2549 2550 if (handler) { 2551 ++trace->nr_events; 2552 handler(trace, evsel, event, sample); 2553 } 2554 out: 2555 thread__put(thread); 2556 return err; 2557 } 2558 2559 static int trace__record(struct trace *trace, int argc, const char **argv) 2560 { 2561 unsigned int rec_argc, i, j; 2562 const char **rec_argv; 2563 const char * const record_args[] = { 2564 "record", 2565 "-R", 2566 "-m", "1024", 2567 "-c", "1", 2568 }; 2569 2570 const char * const sc_args[] = { "-e", }; 2571 unsigned int sc_args_nr = ARRAY_SIZE(sc_args); 2572 const char * const majpf_args[] = { "-e", "major-faults" }; 2573 unsigned int majpf_args_nr = ARRAY_SIZE(majpf_args); 2574 const char * const minpf_args[] = { "-e", "minor-faults" }; 2575 unsigned int minpf_args_nr = ARRAY_SIZE(minpf_args); 2576 2577 /* +1 is for the event string below */ 2578 rec_argc = ARRAY_SIZE(record_args) + sc_args_nr + 1 + 2579 majpf_args_nr + minpf_args_nr + argc; 2580 rec_argv = calloc(rec_argc + 1, sizeof(char *)); 2581 2582 if (rec_argv == NULL) 2583 return -ENOMEM; 2584 2585 j = 0; 2586 for (i = 0; i < ARRAY_SIZE(record_args); i++) 2587 rec_argv[j++] = record_args[i]; 2588 2589 if (trace->trace_syscalls) { 2590 for (i = 0; i < sc_args_nr; i++) 2591 rec_argv[j++] = sc_args[i]; 2592 2593 /* event string may be different for older kernels - e.g., RHEL6 */ 2594 if (is_valid_tracepoint("raw_syscalls:sys_enter")) 2595 rec_argv[j++] = "raw_syscalls:sys_enter,raw_syscalls:sys_exit"; 2596 else if (is_valid_tracepoint("syscalls:sys_enter")) 2597 rec_argv[j++] = "syscalls:sys_enter,syscalls:sys_exit"; 2598 else { 2599 pr_err("Neither raw_syscalls nor syscalls events exist.\n"); 2600 free(rec_argv); 2601 return -1; 2602 } 2603 } 2604 2605 if (trace->trace_pgfaults & TRACE_PFMAJ) 2606 for (i = 0; i < majpf_args_nr; i++) 2607 rec_argv[j++] = majpf_args[i]; 2608 2609 if (trace->trace_pgfaults & TRACE_PFMIN) 2610 for (i = 0; i < minpf_args_nr; i++) 2611 rec_argv[j++] = minpf_args[i]; 2612 2613 for (i = 0; i < (unsigned int)argc; i++) 2614 rec_argv[j++] = argv[i]; 2615 2616 return cmd_record(j, rec_argv); 2617 } 2618 2619 static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp); 2620 2621 static bool evlist__add_vfs_getname(struct evlist *evlist) 2622 { 2623 bool found = false; 2624 struct evsel *evsel, *tmp; 2625 struct parse_events_error err = { .idx = 0, }; 2626 int ret = parse_events(evlist, "probe:vfs_getname*", &err); 2627 2628 if (ret) 2629 return false; 2630 2631 evlist__for_each_entry_safe(evlist, evsel, tmp) { 2632 if (!strstarts(perf_evsel__name(evsel), "probe:vfs_getname")) 2633 continue; 2634 2635 if (perf_evsel__field(evsel, "pathname")) { 2636 evsel->handler = trace__vfs_getname; 2637 found = true; 2638 continue; 2639 } 2640 2641 list_del_init(&evsel->core.node); 2642 evsel->evlist = NULL; 2643 evsel__delete(evsel); 2644 } 2645 2646 return found; 2647 } 2648 2649 static struct evsel *perf_evsel__new_pgfault(u64 config) 2650 { 2651 struct evsel *evsel; 2652 struct perf_event_attr attr = { 2653 .type = PERF_TYPE_SOFTWARE, 2654 .mmap_data = 1, 2655 }; 2656 2657 attr.config = config; 2658 attr.sample_period = 1; 2659 2660 event_attr_init(&attr); 2661 2662 evsel = evsel__new(&attr); 2663 if (evsel) 2664 evsel->handler = trace__pgfault; 2665 2666 return evsel; 2667 } 2668 2669 static void trace__handle_event(struct trace *trace, union perf_event *event, struct perf_sample *sample) 2670 { 2671 const u32 type = event->header.type; 2672 struct evsel *evsel; 2673 2674 if (type != PERF_RECORD_SAMPLE) { 2675 trace__process_event(trace, trace->host, event, sample); 2676 return; 2677 } 2678 2679 evsel = perf_evlist__id2evsel(trace->evlist, sample->id); 2680 if (evsel == NULL) { 2681 fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample->id); 2682 return; 2683 } 2684 2685 if (evswitch__discard(&trace->evswitch, evsel)) 2686 return; 2687 2688 trace__set_base_time(trace, evsel, sample); 2689 2690 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT && 2691 sample->raw_data == NULL) { 2692 fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n", 2693 perf_evsel__name(evsel), sample->tid, 2694 sample->cpu, sample->raw_size); 2695 } else { 2696 tracepoint_handler handler = evsel->handler; 2697 handler(trace, evsel, event, sample); 2698 } 2699 2700 if (trace->nr_events_printed >= trace->max_events && trace->max_events != ULONG_MAX) 2701 interrupted = true; 2702 } 2703 2704 static int trace__add_syscall_newtp(struct trace *trace) 2705 { 2706 int ret = -1; 2707 struct evlist *evlist = trace->evlist; 2708 struct evsel *sys_enter, *sys_exit; 2709 2710 sys_enter = perf_evsel__raw_syscall_newtp("sys_enter", trace__sys_enter); 2711 if (sys_enter == NULL) 2712 goto out; 2713 2714 if (perf_evsel__init_sc_tp_ptr_field(sys_enter, args)) 2715 goto out_delete_sys_enter; 2716 2717 sys_exit = perf_evsel__raw_syscall_newtp("sys_exit", trace__sys_exit); 2718 if (sys_exit == NULL) 2719 goto out_delete_sys_enter; 2720 2721 if (perf_evsel__init_sc_tp_uint_field(sys_exit, ret)) 2722 goto out_delete_sys_exit; 2723 2724 perf_evsel__config_callchain(sys_enter, &trace->opts, &callchain_param); 2725 perf_evsel__config_callchain(sys_exit, &trace->opts, &callchain_param); 2726 2727 evlist__add(evlist, sys_enter); 2728 evlist__add(evlist, sys_exit); 2729 2730 if (callchain_param.enabled && !trace->kernel_syscallchains) { 2731 /* 2732 * We're interested only in the user space callchain 2733 * leading to the syscall, allow overriding that for 2734 * debugging reasons using --kernel_syscall_callchains 2735 */ 2736 sys_exit->core.attr.exclude_callchain_kernel = 1; 2737 } 2738 2739 trace->syscalls.events.sys_enter = sys_enter; 2740 trace->syscalls.events.sys_exit = sys_exit; 2741 2742 ret = 0; 2743 out: 2744 return ret; 2745 2746 out_delete_sys_exit: 2747 evsel__delete_priv(sys_exit); 2748 out_delete_sys_enter: 2749 evsel__delete_priv(sys_enter); 2750 goto out; 2751 } 2752 2753 static int trace__set_ev_qualifier_tp_filter(struct trace *trace) 2754 { 2755 int err = -1; 2756 struct evsel *sys_exit; 2757 char *filter = asprintf_expr_inout_ints("id", !trace->not_ev_qualifier, 2758 trace->ev_qualifier_ids.nr, 2759 trace->ev_qualifier_ids.entries); 2760 2761 if (filter == NULL) 2762 goto out_enomem; 2763 2764 if (!perf_evsel__append_tp_filter(trace->syscalls.events.sys_enter, 2765 filter)) { 2766 sys_exit = trace->syscalls.events.sys_exit; 2767 err = perf_evsel__append_tp_filter(sys_exit, filter); 2768 } 2769 2770 free(filter); 2771 out: 2772 return err; 2773 out_enomem: 2774 errno = ENOMEM; 2775 goto out; 2776 } 2777 2778 #ifdef HAVE_LIBBPF_SUPPORT 2779 static struct bpf_program *trace__find_bpf_program_by_title(struct trace *trace, const char *name) 2780 { 2781 if (trace->bpf_obj == NULL) 2782 return NULL; 2783 2784 return bpf_object__find_program_by_title(trace->bpf_obj, name); 2785 } 2786 2787 static struct bpf_program *trace__find_syscall_bpf_prog(struct trace *trace, struct syscall *sc, 2788 const char *prog_name, const char *type) 2789 { 2790 struct bpf_program *prog; 2791 2792 if (prog_name == NULL) { 2793 char default_prog_name[256]; 2794 scnprintf(default_prog_name, sizeof(default_prog_name), "!syscalls:sys_%s_%s", type, sc->name); 2795 prog = trace__find_bpf_program_by_title(trace, default_prog_name); 2796 if (prog != NULL) 2797 goto out_found; 2798 if (sc->fmt && sc->fmt->alias) { 2799 scnprintf(default_prog_name, sizeof(default_prog_name), "!syscalls:sys_%s_%s", type, sc->fmt->alias); 2800 prog = trace__find_bpf_program_by_title(trace, default_prog_name); 2801 if (prog != NULL) 2802 goto out_found; 2803 } 2804 goto out_unaugmented; 2805 } 2806 2807 prog = trace__find_bpf_program_by_title(trace, prog_name); 2808 2809 if (prog != NULL) { 2810 out_found: 2811 return prog; 2812 } 2813 2814 pr_debug("Couldn't find BPF prog \"%s\" to associate with syscalls:sys_%s_%s, not augmenting it\n", 2815 prog_name, type, sc->name); 2816 out_unaugmented: 2817 return trace->syscalls.unaugmented_prog; 2818 } 2819 2820 static void trace__init_syscall_bpf_progs(struct trace *trace, int id) 2821 { 2822 struct syscall *sc = trace__syscall_info(trace, NULL, id); 2823 2824 if (sc == NULL) 2825 return; 2826 2827 sc->bpf_prog.sys_enter = trace__find_syscall_bpf_prog(trace, sc, sc->fmt ? sc->fmt->bpf_prog_name.sys_enter : NULL, "enter"); 2828 sc->bpf_prog.sys_exit = trace__find_syscall_bpf_prog(trace, sc, sc->fmt ? sc->fmt->bpf_prog_name.sys_exit : NULL, "exit"); 2829 } 2830 2831 static int trace__bpf_prog_sys_enter_fd(struct trace *trace, int id) 2832 { 2833 struct syscall *sc = trace__syscall_info(trace, NULL, id); 2834 return sc ? bpf_program__fd(sc->bpf_prog.sys_enter) : bpf_program__fd(trace->syscalls.unaugmented_prog); 2835 } 2836 2837 static int trace__bpf_prog_sys_exit_fd(struct trace *trace, int id) 2838 { 2839 struct syscall *sc = trace__syscall_info(trace, NULL, id); 2840 return sc ? bpf_program__fd(sc->bpf_prog.sys_exit) : bpf_program__fd(trace->syscalls.unaugmented_prog); 2841 } 2842 2843 static void trace__init_bpf_map_syscall_args(struct trace *trace, int id, struct bpf_map_syscall_entry *entry) 2844 { 2845 struct syscall *sc = trace__syscall_info(trace, NULL, id); 2846 int arg = 0; 2847 2848 if (sc == NULL) 2849 goto out; 2850 2851 for (; arg < sc->nr_args; ++arg) { 2852 entry->string_args_len[arg] = 0; 2853 if (sc->arg_fmt[arg].scnprintf == SCA_FILENAME) { 2854 /* Should be set like strace -s strsize */ 2855 entry->string_args_len[arg] = PATH_MAX; 2856 } 2857 } 2858 out: 2859 for (; arg < 6; ++arg) 2860 entry->string_args_len[arg] = 0; 2861 } 2862 static int trace__set_ev_qualifier_bpf_filter(struct trace *trace) 2863 { 2864 int fd = bpf_map__fd(trace->syscalls.map); 2865 struct bpf_map_syscall_entry value = { 2866 .enabled = !trace->not_ev_qualifier, 2867 }; 2868 int err = 0; 2869 size_t i; 2870 2871 for (i = 0; i < trace->ev_qualifier_ids.nr; ++i) { 2872 int key = trace->ev_qualifier_ids.entries[i]; 2873 2874 if (value.enabled) { 2875 trace__init_bpf_map_syscall_args(trace, key, &value); 2876 trace__init_syscall_bpf_progs(trace, key); 2877 } 2878 2879 err = bpf_map_update_elem(fd, &key, &value, BPF_EXIST); 2880 if (err) 2881 break; 2882 } 2883 2884 return err; 2885 } 2886 2887 static int __trace__init_syscalls_bpf_map(struct trace *trace, bool enabled) 2888 { 2889 int fd = bpf_map__fd(trace->syscalls.map); 2890 struct bpf_map_syscall_entry value = { 2891 .enabled = enabled, 2892 }; 2893 int err = 0, key; 2894 2895 for (key = 0; key < trace->sctbl->syscalls.nr_entries; ++key) { 2896 if (enabled) 2897 trace__init_bpf_map_syscall_args(trace, key, &value); 2898 2899 err = bpf_map_update_elem(fd, &key, &value, BPF_ANY); 2900 if (err) 2901 break; 2902 } 2903 2904 return err; 2905 } 2906 2907 static int trace__init_syscalls_bpf_map(struct trace *trace) 2908 { 2909 bool enabled = true; 2910 2911 if (trace->ev_qualifier_ids.nr) 2912 enabled = trace->not_ev_qualifier; 2913 2914 return __trace__init_syscalls_bpf_map(trace, enabled); 2915 } 2916 2917 static struct bpf_program *trace__find_usable_bpf_prog_entry(struct trace *trace, struct syscall *sc) 2918 { 2919 struct tep_format_field *field, *candidate_field; 2920 int id; 2921 2922 /* 2923 * We're only interested in syscalls that have a pointer: 2924 */ 2925 for (field = sc->args; field; field = field->next) { 2926 if (field->flags & TEP_FIELD_IS_POINTER) 2927 goto try_to_find_pair; 2928 } 2929 2930 return NULL; 2931 2932 try_to_find_pair: 2933 for (id = 0; id < trace->sctbl->syscalls.nr_entries; ++id) { 2934 struct syscall *pair = trace__syscall_info(trace, NULL, id); 2935 struct bpf_program *pair_prog; 2936 bool is_candidate = false; 2937 2938 if (pair == NULL || pair == sc || 2939 pair->bpf_prog.sys_enter == trace->syscalls.unaugmented_prog) 2940 continue; 2941 2942 for (field = sc->args, candidate_field = pair->args; 2943 field && candidate_field; field = field->next, candidate_field = candidate_field->next) { 2944 bool is_pointer = field->flags & TEP_FIELD_IS_POINTER, 2945 candidate_is_pointer = candidate_field->flags & TEP_FIELD_IS_POINTER; 2946 2947 if (is_pointer) { 2948 if (!candidate_is_pointer) { 2949 // The candidate just doesn't copies our pointer arg, might copy other pointers we want. 2950 continue; 2951 } 2952 } else { 2953 if (candidate_is_pointer) { 2954 // The candidate might copy a pointer we don't have, skip it. 2955 goto next_candidate; 2956 } 2957 continue; 2958 } 2959 2960 if (strcmp(field->type, candidate_field->type)) 2961 goto next_candidate; 2962 2963 is_candidate = true; 2964 } 2965 2966 if (!is_candidate) 2967 goto next_candidate; 2968 2969 /* 2970 * Check if the tentative pair syscall augmenter has more pointers, if it has, 2971 * then it may be collecting that and we then can't use it, as it would collect 2972 * more than what is common to the two syscalls. 2973 */ 2974 if (candidate_field) { 2975 for (candidate_field = candidate_field->next; candidate_field; candidate_field = candidate_field->next) 2976 if (candidate_field->flags & TEP_FIELD_IS_POINTER) 2977 goto next_candidate; 2978 } 2979 2980 pair_prog = pair->bpf_prog.sys_enter; 2981 /* 2982 * If the pair isn't enabled, then its bpf_prog.sys_enter will not 2983 * have been searched for, so search it here and if it returns the 2984 * unaugmented one, then ignore it, otherwise we'll reuse that BPF 2985 * program for a filtered syscall on a non-filtered one. 2986 * 2987 * For instance, we have "!syscalls:sys_enter_renameat" and that is 2988 * useful for "renameat2". 2989 */ 2990 if (pair_prog == NULL) { 2991 pair_prog = trace__find_syscall_bpf_prog(trace, pair, pair->fmt ? pair->fmt->bpf_prog_name.sys_enter : NULL, "enter"); 2992 if (pair_prog == trace->syscalls.unaugmented_prog) 2993 goto next_candidate; 2994 } 2995 2996 pr_debug("Reusing \"%s\" BPF sys_enter augmenter for \"%s\"\n", pair->name, sc->name); 2997 return pair_prog; 2998 next_candidate: 2999 continue; 3000 } 3001 3002 return NULL; 3003 } 3004 3005 static int trace__init_syscalls_bpf_prog_array_maps(struct trace *trace) 3006 { 3007 int map_enter_fd = bpf_map__fd(trace->syscalls.prog_array.sys_enter), 3008 map_exit_fd = bpf_map__fd(trace->syscalls.prog_array.sys_exit); 3009 int err = 0, key; 3010 3011 for (key = 0; key < trace->sctbl->syscalls.nr_entries; ++key) { 3012 int prog_fd; 3013 3014 if (!trace__syscall_enabled(trace, key)) 3015 continue; 3016 3017 trace__init_syscall_bpf_progs(trace, key); 3018 3019 // It'll get at least the "!raw_syscalls:unaugmented" 3020 prog_fd = trace__bpf_prog_sys_enter_fd(trace, key); 3021 err = bpf_map_update_elem(map_enter_fd, &key, &prog_fd, BPF_ANY); 3022 if (err) 3023 break; 3024 prog_fd = trace__bpf_prog_sys_exit_fd(trace, key); 3025 err = bpf_map_update_elem(map_exit_fd, &key, &prog_fd, BPF_ANY); 3026 if (err) 3027 break; 3028 } 3029 3030 /* 3031 * Now lets do a second pass looking for enabled syscalls without 3032 * an augmenter that have a signature that is a superset of another 3033 * syscall with an augmenter so that we can auto-reuse it. 3034 * 3035 * I.e. if we have an augmenter for the "open" syscall that has 3036 * this signature: 3037 * 3038 * int open(const char *pathname, int flags, mode_t mode); 3039 * 3040 * I.e. that will collect just the first string argument, then we 3041 * can reuse it for the 'creat' syscall, that has this signature: 3042 * 3043 * int creat(const char *pathname, mode_t mode); 3044 * 3045 * and for: 3046 * 3047 * int stat(const char *pathname, struct stat *statbuf); 3048 * int lstat(const char *pathname, struct stat *statbuf); 3049 * 3050 * Because the 'open' augmenter will collect the first arg as a string, 3051 * and leave alone all the other args, which already helps with 3052 * beautifying 'stat' and 'lstat''s pathname arg. 3053 * 3054 * Then, in time, when 'stat' gets an augmenter that collects both 3055 * first and second arg (this one on the raw_syscalls:sys_exit prog 3056 * array tail call, then that one will be used. 3057 */ 3058 for (key = 0; key < trace->sctbl->syscalls.nr_entries; ++key) { 3059 struct syscall *sc = trace__syscall_info(trace, NULL, key); 3060 struct bpf_program *pair_prog; 3061 int prog_fd; 3062 3063 if (sc == NULL || sc->bpf_prog.sys_enter == NULL) 3064 continue; 3065 3066 /* 3067 * For now we're just reusing the sys_enter prog, and if it 3068 * already has an augmenter, we don't need to find one. 3069 */ 3070 if (sc->bpf_prog.sys_enter != trace->syscalls.unaugmented_prog) 3071 continue; 3072 3073 /* 3074 * Look at all the other syscalls for one that has a signature 3075 * that is close enough that we can share: 3076 */ 3077 pair_prog = trace__find_usable_bpf_prog_entry(trace, sc); 3078 if (pair_prog == NULL) 3079 continue; 3080 3081 sc->bpf_prog.sys_enter = pair_prog; 3082 3083 /* 3084 * Update the BPF_MAP_TYPE_PROG_SHARED for raw_syscalls:sys_enter 3085 * with the fd for the program we're reusing: 3086 */ 3087 prog_fd = bpf_program__fd(sc->bpf_prog.sys_enter); 3088 err = bpf_map_update_elem(map_enter_fd, &key, &prog_fd, BPF_ANY); 3089 if (err) 3090 break; 3091 } 3092 3093 3094 return err; 3095 } 3096 #else 3097 static int trace__set_ev_qualifier_bpf_filter(struct trace *trace __maybe_unused) 3098 { 3099 return 0; 3100 } 3101 3102 static int trace__init_syscalls_bpf_map(struct trace *trace __maybe_unused) 3103 { 3104 return 0; 3105 } 3106 3107 static struct bpf_program *trace__find_bpf_program_by_title(struct trace *trace __maybe_unused, 3108 const char *name __maybe_unused) 3109 { 3110 return NULL; 3111 } 3112 3113 static int trace__init_syscalls_bpf_prog_array_maps(struct trace *trace __maybe_unused) 3114 { 3115 return 0; 3116 } 3117 #endif // HAVE_LIBBPF_SUPPORT 3118 3119 static int trace__set_ev_qualifier_filter(struct trace *trace) 3120 { 3121 if (trace->syscalls.map) 3122 return trace__set_ev_qualifier_bpf_filter(trace); 3123 if (trace->syscalls.events.sys_enter) 3124 return trace__set_ev_qualifier_tp_filter(trace); 3125 return 0; 3126 } 3127 3128 static int bpf_map__set_filter_pids(struct bpf_map *map __maybe_unused, 3129 size_t npids __maybe_unused, pid_t *pids __maybe_unused) 3130 { 3131 int err = 0; 3132 #ifdef HAVE_LIBBPF_SUPPORT 3133 bool value = true; 3134 int map_fd = bpf_map__fd(map); 3135 size_t i; 3136 3137 for (i = 0; i < npids; ++i) { 3138 err = bpf_map_update_elem(map_fd, &pids[i], &value, BPF_ANY); 3139 if (err) 3140 break; 3141 } 3142 #endif 3143 return err; 3144 } 3145 3146 static int trace__set_filter_loop_pids(struct trace *trace) 3147 { 3148 unsigned int nr = 1, err; 3149 pid_t pids[32] = { 3150 getpid(), 3151 }; 3152 struct thread *thread = machine__find_thread(trace->host, pids[0], pids[0]); 3153 3154 while (thread && nr < ARRAY_SIZE(pids)) { 3155 struct thread *parent = machine__find_thread(trace->host, thread->ppid, thread->ppid); 3156 3157 if (parent == NULL) 3158 break; 3159 3160 if (!strcmp(thread__comm_str(parent), "sshd") || 3161 strstarts(thread__comm_str(parent), "gnome-terminal")) { 3162 pids[nr++] = parent->tid; 3163 break; 3164 } 3165 thread = parent; 3166 } 3167 3168 err = perf_evlist__set_tp_filter_pids(trace->evlist, nr, pids); 3169 if (!err && trace->filter_pids.map) 3170 err = bpf_map__set_filter_pids(trace->filter_pids.map, nr, pids); 3171 3172 return err; 3173 } 3174 3175 static int trace__set_filter_pids(struct trace *trace) 3176 { 3177 int err = 0; 3178 /* 3179 * Better not use !target__has_task() here because we need to cover the 3180 * case where no threads were specified in the command line, but a 3181 * workload was, and in that case we will fill in the thread_map when 3182 * we fork the workload in perf_evlist__prepare_workload. 3183 */ 3184 if (trace->filter_pids.nr > 0) { 3185 err = perf_evlist__set_tp_filter_pids(trace->evlist, trace->filter_pids.nr, 3186 trace->filter_pids.entries); 3187 if (!err && trace->filter_pids.map) { 3188 err = bpf_map__set_filter_pids(trace->filter_pids.map, trace->filter_pids.nr, 3189 trace->filter_pids.entries); 3190 } 3191 } else if (thread_map__pid(trace->evlist->core.threads, 0) == -1) { 3192 err = trace__set_filter_loop_pids(trace); 3193 } 3194 3195 return err; 3196 } 3197 3198 static int __trace__deliver_event(struct trace *trace, union perf_event *event) 3199 { 3200 struct evlist *evlist = trace->evlist; 3201 struct perf_sample sample; 3202 int err; 3203 3204 err = perf_evlist__parse_sample(evlist, event, &sample); 3205 if (err) 3206 fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err); 3207 else 3208 trace__handle_event(trace, event, &sample); 3209 3210 return 0; 3211 } 3212 3213 static int __trace__flush_events(struct trace *trace) 3214 { 3215 u64 first = ordered_events__first_time(&trace->oe.data); 3216 u64 flush = trace->oe.last - NSEC_PER_SEC; 3217 3218 /* Is there some thing to flush.. */ 3219 if (first && first < flush) 3220 return ordered_events__flush_time(&trace->oe.data, flush); 3221 3222 return 0; 3223 } 3224 3225 static int trace__flush_events(struct trace *trace) 3226 { 3227 return !trace->sort_events ? 0 : __trace__flush_events(trace); 3228 } 3229 3230 static int trace__deliver_event(struct trace *trace, union perf_event *event) 3231 { 3232 int err; 3233 3234 if (!trace->sort_events) 3235 return __trace__deliver_event(trace, event); 3236 3237 err = perf_evlist__parse_sample_timestamp(trace->evlist, event, &trace->oe.last); 3238 if (err && err != -1) 3239 return err; 3240 3241 err = ordered_events__queue(&trace->oe.data, event, trace->oe.last, 0); 3242 if (err) 3243 return err; 3244 3245 return trace__flush_events(trace); 3246 } 3247 3248 static int ordered_events__deliver_event(struct ordered_events *oe, 3249 struct ordered_event *event) 3250 { 3251 struct trace *trace = container_of(oe, struct trace, oe.data); 3252 3253 return __trace__deliver_event(trace, event->event); 3254 } 3255 3256 static int trace__run(struct trace *trace, int argc, const char **argv) 3257 { 3258 struct evlist *evlist = trace->evlist; 3259 struct evsel *evsel, *pgfault_maj = NULL, *pgfault_min = NULL; 3260 int err = -1, i; 3261 unsigned long before; 3262 const bool forks = argc > 0; 3263 bool draining = false; 3264 3265 trace->live = true; 3266 3267 if (!trace->raw_augmented_syscalls) { 3268 if (trace->trace_syscalls && trace__add_syscall_newtp(trace)) 3269 goto out_error_raw_syscalls; 3270 3271 if (trace->trace_syscalls) 3272 trace->vfs_getname = evlist__add_vfs_getname(evlist); 3273 } 3274 3275 if ((trace->trace_pgfaults & TRACE_PFMAJ)) { 3276 pgfault_maj = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MAJ); 3277 if (pgfault_maj == NULL) 3278 goto out_error_mem; 3279 perf_evsel__config_callchain(pgfault_maj, &trace->opts, &callchain_param); 3280 evlist__add(evlist, pgfault_maj); 3281 } 3282 3283 if ((trace->trace_pgfaults & TRACE_PFMIN)) { 3284 pgfault_min = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MIN); 3285 if (pgfault_min == NULL) 3286 goto out_error_mem; 3287 perf_evsel__config_callchain(pgfault_min, &trace->opts, &callchain_param); 3288 evlist__add(evlist, pgfault_min); 3289 } 3290 3291 if (trace->sched && 3292 perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime", 3293 trace__sched_stat_runtime)) 3294 goto out_error_sched_stat_runtime; 3295 3296 /* 3297 * If a global cgroup was set, apply it to all the events without an 3298 * explicit cgroup. I.e.: 3299 * 3300 * trace -G A -e sched:*switch 3301 * 3302 * Will set all raw_syscalls:sys_{enter,exit}, pgfault, vfs_getname, etc 3303 * _and_ sched:sched_switch to the 'A' cgroup, while: 3304 * 3305 * trace -e sched:*switch -G A 3306 * 3307 * will only set the sched:sched_switch event to the 'A' cgroup, all the 3308 * other events (raw_syscalls:sys_{enter,exit}, etc are left "without" 3309 * a cgroup (on the root cgroup, sys wide, etc). 3310 * 3311 * Multiple cgroups: 3312 * 3313 * trace -G A -e sched:*switch -G B 3314 * 3315 * the syscall ones go to the 'A' cgroup, the sched:sched_switch goes 3316 * to the 'B' cgroup. 3317 * 3318 * evlist__set_default_cgroup() grabs a reference of the passed cgroup 3319 * only for the evsels still without a cgroup, i.e. evsel->cgroup == NULL. 3320 */ 3321 if (trace->cgroup) 3322 evlist__set_default_cgroup(trace->evlist, trace->cgroup); 3323 3324 err = perf_evlist__create_maps(evlist, &trace->opts.target); 3325 if (err < 0) { 3326 fprintf(trace->output, "Problems parsing the target to trace, check your options!\n"); 3327 goto out_delete_evlist; 3328 } 3329 3330 err = trace__symbols_init(trace, evlist); 3331 if (err < 0) { 3332 fprintf(trace->output, "Problems initializing symbol libraries!\n"); 3333 goto out_delete_evlist; 3334 } 3335 3336 perf_evlist__config(evlist, &trace->opts, &callchain_param); 3337 3338 signal(SIGCHLD, sig_handler); 3339 signal(SIGINT, sig_handler); 3340 3341 if (forks) { 3342 err = perf_evlist__prepare_workload(evlist, &trace->opts.target, 3343 argv, false, NULL); 3344 if (err < 0) { 3345 fprintf(trace->output, "Couldn't run the workload!\n"); 3346 goto out_delete_evlist; 3347 } 3348 } 3349 3350 err = evlist__open(evlist); 3351 if (err < 0) 3352 goto out_error_open; 3353 3354 err = bpf__apply_obj_config(); 3355 if (err) { 3356 char errbuf[BUFSIZ]; 3357 3358 bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf)); 3359 pr_err("ERROR: Apply config to BPF failed: %s\n", 3360 errbuf); 3361 goto out_error_open; 3362 } 3363 3364 err = trace__set_filter_pids(trace); 3365 if (err < 0) 3366 goto out_error_mem; 3367 3368 if (trace->syscalls.map) 3369 trace__init_syscalls_bpf_map(trace); 3370 3371 if (trace->syscalls.prog_array.sys_enter) 3372 trace__init_syscalls_bpf_prog_array_maps(trace); 3373 3374 if (trace->ev_qualifier_ids.nr > 0) { 3375 err = trace__set_ev_qualifier_filter(trace); 3376 if (err < 0) 3377 goto out_errno; 3378 3379 if (trace->syscalls.events.sys_exit) { 3380 pr_debug("event qualifier tracepoint filter: %s\n", 3381 trace->syscalls.events.sys_exit->filter); 3382 } 3383 } 3384 3385 /* 3386 * If the "close" syscall is not traced, then we will not have the 3387 * opportunity to, in syscall_arg__scnprintf_close_fd() invalidate the 3388 * fd->pathname table and were ending up showing the last value set by 3389 * syscalls opening a pathname and associating it with a descriptor or 3390 * reading it from /proc/pid/fd/ in cases where that doesn't make 3391 * sense. 3392 * 3393 * So just disable this beautifier (SCA_FD, SCA_FDAT) when 'close' is 3394 * not in use. 3395 */ 3396 trace->fd_path_disabled = !trace__syscall_enabled(trace, syscalltbl__id(trace->sctbl, "close")); 3397 3398 err = perf_evlist__apply_filters(evlist, &evsel); 3399 if (err < 0) 3400 goto out_error_apply_filters; 3401 3402 if (trace->dump.map) 3403 bpf_map__fprintf(trace->dump.map, trace->output); 3404 3405 err = perf_evlist__mmap(evlist, trace->opts.mmap_pages); 3406 if (err < 0) 3407 goto out_error_mmap; 3408 3409 if (!target__none(&trace->opts.target) && !trace->opts.initial_delay) 3410 evlist__enable(evlist); 3411 3412 if (forks) 3413 perf_evlist__start_workload(evlist); 3414 3415 if (trace->opts.initial_delay) { 3416 usleep(trace->opts.initial_delay * 1000); 3417 evlist__enable(evlist); 3418 } 3419 3420 trace->multiple_threads = thread_map__pid(evlist->core.threads, 0) == -1 || 3421 evlist->core.threads->nr > 1 || 3422 perf_evlist__first(evlist)->core.attr.inherit; 3423 3424 /* 3425 * Now that we already used evsel->core.attr to ask the kernel to setup the 3426 * events, lets reuse evsel->core.attr.sample_max_stack as the limit in 3427 * trace__resolve_callchain(), allowing per-event max-stack settings 3428 * to override an explicitly set --max-stack global setting. 3429 */ 3430 evlist__for_each_entry(evlist, evsel) { 3431 if (evsel__has_callchain(evsel) && 3432 evsel->core.attr.sample_max_stack == 0) 3433 evsel->core.attr.sample_max_stack = trace->max_stack; 3434 } 3435 again: 3436 before = trace->nr_events; 3437 3438 for (i = 0; i < evlist->nr_mmaps; i++) { 3439 union perf_event *event; 3440 struct perf_mmap *md; 3441 3442 md = &evlist->mmap[i]; 3443 if (perf_mmap__read_init(md) < 0) 3444 continue; 3445 3446 while ((event = perf_mmap__read_event(md)) != NULL) { 3447 ++trace->nr_events; 3448 3449 err = trace__deliver_event(trace, event); 3450 if (err) 3451 goto out_disable; 3452 3453 perf_mmap__consume(md); 3454 3455 if (interrupted) 3456 goto out_disable; 3457 3458 if (done && !draining) { 3459 evlist__disable(evlist); 3460 draining = true; 3461 } 3462 } 3463 perf_mmap__read_done(md); 3464 } 3465 3466 if (trace->nr_events == before) { 3467 int timeout = done ? 100 : -1; 3468 3469 if (!draining && perf_evlist__poll(evlist, timeout) > 0) { 3470 if (perf_evlist__filter_pollfd(evlist, POLLERR | POLLHUP | POLLNVAL) == 0) 3471 draining = true; 3472 3473 goto again; 3474 } else { 3475 if (trace__flush_events(trace)) 3476 goto out_disable; 3477 } 3478 } else { 3479 goto again; 3480 } 3481 3482 out_disable: 3483 thread__zput(trace->current); 3484 3485 evlist__disable(evlist); 3486 3487 if (trace->sort_events) 3488 ordered_events__flush(&trace->oe.data, OE_FLUSH__FINAL); 3489 3490 if (!err) { 3491 if (trace->summary) 3492 trace__fprintf_thread_summary(trace, trace->output); 3493 3494 if (trace->show_tool_stats) { 3495 fprintf(trace->output, "Stats:\n " 3496 " vfs_getname : %" PRIu64 "\n" 3497 " proc_getname: %" PRIu64 "\n", 3498 trace->stats.vfs_getname, 3499 trace->stats.proc_getname); 3500 } 3501 } 3502 3503 out_delete_evlist: 3504 trace__symbols__exit(trace); 3505 3506 evlist__delete(evlist); 3507 cgroup__put(trace->cgroup); 3508 trace->evlist = NULL; 3509 trace->live = false; 3510 return err; 3511 { 3512 char errbuf[BUFSIZ]; 3513 3514 out_error_sched_stat_runtime: 3515 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "sched", "sched_stat_runtime"); 3516 goto out_error; 3517 3518 out_error_raw_syscalls: 3519 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "raw_syscalls", "sys_(enter|exit)"); 3520 goto out_error; 3521 3522 out_error_mmap: 3523 perf_evlist__strerror_mmap(evlist, errno, errbuf, sizeof(errbuf)); 3524 goto out_error; 3525 3526 out_error_open: 3527 perf_evlist__strerror_open(evlist, errno, errbuf, sizeof(errbuf)); 3528 3529 out_error: 3530 fprintf(trace->output, "%s\n", errbuf); 3531 goto out_delete_evlist; 3532 3533 out_error_apply_filters: 3534 fprintf(trace->output, 3535 "Failed to set filter \"%s\" on event %s with %d (%s)\n", 3536 evsel->filter, perf_evsel__name(evsel), errno, 3537 str_error_r(errno, errbuf, sizeof(errbuf))); 3538 goto out_delete_evlist; 3539 } 3540 out_error_mem: 3541 fprintf(trace->output, "Not enough memory to run!\n"); 3542 goto out_delete_evlist; 3543 3544 out_errno: 3545 fprintf(trace->output, "errno=%d,%s\n", errno, strerror(errno)); 3546 goto out_delete_evlist; 3547 } 3548 3549 static int trace__replay(struct trace *trace) 3550 { 3551 const struct evsel_str_handler handlers[] = { 3552 { "probe:vfs_getname", trace__vfs_getname, }, 3553 }; 3554 struct perf_data data = { 3555 .path = input_name, 3556 .mode = PERF_DATA_MODE_READ, 3557 .force = trace->force, 3558 }; 3559 struct perf_session *session; 3560 struct evsel *evsel; 3561 int err = -1; 3562 3563 trace->tool.sample = trace__process_sample; 3564 trace->tool.mmap = perf_event__process_mmap; 3565 trace->tool.mmap2 = perf_event__process_mmap2; 3566 trace->tool.comm = perf_event__process_comm; 3567 trace->tool.exit = perf_event__process_exit; 3568 trace->tool.fork = perf_event__process_fork; 3569 trace->tool.attr = perf_event__process_attr; 3570 trace->tool.tracing_data = perf_event__process_tracing_data; 3571 trace->tool.build_id = perf_event__process_build_id; 3572 trace->tool.namespaces = perf_event__process_namespaces; 3573 3574 trace->tool.ordered_events = true; 3575 trace->tool.ordering_requires_timestamps = true; 3576 3577 /* add tid to output */ 3578 trace->multiple_threads = true; 3579 3580 session = perf_session__new(&data, false, &trace->tool); 3581 if (session == NULL) 3582 return -1; 3583 3584 if (trace->opts.target.pid) 3585 symbol_conf.pid_list_str = strdup(trace->opts.target.pid); 3586 3587 if (trace->opts.target.tid) 3588 symbol_conf.tid_list_str = strdup(trace->opts.target.tid); 3589 3590 if (symbol__init(&session->header.env) < 0) 3591 goto out; 3592 3593 trace->host = &session->machines.host; 3594 3595 err = perf_session__set_tracepoints_handlers(session, handlers); 3596 if (err) 3597 goto out; 3598 3599 evsel = perf_evlist__find_tracepoint_by_name(session->evlist, 3600 "raw_syscalls:sys_enter"); 3601 /* older kernels have syscalls tp versus raw_syscalls */ 3602 if (evsel == NULL) 3603 evsel = perf_evlist__find_tracepoint_by_name(session->evlist, 3604 "syscalls:sys_enter"); 3605 3606 if (evsel && 3607 (perf_evsel__init_raw_syscall_tp(evsel, trace__sys_enter) < 0 || 3608 perf_evsel__init_sc_tp_ptr_field(evsel, args))) { 3609 pr_err("Error during initialize raw_syscalls:sys_enter event\n"); 3610 goto out; 3611 } 3612 3613 evsel = perf_evlist__find_tracepoint_by_name(session->evlist, 3614 "raw_syscalls:sys_exit"); 3615 if (evsel == NULL) 3616 evsel = perf_evlist__find_tracepoint_by_name(session->evlist, 3617 "syscalls:sys_exit"); 3618 if (evsel && 3619 (perf_evsel__init_raw_syscall_tp(evsel, trace__sys_exit) < 0 || 3620 perf_evsel__init_sc_tp_uint_field(evsel, ret))) { 3621 pr_err("Error during initialize raw_syscalls:sys_exit event\n"); 3622 goto out; 3623 } 3624 3625 evlist__for_each_entry(session->evlist, evsel) { 3626 if (evsel->core.attr.type == PERF_TYPE_SOFTWARE && 3627 (evsel->core.attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ || 3628 evsel->core.attr.config == PERF_COUNT_SW_PAGE_FAULTS_MIN || 3629 evsel->core.attr.config == PERF_COUNT_SW_PAGE_FAULTS)) 3630 evsel->handler = trace__pgfault; 3631 } 3632 3633 setup_pager(); 3634 3635 err = perf_session__process_events(session); 3636 if (err) 3637 pr_err("Failed to process events, error %d", err); 3638 3639 else if (trace->summary) 3640 trace__fprintf_thread_summary(trace, trace->output); 3641 3642 out: 3643 perf_session__delete(session); 3644 3645 return err; 3646 } 3647 3648 static size_t trace__fprintf_threads_header(FILE *fp) 3649 { 3650 size_t printed; 3651 3652 printed = fprintf(fp, "\n Summary of events:\n\n"); 3653 3654 return printed; 3655 } 3656 3657 DEFINE_RESORT_RB(syscall_stats, a->msecs > b->msecs, 3658 struct stats *stats; 3659 double msecs; 3660 int syscall; 3661 ) 3662 { 3663 struct int_node *source = rb_entry(nd, struct int_node, rb_node); 3664 struct stats *stats = source->priv; 3665 3666 entry->syscall = source->i; 3667 entry->stats = stats; 3668 entry->msecs = stats ? (u64)stats->n * (avg_stats(stats) / NSEC_PER_MSEC) : 0; 3669 } 3670 3671 static size_t thread__dump_stats(struct thread_trace *ttrace, 3672 struct trace *trace, FILE *fp) 3673 { 3674 size_t printed = 0; 3675 struct syscall *sc; 3676 struct rb_node *nd; 3677 DECLARE_RESORT_RB_INTLIST(syscall_stats, ttrace->syscall_stats); 3678 3679 if (syscall_stats == NULL) 3680 return 0; 3681 3682 printed += fprintf(fp, "\n"); 3683 3684 printed += fprintf(fp, " syscall calls total min avg max stddev\n"); 3685 printed += fprintf(fp, " (msec) (msec) (msec) (msec) (%%)\n"); 3686 printed += fprintf(fp, " --------------- -------- --------- --------- --------- --------- ------\n"); 3687 3688 resort_rb__for_each_entry(nd, syscall_stats) { 3689 struct stats *stats = syscall_stats_entry->stats; 3690 if (stats) { 3691 double min = (double)(stats->min) / NSEC_PER_MSEC; 3692 double max = (double)(stats->max) / NSEC_PER_MSEC; 3693 double avg = avg_stats(stats); 3694 double pct; 3695 u64 n = (u64) stats->n; 3696 3697 pct = avg ? 100.0 * stddev_stats(stats)/avg : 0.0; 3698 avg /= NSEC_PER_MSEC; 3699 3700 sc = &trace->syscalls.table[syscall_stats_entry->syscall]; 3701 printed += fprintf(fp, " %-15s", sc->name); 3702 printed += fprintf(fp, " %8" PRIu64 " %9.3f %9.3f %9.3f", 3703 n, syscall_stats_entry->msecs, min, avg); 3704 printed += fprintf(fp, " %9.3f %9.2f%%\n", max, pct); 3705 } 3706 } 3707 3708 resort_rb__delete(syscall_stats); 3709 printed += fprintf(fp, "\n\n"); 3710 3711 return printed; 3712 } 3713 3714 static size_t trace__fprintf_thread(FILE *fp, struct thread *thread, struct trace *trace) 3715 { 3716 size_t printed = 0; 3717 struct thread_trace *ttrace = thread__priv(thread); 3718 double ratio; 3719 3720 if (ttrace == NULL) 3721 return 0; 3722 3723 ratio = (double)ttrace->nr_events / trace->nr_events * 100.0; 3724 3725 printed += fprintf(fp, " %s (%d), ", thread__comm_str(thread), thread->tid); 3726 printed += fprintf(fp, "%lu events, ", ttrace->nr_events); 3727 printed += fprintf(fp, "%.1f%%", ratio); 3728 if (ttrace->pfmaj) 3729 printed += fprintf(fp, ", %lu majfaults", ttrace->pfmaj); 3730 if (ttrace->pfmin) 3731 printed += fprintf(fp, ", %lu minfaults", ttrace->pfmin); 3732 if (trace->sched) 3733 printed += fprintf(fp, ", %.3f msec\n", ttrace->runtime_ms); 3734 else if (fputc('\n', fp) != EOF) 3735 ++printed; 3736 3737 printed += thread__dump_stats(ttrace, trace, fp); 3738 3739 return printed; 3740 } 3741 3742 static unsigned long thread__nr_events(struct thread_trace *ttrace) 3743 { 3744 return ttrace ? ttrace->nr_events : 0; 3745 } 3746 3747 DEFINE_RESORT_RB(threads, (thread__nr_events(a->thread->priv) < thread__nr_events(b->thread->priv)), 3748 struct thread *thread; 3749 ) 3750 { 3751 entry->thread = rb_entry(nd, struct thread, rb_node); 3752 } 3753 3754 static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp) 3755 { 3756 size_t printed = trace__fprintf_threads_header(fp); 3757 struct rb_node *nd; 3758 int i; 3759 3760 for (i = 0; i < THREADS__TABLE_SIZE; i++) { 3761 DECLARE_RESORT_RB_MACHINE_THREADS(threads, trace->host, i); 3762 3763 if (threads == NULL) { 3764 fprintf(fp, "%s", "Error sorting output by nr_events!\n"); 3765 return 0; 3766 } 3767 3768 resort_rb__for_each_entry(nd, threads) 3769 printed += trace__fprintf_thread(fp, threads_entry->thread, trace); 3770 3771 resort_rb__delete(threads); 3772 } 3773 return printed; 3774 } 3775 3776 static int trace__set_duration(const struct option *opt, const char *str, 3777 int unset __maybe_unused) 3778 { 3779 struct trace *trace = opt->value; 3780 3781 trace->duration_filter = atof(str); 3782 return 0; 3783 } 3784 3785 static int trace__set_filter_pids_from_option(const struct option *opt, const char *str, 3786 int unset __maybe_unused) 3787 { 3788 int ret = -1; 3789 size_t i; 3790 struct trace *trace = opt->value; 3791 /* 3792 * FIXME: introduce a intarray class, plain parse csv and create a 3793 * { int nr, int entries[] } struct... 3794 */ 3795 struct intlist *list = intlist__new(str); 3796 3797 if (list == NULL) 3798 return -1; 3799 3800 i = trace->filter_pids.nr = intlist__nr_entries(list) + 1; 3801 trace->filter_pids.entries = calloc(i, sizeof(pid_t)); 3802 3803 if (trace->filter_pids.entries == NULL) 3804 goto out; 3805 3806 trace->filter_pids.entries[0] = getpid(); 3807 3808 for (i = 1; i < trace->filter_pids.nr; ++i) 3809 trace->filter_pids.entries[i] = intlist__entry(list, i - 1)->i; 3810 3811 intlist__delete(list); 3812 ret = 0; 3813 out: 3814 return ret; 3815 } 3816 3817 static int trace__open_output(struct trace *trace, const char *filename) 3818 { 3819 struct stat st; 3820 3821 if (!stat(filename, &st) && st.st_size) { 3822 char oldname[PATH_MAX]; 3823 3824 scnprintf(oldname, sizeof(oldname), "%s.old", filename); 3825 unlink(oldname); 3826 rename(filename, oldname); 3827 } 3828 3829 trace->output = fopen(filename, "w"); 3830 3831 return trace->output == NULL ? -errno : 0; 3832 } 3833 3834 static int parse_pagefaults(const struct option *opt, const char *str, 3835 int unset __maybe_unused) 3836 { 3837 int *trace_pgfaults = opt->value; 3838 3839 if (strcmp(str, "all") == 0) 3840 *trace_pgfaults |= TRACE_PFMAJ | TRACE_PFMIN; 3841 else if (strcmp(str, "maj") == 0) 3842 *trace_pgfaults |= TRACE_PFMAJ; 3843 else if (strcmp(str, "min") == 0) 3844 *trace_pgfaults |= TRACE_PFMIN; 3845 else 3846 return -1; 3847 3848 return 0; 3849 } 3850 3851 static void evlist__set_evsel_handler(struct evlist *evlist, void *handler) 3852 { 3853 struct evsel *evsel; 3854 3855 evlist__for_each_entry(evlist, evsel) 3856 evsel->handler = handler; 3857 } 3858 3859 static int evlist__set_syscall_tp_fields(struct evlist *evlist) 3860 { 3861 struct evsel *evsel; 3862 3863 evlist__for_each_entry(evlist, evsel) { 3864 if (evsel->priv || !evsel->tp_format) 3865 continue; 3866 3867 if (strcmp(evsel->tp_format->system, "syscalls")) 3868 continue; 3869 3870 if (perf_evsel__init_syscall_tp(evsel)) 3871 return -1; 3872 3873 if (!strncmp(evsel->tp_format->name, "sys_enter_", 10)) { 3874 struct syscall_tp *sc = evsel->priv; 3875 3876 if (__tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64))) 3877 return -1; 3878 } else if (!strncmp(evsel->tp_format->name, "sys_exit_", 9)) { 3879 struct syscall_tp *sc = evsel->priv; 3880 3881 if (__tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap)) 3882 return -1; 3883 } 3884 } 3885 3886 return 0; 3887 } 3888 3889 /* 3890 * XXX: Hackish, just splitting the combined -e+--event (syscalls 3891 * (raw_syscalls:{sys_{enter,exit}} + events (tracepoints, HW, SW, etc) to use 3892 * existing facilities unchanged (trace->ev_qualifier + parse_options()). 3893 * 3894 * It'd be better to introduce a parse_options() variant that would return a 3895 * list with the terms it didn't match to an event... 3896 */ 3897 static int trace__parse_events_option(const struct option *opt, const char *str, 3898 int unset __maybe_unused) 3899 { 3900 struct trace *trace = (struct trace *)opt->value; 3901 const char *s = str; 3902 char *sep = NULL, *lists[2] = { NULL, NULL, }; 3903 int len = strlen(str) + 1, err = -1, list, idx; 3904 char *strace_groups_dir = system_path(STRACE_GROUPS_DIR); 3905 char group_name[PATH_MAX]; 3906 struct syscall_fmt *fmt; 3907 3908 if (strace_groups_dir == NULL) 3909 return -1; 3910 3911 if (*s == '!') { 3912 ++s; 3913 trace->not_ev_qualifier = true; 3914 } 3915 3916 while (1) { 3917 if ((sep = strchr(s, ',')) != NULL) 3918 *sep = '\0'; 3919 3920 list = 0; 3921 if (syscalltbl__id(trace->sctbl, s) >= 0 || 3922 syscalltbl__strglobmatch_first(trace->sctbl, s, &idx) >= 0) { 3923 list = 1; 3924 goto do_concat; 3925 } 3926 3927 fmt = syscall_fmt__find_by_alias(s); 3928 if (fmt != NULL) { 3929 list = 1; 3930 s = fmt->name; 3931 } else { 3932 path__join(group_name, sizeof(group_name), strace_groups_dir, s); 3933 if (access(group_name, R_OK) == 0) 3934 list = 1; 3935 } 3936 do_concat: 3937 if (lists[list]) { 3938 sprintf(lists[list] + strlen(lists[list]), ",%s", s); 3939 } else { 3940 lists[list] = malloc(len); 3941 if (lists[list] == NULL) 3942 goto out; 3943 strcpy(lists[list], s); 3944 } 3945 3946 if (!sep) 3947 break; 3948 3949 *sep = ','; 3950 s = sep + 1; 3951 } 3952 3953 if (lists[1] != NULL) { 3954 struct strlist_config slist_config = { 3955 .dirname = strace_groups_dir, 3956 }; 3957 3958 trace->ev_qualifier = strlist__new(lists[1], &slist_config); 3959 if (trace->ev_qualifier == NULL) { 3960 fputs("Not enough memory to parse event qualifier", trace->output); 3961 goto out; 3962 } 3963 3964 if (trace__validate_ev_qualifier(trace)) 3965 goto out; 3966 trace->trace_syscalls = true; 3967 } 3968 3969 err = 0; 3970 3971 if (lists[0]) { 3972 struct option o = OPT_CALLBACK('e', "event", &trace->evlist, "event", 3973 "event selector. use 'perf list' to list available events", 3974 parse_events_option); 3975 err = parse_events_option(&o, lists[0], 0); 3976 } 3977 out: 3978 if (sep) 3979 *sep = ','; 3980 3981 return err; 3982 } 3983 3984 static int trace__parse_cgroups(const struct option *opt, const char *str, int unset) 3985 { 3986 struct trace *trace = opt->value; 3987 3988 if (!list_empty(&trace->evlist->core.entries)) 3989 return parse_cgroups(opt, str, unset); 3990 3991 trace->cgroup = evlist__findnew_cgroup(trace->evlist, str); 3992 3993 return 0; 3994 } 3995 3996 static struct bpf_map *trace__find_bpf_map_by_name(struct trace *trace, const char *name) 3997 { 3998 if (trace->bpf_obj == NULL) 3999 return NULL; 4000 4001 return bpf_object__find_map_by_name(trace->bpf_obj, name); 4002 } 4003 4004 static void trace__set_bpf_map_filtered_pids(struct trace *trace) 4005 { 4006 trace->filter_pids.map = trace__find_bpf_map_by_name(trace, "pids_filtered"); 4007 } 4008 4009 static void trace__set_bpf_map_syscalls(struct trace *trace) 4010 { 4011 trace->syscalls.map = trace__find_bpf_map_by_name(trace, "syscalls"); 4012 trace->syscalls.prog_array.sys_enter = trace__find_bpf_map_by_name(trace, "syscalls_sys_enter"); 4013 trace->syscalls.prog_array.sys_exit = trace__find_bpf_map_by_name(trace, "syscalls_sys_exit"); 4014 } 4015 4016 static int trace__config(const char *var, const char *value, void *arg) 4017 { 4018 struct trace *trace = arg; 4019 int err = 0; 4020 4021 if (!strcmp(var, "trace.add_events")) { 4022 struct option o = OPT_CALLBACK('e', "event", &trace->evlist, "event", 4023 "event selector. use 'perf list' to list available events", 4024 parse_events_option); 4025 /* 4026 * We can't propagate parse_event_option() return, as it is 1 4027 * for failure while perf_config() expects -1. 4028 */ 4029 if (parse_events_option(&o, value, 0)) 4030 err = -1; 4031 } else if (!strcmp(var, "trace.show_timestamp")) { 4032 trace->show_tstamp = perf_config_bool(var, value); 4033 } else if (!strcmp(var, "trace.show_duration")) { 4034 trace->show_duration = perf_config_bool(var, value); 4035 } else if (!strcmp(var, "trace.show_arg_names")) { 4036 trace->show_arg_names = perf_config_bool(var, value); 4037 if (!trace->show_arg_names) 4038 trace->show_zeros = true; 4039 } else if (!strcmp(var, "trace.show_zeros")) { 4040 bool new_show_zeros = perf_config_bool(var, value); 4041 if (!trace->show_arg_names && !new_show_zeros) { 4042 pr_warning("trace.show_zeros has to be set when trace.show_arg_names=no\n"); 4043 goto out; 4044 } 4045 trace->show_zeros = new_show_zeros; 4046 } else if (!strcmp(var, "trace.show_prefix")) { 4047 trace->show_string_prefix = perf_config_bool(var, value); 4048 } else if (!strcmp(var, "trace.no_inherit")) { 4049 trace->opts.no_inherit = perf_config_bool(var, value); 4050 } else if (!strcmp(var, "trace.args_alignment")) { 4051 int args_alignment = 0; 4052 if (perf_config_int(&args_alignment, var, value) == 0) 4053 trace->args_alignment = args_alignment; 4054 } 4055 out: 4056 return err; 4057 } 4058 4059 int cmd_trace(int argc, const char **argv) 4060 { 4061 const char *trace_usage[] = { 4062 "perf trace [<options>] [<command>]", 4063 "perf trace [<options>] -- <command> [<options>]", 4064 "perf trace record [<options>] [<command>]", 4065 "perf trace record [<options>] -- <command> [<options>]", 4066 NULL 4067 }; 4068 struct trace trace = { 4069 .opts = { 4070 .target = { 4071 .uid = UINT_MAX, 4072 .uses_mmap = true, 4073 }, 4074 .user_freq = UINT_MAX, 4075 .user_interval = ULLONG_MAX, 4076 .no_buffering = true, 4077 .mmap_pages = UINT_MAX, 4078 }, 4079 .output = stderr, 4080 .show_comm = true, 4081 .show_tstamp = true, 4082 .show_duration = true, 4083 .show_arg_names = true, 4084 .args_alignment = 70, 4085 .trace_syscalls = false, 4086 .kernel_syscallchains = false, 4087 .max_stack = UINT_MAX, 4088 .max_events = ULONG_MAX, 4089 }; 4090 const char *map_dump_str = NULL; 4091 const char *output_name = NULL; 4092 const struct option trace_options[] = { 4093 OPT_CALLBACK('e', "event", &trace, "event", 4094 "event/syscall selector. use 'perf list' to list available events", 4095 trace__parse_events_option), 4096 OPT_BOOLEAN(0, "comm", &trace.show_comm, 4097 "show the thread COMM next to its id"), 4098 OPT_BOOLEAN(0, "tool_stats", &trace.show_tool_stats, "show tool stats"), 4099 OPT_CALLBACK(0, "expr", &trace, "expr", "list of syscalls/events to trace", 4100 trace__parse_events_option), 4101 OPT_STRING('o', "output", &output_name, "file", "output file name"), 4102 OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"), 4103 OPT_STRING('p', "pid", &trace.opts.target.pid, "pid", 4104 "trace events on existing process id"), 4105 OPT_STRING('t', "tid", &trace.opts.target.tid, "tid", 4106 "trace events on existing thread id"), 4107 OPT_CALLBACK(0, "filter-pids", &trace, "CSV list of pids", 4108 "pids to filter (by the kernel)", trace__set_filter_pids_from_option), 4109 OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide, 4110 "system-wide collection from all CPUs"), 4111 OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu", 4112 "list of cpus to monitor"), 4113 OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit, 4114 "child tasks do not inherit counters"), 4115 OPT_CALLBACK('m', "mmap-pages", &trace.opts.mmap_pages, "pages", 4116 "number of mmap data pages", 4117 perf_evlist__parse_mmap_pages), 4118 OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user", 4119 "user to profile"), 4120 OPT_CALLBACK(0, "duration", &trace, "float", 4121 "show only events with duration > N.M ms", 4122 trace__set_duration), 4123 #ifdef HAVE_LIBBPF_SUPPORT 4124 OPT_STRING(0, "map-dump", &map_dump_str, "BPF map", "BPF map to periodically dump"), 4125 #endif 4126 OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"), 4127 OPT_INCR('v', "verbose", &verbose, "be more verbose"), 4128 OPT_BOOLEAN('T', "time", &trace.full_time, 4129 "Show full timestamp, not time relative to first start"), 4130 OPT_BOOLEAN(0, "failure", &trace.failure_only, 4131 "Show only syscalls that failed"), 4132 OPT_BOOLEAN('s', "summary", &trace.summary_only, 4133 "Show only syscall summary with statistics"), 4134 OPT_BOOLEAN('S', "with-summary", &trace.summary, 4135 "Show all syscalls and summary with statistics"), 4136 OPT_CALLBACK_DEFAULT('F', "pf", &trace.trace_pgfaults, "all|maj|min", 4137 "Trace pagefaults", parse_pagefaults, "maj"), 4138 OPT_BOOLEAN(0, "syscalls", &trace.trace_syscalls, "Trace syscalls"), 4139 OPT_BOOLEAN('f', "force", &trace.force, "don't complain, do it"), 4140 OPT_CALLBACK(0, "call-graph", &trace.opts, 4141 "record_mode[,record_size]", record_callchain_help, 4142 &record_parse_callchain_opt), 4143 OPT_BOOLEAN(0, "kernel-syscall-graph", &trace.kernel_syscallchains, 4144 "Show the kernel callchains on the syscall exit path"), 4145 OPT_ULONG(0, "max-events", &trace.max_events, 4146 "Set the maximum number of events to print, exit after that is reached. "), 4147 OPT_UINTEGER(0, "min-stack", &trace.min_stack, 4148 "Set the minimum stack depth when parsing the callchain, " 4149 "anything below the specified depth will be ignored."), 4150 OPT_UINTEGER(0, "max-stack", &trace.max_stack, 4151 "Set the maximum stack depth when parsing the callchain, " 4152 "anything beyond the specified depth will be ignored. " 4153 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)), 4154 OPT_BOOLEAN(0, "sort-events", &trace.sort_events, 4155 "Sort batch of events before processing, use if getting out of order events"), 4156 OPT_BOOLEAN(0, "print-sample", &trace.print_sample, 4157 "print the PERF_RECORD_SAMPLE PERF_SAMPLE_ info, for debugging"), 4158 OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout, 4159 "per thread proc mmap processing timeout in ms"), 4160 OPT_CALLBACK('G', "cgroup", &trace, "name", "monitor event in cgroup name only", 4161 trace__parse_cgroups), 4162 OPT_UINTEGER('D', "delay", &trace.opts.initial_delay, 4163 "ms to wait before starting measurement after program " 4164 "start"), 4165 OPTS_EVSWITCH(&trace.evswitch), 4166 OPT_END() 4167 }; 4168 bool __maybe_unused max_stack_user_set = true; 4169 bool mmap_pages_user_set = true; 4170 struct evsel *evsel; 4171 const char * const trace_subcommands[] = { "record", NULL }; 4172 int err = -1; 4173 char bf[BUFSIZ]; 4174 4175 signal(SIGSEGV, sighandler_dump_stack); 4176 signal(SIGFPE, sighandler_dump_stack); 4177 4178 trace.evlist = evlist__new(); 4179 trace.sctbl = syscalltbl__new(); 4180 4181 if (trace.evlist == NULL || trace.sctbl == NULL) { 4182 pr_err("Not enough memory to run!\n"); 4183 err = -ENOMEM; 4184 goto out; 4185 } 4186 4187 /* 4188 * Parsing .perfconfig may entail creating a BPF event, that may need 4189 * to create BPF maps, so bump RLIM_MEMLOCK as the default 64K setting 4190 * is too small. This affects just this process, not touching the 4191 * global setting. If it fails we'll get something in 'perf trace -v' 4192 * to help diagnose the problem. 4193 */ 4194 rlimit__bump_memlock(); 4195 4196 err = perf_config(trace__config, &trace); 4197 if (err) 4198 goto out; 4199 4200 argc = parse_options_subcommand(argc, argv, trace_options, trace_subcommands, 4201 trace_usage, PARSE_OPT_STOP_AT_NON_OPTION); 4202 4203 if ((nr_cgroups || trace.cgroup) && !trace.opts.target.system_wide) { 4204 usage_with_options_msg(trace_usage, trace_options, 4205 "cgroup monitoring only available in system-wide mode"); 4206 } 4207 4208 evsel = bpf__setup_output_event(trace.evlist, "__augmented_syscalls__"); 4209 if (IS_ERR(evsel)) { 4210 bpf__strerror_setup_output_event(trace.evlist, PTR_ERR(evsel), bf, sizeof(bf)); 4211 pr_err("ERROR: Setup trace syscalls enter failed: %s\n", bf); 4212 goto out; 4213 } 4214 4215 if (evsel) { 4216 trace.syscalls.events.augmented = evsel; 4217 4218 evsel = perf_evlist__find_tracepoint_by_name(trace.evlist, "raw_syscalls:sys_enter"); 4219 if (evsel == NULL) { 4220 pr_err("ERROR: raw_syscalls:sys_enter not found in the augmented BPF object\n"); 4221 goto out; 4222 } 4223 4224 if (evsel->bpf_obj == NULL) { 4225 pr_err("ERROR: raw_syscalls:sys_enter not associated to a BPF object\n"); 4226 goto out; 4227 } 4228 4229 trace.bpf_obj = evsel->bpf_obj; 4230 4231 trace__set_bpf_map_filtered_pids(&trace); 4232 trace__set_bpf_map_syscalls(&trace); 4233 trace.syscalls.unaugmented_prog = trace__find_bpf_program_by_title(&trace, "!raw_syscalls:unaugmented"); 4234 } 4235 4236 err = bpf__setup_stdout(trace.evlist); 4237 if (err) { 4238 bpf__strerror_setup_stdout(trace.evlist, err, bf, sizeof(bf)); 4239 pr_err("ERROR: Setup BPF stdout failed: %s\n", bf); 4240 goto out; 4241 } 4242 4243 err = -1; 4244 4245 if (map_dump_str) { 4246 trace.dump.map = trace__find_bpf_map_by_name(&trace, map_dump_str); 4247 if (trace.dump.map == NULL) { 4248 pr_err("ERROR: BPF map \"%s\" not found\n", map_dump_str); 4249 goto out; 4250 } 4251 } 4252 4253 if (trace.trace_pgfaults) { 4254 trace.opts.sample_address = true; 4255 trace.opts.sample_time = true; 4256 } 4257 4258 if (trace.opts.mmap_pages == UINT_MAX) 4259 mmap_pages_user_set = false; 4260 4261 if (trace.max_stack == UINT_MAX) { 4262 trace.max_stack = input_name ? PERF_MAX_STACK_DEPTH : sysctl__max_stack(); 4263 max_stack_user_set = false; 4264 } 4265 4266 #ifdef HAVE_DWARF_UNWIND_SUPPORT 4267 if ((trace.min_stack || max_stack_user_set) && !callchain_param.enabled) { 4268 record_opts__parse_callchain(&trace.opts, &callchain_param, "dwarf", false); 4269 } 4270 #endif 4271 4272 if (callchain_param.enabled) { 4273 if (!mmap_pages_user_set && geteuid() == 0) 4274 trace.opts.mmap_pages = perf_event_mlock_kb_in_pages() * 4; 4275 4276 symbol_conf.use_callchain = true; 4277 } 4278 4279 if (trace.evlist->core.nr_entries > 0) { 4280 evlist__set_evsel_handler(trace.evlist, trace__event_handler); 4281 if (evlist__set_syscall_tp_fields(trace.evlist)) { 4282 perror("failed to set syscalls:* tracepoint fields"); 4283 goto out; 4284 } 4285 } 4286 4287 if (trace.sort_events) { 4288 ordered_events__init(&trace.oe.data, ordered_events__deliver_event, &trace); 4289 ordered_events__set_copy_on_queue(&trace.oe.data, true); 4290 } 4291 4292 /* 4293 * If we are augmenting syscalls, then combine what we put in the 4294 * __augmented_syscalls__ BPF map with what is in the 4295 * syscalls:sys_exit_FOO tracepoints, i.e. just like we do without BPF, 4296 * combining raw_syscalls:sys_enter with raw_syscalls:sys_exit. 4297 * 4298 * We'll switch to look at two BPF maps, one for sys_enter and the 4299 * other for sys_exit when we start augmenting the sys_exit paths with 4300 * buffers that are being copied from kernel to userspace, think 'read' 4301 * syscall. 4302 */ 4303 if (trace.syscalls.events.augmented) { 4304 evlist__for_each_entry(trace.evlist, evsel) { 4305 bool raw_syscalls_sys_exit = strcmp(perf_evsel__name(evsel), "raw_syscalls:sys_exit") == 0; 4306 4307 if (raw_syscalls_sys_exit) { 4308 trace.raw_augmented_syscalls = true; 4309 goto init_augmented_syscall_tp; 4310 } 4311 4312 if (trace.syscalls.events.augmented->priv == NULL && 4313 strstr(perf_evsel__name(evsel), "syscalls:sys_enter")) { 4314 struct evsel *augmented = trace.syscalls.events.augmented; 4315 if (perf_evsel__init_augmented_syscall_tp(augmented, evsel) || 4316 perf_evsel__init_augmented_syscall_tp_args(augmented)) 4317 goto out; 4318 /* 4319 * Augmented is __augmented_syscalls__ BPF_OUTPUT event 4320 * Above we made sure we can get from the payload the tp fields 4321 * that we get from syscalls:sys_enter tracefs format file. 4322 */ 4323 augmented->handler = trace__sys_enter; 4324 /* 4325 * Now we do the same for the *syscalls:sys_enter event so that 4326 * if we handle it directly, i.e. if the BPF prog returns 0 so 4327 * as not to filter it, then we'll handle it just like we would 4328 * for the BPF_OUTPUT one: 4329 */ 4330 if (perf_evsel__init_augmented_syscall_tp(evsel, evsel) || 4331 perf_evsel__init_augmented_syscall_tp_args(evsel)) 4332 goto out; 4333 evsel->handler = trace__sys_enter; 4334 } 4335 4336 if (strstarts(perf_evsel__name(evsel), "syscalls:sys_exit_")) { 4337 struct syscall_tp *sc; 4338 init_augmented_syscall_tp: 4339 if (perf_evsel__init_augmented_syscall_tp(evsel, evsel)) 4340 goto out; 4341 sc = evsel->priv; 4342 /* 4343 * For now with BPF raw_augmented we hook into 4344 * raw_syscalls:sys_enter and there we get all 4345 * 6 syscall args plus the tracepoint common 4346 * fields and the syscall_nr (another long). 4347 * So we check if that is the case and if so 4348 * don't look after the sc->args_size but 4349 * always after the full raw_syscalls:sys_enter 4350 * payload, which is fixed. 4351 * 4352 * We'll revisit this later to pass 4353 * s->args_size to the BPF augmenter (now 4354 * tools/perf/examples/bpf/augmented_raw_syscalls.c, 4355 * so that it copies only what we need for each 4356 * syscall, like what happens when we use 4357 * syscalls:sys_enter_NAME, so that we reduce 4358 * the kernel/userspace traffic to just what is 4359 * needed for each syscall. 4360 */ 4361 if (trace.raw_augmented_syscalls) 4362 trace.raw_augmented_syscalls_args_size = (6 + 1) * sizeof(long) + sc->id.offset; 4363 perf_evsel__init_augmented_syscall_tp_ret(evsel); 4364 evsel->handler = trace__sys_exit; 4365 } 4366 } 4367 } 4368 4369 if ((argc >= 1) && (strcmp(argv[0], "record") == 0)) 4370 return trace__record(&trace, argc-1, &argv[1]); 4371 4372 /* summary_only implies summary option, but don't overwrite summary if set */ 4373 if (trace.summary_only) 4374 trace.summary = trace.summary_only; 4375 4376 if (!trace.trace_syscalls && !trace.trace_pgfaults && 4377 trace.evlist->core.nr_entries == 0 /* Was --events used? */) { 4378 trace.trace_syscalls = true; 4379 } 4380 4381 if (output_name != NULL) { 4382 err = trace__open_output(&trace, output_name); 4383 if (err < 0) { 4384 perror("failed to create output file"); 4385 goto out; 4386 } 4387 } 4388 4389 err = evswitch__init(&trace.evswitch, trace.evlist, stderr); 4390 if (err) 4391 goto out_close; 4392 4393 err = target__validate(&trace.opts.target); 4394 if (err) { 4395 target__strerror(&trace.opts.target, err, bf, sizeof(bf)); 4396 fprintf(trace.output, "%s", bf); 4397 goto out_close; 4398 } 4399 4400 err = target__parse_uid(&trace.opts.target); 4401 if (err) { 4402 target__strerror(&trace.opts.target, err, bf, sizeof(bf)); 4403 fprintf(trace.output, "%s", bf); 4404 goto out_close; 4405 } 4406 4407 if (!argc && target__none(&trace.opts.target)) 4408 trace.opts.target.system_wide = true; 4409 4410 if (input_name) 4411 err = trace__replay(&trace); 4412 else 4413 err = trace__run(&trace, argc, argv); 4414 4415 out_close: 4416 if (output_name != NULL) 4417 fclose(trace.output); 4418 out: 4419 return err; 4420 } 4421