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