kprobes.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #ifndef _LINUX_KPROBES_H
  2. #define _LINUX_KPROBES_H
  3. /*
  4. * Kernel Probes (KProbes)
  5. * include/linux/kprobes.h
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. *
  21. * Copyright (C) IBM Corporation, 2002, 2004
  22. *
  23. * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
  24. * Probes initial implementation ( includes suggestions from
  25. * Rusty Russell).
  26. * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
  27. * interface to access function arguments.
  28. * 2005-May Hien Nguyen <hien@us.ibm.com> and Jim Keniston
  29. * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
  30. * <prasanna@in.ibm.com> added function-return probes.
  31. */
  32. #include <linux/config.h>
  33. #include <linux/list.h>
  34. #include <linux/notifier.h>
  35. #include <linux/smp.h>
  36. #include <asm/kprobes.h>
  37. /* kprobe_status settings */
  38. #define KPROBE_HIT_ACTIVE 0x00000001
  39. #define KPROBE_HIT_SS 0x00000002
  40. #define KPROBE_REENTER 0x00000004
  41. #define KPROBE_HIT_SSDONE 0x00000008
  42. /* Attach to insert probes on any functions which should be ignored*/
  43. #define __kprobes __attribute__((__section__(".kprobes.text")))
  44. struct kprobe;
  45. struct pt_regs;
  46. struct kretprobe;
  47. struct kretprobe_instance;
  48. typedef int (*kprobe_pre_handler_t) (struct kprobe *, struct pt_regs *);
  49. typedef int (*kprobe_break_handler_t) (struct kprobe *, struct pt_regs *);
  50. typedef void (*kprobe_post_handler_t) (struct kprobe *, struct pt_regs *,
  51. unsigned long flags);
  52. typedef int (*kprobe_fault_handler_t) (struct kprobe *, struct pt_regs *,
  53. int trapnr);
  54. typedef int (*kretprobe_handler_t) (struct kretprobe_instance *,
  55. struct pt_regs *);
  56. struct kprobe {
  57. struct hlist_node hlist;
  58. /* list of kprobes for multi-handler support */
  59. struct list_head list;
  60. /*count the number of times this probe was temporarily disarmed */
  61. unsigned long nmissed;
  62. /* location of the probe point */
  63. kprobe_opcode_t *addr;
  64. /* Called before addr is executed. */
  65. kprobe_pre_handler_t pre_handler;
  66. /* Called after addr is executed, unless... */
  67. kprobe_post_handler_t post_handler;
  68. /* ... called if executing addr causes a fault (eg. page fault).
  69. * Return 1 if it handled fault, otherwise kernel will see it. */
  70. kprobe_fault_handler_t fault_handler;
  71. /* ... called if breakpoint trap occurs in probe handler.
  72. * Return 1 if it handled break, otherwise kernel will see it. */
  73. kprobe_break_handler_t break_handler;
  74. /* Saved opcode (which has been replaced with breakpoint) */
  75. kprobe_opcode_t opcode;
  76. /* copy of the original instruction */
  77. struct arch_specific_insn ainsn;
  78. };
  79. /*
  80. * Special probe type that uses setjmp-longjmp type tricks to resume
  81. * execution at a specified entry with a matching prototype corresponding
  82. * to the probed function - a trick to enable arguments to become
  83. * accessible seamlessly by probe handling logic.
  84. * Note:
  85. * Because of the way compilers allocate stack space for local variables
  86. * etc upfront, regardless of sub-scopes within a function, this mirroring
  87. * principle currently works only for probes placed on function entry points.
  88. */
  89. struct jprobe {
  90. struct kprobe kp;
  91. kprobe_opcode_t *entry; /* probe handling code to jump to */
  92. };
  93. #ifdef ARCH_SUPPORTS_KRETPROBES
  94. extern void arch_prepare_kretprobe(struct kretprobe *rp, struct pt_regs *regs);
  95. #else /* ARCH_SUPPORTS_KRETPROBES */
  96. static inline void arch_prepare_kretprobe(struct kretprobe *rp,
  97. struct pt_regs *regs)
  98. {
  99. }
  100. #endif /* ARCH_SUPPORTS_KRETPROBES */
  101. /*
  102. * Function-return probe -
  103. * Note:
  104. * User needs to provide a handler function, and initialize maxactive.
  105. * maxactive - The maximum number of instances of the probed function that
  106. * can be active concurrently.
  107. * nmissed - tracks the number of times the probed function's return was
  108. * ignored, due to maxactive being too low.
  109. *
  110. */
  111. struct kretprobe {
  112. struct kprobe kp;
  113. kretprobe_handler_t handler;
  114. int maxactive;
  115. int nmissed;
  116. struct hlist_head free_instances;
  117. struct hlist_head used_instances;
  118. };
  119. struct kretprobe_instance {
  120. struct hlist_node uflist; /* either on free list or used list */
  121. struct hlist_node hlist;
  122. struct kretprobe *rp;
  123. kprobe_opcode_t *ret_addr;
  124. struct task_struct *task;
  125. };
  126. #ifdef CONFIG_KPROBES
  127. /* Locks kprobe: irq must be disabled */
  128. void lock_kprobes(void);
  129. void unlock_kprobes(void);
  130. /* kprobe running now on this CPU? */
  131. static inline int kprobe_running(void)
  132. {
  133. extern unsigned int kprobe_cpu;
  134. return kprobe_cpu == smp_processor_id();
  135. }
  136. extern int arch_prepare_kprobe(struct kprobe *p);
  137. extern void arch_copy_kprobe(struct kprobe *p);
  138. extern void arch_arm_kprobe(struct kprobe *p);
  139. extern void arch_disarm_kprobe(struct kprobe *p);
  140. extern void arch_remove_kprobe(struct kprobe *p);
  141. extern int arch_init_kprobes(void);
  142. extern void show_registers(struct pt_regs *regs);
  143. extern kprobe_opcode_t *get_insn_slot(void);
  144. extern void free_insn_slot(kprobe_opcode_t *slot);
  145. /* Get the kprobe at this addr (if any). Must have called lock_kprobes */
  146. struct kprobe *get_kprobe(void *addr);
  147. struct hlist_head * kretprobe_inst_table_head(struct task_struct *tsk);
  148. int register_kprobe(struct kprobe *p);
  149. void unregister_kprobe(struct kprobe *p);
  150. int setjmp_pre_handler(struct kprobe *, struct pt_regs *);
  151. int longjmp_break_handler(struct kprobe *, struct pt_regs *);
  152. int register_jprobe(struct jprobe *p);
  153. void unregister_jprobe(struct jprobe *p);
  154. void jprobe_return(void);
  155. int register_kretprobe(struct kretprobe *rp);
  156. void unregister_kretprobe(struct kretprobe *rp);
  157. struct kretprobe_instance *get_free_rp_inst(struct kretprobe *rp);
  158. void add_rp_inst(struct kretprobe_instance *ri);
  159. void kprobe_flush_task(struct task_struct *tk);
  160. void recycle_rp_inst(struct kretprobe_instance *ri);
  161. #else /* CONFIG_KPROBES */
  162. static inline int kprobe_running(void)
  163. {
  164. return 0;
  165. }
  166. static inline int register_kprobe(struct kprobe *p)
  167. {
  168. return -ENOSYS;
  169. }
  170. static inline void unregister_kprobe(struct kprobe *p)
  171. {
  172. }
  173. static inline int register_jprobe(struct jprobe *p)
  174. {
  175. return -ENOSYS;
  176. }
  177. static inline void unregister_jprobe(struct jprobe *p)
  178. {
  179. }
  180. static inline void jprobe_return(void)
  181. {
  182. }
  183. static inline int register_kretprobe(struct kretprobe *rp)
  184. {
  185. return -ENOSYS;
  186. }
  187. static inline void unregister_kretprobe(struct kretprobe *rp)
  188. {
  189. }
  190. static inline void kprobe_flush_task(struct task_struct *tk)
  191. {
  192. }
  193. #endif /* CONFIG_KPROBES */
  194. #endif /* _LINUX_KPROBES_H */