All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] trace-cruncher: Fix get_comm_from_pid()
@ 2021-09-17 15:21 Yordan Karadzhov (VMware)
  2021-09-17 15:21 ` [PATCH 2/2] trace-cruncher: More options for stopping data iteration Yordan Karadzhov (VMware)
  0 siblings, 1 reply; 2+ messages in thread
From: Yordan Karadzhov (VMware) @ 2021-09-17 15:21 UTC (permalink / raw)
  To: linux-trace-devel; +Cc: Yordan Karadzhov (VMware)

Make sure that the buffer string is properly initialized and that
the new line at the end is removed.

Fixes: defe053 (trace-cruncher: Add generic methods for printing)
SignedFixes: -off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
 src/ftracepy-utils.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/src/ftracepy-utils.c b/src/ftracepy-utils.c
index 58f543a..7b1ff1b 100644
--- a/src/ftracepy-utils.c
+++ b/src/ftracepy-utils.c
@@ -384,10 +384,16 @@ static bool init_print_seq(void)
 	return true;
 }
 
+static inline void trim_new_line(char *val)
+{
+	if (val[strlen(val) - 1] == '\n')
+		val[strlen(val) - 1] = '\0';
+}
+
 static char *get_comm_from_pid(int pid)
 {
 	char *comm_file, *comm = NULL;
-	char buff[PATH_MAX];
+	char buff[PATH_MAX] = {0};
 	int fd, r;
 
 	if (asprintf(&comm_file, "/proc/%i/comm", pid) <= 0) {
@@ -409,6 +415,7 @@ static char *get_comm_from_pid(int pid)
 	if (r <= 0)
 		return NULL;
 
+	trim_new_line(buff);
 	comm = strdup(buff);
 	if (!comm)
 		MEM_ERROR;
@@ -654,11 +661,6 @@ static int read_from_file(struct tracefs_instance *instance,
 	return size;
 }
 
-static inline void trim_new_line(char *val)
-{
-	val[strlen(val) - 1] = '\0';
-}
-
 static bool write_to_file_and_check(struct tracefs_instance *instance,
 				    const char *file,
 				    const char *val)
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [PATCH 2/2] trace-cruncher: More options for stopping data iteration
  2021-09-17 15:21 [PATCH 1/2] trace-cruncher: Fix get_comm_from_pid() Yordan Karadzhov (VMware)
@ 2021-09-17 15:21 ` Yordan Karadzhov (VMware)
  0 siblings, 0 replies; 2+ messages in thread
From: Yordan Karadzhov (VMware) @ 2021-09-17 15:21 UTC (permalink / raw)
  To: linux-trace-devel; +Cc: Yordan Karadzhov (VMware)

So far the only option to stop the iteration over the tracing data
from the callback function was to call 'sys.exit()'. However, this
only stops the iteration without actually exiting. With this change
calling 'sys.exit()' will terminate (exit the module), while in
order to just stop the iteration, the callback should return nonzero
integer value.

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

diff --git a/src/ftracepy-utils.c b/src/ftracepy-utils.c
index 7b1ff1b..814e1d6 100644
--- a/src/ftracepy-utils.c
+++ b/src/ftracepy-utils.c
@@ -2101,21 +2101,36 @@ static int callback(struct tep_event *event, struct tep_record *record,
 	ret = PyObject_CallObject((PyObject *)ctx->py_callback, arglist);
 	Py_DECREF(arglist);
 
-	if (ret) {
-		Py_DECREF(ret);
-	} else {
+	if (!ret) {
 		if (PyErr_Occurred()) {
-			if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
-				PyErr_Clear();
+			PyObject *err_type, *err_value, *err_traceback;
+
+			PyErr_Fetch(&err_type, &err_value, &err_traceback);
+			if (err_type == PyExc_SystemExit) {
+				if (PyLong_CheckExact(err_value))
+					Py_Exit(PyLong_AsLong(err_value));
+				else
+					Py_Exit(0);
 			} else {
+				PyErr_Restore(err_type, err_value, err_traceback);
 				PyErr_Print();
 			}
 		}
 
-		ctx->status = false;
+		goto stop;
+	}
+
+	if (PyLong_CheckExact(ret) && PyLong_AsLong(ret) != 0) {
+		Py_DECREF(ret);
+		goto stop;
 	}
 
+	Py_DECREF(ret);
 	return 0;
+
+ stop:
+	ctx->status = false;
+	return 1;
 }
 
 static bool notrace_this_pid(struct tracefs_instance *instance)
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-09-17 15:23 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-17 15:21 [PATCH 1/2] trace-cruncher: Fix get_comm_from_pid() Yordan Karadzhov (VMware)
2021-09-17 15:21 ` [PATCH 2/2] trace-cruncher: More options for stopping data iteration Yordan Karadzhov (VMware)

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.