kprobe_example.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * NOTE: This example is works on x86 and powerpc.
  3. * Here's a sample kernel module showing the use of kprobes to dump a
  4. * stack trace and selected registers when _do_fork() is called.
  5. *
  6. * For more information on theory of operation of kprobes, see
  7. * Documentation/kprobes.txt
  8. *
  9. * You will see the trace data in /var/log/messages and on the console
  10. * whenever _do_fork() is invoked to create a new process.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/kprobes.h>
  15. #define MAX_SYMBOL_LEN 64
  16. static char symbol[MAX_SYMBOL_LEN] = "_do_fork";
  17. module_param_string(symbol, symbol, sizeof(symbol), 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 handler_pre(struct kprobe *p, struct pt_regs *regs)
  24. {
  25. #ifdef CONFIG_X86
  26. pr_info("<%s> pre_handler: 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> pre_handler: 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> pre_handler: 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_TILEGX
  38. pr_info("<%s> pre_handler: p->addr = 0x%p, pc = 0x%lx, ex1 = 0x%lx\n",
  39. p->symbol_name, p->addr, regs->pc, regs->ex1);
  40. #endif
  41. #ifdef CONFIG_ARM64
  42. pr_info("<%s> pre_handler: p->addr = 0x%p, pc = 0x%lx,"
  43. " pstate = 0x%lx\n",
  44. p->symbol_name, p->addr, (long)regs->pc, (long)regs->pstate);
  45. #endif
  46. /* A dump_stack() here will give a stack backtrace */
  47. return 0;
  48. }
  49. /* kprobe post_handler: called after the probed instruction is executed */
  50. static void handler_post(struct kprobe *p, struct pt_regs *regs,
  51. unsigned long flags)
  52. {
  53. #ifdef CONFIG_X86
  54. pr_info("<%s> post_handler: p->addr = 0x%p, flags = 0x%lx\n",
  55. p->symbol_name, p->addr, regs->flags);
  56. #endif
  57. #ifdef CONFIG_PPC
  58. pr_info("<%s> post_handler: p->addr = 0x%p, msr = 0x%lx\n",
  59. p->symbol_name, p->addr, regs->msr);
  60. #endif
  61. #ifdef CONFIG_MIPS
  62. pr_info("<%s> post_handler: p->addr = 0x%p, status = 0x%lx\n",
  63. p->symbol_name, p->addr, regs->cp0_status);
  64. #endif
  65. #ifdef CONFIG_TILEGX
  66. pr_info("<%s> post_handler: p->addr = 0x%p, ex1 = 0x%lx\n",
  67. p->symbol_name, p->addr, regs->ex1);
  68. #endif
  69. #ifdef CONFIG_ARM64
  70. pr_info("<%s> post_handler: p->addr = 0x%p, pstate = 0x%lx\n",
  71. p->symbol_name, p->addr, (long)regs->pstate);
  72. #endif
  73. }
  74. /*
  75. * fault_handler: this is called if an exception is generated for any
  76. * instruction within the pre- or post-handler, or when Kprobes
  77. * single-steps the probed instruction.
  78. */
  79. static int handler_fault(struct kprobe *p, struct pt_regs *regs, int trapnr)
  80. {
  81. pr_info("fault_handler: p->addr = 0x%p, trap #%dn", p->addr, trapnr);
  82. /* Return 0 because we don't handle the fault. */
  83. return 0;
  84. }
  85. static int __init kprobe_init(void)
  86. {
  87. int ret;
  88. kp.pre_handler = handler_pre;
  89. kp.post_handler = handler_post;
  90. kp.fault_handler = handler_fault;
  91. ret = register_kprobe(&kp);
  92. if (ret < 0) {
  93. pr_err("register_kprobe failed, returned %d\n", ret);
  94. return ret;
  95. }
  96. pr_info("Planted kprobe at %p\n", kp.addr);
  97. return 0;
  98. }
  99. static void __exit kprobe_exit(void)
  100. {
  101. unregister_kprobe(&kp);
  102. pr_info("kprobe at %p unregistered\n", kp.addr);
  103. }
  104. module_init(kprobe_init)
  105. module_exit(kprobe_exit)
  106. MODULE_LICENSE("GPL");