2013-10-17 12:57:53

by Liuyongan

[permalink] [raw]
Subject: kprobe pre_handler change return IP

I use kprobe to probe a function suppose:
int is_winter(int num) { ... }
int replace_is_winter(int num) { ...}
I want to replace is_winter() with replace_is_winter(), so when we call is_winter, replace_is_winter will be called.

so:
int my_pre_handler(struct kprobe *p, struct pt_regs *regs)
{
regs->ip = (unsigned long)&replace_is_winter;
return 1;
}

and echo 0 > /proc/sys/debug/kprobes-optimization so that jump instruction will not be used.

I got a exception in fault_handler, and trap number is 14.

fault_handler: p->addr = 0xffffffffa08e201a, ip = ffffffff8021c59d, trap #14n

Anyone here can help me ?
????{.n?+???????+%?????ݶ??w??{.n?+????{??G?????{ay?ʇڙ?,j??f???h?????????z_??(?階?ݢj"???m??????G????????????&???~???iO???z??v?^?m???? ????????I?


Subject: Re: kprobe pre_handler change return IP

(2013/10/17 21:57), Liuyongan wrote:
> I use kprobe to probe a function suppose:
> int is_winter(int num) { ... }
> int replace_is_winter(int num) { ...}
> I want to replace is_winter() with replace_is_winter(), so when we call is_winter, replace_is_winter will be called.
>
> so:
> int my_pre_handler(struct kprobe *p, struct pt_regs *regs)
> {
> regs->ip = (unsigned long)&replace_is_winter;
> return 1;
> }
>
> and echo 0 > /proc/sys/debug/kprobes-optimization so that jump instruction will not be used.
>
> I got a exception in fault_handler, and trap number is 14.
>
> fault_handler: p->addr = 0xffffffffa08e201a, ip = ffffffff8021c59d, trap #14n
>
> Anyone here can help me ?

If you want to replace something with kprobes, the pre_handler must clean current_kprobe up.
Actually the same thing has been done in setup_detour_execution(). So, what you need to do is

> int my_pre_handler(struct kprobe *p, struct pt_regs *regs)
> {
> regs->ip = (unsigned long)&replace_is_winter;

reset_current_kprobe();
preempt_enable_no_resched();

> return 1;
> }

Happy hacking! ;)


Thank you,

--
Masami HIRAMATSU
IT Management Research Dept. Linux Technology Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]

2013-10-21 06:16:05

by Liuyongan

[permalink] [raw]
Subject: RE: kprobe pre_handler change return IP



> -----Original Message-----
> From: Masami Hiramatsu [mailto:[email protected]]
> Sent: Sunday, October 20, 2013 12:00 AM
> To: Liuyongan
> Cc: [email protected]; Qianhuibin
> Subject: Re: kprobe pre_handler change return IP
>
> (2013/10/17 21:57), Liuyongan wrote:
> > I use kprobe to probe a function suppose:
> > int is_winter(int num) { ... }
> > int replace_is_winter(int num) { ...}
> > I want to replace is_winter() with replace_is_winter(), so when we call
> is_winter, replace_is_winter will be called.
> >
> > so:
> > int my_pre_handler(struct kprobe *p, struct pt_regs *regs)
> > {
> > regs->ip = (unsigned long)&replace_is_winter;
> > return 1;
> > }
> >
> > and echo 0 > /proc/sys/debug/kprobes-optimization so that jump instruction
> will not be used.
> >
> > I got a exception in fault_handler, and trap number is 14.
> >
> > fault_handler: p->addr = 0xffffffffa08e201a, ip = ffffffff8021c59d, trap
> #14n
> >
> > Anyone here can help me ?
>
> If you want to replace something with kprobes, the pre_handler must clean
> current_kprobe up.
> Actually the same thing has been done in setup_detour_execution(). So, what
> you need to do is

Great! It works.
As my_pre_handler() in another modules, I should export current_kprobe using

EXPORT_PER_CPU_SYMBOL(current_kprobe);

in arch/x86/kernel/kprobes.c right after current_kprobe's definition.

>
> > int my_pre_handler(struct kprobe *p, struct pt_regs *regs)
> > {
> > regs->ip = (unsigned long)&replace_is_winter;
>
> reset_current_kprobe();
> preempt_enable_no_resched();
>
> > return 1;
> > }
>
> Happy hacking! ;)
>
>
> Thank you,
>
> --
> Masami HIRAMATSU
> IT Management Research Dept. Linux Technology Center
> Hitachi, Ltd., Yokohama Research Laboratory
> E-mail: [email protected]
>

????{.n?+???????+%?????ݶ??w??{.n?+????{??G?????{ay?ʇڙ?,j??f???h?????????z_??(?階?ݢj"???m??????G????????????&???~???iO???z??v?^?m???? ????????I?