diff mbox series

[1/2] trace-cruncher: Add 'is_attached()'

Message ID 20220106123505.164550-1-y.karadz@gmail.com (mailing list archive)
State Accepted
Headers show
Series [1/2] trace-cruncher: Add 'is_attached()' | expand

Commit Message

Yordan Karadzhov Jan. 6, 2022, 12:35 p.m. UTC
We already have APIs for attaching/detaching an object from/to
the 'ftracepy' module. For the sake of completeness we have to
add an API that checks if the object is attached.

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

Patch

diff --git a/src/ftracepy-utils.c b/src/ftracepy-utils.c
index 1b1f350..aac31a4 100644
--- a/src/ftracepy-utils.c
+++ b/src/ftracepy-utils.c
@@ -1037,6 +1037,28 @@  PyObject *PyFtrace_attach(PyObject *self, PyObject *args, PyObject *kwargs)
 	return set_destroy(args, kwargs, true);
 }
 
+static bool get_destroy_flag(PyObject *py_obj)
+{
+	PyFtrace_Object_HEAD *obj_head = (PyFtrace_Object_HEAD *)py_obj;
+	return obj_head->destroy;
+}
+
+PyObject *PyFtrace_is_attached(PyObject *self, PyObject *args, PyObject *kwargs)
+{
+	static char *kwlist[] = {"object", NULL};
+	PyObject *py_obj;
+
+	if (!PyArg_ParseTupleAndKeywords(args,
+					 kwargs,
+					 "O",
+					 kwlist,
+					 &py_obj)) {
+		return NULL;
+	}
+
+	return get_destroy_flag(py_obj) ? Py_True : Py_False;
+}
+
 static char aname_pool[] =
 	"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
 
diff --git a/src/ftracepy-utils.h b/src/ftracepy-utils.h
index d09c8bf..fc5016c 100644
--- a/src/ftracepy-utils.h
+++ b/src/ftracepy-utils.h
@@ -125,6 +125,8 @@  PyObject *PyFtrace_detach(PyObject *self, PyObject *args, PyObject *kwargs);
 
 PyObject *PyFtrace_attach(PyObject *self, PyObject *args, PyObject *kwargs);
 
+PyObject *PyFtrace_is_attached(PyObject *self, PyObject *args, PyObject *kwargs);
+
 PyObject *PyFtrace_create_instance(PyObject *self, PyObject *args,
 						   PyObject *kwargs);
 
diff --git a/src/ftracepy.c b/src/ftracepy.c
index b270b71..f59bd4c 100644
--- a/src/ftracepy.c
+++ b/src/ftracepy.c
@@ -237,6 +237,11 @@  static PyMethodDef ftracepy_methods[] = {
 	 METH_VARARGS | METH_KEYWORDS,
 	 "Attach object to the \'ftracepy\' module."
 	},
+	{"is_attached",
+	 (PyCFunction) PyFtrace_is_attached,
+	 METH_VARARGS | METH_KEYWORDS,
+	 "Check if the object is attached to the \'ftracepy\' module."
+	},
 	{"create_instance",
 	 (PyCFunction) PyFtrace_create_instance,
 	 METH_VARARGS | METH_KEYWORDS,