All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Yordan Karadzhov (VMware)" <y.karadz@gmail.com>
To: linux-trace-devel@vger.kernel.org
Cc: "Yordan Karadzhov (VMware)" <y.karadz@gmail.com>
Subject: [PATCH 7/8] trace-cruncher: Check kernel support in the eprobe constructor
Date: Thu, 10 Feb 2022 17:23:38 +0200	[thread overview]
Message-ID: <20220210152339.363943-8-y.karadz@gmail.com> (raw)
In-Reply-To: <20220210152339.363943-1-y.karadz@gmail.com>

'eprobes' have been introduced in kernel version 5.15. Before trying to
create eprobe, we have to check if suport is avaiable. In the case of
using an older version of the kernel, an apropriate error message is
provided.

Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
 src/ftracepy-utils.c | 48 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 47 insertions(+), 1 deletion(-)

diff --git a/src/ftracepy-utils.c b/src/ftracepy-utils.c
index 20f0cd9..85f5bbc 100644
--- a/src/ftracepy-utils.c
+++ b/src/ftracepy-utils.c
@@ -12,6 +12,7 @@
 // C
 #include <search.h>
 #include <string.h>
+#include <sys/utsname.h>
 #include <sys/wait.h>
 #include <signal.h>
 #include <time.h>
@@ -23,6 +24,47 @@ PyObject *TFS_ERROR;
 PyObject *TEP_ERROR;
 PyObject *TRACECRUNCHER_ERROR;
 
+static char *kernel_version()
+{
+	struct utsname uts;
+
+	if (uname(&uts) != 0) {
+		PyErr_SetString(TFS_ERROR, "Failed to get kernel version.");
+		return NULL;
+	}
+
+	return strdup(uts.release);
+}
+
+static bool check_kernel_support(const char *api, int major, int minor)
+{
+	char *buff, *this_kernel = kernel_version();
+	const char *dlm = ".";
+	bool ret = false;
+	int mj, mn;
+
+        buff = strtok(this_kernel, dlm);
+	mj = atoi(buff);
+	if (mj > major)
+		ret = true;
+
+	if (mj == major) {
+		buff = strtok(NULL, dlm);
+		mn = atoi(buff);
+		if (mn >= minor)
+			ret = true;
+	}
+
+	free(this_kernel);
+	if (!ret) {
+		PyErr_Format(TFS_ERROR,
+			     "Using \'%s()\' requires kernel versions >= %i.%i",
+			     api, major, minor);
+	}
+
+	return ret;
+}
+
 static const char *get_instance_name(struct tracefs_instance *instance);
 
 static char *tfs_error_log(struct tracefs_instance *instance, bool *ok)
@@ -2382,6 +2424,10 @@ PyObject *PyFtrace_eprobe(PyObject *self, PyObject *args, PyObject *kwargs)
 		return NULL;
 	}
 
+	/* 'eprobes' are introduced in kernel version 5.15. */
+	if (!check_kernel_support("eprobe", 5, 15))
+		return NULL;
+
 	eprobe = tracefs_eprobe_alloc(TC_SYS, event, target_system, target_event, fetchargs);
 	if (!eprobe) {
 		MEM_ERROR;
@@ -2395,7 +2441,7 @@ PyObject *PyFtrace_eprobe(PyObject *self, PyObject *args, PyObject *kwargs)
 	 * there is no need to 'destroy' this event at exit.
 	 */
 	set_destroy_flag(py_dyn, false);
-	return py_dyn;;
+	return py_dyn;
 }
 
 static PyObject *set_filter(PyObject *args, PyObject *kwargs,
-- 
2.32.0


  parent reply	other threads:[~2022-02-10 15:24 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-10 15:23 [PATCH 0/8] trace-cruncher:Fixes before v0.2 (Beta) Yordan Karadzhov (VMware)
2022-02-10 15:23 ` [PATCH 1/8] trace-cruncher: Prefix all python class names with 'tc_' Yordan Karadzhov (VMware)
2022-02-10 15:23 ` [PATCH 2/8] trace-cruncher: Fix bug in the constructor if tc_synth class Yordan Karadzhov (VMware)
2022-02-10 15:23 ` [PATCH 3/8] trace-cruncher: Add tests for synth helper APIs Yordan Karadzhov (VMware)
2022-02-10 15:23 ` [PATCH 4/8] trace-cruncher: Code cleanup in the constructor of tc_synth Yordan Karadzhov (VMware)
2022-02-10 15:23 ` [PATCH 5/8] trace-cruncher: Rename python function argument in PyFtrace_eprobe Yordan Karadzhov (VMware)
2022-02-10 15:23 ` [PATCH 6/8] trace-cruncher: Add tc_eprobe class to ft_utiles Yordan Karadzhov (VMware)
2022-02-10 15:23 ` Yordan Karadzhov (VMware) [this message]
2022-02-10 15:23 ` [PATCH 8/8] trace-cruncher: Add more comments to the examples Yordan Karadzhov (VMware)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220210152339.363943-8-y.karadz@gmail.com \
    --to=y.karadz@gmail.com \
    --cc=linux-trace-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.