test_overhead_kprobe_kern.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* Copyright (c) 2016 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 <linux/version.h>
  8. #include <linux/ptrace.h>
  9. #include <uapi/linux/bpf.h>
  10. #include <bpf/bpf_helpers.h>
  11. #include <bpf/bpf_tracing.h>
  12. #define _(P) \
  13. ({ \
  14. typeof(P) val = 0; \
  15. bpf_probe_read_kernel(&val, sizeof(val), &(P)); \
  16. val; \
  17. })
  18. SEC("kprobe/__set_task_comm")
  19. int prog(struct pt_regs *ctx)
  20. {
  21. struct signal_struct *signal;
  22. struct task_struct *tsk;
  23. char oldcomm[16] = {};
  24. char newcomm[16] = {};
  25. u16 oom_score_adj;
  26. u32 pid;
  27. tsk = (void *)PT_REGS_PARM1(ctx);
  28. pid = _(tsk->pid);
  29. bpf_probe_read_kernel(oldcomm, sizeof(oldcomm), &tsk->comm);
  30. bpf_probe_read_kernel(newcomm, sizeof(newcomm),
  31. (void *)PT_REGS_PARM2(ctx));
  32. signal = _(tsk->signal);
  33. oom_score_adj = _(signal->oom_score_adj);
  34. return 0;
  35. }
  36. SEC("kprobe/urandom_read")
  37. int prog2(struct pt_regs *ctx)
  38. {
  39. return 0;
  40. }
  41. char _license[] SEC("license") = "GPL";
  42. u32 _version SEC("version") = LINUX_VERSION_CODE;