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