about summary refs log tree commit diff stats
path: root/src
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
parent1daf15c525fbe982fdde7f35e6e9a900943fe3dc (diff)
downloadbox64-c74c9bd693df180f2f9fb021d3ffddaa8e34f975.tar.gz
box64-c74c9bd693df180f2f9fb021d3ffddaa8e34f975.zip
Refactored trace to file system, to be more resiliant
Diffstat (limited to 'src')
-rwxr-xr-xsrc/include/debug.h10
-rwxr-xr-xsrc/main.c30
-rw-r--r--src/tools/rcfile.c4
3 files changed, 36 insertions, 8 deletions
diff --git a/src/include/debug.h b/src/include/debug.h
index 3f3a041b..ed729868 100755
--- a/src/include/debug.h
+++ b/src/include/debug.h
@@ -72,15 +72,15 @@ extern int box64_tcmalloc_minimal;  // when using tcmalloc_minimal
 #define LOG_NEVER 3
 #define LOG_VERBOSE 3
 
-extern FILE* ftrace;
+void printf_ftrace(const char* fmt, ...);
 
-#define printf_log(L, ...) do {if((L)<=box64_log) {fprintf(ftrace, __VA_ARGS__); fflush(ftrace);}} while(0)
+#define printf_log(L, ...) do {if((L)<=box64_log) {printf_ftrace(__VA_ARGS__);}} while(0)
 
-#define printf_dump(L, ...) do {if(box64_dump || ((L)<=box64_log)) {fprintf(ftrace, __VA_ARGS__); fflush(ftrace);}} while(0)
+#define printf_dump(L, ...) do {if(box64_dump || ((L)<=box64_log)) {printf_ftrace(__VA_ARGS__);}} while(0)
 
-#define printf_dlsym(L, ...) do {if(dlsym_error || ((L)<=box64_log)) {fprintf(ftrace, __VA_ARGS__); fflush(ftrace);}} while(0)
+#define printf_dlsym(L, ...) do {if(dlsym_error || ((L)<=box64_log)) {printf_ftrace(__VA_ARGS__);}} while(0)
 
-#define dynarec_log(L, ...) do {if((L)<=box64_dynarec_log) {fprintf(ftrace, __VA_ARGS__); fflush(ftrace);}} while(0)
+#define dynarec_log(L, ...) do {if((L)<=box64_dynarec_log) {printf_ftrace(__VA_ARGS__);}} while(0)
 
 #define EXPORT __attribute__((visibility("default")))
 #ifdef BUILD_DYNAMIC
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) {
diff --git a/src/tools/rcfile.c b/src/tools/rcfile.c
index ec083b5c..ebae7a68 100644
--- a/src/tools/rcfile.c
+++ b/src/tools/rcfile.c
@@ -392,6 +392,8 @@ void DeleteParams()
 }
 
 extern int ftrace_has_pid;
+extern FILE* ftrace;
+extern char* ftrace_name;
 void openFTrace(const char* newtrace);
 #ifdef DYNAREC
 void GatherDynarecExtensions();
@@ -457,7 +459,7 @@ void ApplyParams(const char* name)
     if(param->is_box64_path_present) AppendList(&my_context->box64_path, param->box64_path, 1);
     if(param->is_trace_file_present) {
         // open a new ftrace...
-        if(ftrace_has_pid) {
+        if(ftrace_name) {
             fclose(ftrace);
         }
         openFTrace(param->trace_file);