2 * Kernel Probes (KProbes)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * Copyright (C) IBM Corporation, 2002, 2004
21 * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
22 * Probes initial implementation (includes suggestions from
24 * 2004-Aug Updated by Prasanna S Panchamukhi <prasanna@in.ibm.com> with
25 * hlists and exceptions notifier as suggested by Andi Kleen.
26 * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
27 * interface to access function arguments.
28 * 2004-Sep Prasanna S Panchamukhi <prasanna@in.ibm.com> Changed Kprobes
29 * exceptions notifier to be first on the priority list.
30 * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston
31 * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
32 * <prasanna@in.ibm.com> added function-return probes.
34 #include <linux/kprobes.h>
35 #include <linux/hash.h>
36 #include <linux/init.h>
37 #include <linux/slab.h>
38 #include <linux/stddef.h>
39 #include <linux/module.h>
40 #include <linux/moduleloader.h>
41 #include <linux/kallsyms.h>
42 #include <linux/freezer.h>
43 #include <linux/seq_file.h>
44 #include <linux/debugfs.h>
45 #include <linux/kdebug.h>
47 #include <asm-generic/sections.h>
48 #include <asm/cacheflush.h>
49 #include <asm/errno.h>
50 #include <asm/uaccess.h>
52 #define KPROBE_HASH_BITS 6
53 #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
57 * Some oddball architectures like 64bit powerpc have function descriptors
58 * so this must be overridable.
60 #ifndef kprobe_lookup_name
61 #define kprobe_lookup_name(name, addr) \
62 addr = ((kprobe_opcode_t *)(kallsyms_lookup_name(name)))
65 static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
66 static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
68 /* NOTE: change this value only with kprobe_mutex held */
69 static bool kprobe_enabled;
71 DEFINE_MUTEX(kprobe_mutex); /* Protects kprobe_table */
72 DEFINE_SPINLOCK(kretprobe_lock); /* Protects kretprobe_inst_table */
73 static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
75 #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
77 * kprobe->ainsn.insn points to the copy of the instruction to be
78 * single-stepped. x86_64, POWER4 and above have no-exec support and
79 * stepping on the instruction on a vmalloced/kmalloced/data page
80 * is a recipe for disaster
82 #define INSNS_PER_PAGE (PAGE_SIZE/(MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
84 struct kprobe_insn_page {
85 struct hlist_node hlist;
86 kprobe_opcode_t *insns; /* Page of instruction slots */
87 char slot_used[INSNS_PER_PAGE];
92 enum kprobe_slot_state {
98 static struct hlist_head kprobe_insn_pages;
99 static int kprobe_garbage_slots;
100 static int collect_garbage_slots(void);
102 static int __kprobes check_safety(void)
105 #if defined(CONFIG_PREEMPT) && defined(CONFIG_PM)
106 ret = freeze_processes();
108 struct task_struct *p, *q;
109 do_each_thread(p, q) {
110 if (p != current && p->state == TASK_RUNNING &&
112 printk("Check failed: %s is running\n",p->comm);
116 } while_each_thread(p, q);
127 * get_insn_slot() - Find a slot on an executable page for an instruction.
128 * We allocate an executable page if there's no room on existing ones.
130 kprobe_opcode_t __kprobes *get_insn_slot(void)
132 struct kprobe_insn_page *kip;
133 struct hlist_node *pos;
136 hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
137 if (kip->nused < INSNS_PER_PAGE) {
139 for (i = 0; i < INSNS_PER_PAGE; i++) {
140 if (kip->slot_used[i] == SLOT_CLEAN) {
141 kip->slot_used[i] = SLOT_USED;
143 return kip->insns + (i * MAX_INSN_SIZE);
146 /* Surprise! No unused slots. Fix kip->nused. */
147 kip->nused = INSNS_PER_PAGE;
151 /* If there are any garbage slots, collect it and try again. */
152 if (kprobe_garbage_slots && collect_garbage_slots() == 0) {
155 /* All out of space. Need to allocate a new page. Use slot 0. */
156 kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL);
161 * Use module_alloc so this page is within +/- 2GB of where the
162 * kernel image and loaded module images reside. This is required
163 * so x86_64 can correctly handle the %rip-relative fixups.
165 kip->insns = module_alloc(PAGE_SIZE);
170 INIT_HLIST_NODE(&kip->hlist);
171 hlist_add_head(&kip->hlist, &kprobe_insn_pages);
172 memset(kip->slot_used, SLOT_CLEAN, INSNS_PER_PAGE);
173 kip->slot_used[0] = SLOT_USED;
179 /* Return 1 if all garbages are collected, otherwise 0. */
180 static int __kprobes collect_one_slot(struct kprobe_insn_page *kip, int idx)
182 kip->slot_used[idx] = SLOT_CLEAN;
184 if (kip->nused == 0) {
186 * Page is no longer in use. Free it unless
187 * it's the last one. We keep the last one
188 * so as not to have to set it up again the
189 * next time somebody inserts a probe.
191 hlist_del(&kip->hlist);
192 if (hlist_empty(&kprobe_insn_pages)) {
193 INIT_HLIST_NODE(&kip->hlist);
194 hlist_add_head(&kip->hlist,
197 module_free(NULL, kip->insns);
205 static int __kprobes collect_garbage_slots(void)
207 struct kprobe_insn_page *kip;
208 struct hlist_node *pos, *next;
210 /* Ensure no-one is preepmted on the garbages */
211 if (check_safety() != 0)
214 hlist_for_each_entry_safe(kip, pos, next, &kprobe_insn_pages, hlist) {
216 if (kip->ngarbage == 0)
218 kip->ngarbage = 0; /* we will collect all garbages */
219 for (i = 0; i < INSNS_PER_PAGE; i++) {
220 if (kip->slot_used[i] == SLOT_DIRTY &&
221 collect_one_slot(kip, i))
225 kprobe_garbage_slots = 0;
229 void __kprobes free_insn_slot(kprobe_opcode_t * slot, int dirty)
231 struct kprobe_insn_page *kip;
232 struct hlist_node *pos;
234 hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
235 if (kip->insns <= slot &&
236 slot < kip->insns + (INSNS_PER_PAGE * MAX_INSN_SIZE)) {
237 int i = (slot - kip->insns) / MAX_INSN_SIZE;
239 kip->slot_used[i] = SLOT_DIRTY;
242 collect_one_slot(kip, i);
248 if (dirty && ++kprobe_garbage_slots > INSNS_PER_PAGE)
249 collect_garbage_slots();
253 /* We have preemption disabled.. so it is safe to use __ versions */
254 static inline void set_kprobe_instance(struct kprobe *kp)
256 __get_cpu_var(kprobe_instance) = kp;
259 static inline void reset_kprobe_instance(void)
261 __get_cpu_var(kprobe_instance) = NULL;
265 * This routine is called either:
266 * - under the kprobe_mutex - during kprobe_[un]register()
268 * - with preemption disabled - from arch/xxx/kernel/kprobes.c
270 struct kprobe __kprobes *get_kprobe(void *addr)
272 struct hlist_head *head;
273 struct hlist_node *node;
276 head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
277 hlist_for_each_entry_rcu(p, node, head, hlist) {
285 * Aggregate handlers for multiple kprobes support - these handlers
286 * take care of invoking the individual kprobe handlers on p->list
288 static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
292 list_for_each_entry_rcu(kp, &p->list, list) {
293 if (kp->pre_handler) {
294 set_kprobe_instance(kp);
295 if (kp->pre_handler(kp, regs))
298 reset_kprobe_instance();
303 static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
308 list_for_each_entry_rcu(kp, &p->list, list) {
309 if (kp->post_handler) {
310 set_kprobe_instance(kp);
311 kp->post_handler(kp, regs, flags);
312 reset_kprobe_instance();
317 static int __kprobes aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
320 struct kprobe *cur = __get_cpu_var(kprobe_instance);
323 * if we faulted "during" the execution of a user specified
324 * probe handler, invoke just that probe's fault handler
326 if (cur && cur->fault_handler) {
327 if (cur->fault_handler(cur, regs, trapnr))
333 static int __kprobes aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
335 struct kprobe *cur = __get_cpu_var(kprobe_instance);
338 if (cur && cur->break_handler) {
339 if (cur->break_handler(cur, regs))
342 reset_kprobe_instance();
346 /* Walks the list and increments nmissed count for multiprobe case */
347 void __kprobes kprobes_inc_nmissed_count(struct kprobe *p)
350 if (p->pre_handler != aggr_pre_handler) {
353 list_for_each_entry_rcu(kp, &p->list, list)
359 /* Called with kretprobe_lock held */
360 void __kprobes recycle_rp_inst(struct kretprobe_instance *ri,
361 struct hlist_head *head)
363 /* remove rp inst off the rprobe_inst_table */
364 hlist_del(&ri->hlist);
366 /* remove rp inst off the used list */
367 hlist_del(&ri->uflist);
368 /* put rp inst back onto the free list */
369 INIT_HLIST_NODE(&ri->uflist);
370 hlist_add_head(&ri->uflist, &ri->rp->free_instances);
373 hlist_add_head(&ri->hlist, head);
376 struct hlist_head __kprobes *kretprobe_inst_table_head(struct task_struct *tsk)
378 return &kretprobe_inst_table[hash_ptr(tsk, KPROBE_HASH_BITS)];
382 * This function is called from finish_task_switch when task tk becomes dead,
383 * so that we can recycle any function-return probe instances associated
384 * with this task. These left over instances represent probed functions
385 * that have been called but will never return.
387 void __kprobes kprobe_flush_task(struct task_struct *tk)
389 struct kretprobe_instance *ri;
390 struct hlist_head *head, empty_rp;
391 struct hlist_node *node, *tmp;
392 unsigned long flags = 0;
394 INIT_HLIST_HEAD(&empty_rp);
395 spin_lock_irqsave(&kretprobe_lock, flags);
396 head = kretprobe_inst_table_head(tk);
397 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
399 recycle_rp_inst(ri, &empty_rp);
401 spin_unlock_irqrestore(&kretprobe_lock, flags);
403 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
404 hlist_del(&ri->hlist);
409 static inline void free_rp_inst(struct kretprobe *rp)
411 struct kretprobe_instance *ri;
412 struct hlist_node *pos, *next;
414 hlist_for_each_entry_safe(ri, pos, next, &rp->free_instances, uflist) {
415 hlist_del(&ri->uflist);
421 * Keep all fields in the kprobe consistent
423 static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
425 memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t));
426 memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn));
430 * Add the new probe to old_p->list. Fail if this is the
431 * second jprobe at the address - two jprobes can't coexist
433 static int __kprobes add_new_kprobe(struct kprobe *old_p, struct kprobe *p)
435 if (p->break_handler) {
436 if (old_p->break_handler)
438 list_add_tail_rcu(&p->list, &old_p->list);
439 old_p->break_handler = aggr_break_handler;
441 list_add_rcu(&p->list, &old_p->list);
442 if (p->post_handler && !old_p->post_handler)
443 old_p->post_handler = aggr_post_handler;
448 * Fill in the required fields of the "manager kprobe". Replace the
449 * earlier kprobe in the hlist with the manager kprobe
451 static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
456 ap->pre_handler = aggr_pre_handler;
457 ap->fault_handler = aggr_fault_handler;
459 ap->post_handler = aggr_post_handler;
460 if (p->break_handler)
461 ap->break_handler = aggr_break_handler;
463 INIT_LIST_HEAD(&ap->list);
464 list_add_rcu(&p->list, &ap->list);
466 hlist_replace_rcu(&p->hlist, &ap->hlist);
470 * This is the second or subsequent kprobe at the address - handle
473 static int __kprobes register_aggr_kprobe(struct kprobe *old_p,
479 if (old_p->pre_handler == aggr_pre_handler) {
480 copy_kprobe(old_p, p);
481 ret = add_new_kprobe(old_p, p);
483 ap = kzalloc(sizeof(struct kprobe), GFP_KERNEL);
486 add_aggr_kprobe(ap, old_p);
488 ret = add_new_kprobe(ap, p);
493 static int __kprobes in_kprobes_functions(unsigned long addr)
495 if (addr >= (unsigned long)__kprobes_text_start &&
496 addr < (unsigned long)__kprobes_text_end)
502 * If we have a symbol_name argument, look it up and add the offset field
503 * to it. This way, we can specify a relative address to a symbol.
505 static kprobe_opcode_t __kprobes *kprobe_addr(struct kprobe *p)
507 kprobe_opcode_t *addr = p->addr;
508 if (p->symbol_name) {
511 kprobe_lookup_name(p->symbol_name, addr);
516 return (kprobe_opcode_t *)(((char *)addr) + p->offset);
519 static int __kprobes __register_kprobe(struct kprobe *p,
520 unsigned long called_from)
523 struct kprobe *old_p;
524 struct module *probed_mod;
525 kprobe_opcode_t *addr;
527 addr = kprobe_addr(p);
532 if (!kernel_text_address((unsigned long) p->addr) ||
533 in_kprobes_functions((unsigned long) p->addr))
536 p->mod_refcounted = 0;
539 * Check if are we probing a module.
541 probed_mod = module_text_address((unsigned long) p->addr);
543 struct module *calling_mod = module_text_address(called_from);
545 * We must allow modules to probe themself and in this case
546 * avoid incrementing the module refcount, so as to allow
547 * unloading of self probing modules.
549 if (calling_mod && calling_mod != probed_mod) {
550 if (unlikely(!try_module_get(probed_mod)))
552 p->mod_refcounted = 1;
558 mutex_lock(&kprobe_mutex);
559 old_p = get_kprobe(p->addr);
561 ret = register_aggr_kprobe(old_p, p);
565 ret = arch_prepare_kprobe(p);
569 INIT_HLIST_NODE(&p->hlist);
570 hlist_add_head_rcu(&p->hlist,
571 &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
577 mutex_unlock(&kprobe_mutex);
579 if (ret && probed_mod)
580 module_put(probed_mod);
584 int __kprobes register_kprobe(struct kprobe *p)
586 return __register_kprobe(p, (unsigned long)__builtin_return_address(0));
589 void __kprobes unregister_kprobe(struct kprobe *p)
592 struct kprobe *old_p, *list_p;
595 mutex_lock(&kprobe_mutex);
596 old_p = get_kprobe(p->addr);
597 if (unlikely(!old_p)) {
598 mutex_unlock(&kprobe_mutex);
602 list_for_each_entry_rcu(list_p, &old_p->list, list)
604 /* kprobe p is a valid probe */
606 mutex_unlock(&kprobe_mutex);
611 (old_p->pre_handler == aggr_pre_handler &&
612 p->list.next == &old_p->list && p->list.prev == &old_p->list)) {
614 * Only probe on the hash list. Disarm only if kprobes are
615 * enabled - otherwise, the breakpoint would already have
616 * been removed. We save on flushing icache.
619 arch_disarm_kprobe(p);
620 hlist_del_rcu(&old_p->hlist);
623 list_del_rcu(&p->list);
627 mutex_unlock(&kprobe_mutex);
630 if (p->mod_refcounted) {
631 mod = module_text_address((unsigned long)p->addr);
638 list_del_rcu(&p->list);
641 arch_remove_kprobe(p);
643 mutex_lock(&kprobe_mutex);
644 if (p->break_handler)
645 old_p->break_handler = NULL;
646 if (p->post_handler){
647 list_for_each_entry_rcu(list_p, &old_p->list, list){
648 if (list_p->post_handler){
654 old_p->post_handler = NULL;
656 mutex_unlock(&kprobe_mutex);
660 static struct notifier_block kprobe_exceptions_nb = {
661 .notifier_call = kprobe_exceptions_notify,
662 .priority = 0x7fffffff /* we need to be notified first */
665 unsigned long __weak arch_deref_entry_point(void *entry)
667 return (unsigned long)entry;
670 int __kprobes register_jprobe(struct jprobe *jp)
672 unsigned long addr = arch_deref_entry_point(jp->entry);
674 if (!kernel_text_address(addr))
677 /* Todo: Verify probepoint is a function entry point */
678 jp->kp.pre_handler = setjmp_pre_handler;
679 jp->kp.break_handler = longjmp_break_handler;
681 return __register_kprobe(&jp->kp,
682 (unsigned long)__builtin_return_address(0));
685 void __kprobes unregister_jprobe(struct jprobe *jp)
687 unregister_kprobe(&jp->kp);
690 #ifdef CONFIG_KRETPROBES
692 * This kprobe pre_handler is registered with every kretprobe. When probe
693 * hits it will set up the return probe.
695 static int __kprobes pre_handler_kretprobe(struct kprobe *p,
696 struct pt_regs *regs)
698 struct kretprobe *rp = container_of(p, struct kretprobe, kp);
699 unsigned long flags = 0;
701 /*TODO: consider to only swap the RA after the last pre_handler fired */
702 spin_lock_irqsave(&kretprobe_lock, flags);
703 if (!hlist_empty(&rp->free_instances)) {
704 struct kretprobe_instance *ri;
706 ri = hlist_entry(rp->free_instances.first,
707 struct kretprobe_instance, uflist);
711 if (rp->entry_handler && rp->entry_handler(ri, regs)) {
712 spin_unlock_irqrestore(&kretprobe_lock, flags);
716 arch_prepare_kretprobe(ri, regs);
718 /* XXX(hch): why is there no hlist_move_head? */
719 hlist_del(&ri->uflist);
720 hlist_add_head(&ri->uflist, &ri->rp->used_instances);
721 hlist_add_head(&ri->hlist, kretprobe_inst_table_head(ri->task));
724 spin_unlock_irqrestore(&kretprobe_lock, flags);
728 int __kprobes register_kretprobe(struct kretprobe *rp)
731 struct kretprobe_instance *inst;
735 if (kretprobe_blacklist_size) {
736 addr = kprobe_addr(&rp->kp);
740 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
741 if (kretprobe_blacklist[i].addr == addr)
746 rp->kp.pre_handler = pre_handler_kretprobe;
747 rp->kp.post_handler = NULL;
748 rp->kp.fault_handler = NULL;
749 rp->kp.break_handler = NULL;
751 /* Pre-allocate memory for max kretprobe instances */
752 if (rp->maxactive <= 0) {
753 #ifdef CONFIG_PREEMPT
754 rp->maxactive = max(10, 2 * NR_CPUS);
756 rp->maxactive = NR_CPUS;
759 INIT_HLIST_HEAD(&rp->used_instances);
760 INIT_HLIST_HEAD(&rp->free_instances);
761 for (i = 0; i < rp->maxactive; i++) {
762 inst = kmalloc(sizeof(struct kretprobe_instance) +
763 rp->data_size, GFP_KERNEL);
768 INIT_HLIST_NODE(&inst->uflist);
769 hlist_add_head(&inst->uflist, &rp->free_instances);
773 /* Establish function entry probe point */
774 if ((ret = __register_kprobe(&rp->kp,
775 (unsigned long)__builtin_return_address(0))) != 0)
780 #else /* CONFIG_KRETPROBES */
781 int __kprobes register_kretprobe(struct kretprobe *rp)
786 static int __kprobes pre_handler_kretprobe(struct kprobe *p,
787 struct pt_regs *regs)
791 #endif /* CONFIG_KRETPROBES */
793 void __kprobes unregister_kretprobe(struct kretprobe *rp)
796 struct kretprobe_instance *ri;
797 struct hlist_node *pos, *next;
799 unregister_kprobe(&rp->kp);
802 spin_lock_irqsave(&kretprobe_lock, flags);
803 hlist_for_each_entry_safe(ri, pos, next, &rp->used_instances, uflist) {
805 hlist_del(&ri->uflist);
807 spin_unlock_irqrestore(&kretprobe_lock, flags);
811 static int __init init_kprobes(void)
815 /* FIXME allocate the probe table, currently defined statically */
816 /* initialize all list heads */
817 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
818 INIT_HLIST_HEAD(&kprobe_table[i]);
819 INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
822 if (kretprobe_blacklist_size) {
823 /* lookup the function address from its name */
824 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
825 kprobe_lookup_name(kretprobe_blacklist[i].name,
826 kretprobe_blacklist[i].addr);
827 if (!kretprobe_blacklist[i].addr)
828 printk("kretprobe: lookup failed: %s\n",
829 kretprobe_blacklist[i].name);
833 /* By default, kprobes are enabled */
834 kprobe_enabled = true;
836 err = arch_init_kprobes();
838 err = register_die_notifier(&kprobe_exceptions_nb);
845 #ifdef CONFIG_DEBUG_FS
846 static void __kprobes report_probe(struct seq_file *pi, struct kprobe *p,
847 const char *sym, int offset,char *modname)
851 if (p->pre_handler == pre_handler_kretprobe)
853 else if (p->pre_handler == setjmp_pre_handler)
858 seq_printf(pi, "%p %s %s+0x%x %s\n", p->addr, kprobe_type,
859 sym, offset, (modname ? modname : " "));
861 seq_printf(pi, "%p %s %p\n", p->addr, kprobe_type, p->addr);
864 static void __kprobes *kprobe_seq_start(struct seq_file *f, loff_t *pos)
866 return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
869 static void __kprobes *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
872 if (*pos >= KPROBE_TABLE_SIZE)
877 static void __kprobes kprobe_seq_stop(struct seq_file *f, void *v)
882 static int __kprobes show_kprobe_addr(struct seq_file *pi, void *v)
884 struct hlist_head *head;
885 struct hlist_node *node;
886 struct kprobe *p, *kp;
887 const char *sym = NULL;
888 unsigned int i = *(loff_t *) v;
889 unsigned long offset = 0;
890 char *modname, namebuf[128];
892 head = &kprobe_table[i];
894 hlist_for_each_entry_rcu(p, node, head, hlist) {
895 sym = kallsyms_lookup((unsigned long)p->addr, NULL,
896 &offset, &modname, namebuf);
897 if (p->pre_handler == aggr_pre_handler) {
898 list_for_each_entry_rcu(kp, &p->list, list)
899 report_probe(pi, kp, sym, offset, modname);
901 report_probe(pi, p, sym, offset, modname);
907 static struct seq_operations kprobes_seq_ops = {
908 .start = kprobe_seq_start,
909 .next = kprobe_seq_next,
910 .stop = kprobe_seq_stop,
911 .show = show_kprobe_addr
914 static int __kprobes kprobes_open(struct inode *inode, struct file *filp)
916 return seq_open(filp, &kprobes_seq_ops);
919 static struct file_operations debugfs_kprobes_operations = {
920 .open = kprobes_open,
923 .release = seq_release,
926 static void __kprobes enable_all_kprobes(void)
928 struct hlist_head *head;
929 struct hlist_node *node;
933 mutex_lock(&kprobe_mutex);
935 /* If kprobes are already enabled, just return */
937 goto already_enabled;
939 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
940 head = &kprobe_table[i];
941 hlist_for_each_entry_rcu(p, node, head, hlist)
945 kprobe_enabled = true;
946 printk(KERN_INFO "Kprobes globally enabled\n");
949 mutex_unlock(&kprobe_mutex);
953 static void __kprobes disable_all_kprobes(void)
955 struct hlist_head *head;
956 struct hlist_node *node;
960 mutex_lock(&kprobe_mutex);
962 /* If kprobes are already disabled, just return */
964 goto already_disabled;
966 kprobe_enabled = false;
967 printk(KERN_INFO "Kprobes globally disabled\n");
968 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
969 head = &kprobe_table[i];
970 hlist_for_each_entry_rcu(p, node, head, hlist) {
971 if (!arch_trampoline_kprobe(p))
972 arch_disarm_kprobe(p);
976 mutex_unlock(&kprobe_mutex);
977 /* Allow all currently running kprobes to complete */
982 mutex_unlock(&kprobe_mutex);
987 * XXX: The debugfs bool file interface doesn't allow for callbacks
988 * when the bool state is switched. We can reuse that facility when
991 static ssize_t read_enabled_file_bool(struct file *file,
992 char __user *user_buf, size_t count, loff_t *ppos)
1002 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
1005 static ssize_t write_enabled_file_bool(struct file *file,
1006 const char __user *user_buf, size_t count, loff_t *ppos)
1011 buf_size = min(count, (sizeof(buf)-1));
1012 if (copy_from_user(buf, user_buf, buf_size))
1019 enable_all_kprobes();
1024 disable_all_kprobes();
1031 static struct file_operations fops_kp = {
1032 .read = read_enabled_file_bool,
1033 .write = write_enabled_file_bool,
1036 static int __kprobes debugfs_kprobe_init(void)
1038 struct dentry *dir, *file;
1039 unsigned int value = 1;
1041 dir = debugfs_create_dir("kprobes", NULL);
1045 file = debugfs_create_file("list", 0444, dir, NULL,
1046 &debugfs_kprobes_operations);
1048 debugfs_remove(dir);
1052 file = debugfs_create_file("enabled", 0600, dir,
1055 debugfs_remove(dir);
1062 late_initcall(debugfs_kprobe_init);
1063 #endif /* CONFIG_DEBUG_FS */
1065 module_init(init_kprobes);
1067 EXPORT_SYMBOL_GPL(register_kprobe);
1068 EXPORT_SYMBOL_GPL(unregister_kprobe);
1069 EXPORT_SYMBOL_GPL(register_jprobe);
1070 EXPORT_SYMBOL_GPL(unregister_jprobe);
1071 #ifdef CONFIG_KPROBES
1072 EXPORT_SYMBOL_GPL(jprobe_return);
1075 #ifdef CONFIG_KPROBES
1076 EXPORT_SYMBOL_GPL(register_kretprobe);
1077 EXPORT_SYMBOL_GPL(unregister_kretprobe);