kfree_tracer.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // SPDX-License-Identifier: GPL-2.0+
  2. #include "kfree_tracer.h"
  3. #include "asm/atomic.h"
  4. #include <linux/types.h>
  5. #include <linux/kprobes.h>
  6. #include "../ktracer.h"
  7. static int kfree_entry_handler(struct kretprobe_instance *instance,
  8. struct pt_regs *regs)
  9. {
  10. struct hlist_head *head_proc;
  11. struct hlist_head *head_addr;
  12. struct hlist_node *i, *j;
  13. struct tracer_data *data_proc;
  14. struct addr_data *data_addr;
  15. unsigned long addr;
  16. pid_t tgid;
  17. addr = regs->ax;
  18. tgid = current->tgid;
  19. head_proc = &procs_table[hash_min(tgid, HASH_BITS(procs_table))];
  20. if (head_proc->first == NULL)
  21. return HANDLER_STOP;
  22. hlist_for_each_entry_safe(data_proc, i, head_proc, node) {
  23. if (data_proc->tgid == tgid) {
  24. arch_atomic_inc(&data_proc->kfree_data.calls);
  25. head_addr = &addr_table[hash_min(
  26. tgid, HASH_BITS(addr_table))];
  27. if (head_addr->first == NULL)
  28. return HANDLER_STOP;
  29. hlist_for_each_entry_safe(data_addr, j, head_addr,
  30. node) {
  31. if (data_addr->tgid == tgid &&
  32. data_addr->addr == addr) {
  33. arch_atomic_add(
  34. data_addr->size,
  35. &data_proc->kfree_data.mem);
  36. return HANDLER_CONTINUE;
  37. }
  38. }
  39. }
  40. }
  41. return HANDLER_STOP;
  42. }
  43. struct kretprobe kfree_probe = { .entry_handler = kfree_entry_handler,
  44. .kp.symbol_name = KFREE_TRACER_SYMBOL_NAME,
  45. .maxactive = MAX_ACTIVE };