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