ktracer.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. #include "linux/rcupdate.h"
  3. #include "linux/sched.h"
  4. #ifndef KTRACER_H__
  5. #define KTRACER_H__ 1
  6. #include <linux/types.h>
  7. #include <linux/kprobes.h>
  8. #include <linux/hashtable.h>
  9. #include <linux/stddef.h>
  10. #include <asm/atomic.h>
  11. #include "kdata.h"
  12. #include "mem/mem_data.h"
  13. #define MAX_ACTIVE 32
  14. #define PROC_MODE 0644
  15. #define PROCS_TABLE_SIZE 10
  16. #define PRINT_FORMAT "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n"
  17. #define PRINT_HASH_FORMAT "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n"
  18. #define PRINT_PID "PID"
  19. #define PRINT_KMALLOC "kmalloc"
  20. #define PRINT_KFREE "kfree"
  21. #define PRINT_KMALLOC_MEM "kmalloc_mem"
  22. #define PRINT_KFREE_MEM "kfree_mem"
  23. #define PRINT_SCHED "sched"
  24. #define PRINT_UP "up"
  25. #define PRINT_DOWN "down"
  26. #define PRINT_LOCK "lock"
  27. #define PRINT_UNLOCK "unlock"
  28. #define HANDLER_CONTINUE 0
  29. #define HANDLER_STOP 1
  30. struct tracer_data {
  31. struct hlist_node node;
  32. struct mem_data kmalloc_data;
  33. struct mem_data kfree_data;
  34. atomic_t sched_calls;
  35. atomic_t lock_calls;
  36. atomic_t unlock_calls;
  37. atomic_t up_calls;
  38. atomic_t down_calls;
  39. pid_t tgid;
  40. };
  41. static inline int tracer_update_calls(struct kretprobe_instance *instance,
  42. ssize_t displacement)
  43. {
  44. struct tracer_data *data;
  45. struct hlist_head *head;
  46. struct hlist_node *tmp;
  47. pid_t pid;
  48. pid = instance->task->tgid;
  49. head = &procs_table[hash_min(pid, HASH_BITS(procs_table))];
  50. hlist_for_each_entry_safe(data, tmp, head, node) {
  51. if (pid == data->tgid) {
  52. arch_atomic_inc(
  53. (atomic_t *)((ssize_t)data + displacement));
  54. return HANDLER_CONTINUE;
  55. }
  56. }
  57. return HANDLER_STOP;
  58. }
  59. static inline struct task_struct *get_proc(pid_t pid)
  60. {
  61. struct pid *pid_struct;
  62. struct task_struct *task;
  63. rcu_read_lock();
  64. pid_struct = find_get_pid(pid);
  65. task = pid_task(pid_struct, PIDTYPE_TGID);
  66. rcu_read_unlock();
  67. return task;
  68. }
  69. #endif // KTRACER_H__