augmented_syscalls.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Augment the openat syscall with the contents of the filename pointer argument.
  4. *
  5. * Test it with:
  6. *
  7. * perf trace -e tools/perf/examples/bpf/augmented_syscalls.c cat /etc/passwd > /dev/null
  8. *
  9. * It'll catch some openat syscalls related to the dynamic linked and
  10. * the last one should be the one for '/etc/passwd'.
  11. *
  12. * This matches what is marshalled into the raw_syscall:sys_enter payload
  13. * expected by the 'perf trace' beautifiers, and can be used by them unmodified,
  14. * which will be done as that feature is implemented in the next csets, for now
  15. * it will appear in a dump done by the default tracepoint handler in 'perf trace',
  16. * that uses bpf_output__fprintf() to just dump those contents, as done with
  17. * the bpf-output event associated with the __bpf_output__ map declared in
  18. * tools/perf/include/bpf/stdio.h.
  19. */
  20. #include <stdio.h>
  21. struct bpf_map SEC("maps") __augmented_syscalls__ = {
  22. .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
  23. .key_size = sizeof(int),
  24. .value_size = sizeof(u32),
  25. .max_entries = __NR_CPUS__,
  26. };
  27. struct syscall_enter_openat_args {
  28. unsigned long long common_tp_fields;
  29. long syscall_nr;
  30. long dfd;
  31. char *filename_ptr;
  32. long flags;
  33. long mode;
  34. };
  35. struct augmented_enter_openat_args {
  36. struct syscall_enter_openat_args args;
  37. char filename[64];
  38. };
  39. int syscall_enter(openat)(struct syscall_enter_openat_args *args)
  40. {
  41. struct augmented_enter_openat_args augmented_args;
  42. probe_read(&augmented_args.args, sizeof(augmented_args.args), args);
  43. probe_read_str(&augmented_args.filename, sizeof(augmented_args.filename), args->filename_ptr);
  44. perf_event_output(args, &__augmented_syscalls__, BPF_F_CURRENT_CPU,
  45. &augmented_args, sizeof(augmented_args));
  46. return 1;
  47. }
  48. license(GPL);