fprobe_example.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Here's a sample kernel module showing the use of fprobe 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/fprobe.h>
  16. #include <linux/sched/debug.h>
  17. #include <linux/slab.h>
  18. #define BACKTRACE_DEPTH 16
  19. #define MAX_SYMBOL_LEN 4096
  20. static struct fprobe sample_probe;
  21. static unsigned long nhit;
  22. static char symbol[MAX_SYMBOL_LEN] = "kernel_clone";
  23. module_param_string(symbol, symbol, sizeof(symbol), 0644);
  24. MODULE_PARM_DESC(symbol, "Probed symbol(s), given by comma separated symbols or a wildcard pattern.");
  25. static char nosymbol[MAX_SYMBOL_LEN] = "";
  26. module_param_string(nosymbol, nosymbol, sizeof(nosymbol), 0644);
  27. MODULE_PARM_DESC(nosymbol, "Not-probed symbols, given by a wildcard pattern.");
  28. static bool stackdump = true;
  29. module_param(stackdump, bool, 0644);
  30. MODULE_PARM_DESC(stackdump, "Enable stackdump.");
  31. static bool use_trace = false;
  32. module_param(use_trace, bool, 0644);
  33. MODULE_PARM_DESC(use_trace, "Use trace_printk instead of printk. This is only for debugging.");
  34. static void show_backtrace(void)
  35. {
  36. unsigned long stacks[BACKTRACE_DEPTH];
  37. unsigned int len;
  38. len = stack_trace_save(stacks, BACKTRACE_DEPTH, 2);
  39. stack_trace_print(stacks, len, 24);
  40. }
  41. static void sample_entry_handler(struct fprobe *fp, unsigned long ip,
  42. struct pt_regs *regs, void *data)
  43. {
  44. if (use_trace)
  45. /*
  46. * This is just an example, no kernel code should call
  47. * trace_printk() except when actively debugging.
  48. */
  49. trace_printk("Enter <%pS> ip = 0x%p\n", (void *)ip, (void *)ip);
  50. else
  51. pr_info("Enter <%pS> ip = 0x%p\n", (void *)ip, (void *)ip);
  52. nhit++;
  53. if (stackdump)
  54. show_backtrace();
  55. }
  56. static void sample_exit_handler(struct fprobe *fp, unsigned long ip, struct pt_regs *regs,
  57. void *data)
  58. {
  59. unsigned long rip = instruction_pointer(regs);
  60. if (use_trace)
  61. /*
  62. * This is just an example, no kernel code should call
  63. * trace_printk() except when actively debugging.
  64. */
  65. trace_printk("Return from <%pS> ip = 0x%p to rip = 0x%p (%pS)\n",
  66. (void *)ip, (void *)ip, (void *)rip, (void *)rip);
  67. else
  68. pr_info("Return from <%pS> ip = 0x%p to rip = 0x%p (%pS)\n",
  69. (void *)ip, (void *)ip, (void *)rip, (void *)rip);
  70. nhit++;
  71. if (stackdump)
  72. show_backtrace();
  73. }
  74. static int __init fprobe_init(void)
  75. {
  76. char *p, *symbuf = NULL;
  77. const char **syms;
  78. int ret, count, i;
  79. sample_probe.entry_handler = sample_entry_handler;
  80. sample_probe.exit_handler = sample_exit_handler;
  81. if (strchr(symbol, '*')) {
  82. /* filter based fprobe */
  83. ret = register_fprobe(&sample_probe, symbol,
  84. nosymbol[0] == '\0' ? NULL : nosymbol);
  85. goto out;
  86. } else if (!strchr(symbol, ',')) {
  87. symbuf = symbol;
  88. ret = register_fprobe_syms(&sample_probe, (const char **)&symbuf, 1);
  89. goto out;
  90. }
  91. /* Comma separated symbols */
  92. symbuf = kstrdup(symbol, GFP_KERNEL);
  93. if (!symbuf)
  94. return -ENOMEM;
  95. p = symbuf;
  96. count = 1;
  97. while ((p = strchr(++p, ',')) != NULL)
  98. count++;
  99. pr_info("%d symbols found\n", count);
  100. syms = kcalloc(count, sizeof(char *), GFP_KERNEL);
  101. if (!syms) {
  102. kfree(symbuf);
  103. return -ENOMEM;
  104. }
  105. p = symbuf;
  106. for (i = 0; i < count; i++)
  107. syms[i] = strsep(&p, ",");
  108. ret = register_fprobe_syms(&sample_probe, syms, count);
  109. kfree(syms);
  110. kfree(symbuf);
  111. out:
  112. if (ret < 0)
  113. pr_err("register_fprobe failed, returned %d\n", ret);
  114. else
  115. pr_info("Planted fprobe at %s\n", symbol);
  116. return ret;
  117. }
  118. static void __exit fprobe_exit(void)
  119. {
  120. unregister_fprobe(&sample_probe);
  121. pr_info("fprobe at %s unregistered. %ld times hit, %ld times missed\n",
  122. symbol, nhit, sample_probe.nmissed);
  123. }
  124. module_init(fprobe_init)
  125. module_exit(fprobe_exit)
  126. MODULE_LICENSE("GPL");