Skip to contentSkip to conversation

trace-cruncher: Add generic methods for printing

PATCH New Yordan Karadzhov (VMware) 1 participant 2 messages Jul 29, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 trace-cruncher: Add generic methods for printing

The new methods makes it easy to print:
- the name and PID of the process that generated the trace record,
- the unique fields of the record (called also 'info') or
- all information for the event all together.

---
 examples/kprobe_open.py |   8 +-
 src/ftracepy-utils.c    | 159 ++++++++++++++++++++++++++++++++++++++++
 src/ftracepy-utils.h    |   9 +++
 src/ftracepy.c          |  15 ++++
 4 files changed, 186 insertions(+), 5 deletions(-)
examples/kprobe_open.py +3 -5
index f8d4d8b..5b217d3 100755
@@ -26,12 +26,10 @@ open_probe.add_ptr_arg(name='mode',
 
 open_probe.register()
 
+tep = tc.local_tep()
+
 def callback(event, record):
-    if event.id() == open_probe.id():
-        file_name = event.parse_record_field(record=record, field='file')
-        flags = event.parse_record_field(record, 'flags')
-        mode = event.parse_record_field(record, 'mode')
-        print('file: {0}  (flags: {1}; mode: {2})'.format(file_name, hex(flags), hex(mode)))
+    print(tep.info(event, record))
 
 
 if __name__ == "__main__":
src/ftracepy-utils.c +159 -0

169 lines not on this page · raw message

src/ftracepy-utils.h +9 -0
index 5d7c19c..aca1ccc 100644
@@ -44,6 +44,15 @@ PyObject *PyTep_init_local(PyTep *self, PyObject *args,
 PyObject *PyTep_get_event(PyTep *self, PyObject *args,
 				       PyObject *kwargs);
 
+PyObject *PyTep_event_record(PyTep *self, PyObject *args,
+					  PyObject *kwargs);
+
+PyObject *PyTep_info(PyTep *self, PyObject *args,
+				  PyObject *kwargs);
+
+PyObject *PyTep_process(PyTep *self, PyObject *args,
+				     PyObject *kwargs);
+
 PyObject *PyFtrace_dir(PyObject *self);
 
 PyObject *PyFtrace_create_instance(PyObject *self, PyObject *args,
src/ftracepy.c +15 -0
index e3fec7b..e948b15 100644
@@ -68,6 +68,21 @@ static PyMethodDef PyTep_methods[] = {
 	 METH_VARARGS | METH_KEYWORDS,
 	 "Get a PyTepEvent object."
 	},
+	{"event_record",
+	 (PyCFunction) PyTep_event_record,
+	 METH_VARARGS | METH_KEYWORDS,
+	 "Generic print of a trace event."
+	},
+	{"process",
+	 (PyCFunction) PyTep_process,
+	 METH_VARARGS | METH_KEYWORDS,
+	 "Generic print of the process that generated the trace event."
+	},
+	{"info",
+	 (PyCFunction) PyTep_info,
+	 METH_VARARGS | METH_KEYWORDS,
+	 "Generic print of a trace event info."
+	},
 	{NULL}
 };
 
Signature · 1 line
2.30.2
#1 lore raw

PATCH 2/2 trace-cruncher: Provide short info print

The default event handler for parsing ("info print") includes the address of the probe. This address can be valuable for kernel developers, but is meaningless if you only care for userspace. Here we add a method that allows the user to register an alternative event handler that doesn't show the address.

---
 examples/kprobe_open.py   |  5 ++--
 src/ftracepy-utils.c      | 54 +++++++++++++++++++++++++++++++++++++++
 src/ftracepy-utils.h      |  3 +++
 src/ftracepy.c            |  5 ++++
 tracecruncher/ft_utils.py |  7 +++++
 5 files changed, 72 insertions(+), 2 deletions(-)
examples/kprobe_open.py +3 -2
index 5b217d3..7242e5e 100755
@@ -17,16 +17,17 @@ open_probe.add_string_arg(name='file', param_id=2)
 
 open_probe.add_ptr_arg(name='flags',
                        param_id=3,
-                       param_type='x64')
+                       param_type='u64')
 
 open_probe.add_ptr_arg(name='mode',
                        param_id=3,
-                       param_type='x64',
+                       param_type='u64',
                        offset=8)
 
 open_probe.register()
 
 tep = tc.local_tep()
+tc.register_event_prints(tep, [open_probe])
 
 def callback(event, record):
     print(tep.info(event, record))
src/ftracepy-utils.c +54 -0

60 lines not on this page · raw message

src/ftracepy-utils.h +3 -0
index aca1ccc..37b785e 100644
@@ -53,6 +53,9 @@ PyObject *PyTep_info(PyTep *self, PyObject *args,
 PyObject *PyTep_process(PyTep *self, PyObject *args,
 				     PyObject *kwargs);
 
+PyObject *PyFtrace_register_event_print(PyTep *self, PyObject *args,
+						     PyObject *kwargs);
+
 PyObject *PyFtrace_dir(PyObject *self);
 
 PyObject *PyFtrace_create_instance(PyObject *self, PyObject *args,
src/ftracepy.c +5 -0
index e948b15..b095f64 100644
@@ -83,6 +83,11 @@ static PyMethodDef PyTep_methods[] = {
 	 METH_VARARGS | METH_KEYWORDS,
 	 "Generic print of a trace event info."
 	},
+	{"register_event_parser",
+	 (PyCFunction) PyFtrace_register_event_print,
+	 METH_VARARGS | METH_KEYWORDS,
+	 "Register default event print."
+	},
 	{NULL}
 };
 
tracecruncher/ft_utils.py +7 -0
index 8c245b1..9c4d330 100644
@@ -29,6 +29,13 @@ def find_event_id(system, event):
     return tep.get_event(system=system, name=event).id()
 
 
+def register_event_prints(tep, events):
+    """ Register default (short) print for these events.
+    """
+    for e in events:
+        tep.register_event_parser(id=e.evt_id, system=e.system, event=e.name)
+
+
 class event:
     def __init__(self, system, name, static=True):
         """ Constructor.
Signature · 1 line
2.30.2