If the instance is created with "detached=True", the Python module is no longer responsible for destroying it when exiting. It is therefore up to the user free it explicitly, or to keep it active. Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@xxxxxxxxx> --- src/ftracepy-utils.c | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/ftracepy-utils.c b/src/ftracepy-utils.c index 8f4b50c..d061817 100644 --- a/src/ftracepy-utils.c +++ b/src/ftracepy-utils.c @@ -421,6 +421,12 @@ static PyObject *tfs_list2py_list(char **list) struct instance_wrapper { struct tracefs_instance *ptr; + bool detached; + + /* + * This name will be used only for searching. The actual name of + * the instance is owned by the "tracefs_instance" object. + */ const char *name; }; @@ -451,10 +457,12 @@ void instance_wrapper_free(void *ptr) iw = ptr; if (iw->ptr) { - if (tracefs_instance_destroy(iw->ptr) < 0) - fprintf(stderr, - "\ntfs_error: Failed to destroy instance '%s'.\n", - get_instance_name(iw->ptr)); + if (!iw->detached) { + if (tracefs_instance_destroy(iw->ptr) < 0) + fprintf(stderr, + "\ntfs_error: Failed to destroy instance '%s'.\n", + get_instance_name(iw->ptr)); + } free(iw->ptr); } @@ -569,14 +577,16 @@ PyObject *PyFtrace_create_instance(PyObject *self, PyObject *args, struct tracefs_instance *instance; const char *name = NO_ARG; int tracing_on = true; + int detached = false; + static char *kwlist[] = {"name", "tracing_on", "detached", NULL}; - static char *kwlist[] = {"name", "tracing_on", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwargs, - "|sp", + "|spp", kwlist, &name, - &tracing_on)) { + &tracing_on, + &detached)) { return NULL; } @@ -600,6 +610,11 @@ PyObject *PyFtrace_create_instance(PyObject *self, PyObject *args, } iw->ptr = instance; + if (detached) { + printf("detached instance: %s\n", name); + iw->detached = detached; + } + iw_ptr = tsearch(iw, &instance_root, instance_compare); if (!iw_ptr || !(*iw_ptr) || !(*iw_ptr)->ptr || strcmp(tracefs_instance_get_name((*iw_ptr)->ptr), name) != 0) { -- 2.27.0