diff mbox series

[v2,6/7] trace-cruncher: Define Python type for kprobes

Message ID 20210819130827.12327-7-y.karadz@gmail.com (mailing list archive)
State Accepted
Headers show
Series trace-cruncher: Refactor instances and kprobes | expand

Commit Message

Yordan Karadzhov Aug. 19, 2021, 1:08 p.m. UTC
Adding this new Python type is a preparation for a general
refactorung of the way kprobes are handled by trace-cruncher.

Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
 src/ftracepy-utils.c | 34 ++++++++++++++++++++++++++++++++++
 src/ftracepy-utils.h | 14 ++++++++++++++
 src/ftracepy.c       | 32 ++++++++++++++++++++++++++++++++
 3 files changed, 80 insertions(+)
diff mbox series

Patch

diff --git a/src/ftracepy-utils.c b/src/ftracepy-utils.c
index 5d7d9af..b8bcb4c 100644
--- a/src/ftracepy-utils.c
+++ b/src/ftracepy-utils.c
@@ -1366,6 +1366,40 @@  PyObject *PyFtrace_tc_event_system(PyObject *self)
 	return PyUnicode_FromString(TC_SYS);
 }
 
+struct ftracepy_kprobe {
+	char *event;
+	char *function;
+	char *probe;
+};
+
+PyObject *PyKprobe_event(PyKprobe *self)
+{
+	return PyUnicode_FromString(self->ptrObj->event);
+}
+
+PyObject *PyKprobe_system(PyKprobe *self)
+{
+	return PyUnicode_FromString(TC_SYS);
+}
+
+PyObject *PyKprobe_function(PyKprobe *self)
+{
+	return PyUnicode_FromString(self->ptrObj->function);
+}
+
+PyObject *PyKprobe_probe(PyKprobe *self)
+{
+	return PyUnicode_FromString(self->ptrObj->probe);
+}
+
+void ftracepy_kprobe_free(struct ftracepy_kprobe *kp)
+{
+	free(kp->event);
+	free(kp->function);
+	free(kp->probe);
+	free(kp);
+}
+
 static int unregister_kprobe(const char *event)
 {
 	return tracefs_kprobe_clear_probe(TC_SYS, event, true);
diff --git a/src/ftracepy-utils.h b/src/ftracepy-utils.h
index 04de1f3..17e07e5 100644
--- a/src/ftracepy-utils.h
+++ b/src/ftracepy-utils.h
@@ -24,6 +24,12 @@  C_OBJECT_WRAPPER_DECLARE(tep_handle, PyTep)
 
 C_OBJECT_WRAPPER_DECLARE(tracefs_instance, PyTfsInstance)
 
+struct ftracepy_kprobe;
+
+void ftracepy_kprobe_free(struct ftracepy_kprobe *kp);
+
+C_OBJECT_WRAPPER_DECLARE(ftracepy_kprobe, PyKprobe);
+
 PyObject *PyTepRecord_time(PyTepRecord* self);
 
 PyObject *PyTepRecord_cpu(PyTepRecord* self);
@@ -48,6 +54,14 @@  PyObject *PyTep_get_event(PyTep *self, PyObject *args,
 
 PyObject *PyTfsInstance_dir(PyTfsInstance *self);
 
+PyObject *PyKprobe_event(PyKprobe *self);
+
+PyObject *PyKprobe_system(PyKprobe *self);
+
+PyObject *PyKprobe_function(PyKprobe *self);
+
+PyObject *PyKprobe_probe(PyKprobe *self);
+
 PyObject *PyFtrace_dir(PyObject *self);
 
 PyObject *PyFtrace_detach(PyObject *self, PyObject *args, PyObject *kwargs);
diff --git a/src/ftracepy.c b/src/ftracepy.c
index 3c55e50..c434af9 100644
--- a/src/ftracepy.c
+++ b/src/ftracepy.c
@@ -86,6 +86,34 @@  C_OBJECT_WRAPPER(tracefs_instance, PyTfsInstance,
 		 tracefs_instance_destroy,
 		 tracefs_instance_free)
 
+static PyMethodDef PyKprobe_methods[] = {
+	{"event",
+	 (PyCFunction) PyKprobe_event,
+	 METH_NOARGS,
+	 "Get the name of the kprobe event."
+	},
+	{"system",
+	 (PyCFunction) PyKprobe_system,
+	 METH_NOARGS,
+	 "Get the system name of the kprobe event."
+	},
+	{"function",
+	 (PyCFunction) PyKprobe_function,
+	 METH_NOARGS,
+	 "Get the function name of the kprobe event."
+	},
+	{"probe",
+	 (PyCFunction) PyKprobe_probe,
+	 METH_NOARGS,
+	 "Get the kprobe event definition."
+	},
+	{NULL, NULL, 0, NULL}
+};
+
+C_OBJECT_WRAPPER(ftracepy_kprobe, PyKprobe,
+		 NO_DESTROY,
+		 ftracepy_kprobe_free)
+
 static PyMethodDef ftracepy_methods[] = {
 	{"dir",
 	 (PyCFunction) PyFtrace_dir,
@@ -322,6 +350,9 @@  PyMODINIT_FUNC PyInit_ftracepy(void)
 	if (!PyTfsInstanceTypeInit())
 		return NULL;
 
+	if (!PyKprobeTypeInit())
+		return NULL;
+
 	TFS_ERROR = PyErr_NewException("tracecruncher.ftracepy.tfs_error",
 				       NULL, NULL);
 
@@ -337,6 +368,7 @@  PyMODINIT_FUNC PyInit_ftracepy(void)
 	PyModule_AddObject(module, "tep_event", (PyObject *) &PyTepEventType);
 	PyModule_AddObject(module, "tep_record", (PyObject *) &PyTepRecordType);
 	PyModule_AddObject(module, "tracefs_instance", (PyObject *) &PyTfsInstanceType);
+	PyModule_AddObject(module, "ftracepy_kprobe", (PyObject *) &PyKprobeType);
 
 	PyModule_AddObject(module, "tfs_error", TFS_ERROR);
 	PyModule_AddObject(module, "tep_error", TEP_ERROR);