test_overhead_kprobe_kern.c 1.4 KB

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