This is the mail archive of the systemtap@sourceware.org mailing list for the systemtap project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Uprobes: howto calculate the probe offset


Please find the details of calculating uprobe offset below and
let me know if you need more information.

Thanks
Prasanna

1. Allocate a uprobe structure.
	struct uprobe uprobe;

2. Calculate the offset for the application and initialize the
   pathname, address and offset elements.

    Eg : Probe on function foo();
	/* Specify the pathname of the application */
	$char pname[] ="/home/prasanna/MOD/appln";
	$urpobe.pathname = &pname[0];

	/*Speciy the virtual address */
	$cc -o appln app.c
	$nm appln |grep foo()
	$080484a9 T foo

	$uprobe.kp.addr = (kprobe_opcode_t *) 0x080484a9

	/* calculate the offset for function foo() */
	uprobe.offset = uprobe.kp.addr & 0x00000fff;

    Eg: Probe on function printf() defined in libc-2.3.4.so
	/* Specify the pathname of the library*/
	$char pname[] ="/lib/tls/libc-2.3.4.so";
	$uprobe.pathname = &pname[0];

	/*Speciy the virtual address */
	$nm /lib/tls/libc-2.3.4.so |grep printf
	$4ecc9940 W obstack_printf
	$4ecc9830 W obstack_vprintf
	$4ecaf320 T parse_printf_format
	$4ecb1420 T printf
	^^^^^^
	$4ed96088 b __printf_arginfo_table
	$4ed4a050 T __printf_chk
	$4ecacd60 T __printf_fp

	uprobe.kp.addr = (kprobe_opcode_t *) 0x4ecb1420;

	/*Calculate the offset */
	$cat /proc/1/mmaps
	$08048000-0804f000 r-xp 00000000 08:02 1144758    /sbin/init
	$0804f000-08050000 rw-p 00007000 08:02 1144758    /sbin/init
	$09d5d000-09d7e000 rw-p 09d5d000 00:00 0
	$4ec56000-4ec6b000 r-xp 00000000 08:02 1111946    /lib/ld-2.3.4.so
	$4ec6b000-4ec6c000 r--p 00015000 08:02 1111946    /lib/ld-2.3.4.so
	$4ec6c000-4ec6d000 rw-p 00016000 08:02 1111946    /lib/ld-2.3.4.so
	$4ec6f000-4ed93000 r-xp 00000000 08:02 1111959    /lib/tls/libc-2.3.4.so
	^^^^^^^^
	$4ed93000-4ed94000 r--p 00124000 08:02 1111959    /lib/tls/libc-2.3.4.so
	$4ed94000-4ed97000 rw-p 00125000 08:02 1111959    /lib/tls/libc-2.3.4.so
	$4ed97000-4ed99000 rw-p 4ed97000 00:00 0

	/* Lookup where the libc-2.3.4.so is mapped. From above you can see
	   the beginning map address is 0x4ec6f000 */
	$uprobe.offset  =
		(unsigned long)(((unsinged long)urpobe.kp.address) - 0x4ec6f000)
			= 0x4ecb1420 -  0x4ec6f000;
			= 0x42420;
3. Now initialize handlers apart from offset, address and pathname.

        $uprobe.kp.pre_handler = handler_pre;
        $uprobe.kp.post_handler = handler_post;
        $uprobe.kp.fault_handler = handler_fault;

4. Insert probes by registering userspace probe in the init_module.
	register_uprobe(&uprobe);

5. Remove probes by unregistering userspace probe in cleanup_module.
	unregister_uprobe(&uprobe);
-- 
Prasanna S Panchamukhi
Linux Technology Center
India Software Labs, IBM Bangalore
Email: prasanna@in.ibm.com
Ph: 91-80-25044636


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]