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