From: "Yordan Karadzhov (VMware)" <y.karadz@gmail.com>
To: linux-trace-devel@vger.kernel.org
Cc: "Yordan Karadzhov (VMware)" <y.karadz@gmail.com>
Subject: [PATCH 3/4] trace-cruncher: Refactor the python classes for kprobes
Date: Mon, 7 Feb 2022 18:38:59 +0200 [thread overview]
Message-ID: <20220207163900.178524-3-y.karadz@gmail.com> (raw)
In-Reply-To: <20220207163900.178524-1-y.karadz@gmail.com>
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 --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:
--
2.32.0
next prev parent reply other threads:[~2022-02-07 16:54 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-07 16:38 [PATCH 1/4] trace-cruncher: Fix coments & style in ft_utils.py Yordan Karadzhov (VMware)
2022-02-07 16:38 ` [PATCH 2/4] trace-cruncher: Fix misleading comment in start_tracing.py Yordan Karadzhov (VMware)
2022-02-07 16:38 ` Yordan Karadzhov (VMware) [this message]
2022-02-07 16:39 ` [PATCH 4/4] trace-cruncher: Update 'kprobe_open.py' example Yordan Karadzhov (VMware)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220207163900.178524-3-y.karadz@gmail.com \
--to=y.karadz@gmail.com \
--cc=linux-trace-devel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).