diff mbox series

[2/2] trace-cruncher: Provide short info print

Message ID 20210729132256.86569-2-y.karadz@gmail.com (mailing list archive)
State Not Applicable
Headers show
Series [1/2] trace-cruncher: Add generic methods for printing | expand

Commit Message

Yordan Karadzhov July 29, 2021, 1:22 p.m. UTC
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.

Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
 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(-)
diff mbox series

Patch

diff --git a/examples/kprobe_open.py b/examples/kprobe_open.py
index 5b217d3..7242e5e 100755
--- a/examples/kprobe_open.py
+++ b/examples/kprobe_open.py
@@ -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))
diff --git a/src/ftracepy-utils.c b/src/ftracepy-utils.c
index 3c000a7..0f0e907 100644
--- a/src/ftracepy-utils.c
+++ b/src/ftracepy-utils.c
@@ -454,6 +454,60 @@  PyObject *PyTep_process(PyTep *self, PyObject *args,
 	return PyUnicode_FromString(seq.buffer);
 }
 
+static int info_short(struct trace_seq *s,
+		      struct tep_record *record,
+		      struct tep_event *event,
+		      void *context)
+{
+	struct tep_format_field *field;
+
+	field = event->format.fields->next;
+	while (field) {
+		/*
+		 * For the moment all number fields are printed as hex.
+		 *
+		 * TODO: Find a way to specify if the field will have
+		 * the bit flag TEP_FIELD_IS_LONG set at the time the
+		 * kprobe is created. This way we will be able to specify
+		 * if a number field in the probe will be printed as hex
+		 * or as dec.
+		 */
+		if (is_number(field))
+			field->flags |= TEP_FIELD_IS_LONG;
+
+		trace_seq_printf(s, " %s=", field->name);
+		tep_print_field(s, record->data, field);
+		field = field->next;
+	}
+
+	return 0;
+}
+
+PyObject *PyFtrace_register_event_print(PyTep *self, PyObject *args,
+						     PyObject *kwargs)
+{
+	static char *kwlist[] = {"system", "event", "id", NULL};
+	const char *system, *event;
+	int ret, id = -1;
+
+	system = event = NO_ARG;
+
+	if(!PyArg_ParseTupleAndKeywords(args,
+					kwargs,
+					"ss|i",
+					kwlist,
+					&system,
+					&event,
+					&id)) {
+		return false;
+	}
+
+	ret = tep_register_event_handler(self->ptrObj, id, system, event,
+					 info_short, NULL);
+
+	return PyLong_FromLong(ret);
+}
+
 static bool check_file(struct tracefs_instance *instance, const char *file)
 {
 	if (!tracefs_file_exists(instance, file)) {
diff --git a/src/ftracepy-utils.h b/src/ftracepy-utils.h
index aca1ccc..37b785e 100644
--- a/src/ftracepy-utils.h
+++ b/src/ftracepy-utils.h
@@ -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,
diff --git a/src/ftracepy.c b/src/ftracepy.c
index e948b15..b095f64 100644
--- a/src/ftracepy.c
+++ b/src/ftracepy.c
@@ -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}
 };
 
diff --git a/tracecruncher/ft_utils.py b/tracecruncher/ft_utils.py
index 8c245b1..9c4d330 100644
--- a/tracecruncher/ft_utils.py
+++ b/tracecruncher/ft_utils.py
@@ -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.