kretprobe_example.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * kretprobe_example.c
  4. *
  5. * Here's a sample kernel module showing the use of return probes to
  6. * report the return value and total time taken for probed function
  7. * to run.
  8. *
  9. * usage: insmod kretprobe_example.ko func=<func_name>
  10. *
  11. * If no func_name is specified, kernel_clone is instrumented
  12. *
  13. * For more information on theory of operation of kretprobes, see
  14. * Documentation/trace/kprobes.rst
  15. *
  16. * Build and insert the kernel module as done in the kprobe example.
  17. * You will see the trace data in /var/log/messages and on the console
  18. * whenever the probed function returns. (Some messages may be suppressed
  19. * if syslogd is configured to eliminate duplicate messages.)
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/kprobes.h>
  24. #include <linux/ktime.h>
  25. #include <linux/limits.h>
  26. #include <linux/sched.h>
  27. static char func_name[NAME_MAX] = "kernel_clone";
  28. module_param_string(func, func_name, NAME_MAX, S_IRUGO);
  29. MODULE_PARM_DESC(func, "Function to kretprobe; this module will report the"
  30. " function's execution time");
  31. /* per-instance private data */
  32. struct my_data {
  33. ktime_t entry_stamp;
  34. };
  35. /* Here we use the entry_hanlder to timestamp function entry */
  36. static int entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
  37. {
  38. struct my_data *data;
  39. if (!current->mm)
  40. return 1; /* Skip kernel threads */
  41. data = (struct my_data *)ri->data;
  42. data->entry_stamp = ktime_get();
  43. return 0;
  44. }
  45. NOKPROBE_SYMBOL(entry_handler);
  46. /*
  47. * Return-probe handler: Log the return value and duration. Duration may turn
  48. * out to be zero consistently, depending upon the granularity of time
  49. * accounting on the platform.
  50. */
  51. static int ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
  52. {
  53. unsigned long retval = regs_return_value(regs);
  54. struct my_data *data = (struct my_data *)ri->data;
  55. s64 delta;
  56. ktime_t now;
  57. now = ktime_get();
  58. delta = ktime_to_ns(ktime_sub(now, data->entry_stamp));
  59. pr_info("%s returned %lu and took %lld ns to execute\n",
  60. func_name, retval, (long long)delta);
  61. return 0;
  62. }
  63. NOKPROBE_SYMBOL(ret_handler);
  64. static struct kretprobe my_kretprobe = {
  65. .handler = ret_handler,
  66. .entry_handler = entry_handler,
  67. .data_size = sizeof(struct my_data),
  68. /* Probe up to 20 instances concurrently. */
  69. .maxactive = 20,
  70. };
  71. static int __init kretprobe_init(void)
  72. {
  73. int ret;
  74. my_kretprobe.kp.symbol_name = func_name;
  75. ret = register_kretprobe(&my_kretprobe);
  76. if (ret < 0) {
  77. pr_err("register_kretprobe failed, returned %d\n", ret);
  78. return ret;
  79. }
  80. pr_info("Planted return probe at %s: %p\n",
  81. my_kretprobe.kp.symbol_name, my_kretprobe.kp.addr);
  82. return 0;
  83. }
  84. static void __exit kretprobe_exit(void)
  85. {
  86. unregister_kretprobe(&my_kretprobe);
  87. pr_info("kretprobe at %p unregistered\n", my_kretprobe.kp.addr);
  88. /* nmissed > 0 suggests that maxactive was set too low. */
  89. pr_info("Missed probing %d instances of %s\n",
  90. my_kretprobe.nmissed, my_kretprobe.kp.symbol_name);
  91. }
  92. module_init(kretprobe_init)
  93. module_exit(kretprobe_exit)
  94. MODULE_LICENSE("GPL");