kprobe_example.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Here's a sample kernel module showing the use of kprobes to dump a
  4. * stack trace and selected registers when kernel_clone() is called.
  5. *
  6. * For more information on theory of operation of kprobes, see
  7. * Documentation/trace/kprobes.rst
  8. *
  9. * You will see the trace data in /var/log/messages and on the console
  10. * whenever kernel_clone() is invoked to create a new process.
  11. */
  12. #define pr_fmt(fmt) "%s: " fmt, __func__
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/kprobes.h>
  16. static char symbol[KSYM_NAME_LEN] = "kernel_clone";
  17. module_param_string(symbol, symbol, KSYM_NAME_LEN, 0644);
  18. /* For each probe you need to allocate a kprobe structure */
  19. static struct kprobe kp = {
  20. .symbol_name = symbol,
  21. };
  22. /* kprobe pre_handler: called just before the probed instruction is executed */
  23. static int __kprobes handler_pre(struct kprobe *p, struct pt_regs *regs)
  24. {
  25. #ifdef CONFIG_X86
  26. pr_info("<%s> p->addr = 0x%p, ip = %lx, flags = 0x%lx\n",
  27. p->symbol_name, p->addr, regs->ip, regs->flags);
  28. #endif
  29. #ifdef CONFIG_PPC
  30. pr_info("<%s> p->addr = 0x%p, nip = 0x%lx, msr = 0x%lx\n",
  31. p->symbol_name, p->addr, regs->nip, regs->msr);
  32. #endif
  33. #ifdef CONFIG_MIPS
  34. pr_info("<%s> p->addr = 0x%p, epc = 0x%lx, status = 0x%lx\n",
  35. p->symbol_name, p->addr, regs->cp0_epc, regs->cp0_status);
  36. #endif
  37. #ifdef CONFIG_ARM64
  38. pr_info("<%s> p->addr = 0x%p, pc = 0x%lx, pstate = 0x%lx\n",
  39. p->symbol_name, p->addr, (long)regs->pc, (long)regs->pstate);
  40. #endif
  41. #ifdef CONFIG_ARM
  42. pr_info("<%s> p->addr = 0x%p, pc = 0x%lx, cpsr = 0x%lx\n",
  43. p->symbol_name, p->addr, (long)regs->ARM_pc, (long)regs->ARM_cpsr);
  44. #endif
  45. #ifdef CONFIG_RISCV
  46. pr_info("<%s> p->addr = 0x%p, pc = 0x%lx, status = 0x%lx\n",
  47. p->symbol_name, p->addr, regs->epc, regs->status);
  48. #endif
  49. #ifdef CONFIG_S390
  50. pr_info("<%s> p->addr, 0x%p, ip = 0x%lx, flags = 0x%lx\n",
  51. p->symbol_name, p->addr, regs->psw.addr, regs->flags);
  52. #endif
  53. /* A dump_stack() here will give a stack backtrace */
  54. return 0;
  55. }
  56. /* kprobe post_handler: called after the probed instruction is executed */
  57. static void __kprobes handler_post(struct kprobe *p, struct pt_regs *regs,
  58. unsigned long flags)
  59. {
  60. #ifdef CONFIG_X86
  61. pr_info("<%s> p->addr = 0x%p, flags = 0x%lx\n",
  62. p->symbol_name, p->addr, regs->flags);
  63. #endif
  64. #ifdef CONFIG_PPC
  65. pr_info("<%s> p->addr = 0x%p, msr = 0x%lx\n",
  66. p->symbol_name, p->addr, regs->msr);
  67. #endif
  68. #ifdef CONFIG_MIPS
  69. pr_info("<%s> p->addr = 0x%p, status = 0x%lx\n",
  70. p->symbol_name, p->addr, regs->cp0_status);
  71. #endif
  72. #ifdef CONFIG_ARM64
  73. pr_info("<%s> p->addr = 0x%p, pstate = 0x%lx\n",
  74. p->symbol_name, p->addr, (long)regs->pstate);
  75. #endif
  76. #ifdef CONFIG_ARM
  77. pr_info("<%s> p->addr = 0x%p, cpsr = 0x%lx\n",
  78. p->symbol_name, p->addr, (long)regs->ARM_cpsr);
  79. #endif
  80. #ifdef CONFIG_RISCV
  81. pr_info("<%s> p->addr = 0x%p, status = 0x%lx\n",
  82. p->symbol_name, p->addr, regs->status);
  83. #endif
  84. #ifdef CONFIG_S390
  85. pr_info("<%s> p->addr, 0x%p, flags = 0x%lx\n",
  86. p->symbol_name, p->addr, regs->flags);
  87. #endif
  88. }
  89. static int __init kprobe_init(void)
  90. {
  91. int ret;
  92. kp.pre_handler = handler_pre;
  93. kp.post_handler = handler_post;
  94. ret = register_kprobe(&kp);
  95. if (ret < 0) {
  96. pr_err("register_kprobe failed, returned %d\n", ret);
  97. return ret;
  98. }
  99. pr_info("Planted kprobe at %p\n", kp.addr);
  100. return 0;
  101. }
  102. static void __exit kprobe_exit(void)
  103. {
  104. unregister_kprobe(&kp);
  105. pr_info("kprobe at %p unregistered\n", kp.addr);
  106. }
  107. module_init(kprobe_init)
  108. module_exit(kprobe_exit)
  109. MODULE_LICENSE("GPL");