kretprobe_example.c 2.9 KB

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