syscall_tp_kern.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* Copyright (c) 2017 Facebook
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of version 2 of the GNU General Public
  5. * License as published by the Free Software Foundation.
  6. */
  7. #include <uapi/linux/bpf.h>
  8. #include "bpf_helpers.h"
  9. struct syscalls_enter_open_args {
  10. unsigned long long unused;
  11. long syscall_nr;
  12. long filename_ptr;
  13. long flags;
  14. long mode;
  15. };
  16. struct syscalls_exit_open_args {
  17. unsigned long long unused;
  18. long syscall_nr;
  19. long ret;
  20. };
  21. struct bpf_map_def SEC("maps") enter_open_map = {
  22. .type = BPF_MAP_TYPE_ARRAY,
  23. .key_size = sizeof(u32),
  24. .value_size = sizeof(u32),
  25. .max_entries = 1,
  26. };
  27. struct bpf_map_def SEC("maps") exit_open_map = {
  28. .type = BPF_MAP_TYPE_ARRAY,
  29. .key_size = sizeof(u32),
  30. .value_size = sizeof(u32),
  31. .max_entries = 1,
  32. };
  33. static __always_inline void count(void *map)
  34. {
  35. u32 key = 0;
  36. u32 *value, init_val = 1;
  37. value = bpf_map_lookup_elem(map, &key);
  38. if (value)
  39. *value += 1;
  40. else
  41. bpf_map_update_elem(map, &key, &init_val, BPF_NOEXIST);
  42. }
  43. SEC("tracepoint/syscalls/sys_enter_open")
  44. int trace_enter_open(struct syscalls_enter_open_args *ctx)
  45. {
  46. count(&enter_open_map);
  47. return 0;
  48. }
  49. SEC("tracepoint/syscalls/sys_enter_openat")
  50. int trace_enter_open_at(struct syscalls_enter_open_args *ctx)
  51. {
  52. count(&enter_open_map);
  53. return 0;
  54. }
  55. SEC("tracepoint/syscalls/sys_exit_open")
  56. int trace_enter_exit(struct syscalls_exit_open_args *ctx)
  57. {
  58. count(&exit_open_map);
  59. return 0;
  60. }
  61. SEC("tracepoint/syscalls/sys_exit_openat")
  62. int trace_enter_exit_at(struct syscalls_exit_open_args *ctx)
  63. {
  64. count(&exit_open_map);
  65. return 0;
  66. }