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