Re: [RFC] Kprobes: Multiple probes feature at given address

2005-04-11 Thread Prasanna S Panchamukhi
Thanks Maneesh for your comments. Please find
the patch below.

> [..]
> > Assumption : If a user has already inserted a probe using old 
> > register_kprobe()
> > routine, and later wants to insert another probe at the same address using
> > register_multiprobe() routine, then register_multiprobe() will return 
> > EEXIST.
> > This can be avoided by renaming the interface routines.
> > 
> I am not sure if systemTap can tolerate this resitriction.
> 

Basically lets understand that there are two sets of users
1. One set wants to use the older register_kprobe() interface
and dont want multiprobe complexities.

2. Second set of users want multiprobes (such as systemtap).

By adding two new interface to insert multiprobe should help both
the types of users. And now the new interface in this patch
also accepts the same datatype ie struct kprobe *. Just by
writting the wrappers around these interfaces will help the 
systemtap. I have modified this patch as per your comments.


> I think it should not exit here without un-registering any thing if temp
> is an active_probe. Instead, it should parse the ap->head to look
> for the desired multiprobe to unregsiter.
> 

Let the user take care about this.


> -EEXIST doesnot seem to be a proper error code here. When temp is NULL that 
> means, there is no such kprobe.

modified in the new patch.

Please let me know if you have any issues.

Thanks
Prasanna




Here is an attempt to provide multiple handlers feature as an addon patch over
the existing kprobes infrastructure without even changing existing kprobes
infrastructure. The design goal is to provide a simple, compact multiprobe
feature without even changing a single line of existing kprobes. This patch 
introduces two new interface:

register_multiprobe(struct kprobe *p); and
unregister_multiprobe(struct kprobe *p);

register_multiprobe(struct kprobe *p):
User has to allocate kprobe (defined in kprobes.h) and pass the pointer to 
register_multiprobe();
This routine does some housekeeping by storing reference to individual handlers
and registering kprobes with common handler if the user requests for the first 
time at a given address. On subsequenct calls to insert probes on the same 
address, this routines just adds the individual handlers to the hhlist 
(struct kprobe) without registering the kprobes.
unregister_multiprobe(struct kprobe *p):
User has to pass the kprobe pointer to unregister. This routine just checks
if he is the only active user and calls unregister kprobes. If there are more 
active users, it just removes the individual handlers inserted by this user 
from the hhlist.

Advantages :
1. Layered architecture, need not worry about underlying stuff.
2. Its simple and compact.
3. Wrapper routines can be written over new and existing interface
to handle interface naming issue.
4. It works without even changing a single line of existing
kprobes code.

Assumption : If a user has already inserted a probe using old
register_kprobe()
routine, and later wants to insert another probe at the same address using
register_multiprobe() routine, then register_multiprobe() will return EEXIST.
This can be avoided by renaming the interface routines.

Signed-off-by: Prasanna S Panchamukhi <[EMAIL PROTECTED]>


---

 linux-2.6.12-rc2-prasanna/include/linux/kprobes.h |   26 +++
 linux-2.6.12-rc2-prasanna/kernel/kprobes.c|  152 ++
 2 files changed, 178 insertions(+)

diff -puN kernel/kprobes.c~kprobes-layered-multiple-handlers kernel/kprobes.c
--- linux-2.6.12-rc2/kernel/kprobes.c~kprobes-layered-multiple-handlers 
2005-04-11 13:52:52.0 +0530
+++ linux-2.6.12-rc2-prasanna/kernel/kprobes.c  2005-04-11 13:57:55.0 
+0530
@@ -27,6 +27,9 @@
  * interface to access function arguments.
  * 2004-SepPrasanna S Panchamukhi <[EMAIL PROTECTED]> Changed Kprobes
  * exceptions notifier to be first on the priority list.
+ * 2005-April  Prasanna S Panchamukhi <[EMAIL PROTECTED]> Added multiple
+ * handlers feature as an addon interface over existing kprobes
+ * interface.
  */
 #include 
 #include 
@@ -116,6 +119,153 @@ void unregister_kprobe(struct kprobe *p)
spin_unlock_irqrestore(_lock, flags);
 }
 
