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. static char func_name[NAME_MAX] = "do_fork";
  26. module_param_string(func, func_name, NAME_MAX, S_IRUGO);
  27. MODULE_PARM_DESC(func, "Function to kretprobe; this module will report the"
  28. " function's execution time");
  29. /* per-instance private data */
  30. struct my_data {
  31. ktime_t entry_stamp;
  32. };
  33. /* Here we use the entry_hanlder to timestamp function entry */
  34. static int entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
  35. {
  36. struct my_data *data;
  37. if (!current->mm)
  38. return 1; /* Skip kernel threads */
  39. data = (struct my_data *)ri->data;
  40. data->entry_stamp = ktime_get();
  41. return 0;
  42. }
  43. /*
  44. * Return-probe handler: Log the return value and duration. Duration may turn
  45. * out to be zero consistently, depending upon the granularity of time
  46. * accounting on the platform.
  47. */
  48. static int ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
  49. {
  50. int retval = regs_return_value(regs);
  51. struct my_data *data = (struct my_data *)ri->data;
  52. s64 delta;
  53. ktime_t now;
  54. now = ktime_get();
  55. delta = ktime_to_ns(ktime_sub(now, data->entry_stamp));
  56. printk(KERN_INFO "%s returned %d and took %lld ns to execute\n",
  57. func_name, retval, (long long)delta);
  58. return 0;
  59. }
  60. static struct kretprobe my_kretprobe = {
  61. .handler = ret_handler,
  62. .entry_handler = entry_handler,
  63. .data_size = sizeof(struct my_data),
  64. /* Probe up to 20 instances concurrently. */
  65. .maxactive = 20,
  66. };
  67. static int __init kretprobe_init(void)
  68. {
  69. int ret;
  70. my_kretprobe.kp.symbol_name = func_name;
  71. ret = register_kretprobe(&my_kretprobe);
  72. if (ret < 0) {
  73. printk(KERN_INFO "register_kretprobe failed, returned %d\n",
  74. ret);
  75. return -1;
  76. }
  77. printk(KERN_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. printk(KERN_INFO "kretprobe at %p unregistered\n",
  85. my_kretprobe.kp.addr);
  86. /* nmissed > 0 suggests that maxactive was set too low. */
  87. printk(KERN_INFO "Missed probing %d instances of %s\n",
  88. my_kretprobe.nmissed, my_kretprobe.kp.symbol_name);
  89. }
  90. module_init(kretprobe_init)
  91. module_exit(kretprobe_exit)
  92. MODULE_LICENSE("GPL");