diff mbox series

[3/4] trace-cruncher: Refactor the python classes for kprobes

Message ID 20220207163900.178524-3-y.karadz@gmail.com (mailing list archive)
State Accepted
Headers show
Series [1/4] trace-cruncher: Fix coments & style in ft_utils.py | expand

Commit Message

Yordan Karadzhov Feb. 7, 2022, 4:38 p.m. UTC
Currently the usage of the python classes for kprobes exhibits a pattern
that is inconsistent with the usage of the classes for histograms and
synthetic events. The constructors of the histograms and synth. event
classes will directly register the new events on the system, so once the
corresponding object is created it is ready to be used. However, this is not
the case for the constructors of the classes 'kprobe' and 'kretval_probe'.
For these two classes, the user is expected to explicitly call the 'register'
method before using the new dynamic event. Since such inconsistency can
be confusing, here we refactor the 'kprobe' and 'kretval_probe' classes,
eliminating the 'register' method and moving the registration inside the
constructor.

Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
 tracecruncher/ft_utils.py | 136 ++++++++++++++++++--------------------
 1 file changed, 65 insertions(+), 71 deletions(-)
diff mbox series

Patch

diff --git a/tracecruncher/ft_utils.py b/tracecruncher/ft_utils.py
index 26a7306..741887b 100644
--- a/tracecruncher/ft_utils.py
+++ b/tracecruncher/ft_utils.py
@@ -81,87 +81,86 @@  class event:
                               event=self.name)
 
 
-class kprobe_base(event):
-    def __init__(self, name, func=''):
+class _kprobe_base(event):
+    def __init__(self, name, func):
         """ Constructor.
         """
         super().__init__(system=ft.tc_event_system(), name=name, static=False)
         self.func = func
         self.kp = None
+        self.evt_id = -1
 
