1 Title : Kernel Probes (Kprobes)
2 Authors : Jim Keniston <jkenisto@us.ibm.com>
3 : Prasanna S Panchamukhi <prasanna@in.ibm.com>
7 1. Concepts: Kprobes, Jprobes, Return Probes
8 2. Architectures Supported
11 5. Kprobes Features and Limitations
16 10. Kretprobes Example
18 1. Concepts: Kprobes, Jprobes, Return Probes
20 Kprobes enables you to dynamically break into any kernel routine and
21 collect debugging and performance information non-disruptively. You
22 can trap at almost any kernel code address, specifying a handler
23 routine to be invoked when the breakpoint is hit.
25 There are currently three types of probes: kprobes, jprobes, and
26 kretprobes (also called return probes). A kprobe can be inserted
27 on virtually any instruction in the kernel. A jprobe is inserted at
28 the entry to a kernel function, and provides convenient access to the
29 function's arguments. A return probe fires when a specified function
32 In the typical case, Kprobes-based instrumentation is packaged as
33 a kernel module. The module's init function installs ("registers")
34 one or more probes, and the exit function unregisters them. A
35 registration function such as register_kprobe() specifies where
36 the probe is to be inserted and what handler is to be called when
39 The next three subsections explain how the different types of
40 probes work. They explain certain things that you'll need to
41 know in order to make the best use of Kprobes -- e.g., the
42 difference between a pre_handler and a post_handler, and how
43 to use the maxactive and nmissed fields of a kretprobe. But
44 if you're in a hurry to start using Kprobes, you can skip ahead
47 1.1 How Does a Kprobe Work?
49 When a kprobe is registered, Kprobes makes a copy of the probed
50 instruction and replaces the first byte(s) of the probed instruction
51 with a breakpoint instruction (e.g., int3 on i386 and x86_64).
53 When a CPU hits the breakpoint instruction, a trap occurs, the CPU's
54 registers are saved, and control passes to Kprobes via the
55 notifier_call_chain mechanism. Kprobes executes the "pre_handler"
56 associated with the kprobe, passing the handler the addresses of the
57 kprobe struct and the saved registers.
59 Next, Kprobes single-steps its copy of the probed instruction.
60 (It would be simpler to single-step the actual instruction in place,
61 but then Kprobes would have to temporarily remove the breakpoint
62 instruction. This would open a small time window when another CPU
63 could sail right past the probepoint.)
65 After the instruction is single-stepped, Kprobes executes the
66 "post_handler," if any, that is associated with the kprobe.
67 Execution then continues with the instruction following the probepoint.
69 1.2 How Does a Jprobe Work?
71 A jprobe is implemented using a kprobe that is placed on a function's
72 entry point. It employs a simple mirroring principle to allow
73 seamless access to the probed function's arguments. The jprobe
74 handler routine should have the same signature (arg list and return
75 type) as the function being probed, and must always end by calling
76 the Kprobes function jprobe_return().
78 Here's how it works. When the probe is hit, Kprobes makes a copy of
79 the saved registers and a generous portion of the stack (see below).
80 Kprobes then points the saved instruction pointer at the jprobe's
81 handler routine, and returns from the trap. As a result, control
82 passes to the handler, which is presented with the same register and
83 stack contents as the probed function. When it is done, the handler
84 calls jprobe_return(), which traps again to restore the original stack
85 contents and processor state and switch to the probed function.
87 By convention, the callee owns its arguments, so gcc may produce code
88 that unexpectedly modifies that portion of the stack. This is why
89 Kprobes saves a copy of the stack and restores it after the jprobe
90 handler has run. Up to MAX_STACK_SIZE bytes are copied -- e.g.,
93 Note that the probed function's args may be passed on the stack
94 or in registers (e.g., for x86_64 or for an i386 fastcall function).
95 The jprobe will work in either case, so long as the handler's
96 prototype matches that of the probed function.
98 1.3 How Does a Return Probe Work?
100 When you call register_kretprobe(), Kprobes establishes a kprobe at
101 the entry to the function. When the probed function is called and this
102 probe is hit, Kprobes saves a copy of the return address, and replaces
103 the return address with the address of a "trampoline." The trampoline
104 is an arbitrary piece of code -- typically just a nop instruction.
105 At boot time, Kprobes registers a kprobe at the trampoline.
107 When the probed function executes its return instruction, control
108 passes to the trampoline and that probe is hit. Kprobes' trampoline
109 handler calls the user-specified handler associated with the kretprobe,
110 then sets the saved instruction pointer to the saved return address,
111 and that's where execution resumes upon return from the trap.
113 While the probed function is executing, its return address is
114 stored in an object of type kretprobe_instance. Before calling
115 register_kretprobe(), the user sets the maxactive field of the
116 kretprobe struct to specify how many instances of the specified
117 function can be probed simultaneously. register_kretprobe()
118 pre-allocates the indicated number of kretprobe_instance objects.
120 For example, if the function is non-recursive and is called with a
121 spinlock held, maxactive = 1 should be enough. If the function is
122 non-recursive and can never relinquish the CPU (e.g., via a semaphore
123 or preemption), NR_CPUS should be enough. If maxactive <= 0, it is
124 set to a default value. If CONFIG_PREEMPT is enabled, the default
125 is max(10, 2*NR_CPUS). Otherwise, the default is NR_CPUS.
127 It's not a disaster if you set maxactive too low; you'll just miss
128 some probes. In the kretprobe struct, the nmissed field is set to
129 zero when the return probe is registered, and is incremented every
130 time the probed function is entered but there is no kretprobe_instance
131 object available for establishing the return probe.
133 2. Architectures Supported
135 Kprobes, jprobes, and return probes are implemented on the following
139 - x86_64 (AMD-64, E64MT)
141 - ia64 (Support for probes on certain instruction types is still in progress.)
142 - sparc64 (Return probes not yet implemented.)
144 3. Configuring Kprobes
146 When configuring the kernel using make menuconfig/xconfig/oldconfig,
147 ensure that CONFIG_KPROBES is set to "y". Under "Kernel hacking",
148 look for "Kprobes". You may have to enable "Kernel debugging"
149 (CONFIG_DEBUG_KERNEL) before you can enable Kprobes.
151 You may also want to ensure that CONFIG_KALLSYMS and perhaps even
152 CONFIG_KALLSYMS_ALL are set to "y", since kallsyms_lookup_name()
153 is a handy, version-independent way to find a function's address.
155 If you need to insert a probe in the middle of a function, you may find
156 it useful to "Compile the kernel with debug info" (CONFIG_DEBUG_INFO),
157 so you can use "objdump -d -l vmlinux" to see the source-to-object
162 The Kprobes API includes a "register" function and an "unregister"
163 function for each type of probe. Here are terse, mini-man-page
164 specifications for these functions and the associated probe handlers
165 that you'll write. See the latter half of this document for examples.
169 #include <linux/kprobes.h>
170 int register_kprobe(struct kprobe *kp);
172 Sets a breakpoint at the address kp->addr. When the breakpoint is
173 hit, Kprobes calls kp->pre_handler. After the probed instruction
174 is single-stepped, Kprobe calls kp->post_handler. If a fault
175 occurs during execution of kp->pre_handler or kp->post_handler,
176 or during single-stepping of the probed instruction, Kprobes calls
177 kp->fault_handler. Any or all handlers can be NULL.
179 register_kprobe() returns 0 on success, or a negative errno otherwise.
181 User's pre-handler (kp->pre_handler):
182 #include <linux/kprobes.h>
183 #include <linux/ptrace.h>
184 int pre_handler(struct kprobe *p, struct pt_regs *regs);
186 Called with p pointing to the kprobe associated with the breakpoint,
187 and regs pointing to the struct containing the registers saved when
188 the breakpoint was hit. Return 0 here unless you're a Kprobes geek.
190 User's post-handler (kp->post_handler):
191 #include <linux/kprobes.h>
192 #include <linux/ptrace.h>
193 void post_handler(struct kprobe *p, struct pt_regs *regs,
194 unsigned long flags);
196 p and regs are as described for the pre_handler. flags always seems
199 User's fault-handler (kp->fault_handler):
200 #include <linux/kprobes.h>
201 #include <linux/ptrace.h>
202 int fault_handler(struct kprobe *p, struct pt_regs *regs, int trapnr);
204 p and regs are as described for the pre_handler. trapnr is the
205 architecture-specific trap number associated with the fault (e.g.,
206 on i386, 13 for a general protection fault or 14 for a page fault).
207 Returns 1 if it successfully handled the exception.
211 #include <linux/kprobes.h>
212 int register_jprobe(struct jprobe *jp)
214 Sets a breakpoint at the address jp->kp.addr, which must be the address
215 of the first instruction of a function. When the breakpoint is hit,
216 Kprobes runs the handler whose address is jp->entry.
218 The handler should have the same arg list and return type as the probed
219 function; and just before it returns, it must call jprobe_return().
220 (The handler never actually returns, since jprobe_return() returns
221 control to Kprobes.) If the probed function is declared asmlinkage,
222 fastcall, or anything else that affects how args are passed, the
223 handler's declaration must match.
225 register_jprobe() returns 0 on success, or a negative errno otherwise.
227 4.3 register_kretprobe
229 #include <linux/kprobes.h>
230 int register_kretprobe(struct kretprobe *rp);
232 Establishes a return probe for the function whose address is
233 rp->kp.addr. When that function returns, Kprobes calls rp->handler.
234 You must set rp->maxactive appropriately before you call
235 register_kretprobe(); see "How Does a Return Probe Work?" for details.
237 register_kretprobe() returns 0 on success, or a negative errno
240 User's return-probe handler (rp->handler):
241 #include <linux/kprobes.h>
242 #include <linux/ptrace.h>
243 int kretprobe_handler(struct kretprobe_instance *ri, struct pt_regs *regs);
245 regs is as described for kprobe.pre_handler. ri points to the
246 kretprobe_instance object, of which the following fields may be
248 - ret_addr: the return address
249 - rp: points to the corresponding kretprobe object
250 - task: points to the corresponding task struct
251 The handler's return value is currently ignored.
253 4.4 unregister_*probe
255 #include <linux/kprobes.h>
256 void unregister_kprobe(struct kprobe *kp);
257 void unregister_jprobe(struct jprobe *jp);
258 void unregister_kretprobe(struct kretprobe *rp);
260 Removes the specified probe. The unregister function can be called
261 at any time after the probe has been registered.
263 5. Kprobes Features and Limitations
265 As of Linux v2.6.12, Kprobes allows multiple probes at the same
266 address. Currently, however, there cannot be multiple jprobes on
267 the same function at the same time.
269 In general, you can install a probe anywhere in the kernel.
270 In particular, you can probe interrupt handlers. Known exceptions
271 are discussed in this section.
273 For obvious reasons, it's a bad idea to install a probe in
274 the code that implements Kprobes (mostly kernel/kprobes.c and
275 arch/*/kernel/kprobes.c). A patch in the v2.6.13 timeframe instructs
276 Kprobes to reject such requests.
278 If you install a probe in an inline-able function, Kprobes makes
279 no attempt to chase down all inline instances of the function and
280 install probes there. gcc may inline a function without being asked,
281 so keep this in mind if you're not seeing the probe hits you expect.
283 A probe handler can modify the environment of the probed function
284 -- e.g., by modifying kernel data structures, or by modifying the
285 contents of the pt_regs struct (which are restored to the registers
286 upon return from the breakpoint). So Kprobes can be used, for example,
287 to install a bug fix or to inject faults for testing. Kprobes, of
288 course, has no way to distinguish the deliberately injected faults
289 from the accidental ones. Don't drink and probe.
291 Kprobes makes no attempt to prevent probe handlers from stepping on
292 each other -- e.g., probing printk() and then calling printk() from a
293 probe handler. As of Linux v2.6.12, if a probe handler hits a probe,
294 that second probe's handlers won't be run in that instance.
296 In Linux v2.6.12 and previous versions, Kprobes' data structures are
297 protected by a single lock that is held during probe registration and
298 unregistration and while handlers are run. Thus, no two handlers
299 can run simultaneously. To improve scalability on SMP systems,
300 this restriction will probably be removed soon, in which case
301 multiple handlers (or multiple instances of the same handler) may
302 run concurrently on different CPUs. Code your handlers accordingly.
304 Kprobes does not use semaphores or allocate memory except during
305 registration and unregistration.
307 Probe handlers are run with preemption disabled. Depending on the
308 architecture, handlers may also run with interrupts disabled. In any
309 case, your handler should not yield the CPU (e.g., by attempting to
310 acquire a semaphore).
312 Since a return probe is implemented by replacing the return
313 address with the trampoline's address, stack backtraces and calls
314 to __builtin_return_address() will typically yield the trampoline's
315 address instead of the real return address for kretprobed functions.
316 (As far as we can tell, __builtin_return_address() is used only
317 for instrumentation and error reporting.)
319 If the number of times a function is called does not match the
320 number of times it returns, registering a return probe on that
321 function may produce undesirable results. We have the do_exit()
322 and do_execve() cases covered. do_fork() is not an issue. We're
323 unaware of other specific cases where this could be a problem.
327 On a typical CPU in use in 2005, a kprobe hit takes 0.5 to 1.0
328 microseconds to process. Specifically, a benchmark that hits the same
329 probepoint repeatedly, firing a simple handler each time, reports 1-2
330 million hits per second, depending on the architecture. A jprobe or
331 return-probe hit typically takes 50-75% longer than a kprobe hit.
332 When you have a return probe set on a function, adding a kprobe at
333 the entry to that function adds essentially no overhead.
335 Here are sample overhead figures (in usec) for different architectures.
336 k = kprobe; j = jprobe; r = return probe; kr = kprobe + return probe
337 on same function; jr = jprobe + return probe on same function
339 i386: Intel Pentium M, 1495 MHz, 2957.31 bogomips
340 k = 0.57 usec; j = 1.00; r = 0.92; kr = 0.99; jr = 1.40
342 x86_64: AMD Opteron 246, 1994 MHz, 3971.48 bogomips
343 k = 0.49 usec; j = 0.76; r = 0.80; kr = 0.82; jr = 1.07
345 ppc64: POWER5 (gr), 1656 MHz (SMT disabled, 1 virtual CPU per physical CPU)
346 k = 0.77 usec; j = 1.31; r = 1.26; kr = 1.45; jr = 1.99
350 a. SystemTap (http://sourceware.org/systemtap): Work in progress
351 to provide a simplified programming interface for probe-based
353 b. Improved SMP scalability: Currently, work is in progress to handle
354 multiple kprobes in parallel.
355 c. Kernel return probes for sparc64.
356 d. Support for other architectures.
357 e. User-space probes.
361 Here's a sample kernel module showing the use of kprobes to dump a
362 stack trace and selected i386 registers when do_fork() is called.
365 #include <linux/kernel.h>
366 #include <linux/module.h>
367 #include <linux/kprobes.h>
368 #include <linux/kallsyms.h>
369 #include <linux/sched.h>
371 /*For each probe you need to allocate a kprobe structure*/
372 static struct kprobe kp;
374 /*kprobe pre_handler: called just before the probed instruction is executed*/
375 int handler_pre(struct kprobe *p, struct pt_regs *regs)
377 printk("pre_handler: p->addr=0x%p, eip=%lx, eflags=0x%lx\n",
378 p->addr, regs->eip, regs->eflags);
383 /*kprobe post_handler: called after the probed instruction is executed*/
384 void handler_post(struct kprobe *p, struct pt_regs *regs, unsigned long flags)
386 printk("post_handler: p->addr=0x%p, eflags=0x%lx\n",
387 p->addr, regs->eflags);
390 /* fault_handler: this is called if an exception is generated for any
391 * instruction within the pre- or post-handler, or when Kprobes
392 * single-steps the probed instruction.
394 int handler_fault(struct kprobe *p, struct pt_regs *regs, int trapnr)
396 printk("fault_handler: p->addr=0x%p, trap #%dn",
398 /* Return 0 because we don't handle the fault. */
402 int init_module(void)
405 kp.pre_handler = handler_pre;
406 kp.post_handler = handler_post;
407 kp.fault_handler = handler_fault;
408 kp.addr = (kprobe_opcode_t*) kallsyms_lookup_name("do_fork");
409 /* register the kprobe now */
411 printk("Couldn't find %s to plant kprobe\n", "do_fork");
414 ret = register_kprobe(&kp);
416 printk("register_kprobe failed, returned %d\n", ret);
419 printk("kprobe registered\n");
423 void cleanup_module(void)
425 unregister_kprobe(&kp);
426 printk("kprobe unregistered\n");
429 MODULE_LICENSE("GPL");
432 You can build the kernel module, kprobe-example.ko, using the following
435 obj-m := kprobe-example.o
436 KDIR := /lib/modules/$(shell uname -r)/build
439 $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
441 rm -f *.mod.c *.ko *.o
447 # insmod kprobe-example.ko
449 You will see the trace data in /var/log/messages and on the console
450 whenever do_fork() is invoked to create a new process.
454 Here's a sample kernel module showing the use of jprobes to dump
455 the arguments of do_fork().
457 /*jprobe-example.c */
458 #include <linux/kernel.h>
459 #include <linux/module.h>
460 #include <linux/fs.h>
461 #include <linux/uio.h>
462 #include <linux/kprobes.h>
463 #include <linux/kallsyms.h>
466 * Jumper probe for do_fork.
467 * Mirror principle enables access to arguments of the probed routine
468 * from the probe handler.
471 /* Proxy routine having the same arguments as actual do_fork() routine */
472 long jdo_fork(unsigned long clone_flags, unsigned long stack_start,
473 struct pt_regs *regs, unsigned long stack_size,
474 int __user * parent_tidptr, int __user * child_tidptr)
476 printk("jprobe: clone_flags=0x%lx, stack_size=0x%lx, regs=0x%p\n",
477 clone_flags, stack_size, regs);
478 /* Always end with a call to jprobe_return(). */
484 static struct jprobe my_jprobe = {
485 .entry = (kprobe_opcode_t *) jdo_fork
488 int init_module(void)
491 my_jprobe.kp.addr = (kprobe_opcode_t *) kallsyms_lookup_name("do_fork");
492 if (!my_jprobe.kp.addr) {
493 printk("Couldn't find %s to plant jprobe\n", "do_fork");
497 if ((ret = register_jprobe(&my_jprobe)) <0) {
498 printk("register_jprobe failed, returned %d\n", ret);
501 printk("Planted jprobe at %p, handler addr %p\n",
502 my_jprobe.kp.addr, my_jprobe.entry);
506 void cleanup_module(void)
508 unregister_jprobe(&my_jprobe);
509 printk("jprobe unregistered\n");
512 MODULE_LICENSE("GPL");
515 Build and insert the kernel module as shown in the above kprobe
516 example. You will see the trace data in /var/log/messages and on
517 the console whenever do_fork() is invoked to create a new process.
518 (Some messages may be suppressed if syslogd is configured to
519 eliminate duplicate messages.)
521 10. Kretprobes Example
523 Here's a sample kernel module showing the use of return probes to
524 report failed calls to sys_open().
526 /*kretprobe-example.c*/
527 #include <linux/kernel.h>
528 #include <linux/module.h>
529 #include <linux/kprobes.h>
530 #include <linux/kallsyms.h>
532 static const char *probed_func = "sys_open";
534 /* Return-probe handler: If the probed function fails, log the return value. */
535 static int ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
537 // Substitute the appropriate register name for your architecture --
538 // e.g., regs->rax for x86_64, regs->gpr[3] for ppc64.
539 int retval = (int) regs->eax;
541 printk("%s returns %d\n", probed_func, retval);
546 static struct kretprobe my_kretprobe = {
547 .handler = ret_handler,
548 /* Probe up to 20 instances concurrently. */
552 int init_module(void)
555 my_kretprobe.kp.addr =
556 (kprobe_opcode_t *) kallsyms_lookup_name(probed_func);
557 if (!my_kretprobe.kp.addr) {
558 printk("Couldn't find %s to plant return probe\n", probed_func);
561 if ((ret = register_kretprobe(&my_kretprobe)) < 0) {
562 printk("register_kretprobe failed, returned %d\n", ret);
565 printk("Planted return probe at %p\n", my_kretprobe.kp.addr);
569 void cleanup_module(void)
571 unregister_kretprobe(&my_kretprobe);
572 printk("kretprobe unregistered\n");
573 /* nmissed > 0 suggests that maxactive was set too low. */
574 printk("Missed probing %d instances of %s\n",
575 my_kretprobe.nmissed, probed_func);
578 MODULE_LICENSE("GPL");
581 Build and insert the kernel module as shown in the above kprobe
582 example. You will see the trace data in /var/log/messages and on the
583 console whenever sys_open() returns a negative value. (Some messages
584 may be suppressed if syslogd is configured to eliminate duplicate
587 For additional information on Kprobes, refer to the following URLs:
588 http://www-106.ibm.com/developerworks/library/l-kprobes.html?ca=dgr-lnxw42Kprobe
589 http://www.redhat.com/magazine/005mar05/features/kprobes/