fprobe_example.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 int sample_entry_handler(struct fprobe *fp, unsigned long ip,
  42. unsigned long ret_ip,
  43. struct ftrace_regs *fregs, void *data)
  44. {
  45. if (use_trace)
  46. /*
  47. * This is just an example, no kernel code should call
  48. * trace_printk() except when actively debugging.
  49. */
  50. trace_printk("Enter <%pS> ip = 0x%p\n", (void *)ip, (void *)ip);
  51. else
  52. pr_info("Enter <%pS> ip = 0x%p\n", (void *)ip, (void *)ip);
  53. nhit++;
  54. if (stackdump)
  55. show_backtrace();
  56. return 0;
  57. }
  58. static void sample_exit_handler(struct fprobe *fp, unsigned long ip,
  59. unsigned long ret_ip, struct ftrace_regs *regs,
  60. void *data)
  61. {
  62. unsigned long rip = ret_ip;
  63. if (use_trace)
  64. /*
  65. * This is just an example, no kernel code should call
  66. * trace_printk() except when actively debugging.
  67. */
  68. trace_printk("Return from <%pS> ip = 0x%p to rip = 0x%p (%pS)\n",
  69. (void *)ip, (void *)ip, (void *)rip, (void *)rip);
  70. else
  71. pr_info("Return from <%pS> ip = 0x%p to rip = 0x%p (%pS)\n",
  72. (void *)ip, (void *)ip, (void *)rip, (void *)rip);
  73. nhit++;
  74. if (stackdump)
  75. show_backtrace();
  76. }
  77. static int __init fprobe_init(void)
  78. {
  79. char *p, *symbuf = NULL;
  80. const char **syms;
  81. int ret, count, i;
  82. sample_probe.entry_handler = sample_entry_handler;
  83. sample_probe.exit_handler = sample_exit_handler;
  84. if (strchr(symbol, '*')) {
  85. /* filter based fprobe */
  86. ret = register_fprobe(&sample_probe, symbol,
  87. nosymbol[0] == '\0' ? NULL : nosymbol);
  88. goto out;
  89. } else if (!strchr(symbol, ',')) {
  90. symbuf = symbol;
  91. ret = register_fprobe_syms(&sample_probe, (const char **)&symbuf, 1);
  92. goto out;
  93. }
  94. /* Comma separated symbols */
  95. symbuf = kstrdup(symbol, GFP_KERNEL);
  96. if (!symbuf)
  97. return -ENOMEM;
  98. p = symbuf;
  99. count = 1;
  100. while ((p = strchr(++p, ',')) != NULL)
  101. count++;
  102. pr_info("%d symbols found\n", count);
  103. syms = kcalloc(count, sizeof(char *), GFP_KERNEL);
  104. if (!syms) {
  105. kfree(symbuf);
  106. return -ENOMEM;
  107. }
  108. p = symbuf;
  109. for (i = 0; i < count; i++)
  110. syms[i] = strsep(&p, ",");
  111. ret = register_fprobe_syms(&sample_probe, syms, count);
  112. kfree(syms);
  113. kfree(symbuf);
  114. out:
  115. if (ret < 0)
  116. pr_err("register_fprobe failed, returned %d\n", ret);
  117. else
  118. pr_info("Planted fprobe at %s\n", symbol);
  119. return ret;
  120. }
  121. static void __exit fprobe_exit(void)
  122. {
  123. unregister_fprobe(&sample_probe);
  124. pr_info("fprobe at %s unregistered. %ld times hit, %ld times missed\n",
  125. symbol, nhit, sample_probe.nmissed);
  126. }
  127. module_init(fprobe_init)
  128. module_exit(fprobe_exit)
  129. MODULE_DESCRIPTION("sample kernel module showing the use of fprobe");
  130. MODULE_LICENSE("GPL");