Skip to contentSkip to conversation

trace-cruncher: More options for stopping data iteration

PATCH New Yordan Karadzhov (VMware) 1 participant 2 messages Nov 15, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 trace-cruncher: More options for stopping data iteration

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.

---
 src/ftracepy-utils.c | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)
src/ftracepy-utils.c +21 -6
index d25d873..8e803ee 100644
@@ -2046,21 +2046,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)
Signature · 1 line
2.32.0
#1 lore raw

PATCH 2/2 trace-cruncher: Make the tracing of a process stoppable

Both 'trace_process()' and 'trace_shell_process()' can be stopped from the callback function by calling 'sys.exit()' or by returning non zero integer value.

---
 src/ftracepy-utils.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
src/ftracepy-utils.c +9 -2
index 8e803ee..b5cdcee 100644
@@ -2097,10 +2097,17 @@ static void iterate_raw_events_waitpid(struct tracefs_instance *instance,
 				       PyObject *py_func,
 				       pid_t pid)
 {
+	bool *callback_status = &callback_ctx.status;
+	int ret;
+
 	callback_ctx.py_callback = py_func;
+	(*(volatile bool *)callback_status) = true;
 	do {
-		tracefs_iterate_raw_events(tep, instance, NULL, 0,
-					   callback, &callback_ctx);
+		ret = tracefs_iterate_raw_events(tep, instance, NULL, 0,
+						 callback, &callback_ctx);
+
+		if (*(volatile bool *)callback_status == false || ret < 0)
+			break;
 	} while (waitpid(pid, NULL, WNOHANG) != pid);
 }
 
Signature · 1 line
2.32.0