blob: 0f90466fb8b090dc89d04a3c3117838f800078cf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#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.
Hien Nguyenb94cce92005-06-23 00:09:19 -070028 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 */
32#include <linux/config.h>
33#include <linux/list.h>
34#include <linux/notifier.h>
35#include <linux/smp.h>
Hien Nguyenb94cce92005-06-23 00:09:19 -070036#include <linux/spinlock.h>
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/kprobes.h>
39
40struct kprobe;
41struct pt_regs;
Hien Nguyenb94cce92005-06-23 00:09:19 -070042struct kretprobe;
43struct kretprobe_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044typedef int (*kprobe_pre_handler_t) (struct kprobe *, struct pt_regs *);
45typedef int (*kprobe_break_handler_t) (struct kprobe *, struct pt_regs *);
46typedef void (*kprobe_post_handler_t) (struct kprobe *, struct pt_regs *,
47 unsigned long flags);
48typedef int (*kprobe_fault_handler_t) (struct kprobe *, struct pt_regs *,
49 int trapnr);
Hien Nguyenb94cce92005-06-23 00:09:19 -070050typedef int (*kretprobe_handler_t) (struct kretprobe_instance *,
51 struct pt_regs *);
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053struct kprobe {
54 struct hlist_node hlist;
55
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -070056 /* list of kprobes for multi-handler support */
57 struct list_head list;
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 /* location of the probe point */
60 kprobe_opcode_t *addr;
61
62 /* Called before addr is executed. */
63 kprobe_pre_handler_t pre_handler;
64
65 /* Called after addr is executed, unless... */
66 kprobe_post_handler_t post_handler;
67
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
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;
75
76 /* Saved opcode (which has been replaced with breakpoint) */
77 kprobe_opcode_t opcode;
78
79 /* copy of the original instruction */
80 struct arch_specific_insn ainsn;
81};
82
83/*
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.
92 */
93struct jprobe {
94 struct kprobe kp;
95 kprobe_opcode_t *entry; /* probe handling code to jump to */
96};
97
Hien Nguyenb94cce92005-06-23 00:09:19 -070098#ifdef ARCH_SUPPORTS_KRETPROBES
99extern int trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs);
100extern void trampoline_post_handler(struct kprobe *p, struct pt_regs *regs,
101 unsigned long flags);
102extern struct task_struct *arch_get_kprobe_task(void *ptr);
103extern void arch_prepare_kretprobe(struct kretprobe *rp, struct pt_regs *regs);
104extern void arch_kprobe_flush_task(struct task_struct *tk, spinlock_t *kp_lock);
105#else /* ARCH_SUPPORTS_KRETPROBES */
106static inline void kretprobe_trampoline(void)
107{
108}
109static inline int trampoline_probe_handler(struct kprobe *p,
110 struct pt_regs *regs)
111{
112 return 0;
113}
114static inline void trampoline_post_handler(struct kprobe *p,
115 struct pt_regs *regs, unsigned long flags)
116{
117}
118static inline void arch_prepare_kretprobe(struct kretprobe *rp,
119 struct pt_regs *regs)
120{
121}
122static inline void arch_kprobe_flush_task(struct task_struct *tk)
123{
124}
125#define arch_get_kprobe_task(ptr) ((struct task_struct *)NULL)
126#endif /* ARCH_SUPPORTS_KRETPROBES */
127/*
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.
135 *
136 */
137struct 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;
144};
145
146struct 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;
152};
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154#ifdef CONFIG_KPROBES
155/* Locks kprobe: irq must be disabled */
156void lock_kprobes(void);
157void unlock_kprobes(void);
158
159/* kprobe running now on this CPU? */
160static inline int kprobe_running(void)
161{
162 extern unsigned int kprobe_cpu;
163 return kprobe_cpu == smp_processor_id();
164}
165
166extern int arch_prepare_kprobe(struct kprobe *p);
167extern void arch_copy_kprobe(struct kprobe *p);
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700168extern void arch_arm_kprobe(struct kprobe *p);
169extern void arch_disarm_kprobe(struct kprobe *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170extern void arch_remove_kprobe(struct kprobe *p);
171extern void show_registers(struct pt_regs *regs);
172
173/* Get the kprobe at this addr (if any). Must have called lock_kprobes */
174struct kprobe *get_kprobe(void *addr);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700175struct hlist_head * kretprobe_inst_table_head(struct task_struct *tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177int register_kprobe(struct kprobe *p);
178void unregister_kprobe(struct kprobe *p);
179int setjmp_pre_handler(struct kprobe *, struct pt_regs *);
180int longjmp_break_handler(struct kprobe *, struct pt_regs *);
181int register_jprobe(struct jprobe *p);
182void unregister_jprobe(struct jprobe *p);
183void jprobe_return(void);
184
Hien Nguyenb94cce92005-06-23 00:09:19 -0700185int register_kretprobe(struct kretprobe *rp);
186void unregister_kretprobe(struct kretprobe *rp);
187
188struct kretprobe_instance *get_free_rp_inst(struct kretprobe *rp);
189struct kretprobe_instance *get_rp_inst(void *sara);
190struct kretprobe_instance *get_rp_inst_tsk(struct task_struct *tk);
191void add_rp_inst(struct kretprobe_instance *ri);
192void kprobe_flush_task(struct task_struct *tk);
193void recycle_rp_inst(struct kretprobe_instance *ri);
194#else /* CONFIG_KPROBES */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195static inline int kprobe_running(void)
196{
197 return 0;
198}
199static inline int register_kprobe(struct kprobe *p)
200{
201 return -ENOSYS;
202}
203static inline void unregister_kprobe(struct kprobe *p)
204{
205}
206static inline int register_jprobe(struct jprobe *p)
207{
208 return -ENOSYS;
209}
210static inline void unregister_jprobe(struct jprobe *p)
211{
212}
213static inline void jprobe_return(void)
214{
215}
Hien Nguyenb94cce92005-06-23 00:09:19 -0700216static inline int register_kretprobe(struct kretprobe *rp)
217{
218 return -ENOSYS;
219}
220static inline void unregister_kretprobe(struct kretprobe *rp)
221{
222}
223static inline void kprobe_flush_task(struct task_struct *tk)
224{
225}
226#endif /* CONFIG_KPROBES */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227#endif /* _LINUX_KPROBES_H */