diff mbox series

[1/3] trace-cruncher: Add methods for accessing tracefs errorlog

Message ID 20210823125348.145615-1-y.karadz@gmail.com (mailing list archive)
State Superseded
Headers show
Series [1/3] trace-cruncher: Add methods for accessing tracefs errorlog | expand

Commit Message

Yordan Karadzhov Aug. 23, 2021, 12:53 p.m. UTC
Just wrapping the corresponding APIs of libtracefs.
The functionalities are also available as static methods and will be
used by the module itself when printing the error messages.

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

Patch

diff --git a/src/ftracepy-utils.c b/src/ftracepy-utils.c
index 581432d..a3ae03a 100644
--- a/src/ftracepy-utils.c
+++ b/src/ftracepy-utils.c
@@ -23,6 +23,37 @@  PyObject *TFS_ERROR;
 PyObject *TEP_ERROR;
 PyObject *TRACECRUNCHER_ERROR;
 
+static const char *get_instance_name(struct tracefs_instance *instance);
+
+static char *tfs_error_log(struct tracefs_instance *instance, bool *ok)
+{;
+	char *err_log = tracefs_error_all(instance);
+
+	errno = 0;
+	if (errno && !err_log) {
+		PyErr_Format(TFS_ERROR,
+			     "Unable to get error log for instance \'%s\'.",
+		             get_instance_name(instance));
+	}
+
+	if (ok)
+		*ok = errno ? false : true;
+
+	return err_log;
+}
+
+static bool tfs_clear_error_log(struct tracefs_instance *instance)
+{
+	if (tracefs_error_clear(instance) < 0) {
+		PyErr_Format(TFS_ERROR,
+			     "Unable to clear error log for instance \'%s\'.",
+			     get_instance_name(instance));
+		return false;
+	}
+
+	return true;
+}
+
 PyObject *PyTepRecord_time(PyTepRecord* self)
 {
 	unsigned long ts = self->ptrObj ? self->ptrObj->ts : 0;
@@ -2051,6 +2082,42 @@  PyObject *PyFtrace_hook2pid(PyObject *self, PyObject *args, PyObject *kwargs)
 	Py_RETURN_NONE;
 }
 
+PyObject *PyFtrace_error_log(PyObject *self, PyObject *args,
+					     PyObject *kwargs)
+{
+	struct tracefs_instance *instance;
+	PyObject *ret = NULL;
+	char *err_log;
+	bool ok;
+
+	if (!get_instance_from_arg(args, kwargs, &instance))
+		return NULL;
+
+	err_log = tfs_error_log(instance, &ok);
+	if (err_log) {
+		ret = PyUnicode_FromString(err_log);
+		free(err_log);
+	} else if (ok) {
+		ret = PyUnicode_FromString("(nil)");
+	}
+
+	return ret;
+}
+
+PyObject *PyFtrace_clear_error_log(PyObject *self, PyObject *args,
+						   PyObject *kwargs)
+{
+	struct tracefs_instance *instance;
+
+	if (!get_instance_from_arg(args, kwargs, &instance))
+		return NULL;
+
+	if (!tfs_clear_error_log(instance))
+		return NULL;
+
+	Py_RETURN_NONE;
+}
+
 void PyFtrace_at_exit(void)
 {
 }
diff --git a/src/ftracepy-utils.h b/src/ftracepy-utils.h
index 4e04fb0..26a2f14 100644
--- a/src/ftracepy-utils.h
+++ b/src/ftracepy-utils.h
@@ -177,6 +177,12 @@  PyObject *PyFtrace_iterate_trace(PyObject *self, PyObject *args,
 
 PyObject *PyFtrace_hook2pid(PyObject *self, PyObject *args, PyObject *kwargs);
 
+PyObject *PyFtrace_error_log(PyObject *self, PyObject *args,
+					     PyObject *kwargs);
+
+PyObject *PyFtrace_clear_error_log(PyObject *self, PyObject *args,
+						   PyObject *kwargs);
+
 void PyFtrace_at_exit(void);
 
 #endif
diff --git a/src/ftracepy.c b/src/ftracepy.c
index a4fba01..e5a91fc 100644
--- a/src/ftracepy.c
+++ b/src/ftracepy.c
@@ -310,6 +310,16 @@  static PyMethodDef ftracepy_methods[] = {
 	 METH_VARARGS | METH_KEYWORDS,
 	 "Trace only particular process."
 	},
+	{"error_log",
+	 (PyCFunction) PyFtrace_error_log,
+	 METH_VARARGS | METH_KEYWORDS,
+	 "Get the content of the error log."
+	},
+	{"clear_error_log",
+	 (PyCFunction) PyFtrace_clear_error_log,
+	 METH_VARARGS | METH_KEYWORDS,
+	 "Clear the content of the error log."
+	},
 	{NULL, NULL, 0, NULL}
 };