aboutsummaryrefslogtreecommitdiffstats
path: root/Documentation
diff options
context:
space:
mode:
authorAnanth N Mavinakayanahalli <ananth@in.ibm.com>2006-10-02 05:17:32 -0400
committerLinus Torvalds <torvalds@g5.osdl.org>2006-10-02 10:57:16 -0400
commit09b18203d772db318ef92f6908c439ee5a35a4f9 (patch)
tree70d05f58602ab72b761360dd91ac55f59a9d9600 /Documentation
parentb3f827cb0fe0660c2eacea2c2f9bdb1f225ff768 (diff)
[PATCH] Update Documentation/kprobes.txt
Documentation/kprobes.txt updated to reflect: o In-kernel symbol resolution o CONFIG_KALLSYMS dependency o Usage of JPROBE_ENTRY o Addition of regs_return_value() Also update the references list and usage examples to use correct module interfaces. Signed-off-by: Ananth N Mavinakayanahalli <ananth@in.ibm.com> Acked-by: Jim Keniston <jkenisto@us.ibm.com> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Diffstat (limited to 'Documentation')
-rw-r--r--Documentation/kprobes.txt89
1 files changed, 56 insertions, 33 deletions
diff --git a/Documentation/kprobes.txt b/Documentation/kprobes.txt
index 2c3b1eae4280..ba26201d5023 100644
--- a/Documentation/kprobes.txt
+++ b/Documentation/kprobes.txt
@@ -151,9 +151,9 @@ So that you can load and unload Kprobes-based instrumentation modules,
151make sure "Loadable module support" (CONFIG_MODULES) and "Module 151make sure "Loadable module support" (CONFIG_MODULES) and "Module
152unloading" (CONFIG_MODULE_UNLOAD) are set to "y". 152unloading" (CONFIG_MODULE_UNLOAD) are set to "y".
153 153
154You may also want to ensure that CONFIG_KALLSYMS and perhaps even 154Also make sure that CONFIG_KALLSYMS and perhaps even CONFIG_KALLSYMS_ALL
155CONFIG_KALLSYMS_ALL are set to "y", since kallsyms_lookup_name() 155are set to "y", since kallsyms_lookup_name() is used by the in-kernel
156is a handy, version-independent way to find a function's address. 156kprobe address resolution code.
157 157
158If you need to insert a probe in the middle of a function, you may find 158If you need to insert a probe in the middle of a function, you may find
159it useful to "Compile the kernel with debug info" (CONFIG_DEBUG_INFO), 159it useful to "Compile the kernel with debug info" (CONFIG_DEBUG_INFO),
@@ -179,6 +179,27 @@ occurs during execution of kp->pre_handler or kp->post_handler,
179or during single-stepping of the probed instruction, Kprobes calls 179or during single-stepping of the probed instruction, Kprobes calls
180kp->fault_handler. Any or all handlers can be NULL. 180kp->fault_handler. Any or all handlers can be NULL.
181 181
182NOTE:
1831. With the introduction of the "symbol_name" field to struct kprobe,
184the probepoint address resolution will now be taken care of by the kernel.
185The following will now work:
186
187 kp.symbol_name = "symbol_name";
188
189(64-bit powerpc intricacies such as function descriptors are handled
190transparently)
191
1922. Use the "offset" field of struct kprobe if the offset into the symbol
193to install a probepoint is known. This field is used to calculate the
194probepoint.
195
1963. Specify either the kprobe "symbol_name" OR the "addr". If both are
197specified, kprobe registration will fail with -EINVAL.
198
1994. With CISC architectures (such as i386 and x86_64), the kprobes code
200does not validate if the kprobe.addr is at an instruction boundary.
201Use "offset" with caution.
202
182register_kprobe() returns 0 on success, or a negative errno otherwise. 203register_kprobe() returns 0 on success, or a negative errno otherwise.
183 204
184User's pre-handler (kp->pre_handler): 205User's pre-handler (kp->pre_handler):
@@ -225,6 +246,12 @@ control to Kprobes.) If the probed function is declared asmlinkage,
225fastcall, or anything else that affects how args are passed, the 246fastcall, or anything else that affects how args are passed, the
226handler's declaration must match. 247handler's declaration must match.
227 248
249NOTE: A macro JPROBE_ENTRY is provided to handle architecture-specific
250aliasing of jp->entry. In the interest of portability, it is advised
251to use:
252
253 jp->entry = JPROBE_ENTRY(handler);
254
228register_jprobe() returns 0 on success, or a negative errno otherwise. 255register_jprobe() returns 0 on success, or a negative errno otherwise.
229 256
2304.3 register_kretprobe 2574.3 register_kretprobe
@@ -251,6 +278,11 @@ of interest:
251- ret_addr: the return address 278- ret_addr: the return address
252- rp: points to the corresponding kretprobe object 279- rp: points to the corresponding kretprobe object
253- task: points to the corresponding task struct 280- task: points to the corresponding task struct
281
282The regs_return_value(regs) macro provides a simple abstraction to
283extract the return value from the appropriate register as defined by
284the architecture's ABI.
285
254The handler's return value is currently ignored. 286The handler's return value is currently ignored.
255 287
2564.4 unregister_*probe 2884.4 unregister_*probe
@@ -369,7 +401,6 @@ stack trace and selected i386 registers when do_fork() is called.
369#include <linux/kernel.h> 401#include <linux/kernel.h>
370#include <linux/module.h> 402#include <linux/module.h>
371#include <linux/kprobes.h> 403#include <linux/kprobes.h>
372#include <linux/kallsyms.h>
373#include <linux/sched.h> 404#include <linux/sched.h>
374 405
375/*For each probe you need to allocate a kprobe structure*/ 406/*For each probe you need to allocate a kprobe structure*/
@@ -403,18 +434,14 @@ int handler_fault(struct kprobe *p, struct pt_regs *regs, int trapnr)
403 return 0; 434 return 0;
404} 435}
405 436
406int init_module(void) 437static int __init kprobe_init(void)
407{ 438{
408 int ret; 439 int ret;
409 kp.pre_handler = handler_pre; 440 kp.pre_handler = handler_pre;
410 kp.post_handler = handler_post; 441 kp.post_handler = handler_post;
411 kp.fault_handler = handler_fault; 442 kp.fault_handler = handler_fault;
412 kp.addr = (kprobe_opcode_t*) kallsyms_lookup_name("do_fork"); 443 kp.symbol_name = "do_fork";
413 /* register the kprobe now */ 444
414 if (!kp.addr) {
415 printk("Couldn't find %s to plant kprobe\n", "do_fork");
416 return -1;
417 }
418 if ((ret = register_kprobe(&kp) < 0)) { 445 if ((ret = register_kprobe(&kp) < 0)) {
419 printk("register_kprobe failed, returned %d\n", ret); 446 printk("register_kprobe failed, returned %d\n", ret);
420 return -1; 447 return -1;
@@ -423,12 +450,14 @@ int init_module(void)
423 return 0; 450 return 0;
424} 451}
425 452
426void cleanup_module(void) 453static void __exit kprobe_exit(void)
427{ 454{
428 unregister_kprobe(&kp); 455 unregister_kprobe(&kp);
429 printk("kprobe unregistered\n"); 456 printk("kprobe unregistered\n");
430} 457}
431 458
459module_init(kprobe_init)
460module_exit(kprobe_exit)
432MODULE_LICENSE("GPL"); 461MODULE_LICENSE("GPL");
433----- cut here ----- 462----- cut here -----
434 463
@@ -463,7 +492,6 @@ the arguments of do_fork().
463#include <linux/fs.h> 492#include <linux/fs.h>
464#include <linux/uio.h> 493#include <linux/uio.h>
465#include <linux/kprobes.h> 494#include <linux/kprobes.h>
466#include <linux/kallsyms.h>
467 495
468/* 496/*
469 * Jumper probe for do_fork. 497 * Jumper probe for do_fork.
@@ -485,17 +513,13 @@ long jdo_fork(unsigned long clone_flags, unsigned long stack_start,
485} 513}
486 514
487static struct jprobe my_jprobe = { 515static struct jprobe my_jprobe = {
488 .entry = (kprobe_opcode_t *) jdo_fork 516 .entry = JPROBE_ENTRY(jdo_fork)
489}; 517};
490 518
491int init_module(void) 519static int __init jprobe_init(void)
492{ 520{
493 int ret; 521 int ret;
494 my_jprobe.kp.addr = (kprobe_opcode_t *) kallsyms_lookup_name("do_fork"); 522 my_jprobe.kp.symbol_name = "do_fork";
495 if (!my_jprobe.kp.addr) {
496 printk("Couldn't find %s to plant jprobe\n", "do_fork");
497 return -1;
498 }
499 523
500 if ((ret = register_jprobe(&my_jprobe)) <0) { 524 if ((ret = register_jprobe(&my_jprobe)) <0) {
501 printk("register_jprobe failed, returned %d\n", ret); 525 printk("register_jprobe failed, returned %d\n", ret);
@@ -506,12 +530,14 @@ int init_module(void)
506 return 0; 530 return 0;
507} 531}
508 532
509void cleanup_module(void) 533static void __exit jprobe_exit(void)
510{ 534{
511 unregister_jprobe(&my_jprobe); 535 unregister_jprobe(&my_jprobe);
512 printk("jprobe unregistered\n"); 536 printk("jprobe unregistered\n");
513} 537}
514 538
539module_init(jprobe_init)
540module_exit(jprobe_exit)
515MODULE_LICENSE("GPL"); 541MODULE_LICENSE("GPL");
516----- cut here ----- 542----- cut here -----
517 543
@@ -530,16 +556,13 @@ report failed calls to sys_open().
530#include <linux/kernel.h> 556#include <linux/kernel.h>
531#include <linux/module.h> 557#include <linux/module.h>
532#include <linux/kprobes.h> 558#include <linux/kprobes.h>
533#include <linux/kallsyms.h>
534 559
535static const char *probed_func = "sys_open"; 560static const char *probed_func = "sys_open";
536 561
537/* Return-probe handler: If the probed function fails, log the return value. */ 562/* Return-probe handler: If the probed function fails, log the return value. */
538static int ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs) 563static int ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
539{ 564{
540 // Substitute the appropriate register name for your architecture -- 565 int retval = regs_return_value(regs);
541 // e.g., regs->rax for x86_64, regs->gpr[3] for ppc64.
542 int retval = (int) regs->eax;
543 if (retval < 0) { 566 if (retval < 0) {
544 printk("%s returns %d\n", probed_func, retval); 567 printk("%s returns %d\n", probed_func, retval);
545 } 568 }
@@ -552,15 +575,11 @@ static struct kretprobe my_kretprobe = {
552 .maxactive = 20 575 .maxactive = 20
553}; 576};
554 577
555int init_module(void) 578static int __init kretprobe_init(void)
556{ 579{
557 int ret; 580 int ret;
558 my_kretprobe.kp.addr = 581 my_kretprobe.kp.symbol_name = (char *)probed_func;
559 (kprobe_opcode_t *) kallsyms_lookup_name(probed_func); 582
560 if (!my_kretprobe.kp.addr) {
561 printk("Couldn't find %s to plant return probe\n", probed_func);
562 return -1;
563 }
564 if ((ret = register_kretprobe(&my_kretprobe)) < 0) { 583 if ((ret = register_kretprobe(&my_kretprobe)) < 0) {
565 printk("register_kretprobe failed, returned %d\n", ret); 584 printk("register_kretprobe failed, returned %d\n", ret);
566 return -1; 585 return -1;
@@ -569,7 +588,7 @@ int init_module(void)
569 return 0; 588 return 0;
570} 589}
571 590
572void cleanup_module(void) 591static void __exit kretprobe_exit(void)
573{ 592{
574 unregister_kretprobe(&my_kretprobe); 593 unregister_kretprobe(&my_kretprobe);
575 printk("kretprobe unregistered\n"); 594 printk("kretprobe unregistered\n");
@@ -578,6 +597,8 @@ void cleanup_module(void)
578 my_kretprobe.nmissed, probed_func); 597 my_kretprobe.nmissed, probed_func);
579} 598}
580 599
600module_init(kretprobe_init)
601module_exit(kretprobe_exit)
581MODULE_LICENSE("GPL"); 602MODULE_LICENSE("GPL");
582----- cut here ----- 603----- cut here -----
583 604
@@ -590,3 +611,5 @@ messages.)
590For additional information on Kprobes, refer to the following URLs: 611For additional information on Kprobes, refer to the following URLs:
591http://www-106.ibm.com/developerworks/library/l-kprobes.html?ca=dgr-lnxw42Kprobe 612http://www-106.ibm.com/developerworks/library/l-kprobes.html?ca=dgr-lnxw42Kprobe
592http://www.redhat.com/magazine/005mar05/features/kprobes/ 613http://www.redhat.com/magazine/005mar05/features/kprobes/
614http://www-users.cs.umn.edu/~boutcher/kprobes/
615http://www.linuxsymposium.org/2006/linuxsymposium_procv2.pdf (pages 101-115)