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