-    def set_function(self, name):
-        """ Set the name of the function to be traced.
+    def register(self):
+        """ Register this probe to Ftrace.
         """
-        self.func = name
-
+        self.kp.register()
+        self.evt_id = find_event_id(system=ft.tc_event_system(), event=self.name)
 
-class kprobe(kprobe_base):
-    def __init__(self, name, func=''):
+class kprobe(_kprobe_base):
+    def __init__(self, name, func, fields):
         """ Constructor.
         """
         super().__init__(name, func)
-        self.fields = {}
-
-    def add_raw_field(self, name, probe):
-        """ Add a raw definition of a data field to this probe.
-        """
-        self.fields[str(name)] = str(probe)
-
-    def add_arg(self, name, param_id, param_type):
-        """ Add a function parameter data field to this probe.
-        """
-        probe = '$arg{0}:{1}'.format(param_id, param_type)
-        self.add_raw_field(name, probe)
+        self.fields = fields
+        probe = ' '.join('{!s}={!s}'.format(key,val) for (key, val) in self.fields.items())
+        self.kp = ft.kprobe(event=self.name, function=self.func, probe=probe)
+        self.register()
 
-    def add_ptr_arg(self, name, param_id, param_type, offset=0):
-        """ Add a pointer function parameter data field to this probe.
-        """
-        probe = '+{0}($arg{1}):{2}'.format(offset, param_id, param_type)
-        self.add_raw_field(name, probe)
+def kprobe_add_raw_field(name, probe, fields={}):
+    """ Add a raw definition of a data field to the probe descriptor.
+    """
+    fields[str(name)] = str(probe)
+    return fields
 
-    def add_array_arg(self, name, param_id, param_type, offset=0, size=-1):
-        """ Add an array function parameter data field to this probe.
-        """
-        if size < 0:
-            size = 10
-
-        ptr_size = ctypes.sizeof(ctypes.c_voidp)
-        for i in range(size):
-            field_name = name + str(i)
-            probe = '+{0}(+{1}'.format(offset, i * ptr_size)
-            probe += '($arg{0})):{1}'.format(param_id, param_type)
-            self.add_raw_field(field_name, probe)
-
-    def add_string_arg(self, name, param_id, offset=0, usr_space=False):
-        """ Add a string function parameter data field to this probe.
-        """
-        p_type = 'ustring' if usr_space else 'string'
-        self.add_ptr_arg(name=name,
-                         param_id=param_id,
-                         param_type=p_type,
-                         offset=offset)
-
-    def add_string_array_arg(self, name, param_id, offset=0, usr_space=False, size=-1):
-        """ Add a string array function parameter data field to this probe.
-        """
-        p_type = 'ustring' if usr_space else 'string'
-        self.add_array_arg(name=name,
-                           param_id=param_id,
-                           param_type=p_type,
-                           offset=offset,
-                           size=size)
+def kprobe_add_arg(name, param_id, param_type, fields={}):
+    """ Add a function parameter data field to the probe descriptor.
+    """
+    probe = '$arg{0}:{1}'.format(param_id, param_type)
+    return kprobe_add_raw_field(name=name, probe=probe, fields=fields)
 
-    def register(self):
-        """ Register this probe to Ftrace.
-        """
-        probe = ' '.join('{!s}={!s}'.format(key,val) for (key, val) in self.fields.items())
+def kprobe_add_ptr_arg(name, param_id, param_type, offset=0, fields={}):
+    """ Add a pointer function parameter data field to the probe descriptor.
+    """
+    probe = '+{0}($arg{1}):{2}'.format(offset, param_id, param_type)
+    return kprobe_add_raw_field(name=name, probe=probe, fields=fields)
 
-        self.kp = ft.kprobe(event=self.name, function=self.func, probe=probe)
-        self.kp.register()
-        self.evt_id = find_event_id(system=ft.tc_event_system(), event=self.name)
+def kprobe_add_array_arg(name, param_id, param_type, offset=0,
+                         size=-1, fields={}):
+    """ Add an array function parameter data field to the probe descriptor.
+    """
+    if size < 0:
+        size = 10
 
+    ptr_size = ctypes.sizeof(ctypes.c_voidp)
+    for i in range(size):
+        field_name = name + str(i)
+        probe = '+{0}(+{1}'.format(offset, i * ptr_size)
+        probe += '($arg{0})):{1}'.format(param_id, param_type)
+        return kprobe_add_raw_field(name=field_name, probe=probe, fields=fields)
 
-def parse_record_array_field(event, record, field, size=-1):
+def kprobe_add_string_arg(name, param_id, offset=0, usr_space=False, fields={}):
+    """ Add a string function parameter data field to the probe descriptor.
+    """
+    p_type = 'ustring' if usr_space else 'string'
+    return kprobe_add_ptr_arg(name=name,
+                              param_id=param_id,
+                              param_type=p_type,
+                              offset=offset,
+                              fields=fields)
+
+def kprobe_add_string_array_arg(name, param_id, offset=0, usr_space=False,
+                                size=-1, fields={}):
+    """ Add a string array function parameter data field to the probe descriptor.
+    """
+    p_type = 'ustring' if usr_space else 'string'
+    return kprobe_add_array_arg(name=name,
+                                param_id=param_id,
+                                param_type=p_type,
+                                offset=offset,
+                                size=size,
+                                fields=fields)
+
+def kprobe_parse_record_array_field(event, record, field, size=-1):
     """ Parse the content of an array function parameter data field.
     """
     if size < 0:
@@ -178,18 +177,13 @@  def parse_record_array_field(event, record, field, size=-1):
     return arr
 
 
-class kretval_probe(kprobe_base):
-    def __init__(self, name, func=''):
+class kretval_probe(_kprobe_base):
+    def __init__(self, name, func):
         """ Constructor.
         """
         super().__init__(name, func)
-
-    def register(self):
-        """ Register this probe to Ftrace.
-        """
         self.kp = ft.kprobe(event=self.name, function=self.func)
         self.kp.register()
-        self.evt_id = find_event_id(system=ft.tc_event_system(), event=self.name)
 
 
 class khist: