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 <api/fs/tracing_path.h> 19 #ifdef HAVE_LIBBPF_SUPPORT 20 #include <bpf/bpf.h> 21 #include <bpf/libbpf.h> 22 #include <bpf/btf.h> 23 #endif 24 #include "util/bpf_map.h" 25 #include "util/rlimit.h" 26 #include "builtin.h" 27 #include "util/cgroup.h" 28 #include "util/color.h" 29 #include "util/config.h" 30 #include "util/debug.h" 31 #include "util/dso.h" 32 #include "util/env.h" 33 #include "util/event.h" 34 #include "util/evsel.h" 35 #include "util/evsel_fprintf.h" 36 #include "util/synthetic-events.h" 37 #include "util/evlist.h" 38 #include "util/evswitch.h" 39 #include "util/hashmap.h" 40 #include "util/mmap.h" 41 #include <subcmd/pager.h> 42 #include <subcmd/exec-cmd.h> 43 #include "util/machine.h" 44 #include "util/map.h" 45 #include "util/symbol.h" 46 #include "util/path.h" 47 #include "util/session.h" 48 #include "util/thread.h" 49 #include <subcmd/parse-options.h> 50 #include "util/strlist.h" 51 #include "util/intlist.h" 52 #include "util/thread_map.h" 53 #include "util/stat.h" 54 #include "util/tool.h" 55 #include "util/trace.h" 56 #include "util/util.h" 57 #include "trace/beauty/beauty.h" 58 #include "trace-event.h" 59 #include "util/parse-events.h" 60 #include "util/tracepoint.h" 61 #include "callchain.h" 62 #include "print_binary.h" 63 #include "string2.h" 64 #include "syscalltbl.h" 65 #include "../perf.h" 66 #include "trace_augment.h" 67 #include "dwarf-regs.h" 68 69 #include <errno.h> 70 #include <inttypes.h> 71 #include <poll.h> 72 #include <signal.h> 73 #include <stdlib.h> 74 #include <string.h> 75 #include <linux/err.h> 76 #include <linux/filter.h> 77 #include <linux/kernel.h> 78 #include <linux/list_sort.h> 79 #include <linux/random.h> 80 #include <linux/stringify.h> 81 #include <linux/time64.h> 82 #include <linux/zalloc.h> 83 #include <fcntl.h> 84 #include <sys/sysmacros.h> 85 86 #include <linux/ctype.h> 87 #include <perf/mmap.h> 88 #include <tools/libc_compat.h> 89 90 #ifdef HAVE_LIBTRACEEVENT 91 #include <event-parse.h> 92 #endif 93 94 #ifndef O_CLOEXEC 95 # define O_CLOEXEC 02000000 96 #endif 97 98 #ifndef F_LINUX_SPECIFIC_BASE 99 # define F_LINUX_SPECIFIC_BASE 1024 100 #endif 101 102 #define RAW_SYSCALL_ARGS_NUM 6 103 104 /* 105 * strtoul: Go from a string to a value, i.e. for msr: MSR_FS_BASE to 0xc0000100 106 * 107 * We have to explicitely mark the direction of the flow of data, if from the 108 * kernel to user space or the other way around, since the BPF collector we 109 * have so far copies only from user to kernel space, mark the arguments that 110 * go that direction, so that we don´t end up collecting the previous contents 111 * for syscall args that goes from kernel to user space. 112 */ 113 struct syscall_arg_fmt { 114 size_t (*scnprintf)(char *bf, size_t size, struct syscall_arg *arg); 115 bool (*strtoul)(char *bf, size_t size, struct syscall_arg *arg, u64 *val); 116 unsigned long (*mask_val)(struct syscall_arg *arg, unsigned long val); 117 void *parm; 118 const char *name; 119 u16 nr_entries; // for arrays 120 bool from_user; 121 bool show_zero; 122 #ifdef HAVE_LIBBPF_SUPPORT 123 const struct btf_type *type; 124 int type_id; /* used in btf_dump */ 125 #endif 126 }; 127 128 struct syscall_fmt { 129 const char *name; 130 const char *alias; 131 struct { 132 const char *sys_enter, 133 *sys_exit; 134 } bpf_prog_name; 135 struct syscall_arg_fmt arg[RAW_SYSCALL_ARGS_NUM]; 136 u8 nr_args; 137 bool errpid; 138 bool timeout; 139 bool hexret; 140 }; 141 142 struct trace { 143 struct perf_env host_env; 144 struct perf_tool tool; 145 struct { 146 /** Sorted sycall numbers used by the trace. */ 147 struct syscall **table; 148 /** Size of table. */ 149 size_t table_size; 150 struct { 151 struct evsel *sys_enter, 152 *sys_exit, 153 *bpf_output; 154 } events; 155 } syscalls; 156 #ifdef HAVE_LIBBPF_SUPPORT 157 struct btf *btf; 158 #endif 159 struct record_opts opts; 160 struct evlist *evlist; 161 struct machine *host; 162 struct thread *current; 163 struct cgroup *cgroup; 164 u64 base_time; 165 FILE *output; 166 unsigned long nr_events; 167 unsigned long nr_events_printed; 168 unsigned long max_events; 169 struct evswitch evswitch; 170 struct strlist *ev_qualifier; 171 struct { 172 size_t nr; 173 int *entries; 174 } ev_qualifier_ids; 175 struct { 176 size_t nr; 177 pid_t *entries; 178 struct bpf_map *map; 179 } filter_pids; 180 /* 181 * TODO: The map is from an ID (aka system call number) to struct 182 * syscall_stats. If there is >1 e_machine, such as i386 and x86-64 183 * processes, then the stats here will gather wrong the statistics for 184 * the non EM_HOST system calls. A fix would be to add the e_machine 185 * into the key, but this would make the code inconsistent with the 186 * per-thread version. 187 */ 188 struct hashmap *syscall_stats; 189 double duration_filter; 190 double runtime_ms; 191 unsigned long pfmaj, pfmin; 192 struct { 193 u64 vfs_getname, 194 proc_getname; 195 } stats; 196 unsigned int max_stack; 197 unsigned int min_stack; 198 enum trace_summary_mode summary_mode; 199 int max_summary; 200 int raw_augmented_syscalls_args_size; 201 bool raw_augmented_syscalls; 202 bool fd_path_disabled; 203 bool sort_events; 204 bool not_ev_qualifier; 205 bool live; 206 bool full_time; 207 bool sched; 208 bool multiple_threads; 209 bool summary; 210 bool summary_only; 211 bool errno_summary; 212 bool failure_only; 213 bool show_comm; 214 bool print_sample; 215 bool show_tool_stats; 216 bool trace_syscalls; 217 bool libtraceevent_print; 218 bool kernel_syscallchains; 219 s16 args_alignment; 220 bool show_tstamp; 221 bool show_duration; 222 bool show_zeros; 223 bool show_arg_names; 224 bool show_string_prefix; 225 bool force; 226 bool vfs_getname; 227 bool force_btf; 228 bool summary_bpf; 229 int trace_pgfaults; 230 char *perfconfig_events; 231 struct { 232 struct ordered_events data; 233 u64 last; 234 } oe; 235 const char *uid_str; 236 }; 237 238 static void trace__load_vmlinux_btf(struct trace *trace __maybe_unused) 239 { 240 #ifdef HAVE_LIBBPF_SUPPORT 241 if (trace->btf != NULL) 242 return; 243 244 trace->btf = btf__load_vmlinux_btf(); 245 if (verbose > 0) { 246 fprintf(trace->output, trace->btf ? "vmlinux BTF loaded\n" : 247 "Failed to load vmlinux BTF\n"); 248 } 249 #endif 250 } 251 252 struct tp_field { 253 int offset; 254 union { 255 u64 (*integer)(struct tp_field *field, struct perf_sample *sample); 256 void *(*pointer)(struct tp_field *field, struct perf_sample *sample); 257 }; 258 }; 259 260 #define TP_UINT_FIELD(bits) \ 261 static u64 tp_field__u##bits(struct tp_field *field, struct perf_sample *sample) \ 262 { \ 263 u##bits value; \ 264 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \ 265 return value; \ 266 } 267 268 TP_UINT_FIELD(8); 269 TP_UINT_FIELD(16); 270 TP_UINT_FIELD(32); 271 TP_UINT_FIELD(64); 272 273 #define TP_UINT_FIELD__SWAPPED(bits) \ 274 static u64 tp_field__swapped_u##bits(struct tp_field *field, struct perf_sample *sample) \ 275 { \ 276 u##bits value; \ 277 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \ 278 return bswap_##bits(value);\ 279 } 280 281 TP_UINT_FIELD__SWAPPED(16); 282 TP_UINT_FIELD__SWAPPED(32); 283 TP_UINT_FIELD__SWAPPED(64); 284 285 static int __tp_field__init_uint(struct tp_field *field, int size, int offset, bool needs_swap) 286 { 287 field->offset = offset; 288 289 switch (size) { 290 case 1: 291 field->integer = tp_field__u8; 292 break; 293 case 2: 294 field->integer = needs_swap ? tp_field__swapped_u16 : tp_field__u16; 295 break; 296 case 4: 297 field->integer = needs_swap ? tp_field__swapped_u32 : tp_field__u32; 298 break; 299 case 8: 300 field->integer = needs_swap ? tp_field__swapped_u64 : tp_field__u64; 301 break; 302 default: 303 return -1; 304 } 305 306 return 0; 307 } 308 309 static int tp_field__init_uint(struct tp_field *field, struct tep_format_field *format_field, bool needs_swap) 310 { 311 return __tp_field__init_uint(field, format_field->size, format_field->offset, needs_swap); 312 } 313 314 static void *tp_field__ptr(struct tp_field *field, struct perf_sample *sample) 315 { 316 return sample->raw_data + field->offset; 317 } 318 319 static int __tp_field__init_ptr(struct tp_field *field, int offset) 320 { 321 field->offset = offset; 322 field->pointer = tp_field__ptr; 323 return 0; 324 } 325 326 static int tp_field__init_ptr(struct tp_field *field, struct tep_format_field *format_field) 327 { 328 return __tp_field__init_ptr(field, format_field->offset); 329 } 330 331 struct syscall_tp { 332 struct tp_field id; 333 union { 334 struct tp_field args, ret; 335 }; 336 }; 337 338 /* 339 * The evsel->priv as used by 'perf trace' 340 * sc: for raw_syscalls:sys_{enter,exit} and syscalls:sys_{enter,exit}_SYSCALLNAME 341 * fmt: for all the other tracepoints 342 */ 343 struct evsel_trace { 344 struct syscall_tp sc; 345 struct syscall_arg_fmt *fmt; 346 }; 347 348 static struct evsel_trace *evsel_trace__new(void) 349 { 350 return zalloc(sizeof(struct evsel_trace)); 351 } 352 353 static void evsel_trace__delete(struct evsel_trace *et) 354 { 355 if (et == NULL) 356 return; 357 358 zfree(&et->fmt); 359 free(et); 360 } 361 362 /* 363 * Used with raw_syscalls:sys_{enter,exit} and with the 364 * syscalls:sys_{enter,exit}_SYSCALL tracepoints 365 */ 366 static inline struct syscall_tp *__evsel__syscall_tp(struct evsel *evsel) 367 { 368 struct evsel_trace *et = evsel->priv; 369 370 return &et->sc; 371 } 372 373 static struct syscall_tp *evsel__syscall_tp(struct evsel *evsel) 374 { 375 if (evsel->priv == NULL) { 376 evsel->priv = evsel_trace__new(); 377 if (evsel->priv == NULL) 378 return NULL; 379 } 380 381 return __evsel__syscall_tp(evsel); 382 } 383 384 /* 385 * Used with all the other tracepoints. 386 */ 387 static inline struct syscall_arg_fmt *__evsel__syscall_arg_fmt(struct evsel *evsel) 388 { 389 struct evsel_trace *et = evsel->priv; 390 391 return et->fmt; 392 } 393 394 static struct syscall_arg_fmt *evsel__syscall_arg_fmt(struct evsel *evsel) 395 { 396 struct evsel_trace *et = evsel->priv; 397 398 if (evsel->priv == NULL) { 399 et = evsel->priv = evsel_trace__new(); 400 401 if (et == NULL) 402 return NULL; 403 } 404 405 if (et->fmt == NULL) { 406 const struct tep_event *tp_format = evsel__tp_format(evsel); 407 408 if (tp_format == NULL) 409 goto out_delete; 410 411 et->fmt = calloc(tp_format->format.nr_fields, sizeof(struct syscall_arg_fmt)); 412 if (et->fmt == NULL) 413 goto out_delete; 414 } 415 416 return __evsel__syscall_arg_fmt(evsel); 417 418 out_delete: 419 evsel_trace__delete(evsel->priv); 420 evsel->priv = NULL; 421 return NULL; 422 } 423 424 static int evsel__init_tp_uint_field(struct evsel *evsel, struct tp_field *field, const char *name) 425 { 426 struct tep_format_field *format_field = evsel__field(evsel, name); 427 428 if (format_field == NULL) 429 return -1; 430 431 return tp_field__init_uint(field, format_field, evsel->needs_swap); 432 } 433 434 #define perf_evsel__init_sc_tp_uint_field(evsel, name) \ 435 ({ struct syscall_tp *sc = __evsel__syscall_tp(evsel);\ 436 evsel__init_tp_uint_field(evsel, &sc->name, #name); }) 437 438 static int evsel__init_tp_ptr_field(struct evsel *evsel, struct tp_field *field, const char *name) 439 { 440 struct tep_format_field *format_field = evsel__field(evsel, name); 441 442 if (format_field == NULL) 443 return -1; 444 445 return tp_field__init_ptr(field, format_field); 446 } 447 448 #define perf_evsel__init_sc_tp_ptr_field(evsel, name) \ 449 ({ struct syscall_tp *sc = __evsel__syscall_tp(evsel);\ 450 evsel__init_tp_ptr_field(evsel, &sc->name, #name); }) 451 452 static void evsel__delete_priv(struct evsel *evsel) 453 { 454 zfree(&evsel->priv); 455 evsel__delete(evsel); 456 } 457 458 static int evsel__init_syscall_tp(struct evsel *evsel) 459 { 460 struct syscall_tp *sc = evsel__syscall_tp(evsel); 461 462 if (sc != NULL) { 463 if (evsel__init_tp_uint_field(evsel, &sc->id, "__syscall_nr") && 464 evsel__init_tp_uint_field(evsel, &sc->id, "nr")) 465 return -ENOENT; 466 467 return 0; 468 } 469 470 return -ENOMEM; 471 } 472 473 static int evsel__init_augmented_syscall_tp(struct evsel *evsel, struct evsel *tp) 474 { 475 struct syscall_tp *sc = evsel__syscall_tp(evsel); 476 477 if (sc != NULL) { 478 struct tep_format_field *syscall_id = evsel__field(tp, "id"); 479 if (syscall_id == NULL) 480 syscall_id = evsel__field(tp, "__syscall_nr"); 481 if (syscall_id == NULL || 482 __tp_field__init_uint(&sc->id, syscall_id->size, syscall_id->offset, evsel->needs_swap)) 483 return -EINVAL; 484 485 return 0; 486 } 487 488 return -ENOMEM; 489 } 490 491 static int evsel__init_augmented_syscall_tp_args(struct evsel *evsel) 492 { 493 struct syscall_tp *sc = __evsel__syscall_tp(evsel); 494 495 return __tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64)); 496 } 497 498 static int evsel__init_augmented_syscall_tp_ret(struct evsel *evsel) 499 { 500 struct syscall_tp *sc = __evsel__syscall_tp(evsel); 501 502 return __tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap); 503 } 504 505 static int evsel__init_raw_syscall_tp(struct evsel *evsel, void *handler) 506 { 507 if (evsel__syscall_tp(evsel) != NULL) { 508 if (perf_evsel__init_sc_tp_uint_field(evsel, id)) 509 return -ENOENT; 510 511 evsel->handler = handler; 512 return 0; 513 } 514 515 return -ENOMEM; 516 } 517 518 static struct evsel *perf_evsel__raw_syscall_newtp(const char *direction, void *handler) 519 { 520 struct evsel *evsel = evsel__newtp("raw_syscalls", direction); 521 522 /* older kernel (e.g., RHEL6) use syscalls:{enter,exit} */ 523 if (IS_ERR(evsel)) 524 evsel = evsel__newtp("syscalls", direction); 525 526 if (IS_ERR(evsel)) 527 return NULL; 528 529 if (evsel__init_raw_syscall_tp(evsel, handler)) 530 goto out_delete; 531 532 return evsel; 533 534 out_delete: 535 evsel__delete_priv(evsel); 536 return NULL; 537 } 538 539 #define perf_evsel__sc_tp_uint(evsel, name, sample) \ 540 ({ struct syscall_tp *fields = __evsel__syscall_tp(evsel); \ 541 fields->name.integer(&fields->name, sample); }) 542 543 #define perf_evsel__sc_tp_ptr(evsel, name, sample) \ 544 ({ struct syscall_tp *fields = __evsel__syscall_tp(evsel); \ 545 fields->name.pointer(&fields->name, sample); }) 546 547 size_t strarray__scnprintf_suffix(struct strarray *sa, char *bf, size_t size, const char *intfmt, bool show_suffix, int val) 548 { 549 int idx = val - sa->offset; 550 551 if (idx < 0 || idx >= sa->nr_entries || sa->entries[idx] == NULL) { 552 size_t printed = scnprintf(bf, size, intfmt, val); 553 if (show_suffix) 554 printed += scnprintf(bf + printed, size - printed, " /* %s??? */", sa->prefix); 555 return printed; 556 } 557 558 return scnprintf(bf, size, "%s%s", sa->entries[idx], show_suffix ? sa->prefix : ""); 559 } 560 561 size_t strarray__scnprintf(struct strarray *sa, char *bf, size_t size, const char *intfmt, bool show_prefix, int val) 562 { 563 int idx = val - sa->offset; 564 565 if (idx < 0 || idx >= sa->nr_entries || sa->entries[idx] == NULL) { 566 size_t printed = scnprintf(bf, size, intfmt, val); 567 if (show_prefix) 568 printed += scnprintf(bf + printed, size - printed, " /* %s??? */", sa->prefix); 569 return printed; 570 } 571 572 return scnprintf(bf, size, "%s%s", show_prefix ? sa->prefix : "", sa->entries[idx]); 573 } 574 575 static size_t __syscall_arg__scnprintf_strarray(char *bf, size_t size, 576 const char *intfmt, 577 struct syscall_arg *arg) 578 { 579 return strarray__scnprintf(arg->parm, bf, size, intfmt, arg->show_string_prefix, arg->val); 580 } 581 582 static size_t syscall_arg__scnprintf_strarray(char *bf, size_t size, 583 struct syscall_arg *arg) 584 { 585 return __syscall_arg__scnprintf_strarray(bf, size, "%d", arg); 586 } 587 588 #define SCA_STRARRAY syscall_arg__scnprintf_strarray 589 590 bool syscall_arg__strtoul_strarray(char *bf, size_t size, struct syscall_arg *arg, u64 *ret) 591 { 592 return strarray__strtoul(arg->parm, bf, size, ret); 593 } 594 595 bool syscall_arg__strtoul_strarray_flags(char *bf, size_t size, struct syscall_arg *arg, u64 *ret) 596 { 597 return strarray__strtoul_flags(arg->parm, bf, size, ret); 598 } 599 600 bool syscall_arg__strtoul_strarrays(char *bf, size_t size, struct syscall_arg *arg, u64 *ret) 601 { 602 return strarrays__strtoul(arg->parm, bf, size, ret); 603 } 604 605 size_t syscall_arg__scnprintf_strarray_flags(char *bf, size_t size, struct syscall_arg *arg) 606 { 607 return strarray__scnprintf_flags(arg->parm, bf, size, arg->show_string_prefix, arg->val); 608 } 609 610 size_t strarrays__scnprintf(struct strarrays *sas, char *bf, size_t size, const char *intfmt, bool show_prefix, int val) 611 { 612 size_t printed; 613 int i; 614 615 for (i = 0; i < sas->nr_entries; ++i) { 616 struct strarray *sa = sas->entries[i]; 617 int idx = val - sa->offset; 618 619 if (idx >= 0 && idx < sa->nr_entries) { 620 if (sa->entries[idx] == NULL) 621 break; 622 return scnprintf(bf, size, "%s%s", show_prefix ? sa->prefix : "", sa->entries[idx]); 623 } 624 } 625 626 printed = scnprintf(bf, size, intfmt, val); 627 if (show_prefix) 628 printed += scnprintf(bf + printed, size - printed, " /* %s??? */", sas->entries[0]->prefix); 629 return printed; 630 } 631 632 bool strarray__strtoul(struct strarray *sa, char *bf, size_t size, u64 *ret) 633 { 634 int i; 635 636 for (i = 0; i < sa->nr_entries; ++i) { 637 if (sa->entries[i] && strncmp(sa->entries[i], bf, size) == 0 && sa->entries[i][size] == '\0') { 638 *ret = sa->offset + i; 639 return true; 640 } 641 } 642 643 return false; 644 } 645 646 bool strarray__strtoul_flags(struct strarray *sa, char *bf, size_t size, u64 *ret) 647 { 648 u64 val = 0; 649 char *tok = bf, *sep, *end; 650 651 *ret = 0; 652 653 while (size != 0) { 654 int toklen = size; 655 656 sep = memchr(tok, '|', size); 657 if (sep != NULL) { 658 size -= sep - tok + 1; 659 660 end = sep - 1; 661 while (end > tok && isspace(*end)) 662 --end; 663 664 toklen = end - tok + 1; 665 } 666 667 while (isspace(*tok)) 668 ++tok; 669 670 if (isalpha(*tok) || *tok == '_') { 671 if (!strarray__strtoul(sa, tok, toklen, &val)) 672 return false; 673 } else 674 val = strtoul(tok, NULL, 0); 675 676 *ret |= (1 << (val - 1)); 677 678 if (sep == NULL) 679 break; 680 tok = sep + 1; 681 } 682 683 return true; 684 } 685 686 bool strarrays__strtoul(struct strarrays *sas, char *bf, size_t size, u64 *ret) 687 { 688 int i; 689 690 for (i = 0; i < sas->nr_entries; ++i) { 691 struct strarray *sa = sas->entries[i]; 692 693 if (strarray__strtoul(sa, bf, size, ret)) 694 return true; 695 } 696 697 return false; 698 } 699 700 size_t syscall_arg__scnprintf_strarrays(char *bf, size_t size, 701 struct syscall_arg *arg) 702 { 703 return strarrays__scnprintf(arg->parm, bf, size, "%d", arg->show_string_prefix, arg->val); 704 } 705 706 #ifndef AT_FDCWD 707 #define AT_FDCWD -100 708 #endif 709 710 static size_t syscall_arg__scnprintf_fd_at(char *bf, size_t size, 711 struct syscall_arg *arg) 712 { 713 int fd = arg->val; 714 const char *prefix = "AT_FD"; 715 716 if (fd == AT_FDCWD) 717 return scnprintf(bf, size, "%s%s", arg->show_string_prefix ? prefix : "", "CWD"); 718 719 return syscall_arg__scnprintf_fd(bf, size, arg); 720 } 721 722 #define SCA_FDAT syscall_arg__scnprintf_fd_at 723 724 static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size, 725 struct syscall_arg *arg); 726 727 #define SCA_CLOSE_FD syscall_arg__scnprintf_close_fd 728 729 size_t syscall_arg__scnprintf_hex(char *bf, size_t size, struct syscall_arg *arg) 730 { 731 return scnprintf(bf, size, "%#lx", arg->val); 732 } 733 734 size_t syscall_arg__scnprintf_ptr(char *bf, size_t size, struct syscall_arg *arg) 735 { 736 if (arg->val == 0) 737 return scnprintf(bf, size, "NULL"); 738 return syscall_arg__scnprintf_hex(bf, size, arg); 739 } 740 741 size_t syscall_arg__scnprintf_int(char *bf, size_t size, struct syscall_arg *arg) 742 { 743 return scnprintf(bf, size, "%d", arg->val); 744 } 745 746 size_t syscall_arg__scnprintf_long(char *bf, size_t size, struct syscall_arg *arg) 747 { 748 return scnprintf(bf, size, "%ld", arg->val); 749 } 750 751 static size_t syscall_arg__scnprintf_char_array(char *bf, size_t size, struct syscall_arg *arg) 752 { 753 // XXX Hey, maybe for sched:sched_switch prev/next comm fields we can 754 // fill missing comms using thread__set_comm()... 755 // here or in a special syscall_arg__scnprintf_pid_sched_tp... 756 return scnprintf(bf, size, "\"%-.*s\"", arg->fmt->nr_entries ?: arg->len, arg->val); 757 } 758 759 #define SCA_CHAR_ARRAY syscall_arg__scnprintf_char_array 760 761 static const char *bpf_cmd[] = { 762 "MAP_CREATE", "MAP_LOOKUP_ELEM", "MAP_UPDATE_ELEM", "MAP_DELETE_ELEM", 763 "MAP_GET_NEXT_KEY", "PROG_LOAD", "OBJ_PIN", "OBJ_GET", "PROG_ATTACH", 764 "PROG_DETACH", "PROG_TEST_RUN", "PROG_GET_NEXT_ID", "MAP_GET_NEXT_ID", 765 "PROG_GET_FD_BY_ID", "MAP_GET_FD_BY_ID", "OBJ_GET_INFO_BY_FD", 766 "PROG_QUERY", "RAW_TRACEPOINT_OPEN", "BTF_LOAD", "BTF_GET_FD_BY_ID", 767 "TASK_FD_QUERY", "MAP_LOOKUP_AND_DELETE_ELEM", "MAP_FREEZE", 768 "BTF_GET_NEXT_ID", "MAP_LOOKUP_BATCH", "MAP_LOOKUP_AND_DELETE_BATCH", 769 "MAP_UPDATE_BATCH", "MAP_DELETE_BATCH", "LINK_CREATE", "LINK_UPDATE", 770 "LINK_GET_FD_BY_ID", "LINK_GET_NEXT_ID", "ENABLE_STATS", "ITER_CREATE", 771 "LINK_DETACH", "PROG_BIND_MAP", 772 }; 773 static DEFINE_STRARRAY(bpf_cmd, "BPF_"); 774 775 static const char *fsmount_flags[] = { 776 [1] = "CLOEXEC", 777 }; 778 static DEFINE_STRARRAY(fsmount_flags, "FSMOUNT_"); 779 780 #include "trace/beauty/generated/fsconfig_arrays.c" 781 782 static DEFINE_STRARRAY(fsconfig_cmds, "FSCONFIG_"); 783 784 static const char *epoll_ctl_ops[] = { "ADD", "DEL", "MOD", }; 785 static DEFINE_STRARRAY_OFFSET(epoll_ctl_ops, "EPOLL_CTL_", 1); 786 787 static const char *itimers[] = { "REAL", "VIRTUAL", "PROF", }; 788 static DEFINE_STRARRAY(itimers, "ITIMER_"); 789 790 static const char *keyctl_options[] = { 791 "GET_KEYRING_ID", "JOIN_SESSION_KEYRING", "UPDATE", "REVOKE", "CHOWN", 792 "SETPERM", "DESCRIBE", "CLEAR", "LINK", "UNLINK", "SEARCH", "READ", 793 "INSTANTIATE", "NEGATE", "SET_REQKEY_KEYRING", "SET_TIMEOUT", 794 "ASSUME_AUTHORITY", "GET_SECURITY", "SESSION_TO_PARENT", "REJECT", 795 "INSTANTIATE_IOV", "INVALIDATE", "GET_PERSISTENT", 796 }; 797 static DEFINE_STRARRAY(keyctl_options, "KEYCTL_"); 798 799 static const char *whences[] = { "SET", "CUR", "END", 800 #ifdef SEEK_DATA 801 "DATA", 802 #endif 803 #ifdef SEEK_HOLE 804 "HOLE", 805 #endif 806 }; 807 static DEFINE_STRARRAY(whences, "SEEK_"); 808 809 static const char *fcntl_cmds[] = { 810 "DUPFD", "GETFD", "SETFD", "GETFL", "SETFL", "GETLK", "SETLK", 811 "SETLKW", "SETOWN", "GETOWN", "SETSIG", "GETSIG", "GETLK64", 812 "SETLK64", "SETLKW64", "SETOWN_EX", "GETOWN_EX", 813 "GETOWNER_UIDS", 814 }; 815 static DEFINE_STRARRAY(fcntl_cmds, "F_"); 816 817 static const char *fcntl_linux_specific_cmds[] = { 818 "SETLEASE", "GETLEASE", "NOTIFY", "DUPFD_QUERY", [5] = "CANCELLK", "DUPFD_CLOEXEC", 819 "SETPIPE_SZ", "GETPIPE_SZ", "ADD_SEALS", "GET_SEALS", 820 "GET_RW_HINT", "SET_RW_HINT", "GET_FILE_RW_HINT", "SET_FILE_RW_HINT", 821 }; 822 823 static DEFINE_STRARRAY_OFFSET(fcntl_linux_specific_cmds, "F_", F_LINUX_SPECIFIC_BASE); 824 825 static struct strarray *fcntl_cmds_arrays[] = { 826 &strarray__fcntl_cmds, 827 &strarray__fcntl_linux_specific_cmds, 828 }; 829 830 static DEFINE_STRARRAYS(fcntl_cmds_arrays); 831 832 static const char *rlimit_resources[] = { 833 "CPU", "FSIZE", "DATA", "STACK", "CORE", "RSS", "NPROC", "NOFILE", 834 "MEMLOCK", "AS", "LOCKS", "SIGPENDING", "MSGQUEUE", "NICE", "RTPRIO", 835 "RTTIME", 836 }; 837 static DEFINE_STRARRAY(rlimit_resources, "RLIMIT_"); 838 839 static const char *sighow[] = { "BLOCK", "UNBLOCK", "SETMASK", }; 840 static DEFINE_STRARRAY(sighow, "SIG_"); 841 842 static const char *clockid[] = { 843 "REALTIME", "MONOTONIC", "PROCESS_CPUTIME_ID", "THREAD_CPUTIME_ID", 844 "MONOTONIC_RAW", "REALTIME_COARSE", "MONOTONIC_COARSE", "BOOTTIME", 845 "REALTIME_ALARM", "BOOTTIME_ALARM", "SGI_CYCLE", "TAI" 846 }; 847 static DEFINE_STRARRAY(clockid, "CLOCK_"); 848 849 static size_t syscall_arg__scnprintf_access_mode(char *bf, size_t size, 850 struct syscall_arg *arg) 851 { 852 bool show_prefix = arg->show_string_prefix; 853 const char *suffix = "_OK"; 854 size_t printed = 0; 855 int mode = arg->val; 856 857 if (mode == F_OK) /* 0 */ 858 return scnprintf(bf, size, "F%s", show_prefix ? suffix : ""); 859 #define P_MODE(n) \ 860 if (mode & n##_OK) { \ 861 printed += scnprintf(bf + printed, size - printed, "%s%s", #n, show_prefix ? suffix : ""); \ 862 mode &= ~n##_OK; \ 863 } 864 865 P_MODE(R); 866 P_MODE(W); 867 P_MODE(X); 868 #undef P_MODE 869 870 if (mode) 871 printed += scnprintf(bf + printed, size - printed, "|%#x", mode); 872 873 return printed; 874 } 875 876 #define SCA_ACCMODE syscall_arg__scnprintf_access_mode 877 878 static size_t syscall_arg__scnprintf_filename(char *bf, size_t size, 879 struct syscall_arg *arg); 880 881 #define SCA_FILENAME syscall_arg__scnprintf_filename 882 883 // 'argname' is just documentational at this point, to remove the previous comment with that info 884 #define SCA_FILENAME_FROM_USER(argname) \ 885 { .scnprintf = SCA_FILENAME, \ 886 .from_user = true, } 887 888 static size_t syscall_arg__scnprintf_buf(char *bf, size_t size, struct syscall_arg *arg); 889 890 #define SCA_BUF syscall_arg__scnprintf_buf 891 892 static size_t syscall_arg__scnprintf_pipe_flags(char *bf, size_t size, 893 struct syscall_arg *arg) 894 { 895 bool show_prefix = arg->show_string_prefix; 896 const char *prefix = "O_"; 897 int printed = 0, flags = arg->val; 898 899 #define P_FLAG(n) \ 900 if (flags & O_##n) { \ 901 printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", #n); \ 902 flags &= ~O_##n; \ 903 } 904 905 P_FLAG(CLOEXEC); 906 P_FLAG(NONBLOCK); 907 #undef P_FLAG 908 909 if (flags) 910 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags); 911 912 return printed; 913 } 914 915 #define SCA_PIPE_FLAGS syscall_arg__scnprintf_pipe_flags 916 917 #ifndef GRND_NONBLOCK 918 #define GRND_NONBLOCK 0x0001 919 #endif 920 #ifndef GRND_RANDOM 921 #define GRND_RANDOM 0x0002 922 #endif 923 924 static size_t syscall_arg__scnprintf_getrandom_flags(char *bf, size_t size, 925 struct syscall_arg *arg) 926 { 927 bool show_prefix = arg->show_string_prefix; 928 const char *prefix = "GRND_"; 929 int printed = 0, flags = arg->val; 930 931 #define P_FLAG(n) \ 932 if (flags & GRND_##n) { \ 933 printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", #n); \ 934 flags &= ~GRND_##n; \ 935 } 936 937 P_FLAG(RANDOM); 938 P_FLAG(NONBLOCK); 939 #undef P_FLAG 940 941 if (flags) 942 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags); 943 944 return printed; 945 } 946 947 #define SCA_GETRANDOM_FLAGS syscall_arg__scnprintf_getrandom_flags 948 949 #ifdef HAVE_LIBBPF_SUPPORT 950 static void syscall_arg_fmt__cache_btf_enum(struct syscall_arg_fmt *arg_fmt, struct btf *btf, char *type) 951 { 952 int id; 953 954 type = strstr(type, "enum "); 955 if (type == NULL) 956 return; 957 958 type += 5; // skip "enum " to get the enumeration name 959 960 id = btf__find_by_name(btf, type); 961 if (id < 0) 962 return; 963 964 arg_fmt->type = btf__type_by_id(btf, id); 965 } 966 967 static bool syscall_arg__strtoul_btf_enum(char *bf, size_t size, struct syscall_arg *arg, u64 *val) 968 { 969 const struct btf_type *bt = arg->fmt->type; 970 struct btf *btf = arg->trace->btf; 971 struct btf_enum *be = btf_enum(bt); 972 973 for (int i = 0; i < btf_vlen(bt); ++i, ++be) { 974 const char *name = btf__name_by_offset(btf, be->name_off); 975 int max_len = max(size, strlen(name)); 976 977 if (strncmp(name, bf, max_len) == 0) { 978 *val = be->val; 979 return true; 980 } 981 } 982 983 return false; 984 } 985 986 static bool syscall_arg__strtoul_btf_type(char *bf, size_t size, struct syscall_arg *arg, u64 *val) 987 { 988 const struct btf_type *bt; 989 char *type = arg->type_name; 990 struct btf *btf; 991 992 trace__load_vmlinux_btf(arg->trace); 993 994 btf = arg->trace->btf; 995 if (btf == NULL) 996 return false; 997 998 if (arg->fmt->type == NULL) { 999 // See if this is an enum 1000 syscall_arg_fmt__cache_btf_enum(arg->fmt, btf, type); 1001 } 1002 1003 // Now let's see if we have a BTF type resolved 1004 bt = arg->fmt->type; 1005 if (bt == NULL) 1006 return false; 1007 1008 // If it is an enum: 1009 if (btf_is_enum(arg->fmt->type)) 1010 return syscall_arg__strtoul_btf_enum(bf, size, arg, val); 1011 1012 return false; 1013 } 1014 1015 static size_t btf_enum_scnprintf(const struct btf_type *type, struct btf *btf, char *bf, size_t size, int val) 1016 { 1017 struct btf_enum *be = btf_enum(type); 1018 const int nr_entries = btf_vlen(type); 1019 1020 for (int i = 0; i < nr_entries; ++i, ++be) { 1021 if (be->val == val) { 1022 return scnprintf(bf, size, "%s", 1023 btf__name_by_offset(btf, be->name_off)); 1024 } 1025 } 1026 1027 return 0; 1028 } 1029 1030 struct trace_btf_dump_snprintf_ctx { 1031 char *bf; 1032 size_t printed, size; 1033 }; 1034 1035 static void trace__btf_dump_snprintf(void *vctx, const char *fmt, va_list args) 1036 { 1037 struct trace_btf_dump_snprintf_ctx *ctx = vctx; 1038 1039 ctx->printed += vscnprintf(ctx->bf + ctx->printed, ctx->size - ctx->printed, fmt, args); 1040 } 1041 1042 static size_t btf_struct_scnprintf(const struct btf_type *type, struct btf *btf, char *bf, size_t size, struct syscall_arg *arg) 1043 { 1044 struct trace_btf_dump_snprintf_ctx ctx = { 1045 .bf = bf, 1046 .size = size, 1047 }; 1048 struct augmented_arg *augmented_arg = arg->augmented.args; 1049 int type_id = arg->fmt->type_id, consumed; 1050 struct btf_dump *btf_dump; 1051 1052 LIBBPF_OPTS(btf_dump_opts, dump_opts); 1053 LIBBPF_OPTS(btf_dump_type_data_opts, dump_data_opts); 1054 1055 if (arg == NULL || arg->augmented.args == NULL) 1056 return 0; 1057 1058 dump_data_opts.compact = true; 1059 dump_data_opts.skip_names = !arg->trace->show_arg_names; 1060 1061 btf_dump = btf_dump__new(btf, trace__btf_dump_snprintf, &ctx, &dump_opts); 1062 if (btf_dump == NULL) 1063 return 0; 1064 1065 /* pretty print the struct data here */ 1066 if (btf_dump__dump_type_data(btf_dump, type_id, arg->augmented.args->value, type->size, &dump_data_opts) == 0) 1067 return 0; 1068 1069 consumed = sizeof(*augmented_arg) + augmented_arg->size; 1070 arg->augmented.args = ((void *)arg->augmented.args) + consumed; 1071 arg->augmented.size -= consumed; 1072 1073 btf_dump__free(btf_dump); 1074 1075 return ctx.printed; 1076 } 1077 1078 static size_t trace__btf_scnprintf(struct trace *trace, struct syscall_arg *arg, char *bf, 1079 size_t size, int val, char *type) 1080 { 1081 struct syscall_arg_fmt *arg_fmt = arg->fmt; 1082 1083 if (trace->btf == NULL) 1084 return 0; 1085 1086 if (arg_fmt->type == NULL) { 1087 // Check if this is an enum and if we have the BTF type for it. 1088 syscall_arg_fmt__cache_btf_enum(arg_fmt, trace->btf, type); 1089 } 1090 1091 // Did we manage to find a BTF type for the syscall/tracepoint argument? 1092 if (arg_fmt->type == NULL) 1093 return 0; 1094 1095 if (btf_is_enum(arg_fmt->type)) 1096 return btf_enum_scnprintf(arg_fmt->type, trace->btf, bf, size, val); 1097 else if (btf_is_struct(arg_fmt->type) || btf_is_union(arg_fmt->type)) 1098 return btf_struct_scnprintf(arg_fmt->type, trace->btf, bf, size, arg); 1099 1100 return 0; 1101 } 1102 1103 #else // HAVE_LIBBPF_SUPPORT 1104 static size_t trace__btf_scnprintf(struct trace *trace __maybe_unused, struct syscall_arg *arg __maybe_unused, 1105 char *bf __maybe_unused, size_t size __maybe_unused, int val __maybe_unused, 1106 char *type __maybe_unused) 1107 { 1108 return 0; 1109 } 1110 1111 static bool syscall_arg__strtoul_btf_type(char *bf __maybe_unused, size_t size __maybe_unused, 1112 struct syscall_arg *arg __maybe_unused, u64 *val __maybe_unused) 1113 { 1114 return false; 1115 } 1116 #endif // HAVE_LIBBPF_SUPPORT 1117 1118 #define STUL_BTF_TYPE syscall_arg__strtoul_btf_type 1119 1120 #define STRARRAY(name, array) \ 1121 { .scnprintf = SCA_STRARRAY, \ 1122 .strtoul = STUL_STRARRAY, \ 1123 .parm = &strarray__##array, \ 1124 .show_zero = true, } 1125 1126 #define STRARRAY_FLAGS(name, array) \ 1127 { .scnprintf = SCA_STRARRAY_FLAGS, \ 1128 .strtoul = STUL_STRARRAY_FLAGS, \ 1129 .parm = &strarray__##array, \ 1130 .show_zero = true, } 1131 1132 #include "trace/beauty/eventfd.c" 1133 #include "trace/beauty/futex_op.c" 1134 #include "trace/beauty/futex_val3.c" 1135 #include "trace/beauty/mmap.c" 1136 #include "trace/beauty/mode_t.c" 1137 #include "trace/beauty/msg_flags.c" 1138 #include "trace/beauty/open_flags.c" 1139 #include "trace/beauty/perf_event_open.c" 1140 #include "trace/beauty/pid.c" 1141 #include "trace/beauty/sched_policy.c" 1142 #include "trace/beauty/seccomp.c" 1143 #include "trace/beauty/signum.c" 1144 #include "trace/beauty/socket_type.c" 1145 #include "trace/beauty/waitid_options.c" 1146 1147 static const struct syscall_fmt syscall_fmts[] = { 1148 { .name = "access", 1149 .arg = { [1] = { .scnprintf = SCA_ACCMODE, /* mode */ }, }, }, 1150 { .name = "arch_prctl", 1151 .arg = { [0] = { .scnprintf = SCA_X86_ARCH_PRCTL_CODE, /* code */ }, 1152 [1] = { .scnprintf = SCA_PTR, /* arg2 */ }, }, }, 1153 { .name = "bind", 1154 .arg = { [0] = { .scnprintf = SCA_INT, /* fd */ }, 1155 [1] = SCA_SOCKADDR_FROM_USER(umyaddr), 1156 [2] = { .scnprintf = SCA_INT, /* addrlen */ }, }, }, 1157 { .name = "bpf", 1158 .arg = { [0] = STRARRAY(cmd, bpf_cmd), 1159 [1] = { .from_user = true /* attr */, }, } }, 1160 { .name = "brk", .hexret = true, 1161 .arg = { [0] = { .scnprintf = SCA_PTR, /* brk */ }, }, }, 1162 { .name = "clock_gettime", 1163 .arg = { [0] = STRARRAY(clk_id, clockid), }, }, 1164 { .name = "clock_nanosleep", 1165 .arg = { [2] = SCA_TIMESPEC_FROM_USER(req), }, }, 1166 { .name = "clone", .errpid = true, .nr_args = 5, 1167 .arg = { [0] = { .name = "flags", .scnprintf = SCA_CLONE_FLAGS, }, 1168 [1] = { .name = "child_stack", .scnprintf = SCA_HEX, }, 1169 [2] = { .name = "parent_tidptr", .scnprintf = SCA_HEX, }, 1170 [3] = { .name = "child_tidptr", .scnprintf = SCA_HEX, }, 1171 [4] = { .name = "tls", .scnprintf = SCA_HEX, }, }, }, 1172 { .name = "close", 1173 .arg = { [0] = { .scnprintf = SCA_CLOSE_FD, /* fd */ }, }, }, 1174 { .name = "connect", 1175 .arg = { [0] = { .scnprintf = SCA_INT, /* fd */ }, 1176 [1] = SCA_SOCKADDR_FROM_USER(servaddr), 1177 [2] = { .scnprintf = SCA_INT, /* addrlen */ }, }, }, 1178 { .name = "epoll_ctl", 1179 .arg = { [1] = STRARRAY(op, epoll_ctl_ops), }, }, 1180 { .name = "eventfd2", 1181 .arg = { [1] = { .scnprintf = SCA_EFD_FLAGS, /* flags */ }, }, }, 1182 { .name = "faccessat", 1183 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dirfd */ }, 1184 [1] = SCA_FILENAME_FROM_USER(pathname), 1185 [2] = { .scnprintf = SCA_ACCMODE, /* mode */ }, }, }, 1186 { .name = "faccessat2", 1187 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dirfd */ }, 1188 [1] = SCA_FILENAME_FROM_USER(pathname), 1189 [2] = { .scnprintf = SCA_ACCMODE, /* mode */ }, 1190 [3] = { .scnprintf = SCA_FACCESSAT2_FLAGS, /* flags */ }, }, }, 1191 { .name = "fchmodat", 1192 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, }, 1193 { .name = "fchownat", 1194 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, }, 1195 { .name = "fcntl", 1196 .arg = { [1] = { .scnprintf = SCA_FCNTL_CMD, /* cmd */ 1197 .strtoul = STUL_STRARRAYS, 1198 .parm = &strarrays__fcntl_cmds_arrays, 1199 .show_zero = true, }, 1200 [2] = { .scnprintf = SCA_FCNTL_ARG, /* arg */ }, }, }, 1201 { .name = "flock", 1202 .arg = { [1] = { .scnprintf = SCA_FLOCK, /* cmd */ }, }, }, 1203 { .name = "fsconfig", 1204 .arg = { [1] = STRARRAY(cmd, fsconfig_cmds), }, }, 1205 { .name = "fsmount", 1206 .arg = { [1] = STRARRAY_FLAGS(flags, fsmount_flags), 1207 [2] = { .scnprintf = SCA_FSMOUNT_ATTR_FLAGS, /* attr_flags */ }, }, }, 1208 { .name = "fspick", 1209 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, 1210 [1] = SCA_FILENAME_FROM_USER(path), 1211 [2] = { .scnprintf = SCA_FSPICK_FLAGS, /* flags */ }, }, }, 1212 { .name = "fstat", .alias = "newfstat", }, 1213 { .name = "futex", 1214 .arg = { [1] = { .scnprintf = SCA_FUTEX_OP, /* op */ }, 1215 [5] = { .scnprintf = SCA_FUTEX_VAL3, /* val3 */ }, }, }, 1216 { .name = "futimesat", 1217 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, }, 1218 { .name = "getitimer", 1219 .arg = { [0] = STRARRAY(which, itimers), }, }, 1220 { .name = "getpid", .errpid = true, }, 1221 { .name = "getpgid", .errpid = true, }, 1222 { .name = "getppid", .errpid = true, }, 1223 { .name = "getrandom", 1224 .arg = { [2] = { .scnprintf = SCA_GETRANDOM_FLAGS, /* flags */ }, }, }, 1225 { .name = "getrlimit", 1226 .arg = { [0] = STRARRAY(resource, rlimit_resources), }, }, 1227 { .name = "getsockopt", 1228 .arg = { [1] = STRARRAY(level, socket_level), }, }, 1229 { .name = "gettid", .errpid = true, }, 1230 { .name = "ioctl", 1231 .arg = { 1232 #if defined(__i386__) || defined(__x86_64__) 1233 /* 1234 * FIXME: Make this available to all arches. 1235 */ 1236 [1] = { .scnprintf = SCA_IOCTL_CMD, /* cmd */ }, 1237 [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, }, 1238 #else 1239 [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, }, 1240 #endif 1241 { .name = "kcmp", .nr_args = 5, 1242 .arg = { [0] = { .name = "pid1", .scnprintf = SCA_PID, }, 1243 [1] = { .name = "pid2", .scnprintf = SCA_PID, }, 1244 [2] = { .name = "type", .scnprintf = SCA_KCMP_TYPE, }, 1245 [3] = { .name = "idx1", .scnprintf = SCA_KCMP_IDX, }, 1246 [4] = { .name = "idx2", .scnprintf = SCA_KCMP_IDX, }, }, }, 1247 { .name = "keyctl", 1248 .arg = { [0] = STRARRAY(option, keyctl_options), }, }, 1249 { .name = "kill", 1250 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, }, 1251 { .name = "linkat", 1252 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, }, 1253 { .name = "lseek", 1254 .arg = { [2] = STRARRAY(whence, whences), }, }, 1255 { .name = "lstat", .alias = "newlstat", }, 1256 { .name = "madvise", 1257 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ }, 1258 [2] = { .scnprintf = SCA_MADV_BHV, /* behavior */ }, }, }, 1259 { .name = "mkdirat", 1260 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, }, 1261 { .name = "mknodat", 1262 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, }, 1263 { .name = "mmap", .hexret = true, 1264 /* The standard mmap maps to old_mmap on s390x */ 1265 #if defined(__s390x__) 1266 .alias = "old_mmap", 1267 #endif 1268 .arg = { [2] = { .scnprintf = SCA_MMAP_PROT, .show_zero = true, /* prot */ }, 1269 [3] = { .scnprintf = SCA_MMAP_FLAGS, /* flags */ 1270 .strtoul = STUL_STRARRAY_FLAGS, 1271 .parm = &strarray__mmap_flags, }, 1272 [5] = { .scnprintf = SCA_HEX, /* offset */ }, }, }, 1273 { .name = "mount", 1274 .arg = { [0] = SCA_FILENAME_FROM_USER(devname), 1275 [3] = { .scnprintf = SCA_MOUNT_FLAGS, /* flags */ 1276 .mask_val = SCAMV_MOUNT_FLAGS, /* flags */ }, }, }, 1277 { .name = "move_mount", 1278 .arg = { [0] = { .scnprintf = SCA_FDAT, /* from_dfd */ }, 1279 [1] = SCA_FILENAME_FROM_USER(pathname), 1280 [2] = { .scnprintf = SCA_FDAT, /* to_dfd */ }, 1281 [3] = SCA_FILENAME_FROM_USER(pathname), 1282 [4] = { .scnprintf = SCA_MOVE_MOUNT_FLAGS, /* flags */ }, }, }, 1283 { .name = "mprotect", 1284 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ }, 1285 [2] = { .scnprintf = SCA_MMAP_PROT, .show_zero = true, /* prot */ }, }, }, 1286 { .name = "mq_unlink", 1287 .arg = { [0] = SCA_FILENAME_FROM_USER(u_name), }, }, 1288 { .name = "mremap", .hexret = true, 1289 .arg = { [3] = { .scnprintf = SCA_MREMAP_FLAGS, /* flags */ }, }, }, 1290 { .name = "name_to_handle_at", 1291 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, }, 1292 { .name = "nanosleep", 1293 .arg = { [0] = SCA_TIMESPEC_FROM_USER(req), }, }, 1294 { .name = "newfstatat", .alias = "fstatat", 1295 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dirfd */ }, 1296 [1] = SCA_FILENAME_FROM_USER(pathname), 1297 [3] = { .scnprintf = SCA_FS_AT_FLAGS, /* flags */ }, }, }, 1298 { .name = "open", 1299 .arg = { [1] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, }, 1300 { .name = "open_by_handle_at", 1301 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, 1302 [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, }, 1303 { .name = "openat", 1304 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, 1305 [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, }, 1306 { .name = "perf_event_open", 1307 .arg = { [0] = SCA_PERF_ATTR_FROM_USER(attr), 1308 [2] = { .scnprintf = SCA_INT, /* cpu */ }, 1309 [3] = { .scnprintf = SCA_FD, /* group_fd */ }, 1310 [4] = { .scnprintf = SCA_PERF_FLAGS, /* flags */ }, }, }, 1311 { .name = "pipe2", 1312 .arg = { [1] = { .scnprintf = SCA_PIPE_FLAGS, /* flags */ }, }, }, 1313 { .name = "pkey_alloc", 1314 .arg = { [1] = { .scnprintf = SCA_PKEY_ALLOC_ACCESS_RIGHTS, /* access_rights */ }, }, }, 1315 { .name = "pkey_free", 1316 .arg = { [0] = { .scnprintf = SCA_INT, /* key */ }, }, }, 1317 { .name = "pkey_mprotect", 1318 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ }, 1319 [2] = { .scnprintf = SCA_MMAP_PROT, .show_zero = true, /* prot */ }, 1320 [3] = { .scnprintf = SCA_INT, /* pkey */ }, }, }, 1321 { .name = "poll", .timeout = true, }, 1322 { .name = "ppoll", .timeout = true, }, 1323 { .name = "prctl", 1324 .arg = { [0] = { .scnprintf = SCA_PRCTL_OPTION, /* option */ 1325 .strtoul = STUL_STRARRAY, 1326 .parm = &strarray__prctl_options, }, 1327 [1] = { .scnprintf = SCA_PRCTL_ARG2, /* arg2 */ }, 1328 [2] = { .scnprintf = SCA_PRCTL_ARG3, /* arg3 */ }, }, }, 1329 { .name = "pread", .alias = "pread64", }, 1330 { .name = "preadv", .alias = "pread", }, 1331 { .name = "prlimit64", 1332 .arg = { [1] = STRARRAY(resource, rlimit_resources), 1333 [2] = { .from_user = true /* new_rlim */, }, }, }, 1334 { .name = "pwrite", .alias = "pwrite64", }, 1335 { .name = "readlinkat", 1336 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, }, 1337 { .name = "recvfrom", 1338 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, }, 1339 { .name = "recvmmsg", 1340 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, }, 1341 { .name = "recvmsg", 1342 .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, }, 1343 { .name = "renameat", 1344 .arg = { [0] = { .scnprintf = SCA_FDAT, /* olddirfd */ }, 1345 [2] = { .scnprintf = SCA_FDAT, /* newdirfd */ }, }, }, 1346 { .name = "renameat2", 1347 .arg = { [0] = { .scnprintf = SCA_FDAT, /* olddirfd */ }, 1348 [2] = { .scnprintf = SCA_FDAT, /* newdirfd */ }, 1349 [4] = { .scnprintf = SCA_RENAMEAT2_FLAGS, /* flags */ }, }, }, 1350 { .name = "rseq", 1351 .arg = { [0] = { .from_user = true /* rseq */, }, }, }, 1352 { .name = "rt_sigaction", 1353 .arg = { [0] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, }, 1354 { .name = "rt_sigprocmask", 1355 .arg = { [0] = STRARRAY(how, sighow), }, }, 1356 { .name = "rt_sigqueueinfo", 1357 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, }, 1358 { .name = "rt_tgsigqueueinfo", 1359 .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, }, 1360 { .name = "sched_setscheduler", 1361 .arg = { [1] = { .scnprintf = SCA_SCHED_POLICY, /* policy */ }, }, }, 1362 { .name = "seccomp", 1363 .arg = { [0] = { .scnprintf = SCA_SECCOMP_OP, /* op */ }, 1364 [1] = { .scnprintf = SCA_SECCOMP_FLAGS, /* flags */ }, }, }, 1365 { .name = "select", .timeout = true, }, 1366 { .name = "sendfile", .alias = "sendfile64", }, 1367 { .name = "sendmmsg", 1368 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, }, 1369 { .name = "sendmsg", 1370 .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, }, 1371 { .name = "sendto", 1372 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, 1373 [4] = SCA_SOCKADDR_FROM_USER(addr), }, }, 1374 { .name = "set_robust_list", 1375 .arg = { [0] = { .from_user = true /* head */, }, }, }, 1376 { .name = "set_tid_address", .errpid = true, }, 1377 { .name = "setitimer", 1378 .arg = { [0] = STRARRAY(which, itimers), }, }, 1379 { .name = "setrlimit", 1380 .arg = { [0] = STRARRAY(resource, rlimit_resources), 1381 [1] = { .from_user = true /* rlim */, }, }, }, 1382 { .name = "setsockopt", 1383 .arg = { [1] = STRARRAY(level, socket_level), }, }, 1384 { .name = "socket", 1385 .arg = { [0] = STRARRAY(family, socket_families), 1386 [1] = { .scnprintf = SCA_SK_TYPE, /* type */ }, 1387 [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, }, 1388 { .name = "socketpair", 1389 .arg = { [0] = STRARRAY(family, socket_families), 1390 [1] = { .scnprintf = SCA_SK_TYPE, /* type */ }, 1391 [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, }, 1392 { .name = "stat", .alias = "newstat", }, 1393 { .name = "statx", 1394 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fdat */ }, 1395 [2] = { .scnprintf = SCA_FS_AT_FLAGS, /* flags */ } , 1396 [3] = { .scnprintf = SCA_STATX_MASK, /* mask */ }, }, }, 1397 { .name = "swapoff", 1398 .arg = { [0] = SCA_FILENAME_FROM_USER(specialfile), }, }, 1399 { .name = "swapon", 1400 .arg = { [0] = SCA_FILENAME_FROM_USER(specialfile), }, }, 1401 { .name = "symlinkat", 1402 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, }, 1403 { .name = "sync_file_range", 1404 .arg = { [3] = { .scnprintf = SCA_SYNC_FILE_RANGE_FLAGS, /* flags */ }, }, }, 1405 { .name = "tgkill", 1406 .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, }, 1407 { .name = "tkill", 1408 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, }, 1409 { .name = "umount2", .alias = "umount", 1410 .arg = { [0] = SCA_FILENAME_FROM_USER(name), }, }, 1411 { .name = "uname", .alias = "newuname", }, 1412 { .name = "unlinkat", 1413 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, 1414 [1] = SCA_FILENAME_FROM_USER(pathname), 1415 [2] = { .scnprintf = SCA_FS_AT_FLAGS, /* flags */ }, }, }, 1416 { .name = "utimensat", 1417 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dirfd */ }, }, }, 1418 { .name = "wait4", .errpid = true, 1419 .arg = { [2] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, }, 1420 { .name = "waitid", .errpid = true, 1421 .arg = { [3] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, }, 1422 { .name = "write", 1423 .arg = { [1] = { .scnprintf = SCA_BUF /* buf */, .from_user = true, }, }, }, 1424 }; 1425 1426 static int syscall_fmt__cmp(const void *name, const void *fmtp) 1427 { 1428 const struct syscall_fmt *fmt = fmtp; 1429 return strcmp(name, fmt->name); 1430 } 1431 1432 static const struct syscall_fmt *__syscall_fmt__find(const struct syscall_fmt *fmts, 1433 const int nmemb, 1434 const char *name) 1435 { 1436 return bsearch(name, fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp); 1437 } 1438 1439 static const struct syscall_fmt *syscall_fmt__find(const char *name) 1440 { 1441 const int nmemb = ARRAY_SIZE(syscall_fmts); 1442 return __syscall_fmt__find(syscall_fmts, nmemb, name); 1443 } 1444 1445 static const struct syscall_fmt *__syscall_fmt__find_by_alias(const struct syscall_fmt *fmts, 1446 const int nmemb, const char *alias) 1447 { 1448 int i; 1449 1450 for (i = 0; i < nmemb; ++i) { 1451 if (fmts[i].alias && strcmp(fmts[i].alias, alias) == 0) 1452 return &fmts[i]; 1453 } 1454 1455 return NULL; 1456 } 1457 1458 static const struct syscall_fmt *syscall_fmt__find_by_alias(const char *alias) 1459 { 1460 const int nmemb = ARRAY_SIZE(syscall_fmts); 1461 return __syscall_fmt__find_by_alias(syscall_fmts, nmemb, alias); 1462 } 1463 1464 /** 1465 * struct syscall 1466 */ 1467 struct syscall { 1468 /** @e_machine: The ELF machine associated with the entry. */ 1469 int e_machine; 1470 /** @id: id value from the tracepoint, the system call number. */ 1471 int id; 1472 struct tep_event *tp_format; 1473 int nr_args; 1474 /** 1475 * @args_size: sum of the sizes of the syscall arguments, anything 1476 * after that is augmented stuff: pathname for openat, etc. 1477 */ 1478 1479 int args_size; 1480 struct { 1481 struct bpf_program *sys_enter, 1482 *sys_exit; 1483 } bpf_prog; 1484 /** @is_exit: is this "exit" or "exit_group"? */ 1485 bool is_exit; 1486 /** 1487 * @is_open: is this "open" or "openat"? To associate the fd returned in 1488 * sys_exit with the pathname in sys_enter. 1489 */ 1490 bool is_open; 1491 /** 1492 * @nonexistent: Name lookup failed. Just a hole in the syscall table, 1493 * syscall id not allocated. 1494 */ 1495 bool nonexistent; 1496 bool use_btf; 1497 struct tep_format_field *args; 1498 const char *name; 1499 const struct syscall_fmt *fmt; 1500 struct syscall_arg_fmt *arg_fmt; 1501 }; 1502 1503 /* 1504 * We need to have this 'calculated' boolean because in some cases we really 1505 * don't know what is the duration of a syscall, for instance, when we start 1506 * a session and some threads are waiting for a syscall to finish, say 'poll', 1507 * in which case all we can do is to print "( ? ) for duration and for the 1508 * start timestamp. 1509 */ 1510 static size_t fprintf_duration(unsigned long t, bool calculated, FILE *fp) 1511 { 1512 double duration = (double)t / NSEC_PER_MSEC; 1513 size_t printed = fprintf(fp, "("); 1514 1515 if (!calculated) 1516 printed += fprintf(fp, " "); 1517 else if (duration >= 1.0) 1518 printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration); 1519 else if (duration >= 0.01) 1520 printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration); 1521 else 1522 printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration); 1523 return printed + fprintf(fp, "): "); 1524 } 1525 1526 /** 1527 * filename.ptr: The filename char pointer that will be vfs_getname'd 1528 * filename.entry_str_pos: Where to insert the string translated from 1529 * filename.ptr by the vfs_getname tracepoint/kprobe. 1530 * ret_scnprintf: syscall args may set this to a different syscall return 1531 * formatter, for instance, fcntl may return fds, file flags, etc. 1532 */ 1533 struct thread_trace { 1534 u64 entry_time; 1535 bool entry_pending; 1536 unsigned long nr_events; 1537 unsigned long pfmaj, pfmin; 1538 char *entry_str; 1539 double runtime_ms; 1540 size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg); 1541 struct { 1542 unsigned long ptr; 1543 short int entry_str_pos; 1544 bool pending_open; 1545 unsigned int namelen; 1546 char *name; 1547 } filename; 1548 struct { 1549 int max; 1550 struct file *table; 1551 } files; 1552 1553 struct hashmap *syscall_stats; 1554 }; 1555 1556 static size_t syscall_id_hash(long key, void *ctx __maybe_unused) 1557 { 1558 return key; 1559 } 1560 1561 static bool syscall_id_equal(long key1, long key2, void *ctx __maybe_unused) 1562 { 1563 return key1 == key2; 1564 } 1565 1566 static struct hashmap *alloc_syscall_stats(void) 1567 { 1568 return hashmap__new(syscall_id_hash, syscall_id_equal, NULL); 1569 } 1570 1571 static void delete_syscall_stats(struct hashmap *syscall_stats) 1572 { 1573 struct hashmap_entry *pos; 1574 size_t bkt; 1575 1576 if (syscall_stats == NULL) 1577 return; 1578 1579 hashmap__for_each_entry(syscall_stats, pos, bkt) 1580 zfree(&pos->pvalue); 1581 hashmap__free(syscall_stats); 1582 } 1583 1584 static struct thread_trace *thread_trace__new(struct trace *trace) 1585 { 1586 struct thread_trace *ttrace = zalloc(sizeof(struct thread_trace)); 1587 1588 if (ttrace) { 1589 ttrace->files.max = -1; 1590 if (trace->summary) { 1591 ttrace->syscall_stats = alloc_syscall_stats(); 1592 if (IS_ERR(ttrace->syscall_stats)) 1593 zfree(&ttrace); 1594 } 1595 } 1596 1597 return ttrace; 1598 } 1599 1600 static void thread_trace__free_files(struct thread_trace *ttrace); 1601 1602 static void thread_trace__delete(void *pttrace) 1603 { 1604 struct thread_trace *ttrace = pttrace; 1605 1606 if (!ttrace) 1607 return; 1608 1609 delete_syscall_stats(ttrace->syscall_stats); 1610 ttrace->syscall_stats = NULL; 1611 thread_trace__free_files(ttrace); 1612 zfree(&ttrace->entry_str); 1613 free(ttrace); 1614 } 1615 1616 static struct thread_trace *thread__trace(struct thread *thread, struct trace *trace) 1617 { 1618 struct thread_trace *ttrace; 1619 1620 if (thread == NULL) 1621 goto fail; 1622 1623 if (thread__priv(thread) == NULL) 1624 thread__set_priv(thread, thread_trace__new(trace)); 1625 1626 if (thread__priv(thread) == NULL) 1627 goto fail; 1628 1629 ttrace = thread__priv(thread); 1630 ++ttrace->nr_events; 1631 1632 return ttrace; 1633 fail: 1634 color_fprintf(trace->output, PERF_COLOR_RED, 1635 "WARNING: not enough memory, dropping samples!\n"); 1636 return NULL; 1637 } 1638 1639 1640 void syscall_arg__set_ret_scnprintf(struct syscall_arg *arg, 1641 size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg)) 1642 { 1643 struct thread_trace *ttrace = thread__priv(arg->thread); 1644 1645 ttrace->ret_scnprintf = ret_scnprintf; 1646 } 1647 1648 #define TRACE_PFMAJ (1 << 0) 1649 #define TRACE_PFMIN (1 << 1) 1650 1651 static const size_t trace__entry_str_size = 2048; 1652 1653 static void thread_trace__free_files(struct thread_trace *ttrace) 1654 { 1655 for (int i = 0; i <= ttrace->files.max; ++i) { 1656 struct file *file = ttrace->files.table + i; 1657 zfree(&file->pathname); 1658 } 1659 1660 zfree(&ttrace->files.table); 1661 ttrace->files.max = -1; 1662 } 1663 1664 static struct file *thread_trace__files_entry(struct thread_trace *ttrace, int fd) 1665 { 1666 if (fd < 0) 1667 return NULL; 1668 1669 if (fd > ttrace->files.max) { 1670 struct file *nfiles = realloc(ttrace->files.table, (fd + 1) * sizeof(struct file)); 1671 1672 if (nfiles == NULL) 1673 return NULL; 1674 1675 if (ttrace->files.max != -1) { 1676 memset(nfiles + ttrace->files.max + 1, 0, 1677 (fd - ttrace->files.max) * sizeof(struct file)); 1678 } else { 1679 memset(nfiles, 0, (fd + 1) * sizeof(struct file)); 1680 } 1681 1682 ttrace->files.table = nfiles; 1683 ttrace->files.max = fd; 1684 } 1685 1686 return ttrace->files.table + fd; 1687 } 1688 1689 struct file *thread__files_entry(struct thread *thread, int fd) 1690 { 1691 return thread_trace__files_entry(thread__priv(thread), fd); 1692 } 1693 1694 static int trace__set_fd_pathname(struct thread *thread, int fd, const char *pathname) 1695 { 1696 struct thread_trace *ttrace = thread__priv(thread); 1697 struct file *file = thread_trace__files_entry(ttrace, fd); 1698 1699 if (file != NULL) { 1700 struct stat st; 1701 1702 if (stat(pathname, &st) == 0) 1703 file->dev_maj = major(st.st_rdev); 1704 file->pathname = strdup(pathname); 1705 if (file->pathname) 1706 return 0; 1707 } 1708 1709 return -1; 1710 } 1711 1712 static int thread__read_fd_path(struct thread *thread, int fd) 1713 { 1714 char linkname[PATH_MAX], pathname[PATH_MAX]; 1715 struct stat st; 1716 int ret; 1717 1718 if (thread__pid(thread) == thread__tid(thread)) { 1719 scnprintf(linkname, sizeof(linkname), 1720 "/proc/%d/fd/%d", thread__pid(thread), fd); 1721 } else { 1722 scnprintf(linkname, sizeof(linkname), 1723 "/proc/%d/task/%d/fd/%d", 1724 thread__pid(thread), thread__tid(thread), fd); 1725 } 1726 1727 if (lstat(linkname, &st) < 0 || st.st_size + 1 > (off_t)sizeof(pathname)) 1728 return -1; 1729 1730 ret = readlink(linkname, pathname, sizeof(pathname)); 1731 1732 if (ret < 0 || ret > st.st_size) 1733 return -1; 1734 1735 pathname[ret] = '\0'; 1736 return trace__set_fd_pathname(thread, fd, pathname); 1737 } 1738 1739 static const char *thread__fd_path(struct thread *thread, int fd, 1740 struct trace *trace) 1741 { 1742 struct thread_trace *ttrace = thread__priv(thread); 1743 1744 if (ttrace == NULL || trace->fd_path_disabled) 1745 return NULL; 1746 1747 if (fd < 0) 1748 return NULL; 1749 1750 if ((fd > ttrace->files.max || ttrace->files.table[fd].pathname == NULL)) { 1751 if (!trace->live) 1752 return NULL; 1753 ++trace->stats.proc_getname; 1754 if (thread__read_fd_path(thread, fd)) 1755 return NULL; 1756 } 1757 1758 return ttrace->files.table[fd].pathname; 1759 } 1760 1761 size_t syscall_arg__scnprintf_fd(char *bf, size_t size, struct syscall_arg *arg) 1762 { 1763 int fd = arg->val; 1764 size_t printed = scnprintf(bf, size, "%d", fd); 1765 const char *path = thread__fd_path(arg->thread, fd, arg->trace); 1766 1767 if (path) 1768 printed += scnprintf(bf + printed, size - printed, "<%s>", path); 1769 1770 return printed; 1771 } 1772 1773 size_t pid__scnprintf_fd(struct trace *trace, pid_t pid, int fd, char *bf, size_t size) 1774 { 1775 size_t printed = scnprintf(bf, size, "%d", fd); 1776 struct thread *thread = machine__find_thread(trace->host, pid, pid); 1777 1778 if (thread) { 1779 const char *path = thread__fd_path(thread, fd, trace); 1780 1781 if (path) 1782 printed += scnprintf(bf + printed, size - printed, "<%s>", path); 1783 1784 thread__put(thread); 1785 } 1786 1787 return printed; 1788 } 1789 1790 static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size, 1791 struct syscall_arg *arg) 1792 { 1793 int fd = arg->val; 1794 size_t printed = syscall_arg__scnprintf_fd(bf, size, arg); 1795 struct thread_trace *ttrace = thread__priv(arg->thread); 1796 1797 if (ttrace && fd >= 0 && fd <= ttrace->files.max) 1798 zfree(&ttrace->files.table[fd].pathname); 1799 1800 return printed; 1801 } 1802 1803 static void thread__set_filename_pos(struct thread *thread, const char *bf, 1804 unsigned long ptr) 1805 { 1806 struct thread_trace *ttrace = thread__priv(thread); 1807 1808 ttrace->filename.ptr = ptr; 1809 ttrace->filename.entry_str_pos = bf - ttrace->entry_str; 1810 } 1811 1812 static size_t syscall_arg__scnprintf_augmented_string(struct syscall_arg *arg, char *bf, size_t size) 1813 { 1814 struct augmented_arg *augmented_arg = arg->augmented.args; 1815 size_t printed = scnprintf(bf, size, "\"%.*s\"", augmented_arg->size, augmented_arg->value); 1816 /* 1817 * So that the next arg with a payload can consume its augmented arg, i.e. for rename* syscalls 1818 * we would have two strings, each prefixed by its size. 1819 */ 1820 int consumed = sizeof(*augmented_arg) + augmented_arg->size; 1821 1822 arg->augmented.args = ((void *)arg->augmented.args) + consumed; 1823 arg->augmented.size -= consumed; 1824 1825 return printed; 1826 } 1827 1828 static size_t syscall_arg__scnprintf_filename(char *bf, size_t size, 1829 struct syscall_arg *arg) 1830 { 1831 unsigned long ptr = arg->val; 1832 1833 if (arg->augmented.args) 1834 return syscall_arg__scnprintf_augmented_string(arg, bf, size); 1835 1836 if (!arg->trace->vfs_getname) 1837 return scnprintf(bf, size, "%#x", ptr); 1838 1839 thread__set_filename_pos(arg->thread, bf, ptr); 1840 return 0; 1841 } 1842 1843 #define MAX_CONTROL_CHAR 31 1844 #define MAX_ASCII 127 1845 1846 static size_t syscall_arg__scnprintf_buf(char *bf, size_t size, struct syscall_arg *arg) 1847 { 1848 struct augmented_arg *augmented_arg = arg->augmented.args; 1849 unsigned char *orig = (unsigned char *)augmented_arg->value; 1850 size_t printed = 0; 1851 int consumed; 1852 1853 if (augmented_arg == NULL) 1854 return 0; 1855 1856 for (int j = 0; j < augmented_arg->size; ++j) { 1857 bool control_char = orig[j] <= MAX_CONTROL_CHAR || orig[j] >= MAX_ASCII; 1858 /* print control characters (0~31 and 127), and non-ascii characters in \(digits) */ 1859 printed += scnprintf(bf + printed, size - printed, control_char ? "\\%d" : "%c", (int)orig[j]); 1860 } 1861 1862 consumed = sizeof(*augmented_arg) + augmented_arg->size; 1863 arg->augmented.args = ((void *)arg->augmented.args) + consumed; 1864 arg->augmented.size -= consumed; 1865 1866 return printed; 1867 } 1868 1869 static bool trace__filter_duration(struct trace *trace, double t) 1870 { 1871 return t < (trace->duration_filter * NSEC_PER_MSEC); 1872 } 1873 1874 static size_t __trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp) 1875 { 1876 double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC; 1877 1878 return fprintf(fp, "%10.3f ", ts); 1879 } 1880 1881 /* 1882 * We're handling tstamp=0 as an undefined tstamp, i.e. like when we are 1883 * using ttrace->entry_time for a thread that receives a sys_exit without 1884 * first having received a sys_enter ("poll" issued before tracing session 1885 * starts, lost sys_enter exit due to ring buffer overflow). 1886 */ 1887 static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp) 1888 { 1889 if (tstamp > 0) 1890 return __trace__fprintf_tstamp(trace, tstamp, fp); 1891 1892 return fprintf(fp, " ? "); 1893 } 1894 1895 static pid_t workload_pid = -1; 1896 static volatile sig_atomic_t done = false; 1897 static volatile sig_atomic_t interrupted = false; 1898 1899 static void sighandler_interrupt(int sig __maybe_unused) 1900 { 1901 done = interrupted = true; 1902 } 1903 1904 static void sighandler_chld(int sig __maybe_unused, siginfo_t *info, 1905 void *context __maybe_unused) 1906 { 1907 if (info->si_pid == workload_pid) 1908 done = true; 1909 } 1910 1911 static size_t trace__fprintf_comm_tid(struct trace *trace, struct thread *thread, FILE *fp) 1912 { 1913 size_t printed = 0; 1914 1915 if (trace->multiple_threads) { 1916 if (trace->show_comm) 1917 printed += fprintf(fp, "%.14s/", thread__comm_str(thread)); 1918 printed += fprintf(fp, "%d ", thread__tid(thread)); 1919 } 1920 1921 return printed; 1922 } 1923 1924 static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread, 1925 u64 duration, bool duration_calculated, u64 tstamp, FILE *fp) 1926 { 1927 size_t printed = 0; 1928 1929 if (trace->show_tstamp) 1930 printed = trace__fprintf_tstamp(trace, tstamp, fp); 1931 if (trace->show_duration) 1932 printed += fprintf_duration(duration, duration_calculated, fp); 1933 return printed + trace__fprintf_comm_tid(trace, thread, fp); 1934 } 1935 1936 static int trace__process_event(struct trace *trace, struct machine *machine, 1937 union perf_event *event, struct perf_sample *sample) 1938 { 1939 int ret = 0; 1940 1941 switch (event->header.type) { 1942 case PERF_RECORD_LOST: 1943 color_fprintf(trace->output, PERF_COLOR_RED, 1944 "LOST %" PRIu64 " events!\n", (u64)event->lost.lost); 1945 ret = machine__process_lost_event(machine, event, sample); 1946 break; 1947 default: 1948 ret = machine__process_event(machine, event, sample); 1949 break; 1950 } 1951 1952 return ret; 1953 } 1954 1955 static int trace__tool_process(const struct perf_tool *tool, 1956 union perf_event *event, 1957 struct perf_sample *sample, 1958 struct machine *machine) 1959 { 1960 struct trace *trace = container_of(tool, struct trace, tool); 1961 return trace__process_event(trace, machine, event, sample); 1962 } 1963 1964 static char *trace__machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp) 1965 { 1966 struct machine *machine = vmachine; 1967 1968 if (machine->kptr_restrict_warned) 1969 return NULL; 1970 1971 if (symbol_conf.kptr_restrict) { 1972 pr_warning("Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n" 1973 "Check /proc/sys/kernel/kptr_restrict and /proc/sys/kernel/perf_event_paranoid.\n\n" 1974 "Kernel samples will not be resolved.\n"); 1975 machine->kptr_restrict_warned = true; 1976 return NULL; 1977 } 1978 1979 return machine__resolve_kernel_addr(vmachine, addrp, modp); 1980 } 1981 1982 static int trace__symbols_init(struct trace *trace, int argc, const char **argv, 1983 struct evlist *evlist) 1984 { 1985 int err = symbol__init(NULL); 1986 1987 if (err) 1988 return err; 1989 1990 perf_env__init(&trace->host_env); 1991 err = perf_env__set_cmdline(&trace->host_env, argc, argv); 1992 if (err) 1993 goto out; 1994 1995 trace->host = machine__new_host(&trace->host_env); 1996 if (trace->host == NULL) { 1997 err = -ENOMEM; 1998 goto out; 1999 } 2000 thread__set_priv_destructor(thread_trace__delete); 2001 2002 err = trace_event__register_resolver(trace->host, trace__machine__resolve_kernel_addr); 2003 if (err < 0) 2004 goto out; 2005 2006 err = __machine__synthesize_threads(trace->host, &trace->tool, &trace->opts.target, 2007 evlist->core.threads, trace__tool_process, 2008 /*needs_mmap=*/callchain_param.enabled, 2009 /*mmap_data=*/false, 2010 /*nr_threads_synthesize=*/1); 2011 out: 2012 if (err) { 2013 perf_env__exit(&trace->host_env); 2014 symbol__exit(); 2015 } 2016 return err; 2017 } 2018 2019 static void trace__symbols__exit(struct trace *trace) 2020 { 2021 machine__exit(trace->host); 2022 trace->host = NULL; 2023 2024 perf_env__exit(&trace->host_env); 2025 symbol__exit(); 2026 } 2027 2028 static int syscall__alloc_arg_fmts(struct syscall *sc, int nr_args) 2029 { 2030 int idx; 2031 2032 if (nr_args == RAW_SYSCALL_ARGS_NUM && sc->fmt && sc->fmt->nr_args != 0) 2033 nr_args = sc->fmt->nr_args; 2034 2035 sc->arg_fmt = calloc(nr_args, sizeof(*sc->arg_fmt)); 2036 if (sc->arg_fmt == NULL) 2037 return -1; 2038 2039 for (idx = 0; idx < nr_args; ++idx) { 2040 if (sc->fmt) 2041 sc->arg_fmt[idx] = sc->fmt->arg[idx]; 2042 } 2043 2044 sc->nr_args = nr_args; 2045 return 0; 2046 } 2047 2048 static const struct syscall_arg_fmt syscall_arg_fmts__by_name[] = { 2049 { .name = "msr", .scnprintf = SCA_X86_MSR, .strtoul = STUL_X86_MSR, }, 2050 { .name = "vector", .scnprintf = SCA_X86_IRQ_VECTORS, .strtoul = STUL_X86_IRQ_VECTORS, }, 2051 }; 2052 2053 static int syscall_arg_fmt__cmp(const void *name, const void *fmtp) 2054 { 2055 const struct syscall_arg_fmt *fmt = fmtp; 2056 return strcmp(name, fmt->name); 2057 } 2058 2059 static const struct syscall_arg_fmt * 2060 __syscall_arg_fmt__find_by_name(const struct syscall_arg_fmt *fmts, const int nmemb, 2061 const char *name) 2062 { 2063 return bsearch(name, fmts, nmemb, sizeof(struct syscall_arg_fmt), syscall_arg_fmt__cmp); 2064 } 2065 2066 static const struct syscall_arg_fmt *syscall_arg_fmt__find_by_name(const char *name) 2067 { 2068 const int nmemb = ARRAY_SIZE(syscall_arg_fmts__by_name); 2069 return __syscall_arg_fmt__find_by_name(syscall_arg_fmts__by_name, nmemb, name); 2070 } 2071 2072 /* 2073 * v6.19 kernel added new fields to read userspace memory for event tracing. 2074 * But it's not used by perf and confuses the syscall parameters. 2075 */ 2076 static bool is_internal_field(struct tep_format_field *field) 2077 { 2078 return !strcmp(field->type, "__data_loc char[]"); 2079 } 2080 2081 static struct tep_format_field * 2082 syscall_arg_fmt__init_array(struct syscall_arg_fmt *arg, struct tep_format_field *field, 2083 bool *use_btf) 2084 { 2085 struct tep_format_field *last_field = NULL; 2086 int len; 2087 2088 for (; field; field = field->next, ++arg) { 2089 /* assume it's the last argument */ 2090 if (is_internal_field(field)) 2091 continue; 2092 2093 last_field = field; 2094 2095 if (arg->scnprintf) 2096 continue; 2097 2098 len = strlen(field->name); 2099 2100 // As far as heuristics (or intention) goes this seems to hold true, and makes sense! 2101 if ((field->flags & TEP_FIELD_IS_POINTER) && strstarts(field->type, "const ")) 2102 arg->from_user = true; 2103 2104 if (strcmp(field->type, "const char *") == 0 && 2105 ((len >= 4 && strcmp(field->name + len - 4, "name") == 0) || 2106 strstr(field->name, "path") != NULL)) { 2107 arg->scnprintf = SCA_FILENAME; 2108 } else if ((field->flags & TEP_FIELD_IS_POINTER) || strstr(field->name, "addr")) 2109 arg->scnprintf = SCA_PTR; 2110 else if (strcmp(field->type, "pid_t") == 0) 2111 arg->scnprintf = SCA_PID; 2112 else if (strcmp(field->type, "umode_t") == 0) 2113 arg->scnprintf = SCA_MODE_T; 2114 else if ((field->flags & TEP_FIELD_IS_ARRAY) && strstr(field->type, "char")) { 2115 arg->scnprintf = SCA_CHAR_ARRAY; 2116 arg->nr_entries = field->arraylen; 2117 } else if ((strcmp(field->type, "int") == 0 || 2118 strcmp(field->type, "unsigned int") == 0 || 2119 strcmp(field->type, "long") == 0) && 2120 len >= 2 && strcmp(field->name + len - 2, "fd") == 0) { 2121 /* 2122 * /sys/kernel/tracing/events/syscalls/sys_enter* 2123 * grep -E 'field:.*fd;' .../format|sed -r 's/.*field:([a-z ]+) [a-z_]*fd.+/\1/g'|sort|uniq -c 2124 * 65 int 2125 * 23 unsigned int 2126 * 7 unsigned long 2127 */ 2128 arg->scnprintf = SCA_FD; 2129 } else if (strstr(field->type, "enum") && use_btf != NULL) { 2130 *use_btf = true; 2131 arg->strtoul = STUL_BTF_TYPE; 2132 } else { 2133 const struct syscall_arg_fmt *fmt = 2134 syscall_arg_fmt__find_by_name(field->name); 2135 2136 if (fmt) { 2137 arg->scnprintf = fmt->scnprintf; 2138 arg->strtoul = fmt->strtoul; 2139 } 2140 } 2141 } 2142 2143 return last_field; 2144 } 2145 2146 static int syscall__set_arg_fmts(struct syscall *sc) 2147 { 2148 struct tep_format_field *last_field = syscall_arg_fmt__init_array(sc->arg_fmt, sc->args, 2149 &sc->use_btf); 2150 2151 if (last_field) 2152 sc->args_size = last_field->offset + last_field->size; 2153 2154 return 0; 2155 } 2156 2157 static int syscall__read_info(struct syscall *sc, struct trace *trace) 2158 { 2159 char tp_name[128]; 2160 const char *name; 2161 struct tep_format_field *field; 2162 int err; 2163 2164 if (sc->nonexistent) 2165 return -EEXIST; 2166 2167 if (sc->name) { 2168 /* Info already read. */ 2169 return 0; 2170 } 2171 2172 name = syscalltbl__name(sc->e_machine, sc->id); 2173 if (name == NULL) { 2174 sc->nonexistent = true; 2175 return -EEXIST; 2176 } 2177 2178 sc->name = name; 2179 sc->fmt = syscall_fmt__find(sc->name); 2180 2181 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name); 2182 sc->tp_format = trace_event__tp_format("syscalls", tp_name); 2183 2184 if (IS_ERR(sc->tp_format) && sc->fmt && sc->fmt->alias) { 2185 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias); 2186 sc->tp_format = trace_event__tp_format("syscalls", tp_name); 2187 } 2188 2189 /* 2190 * Fails to read trace point format via sysfs node, so the trace point 2191 * doesn't exist. Set the 'nonexistent' flag as true. 2192 */ 2193 if (IS_ERR(sc->tp_format)) { 2194 sc->nonexistent = true; 2195 err = PTR_ERR(sc->tp_format); 2196 sc->tp_format = NULL; 2197 return err; 2198 } 2199 2200 /* 2201 * The tracepoint format contains __syscall_nr field, so it's one more 2202 * than the actual number of syscall arguments. 2203 */ 2204 if (syscall__alloc_arg_fmts(sc, sc->tp_format->format.nr_fields - 1)) 2205 return -ENOMEM; 2206 2207 sc->args = sc->tp_format->format.fields; 2208 /* 2209 * We need to check and discard the first variable '__syscall_nr' 2210 * or 'nr' that mean the syscall number. It is needless here. 2211 * So drop '__syscall_nr' or 'nr' field but does not exist on older kernels. 2212 */ 2213 if (sc->args && (!strcmp(sc->args->name, "__syscall_nr") || !strcmp(sc->args->name, "nr"))) { 2214 sc->args = sc->args->next; 2215 --sc->nr_args; 2216 } 2217 2218 field = sc->args; 2219 while (field) { 2220 if (is_internal_field(field)) 2221 --sc->nr_args; 2222 field = field->next; 2223 } 2224 2225 sc->is_exit = !strcmp(name, "exit_group") || !strcmp(name, "exit"); 2226 sc->is_open = !strcmp(name, "open") || !strcmp(name, "openat"); 2227 2228 err = syscall__set_arg_fmts(sc); 2229 2230 /* after calling syscall__set_arg_fmts() we'll know whether use_btf is true */ 2231 if (sc->use_btf) 2232 trace__load_vmlinux_btf(trace); 2233 2234 return err; 2235 } 2236 2237 static int evsel__init_tp_arg_scnprintf(struct evsel *evsel, bool *use_btf) 2238 { 2239 struct syscall_arg_fmt *fmt = evsel__syscall_arg_fmt(evsel); 2240 2241 if (fmt != NULL) { 2242 const struct tep_event *tp_format = evsel__tp_format(evsel); 2243 2244 if (tp_format) { 2245 syscall_arg_fmt__init_array(fmt, tp_format->format.fields, use_btf); 2246 return 0; 2247 } 2248 } 2249 2250 return -ENOMEM; 2251 } 2252 2253 static int intcmp(const void *a, const void *b) 2254 { 2255 const int *one = a, *another = b; 2256 2257 return *one - *another; 2258 } 2259 2260 static int trace__validate_ev_qualifier(struct trace *trace) 2261 { 2262 int err = 0; 2263 bool printed_invalid_prefix = false; 2264 struct str_node *pos; 2265 size_t nr_used = 0, nr_allocated = strlist__nr_entries(trace->ev_qualifier); 2266 2267 trace->ev_qualifier_ids.entries = malloc(nr_allocated * 2268 sizeof(trace->ev_qualifier_ids.entries[0])); 2269 2270 if (trace->ev_qualifier_ids.entries == NULL) { 2271 fputs("Error:\tNot enough memory for allocating events qualifier ids\n", 2272 trace->output); 2273 err = -EINVAL; 2274 goto out; 2275 } 2276 2277 strlist__for_each_entry(pos, trace->ev_qualifier) { 2278 const char *sc = pos->s; 2279 /* 2280 * TODO: Assume more than the validation/warnings are all for 2281 * the same binary type as perf. 2282 */ 2283 int id = syscalltbl__id(EM_HOST, sc), match_next = -1; 2284 2285 if (id < 0) { 2286 id = syscalltbl__strglobmatch_first(EM_HOST, sc, &match_next); 2287 if (id >= 0) 2288 goto matches; 2289 2290 if (!printed_invalid_prefix) { 2291 pr_debug("Skipping unknown syscalls: "); 2292 printed_invalid_prefix = true; 2293 } else { 2294 pr_debug(", "); 2295 } 2296 2297 pr_debug("%s", sc); 2298 continue; 2299 } 2300 matches: 2301 trace->ev_qualifier_ids.entries[nr_used++] = id; 2302 if (match_next == -1) 2303 continue; 2304 2305 while (1) { 2306 id = syscalltbl__strglobmatch_next(EM_HOST, sc, &match_next); 2307 if (id < 0) 2308 break; 2309 if (nr_allocated == nr_used) { 2310 void *entries; 2311 2312 nr_allocated += 8; 2313 entries = realloc(trace->ev_qualifier_ids.entries, 2314 nr_allocated * sizeof(trace->ev_qualifier_ids.entries[0])); 2315 if (entries == NULL) { 2316 err = -ENOMEM; 2317 fputs("\nError:\t Not enough memory for parsing\n", trace->output); 2318 goto out_free; 2319 } 2320 trace->ev_qualifier_ids.entries = entries; 2321 } 2322 trace->ev_qualifier_ids.entries[nr_used++] = id; 2323 } 2324 } 2325 2326 trace->ev_qualifier_ids.nr = nr_used; 2327 qsort(trace->ev_qualifier_ids.entries, nr_used, sizeof(int), intcmp); 2328 out: 2329 if (printed_invalid_prefix) 2330 pr_debug("\n"); 2331 return err; 2332 out_free: 2333 zfree(&trace->ev_qualifier_ids.entries); 2334 trace->ev_qualifier_ids.nr = 0; 2335 goto out; 2336 } 2337 2338 static __maybe_unused bool trace__syscall_enabled(struct trace *trace, int id) 2339 { 2340 bool in_ev_qualifier; 2341 2342 if (trace->ev_qualifier_ids.nr == 0) 2343 return true; 2344 2345 in_ev_qualifier = bsearch(&id, trace->ev_qualifier_ids.entries, 2346 trace->ev_qualifier_ids.nr, sizeof(int), intcmp) != NULL; 2347 2348 if (in_ev_qualifier) 2349 return !trace->not_ev_qualifier; 2350 2351 return trace->not_ev_qualifier; 2352 } 2353 2354 /* 2355 * args is to be interpreted as a series of longs but we need to handle 2356 * 8-byte unaligned accesses. args points to raw_data within the event 2357 * and raw_data is guaranteed to be 8-byte unaligned because it is 2358 * preceded by raw_size which is a u32. So we need to copy args to a temp 2359 * variable to read it. Most notably this avoids extended load instructions 2360 * on unaligned addresses 2361 */ 2362 unsigned long syscall_arg__val(struct syscall_arg *arg, u8 idx) 2363 { 2364 unsigned long val; 2365 unsigned char *p = arg->args + sizeof(unsigned long) * idx; 2366 2367 memcpy(&val, p, sizeof(val)); 2368 return val; 2369 } 2370 2371 static size_t syscall__scnprintf_name(struct syscall *sc, char *bf, size_t size, 2372 struct syscall_arg *arg) 2373 { 2374 if (sc->arg_fmt && sc->arg_fmt[arg->idx].name) 2375 return scnprintf(bf, size, "%s: ", sc->arg_fmt[arg->idx].name); 2376 2377 return scnprintf(bf, size, "arg%d: ", arg->idx); 2378 } 2379 2380 /* 2381 * Check if the value is in fact zero, i.e. mask whatever needs masking, such 2382 * as mount 'flags' argument that needs ignoring some magic flag, see comment 2383 * in tools/perf/trace/beauty/mount_flags.c 2384 */ 2385 static unsigned long syscall_arg_fmt__mask_val(struct syscall_arg_fmt *fmt, struct syscall_arg *arg, unsigned long val) 2386 { 2387 if (fmt && fmt->mask_val) 2388 return fmt->mask_val(arg, val); 2389 2390 return val; 2391 } 2392 2393 static size_t syscall_arg_fmt__scnprintf_val(struct syscall_arg_fmt *fmt, char *bf, size_t size, 2394 struct syscall_arg *arg, unsigned long val) 2395 { 2396 if (fmt && fmt->scnprintf) { 2397 arg->val = val; 2398 if (fmt->parm) 2399 arg->parm = fmt->parm; 2400 return fmt->scnprintf(bf, size, arg); 2401 } 2402 return scnprintf(bf, size, "%ld", val); 2403 } 2404 2405 static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size, 2406 unsigned char *args, void *augmented_args, int augmented_args_size, 2407 struct trace *trace, struct thread *thread) 2408 { 2409 size_t printed = 0, btf_printed; 2410 unsigned long val; 2411 u8 bit = 1; 2412 struct syscall_arg arg = { 2413 .args = args, 2414 .augmented = { 2415 .size = augmented_args_size, 2416 .args = augmented_args, 2417 }, 2418 .idx = 0, 2419 .mask = 0, 2420 .trace = trace, 2421 .thread = thread, 2422 .show_string_prefix = trace->show_string_prefix, 2423 }; 2424 struct thread_trace *ttrace = thread__priv(thread); 2425 void *default_scnprintf; 2426 2427 /* 2428 * Things like fcntl will set this in its 'cmd' formatter to pick the 2429 * right formatter for the return value (an fd? file flags?), which is 2430 * not needed for syscalls that always return a given type, say an fd. 2431 */ 2432 ttrace->ret_scnprintf = NULL; 2433 2434 if (sc->args != NULL) { 2435 struct tep_format_field *field; 2436 2437 for (field = sc->args; field; 2438 field = field->next, ++arg.idx, bit <<= 1) { 2439 if (arg.mask & bit) 2440 continue; 2441 2442 arg.fmt = &sc->arg_fmt[arg.idx]; 2443 val = syscall_arg__val(&arg, arg.idx); 2444 /* 2445 * Some syscall args need some mask, most don't and 2446 * return val untouched. 2447 */ 2448 val = syscall_arg_fmt__mask_val(&sc->arg_fmt[arg.idx], &arg, val); 2449 2450 /* 2451 * Suppress this argument if its value is zero and show_zero 2452 * property isn't set. 2453 * 2454 * If it has a BTF type, then override the zero suppression knob 2455 * as the common case is for zero in an enum to have an associated entry. 2456 */ 2457 if (val == 0 && !trace->show_zeros && 2458 !(sc->arg_fmt && sc->arg_fmt[arg.idx].show_zero) && 2459 !(sc->arg_fmt && sc->arg_fmt[arg.idx].strtoul == STUL_BTF_TYPE)) 2460 continue; 2461 2462 printed += scnprintf(bf + printed, size - printed, "%s", printed ? ", " : ""); 2463 2464 if (trace->show_arg_names) 2465 printed += scnprintf(bf + printed, size - printed, "%s: ", field->name); 2466 2467 default_scnprintf = sc->arg_fmt[arg.idx].scnprintf; 2468 2469 if (trace->force_btf || default_scnprintf == NULL || default_scnprintf == SCA_PTR) { 2470 btf_printed = trace__btf_scnprintf(trace, &arg, bf + printed, 2471 size - printed, val, field->type); 2472 if (btf_printed) { 2473 printed += btf_printed; 2474 continue; 2475 } 2476 } 2477 2478 printed += syscall_arg_fmt__scnprintf_val(&sc->arg_fmt[arg.idx], 2479 bf + printed, size - printed, &arg, val); 2480 } 2481 } else if (IS_ERR(sc->tp_format)) { 2482 /* 2483 * If we managed to read the tracepoint /format file, then we 2484 * may end up not having any args, like with gettid(), so only 2485 * print the raw args when we didn't manage to read it. 2486 */ 2487 while (arg.idx < sc->nr_args) { 2488 if (arg.mask & bit) 2489 goto next_arg; 2490 val = syscall_arg__val(&arg, arg.idx); 2491 if (printed) 2492 printed += scnprintf(bf + printed, size - printed, ", "); 2493 printed += syscall__scnprintf_name(sc, bf + printed, size - printed, &arg); 2494 printed += syscall_arg_fmt__scnprintf_val(&sc->arg_fmt[arg.idx], bf + printed, size - printed, &arg, val); 2495 next_arg: 2496 ++arg.idx; 2497 bit <<= 1; 2498 } 2499 } 2500 2501 return printed; 2502 } 2503 2504 static struct syscall *syscall__new(int e_machine, int id) 2505 { 2506 struct syscall *sc = zalloc(sizeof(*sc)); 2507 2508 if (!sc) 2509 return NULL; 2510 2511 sc->e_machine = e_machine; 2512 sc->id = id; 2513 return sc; 2514 } 2515 2516 static void syscall__delete(struct syscall *sc) 2517 { 2518 if (!sc) 2519 return; 2520 2521 free(sc->arg_fmt); 2522 free(sc); 2523 } 2524 2525 static int syscall__bsearch_cmp(const void *key, const void *entry) 2526 { 2527 const struct syscall *a = key, *b = *((const struct syscall **)entry); 2528 2529 if (a->e_machine != b->e_machine) 2530 return a->e_machine - b->e_machine; 2531 2532 return a->id - b->id; 2533 } 2534 2535 static int syscall__cmp(const void *va, const void *vb) 2536 { 2537 const struct syscall *a = *((const struct syscall **)va); 2538 const struct syscall *b = *((const struct syscall **)vb); 2539 2540 if (a->e_machine != b->e_machine) 2541 return a->e_machine - b->e_machine; 2542 2543 return a->id - b->id; 2544 } 2545 2546 static struct syscall *trace__find_syscall(struct trace *trace, int e_machine, int id) 2547 { 2548 struct syscall key = { 2549 .e_machine = e_machine, 2550 .id = id, 2551 }; 2552 struct syscall *sc, **tmp; 2553 2554 if (trace->syscalls.table) { 2555 struct syscall **sc_entry = bsearch(&key, trace->syscalls.table, 2556 trace->syscalls.table_size, 2557 sizeof(trace->syscalls.table[0]), 2558 syscall__bsearch_cmp); 2559 2560 if (sc_entry) 2561 return *sc_entry; 2562 } 2563 2564 sc = syscall__new(e_machine, id); 2565 if (!sc) 2566 return NULL; 2567 2568 tmp = reallocarray(trace->syscalls.table, trace->syscalls.table_size + 1, 2569 sizeof(trace->syscalls.table[0])); 2570 if (!tmp) { 2571 syscall__delete(sc); 2572 return NULL; 2573 } 2574 2575 trace->syscalls.table = tmp; 2576 trace->syscalls.table[trace->syscalls.table_size++] = sc; 2577 qsort(trace->syscalls.table, trace->syscalls.table_size, sizeof(trace->syscalls.table[0]), 2578 syscall__cmp); 2579 return sc; 2580 } 2581 2582 typedef int (*tracepoint_handler)(struct trace *trace, struct evsel *evsel, 2583 union perf_event *event, 2584 struct perf_sample *sample); 2585 2586 static struct syscall *trace__syscall_info(struct trace *trace, struct evsel *evsel, 2587 int e_machine, int id) 2588 { 2589 struct syscall *sc; 2590 int err = 0; 2591 2592 if (id < 0) { 2593 2594 /* 2595 * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried 2596 * before that, leaving at a higher verbosity level till that is 2597 * explained. Reproduced with plain ftrace with: 2598 * 2599 * echo 1 > /t/events/raw_syscalls/sys_exit/enable 2600 * grep "NR -1 " /t/trace_pipe 2601 * 2602 * After generating some load on the machine. 2603 */ 2604 if (verbose > 1) { 2605 static u64 n; 2606 fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n", 2607 id, evsel__name(evsel), ++n); 2608 } 2609 return NULL; 2610 } 2611 2612 err = -EINVAL; 2613 2614 sc = trace__find_syscall(trace, e_machine, id); 2615 if (sc) 2616 err = syscall__read_info(sc, trace); 2617 2618 if (err && verbose > 0) { 2619 errno = -err; 2620 fprintf(trace->output, "Problems reading syscall %d: %m", id); 2621 if (sc && sc->name) 2622 fprintf(trace->output, " (%s)", sc->name); 2623 fputs(" information\n", trace->output); 2624 } 2625 return err ? NULL : sc; 2626 } 2627 2628 struct syscall_stats { 2629 struct stats stats; 2630 u64 nr_failures; 2631 int max_errno; 2632 u32 *errnos; 2633 }; 2634 2635 static void thread__update_stats(struct thread *thread, struct thread_trace *ttrace, 2636 int id, struct perf_sample *sample, long err, 2637 struct trace *trace) 2638 { 2639 struct hashmap *syscall_stats = ttrace->syscall_stats; 2640 struct syscall_stats *stats = NULL; 2641 u64 duration = 0; 2642 2643 if (trace->summary_bpf) 2644 return; 2645 2646 if (trace->summary_mode == SUMMARY__BY_TOTAL) 2647 syscall_stats = trace->syscall_stats; 2648 2649 if (!hashmap__find(syscall_stats, id, &stats)) { 2650 stats = zalloc(sizeof(*stats)); 2651 if (stats == NULL) 2652 return; 2653 2654 init_stats(&stats->stats); 2655 if (hashmap__add(syscall_stats, id, stats) < 0) { 2656 free(stats); 2657 return; 2658 } 2659 } 2660 2661 if (ttrace->entry_time && sample->time > ttrace->entry_time) 2662 duration = sample->time - ttrace->entry_time; 2663 2664 update_stats(&stats->stats, duration); 2665 2666 if (err < 0) { 2667 ++stats->nr_failures; 2668 2669 if (!trace->errno_summary) 2670 return; 2671 2672 err = -err; 2673 if (err > stats->max_errno) { 2674 u32 *new_errnos = realloc(stats->errnos, err * sizeof(u32)); 2675 2676 if (new_errnos) { 2677 memset(new_errnos + stats->max_errno, 0, (err - stats->max_errno) * sizeof(u32)); 2678 } else { 2679 pr_debug("Not enough memory for errno stats for thread \"%s\"(%d/%d), results will be incomplete\n", 2680 thread__comm_str(thread), thread__pid(thread), 2681 thread__tid(thread)); 2682 return; 2683 } 2684 2685 stats->errnos = new_errnos; 2686 stats->max_errno = err; 2687 } 2688 2689 ++stats->errnos[err - 1]; 2690 } 2691 } 2692 2693 static int trace__printf_interrupted_entry(struct trace *trace) 2694 { 2695 struct thread_trace *ttrace; 2696 size_t printed; 2697 int len; 2698 2699 if (trace->failure_only || trace->current == NULL) 2700 return 0; 2701 2702 ttrace = thread__priv(trace->current); 2703 2704 if (!ttrace->entry_pending) 2705 return 0; 2706 2707 printed = trace__fprintf_entry_head(trace, trace->current, 0, false, ttrace->entry_time, trace->output); 2708 printed += len = fprintf(trace->output, "%s)", ttrace->entry_str); 2709 2710 if (len < trace->args_alignment - 4) 2711 printed += fprintf(trace->output, "%-*s", trace->args_alignment - 4 - len, " "); 2712 2713 printed += fprintf(trace->output, " ...\n"); 2714 2715 ttrace->entry_pending = false; 2716 ++trace->nr_events_printed; 2717 2718 return printed; 2719 } 2720 2721 static int trace__fprintf_sample(struct trace *trace, struct evsel *evsel, 2722 struct perf_sample *sample, struct thread *thread) 2723 { 2724 int printed = 0; 2725 2726 if (trace->print_sample) { 2727 double ts = (double)sample->time / NSEC_PER_MSEC; 2728 2729 printed += fprintf(trace->output, "%22s %10.3f %s %d/%d [%d]\n", 2730 evsel__name(evsel), ts, 2731 thread__comm_str(thread), 2732 sample->pid, sample->tid, sample->cpu); 2733 } 2734 2735 return printed; 2736 } 2737 2738 static void *syscall__augmented_args(struct syscall *sc, struct perf_sample *sample, int *augmented_args_size, int raw_augmented_args_size) 2739 { 2740 /* 2741 * For now with BPF raw_augmented we hook into raw_syscalls:sys_enter 2742 * and there we get all 6 syscall args plus the tracepoint common fields 2743 * that gets calculated at the start and the syscall_nr (another long). 2744 * So we check if that is the case and if so don't look after the 2745 * sc->args_size but always after the full raw_syscalls:sys_enter payload, 2746 * which is fixed. 2747 * 2748 * We'll revisit this later to pass s->args_size to the BPF augmenter 2749 * (now tools/perf/examples/bpf/augmented_raw_syscalls.c, so that it 2750 * copies only what we need for each syscall, like what happens when we 2751 * use syscalls:sys_enter_NAME, so that we reduce the kernel/userspace 2752 * traffic to just what is needed for each syscall. 2753 */ 2754 int args_size = raw_augmented_args_size ?: sc->args_size; 2755 2756 *augmented_args_size = sample->raw_size - args_size; 2757 if (*augmented_args_size > 0) { 2758 static uintptr_t argbuf[1024]; /* assuming single-threaded */ 2759 2760 if ((size_t)(*augmented_args_size) > sizeof(argbuf)) 2761 return NULL; 2762 2763 /* 2764 * The perf ring-buffer is 8-byte aligned but sample->raw_data 2765 * is not because it's preceded by u32 size. Later, beautifier 2766 * will use the augmented args with stricter alignments like in 2767 * some struct. To make sure it's aligned, let's copy the args 2768 * into a static buffer as it's single-threaded for now. 2769 */ 2770 memcpy(argbuf, sample->raw_data + args_size, *augmented_args_size); 2771 2772 return argbuf; 2773 } 2774 return NULL; 2775 } 2776 2777 static int trace__sys_enter(struct trace *trace, struct evsel *evsel, 2778 union perf_event *event __maybe_unused, 2779 struct perf_sample *sample) 2780 { 2781 char *msg; 2782 void *args; 2783 int printed = 0; 2784 struct thread *thread; 2785 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1; 2786 int augmented_args_size = 0, e_machine; 2787 void *augmented_args = NULL; 2788 struct syscall *sc; 2789 struct thread_trace *ttrace; 2790 2791 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid); 2792 e_machine = thread__e_machine(thread, trace->host); 2793 sc = trace__syscall_info(trace, evsel, e_machine, id); 2794 if (sc == NULL) 2795 goto out_put; 2796 ttrace = thread__trace(thread, trace); 2797 if (ttrace == NULL) 2798 goto out_put; 2799 2800 trace__fprintf_sample(trace, evsel, sample, thread); 2801 2802 args = perf_evsel__sc_tp_ptr(evsel, args, sample); 2803 2804 if (ttrace->entry_str == NULL) { 2805 ttrace->entry_str = malloc(trace__entry_str_size); 2806 if (!ttrace->entry_str) 2807 goto out_put; 2808 } 2809 2810 if (!(trace->duration_filter || trace->summary_only || trace->min_stack)) 2811 trace__printf_interrupted_entry(trace); 2812 /* 2813 * If this is raw_syscalls.sys_enter, then it always comes with the 6 possible 2814 * arguments, even if the syscall being handled, say "openat", uses only 4 arguments 2815 * this breaks syscall__augmented_args() check for augmented args, as we calculate 2816 * syscall->args_size using each syscalls:sys_enter_NAME tracefs format file, 2817 * so when handling, say the openat syscall, we end up getting 6 args for the 2818 * raw_syscalls:sys_enter event, when we expected just 4, we end up mistakenly 2819 * thinking that the extra 2 u64 args are the augmented filename, so just check 2820 * here and avoid using augmented syscalls when the evsel is the raw_syscalls one. 2821 */ 2822 if (evsel != trace->syscalls.events.sys_enter) 2823 augmented_args = syscall__augmented_args(sc, sample, &augmented_args_size, trace->raw_augmented_syscalls_args_size); 2824 ttrace->entry_time = sample->time; 2825 msg = ttrace->entry_str; 2826 printed += scnprintf(msg + printed, trace__entry_str_size - printed, "%s(", sc->name); 2827 2828 printed += syscall__scnprintf_args(sc, msg + printed, trace__entry_str_size - printed, 2829 args, augmented_args, augmented_args_size, trace, thread); 2830 2831 if (sc->is_exit) { 2832 if (!(trace->duration_filter || trace->summary_only || trace->failure_only || trace->min_stack)) { 2833 int alignment = 0; 2834 2835 trace__fprintf_entry_head(trace, thread, 0, false, ttrace->entry_time, trace->output); 2836 printed = fprintf(trace->output, "%s)", ttrace->entry_str); 2837 if (trace->args_alignment > printed) 2838 alignment = trace->args_alignment - printed; 2839 fprintf(trace->output, "%*s= ?\n", alignment, " "); 2840 } 2841 } else { 2842 ttrace->entry_pending = true; 2843 /* See trace__vfs_getname & trace__sys_exit */ 2844 ttrace->filename.pending_open = false; 2845 } 2846 2847 if (trace->current != thread) { 2848 thread__put(trace->current); 2849 trace->current = thread__get(thread); 2850 } 2851 err = 0; 2852 out_put: 2853 thread__put(thread); 2854 return err; 2855 } 2856 2857 static int trace__fprintf_sys_enter(struct trace *trace, struct evsel *evsel, 2858 struct perf_sample *sample) 2859 { 2860 struct thread_trace *ttrace; 2861 struct thread *thread; 2862 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1; 2863 struct syscall *sc; 2864 char msg[1024]; 2865 void *args, *augmented_args = NULL; 2866 int augmented_args_size, e_machine; 2867 size_t printed = 0; 2868 2869 2870 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid); 2871 e_machine = thread__e_machine(thread, trace->host); 2872 sc = trace__syscall_info(trace, evsel, e_machine, id); 2873 if (sc == NULL) 2874 goto out_put; 2875 ttrace = thread__trace(thread, trace); 2876 /* 2877 * We need to get ttrace just to make sure it is there when syscall__scnprintf_args() 2878 * and the rest of the beautifiers accessing it via struct syscall_arg touches it. 2879 */ 2880 if (ttrace == NULL) 2881 goto out_put; 2882 2883 args = perf_evsel__sc_tp_ptr(evsel, args, sample); 2884 augmented_args = syscall__augmented_args(sc, sample, &augmented_args_size, trace->raw_augmented_syscalls_args_size); 2885 printed += syscall__scnprintf_args(sc, msg, sizeof(msg), args, augmented_args, augmented_args_size, trace, thread); 2886 fprintf(trace->output, "%.*s", (int)printed, msg); 2887 err = 0; 2888 out_put: 2889 thread__put(thread); 2890 return err; 2891 } 2892 2893 static int trace__resolve_callchain(struct trace *trace, struct evsel *evsel, 2894 struct perf_sample *sample, 2895 struct callchain_cursor *cursor) 2896 { 2897 struct addr_location al; 2898 int max_stack = evsel->core.attr.sample_max_stack ? 2899 evsel->core.attr.sample_max_stack : 2900 trace->max_stack; 2901 int err = -1; 2902 2903 addr_location__init(&al); 2904 if (machine__resolve(trace->host, &al, sample) < 0) 2905 goto out; 2906 2907 err = thread__resolve_callchain(al.thread, cursor, evsel, sample, NULL, NULL, max_stack); 2908 out: 2909 addr_location__exit(&al); 2910 return err; 2911 } 2912 2913 static int trace__fprintf_callchain(struct trace *trace, struct perf_sample *sample) 2914 { 2915 /* TODO: user-configurable print_opts */ 2916 const unsigned int print_opts = EVSEL__PRINT_SYM | 2917 EVSEL__PRINT_DSO | 2918 EVSEL__PRINT_UNKNOWN_AS_ADDR; 2919 2920 return sample__fprintf_callchain(sample, 38, print_opts, get_tls_callchain_cursor(), symbol_conf.bt_stop_list, trace->output); 2921 } 2922 2923 static int trace__sys_exit(struct trace *trace, struct evsel *evsel, 2924 union perf_event *event __maybe_unused, 2925 struct perf_sample *sample) 2926 { 2927 long ret; 2928 u64 duration = 0; 2929 bool duration_calculated = false; 2930 struct thread *thread; 2931 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1, callchain_ret = 0, printed = 0; 2932 int alignment = trace->args_alignment, e_machine; 2933 struct syscall *sc; 2934 struct thread_trace *ttrace; 2935 2936 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid); 2937 e_machine = thread__e_machine(thread, trace->host); 2938 sc = trace__syscall_info(trace, evsel, e_machine, id); 2939 if (sc == NULL) 2940 goto out_put; 2941 ttrace = thread__trace(thread, trace); 2942 if (ttrace == NULL) 2943 goto out_put; 2944 2945 trace__fprintf_sample(trace, evsel, sample, thread); 2946 2947 ret = perf_evsel__sc_tp_uint(evsel, ret, sample); 2948 2949 if (trace->summary) 2950 thread__update_stats(thread, ttrace, id, sample, ret, trace); 2951 2952 if (!trace->fd_path_disabled && sc->is_open && ret >= 0 && ttrace->filename.pending_open) { 2953 trace__set_fd_pathname(thread, ret, ttrace->filename.name); 2954 ttrace->filename.pending_open = false; 2955 ++trace->stats.vfs_getname; 2956 } 2957 2958 if (ttrace->entry_time) { 2959 duration = sample->time - ttrace->entry_time; 2960 if (trace__filter_duration(trace, duration)) 2961 goto out; 2962 duration_calculated = true; 2963 } else if (trace->duration_filter) 2964 goto out; 2965 2966 if (sample->callchain) { 2967 struct callchain_cursor *cursor = get_tls_callchain_cursor(); 2968 2969 callchain_ret = trace__resolve_callchain(trace, evsel, sample, cursor); 2970 if (callchain_ret == 0) { 2971 if (cursor->nr < trace->min_stack) 2972 goto out; 2973 callchain_ret = 1; 2974 } 2975 } 2976 2977 if (trace->summary_only || (ret >= 0 && trace->failure_only)) 2978 goto out; 2979 2980 trace__fprintf_entry_head(trace, thread, duration, duration_calculated, ttrace->entry_time, trace->output); 2981 2982 if (ttrace->entry_pending) { 2983 printed = fprintf(trace->output, "%s", ttrace->entry_str); 2984 } else { 2985 printed += fprintf(trace->output, " ... ["); 2986 color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued"); 2987 printed += 9; 2988 printed += fprintf(trace->output, "]: %s()", sc->name); 2989 } 2990 2991 printed++; /* the closing ')' */ 2992 2993 if (alignment > printed) 2994 alignment -= printed; 2995 else 2996 alignment = 0; 2997 2998 fprintf(trace->output, ")%*s= ", alignment, " "); 2999 3000 if (sc->fmt == NULL) { 3001 if (ret < 0) 3002 goto errno_print; 3003 signed_print: 3004 fprintf(trace->output, "%ld", ret); 3005 } else if (ret < 0) { 3006 errno_print: { 3007 char bf[STRERR_BUFSIZE]; 3008 struct perf_env *env = evsel__env(evsel) ?: &trace->host_env; 3009 const char *emsg = str_error_r(-ret, bf, sizeof(bf)); 3010 const char *e = perf_env__arch_strerrno(env, err); 3011 3012 fprintf(trace->output, "-1 %s (%s)", e, emsg); 3013 } 3014 } else if (ret == 0 && sc->fmt->timeout) 3015 fprintf(trace->output, "0 (Timeout)"); 3016 else if (ttrace->ret_scnprintf) { 3017 char bf[1024]; 3018 struct syscall_arg arg = { 3019 .val = ret, 3020 .thread = thread, 3021 .trace = trace, 3022 }; 3023 ttrace->ret_scnprintf(bf, sizeof(bf), &arg); 3024 ttrace->ret_scnprintf = NULL; 3025 fprintf(trace->output, "%s", bf); 3026 } else if (sc->fmt->hexret) 3027 fprintf(trace->output, "%#lx", ret); 3028 else if (sc->fmt->errpid) { 3029 struct thread *child = machine__find_thread(trace->host, ret, ret); 3030 3031 fprintf(trace->output, "%ld", ret); 3032 if (child != NULL) { 3033 if (thread__comm_set(child)) 3034 fprintf(trace->output, " (%s)", thread__comm_str(child)); 3035 thread__put(child); 3036 } 3037 } else 3038 goto signed_print; 3039 3040 fputc('\n', trace->output); 3041 3042 /* 3043 * We only consider an 'event' for the sake of --max-events a non-filtered 3044 * sys_enter + sys_exit and other tracepoint events. 3045 */ 3046 if (++trace->nr_events_printed == trace->max_events && trace->max_events != ULONG_MAX) 3047 interrupted = true; 3048 3049 if (callchain_ret > 0) 3050 trace__fprintf_callchain(trace, sample); 3051 else if (callchain_ret < 0) 3052 pr_err("Problem processing %s callchain, skipping...\n", evsel__name(evsel)); 3053 out: 3054 ttrace->entry_pending = false; 3055 err = 0; 3056 out_put: 3057 thread__put(thread); 3058 return err; 3059 } 3060 3061 static int trace__vfs_getname(struct trace *trace, struct evsel *evsel, 3062 union perf_event *event __maybe_unused, 3063 struct perf_sample *sample) 3064 { 3065 struct thread *thread = machine__findnew_thread(trace->host, sample->pid, sample->tid); 3066 struct thread_trace *ttrace; 3067 size_t filename_len, entry_str_len, to_move; 3068 ssize_t remaining_space; 3069 char *pos; 3070 const char *filename = evsel__rawptr(evsel, sample, "pathname"); 3071 3072 if (!thread) 3073 goto out; 3074 3075 ttrace = thread__priv(thread); 3076 if (!ttrace) 3077 goto out_put; 3078 3079 filename_len = strlen(filename); 3080 if (filename_len == 0) 3081 goto out_put; 3082 3083 if (ttrace->filename.namelen < filename_len) { 3084 char *f = realloc(ttrace->filename.name, filename_len + 1); 3085 3086 if (f == NULL) 3087 goto out_put; 3088 3089 ttrace->filename.namelen = filename_len; 3090 ttrace->filename.name = f; 3091 } 3092 3093 strcpy(ttrace->filename.name, filename); 3094 ttrace->filename.pending_open = true; 3095 3096 if (!ttrace->filename.ptr) 3097 goto out_put; 3098 3099 entry_str_len = strlen(ttrace->entry_str); 3100 remaining_space = trace__entry_str_size - entry_str_len - 1; /* \0 */ 3101 if (remaining_space <= 0) 3102 goto out_put; 3103 3104 if (filename_len > (size_t)remaining_space) { 3105 filename += filename_len - remaining_space; 3106 filename_len = remaining_space; 3107 } 3108 3109 to_move = entry_str_len - ttrace->filename.entry_str_pos + 1; /* \0 */ 3110 pos = ttrace->entry_str + ttrace->filename.entry_str_pos; 3111 memmove(pos + filename_len, pos, to_move); 3112 memcpy(pos, filename, filename_len); 3113 3114 ttrace->filename.ptr = 0; 3115 ttrace->filename.entry_str_pos = 0; 3116 out_put: 3117 thread__put(thread); 3118 out: 3119 return 0; 3120 } 3121 3122 static int trace__sched_stat_runtime(struct trace *trace, struct evsel *evsel, 3123 union perf_event *event __maybe_unused, 3124 struct perf_sample *sample) 3125 { 3126 u64 runtime = evsel__intval(evsel, sample, "runtime"); 3127 double runtime_ms = (double)runtime / NSEC_PER_MSEC; 3128 struct thread *thread = machine__findnew_thread(trace->host, 3129 sample->pid, 3130 sample->tid); 3131 struct thread_trace *ttrace = thread__trace(thread, trace); 3132 3133 if (ttrace == NULL) 3134 goto out_dump; 3135 3136 ttrace->runtime_ms += runtime_ms; 3137 trace->runtime_ms += runtime_ms; 3138 out_put: 3139 thread__put(thread); 3140 return 0; 3141 3142 out_dump: 3143 fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n", 3144 evsel->name, 3145 evsel__strval(evsel, sample, "comm"), 3146 (pid_t)evsel__intval(evsel, sample, "pid"), 3147 runtime, 3148 evsel__intval(evsel, sample, "vruntime")); 3149 goto out_put; 3150 } 3151 3152 static int bpf_output__printer(enum binary_printer_ops op, 3153 unsigned int val, void *extra __maybe_unused, FILE *fp) 3154 { 3155 unsigned char ch = (unsigned char)val; 3156 3157 switch (op) { 3158 case BINARY_PRINT_CHAR_DATA: 3159 return fprintf(fp, "%c", isprint(ch) ? ch : '.'); 3160 case BINARY_PRINT_DATA_BEGIN: 3161 case BINARY_PRINT_LINE_BEGIN: 3162 case BINARY_PRINT_ADDR: 3163 case BINARY_PRINT_NUM_DATA: 3164 case BINARY_PRINT_NUM_PAD: 3165 case BINARY_PRINT_SEP: 3166 case BINARY_PRINT_CHAR_PAD: 3167 case BINARY_PRINT_LINE_END: 3168 case BINARY_PRINT_DATA_END: 3169 default: 3170 break; 3171 } 3172 3173 return 0; 3174 } 3175 3176 static void bpf_output__fprintf(struct trace *trace, 3177 struct perf_sample *sample) 3178 { 3179 binary__fprintf(sample->raw_data, sample->raw_size, 8, 3180 bpf_output__printer, NULL, trace->output); 3181 ++trace->nr_events_printed; 3182 } 3183 3184 static size_t trace__fprintf_tp_fields(struct trace *trace, struct evsel *evsel, struct perf_sample *sample, 3185 struct thread *thread, void *augmented_args, int augmented_args_size) 3186 { 3187 char bf[2048]; 3188 size_t size = sizeof(bf); 3189 const struct tep_event *tp_format = evsel__tp_format(evsel); 3190 struct tep_format_field *field = tp_format ? tp_format->format.fields : NULL; 3191 struct syscall_arg_fmt *arg = __evsel__syscall_arg_fmt(evsel); 3192 size_t printed = 0, btf_printed; 3193 unsigned long val; 3194 u8 bit = 1; 3195 struct syscall_arg syscall_arg = { 3196 .augmented = { 3197 .size = augmented_args_size, 3198 .args = augmented_args, 3199 }, 3200 .idx = 0, 3201 .mask = 0, 3202 .trace = trace, 3203 .thread = thread, 3204 .show_string_prefix = trace->show_string_prefix, 3205 }; 3206 3207 for (; field && arg; field = field->next, ++syscall_arg.idx, bit <<= 1, ++arg) { 3208 if (syscall_arg.mask & bit) 3209 continue; 3210 3211 syscall_arg.len = 0; 3212 syscall_arg.fmt = arg; 3213 if (field->flags & TEP_FIELD_IS_ARRAY) { 3214 int offset = field->offset; 3215 3216 if (field->flags & TEP_FIELD_IS_DYNAMIC) { 3217 offset = format_field__intval(field, sample, evsel->needs_swap); 3218 syscall_arg.len = offset >> 16; 3219 offset &= 0xffff; 3220 if (tep_field_is_relative(field->flags)) 3221 offset += field->offset + field->size; 3222 } 3223 3224 val = (uintptr_t)(sample->raw_data + offset); 3225 } else 3226 val = format_field__intval(field, sample, evsel->needs_swap); 3227 /* 3228 * Some syscall args need some mask, most don't and 3229 * return val untouched. 3230 */ 3231 val = syscall_arg_fmt__mask_val(arg, &syscall_arg, val); 3232 3233 /* Suppress this argument if its value is zero and show_zero property isn't set. */ 3234 if (val == 0 && !trace->show_zeros && !arg->show_zero && arg->strtoul != STUL_BTF_TYPE) 3235 continue; 3236 3237 printed += scnprintf(bf + printed, size - printed, "%s", printed ? ", " : ""); 3238 3239 if (trace->show_arg_names) 3240 printed += scnprintf(bf + printed, size - printed, "%s: ", field->name); 3241 3242 btf_printed = trace__btf_scnprintf(trace, &syscall_arg, bf + printed, size - printed, val, field->type); 3243 if (btf_printed) { 3244 printed += btf_printed; 3245 continue; 3246 } 3247 3248 printed += syscall_arg_fmt__scnprintf_val(arg, bf + printed, size - printed, &syscall_arg, val); 3249 } 3250 3251 return fprintf(trace->output, "%.*s", (int)printed, bf); 3252 } 3253 3254 static int trace__event_handler(struct trace *trace, struct evsel *evsel, 3255 union perf_event *event __maybe_unused, 3256 struct perf_sample *sample) 3257 { 3258 struct thread *thread; 3259 int callchain_ret = 0; 3260 3261 if (evsel->nr_events_printed >= evsel->max_events) 3262 return 0; 3263 3264 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid); 3265 3266 if (sample->callchain) { 3267 struct callchain_cursor *cursor = get_tls_callchain_cursor(); 3268 3269 callchain_ret = trace__resolve_callchain(trace, evsel, sample, cursor); 3270 if (callchain_ret == 0) { 3271 if (cursor->nr < trace->min_stack) 3272 goto out; 3273 callchain_ret = 1; 3274 } 3275 } 3276 3277 trace__printf_interrupted_entry(trace); 3278 trace__fprintf_tstamp(trace, sample->time, trace->output); 3279 3280 if (trace->trace_syscalls && trace->show_duration) 3281 fprintf(trace->output, "( ): "); 3282 3283 if (thread) 3284 trace__fprintf_comm_tid(trace, thread, trace->output); 3285 3286 if (evsel == trace->syscalls.events.bpf_output) { 3287 int id = perf_evsel__sc_tp_uint(evsel, id, sample); 3288 int e_machine = thread ? thread__e_machine(thread, trace->host) : EM_HOST; 3289 struct syscall *sc = trace__syscall_info(trace, evsel, e_machine, id); 3290 3291 if (sc) { 3292 fprintf(trace->output, "%s(", sc->name); 3293 trace__fprintf_sys_enter(trace, evsel, sample); 3294 fputc(')', trace->output); 3295 goto newline; 3296 } 3297 3298 /* 3299 * XXX: Not having the associated syscall info or not finding/adding 3300 * the thread should never happen, but if it does... 3301 * fall thru and print it as a bpf_output event. 3302 */ 3303 } 3304 3305 fprintf(trace->output, "%s(", evsel->name); 3306 3307 if (evsel__is_bpf_output(evsel)) { 3308 bpf_output__fprintf(trace, sample); 3309 } else { 3310 const struct tep_event *tp_format = evsel__tp_format(evsel); 3311 3312 if (tp_format && (strncmp(tp_format->name, "sys_enter_", 10) || 3313 trace__fprintf_sys_enter(trace, evsel, sample))) { 3314 if (trace->libtraceevent_print) { 3315 event_format__fprintf(tp_format, sample->cpu, 3316 sample->raw_data, sample->raw_size, 3317 trace->output); 3318 } else { 3319 trace__fprintf_tp_fields(trace, evsel, sample, thread, NULL, 0); 3320 } 3321 } 3322 } 3323 3324 newline: 3325 fprintf(trace->output, ")\n"); 3326 3327 if (callchain_ret > 0) 3328 trace__fprintf_callchain(trace, sample); 3329 else if (callchain_ret < 0) 3330 pr_err("Problem processing %s callchain, skipping...\n", evsel__name(evsel)); 3331 3332 ++trace->nr_events_printed; 3333 3334 if (evsel->max_events != ULONG_MAX && ++evsel->nr_events_printed == evsel->max_events) { 3335 evsel__disable(evsel); 3336 evsel__close(evsel); 3337 } 3338 out: 3339 thread__put(thread); 3340 return 0; 3341 } 3342 3343 static void print_location(FILE *f, struct perf_sample *sample, 3344 struct addr_location *al, 3345 bool print_dso, bool print_sym) 3346 { 3347 3348 if ((verbose > 0 || print_dso) && al->map) 3349 fprintf(f, "%s@", dso__long_name(map__dso(al->map))); 3350 3351 if ((verbose > 0 || print_sym) && al->sym) 3352 fprintf(f, "%s+0x%" PRIx64, al->sym->name, 3353 al->addr - al->sym->start); 3354 else if (al->map) 3355 fprintf(f, "0x%" PRIx64, al->addr); 3356 else 3357 fprintf(f, "0x%" PRIx64, sample->addr); 3358 } 3359 3360 static int trace__pgfault(struct trace *trace, 3361 struct evsel *evsel, 3362 union perf_event *event __maybe_unused, 3363 struct perf_sample *sample) 3364 { 3365 struct thread *thread; 3366 struct addr_location al; 3367 char map_type = 'd'; 3368 struct thread_trace *ttrace; 3369 int err = -1; 3370 int callchain_ret = 0; 3371 3372 addr_location__init(&al); 3373 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid); 3374 3375 if (sample->callchain) { 3376 struct callchain_cursor *cursor = get_tls_callchain_cursor(); 3377 3378 callchain_ret = trace__resolve_callchain(trace, evsel, sample, cursor); 3379 if (callchain_ret == 0) { 3380 if (cursor->nr < trace->min_stack) 3381 goto out_put; 3382 callchain_ret = 1; 3383 } 3384 } 3385 3386 ttrace = thread__trace(thread, trace); 3387 if (ttrace == NULL) 3388 goto out_put; 3389 3390 if (evsel->core.attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ) { 3391 ttrace->pfmaj++; 3392 trace->pfmaj++; 3393 } else { 3394 ttrace->pfmin++; 3395 trace->pfmin++; 3396 } 3397 3398 if (trace->summary_only) 3399 goto out; 3400 3401 thread__find_symbol(thread, sample->cpumode, sample->ip, &al); 3402 3403 trace__fprintf_entry_head(trace, thread, 0, true, sample->time, trace->output); 3404 3405 fprintf(trace->output, "%sfault [", 3406 evsel->core.attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ? 3407 "maj" : "min"); 3408 3409 print_location(trace->output, sample, &al, false, true); 3410 3411 fprintf(trace->output, "] => "); 3412 3413 thread__find_symbol(thread, sample->cpumode, sample->addr, &al); 3414 3415 if (!al.map) { 3416 thread__find_symbol(thread, sample->cpumode, sample->addr, &al); 3417 3418 if (al.map) 3419 map_type = 'x'; 3420 else 3421 map_type = '?'; 3422 } 3423 3424 print_location(trace->output, sample, &al, true, false); 3425 3426 fprintf(trace->output, " (%c%c)\n", map_type, al.level); 3427 3428 if (callchain_ret > 0) 3429 trace__fprintf_callchain(trace, sample); 3430 else if (callchain_ret < 0) 3431 pr_err("Problem processing %s callchain, skipping...\n", evsel__name(evsel)); 3432 3433 ++trace->nr_events_printed; 3434 out: 3435 err = 0; 3436 out_put: 3437 thread__put(thread); 3438 addr_location__exit(&al); 3439 return err; 3440 } 3441 3442 static void trace__set_base_time(struct trace *trace, 3443 struct evsel *evsel, 3444 struct perf_sample *sample) 3445 { 3446 /* 3447 * BPF events were not setting PERF_SAMPLE_TIME, so be more robust 3448 * and don't use sample->time unconditionally, we may end up having 3449 * some other event in the future without PERF_SAMPLE_TIME for good 3450 * reason, i.e. we may not be interested in its timestamps, just in 3451 * it taking place, picking some piece of information when it 3452 * appears in our event stream (vfs_getname comes to mind). 3453 */ 3454 if (trace->base_time == 0 && !trace->full_time && 3455 (evsel->core.attr.sample_type & PERF_SAMPLE_TIME)) 3456 trace->base_time = sample->time; 3457 } 3458 3459 static int trace__process_sample(const struct perf_tool *tool, 3460 union perf_event *event, 3461 struct perf_sample *sample, 3462 struct evsel *evsel, 3463 struct machine *machine __maybe_unused) 3464 { 3465 struct trace *trace = container_of(tool, struct trace, tool); 3466 struct thread *thread; 3467 int err = 0; 3468 3469 tracepoint_handler handler = evsel->handler; 3470 3471 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid); 3472 if (thread && thread__is_filtered(thread)) 3473 goto out; 3474 3475 trace__set_base_time(trace, evsel, sample); 3476 3477 if (handler) { 3478 ++trace->nr_events; 3479 handler(trace, evsel, event, sample); 3480 } 3481 out: 3482 thread__put(thread); 3483 return err; 3484 } 3485 3486 static int trace__record(struct trace *trace, int argc, const char **argv) 3487 { 3488 unsigned int rec_argc, i, j; 3489 const char **rec_argv; 3490 const char * const record_args[] = { 3491 "record", 3492 "-R", 3493 "-m", "1024", 3494 "-c", "1", 3495 }; 3496 pid_t pid = getpid(); 3497 char *filter = asprintf__tp_filter_pids(1, &pid); 3498 const char * const sc_args[] = { "-e", }; 3499 unsigned int sc_args_nr = ARRAY_SIZE(sc_args); 3500 const char * const majpf_args[] = { "-e", "major-faults" }; 3501 unsigned int majpf_args_nr = ARRAY_SIZE(majpf_args); 3502 const char * const minpf_args[] = { "-e", "minor-faults" }; 3503 unsigned int minpf_args_nr = ARRAY_SIZE(minpf_args); 3504 int err = -1; 3505 3506 /* +3 is for the event string below and the pid filter */ 3507 rec_argc = ARRAY_SIZE(record_args) + sc_args_nr + 3 + 3508 majpf_args_nr + minpf_args_nr + argc; 3509 rec_argv = calloc(rec_argc + 1, sizeof(char *)); 3510 3511 if (rec_argv == NULL || filter == NULL) 3512 goto out_free; 3513 3514 j = 0; 3515 for (i = 0; i < ARRAY_SIZE(record_args); i++) 3516 rec_argv[j++] = record_args[i]; 3517 3518 if (trace->trace_syscalls) { 3519 for (i = 0; i < sc_args_nr; i++) 3520 rec_argv[j++] = sc_args[i]; 3521 3522 /* event string may be different for older kernels - e.g., RHEL6 */ 3523 if (is_valid_tracepoint("raw_syscalls:sys_enter")) 3524 rec_argv[j++] = "raw_syscalls:sys_enter,raw_syscalls:sys_exit"; 3525 else if (is_valid_tracepoint("syscalls:sys_enter")) 3526 rec_argv[j++] = "syscalls:sys_enter,syscalls:sys_exit"; 3527 else { 3528 pr_err("Neither raw_syscalls nor syscalls events exist.\n"); 3529 goto out_free; 3530 } 3531 } 3532 3533 rec_argv[j++] = "--filter"; 3534 rec_argv[j++] = filter; 3535 3536 if (trace->trace_pgfaults & TRACE_PFMAJ) 3537 for (i = 0; i < majpf_args_nr; i++) 3538 rec_argv[j++] = majpf_args[i]; 3539 3540 if (trace->trace_pgfaults & TRACE_PFMIN) 3541 for (i = 0; i < minpf_args_nr; i++) 3542 rec_argv[j++] = minpf_args[i]; 3543 3544 for (i = 0; i < (unsigned int)argc; i++) 3545 rec_argv[j++] = argv[i]; 3546 3547 err = cmd_record(j, rec_argv); 3548 out_free: 3549 free(filter); 3550 free(rec_argv); 3551 return err; 3552 } 3553 3554 static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp); 3555 static size_t trace__fprintf_total_summary(struct trace *trace, FILE *fp); 3556 3557 static bool evlist__add_vfs_getname(struct evlist *evlist) 3558 { 3559 bool found = false; 3560 struct evsel *evsel, *tmp; 3561 struct parse_events_error err; 3562 int ret; 3563 3564 parse_events_error__init(&err); 3565 ret = parse_events(evlist, "probe:vfs_getname*", &err); 3566 parse_events_error__exit(&err); 3567 if (ret) 3568 return false; 3569 3570 evlist__for_each_entry_safe(evlist, evsel, tmp) { 3571 if (!strstarts(evsel__name(evsel), "probe:vfs_getname")) 3572 continue; 3573 3574 if (evsel__field(evsel, "pathname")) { 3575 evsel->handler = trace__vfs_getname; 3576 found = true; 3577 continue; 3578 } 3579 3580 list_del_init(&evsel->core.node); 3581 evsel->evlist = NULL; 3582 evsel__delete(evsel); 3583 } 3584 3585 return found; 3586 } 3587 3588 static struct evsel *evsel__new_pgfault(u64 config) 3589 { 3590 struct evsel *evsel; 3591 struct perf_event_attr attr = { 3592 .type = PERF_TYPE_SOFTWARE, 3593 .mmap_data = 1, 3594 }; 3595 3596 attr.config = config; 3597 attr.sample_period = 1; 3598 3599 event_attr_init(&attr); 3600 3601 evsel = evsel__new(&attr); 3602 if (evsel) 3603 evsel->handler = trace__pgfault; 3604 3605 return evsel; 3606 } 3607 3608 static void evlist__free_syscall_tp_fields(struct evlist *evlist) 3609 { 3610 struct evsel *evsel; 3611 3612 evlist__for_each_entry(evlist, evsel) { 3613 evsel_trace__delete(evsel->priv); 3614 evsel->priv = NULL; 3615 } 3616 } 3617 3618 static void trace__handle_event(struct trace *trace, union perf_event *event, struct perf_sample *sample) 3619 { 3620 const u32 type = event->header.type; 3621 struct evsel *evsel; 3622 3623 if (type != PERF_RECORD_SAMPLE) { 3624 trace__process_event(trace, trace->host, event, sample); 3625 return; 3626 } 3627 3628 evsel = evlist__id2evsel(trace->evlist, sample->id); 3629 if (evsel == NULL) { 3630 fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample->id); 3631 return; 3632 } 3633 3634 if (evswitch__discard(&trace->evswitch, evsel)) 3635 return; 3636 3637 trace__set_base_time(trace, evsel, sample); 3638 3639 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT && 3640 sample->raw_data == NULL) { 3641 fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n", 3642 evsel__name(evsel), sample->tid, 3643 sample->cpu, sample->raw_size); 3644 } else { 3645 tracepoint_handler handler = evsel->handler; 3646 handler(trace, evsel, event, sample); 3647 } 3648 3649 if (trace->nr_events_printed >= trace->max_events && trace->max_events != ULONG_MAX) 3650 interrupted = true; 3651 } 3652 3653 static int trace__add_syscall_newtp(struct trace *trace) 3654 { 3655 int ret = -1; 3656 struct evlist *evlist = trace->evlist; 3657 struct evsel *sys_enter, *sys_exit; 3658 3659 sys_enter = perf_evsel__raw_syscall_newtp("sys_enter", trace__sys_enter); 3660 if (sys_enter == NULL) 3661 goto out; 3662 3663 if (perf_evsel__init_sc_tp_ptr_field(sys_enter, args)) 3664 goto out_delete_sys_enter; 3665 3666 sys_exit = perf_evsel__raw_syscall_newtp("sys_exit", trace__sys_exit); 3667 if (sys_exit == NULL) 3668 goto out_delete_sys_enter; 3669 3670 if (perf_evsel__init_sc_tp_uint_field(sys_exit, ret)) 3671 goto out_delete_sys_exit; 3672 3673 evsel__config_callchain(sys_enter, &trace->opts, &callchain_param); 3674 evsel__config_callchain(sys_exit, &trace->opts, &callchain_param); 3675 3676 evlist__add(evlist, sys_enter); 3677 evlist__add(evlist, sys_exit); 3678 3679 if (callchain_param.enabled && !trace->kernel_syscallchains) { 3680 /* 3681 * We're interested only in the user space callchain 3682 * leading to the syscall, allow overriding that for 3683 * debugging reasons using --kernel_syscall_callchains 3684 */ 3685 sys_exit->core.attr.exclude_callchain_kernel = 1; 3686 } 3687 3688 trace->syscalls.events.sys_enter = sys_enter; 3689 trace->syscalls.events.sys_exit = sys_exit; 3690 3691 ret = 0; 3692 out: 3693 return ret; 3694 3695 out_delete_sys_exit: 3696 evsel__delete_priv(sys_exit); 3697 out_delete_sys_enter: 3698 evsel__delete_priv(sys_enter); 3699 goto out; 3700 } 3701 3702 static int trace__set_ev_qualifier_tp_filter(struct trace *trace) 3703 { 3704 int err = -1; 3705 struct evsel *sys_exit; 3706 char *filter = asprintf_expr_inout_ints("id", !trace->not_ev_qualifier, 3707 trace->ev_qualifier_ids.nr, 3708 trace->ev_qualifier_ids.entries); 3709 3710 if (filter == NULL) 3711 goto out_enomem; 3712 3713 if (!evsel__append_tp_filter(trace->syscalls.events.sys_enter, filter)) { 3714 sys_exit = trace->syscalls.events.sys_exit; 3715 err = evsel__append_tp_filter(sys_exit, filter); 3716 } 3717 3718 free(filter); 3719 out: 3720 return err; 3721 out_enomem: 3722 errno = ENOMEM; 3723 goto out; 3724 } 3725 3726 #ifdef HAVE_LIBBPF_SUPPORT 3727 3728 static struct bpf_program *unaugmented_prog; 3729 3730 static int syscall_arg_fmt__cache_btf_struct(struct syscall_arg_fmt *arg_fmt, struct btf *btf, char *type) 3731 { 3732 int id; 3733 3734 if (arg_fmt->type != NULL) 3735 return -1; 3736 3737 id = btf__find_by_name(btf, type); 3738 if (id < 0) 3739 return -1; 3740 3741 arg_fmt->type = btf__type_by_id(btf, id); 3742 arg_fmt->type_id = id; 3743 3744 return 0; 3745 } 3746 3747 static struct bpf_program *trace__find_syscall_bpf_prog(struct trace *trace __maybe_unused, 3748 struct syscall *sc, 3749 const char *prog_name, const char *type) 3750 { 3751 struct bpf_program *prog; 3752 3753 if (prog_name == NULL) { 3754 char default_prog_name[256]; 3755 scnprintf(default_prog_name, sizeof(default_prog_name), "tp/syscalls/sys_%s_%s", type, sc->name); 3756 prog = augmented_syscalls__find_by_title(default_prog_name); 3757 if (prog != NULL) 3758 goto out_found; 3759 if (sc->fmt && sc->fmt->alias) { 3760 scnprintf(default_prog_name, sizeof(default_prog_name), "tp/syscalls/sys_%s_%s", type, sc->fmt->alias); 3761 prog = augmented_syscalls__find_by_title(default_prog_name); 3762 if (prog != NULL) 3763 goto out_found; 3764 } 3765 goto out_unaugmented; 3766 } 3767 3768 prog = augmented_syscalls__find_by_title(prog_name); 3769 3770 if (prog != NULL) { 3771 out_found: 3772 return prog; 3773 } 3774 3775 pr_debug("Couldn't find BPF prog \"%s\" to associate with syscalls:sys_%s_%s, not augmenting it\n", 3776 prog_name, type, sc->name); 3777 out_unaugmented: 3778 return unaugmented_prog; 3779 } 3780 3781 static void trace__init_syscall_bpf_progs(struct trace *trace, int e_machine, int id) 3782 { 3783 struct syscall *sc = trace__syscall_info(trace, NULL, e_machine, id); 3784 3785 if (sc == NULL) 3786 return; 3787 3788 sc->bpf_prog.sys_enter = trace__find_syscall_bpf_prog(trace, sc, sc->fmt ? sc->fmt->bpf_prog_name.sys_enter : NULL, "enter"); 3789 sc->bpf_prog.sys_exit = trace__find_syscall_bpf_prog(trace, sc, sc->fmt ? sc->fmt->bpf_prog_name.sys_exit : NULL, "exit"); 3790 } 3791 3792 static int trace__bpf_prog_sys_enter_fd(struct trace *trace, int e_machine, int id) 3793 { 3794 struct syscall *sc = trace__syscall_info(trace, NULL, e_machine, id); 3795 return sc ? bpf_program__fd(sc->bpf_prog.sys_enter) : bpf_program__fd(unaugmented_prog); 3796 } 3797 3798 static int trace__bpf_prog_sys_exit_fd(struct trace *trace, int e_machine, int id) 3799 { 3800 struct syscall *sc = trace__syscall_info(trace, NULL, e_machine, id); 3801 return sc ? bpf_program__fd(sc->bpf_prog.sys_exit) : bpf_program__fd(unaugmented_prog); 3802 } 3803 3804 static int trace__bpf_sys_enter_beauty_map(struct trace *trace, int e_machine, int key, unsigned int *beauty_array) 3805 { 3806 struct tep_format_field *field; 3807 struct syscall *sc = trace__syscall_info(trace, NULL, e_machine, key); 3808 const struct btf_type *bt; 3809 char *struct_offset, *tmp, name[32]; 3810 bool can_augment = false; 3811 int i, cnt; 3812 3813 if (sc == NULL) 3814 return -1; 3815 3816 trace__load_vmlinux_btf(trace); 3817 if (trace->btf == NULL) 3818 return -1; 3819 3820 for (i = 0, field = sc->args; field; ++i, field = field->next) { 3821 // XXX We're only collecting pointer payloads _from_ user space 3822 if (!sc->arg_fmt[i].from_user) 3823 continue; 3824 3825 struct_offset = strstr(field->type, "struct "); 3826 if (struct_offset == NULL) 3827 struct_offset = strstr(field->type, "union "); 3828 else 3829 struct_offset++; // "union" is shorter 3830 3831 if (field->flags & TEP_FIELD_IS_POINTER && struct_offset) { /* struct or union (think BPF's attr arg) */ 3832 struct_offset += 6; 3833 3834 /* for 'struct foo *', we only want 'foo' */ 3835 for (tmp = struct_offset, cnt = 0; *tmp != ' ' && *tmp != '\0'; ++tmp, ++cnt) { 3836 } 3837 3838 strncpy(name, struct_offset, cnt); 3839 name[cnt] = '\0'; 3840 3841 /* cache struct's btf_type and type_id */ 3842 if (syscall_arg_fmt__cache_btf_struct(&sc->arg_fmt[i], trace->btf, name)) 3843 continue; 3844 3845 bt = sc->arg_fmt[i].type; 3846 beauty_array[i] = bt->size; 3847 can_augment = true; 3848 } else if (field->flags & TEP_FIELD_IS_POINTER && /* string */ 3849 strcmp(field->type, "const char *") == 0 && 3850 (strstr(field->name, "name") || 3851 strstr(field->name, "path") || 3852 strstr(field->name, "file") || 3853 strstr(field->name, "root") || 3854 strstr(field->name, "key") || 3855 strstr(field->name, "special") || 3856 strstr(field->name, "type") || 3857 strstr(field->name, "description"))) { 3858 beauty_array[i] = 1; 3859 can_augment = true; 3860 } else if (field->flags & TEP_FIELD_IS_POINTER && /* buffer */ 3861 strstr(field->type, "char *") && 3862 (strstr(field->name, "buf") || 3863 strstr(field->name, "val") || 3864 strstr(field->name, "msg"))) { 3865 int j; 3866 struct tep_format_field *field_tmp; 3867 3868 /* find the size of the buffer that appears in pairs with buf */ 3869 for (j = 0, field_tmp = sc->args; field_tmp; ++j, field_tmp = field_tmp->next) { 3870 if (!(field_tmp->flags & TEP_FIELD_IS_POINTER) && /* only integers */ 3871 (strstr(field_tmp->name, "count") || 3872 strstr(field_tmp->name, "siz") || /* size, bufsiz */ 3873 (strstr(field_tmp->name, "len") && strcmp(field_tmp->name, "filename")))) { 3874 /* filename's got 'len' in it, we don't want that */ 3875 beauty_array[i] = -(j + 1); 3876 can_augment = true; 3877 break; 3878 } 3879 } 3880 } 3881 } 3882 3883 if (can_augment) 3884 return 0; 3885 3886 return -1; 3887 } 3888 3889 static struct bpf_program *trace__find_usable_bpf_prog_entry(struct trace *trace, 3890 struct syscall *sc) 3891 { 3892 struct tep_format_field *field, *candidate_field; 3893 /* 3894 * We're only interested in syscalls that have a pointer: 3895 */ 3896 for (field = sc->args; field; field = field->next) { 3897 if (field->flags & TEP_FIELD_IS_POINTER) 3898 goto try_to_find_pair; 3899 } 3900 3901 return NULL; 3902 3903 try_to_find_pair: 3904 for (int i = 0, num_idx = syscalltbl__num_idx(sc->e_machine); i < num_idx; ++i) { 3905 int id = syscalltbl__id_at_idx(sc->e_machine, i); 3906 struct syscall *pair = trace__syscall_info(trace, NULL, sc->e_machine, id); 3907 struct bpf_program *pair_prog; 3908 bool is_candidate = false; 3909 3910 if (pair == NULL || pair->id == sc->id || 3911 pair->bpf_prog.sys_enter == unaugmented_prog) 3912 continue; 3913 3914 for (field = sc->args, candidate_field = pair->args; 3915 field && candidate_field; field = field->next, candidate_field = candidate_field->next) { 3916 bool is_pointer = field->flags & TEP_FIELD_IS_POINTER, 3917 candidate_is_pointer = candidate_field->flags & TEP_FIELD_IS_POINTER; 3918 3919 if (is_pointer) { 3920 if (!candidate_is_pointer) { 3921 // The candidate just doesn't copies our pointer arg, might copy other pointers we want. 3922 continue; 3923 } 3924 } else { 3925 if (candidate_is_pointer) { 3926 // The candidate might copy a pointer we don't have, skip it. 3927 goto next_candidate; 3928 } 3929 continue; 3930 } 3931 3932 if (strcmp(field->type, candidate_field->type)) 3933 goto next_candidate; 3934 3935 /* 3936 * This is limited in the BPF program but sys_write 3937 * uses "const char *" for its "buf" arg so we need to 3938 * use some heuristic that is kinda future proof... 3939 */ 3940 if (strcmp(field->type, "const char *") == 0 && 3941 !(strstr(field->name, "name") || 3942 strstr(field->name, "path") || 3943 strstr(field->name, "file") || 3944 strstr(field->name, "root") || 3945 strstr(field->name, "description"))) 3946 goto next_candidate; 3947 3948 is_candidate = true; 3949 } 3950 3951 if (!is_candidate) 3952 goto next_candidate; 3953 3954 /* 3955 * Check if the tentative pair syscall augmenter has more pointers, if it has, 3956 * then it may be collecting that and we then can't use it, as it would collect 3957 * more than what is common to the two syscalls. 3958 */ 3959 if (candidate_field) { 3960 for (candidate_field = candidate_field->next; candidate_field; candidate_field = candidate_field->next) 3961 if (candidate_field->flags & TEP_FIELD_IS_POINTER) 3962 goto next_candidate; 3963 } 3964 3965 pair_prog = pair->bpf_prog.sys_enter; 3966 /* 3967 * If the pair isn't enabled, then its bpf_prog.sys_enter will not 3968 * have been searched for, so search it here and if it returns the 3969 * unaugmented one, then ignore it, otherwise we'll reuse that BPF 3970 * program for a filtered syscall on a non-filtered one. 3971 * 3972 * For instance, we have "!syscalls:sys_enter_renameat" and that is 3973 * useful for "renameat2". 3974 */ 3975 if (pair_prog == NULL) { 3976 pair_prog = trace__find_syscall_bpf_prog(trace, pair, pair->fmt ? pair->fmt->bpf_prog_name.sys_enter : NULL, "enter"); 3977 if (pair_prog == unaugmented_prog) 3978 goto next_candidate; 3979 } 3980 3981 pr_debug("Reusing \"%s\" BPF sys_enter augmenter for \"%s\"\n", pair->name, 3982 sc->name); 3983 return pair_prog; 3984 next_candidate: 3985 continue; 3986 } 3987 3988 return NULL; 3989 } 3990 3991 static int trace__init_syscalls_bpf_prog_array_maps(struct trace *trace, int e_machine) 3992 { 3993 int map_enter_fd; 3994 int map_exit_fd; 3995 int beauty_map_fd; 3996 int err = 0; 3997 unsigned int beauty_array[6]; 3998 3999 if (augmented_syscalls__get_map_fds(&map_enter_fd, &map_exit_fd, &beauty_map_fd) < 0) 4000 return -1; 4001 4002 unaugmented_prog = augmented_syscalls__unaugmented(); 4003 4004 for (int i = 0, num_idx = syscalltbl__num_idx(e_machine); i < num_idx; ++i) { 4005 int prog_fd, key = syscalltbl__id_at_idx(e_machine, i); 4006 4007 if (!trace__syscall_enabled(trace, key)) 4008 continue; 4009 4010 trace__init_syscall_bpf_progs(trace, e_machine, key); 4011 4012 // It'll get at least the "!raw_syscalls:unaugmented" 4013 prog_fd = trace__bpf_prog_sys_enter_fd(trace, e_machine, key); 4014 err = bpf_map_update_elem(map_enter_fd, &key, &prog_fd, BPF_ANY); 4015 if (err) 4016 break; 4017 prog_fd = trace__bpf_prog_sys_exit_fd(trace, e_machine, key); 4018 err = bpf_map_update_elem(map_exit_fd, &key, &prog_fd, BPF_ANY); 4019 if (err) 4020 break; 4021 4022 /* use beauty_map to tell BPF how many bytes to collect, set beauty_map's value here */ 4023 memset(beauty_array, 0, sizeof(beauty_array)); 4024 err = trace__bpf_sys_enter_beauty_map(trace, e_machine, key, (unsigned int *)beauty_array); 4025 if (err) 4026 continue; 4027 err = bpf_map_update_elem(beauty_map_fd, &key, beauty_array, BPF_ANY); 4028 if (err) 4029 break; 4030 } 4031 4032 /* 4033 * Now lets do a second pass looking for enabled syscalls without 4034 * an augmenter that have a signature that is a superset of another 4035 * syscall with an augmenter so that we can auto-reuse it. 4036 * 4037 * I.e. if we have an augmenter for the "open" syscall that has 4038 * this signature: 4039 * 4040 * int open(const char *pathname, int flags, mode_t mode); 4041 * 4042 * I.e. that will collect just the first string argument, then we 4043 * can reuse it for the 'creat' syscall, that has this signature: 4044 * 4045 * int creat(const char *pathname, mode_t mode); 4046 * 4047 * and for: 4048 * 4049 * int stat(const char *pathname, struct stat *statbuf); 4050 * int lstat(const char *pathname, struct stat *statbuf); 4051 * 4052 * Because the 'open' augmenter will collect the first arg as a string, 4053 * and leave alone all the other args, which already helps with 4054 * beautifying 'stat' and 'lstat''s pathname arg. 4055 * 4056 * Then, in time, when 'stat' gets an augmenter that collects both 4057 * first and second arg (this one on the raw_syscalls:sys_exit prog 4058 * array tail call, then that one will be used. 4059 */ 4060 for (int i = 0, num_idx = syscalltbl__num_idx(e_machine); i < num_idx; ++i) { 4061 int key = syscalltbl__id_at_idx(e_machine, i); 4062 struct syscall *sc = trace__syscall_info(trace, NULL, e_machine, key); 4063 struct bpf_program *pair_prog; 4064 int prog_fd; 4065 4066 if (sc == NULL || sc->bpf_prog.sys_enter == NULL) 4067 continue; 4068 4069 /* 4070 * For now we're just reusing the sys_enter prog, and if it 4071 * already has an augmenter, we don't need to find one. 4072 */ 4073 if (sc->bpf_prog.sys_enter != unaugmented_prog) 4074 continue; 4075 4076 /* 4077 * Look at all the other syscalls for one that has a signature 4078 * that is close enough that we can share: 4079 */ 4080 pair_prog = trace__find_usable_bpf_prog_entry(trace, sc); 4081 if (pair_prog == NULL) 4082 continue; 4083 4084 sc->bpf_prog.sys_enter = pair_prog; 4085 4086 /* 4087 * Update the BPF_MAP_TYPE_PROG_SHARED for raw_syscalls:sys_enter 4088 * with the fd for the program we're reusing: 4089 */ 4090 prog_fd = bpf_program__fd(sc->bpf_prog.sys_enter); 4091 err = bpf_map_update_elem(map_enter_fd, &key, &prog_fd, BPF_ANY); 4092 if (err) 4093 break; 4094 } 4095 4096 return err; 4097 } 4098 #else // !HAVE_LIBBPF_SUPPORT 4099 static int trace__init_syscalls_bpf_prog_array_maps(struct trace *trace __maybe_unused, 4100 int e_machine __maybe_unused) 4101 { 4102 return -1; 4103 } 4104 #endif // HAVE_LIBBPF_SUPPORT 4105 4106 static int trace__set_ev_qualifier_filter(struct trace *trace) 4107 { 4108 if (trace->syscalls.events.sys_enter) 4109 return trace__set_ev_qualifier_tp_filter(trace); 4110 return 0; 4111 } 4112 4113 static int trace__set_filter_loop_pids(struct trace *trace) 4114 { 4115 unsigned int nr = 1, err; 4116 pid_t pids[32] = { 4117 getpid(), 4118 }; 4119 struct thread *thread = machine__find_thread(trace->host, pids[0], pids[0]); 4120 4121 while (thread && nr < ARRAY_SIZE(pids)) { 4122 struct thread *parent = machine__find_thread(trace->host, 4123 thread__ppid(thread), 4124 thread__ppid(thread)); 4125 4126 if (parent == NULL) 4127 break; 4128 4129 if (!strcmp(thread__comm_str(parent), "sshd") || 4130 strstarts(thread__comm_str(parent), "gnome-terminal")) { 4131 pids[nr++] = thread__tid(parent); 4132 thread__put(parent); 4133 break; 4134 } 4135 thread__put(thread); 4136 thread = parent; 4137 } 4138 thread__put(thread); 4139 4140 err = evlist__append_tp_filter_pids(trace->evlist, nr, pids); 4141 if (!err) 4142 err = augmented_syscalls__set_filter_pids(nr, pids); 4143 4144 return err; 4145 } 4146 4147 static int trace__set_filter_pids(struct trace *trace) 4148 { 4149 int err = 0; 4150 /* 4151 * Better not use !target__has_task() here because we need to cover the 4152 * case where no threads were specified in the command line, but a 4153 * workload was, and in that case we will fill in the thread_map when 4154 * we fork the workload in evlist__prepare_workload. 4155 */ 4156 if (trace->filter_pids.nr > 0) { 4157 err = evlist__append_tp_filter_pids(trace->evlist, trace->filter_pids.nr, 4158 trace->filter_pids.entries); 4159 if (!err) { 4160 err = augmented_syscalls__set_filter_pids(trace->filter_pids.nr, 4161 trace->filter_pids.entries); 4162 } 4163 } else if (perf_thread_map__pid(trace->evlist->core.threads, 0) == -1) { 4164 err = trace__set_filter_loop_pids(trace); 4165 } 4166 4167 return err; 4168 } 4169 4170 static int __trace__deliver_event(struct trace *trace, union perf_event *event) 4171 { 4172 struct evlist *evlist = trace->evlist; 4173 struct perf_sample sample; 4174 int err; 4175 4176 perf_sample__init(&sample, /*all=*/false); 4177 err = evlist__parse_sample(evlist, event, &sample); 4178 if (err) 4179 fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err); 4180 else 4181 trace__handle_event(trace, event, &sample); 4182 4183 perf_sample__exit(&sample); 4184 return 0; 4185 } 4186 4187 static int __trace__flush_events(struct trace *trace) 4188 { 4189 u64 first = ordered_events__first_time(&trace->oe.data); 4190 u64 flush = trace->oe.last - NSEC_PER_SEC; 4191 4192 /* Is there some thing to flush.. */ 4193 if (first && first < flush) 4194 return ordered_events__flush_time(&trace->oe.data, flush); 4195 4196 return 0; 4197 } 4198 4199 static int trace__flush_events(struct trace *trace) 4200 { 4201 return !trace->sort_events ? 0 : __trace__flush_events(trace); 4202 } 4203 4204 static int trace__deliver_event(struct trace *trace, union perf_event *event) 4205 { 4206 int err; 4207 4208 if (!trace->sort_events) 4209 return __trace__deliver_event(trace, event); 4210 4211 err = evlist__parse_sample_timestamp(trace->evlist, event, &trace->oe.last); 4212 if (err && err != -1) 4213 return err; 4214 4215 err = ordered_events__queue(&trace->oe.data, event, trace->oe.last, 0, NULL); 4216 if (err) 4217 return err; 4218 4219 return trace__flush_events(trace); 4220 } 4221 4222 static int ordered_events__deliver_event(struct ordered_events *oe, 4223 struct ordered_event *event) 4224 { 4225 struct trace *trace = container_of(oe, struct trace, oe.data); 4226 4227 return __trace__deliver_event(trace, event->event); 4228 } 4229 4230 static struct syscall_arg_fmt *evsel__find_syscall_arg_fmt_by_name(struct evsel *evsel, char *arg, 4231 char **type) 4232 { 4233 struct syscall_arg_fmt *fmt = __evsel__syscall_arg_fmt(evsel); 4234 const struct tep_event *tp_format; 4235 4236 if (!fmt) 4237 return NULL; 4238 4239 tp_format = evsel__tp_format(evsel); 4240 if (!tp_format) 4241 return NULL; 4242 4243 for (const struct tep_format_field *field = tp_format->format.fields; field; 4244 field = field->next, ++fmt) { 4245 if (strcmp(field->name, arg) == 0) { 4246 *type = field->type; 4247 return fmt; 4248 } 4249 } 4250 4251 return NULL; 4252 } 4253 4254 static int trace__expand_filter(struct trace *trace, struct evsel *evsel) 4255 { 4256 char *tok, *left = evsel->filter, *new_filter = evsel->filter; 4257 4258 while ((tok = strpbrk(left, "=<>!")) != NULL) { 4259 char *right = tok + 1, *right_end; 4260 4261 if (*right == '=') 4262 ++right; 4263 4264 while (isspace(*right)) 4265 ++right; 4266 4267 if (*right == '\0') 4268 break; 4269 4270 while (!isalpha(*left)) 4271 if (++left == tok) { 4272 /* 4273 * Bail out, can't find the name of the argument that is being 4274 * used in the filter, let it try to set this filter, will fail later. 4275 */ 4276 return 0; 4277 } 4278 4279 right_end = right + 1; 4280 while (isalnum(*right_end) || *right_end == '_' || *right_end == '|') 4281 ++right_end; 4282 4283 if (isalpha(*right)) { 4284 struct syscall_arg_fmt *fmt; 4285 int left_size = tok - left, 4286 right_size = right_end - right; 4287 char arg[128], *type; 4288 4289 while (isspace(left[left_size - 1])) 4290 --left_size; 4291 4292 scnprintf(arg, sizeof(arg), "%.*s", left_size, left); 4293 4294 fmt = evsel__find_syscall_arg_fmt_by_name(evsel, arg, &type); 4295 if (fmt == NULL) { 4296 pr_err("\"%s\" not found in \"%s\", can't set filter \"%s\"\n", 4297 arg, evsel->name, evsel->filter); 4298 return -1; 4299 } 4300 4301 pr_debug2("trying to expand \"%s\" \"%.*s\" \"%.*s\" -> ", 4302 arg, (int)(right - tok), tok, right_size, right); 4303 4304 if (fmt->strtoul) { 4305 u64 val; 4306 struct syscall_arg syscall_arg = { 4307 .trace = trace, 4308 .fmt = fmt, 4309 .type_name = type, 4310 .parm = fmt->parm, 4311 }; 4312 4313 if (fmt->strtoul(right, right_size, &syscall_arg, &val)) { 4314 char *n, expansion[19]; 4315 int expansion_lenght = scnprintf(expansion, sizeof(expansion), "%#" PRIx64, val); 4316 int expansion_offset = right - new_filter; 4317 4318 pr_debug("%s", expansion); 4319 4320 if (asprintf(&n, "%.*s%s%s", expansion_offset, new_filter, expansion, right_end) < 0) { 4321 pr_debug(" out of memory!\n"); 4322 free(new_filter); 4323 return -1; 4324 } 4325 if (new_filter != evsel->filter) 4326 free(new_filter); 4327 left = n + expansion_offset + expansion_lenght; 4328 new_filter = n; 4329 } else { 4330 pr_err("\"%.*s\" not found for \"%s\" in \"%s\", can't set filter \"%s\"\n", 4331 right_size, right, arg, evsel->name, evsel->filter); 4332 return -1; 4333 } 4334 } else { 4335 pr_err("No resolver (strtoul) for \"%s\" in \"%s\", can't set filter \"%s\"\n", 4336 arg, evsel->name, evsel->filter); 4337 return -1; 4338 } 4339 4340 pr_debug("\n"); 4341 } else { 4342 left = right_end; 4343 } 4344 } 4345 4346 if (new_filter != evsel->filter) { 4347 pr_debug("New filter for %s: %s\n", evsel->name, new_filter); 4348 evsel__set_filter(evsel, new_filter); 4349 free(new_filter); 4350 } 4351 4352 return 0; 4353 } 4354 4355 static int trace__expand_filters(struct trace *trace, struct evsel **err_evsel) 4356 { 4357 struct evlist *evlist = trace->evlist; 4358 struct evsel *evsel; 4359 4360 evlist__for_each_entry(evlist, evsel) { 4361 if (evsel->filter == NULL) 4362 continue; 4363 4364 if (trace__expand_filter(trace, evsel)) { 4365 *err_evsel = evsel; 4366 return -1; 4367 } 4368 } 4369 4370 return 0; 4371 } 4372 4373 static int trace__run(struct trace *trace, int argc, const char **argv) 4374 { 4375 struct evlist *evlist = trace->evlist; 4376 struct evsel *evsel, *pgfault_maj = NULL, *pgfault_min = NULL; 4377 int err = -1, i; 4378 unsigned long before; 4379 const bool forks = argc > 0; 4380 bool draining = false; 4381 4382 trace->live = true; 4383 4384 if (trace->summary_bpf) { 4385 if (trace_prepare_bpf_summary(trace->summary_mode) < 0) 4386 goto out_delete_evlist; 4387 4388 if (trace->summary_only) 4389 goto create_maps; 4390 } 4391 4392 if (!trace->raw_augmented_syscalls) { 4393 if (trace->trace_syscalls && trace__add_syscall_newtp(trace)) 4394 goto out_error_raw_syscalls; 4395 4396 if (trace->trace_syscalls) 4397 trace->vfs_getname = evlist__add_vfs_getname(evlist); 4398 } 4399 4400 if ((trace->trace_pgfaults & TRACE_PFMAJ)) { 4401 pgfault_maj = evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MAJ); 4402 if (pgfault_maj == NULL) 4403 goto out_error_mem; 4404 evsel__config_callchain(pgfault_maj, &trace->opts, &callchain_param); 4405 evlist__add(evlist, pgfault_maj); 4406 } 4407 4408 if ((trace->trace_pgfaults & TRACE_PFMIN)) { 4409 pgfault_min = evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MIN); 4410 if (pgfault_min == NULL) 4411 goto out_error_mem; 4412 evsel__config_callchain(pgfault_min, &trace->opts, &callchain_param); 4413 evlist__add(evlist, pgfault_min); 4414 } 4415 4416 /* Enable ignoring missing threads when -p option is defined. */ 4417 trace->opts.ignore_missing_thread = trace->opts.target.pid; 4418 4419 if (trace->sched && 4420 evlist__add_newtp(evlist, "sched", "sched_stat_runtime", trace__sched_stat_runtime)) 4421 goto out_error_sched_stat_runtime; 4422 /* 4423 * If a global cgroup was set, apply it to all the events without an 4424 * explicit cgroup. I.e.: 4425 * 4426 * trace -G A -e sched:*switch 4427 * 4428 * Will set all raw_syscalls:sys_{enter,exit}, pgfault, vfs_getname, etc 4429 * _and_ sched:sched_switch to the 'A' cgroup, while: 4430 * 4431 * trace -e sched:*switch -G A 4432 * 4433 * will only set the sched:sched_switch event to the 'A' cgroup, all the 4434 * other events (raw_syscalls:sys_{enter,exit}, etc are left "without" 4435 * a cgroup (on the root cgroup, sys wide, etc). 4436 * 4437 * Multiple cgroups: 4438 * 4439 * trace -G A -e sched:*switch -G B 4440 * 4441 * the syscall ones go to the 'A' cgroup, the sched:sched_switch goes 4442 * to the 'B' cgroup. 4443 * 4444 * evlist__set_default_cgroup() grabs a reference of the passed cgroup 4445 * only for the evsels still without a cgroup, i.e. evsel->cgroup == NULL. 4446 */ 4447 if (trace->cgroup) 4448 evlist__set_default_cgroup(trace->evlist, trace->cgroup); 4449 4450 create_maps: 4451 err = evlist__create_maps(evlist, &trace->opts.target); 4452 if (err < 0) { 4453 fprintf(trace->output, "Problems parsing the target to trace, check your options!\n"); 4454 goto out_delete_evlist; 4455 } 4456 4457 err = trace__symbols_init(trace, argc, argv, evlist); 4458 if (err < 0) { 4459 fprintf(trace->output, "Problems initializing symbol libraries!\n"); 4460 goto out_delete_evlist; 4461 } 4462 4463 if (trace->summary_mode == SUMMARY__BY_TOTAL && !trace->summary_bpf) { 4464 trace->syscall_stats = alloc_syscall_stats(); 4465 if (IS_ERR(trace->syscall_stats)) 4466 goto out_delete_evlist; 4467 } 4468 4469 evlist__config(evlist, &trace->opts, &callchain_param); 4470 4471 if (forks) { 4472 err = evlist__prepare_workload(evlist, &trace->opts.target, argv, false, NULL); 4473 if (err < 0) { 4474 fprintf(trace->output, "Couldn't run the workload!\n"); 4475 goto out_delete_evlist; 4476 } 4477 workload_pid = evlist->workload.pid; 4478 } 4479 4480 err = evlist__open(evlist); 4481 if (err < 0) 4482 goto out_error_open; 4483 4484 augmented_syscalls__setup_bpf_output(); 4485 4486 err = trace__set_filter_pids(trace); 4487 if (err < 0) 4488 goto out_error_mem; 4489 4490 /* 4491 * TODO: Initialize for all host binary machine types, not just 4492 * those matching the perf binary. 4493 */ 4494 trace__init_syscalls_bpf_prog_array_maps(trace, EM_HOST); 4495 4496 if (trace->ev_qualifier_ids.nr > 0) { 4497 err = trace__set_ev_qualifier_filter(trace); 4498 if (err < 0) 4499 goto out_errno; 4500 4501 if (trace->syscalls.events.sys_exit) { 4502 pr_debug("event qualifier tracepoint filter: %s\n", 4503 trace->syscalls.events.sys_exit->filter); 4504 } 4505 } 4506 4507 /* 4508 * If the "close" syscall is not traced, then we will not have the 4509 * opportunity to, in syscall_arg__scnprintf_close_fd() invalidate the 4510 * fd->pathname table and were ending up showing the last value set by 4511 * syscalls opening a pathname and associating it with a descriptor or 4512 * reading it from /proc/pid/fd/ in cases where that doesn't make 4513 * sense. 4514 * 4515 * So just disable this beautifier (SCA_FD, SCA_FDAT) when 'close' is 4516 * not in use. 4517 */ 4518 /* TODO: support for more than just perf binary machine type close. */ 4519 trace->fd_path_disabled = !trace__syscall_enabled(trace, syscalltbl__id(EM_HOST, "close")); 4520 4521 err = trace__expand_filters(trace, &evsel); 4522 if (err) 4523 goto out_delete_evlist; 4524 err = evlist__apply_filters(evlist, &evsel, &trace->opts.target); 4525 if (err < 0) 4526 goto out_error_apply_filters; 4527 4528 if (!trace->summary_only || !trace->summary_bpf) { 4529 err = evlist__mmap(evlist, trace->opts.mmap_pages); 4530 if (err < 0) 4531 goto out_error_mmap; 4532 } 4533 4534 if (!target__none(&trace->opts.target) && !trace->opts.target.initial_delay) 4535 evlist__enable(evlist); 4536 4537 if (forks) 4538 evlist__start_workload(evlist); 4539 4540 if (trace->opts.target.initial_delay) { 4541 usleep(trace->opts.target.initial_delay * 1000); 4542 evlist__enable(evlist); 4543 } 4544 4545 if (trace->summary_bpf) 4546 trace_start_bpf_summary(); 4547 4548 trace->multiple_threads = perf_thread_map__pid(evlist->core.threads, 0) == -1 || 4549 perf_thread_map__nr(evlist->core.threads) > 1 || 4550 evlist__first(evlist)->core.attr.inherit; 4551 4552 /* 4553 * Now that we already used evsel->core.attr to ask the kernel to setup the 4554 * events, lets reuse evsel->core.attr.sample_max_stack as the limit in 4555 * trace__resolve_callchain(), allowing per-event max-stack settings 4556 * to override an explicitly set --max-stack global setting. 4557 */ 4558 evlist__for_each_entry(evlist, evsel) { 4559 if (evsel__has_callchain(evsel) && 4560 evsel->core.attr.sample_max_stack == 0) 4561 evsel->core.attr.sample_max_stack = trace->max_stack; 4562 } 4563 again: 4564 before = trace->nr_events; 4565 4566 for (i = 0; i < evlist->core.nr_mmaps; i++) { 4567 union perf_event *event; 4568 struct mmap *md; 4569 4570 md = &evlist->mmap[i]; 4571 if (perf_mmap__read_init(&md->core) < 0) 4572 continue; 4573 4574 while ((event = perf_mmap__read_event(&md->core)) != NULL) { 4575 ++trace->nr_events; 4576 4577 err = trace__deliver_event(trace, event); 4578 if (err) 4579 goto out_disable; 4580 4581 perf_mmap__consume(&md->core); 4582 4583 if (interrupted) 4584 goto out_disable; 4585 4586 if (done && !draining) { 4587 evlist__disable(evlist); 4588 draining = true; 4589 } 4590 } 4591 perf_mmap__read_done(&md->core); 4592 } 4593 4594 if (trace->nr_events == before) { 4595 int timeout = done ? 100 : -1; 4596 4597 if (!draining && evlist__poll(evlist, timeout) > 0) { 4598 if (evlist__filter_pollfd(evlist, POLLERR | POLLHUP | POLLNVAL) == 0) 4599 draining = true; 4600 4601 goto again; 4602 } else { 4603 if (trace__flush_events(trace)) 4604 goto out_disable; 4605 } 4606 } else { 4607 goto again; 4608 } 4609 4610 out_disable: 4611 thread__zput(trace->current); 4612 4613 evlist__disable(evlist); 4614 4615 if (trace->summary_bpf) 4616 trace_end_bpf_summary(); 4617 4618 if (trace->sort_events) 4619 ordered_events__flush(&trace->oe.data, OE_FLUSH__FINAL); 4620 4621 if (!err) { 4622 if (trace->summary) { 4623 if (trace->summary_bpf) 4624 trace_print_bpf_summary(trace->output, trace->max_summary); 4625 else if (trace->summary_mode == SUMMARY__BY_TOTAL) 4626 trace__fprintf_total_summary(trace, trace->output); 4627 else 4628 trace__fprintf_thread_summary(trace, trace->output); 4629 } 4630 4631 if (trace->show_tool_stats) { 4632 fprintf(trace->output, "Stats:\n " 4633 " vfs_getname : %" PRIu64 "\n" 4634 " proc_getname: %" PRIu64 "\n", 4635 trace->stats.vfs_getname, 4636 trace->stats.proc_getname); 4637 } 4638 } 4639 4640 out_delete_evlist: 4641 trace_cleanup_bpf_summary(); 4642 delete_syscall_stats(trace->syscall_stats); 4643 trace__symbols__exit(trace); 4644 evlist__free_syscall_tp_fields(evlist); 4645 evlist__delete(evlist); 4646 cgroup__put(trace->cgroup); 4647 trace->evlist = NULL; 4648 trace->live = false; 4649 return err; 4650 { 4651 char errbuf[BUFSIZ]; 4652 4653 out_error_sched_stat_runtime: 4654 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "sched", "sched_stat_runtime"); 4655 goto out_error; 4656 4657 out_error_raw_syscalls: 4658 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "raw_syscalls", "sys_(enter|exit)"); 4659 goto out_error; 4660 4661 out_error_mmap: 4662 evlist__strerror_mmap(evlist, errno, errbuf, sizeof(errbuf)); 4663 goto out_error; 4664 4665 out_error_open: 4666 evlist__strerror_open(evlist, errno, errbuf, sizeof(errbuf)); 4667 4668 out_error: 4669 fprintf(trace->output, "%s\n", errbuf); 4670 goto out_delete_evlist; 4671 4672 out_error_apply_filters: 4673 fprintf(trace->output, 4674 "Failed to set filter \"%s\" on event %s: %m\n", 4675 evsel->filter, evsel__name(evsel)); 4676 goto out_delete_evlist; 4677 } 4678 out_error_mem: 4679 fprintf(trace->output, "Not enough memory to run!\n"); 4680 goto out_delete_evlist; 4681 4682 out_errno: 4683 fprintf(trace->output, "%m\n"); 4684 goto out_delete_evlist; 4685 } 4686 4687 static int trace__replay(struct trace *trace) 4688 { 4689 const struct evsel_str_handler handlers[] = { 4690 { "probe:vfs_getname", trace__vfs_getname, }, 4691 }; 4692 struct perf_data data = { 4693 .path = input_name, 4694 .mode = PERF_DATA_MODE_READ, 4695 .force = trace->force, 4696 }; 4697 struct perf_session *session; 4698 struct evsel *evsel; 4699 int err = -1; 4700 4701 perf_tool__init(&trace->tool, /*ordered_events=*/true); 4702 trace->tool.sample = trace__process_sample; 4703 trace->tool.mmap = perf_event__process_mmap; 4704 trace->tool.mmap2 = perf_event__process_mmap2; 4705 trace->tool.comm = perf_event__process_comm; 4706 trace->tool.exit = perf_event__process_exit; 4707 trace->tool.fork = perf_event__process_fork; 4708 trace->tool.attr = perf_event__process_attr; 4709 trace->tool.tracing_data = perf_event__process_tracing_data; 4710 trace->tool.build_id = perf_event__process_build_id; 4711 trace->tool.namespaces = perf_event__process_namespaces; 4712 4713 trace->tool.ordered_events = true; 4714 trace->tool.ordering_requires_timestamps = true; 4715 4716 /* add tid to output */ 4717 trace->multiple_threads = true; 4718 4719 session = perf_session__new(&data, &trace->tool); 4720 if (IS_ERR(session)) 4721 return PTR_ERR(session); 4722 4723 if (trace->opts.target.pid) 4724 symbol_conf.pid_list_str = strdup(trace->opts.target.pid); 4725 4726 if (trace->opts.target.tid) 4727 symbol_conf.tid_list_str = strdup(trace->opts.target.tid); 4728 4729 if (symbol__init(perf_session__env(session)) < 0) 4730 goto out; 4731 4732 trace->host = &session->machines.host; 4733 4734 err = perf_session__set_tracepoints_handlers(session, handlers); 4735 if (err) 4736 goto out; 4737 4738 evsel = evlist__find_tracepoint_by_name(session->evlist, "raw_syscalls:sys_enter"); 4739 trace->syscalls.events.sys_enter = evsel; 4740 /* older kernels have syscalls tp versus raw_syscalls */ 4741 if (evsel == NULL) 4742 evsel = evlist__find_tracepoint_by_name(session->evlist, "syscalls:sys_enter"); 4743 4744 if (evsel && 4745 (evsel__init_raw_syscall_tp(evsel, trace__sys_enter) < 0 || 4746 perf_evsel__init_sc_tp_ptr_field(evsel, args))) { 4747 pr_err("Error during initialize raw_syscalls:sys_enter event\n"); 4748 goto out; 4749 } 4750 4751 evsel = evlist__find_tracepoint_by_name(session->evlist, "raw_syscalls:sys_exit"); 4752 trace->syscalls.events.sys_exit = evsel; 4753 if (evsel == NULL) 4754 evsel = evlist__find_tracepoint_by_name(session->evlist, "syscalls:sys_exit"); 4755 if (evsel && 4756 (evsel__init_raw_syscall_tp(evsel, trace__sys_exit) < 0 || 4757 perf_evsel__init_sc_tp_uint_field(evsel, ret))) { 4758 pr_err("Error during initialize raw_syscalls:sys_exit event\n"); 4759 goto out; 4760 } 4761 4762 evlist__for_each_entry(session->evlist, evsel) { 4763 if (evsel->core.attr.type == PERF_TYPE_SOFTWARE && 4764 (evsel->core.attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ || 4765 evsel->core.attr.config == PERF_COUNT_SW_PAGE_FAULTS_MIN || 4766 evsel->core.attr.config == PERF_COUNT_SW_PAGE_FAULTS)) 4767 evsel->handler = trace__pgfault; 4768 } 4769 4770 if (trace->summary_mode == SUMMARY__BY_TOTAL) { 4771 trace->syscall_stats = alloc_syscall_stats(); 4772 if (IS_ERR(trace->syscall_stats)) 4773 goto out; 4774 } 4775 4776 setup_pager(); 4777 4778 err = perf_session__process_events(session); 4779 if (err) 4780 pr_err("Failed to process events, error %d", err); 4781 4782 else if (trace->summary) 4783 trace__fprintf_thread_summary(trace, trace->output); 4784 4785 out: 4786 delete_syscall_stats(trace->syscall_stats); 4787 perf_session__delete(session); 4788 4789 return err; 4790 } 4791 4792 static size_t trace__fprintf_summary_header(FILE *fp) 4793 { 4794 size_t printed; 4795 4796 printed = fprintf(fp, "\n Summary of events:\n\n"); 4797 4798 return printed; 4799 } 4800 4801 struct syscall_entry { 4802 struct syscall_stats *stats; 4803 double msecs; 4804 int syscall; 4805 }; 4806 4807 static int entry_cmp(const void *e1, const void *e2) 4808 { 4809 const struct syscall_entry *entry1 = e1; 4810 const struct syscall_entry *entry2 = e2; 4811 4812 return entry1->msecs > entry2->msecs ? -1 : 1; 4813 } 4814 4815 static struct syscall_entry *syscall__sort_stats(struct hashmap *syscall_stats) 4816 { 4817 struct syscall_entry *entry; 4818 struct hashmap_entry *pos; 4819 unsigned bkt, i, nr; 4820 4821 nr = syscall_stats->sz; 4822 entry = malloc(nr * sizeof(*entry)); 4823 if (entry == NULL) 4824 return NULL; 4825 4826 i = 0; 4827 hashmap__for_each_entry(syscall_stats, pos, bkt) { 4828 struct syscall_stats *ss = pos->pvalue; 4829 struct stats *st = &ss->stats; 4830 4831 entry[i].stats = ss; 4832 entry[i].msecs = (u64)st->n * (avg_stats(st) / NSEC_PER_MSEC); 4833 entry[i].syscall = pos->key; 4834 i++; 4835 } 4836 assert(i == nr); 4837 4838 qsort(entry, nr, sizeof(*entry), entry_cmp); 4839 return entry; 4840 } 4841 4842 static size_t syscall__dump_stats(struct trace *trace, int e_machine, FILE *fp, 4843 struct hashmap *syscall_stats) 4844 { 4845 size_t printed = 0; 4846 int lines = 0; 4847 struct syscall *sc; 4848 struct syscall_entry *entries; 4849 4850 entries = syscall__sort_stats(syscall_stats); 4851 if (entries == NULL) 4852 return 0; 4853 4854 printed += fprintf(fp, "\n"); 4855 4856 printed += fprintf(fp, " syscall calls errors total min avg max stddev\n"); 4857 printed += fprintf(fp, " (msec) (msec) (msec) (msec) (%%)\n"); 4858 printed += fprintf(fp, " --------------- -------- ------ -------- --------- --------- --------- ------\n"); 4859 4860 for (size_t i = 0; i < syscall_stats->sz; i++) { 4861 struct syscall_entry *entry = &entries[i]; 4862 struct syscall_stats *stats = entry->stats; 4863 4864 if (stats) { 4865 double min = (double)(stats->stats.min) / NSEC_PER_MSEC; 4866 double max = (double)(stats->stats.max) / NSEC_PER_MSEC; 4867 double avg = avg_stats(&stats->stats); 4868 double pct; 4869 u64 n = (u64)stats->stats.n; 4870 4871 pct = avg ? 100.0 * stddev_stats(&stats->stats) / avg : 0.0; 4872 avg /= NSEC_PER_MSEC; 4873 4874 sc = trace__syscall_info(trace, /*evsel=*/NULL, e_machine, entry->syscall); 4875 if (!sc) 4876 continue; 4877 4878 printed += fprintf(fp, " %-15s", sc->name); 4879 printed += fprintf(fp, " %8" PRIu64 " %6" PRIu64 " %9.3f %9.3f %9.3f", 4880 n, stats->nr_failures, entry->msecs, min, avg); 4881 printed += fprintf(fp, " %9.3f %9.2f%%\n", max, pct); 4882 4883 if (trace->errno_summary && stats->nr_failures) { 4884 int e; 4885 4886 for (e = 0; e < stats->max_errno; ++e) { 4887 if (stats->errnos[e] != 0) 4888 fprintf(fp, "\t\t\t\t%s: %d\n", perf_env__arch_strerrno(trace->host->env, e + 1), stats->errnos[e]); 4889 } 4890 } 4891 lines++; 4892 } 4893 4894 if (trace->max_summary && trace->max_summary <= lines) 4895 break; 4896 } 4897 4898 free(entries); 4899 printed += fprintf(fp, "\n\n"); 4900 4901 return printed; 4902 } 4903 4904 static size_t thread__dump_stats(struct thread_trace *ttrace, 4905 struct trace *trace, int e_machine, FILE *fp) 4906 { 4907 return syscall__dump_stats(trace, e_machine, fp, ttrace->syscall_stats); 4908 } 4909 4910 static size_t system__dump_stats(struct trace *trace, int e_machine, FILE *fp) 4911 { 4912 return syscall__dump_stats(trace, e_machine, fp, trace->syscall_stats); 4913 } 4914 4915 static size_t trace__fprintf_thread(FILE *fp, struct thread *thread, struct trace *trace) 4916 { 4917 size_t printed = 0; 4918 struct thread_trace *ttrace = thread__priv(thread); 4919 int e_machine = thread__e_machine(thread, trace->host); 4920 double ratio; 4921 4922 if (ttrace == NULL) 4923 return 0; 4924 4925 ratio = (double)ttrace->nr_events / trace->nr_events * 100.0; 4926 4927 printed += fprintf(fp, " %s (%d), ", thread__comm_str(thread), thread__tid(thread)); 4928 printed += fprintf(fp, "%lu events, ", ttrace->nr_events); 4929 printed += fprintf(fp, "%.1f%%", ratio); 4930 if (ttrace->pfmaj) 4931 printed += fprintf(fp, ", %lu majfaults", ttrace->pfmaj); 4932 if (ttrace->pfmin) 4933 printed += fprintf(fp, ", %lu minfaults", ttrace->pfmin); 4934 if (trace->sched) 4935 printed += fprintf(fp, ", %.3f msec\n", ttrace->runtime_ms); 4936 else if (fputc('\n', fp) != EOF) 4937 ++printed; 4938 4939 printed += thread__dump_stats(ttrace, trace, e_machine, fp); 4940 4941 return printed; 4942 } 4943 4944 static unsigned long thread__nr_events(struct thread_trace *ttrace) 4945 { 4946 return ttrace ? ttrace->nr_events : 0; 4947 } 4948 4949 static int trace_nr_events_cmp(void *priv __maybe_unused, 4950 const struct list_head *la, 4951 const struct list_head *lb) 4952 { 4953 struct thread_list *a = list_entry(la, struct thread_list, list); 4954 struct thread_list *b = list_entry(lb, struct thread_list, list); 4955 unsigned long a_nr_events = thread__nr_events(thread__priv(a->thread)); 4956 unsigned long b_nr_events = thread__nr_events(thread__priv(b->thread)); 4957 4958 if (a_nr_events != b_nr_events) 4959 return a_nr_events < b_nr_events ? -1 : 1; 4960 4961 /* Identical number of threads, place smaller tids first. */ 4962 return thread__tid(a->thread) < thread__tid(b->thread) 4963 ? -1 4964 : (thread__tid(a->thread) > thread__tid(b->thread) ? 1 : 0); 4965 } 4966 4967 static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp) 4968 { 4969 size_t printed = trace__fprintf_summary_header(fp); 4970 LIST_HEAD(threads); 4971 4972 if (machine__thread_list(trace->host, &threads) == 0) { 4973 struct thread_list *pos; 4974 4975 list_sort(NULL, &threads, trace_nr_events_cmp); 4976 4977 list_for_each_entry(pos, &threads, list) 4978 printed += trace__fprintf_thread(fp, pos->thread, trace); 4979 } 4980 thread_list__delete(&threads); 4981 return printed; 4982 } 4983 4984 static size_t trace__fprintf_total_summary(struct trace *trace, FILE *fp) 4985 { 4986 size_t printed = trace__fprintf_summary_header(fp); 4987 4988 printed += fprintf(fp, " total, "); 4989 printed += fprintf(fp, "%lu events", trace->nr_events); 4990 4991 if (trace->pfmaj) 4992 printed += fprintf(fp, ", %lu majfaults", trace->pfmaj); 4993 if (trace->pfmin) 4994 printed += fprintf(fp, ", %lu minfaults", trace->pfmin); 4995 if (trace->sched) 4996 printed += fprintf(fp, ", %.3f msec\n", trace->runtime_ms); 4997 else if (fputc('\n', fp) != EOF) 4998 ++printed; 4999 5000 /* TODO: get all system e_machines. */ 5001 printed += system__dump_stats(trace, EM_HOST, fp); 5002 5003 return printed; 5004 } 5005 5006 static int trace__set_duration(const struct option *opt, const char *str, 5007 int unset __maybe_unused) 5008 { 5009 struct trace *trace = opt->value; 5010 5011 trace->duration_filter = atof(str); 5012 return 0; 5013 } 5014 5015 static int trace__set_filter_pids_from_option(const struct option *opt, const char *str, 5016 int unset __maybe_unused) 5017 { 5018 int ret = -1; 5019 size_t i; 5020 struct trace *trace = opt->value; 5021 /* 5022 * FIXME: introduce a intarray class, plain parse csv and create a 5023 * { int nr, int entries[] } struct... 5024 */ 5025 struct intlist *list = intlist__new(str); 5026 5027 if (list == NULL) 5028 return -1; 5029 5030 i = trace->filter_pids.nr = intlist__nr_entries(list) + 1; 5031 trace->filter_pids.entries = calloc(i, sizeof(pid_t)); 5032 5033 if (trace->filter_pids.entries == NULL) 5034 goto out; 5035 5036 trace->filter_pids.entries[0] = getpid(); 5037 5038 for (i = 1; i < trace->filter_pids.nr; ++i) 5039 trace->filter_pids.entries[i] = intlist__entry(list, i - 1)->i; 5040 5041 intlist__delete(list); 5042 ret = 0; 5043 out: 5044 return ret; 5045 } 5046 5047 static int trace__open_output(struct trace *trace, const char *filename) 5048 { 5049 struct stat st; 5050 5051 if (!stat(filename, &st) && st.st_size) { 5052 char oldname[PATH_MAX]; 5053 5054 scnprintf(oldname, sizeof(oldname), "%s.old", filename); 5055 unlink(oldname); 5056 rename(filename, oldname); 5057 } 5058 5059 trace->output = fopen(filename, "w"); 5060 5061 return trace->output == NULL ? -errno : 0; 5062 } 5063 5064 static int parse_pagefaults(const struct option *opt, const char *str, 5065 int unset __maybe_unused) 5066 { 5067 int *trace_pgfaults = opt->value; 5068 5069 if (strcmp(str, "all") == 0) 5070 *trace_pgfaults |= TRACE_PFMAJ | TRACE_PFMIN; 5071 else if (strcmp(str, "maj") == 0) 5072 *trace_pgfaults |= TRACE_PFMAJ; 5073 else if (strcmp(str, "min") == 0) 5074 *trace_pgfaults |= TRACE_PFMIN; 5075 else 5076 return -1; 5077 5078 return 0; 5079 } 5080 5081 static void evlist__set_default_evsel_handler(struct evlist *evlist, void *handler) 5082 { 5083 struct evsel *evsel; 5084 5085 evlist__for_each_entry(evlist, evsel) { 5086 if (evsel->handler == NULL) 5087 evsel->handler = handler; 5088 } 5089 } 5090 5091 static void evsel__set_syscall_arg_fmt(struct evsel *evsel, const char *name) 5092 { 5093 struct syscall_arg_fmt *fmt = evsel__syscall_arg_fmt(evsel); 5094 5095 if (fmt) { 5096 const struct syscall_fmt *scfmt = syscall_fmt__find(name); 5097 5098 if (scfmt) { 5099 const struct tep_event *tp_format = evsel__tp_format(evsel); 5100 5101 if (tp_format) { 5102 int skip = 0; 5103 5104 if (strcmp(tp_format->format.fields->name, "__syscall_nr") == 0 || 5105 strcmp(tp_format->format.fields->name, "nr") == 0) 5106 ++skip; 5107 5108 memcpy(fmt + skip, scfmt->arg, 5109 (tp_format->format.nr_fields - skip) * sizeof(*fmt)); 5110 } 5111 } 5112 } 5113 } 5114 5115 static int evlist__set_syscall_tp_fields(struct evlist *evlist, bool *use_btf) 5116 { 5117 struct evsel *evsel; 5118 5119 evlist__for_each_entry(evlist, evsel) { 5120 const struct tep_event *tp_format; 5121 5122 if (evsel->priv) 5123 continue; 5124 5125 tp_format = evsel__tp_format(evsel); 5126 if (!tp_format) 5127 continue; 5128 5129 if (strcmp(tp_format->system, "syscalls")) { 5130 evsel__init_tp_arg_scnprintf(evsel, use_btf); 5131 continue; 5132 } 5133 5134 if (evsel__init_syscall_tp(evsel)) 5135 return -1; 5136 5137 if (!strncmp(tp_format->name, "sys_enter_", 10)) { 5138 struct syscall_tp *sc = __evsel__syscall_tp(evsel); 5139 5140 if (__tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64))) 5141 return -1; 5142 5143 evsel__set_syscall_arg_fmt(evsel, 5144 tp_format->name + sizeof("sys_enter_") - 1); 5145 } else if (!strncmp(tp_format->name, "sys_exit_", 9)) { 5146 struct syscall_tp *sc = __evsel__syscall_tp(evsel); 5147 5148 if (__tp_field__init_uint(&sc->ret, sizeof(u64), 5149 sc->id.offset + sizeof(u64), 5150 evsel->needs_swap)) 5151 return -1; 5152 5153 evsel__set_syscall_arg_fmt(evsel, 5154 tp_format->name + sizeof("sys_exit_") - 1); 5155 } 5156 } 5157 5158 return 0; 5159 } 5160 5161 /* 5162 * XXX: Hackish, just splitting the combined -e+--event (syscalls 5163 * (raw_syscalls:{sys_{enter,exit}} + events (tracepoints, HW, SW, etc) to use 5164 * existing facilities unchanged (trace->ev_qualifier + parse_options()). 5165 * 5166 * It'd be better to introduce a parse_options() variant that would return a 5167 * list with the terms it didn't match to an event... 5168 */ 5169 static int trace__parse_events_option(const struct option *opt, const char *str, 5170 int unset __maybe_unused) 5171 { 5172 struct trace *trace = (struct trace *)opt->value; 5173 const char *s; 5174 char *strd, *sep = NULL, *lists[2] = { NULL, NULL, }; 5175 int len = strlen(str) + 1, err = -1, list, idx; 5176 char *strace_groups_dir = system_path(STRACE_GROUPS_DIR); 5177 char group_name[PATH_MAX]; 5178 const struct syscall_fmt *fmt; 5179 5180 if (strace_groups_dir == NULL) 5181 return -1; 5182 5183 s = strd = strdup(str); 5184 if (strd == NULL) 5185 return -1; 5186 5187 if (*s == '!') { 5188 ++s; 5189 trace->not_ev_qualifier = true; 5190 } 5191 5192 while (1) { 5193 if ((sep = strchr(s, ',')) != NULL) 5194 *sep = '\0'; 5195 5196 list = 0; 5197 /* TODO: support for more than just perf binary machine type syscalls. */ 5198 if (syscalltbl__id(EM_HOST, s) >= 0 || 5199 syscalltbl__strglobmatch_first(EM_HOST, s, &idx) >= 0) { 5200 list = 1; 5201 goto do_concat; 5202 } 5203 5204 fmt = syscall_fmt__find_by_alias(s); 5205 if (fmt != NULL) { 5206 list = 1; 5207 s = fmt->name; 5208 } else { 5209 path__join(group_name, sizeof(group_name), strace_groups_dir, s); 5210 if (access(group_name, R_OK) == 0) 5211 list = 1; 5212 } 5213 do_concat: 5214 if (lists[list]) { 5215 sprintf(lists[list] + strlen(lists[list]), ",%s", s); 5216 } else { 5217 lists[list] = malloc(len); 5218 if (lists[list] == NULL) 5219 goto out; 5220 strcpy(lists[list], s); 5221 } 5222 5223 if (!sep) 5224 break; 5225 5226 *sep = ','; 5227 s = sep + 1; 5228 } 5229 5230 if (lists[1] != NULL) { 5231 struct strlist_config slist_config = { 5232 .dirname = strace_groups_dir, 5233 }; 5234 5235 trace->ev_qualifier = strlist__new(lists[1], &slist_config); 5236 if (trace->ev_qualifier == NULL) { 5237 fputs("Not enough memory to parse event qualifier", trace->output); 5238 goto out; 5239 } 5240 5241 if (trace__validate_ev_qualifier(trace)) 5242 goto out; 5243 trace->trace_syscalls = true; 5244 } 5245 5246 err = 0; 5247 5248 if (lists[0]) { 5249 struct parse_events_option_args parse_events_option_args = { 5250 .evlistp = &trace->evlist, 5251 }; 5252 struct option o = { 5253 .value = &parse_events_option_args, 5254 }; 5255 err = parse_events_option(&o, lists[0], 0); 5256 } 5257 out: 5258 free(strace_groups_dir); 5259 free(lists[0]); 5260 free(lists[1]); 5261 free(strd); 5262 5263 return err; 5264 } 5265 5266 static int trace__parse_cgroups(const struct option *opt, const char *str, int unset) 5267 { 5268 struct trace *trace = opt->value; 5269 5270 if (!list_empty(&trace->evlist->core.entries)) { 5271 struct option o = { 5272 .value = &trace->evlist, 5273 }; 5274 return parse_cgroups(&o, str, unset); 5275 } 5276 trace->cgroup = evlist__findnew_cgroup(trace->evlist, str); 5277 5278 return 0; 5279 } 5280 5281 static int trace__parse_summary_mode(const struct option *opt, const char *str, 5282 int unset __maybe_unused) 5283 { 5284 struct trace *trace = opt->value; 5285 5286 if (!strcmp(str, "thread")) { 5287 trace->summary_mode = SUMMARY__BY_THREAD; 5288 } else if (!strcmp(str, "total")) { 5289 trace->summary_mode = SUMMARY__BY_TOTAL; 5290 } else if (!strcmp(str, "cgroup")) { 5291 trace->summary_mode = SUMMARY__BY_CGROUP; 5292 } else { 5293 pr_err("Unknown summary mode: %s\n", str); 5294 return -1; 5295 } 5296 5297 return 0; 5298 } 5299 5300 static int trace__config(const char *var, const char *value, void *arg) 5301 { 5302 struct trace *trace = arg; 5303 int err = 0; 5304 5305 if (!strcmp(var, "trace.add_events")) { 5306 trace->perfconfig_events = strdup(value); 5307 if (trace->perfconfig_events == NULL) { 5308 pr_err("Not enough memory for %s\n", "trace.add_events"); 5309 return -1; 5310 } 5311 } else if (!strcmp(var, "trace.show_timestamp")) { 5312 trace->show_tstamp = perf_config_bool(var, value); 5313 } else if (!strcmp(var, "trace.show_duration")) { 5314 trace->show_duration = perf_config_bool(var, value); 5315 } else if (!strcmp(var, "trace.show_arg_names")) { 5316 trace->show_arg_names = perf_config_bool(var, value); 5317 if (!trace->show_arg_names) 5318 trace->show_zeros = true; 5319 } else if (!strcmp(var, "trace.show_zeros")) { 5320 bool new_show_zeros = perf_config_bool(var, value); 5321 if (!trace->show_arg_names && !new_show_zeros) { 5322 pr_warning("trace.show_zeros has to be set when trace.show_arg_names=no\n"); 5323 goto out; 5324 } 5325 trace->show_zeros = new_show_zeros; 5326 } else if (!strcmp(var, "trace.show_prefix")) { 5327 trace->show_string_prefix = perf_config_bool(var, value); 5328 } else if (!strcmp(var, "trace.no_inherit")) { 5329 trace->opts.no_inherit = perf_config_bool(var, value); 5330 } else if (!strcmp(var, "trace.args_alignment")) { 5331 int args_alignment = 0; 5332 if (perf_config_int(&args_alignment, var, value) == 0) 5333 trace->args_alignment = args_alignment; 5334 } else if (!strcmp(var, "trace.tracepoint_beautifiers")) { 5335 if (strcasecmp(value, "libtraceevent") == 0) 5336 trace->libtraceevent_print = true; 5337 else if (strcasecmp(value, "libbeauty") == 0) 5338 trace->libtraceevent_print = false; 5339 } 5340 out: 5341 return err; 5342 } 5343 5344 static void trace__exit(struct trace *trace) 5345 { 5346 thread__zput(trace->current); 5347 strlist__delete(trace->ev_qualifier); 5348 zfree(&trace->ev_qualifier_ids.entries); 5349 if (trace->syscalls.table) { 5350 for (size_t i = 0; i < trace->syscalls.table_size; i++) 5351 syscall__delete(trace->syscalls.table[i]); 5352 zfree(&trace->syscalls.table); 5353 } 5354 zfree(&trace->perfconfig_events); 5355 evlist__delete(trace->evlist); 5356 trace->evlist = NULL; 5357 ordered_events__free(&trace->oe.data); 5358 #ifdef HAVE_LIBBPF_SUPPORT 5359 btf__free(trace->btf); 5360 trace->btf = NULL; 5361 #endif 5362 } 5363 5364 int cmd_trace(int argc, const char **argv) 5365 { 5366 const char *trace_usage[] = { 5367 "perf trace [<options>] [<command>]", 5368 "perf trace [<options>] -- <command> [<options>]", 5369 "perf trace record [<options>] [<command>]", 5370 "perf trace record [<options>] -- <command> [<options>]", 5371 NULL 5372 }; 5373 struct trace trace = { 5374 .opts = { 5375 .target = { 5376 .uses_mmap = true, 5377 }, 5378 .user_freq = UINT_MAX, 5379 .user_interval = ULLONG_MAX, 5380 .no_buffering = true, 5381 .mmap_pages = UINT_MAX, 5382 }, 5383 .output = stderr, 5384 .show_comm = true, 5385 .show_tstamp = true, 5386 .show_duration = true, 5387 .show_arg_names = true, 5388 .args_alignment = 70, 5389 .trace_syscalls = false, 5390 .kernel_syscallchains = false, 5391 .max_stack = UINT_MAX, 5392 .max_events = ULONG_MAX, 5393 }; 5394 const char *output_name = NULL; 5395 const struct option trace_options[] = { 5396 OPT_CALLBACK('e', "event", &trace, "event", 5397 "event/syscall selector. use 'perf list' to list available events", 5398 trace__parse_events_option), 5399 OPT_CALLBACK(0, "filter", &trace.evlist, "filter", 5400 "event filter", parse_filter), 5401 OPT_BOOLEAN(0, "comm", &trace.show_comm, 5402 "show the thread COMM next to its id"), 5403 OPT_BOOLEAN(0, "tool_stats", &trace.show_tool_stats, "show tool stats"), 5404 OPT_CALLBACK(0, "expr", &trace, "expr", "list of syscalls/events to trace", 5405 trace__parse_events_option), 5406 OPT_STRING('o', "output", &output_name, "file", "output file name"), 5407 OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"), 5408 OPT_STRING('p', "pid", &trace.opts.target.pid, "pid", 5409 "trace events on existing process id"), 5410 OPT_STRING('t', "tid", &trace.opts.target.tid, "tid", 5411 "trace events on existing thread id"), 5412 OPT_CALLBACK(0, "filter-pids", &trace, "CSV list of pids", 5413 "pids to filter (by the kernel)", trace__set_filter_pids_from_option), 5414 OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide, 5415 "system-wide collection from all CPUs"), 5416 OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu", 5417 "list of cpus to monitor"), 5418 OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit, 5419 "child tasks do not inherit counters"), 5420 OPT_CALLBACK('m', "mmap-pages", &trace.opts.mmap_pages, "pages", 5421 "number of mmap data pages", evlist__parse_mmap_pages), 5422 OPT_STRING('u', "uid", &trace.uid_str, "user", "user to profile"), 5423 OPT_CALLBACK(0, "duration", &trace, "float", 5424 "show only events with duration > N.M ms", 5425 trace__set_duration), 5426 OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"), 5427 OPT_INCR('v', "verbose", &verbose, "be more verbose"), 5428 OPT_BOOLEAN('T', "time", &trace.full_time, 5429 "Show full timestamp, not time relative to first start"), 5430 OPT_BOOLEAN(0, "failure", &trace.failure_only, 5431 "Show only syscalls that failed"), 5432 OPT_BOOLEAN('s', "summary", &trace.summary_only, 5433 "Show only syscall summary with statistics"), 5434 OPT_BOOLEAN('S', "with-summary", &trace.summary, 5435 "Show all syscalls and summary with statistics"), 5436 OPT_BOOLEAN(0, "errno-summary", &trace.errno_summary, 5437 "Show errno stats per syscall, use with -s or -S"), 5438 OPT_CALLBACK(0, "summary-mode", &trace, "mode", 5439 "How to show summary: select thread (default), total or cgroup", 5440 trace__parse_summary_mode), 5441 OPT_CALLBACK_DEFAULT('F', "pf", &trace.trace_pgfaults, "all|maj|min", 5442 "Trace pagefaults", parse_pagefaults, "maj"), 5443 OPT_BOOLEAN(0, "syscalls", &trace.trace_syscalls, "Trace syscalls"), 5444 OPT_BOOLEAN('f', "force", &trace.force, "don't complain, do it"), 5445 OPT_CALLBACK(0, "call-graph", &trace.opts, 5446 "record_mode[,record_size]", record_callchain_help, 5447 &record_parse_callchain_opt), 5448 OPT_BOOLEAN(0, "libtraceevent_print", &trace.libtraceevent_print, 5449 "Use libtraceevent to print the tracepoint arguments."), 5450 OPT_BOOLEAN(0, "kernel-syscall-graph", &trace.kernel_syscallchains, 5451 "Show the kernel callchains on the syscall exit path"), 5452 OPT_ULONG(0, "max-events", &trace.max_events, 5453 "Set the maximum number of events to print, exit after that is reached. "), 5454 OPT_UINTEGER(0, "min-stack", &trace.min_stack, 5455 "Set the minimum stack depth when parsing the callchain, " 5456 "anything below the specified depth will be ignored."), 5457 OPT_UINTEGER(0, "max-stack", &trace.max_stack, 5458 "Set the maximum stack depth when parsing the callchain, " 5459 "anything beyond the specified depth will be ignored. " 5460 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)), 5461 OPT_BOOLEAN(0, "sort-events", &trace.sort_events, 5462 "Sort batch of events before processing, use if getting out of order events"), 5463 OPT_BOOLEAN(0, "print-sample", &trace.print_sample, 5464 "print the PERF_RECORD_SAMPLE PERF_SAMPLE_ info, for debugging"), 5465 OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout, 5466 "per thread proc mmap processing timeout in ms"), 5467 OPT_CALLBACK('G', "cgroup", &trace, "name", "monitor event in cgroup name only", 5468 trace__parse_cgroups), 5469 OPT_INTEGER('D', "delay", &trace.opts.target.initial_delay, 5470 "ms to wait before starting measurement after program " 5471 "start"), 5472 OPT_BOOLEAN(0, "force-btf", &trace.force_btf, "Prefer btf_dump general pretty printer" 5473 "to customized ones"), 5474 OPT_BOOLEAN(0, "bpf-summary", &trace.summary_bpf, "Summary syscall stats in BPF"), 5475 OPT_INTEGER(0, "max-summary", &trace.max_summary, 5476 "Max number of entries in the summary."), 5477 OPTS_EVSWITCH(&trace.evswitch), 5478 OPT_END() 5479 }; 5480 bool __maybe_unused max_stack_user_set = true; 5481 bool mmap_pages_user_set = true; 5482 struct evsel *evsel; 5483 const char * const trace_subcommands[] = { "record", NULL }; 5484 int err = -1; 5485 char bf[BUFSIZ]; 5486 struct sigaction sigchld_act; 5487 5488 signal(SIGSEGV, sighandler_dump_stack); 5489 signal(SIGFPE, sighandler_dump_stack); 5490 signal(SIGINT, sighandler_interrupt); 5491 5492 memset(&sigchld_act, 0, sizeof(sigchld_act)); 5493 sigchld_act.sa_flags = SA_SIGINFO; 5494 sigchld_act.sa_sigaction = sighandler_chld; 5495 sigaction(SIGCHLD, &sigchld_act, NULL); 5496 5497 ordered_events__init(&trace.oe.data, ordered_events__deliver_event, &trace); 5498 ordered_events__set_copy_on_queue(&trace.oe.data, true); 5499 5500 trace.evlist = evlist__new(); 5501 5502 if (trace.evlist == NULL) { 5503 pr_err("Not enough memory to run!\n"); 5504 err = -ENOMEM; 5505 goto out; 5506 } 5507 5508 /* 5509 * Parsing .perfconfig may entail creating a BPF event, that may need 5510 * to create BPF maps, so bump RLIM_MEMLOCK as the default 64K setting 5511 * is too small. This affects just this process, not touching the 5512 * global setting. If it fails we'll get something in 'perf trace -v' 5513 * to help diagnose the problem. 5514 */ 5515 rlimit__bump_memlock(); 5516 5517 err = perf_config(trace__config, &trace); 5518 if (err) 5519 goto out; 5520 5521 argc = parse_options_subcommand(argc, argv, trace_options, trace_subcommands, 5522 trace_usage, PARSE_OPT_STOP_AT_NON_OPTION); 5523 5524 /* 5525 * Here we already passed thru trace__parse_events_option() and it has 5526 * already figured out if -e syscall_name, if not but if --event 5527 * foo:bar was used, the user is interested _just_ in those, say, 5528 * tracepoint events, not in the strace-like syscall-name-based mode. 5529 * 5530 * This is important because we need to check if strace-like mode is 5531 * needed to decided if we should filter out the eBPF 5532 * __augmented_syscalls__ code, if it is in the mix, say, via 5533 * .perfconfig trace.add_events, and filter those out. 5534 */ 5535 if (!trace.trace_syscalls && !trace.trace_pgfaults && 5536 trace.evlist->core.nr_entries == 0 /* Was --events used? */) { 5537 trace.trace_syscalls = true; 5538 } 5539 /* 5540 * Now that we have --verbose figured out, lets see if we need to parse 5541 * events from .perfconfig, so that if those events fail parsing, say some 5542 * BPF program fails, then we'll be able to use --verbose to see what went 5543 * wrong in more detail. 5544 */ 5545 if (trace.perfconfig_events != NULL) { 5546 struct parse_events_error parse_err; 5547 5548 parse_events_error__init(&parse_err); 5549 err = parse_events(trace.evlist, trace.perfconfig_events, &parse_err); 5550 if (err) 5551 parse_events_error__print(&parse_err, trace.perfconfig_events); 5552 parse_events_error__exit(&parse_err); 5553 if (err) 5554 goto out; 5555 } 5556 5557 if ((nr_cgroups || trace.cgroup) && !trace.opts.target.system_wide) { 5558 usage_with_options_msg(trace_usage, trace_options, 5559 "cgroup monitoring only available in system-wide mode"); 5560 } 5561 5562 if (!trace.trace_syscalls) 5563 goto skip_augmentation; 5564 5565 if ((argc >= 1) && (strcmp(argv[0], "record") == 0)) { 5566 pr_debug("Syscall augmentation fails with record, disabling augmentation"); 5567 goto skip_augmentation; 5568 } 5569 5570 if (trace.summary_bpf) { 5571 if (!trace.opts.target.system_wide) { 5572 /* TODO: Add filters in the BPF to support other targets. */ 5573 pr_err("Error: --bpf-summary only works for system-wide mode.\n"); 5574 goto out; 5575 } 5576 if (trace.summary_only) 5577 goto skip_augmentation; 5578 } 5579 5580 err = augmented_syscalls__prepare(); 5581 if (err < 0) 5582 goto skip_augmentation; 5583 5584 trace__add_syscall_newtp(&trace); 5585 5586 err = augmented_syscalls__create_bpf_output(trace.evlist); 5587 if (err == 0) 5588 trace.syscalls.events.bpf_output = evlist__last(trace.evlist); 5589 5590 skip_augmentation: 5591 err = -1; 5592 5593 if (trace.trace_pgfaults) { 5594 trace.opts.sample_address = true; 5595 trace.opts.sample_time = true; 5596 } 5597 5598 if (trace.opts.mmap_pages == UINT_MAX) 5599 mmap_pages_user_set = false; 5600 5601 if (trace.max_stack == UINT_MAX) { 5602 trace.max_stack = input_name ? PERF_MAX_STACK_DEPTH : sysctl__max_stack(); 5603 max_stack_user_set = false; 5604 } 5605 5606 #ifdef HAVE_DWARF_UNWIND_SUPPORT 5607 if ((trace.min_stack || max_stack_user_set) && !callchain_param.enabled) { 5608 record_opts__parse_callchain(&trace.opts, &callchain_param, "dwarf", false); 5609 } 5610 #endif 5611 5612 if (callchain_param.enabled) { 5613 if (!mmap_pages_user_set && geteuid() == 0) 5614 trace.opts.mmap_pages = perf_event_mlock_kb_in_pages() * 4; 5615 5616 symbol_conf.use_callchain = true; 5617 } 5618 5619 if (trace.evlist->core.nr_entries > 0) { 5620 bool use_btf = false; 5621 5622 evlist__set_default_evsel_handler(trace.evlist, trace__event_handler); 5623 if (evlist__set_syscall_tp_fields(trace.evlist, &use_btf)) { 5624 perror("failed to set syscalls:* tracepoint fields"); 5625 goto out; 5626 } 5627 5628 if (use_btf) 5629 trace__load_vmlinux_btf(&trace); 5630 } 5631 5632 /* 5633 * If we are augmenting syscalls, then combine what we put in the 5634 * __augmented_syscalls__ BPF map with what is in the 5635 * syscalls:sys_exit_FOO tracepoints, i.e. just like we do without BPF, 5636 * combining raw_syscalls:sys_enter with raw_syscalls:sys_exit. 5637 * 5638 * We'll switch to look at two BPF maps, one for sys_enter and the 5639 * other for sys_exit when we start augmenting the sys_exit paths with 5640 * buffers that are being copied from kernel to userspace, think 'read' 5641 * syscall. 5642 */ 5643 if (trace.syscalls.events.bpf_output) { 5644 evlist__for_each_entry(trace.evlist, evsel) { 5645 bool raw_syscalls_sys_exit = evsel__name_is(evsel, "raw_syscalls:sys_exit"); 5646 5647 if (raw_syscalls_sys_exit) { 5648 trace.raw_augmented_syscalls = true; 5649 goto init_augmented_syscall_tp; 5650 } 5651 5652 if (trace.syscalls.events.bpf_output->priv == NULL && 5653 strstr(evsel__name(evsel), "syscalls:sys_enter")) { 5654 struct evsel *augmented = trace.syscalls.events.bpf_output; 5655 if (evsel__init_augmented_syscall_tp(augmented, evsel) || 5656 evsel__init_augmented_syscall_tp_args(augmented)) 5657 goto out; 5658 /* 5659 * Augmented is __augmented_syscalls__ BPF_OUTPUT event 5660 * Above we made sure we can get from the payload the tp fields 5661 * that we get from syscalls:sys_enter tracefs format file. 5662 */ 5663 augmented->handler = trace__sys_enter; 5664 /* 5665 * Now we do the same for the *syscalls:sys_enter event so that 5666 * if we handle it directly, i.e. if the BPF prog returns 0 so 5667 * as not to filter it, then we'll handle it just like we would 5668 * for the BPF_OUTPUT one: 5669 */ 5670 if (evsel__init_augmented_syscall_tp(evsel, evsel) || 5671 evsel__init_augmented_syscall_tp_args(evsel)) 5672 goto out; 5673 evsel->handler = trace__sys_enter; 5674 } 5675 5676 if (strstarts(evsel__name(evsel), "syscalls:sys_exit_")) { 5677 struct syscall_tp *sc; 5678 init_augmented_syscall_tp: 5679 if (evsel__init_augmented_syscall_tp(evsel, evsel)) 5680 goto out; 5681 sc = __evsel__syscall_tp(evsel); 5682 /* 5683 * For now with BPF raw_augmented we hook into 5684 * raw_syscalls:sys_enter and there we get all 5685 * 6 syscall args plus the tracepoint common 5686 * fields and the syscall_nr (another long). 5687 * So we check if that is the case and if so 5688 * don't look after the sc->args_size but 5689 * always after the full raw_syscalls:sys_enter 5690 * payload, which is fixed. 5691 * 5692 * We'll revisit this later to pass 5693 * s->args_size to the BPF augmenter (now 5694 * tools/perf/examples/bpf/augmented_raw_syscalls.c, 5695 * so that it copies only what we need for each 5696 * syscall, like what happens when we use 5697 * syscalls:sys_enter_NAME, so that we reduce 5698 * the kernel/userspace traffic to just what is 5699 * needed for each syscall. 5700 */ 5701 if (trace.raw_augmented_syscalls) 5702 trace.raw_augmented_syscalls_args_size = (6 + 1) * sizeof(long) + sc->id.offset; 5703 evsel__init_augmented_syscall_tp_ret(evsel); 5704 evsel->handler = trace__sys_exit; 5705 } 5706 } 5707 } 5708 5709 if ((argc >= 1) && (strcmp(argv[0], "record") == 0)) { 5710 err = trace__record(&trace, argc-1, &argv[1]); 5711 goto out; 5712 } 5713 5714 /* Using just --errno-summary will trigger --summary */ 5715 if (trace.errno_summary && !trace.summary && !trace.summary_only) 5716 trace.summary_only = true; 5717 5718 /* summary_only implies summary option, but don't overwrite summary if set */ 5719 if (trace.summary_only) 5720 trace.summary = trace.summary_only; 5721 5722 /* Keep exited threads, otherwise information might be lost for summary */ 5723 if (trace.summary) { 5724 symbol_conf.keep_exited_threads = true; 5725 if (trace.summary_mode == SUMMARY__NONE) 5726 trace.summary_mode = SUMMARY__BY_THREAD; 5727 5728 if (!trace.summary_bpf && trace.summary_mode == SUMMARY__BY_CGROUP) { 5729 pr_err("Error: --summary-mode=cgroup only works with --bpf-summary\n"); 5730 err = -EINVAL; 5731 goto out; 5732 } 5733 } 5734 5735 if (output_name != NULL) { 5736 err = trace__open_output(&trace, output_name); 5737 if (err < 0) { 5738 perror("failed to create output file"); 5739 goto out; 5740 } 5741 } 5742 5743 err = evswitch__init(&trace.evswitch, trace.evlist, stderr); 5744 if (err) 5745 goto out_close; 5746 5747 err = target__validate(&trace.opts.target); 5748 if (err) { 5749 target__strerror(&trace.opts.target, err, bf, sizeof(bf)); 5750 fprintf(trace.output, "%s", bf); 5751 goto out_close; 5752 } 5753 5754 if (trace.uid_str) { 5755 uid_t uid = parse_uid(trace.uid_str); 5756 5757 if (uid == UINT_MAX) { 5758 ui__error("Invalid User: %s", trace.uid_str); 5759 err = -EINVAL; 5760 goto out_close; 5761 } 5762 err = parse_uid_filter(trace.evlist, uid); 5763 if (err) 5764 goto out_close; 5765 5766 trace.opts.target.system_wide = true; 5767 } 5768 5769 if (!argc && target__none(&trace.opts.target)) 5770 trace.opts.target.system_wide = true; 5771 5772 if (input_name) 5773 err = trace__replay(&trace); 5774 else 5775 err = trace__run(&trace, argc, argv); 5776 5777 out_close: 5778 if (output_name != NULL) 5779 fclose(trace.output); 5780 out: 5781 trace__exit(&trace); 5782 augmented_syscalls__cleanup(); 5783 return err; 5784 } 5785