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