about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorYang Liu <liuyang22@iscas.ac.cn>2024-10-13 19:08:18 +0800
committerGitHub <noreply@github.com>2024-10-13 13:08:18 +0200
commit7430463b336c8dc957ca53b27b43f6a997282455 (patch)
tree77837807bf2492da7b34ca0ff1ae09b29e84b9be /src
parent6f2d067f3abb70aa6d8d386a1a2395e4bff39c9b (diff)
downloadbox64-7430463b336c8dc957ca53b27b43f6a997282455.tar.gz
box64-7430463b336c8dc957ca53b27b43f6a997282455.zip
[CORE] Refactored %pid trace file handling (#1933)
Diffstat (limited to 'src')
-rw-r--r--src/core.c82
-rw-r--r--src/tools/rcfile.c4
2 files changed, 52 insertions, 34 deletions
diff --git a/src/core.c b/src/core.c
index 8a72e009..ef6c473a 100644
--- a/src/core.c
+++ b/src/core.c
@@ -202,7 +202,7 @@ FILE* ftrace = NULL;
 char* ftrace_name = NULL;
 int ftrace_has_pid = 0;
 
-void openFTrace(const char* newtrace)
+void openFTrace(const char* newtrace, int reopen)
 {
     const char* t = newtrace?newtrace:getenv("BOX64_TRACE_FILE");
     #ifndef MAX_PATH
@@ -216,30 +216,35 @@ void openFTrace(const char* newtrace)
         strncpy(tmp2, p, sizeof(tmp2));
         tmp2[strlen(p)-1]='\0';
         p = tmp2;
-        append=1;
+        append = 1;
+    }
+    if (reopen) {
+        p = ftrace_name;
+        append = 1;
+    } else {
+        if (p && strstr(p, "%pid")) {
+            int next = 0;
+            do {
+                strcpy(tmp, p);
+                char* c = strstr(tmp, "%pid");
+                *c = 0; // cut
+                char pid[16];
+                if (next)
+                    sprintf(pid, "%d-%d", getpid(), next);
+                else
+                    sprintf(pid, "%d", getpid());
+                strcat(tmp, pid);
+                c = strstr(p, "%pid") + strlen("%pid");
+                strcat(tmp, c);
+                ++next;
+            } while (FileExist(tmp, IS_FILE) && !append);
+            p = tmp;
+            ftrace_has_pid = 1;
+        }
+        if (ftrace_name)
+            free(ftrace_name);
+        ftrace_name = NULL;
     }
-    if(p && strstr(p, "%pid")) {
-        int next = 0;
-        do {
-            strcpy(tmp, p);
-            char* c = strstr(tmp, "%pid");
-            *c = 0; // cut
-            char pid[16];
-            if(next)
-                sprintf(pid, "%d-%d", getpid(), next);
-            else
-                sprintf(pid, "%d", getpid());
-            strcat(tmp, pid);
-            c = strstr(p, "%pid") + strlen("%pid");
-            strcat(tmp, c);
-            ++next;
-        } while (FileExist(tmp, IS_FILE) && !append);
-        p = tmp;
-        ftrace_has_pid = 1;
-    }
-    if(ftrace_name)
-        free(ftrace_name);
-    ftrace_name = NULL;
     if(p) {
         if(!strcmp(p, "stderr"))
             ftrace = stderr;
@@ -252,7 +257,7 @@ void openFTrace(const char* newtrace)
                 ftrace = stdout;
                 printf_log(LOG_INFO, "Cannot open trace file \"%s\" for writing (error=%s)\n", p, strerror(errno));
             } else {
-                ftrace_name = strdup(p);
+                if (!reopen) ftrace_name = strdup(p);
                 /*fclose(ftrace);
                 ftrace = NULL;*/
                 if(!box64_nobanner) {
@@ -282,14 +287,27 @@ void printf_ftrace(const char* fmt, ...)
     va_end(args);
 }
 
+void my_prepare_fork()
+{
+    if (ftrace_has_pid && ftrace && ftrace != stdout && ftrace != stderr) {
+        fclose(ftrace);
+        printf_log(LOG_INFO, "Closed trace file of %s at prepare\n", GetLastApplyName());
+    }
+}
+
+void my_parent_fork()
+{
+    if (ftrace_has_pid) {
+        openFTrace(NULL, 1);
+        printf_log(LOG_INFO, "Reopened trace file of %s at parent\n", GetLastApplyName());
+    }
+}
+
 void my_child_fork()
 {
-    if(ftrace_has_pid) {
-        // open a new ftrace...
-        if(!ftrace_name)
-            fclose(ftrace);
-        openFTrace(NULL);
-        printf_log(/*LOG_DEBUG*/LOG_INFO, "Forked child of %s\n", GetLastApplyName());
+    if (ftrace_has_pid) {
+        openFTrace(NULL, 0);
+        printf_log(LOG_INFO, "Created trace file of %s at child\n", GetLastApplyName());
     }
 }
 
@@ -622,7 +640,7 @@ void LoadLogEnv()
         }
     }
     // grab BOX64_TRACE_FILE envvar, and change %pid to actual pid is present in the name
-    openFTrace(NULL);
+    openFTrace(NULL, 0);
     box64_log = ftrace_name?LOG_INFO:(isatty(fileno(stdout))?LOG_INFO:LOG_NONE); //default LOG value different if stdout is redirected or not
     p = getenv("BOX64_LOG");
     if(p) {
diff --git a/src/tools/rcfile.c b/src/tools/rcfile.c
index 0d19901b..abebd523 100644
--- a/src/tools/rcfile.c
+++ b/src/tools/rcfile.c
@@ -503,7 +503,7 @@ extern FILE* ftrace;
 extern char* ftrace_name;
 extern char* box64_new_args;
 extern char* box64_insert_args;
-void openFTrace(const char* newtrace);
+void openFTrace(const char* newtrace, int reopen);
 void addNewEnvVar(const char* s);
 void AddNewLibs(const char* libs);
 void computeRDTSC();
@@ -622,7 +622,7 @@ void internal_ApplyParams(const char* name, const my_params_t* param) {
         if(ftrace_name) {
             fclose(ftrace);
         }
-        openFTrace(param->trace_file);
+        openFTrace(param->trace_file, 0);
     }
     if(param->is_emulated_libs_present) {
         AppendList(&my_context->box64_emulated_libs, param->emulated_libs, 0);