jprobe_example.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Here's a sample kernel module showing the use of jprobes to dump
  3. * the arguments of _do_fork().
  4. *
  5. * For more information on theory of operation of jprobes, see
  6. * Documentation/kprobes.txt
  7. *
  8. * Build and insert the kernel module as done in the kprobe example.
  9. * You will see the trace data in /var/log/messages and on the
  10. * console whenever _do_fork() is invoked to create a new process.
  11. * (Some messages may be suppressed if syslogd is configured to
  12. * eliminate duplicate messages.)
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/kprobes.h>
  17. /*
  18. * Jumper probe for _do_fork.
  19. * Mirror principle enables access to arguments of the probed routine
  20. * from the probe handler.
  21. */
  22. /* Proxy routine having the same arguments as actual _do_fork() routine */
  23. static long j_do_fork(unsigned long clone_flags, unsigned long stack_start,
  24. unsigned long stack_size, int __user *parent_tidptr,
  25. int __user *child_tidptr, unsigned long tls)
  26. {
  27. pr_info("jprobe: clone_flags = 0x%lx, stack_start = 0x%lx "
  28. "stack_size = 0x%lx\n", clone_flags, stack_start, stack_size);
  29. /* Always end with a call to jprobe_return(). */
  30. jprobe_return();
  31. return 0;
  32. }
  33. static struct jprobe my_jprobe = {
  34. .entry = j_do_fork,
  35. .kp = {
  36. .symbol_name = "_do_fork",
  37. },
  38. };
  39. static int __init jprobe_init(void)
  40. {
  41. int ret;
  42. ret = register_jprobe(&my_jprobe);
  43. if (ret < 0) {
  44. pr_err("register_jprobe failed, returned %d\n", ret);
  45. return -1;
  46. }
  47. pr_info("Planted jprobe at %p, handler addr %p\n",
  48. my_jprobe.kp.addr, my_jprobe.entry);
  49. return 0;
  50. }
  51. static void __exit jprobe_exit(void)
  52. {
  53. unregister_jprobe(&my_jprobe);
  54. pr_info("jprobe at %p unregistered\n", my_jprobe.kp.addr);
  55. }
  56. module_init(jprobe_init)
  57. module_exit(jprobe_exit)
  58. MODULE_LICENSE("GPL");