+
+/* common kprobes pre handler that gets control when the registered probe
+ * gets fired. This routines is wrapper over the inserted multiple handlers
+ * at a given address and calls individual handlers.
+ */
+int comm_pre_handler(struct kprobe *p, struct pt_regs *regs)
+{
+   struct active_probe *ap;
+   struct hlist_node *node;
+   struct hlist_head *head;
+
+   ap = container_of(p, struct active_probe, comm_probe);
+   head = >head;
+   hlist_for_each(node, head) {
+   struct kprobe *kp = hlist_entry(node, struct kprobe , hhlist);
+   if (kp->pre_handler)
+   kp->pre_handler(kp, regs);
+   }
+   return 

Re: [RFC] Kprobes: Multiple probes feature at given address

2005-04-11 Thread Prasanna S Panchamukhi
Thanks Maneesh for your comments. Please find
the patch below.

 [..]
  Assumption : If a user has already inserted a probe using old 
  register_kprobe()
  routine, and later wants to insert another probe at the same address using
  register_multiprobe() routine, then register_multiprobe() will return 
  EEXIST.
  This can be avoided by renaming the interface routines.
  
 I am not sure if systemTap can tolerate this resitriction.
 

Basically lets understand that there are two sets of users
1. One set wants to use the older register_kprobe() interface
and dont want multiprobe complexities.

2. Second set of users want multiprobes (such as systemtap).

By adding two new interface to insert multiprobe should help both
the types of users. And now the new interface in this patch
also accepts the same datatype ie struct kprobe *. Just by
writting the wrappers around these interfaces will help the 
systemtap. I have modified this patch as per your comments.


 I think it should not exit here without un-registering any thing if temp
 is an active_probe. Instead, it should parse the ap-head to look
 for the desired multiprobe to unregsiter.
 

Let the user take care about this.


 -EEXIST doesnot seem to be a proper error code here. When temp is NULL that 
 means, there is no such kprobe.

modified in the new patch.

Please let me know if you have any issues.

Thanks
Prasanna




Here is an attempt to provide multiple handlers feature as an addon patch over
the existing kprobes infrastructure without even changing existing kprobes
infrastructure. The design goal is to provide a simple, compact multiprobe
feature without even changing a single line of existing kprobes. This patch 
introduces two new interface:

register_multiprobe(struct kprobe *p); and
unregister_multiprobe(struct kprobe *p);

register_multiprobe(struct kprobe *p):
User has to allocate kprobe (defined in kprobes.h) and pass the pointer to 
register_multiprobe();
This routine does some housekeeping by storing reference to individual handlers
and registering kprobes with common handler if the user requests for the first 
time at a given address. On subsequenct calls to insert probes on the same 
address, this routines just adds the individual handlers to the hhlist 
(struct kprobe) without registering the kprobes.
unregister_multiprobe(struct kprobe *p):
User has to pass the kprobe pointer to unregister. This routine just checks
if he is the only active user and calls unregister kprobes. If there are more 
active users, it just removes the individual handlers inserted by this user 
from the hhlist.

Advantages :
1. Layered architecture, need not worry about underlying stuff.
2. Its simple and compact.
3. Wrapper routines can be written over new and existing interface
to handle interface naming issue.
4. It works without even changing a single line of existing
kprobes code.

Assumption : If a user has already inserted a probe using old
register_kprobe()
routine, and later wants to insert another probe at the same address using
register_multiprobe() routine, then register_multiprobe() will return EEXIST.
This can be avoided by renaming the interface routines.

Signed-off-by: Prasanna S Panchamukhi [EMAIL PROTECTED]


---

 linux-2.6.12-rc2-prasanna/include/linux/kprobes.h |   26 +++
 linux-2.6.12-rc2-prasanna/kernel/kprobes.c|  152 ++
 2 files changed, 178 insertions(+)

diff -puN kernel/kprobes.c~kprobes-layered-multiple-handlers kernel/kprobes.c
--- linux-2.6.12-rc2/kernel/kprobes.c~kprobes-layered-multiple-handlers 
2005-04-11 13:52:52.0 +0530
+++ linux-2.6.12-rc2-prasanna/kernel/kprobes.c  2005-04-11 13:57:55.0 
+0530
@@ -27,6 +27,9 @@
  * interface to access function arguments.
  * 2004-SepPrasanna S Panchamukhi [EMAIL PROTECTED] Changed Kprobes
  * exceptions notifier to be first on the priority list.
+ * 2005-April  Prasanna S Panchamukhi [EMAIL PROTECTED] Added multiple
+ * handlers feature as an addon interface over existing kprobes
+ * interface.
  */
 #include linux/kprobes.h
 #include linux/spinlock.h
@@ -116,6 +119,153 @@ void unregister_kprobe(struct kprobe *p)
spin_unlock_irqrestore(kprobe_lock, flags);
 }
 
