about summary refs log tree commit diff stats
path: root/src/main.c
diff options
context:
space:
mode:
authorptitSeb <sebastien.chev@gmail.com>2023-02-14 20:10:32 +0100
committerptitSeb <sebastien.chev@gmail.com>2023-02-14 20:10:32 +0100
commitc74c9bd693df180f2f9fb021d3ffddaa8e34f975 (patch)
tree556db4993d1272836349ba8613240ef1b0ce7f08 /src/main.c
parent1daf15c525fbe982fdde7f35e6e9a900943fe3dc (diff)
downloadbox64-c74c9bd693df180f2f9fb021d3ffddaa8e34f975.tar.gz
box64-c74c9bd693df180f2f9fb021d3ffddaa8e34f975.zip
Refactored trace to file system, to be more resiliant
Diffstat (limited to 'src/main.c')
-rwxr-xr-xsrc/main.c30
1 files changed, 28 insertions, 2 deletions
diff --git a/src/main.c b/src/main.c
index 2ac564cc..863f40a3 100755
--- a/src/main.c
+++ b/src/main.c
@@ -11,6 +11,7 @@
 #include <sys/mman.h>
 #include <pthread.h>
 #include <sys/prctl.h>
+#include <stdarg.h>
 #ifdef DYNAREC
 #ifdef ARM64
 #include <linux/auxvec.h>
@@ -111,6 +112,7 @@ int jit_gdb = 0;
 int box64_tcmalloc_minimal = 0;
 
 FILE* ftrace = NULL;
+char* ftrace_name = NULL;
 int ftrace_has_pid = 0;
 
 void openFTrace(const char* newtrace)
@@ -145,6 +147,9 @@ void openFTrace(const char* newtrace)
         p = tmp;
         ftrace_has_pid = 1;
     }
+    if(ftrace_name)
+        free(ftrace_name);
+    ftrace_name = NULL;
     if(p) {
         if(!strcmp(p, "stderr"))
             ftrace = stderr;
@@ -157,6 +162,9 @@ 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);
+                /*fclose(ftrace);
+                ftrace = NULL;*/
                 if(!box64_nobanner)
                     printf("BOX64 Trace %s to \"%s\"\n", append?"appended":"redirected", p);
             }
@@ -164,11 +172,29 @@ void openFTrace(const char* newtrace)
     }
 }
 
+void printf_ftrace(const char* fmt, ...)
+{
+    if(ftrace_name) {
+        int fd = fileno(ftrace);
+        if(fd<0 || lseek(fd, 0, SEEK_CUR)==(off_t)-1)
+            ftrace=fopen(ftrace_name, "w+");
+    }
+
+    va_list args;
+    va_start(args, fmt);
+    vfprintf(ftrace, fmt, args);
+
+    fflush(ftrace);
+
+    va_end(args);
+}
+
 void my_child_fork()
 {
     if(ftrace_has_pid) {
         // open a new ftrace...
-        fclose(ftrace);
+        if(!ftrace_name) 
+            fclose(ftrace);
         openFTrace(NULL);
     }
 }
@@ -343,7 +369,7 @@ void LoadLogEnv()
     }
     // grab BOX64_TRACE_FILE envvar, and change %pid to actual pid is present in the name
     openFTrace(NULL);
-    box64_log = isatty(fileno(ftrace))?LOG_INFO:LOG_NONE; //default LOG value different if stdout is redirected or not
+    box64_log = ftrace_name?LOG_INFO:(isatty(fileno(ftrace))?LOG_INFO:LOG_NONE); //default LOG value different if stdout is redirected or not
     p = getenv("BOX64_LOG");
     if(p) {
         if(strlen(p)==1) {