[PATCH] kprobes: function-return probes
[linux-2.6/verdex.git] / include / linux / kprobes.h
blobfba39f87efec2cc3f572c6d053b9ab0d30620f27
1 #ifndef _LINUX_KPROBES_H
2 #define _LINUX_KPROBES_H
3 /*
4 * Kernel Probes (KProbes)
5 * include/linux/kprobes.h
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.
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.
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.
21 * Copyright (C) IBM Corporation, 2002, 2004
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.
32 #include <linux/config.h>
33 #include <linux/list.h>
34 #include <linux/notifier.h>
35 #include <linux/smp.h>
36 #include <linux/spinlock.h>
38 #include <asm/kprobes.h>
40 struct kprobe;
41 struct pt_regs;
42 struct kretprobe;
43 struct kretprobe_instance;
44 typedef int (*kprobe_pre_handler_t) (struct kprobe *, struct pt_regs *);
45 typedef int (*kprobe_break_handler_t) (struct kprobe *, struct pt_regs *);
46 typedef void (*kprobe_post_handler_t) (struct kprobe *, struct pt_regs *,
47 unsigned long flags);
48 typedef int (*kprobe_fault_handler_t) (struct kprobe *, struct pt_regs *,
49 int trapnr);
50 typedef int (*kretprobe_handler_t) (struct kretprobe_instance *,
51 struct pt_regs *);
53 struct kprobe {
54 struct hlist_node hlist;
56 /* list of kprobes for multi-handler support */
57 struct list_head list;
59 /* location of the probe point */
60 kprobe_opcode_t *addr;
62 /* Called before addr is executed. */
63 kprobe_pre_handler_t pre_handler;
65 /* Called after addr is executed, unless... */
66 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;
72 /* ... called if breakpoint trap occurs in probe handler.
73 * Return 1 if it handled break, otherwise kernel will see it. */
74 kprobe_break_handler_t break_handler;
76 /* Saved opcode (which has been replaced with breakpoint) */
77 kprobe_opcode_t opcode;
79 /* copy of the original instruction */
80 struct arch_specific_insn ainsn;
84 * Special probe type that uses setjmp-longjmp type tricks to resume
85 * execution at a specified entry with a matching prototype corresponding
86 * to the probed function - a trick to enable arguments to become
87 * accessible seamlessly by probe handling logic.
88 * Note:
89 * Because of the way compilers allocate stack space for local variables
90 * etc upfront, regardless of sub-scopes within a function, this mirroring
91 * principle currently works only for probes placed on function entry points.
93 struct jprobe {
94 struct kprobe kp;
95 kprobe_opcode_t *entry; /* probe handling code to jump to */
98 #ifdef ARCH_SUPPORTS_KRETPROBES
99 extern int trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs);
100 extern void trampoline_post_handler(struct kprobe *p, struct pt_regs *regs,
101 unsigned long flags);
102 extern struct task_struct *arch_get_kprobe_task(void *ptr);
103 extern void arch_prepare_kretprobe(struct kretprobe *rp, struct pt_regs *regs);
104 extern void arch_kprobe_flush_task(struct task_struct *tk, spinlock_t *kp_lock);
105 #else /* ARCH_SUPPORTS_KRETPROBES */
106 static inline void kretprobe_trampoline(void)
109 static inline int trampoline_probe_handler(struct kprobe *p,
110 struct pt_regs *regs)
112 return 0;
114 static inline void trampoline_post_handler(struct kprobe *p,
115 struct pt_regs *regs, unsigned long flags)
118 static inline void arch_prepare_kretprobe(struct kretprobe *rp,
119 struct pt_regs *regs)
122 static inline void arch_kprobe_flush_task(struct task_struct *tk)
125 #define arch_get_kprobe_task(ptr) ((struct task_struct *)NULL)
126 #endif /* ARCH_SUPPORTS_KRETPROBES */
128 * Function-return probe -
129 * Note:
130 * User needs to provide a handler function, and initialize maxactive.
131 * maxactive - The maximum number of instances of the probed function that
132 * can be active concurrently.
133 * nmissed - tracks the number of times the probed function's return was
134 * ignored, due to maxactive being too low.
137 struct kretprobe {
138 struct kprobe kp;
139 kretprobe_handler_t handler;
140 int maxactive;
141 int nmissed;
142 struct hlist_head free_instances;
143 struct hlist_head used_instances;
146 struct kretprobe_instance {
147 struct hlist_node uflist; /* either on free list or used list */
148 struct hlist_node hlist;
149 struct kretprobe *rp;
150 void *ret_addr;
151 void *stack_addr;
154 #ifdef CONFIG_KPROBES
155 /* Locks kprobe: irq must be disabled */
156 void lock_kprobes(void);
157 void unlock_kprobes(void);
159 /* kprobe running now on this CPU? */
160 static inline int kprobe_running(void)
162 extern unsigned int kprobe_cpu;
163 return kprobe_cpu == smp_processor_id();
166 extern int arch_prepare_kprobe(struct kprobe *p);
167 extern void arch_copy_kprobe(struct kprobe *p);
168 extern void arch_remove_kprobe(struct kprobe *p);
169 extern void show_registers(struct pt_regs *regs);
171 /* Get the kprobe at this addr (if any). Must have called lock_kprobes */
172 struct kprobe *get_kprobe(void *addr);
173 struct hlist_head * kretprobe_inst_table_head(struct task_struct *tsk);
175 int register_kprobe(struct kprobe *p);
176 void unregister_kprobe(struct kprobe *p);
177 int setjmp_pre_handler(struct kprobe *, struct pt_regs *);
178 int longjmp_break_handler(struct kprobe *, struct pt_regs *);
179 int register_jprobe(struct jprobe *p);
180 void unregister_jprobe(struct jprobe *p);
181 void jprobe_return(void);
183 int register_kretprobe(struct kretprobe *rp);
184 void unregister_kretprobe(struct kretprobe *rp);
186 struct kretprobe_instance *get_free_rp_inst(struct kretprobe *rp);
187 struct kretprobe_instance *get_rp_inst(void *sara);
188 struct kretprobe_instance *get_rp_inst_tsk(struct task_struct *tk);
189 void add_rp_inst(struct kretprobe_instance *ri);
190 void kprobe_flush_task(struct task_struct *tk);
191 void recycle_rp_inst(struct kretprobe_instance *ri);
192 #else /* CONFIG_KPROBES */
193 static inline int kprobe_running(void)
195 return 0;
197 static inline int register_kprobe(struct kprobe *p)
199 return -ENOSYS;
201 static inline void unregister_kprobe(struct kprobe *p)
204 static inline int register_jprobe(struct jprobe *p)
206 return -ENOSYS;
208 static inline void unregister_jprobe(struct jprobe *p)
211 static inline void jprobe_return(void)
214 static inline int register_kretprobe(struct kretprobe *rp)
216 return -ENOSYS;
218 static inline void unregister_kretprobe(struct kretprobe *rp)
221 static inline void kprobe_flush_task(struct task_struct *tk)
224 #endif /* CONFIG_KPROBES */
225 #endif /* _LINUX_KPROBES_H */