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