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