syscall_tp_kern.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2017 Facebook
  3. */
  4. #include <uapi/linux/bpf.h>
  5. #include <bpf/bpf_helpers.h>
  6. struct syscalls_enter_open_args {
  7. unsigned long long unused;
  8. long syscall_nr;
  9. long filename_ptr;
  10. long flags;
  11. long mode;
  12. };
  13. struct syscalls_exit_open_args {
  14. unsigned long long unused;
  15. long syscall_nr;
  16. long ret;
  17. };
  18. struct {
  19. __uint(type, BPF_MAP_TYPE_ARRAY);
  20. __type(key, u32);
  21. __type(value, u32);
  22. __uint(max_entries, 1);
  23. } enter_open_map SEC(".maps");
  24. struct {
  25. __uint(type, BPF_MAP_TYPE_ARRAY);
  26. __type(key, u32);
  27. __type(value, u32);
  28. __uint(max_entries, 1);
  29. } exit_open_map SEC(".maps");
  30. static __always_inline void count(void *map)
  31. {
  32. u32 key = 0;
  33. u32 *value, init_val = 1;
  34. value = bpf_map_lookup_elem(map, &key);
  35. if (value)
  36. *value += 1;
  37. else
  38. bpf_map_update_elem(map, &key, &init_val, BPF_NOEXIST);
  39. }
  40. SEC("tracepoint/syscalls/sys_enter_open")
  41. int trace_enter_open(struct syscalls_enter_open_args *ctx)
  42. {
  43. count(&enter_open_map);
  44. return 0;
  45. }
  46. SEC("tracepoint/syscalls/sys_enter_openat")
  47. int trace_enter_open_at(struct syscalls_enter_open_args *ctx)
  48. {
  49. count(&enter_open_map);
  50. return 0;
  51. }
  52. SEC("tracepoint/syscalls/sys_exit_open")
  53. int trace_enter_exit(struct syscalls_exit_open_args *ctx)
  54. {
  55. count(&exit_open_map);
  56. return 0;
  57. }
  58. SEC("tracepoint/syscalls/sys_exit_openat")
  59. int trace_enter_exit_at(struct syscalls_exit_open_args *ctx)
  60. {
  61. count(&exit_open_map);
  62. return 0;
  63. }