+
+/* common kprobes pre handler that gets control when the registered probe
+ * gets fired. This routines is wrapper over the inserted multiple handlers
+ * at a given address and calls individual handlers.
+ */
+int comm_pre_handler(struct kprobe *p, struct pt_regs *regs)
+{
+   struct active_probe *ap;
+   struct hlist_node *node;
+   struct hlist_head *head;
+
+   ap = container_of(p, struct active_probe, comm_probe);
+   head = ap-head;
+   hlist_for_each(node, head) {
+   struct kprobe *kp = hlist_entry(node, struct kprobe , hhlist);
+   if (kp-pre_handler)
+   kp-pre_handler(kp, regs);
+   }
+   

Re: [RFC] Kprobes: Multiple probes feature at given address

2005-04-10 Thread Maneesh Soni
On Fri, Apr 08, 2005 at 08:17:46PM +0530, Prasanna S Panchamukhi wrote:
[..]
> Assumption : If a user has already inserted a probe using old 
> register_kprobe()
> routine, and later wants to insert another probe at the same address using
> register_multiprobe() routine, then register_multiprobe() will return EEXIST.
> This can be avoided by renaming the interface routines.
>   
I am not sure if systemTap can tolerate this resitriction.

(Pls look for more comments below)
> Signed-off-by: Prasanna S Panchamukhi <[EMAIL PROTECTED]>
> 
> 
> ---
> 
>  linux-2.6.12-rc2-prasanna/include/linux/kprobes.h |   30 
>  linux-2.6.12-rc2-prasanna/kernel/kprobes.c|  164 
> ++
>  2 files changed, 194 insertions(+)
> 
> diff -puN kernel/kprobes.c~kprobes-layered-multiple-handlers kernel/kprobes.c
> --- linux-2.6.12-rc2/kernel/kprobes.c~kprobes-layered-multiple-handlers   
> 2005-04-08 16:39:05.0 +0530
> +++ linux-2.6.12-rc2-prasanna/kernel/kprobes.c2005-04-08 
> 19:23:11.0 +0530
> @@ -27,6 +27,9 @@
>   *   interface to access function arguments.
>   * 2004-Sep  Prasanna S Panchamukhi <[EMAIL PROTECTED]> Changed Kprobes
>   *   exceptions notifier to be first on the priority list.
> + * 2005-AprilPrasanna S Panchamukhi <[EMAIL PROTECTED]> Added 
> multiple
> + *   handlers feature as an addon interface over existing kprobes
> + *   interface.
>   */
>  #include 
>  #include 
> @@ -44,6 +47,7 @@ static struct hlist_head kprobe_table[KP
>  
>  unsigned int kprobe_cpu = NR_CPUS;
>  static DEFINE_SPINLOCK(kprobe_lock);
> +static DEFINE_RWLOCK(multiprobe_lock);
>  
>  /* Locks kprobe: irqs must be disabled */
>  void lock_kprobes(void)
> @@ -116,6 +120,164 @@ void unregister_kprobe(struct kprobe *p)
>   spin_unlock_irqrestore(_lock, flags);
>  }
>  
> +
> +/* common kprobes pre handler that gets control when the registered probe
> + * gets fired. This routines is wrapper over the inserted multiple handlers
> + * at a given address and calls individual handlers.
> + */
> +int comm_pre_handler(struct kprobe *p, struct pt_regs *regs)
> +{
> + struct active_probe *ap;
> + struct hlist_node *node;
> + struct hlist_head *head;
> +
> + read_lock(_lock);
> + ap = container_of(p, struct active_probe, comm_probe);
> + head = >head;
> + hlist_for_each(node, head) {
> + struct multiprobe *mp =
> + hlist_entry(node, struct multiprobe, hlist);
> + if (mp->kp.pre_handler)
> + mp->kp.pre_handler(>kp, regs);
> + }
> + read_unlock(_lock);
> + return 0;
> +}
> +
> +/* common kprobes post handler that gets control when the registered probe
> + * gets fired. This routines is wrapper over the insereted multiple handlers
> + * at a given address and calls individual handlers.
> + */
> +void comm_post_handler(struct kprobe *p, struct pt_regs *regs,
> + unsigned long flags)
> +{
> + struct active_probe *ap;
> + struct hlist_node *node;
> + struct hlist_head *head;
> +
> + read_lock(_lock);
> + ap = container_of(p, struct active_probe, comm_probe);
> + head = >head;
> + hlist_for_each(node, head) {
> + struct multiprobe *mp =
> + hlist_entry(node, struct multiprobe, hlist);
> + if (mp->kp.post_handler)
> + mp->kp.post_handler(>kp, regs, flags);
> + }
> + read_unlock(_lock);
> + return;
> +}
> +
> +/* common kprobes fault handler that gets control when the registered probe
> + * gets fired. This routines is wrapper over the inserted multiple handlers
> + * at a given address and calls individual handlers.
> + */
> +int comm_fault_handler(struct kprobe *p, struct pt_regs *regs, int trapnr)
> +{
> + struct active_probe *ap;
> + struct hlist_node *node;
> + struct hlist_head *head;
> +
> + read_lock(_lock);
> + ap = container_of(p, struct active_probe, comm_probe);
> + head = >head;
> + hlist_for_each(node, head) {
> + struct multiprobe *mp =
> + hlist_entry(node, struct multiprobe, hlist);
> + if (mp->kp.fault_handler)
> + mp->kp.fault_handler(>kp, regs, trapnr);
> + }
> + read_unlock(_lock);
> + return 1;
> +}
> +
> +/* New interface to support multiple handlers feature without even changing a
> + * single line of exiting kprobes interface and data structures. This 
> routines
> + * accepts pointer to multiprobe structure, user has to allocate
> + * multiprobe structure and pass the pointer. This routine basically checks
> + * and registers the kprobes common handlers if the user is inserting a probe
> + * for the first time and saves the references to individual kprobes 
> handlers.
> + * On subsequent call to this routine to insert multiple handler at the same
> + * 

Re: [RFC] Kprobes: Multiple probes feature at given address

2005-04-10 Thread Maneesh Soni
On Fri, Apr 08, 2005 at 08:17:46PM +0530, Prasanna S Panchamukhi wrote:
[..]
 Assumption : If a user has already inserted a probe using old 
 register_kprobe()
 routine, and later wants to insert another probe at the same address using
 register_multiprobe() routine, then register_multiprobe() will return EEXIST.
 This can be avoided by renaming the interface routines.
   
I am not sure if systemTap can tolerate this resitriction.

(Pls look for more comments below)
 Signed-off-by: Prasanna S Panchamukhi [EMAIL PROTECTED]
 
 
 ---
 
  linux-2.6.12-rc2-prasanna/include/linux/kprobes.h |   30 
  linux-2.6.12-rc2-prasanna/kernel/kprobes.c|  164 
 ++
  2 files changed, 194 insertions(+)
 
 diff -puN kernel/kprobes.c~kprobes-layered-multiple-handlers kernel/kprobes.c
 --- linux-2.6.12-rc2/kernel/kprobes.c~kprobes-layered-multiple-handlers   
 2005-04-08 16:39:05.0 +0530
 +++ linux-2.6.12-rc2-prasanna/kernel/kprobes.c2005-04-08 
 19:23:11.0 +0530
 @@ -27,6 +27,9 @@
   *   interface to access function arguments.
   * 2004-Sep  Prasanna S Panchamukhi [EMAIL PROTECTED] Changed Kprobes
   *   exceptions notifier to be first on the priority list.
 + * 2005-AprilPrasanna S Panchamukhi [EMAIL PROTECTED] Added 
 multiple
 + *   handlers feature as an addon interface over existing kprobes
 + *   interface.
   */
  #include linux/kprobes.h
  #include linux/spinlock.h
 @@ -44,6 +47,7 @@ static struct hlist_head kprobe_table[KP
  
  unsigned int kprobe_cpu = NR_CPUS;
  static DEFINE_SPINLOCK(kprobe_lock);
 +static DEFINE_RWLOCK(multiprobe_lock);
  
  /* Locks kprobe: irqs must be disabled */
  void lock_kprobes(void)
 @@ -116,6 +120,164 @@ void unregister_kprobe(struct kprobe *p)
   spin_unlock_irqrestore(kprobe_lock, flags);
  }
  
 +
 +/* common kprobes pre handler that gets control when the registered probe
 + * gets fired. This routines is wrapper over the inserted multiple handlers
 + * at a given address and calls individual handlers.
 + */
 +int comm_pre_handler(struct kprobe *p, struct pt_regs *regs)
 +{
 + struct active_probe *ap;
 + struct hlist_node *node;
 + struct hlist_head *head;
 +
 + read_lock(multiprobe_lock);
 + ap = container_of(p, struct active_probe, comm_probe);
 + head = ap-head;
 + hlist_for_each(node, head) {
 + struct multiprobe *mp =
 + hlist_entry(node, struct multiprobe, hlist);
 + if (mp-kp.pre_handler)
 + mp-kp.pre_handler(mp-kp, regs);
 + }
 + read_unlock(multiprobe_lock);
 + return 0;
 +}
 +
 +/* common kprobes post handler that gets control when the registered probe
 + * gets fired. This routines is wrapper over the insereted multiple handlers
 + * at a given address and calls individual handlers.
 + */
 +void comm_post_handler(struct kprobe *p, struct pt_regs *regs,
 + unsigned long flags)
 +{
 + struct active_probe *ap;
 + struct hlist_node *node;
 + struct hlist_head *head;
 +
 + read_lock(multiprobe_lock);
 + ap = container_of(p, struct active_probe, comm_probe);
 + head = ap-head;
 + hlist_for_each(node, head) {
 + struct multiprobe *mp =
 + hlist_entry(node, struct multiprobe, hlist);
 + if (mp-kp.post_handler)
 + mp-kp.post_handler(mp-kp, regs, flags);
 + }
 + read_unlock(multiprobe_lock);
 + return;
 +}
 +
 +/* common kprobes fault handler that gets control when the registered probe
 + * gets fired. This routines is wrapper over the inserted multiple handlers
 + * at a given address and calls individual handlers.
 + */
 +int comm_fault_handler(struct kprobe *p, struct pt_regs *regs, int trapnr)
 +{
 + struct active_probe *ap;
 + struct hlist_node *node;
 + struct hlist_head *head;
 +
 + read_lock(multiprobe_lock);
 + ap = container_of(p, struct active_probe, comm_probe);
 + head = ap-head;
 + hlist_for_each(node, head) {
 + struct multiprobe *mp =
 + hlist_entry(node, struct multiprobe, hlist);
 + if (mp-kp.fault_handler)
 + mp-kp.fault_handler(mp-kp, regs, trapnr);
 + }
 + read_unlock(multiprobe_lock);
 + return 1;
 +}
 +
 +/* New interface to support multiple handlers feature without even changing a
 + * single line of exiting kprobes interface and data structures. This 
 routines
 + * accepts pointer to multiprobe structure, user has to allocate
 + * multiprobe structure and pass the pointer. This routine basically checks
 + * and registers the kprobes common handlers if the user is inserting a probe
 + * for the first time and saves the references to individual kprobes 
 handlers.
 + * On subsequent call to this routine to insert multiple handler at the same
 + * address, it just adds the