]> git.cworth.org Git - apitrace/blobdiff - common/os_posix.cpp
common: dump backtrace on signals
[apitrace] / common / os_posix.cpp
index bd15bbac43db16710075fff3afaa59bf11e3e15c..5d6bffe5dcdab406a23624692b965e1d0730305b 100644 (file)
@@ -23,6 +23,7 @@
  *
  **************************************************************************/
 
+#ifndef _WIN32
 
 #include <assert.h>
 #include <string.h>
@@ -55,6 +56,7 @@
 
 #include "os.hpp"
 #include "os_string.hpp"
+#include "os_backtrace.hpp"
 
 
 namespace os {
@@ -71,25 +73,33 @@ getProcessName(void)
 #ifdef __APPLE__
     uint32_t len = size;
     if (_NSGetExecutablePath(buf, &len) != 0) {
-        *buf = 0;
-        return path;
+        // grow buf and retry
+        buf = path.buf(len);
+        _NSGetExecutablePath(buf, &len);
     }
     len = strlen(buf);
 #else
     ssize_t len;
     len = readlink("/proc/self/exe", buf, size - 1);
-    if (len == -1) {
+    if (len <= 0) {
         // /proc/self/exe is not available on setuid processes, so fallback to
         // /proc/self/cmdline.
         int fd = open("/proc/self/cmdline", O_RDONLY);
         if (fd >= 0) {
-            len = read(fd, buf, size - 1);
+            // buf already includes trailing zero
+            len = read(fd, buf, size);
             close(fd);
+            if (len >= 0) {
+                len = strlen(buf);
+            }
         }
     }
     if (len <= 0) {
-        snprintf(buf, size, "%i", (int)getpid());
-        return path;
+        // fallback to process ID
+        len = snprintf(buf, size, "%i", (int)getpid());
+        if (len >= size) {
+            len = size - 1;
+        }
     }
 #endif
     path.truncate(len);
@@ -111,6 +121,12 @@ getCurrentDir(void)
     return path;
 }
 
+bool
+createDirectory(const String &path)
+{
+    return mkdir(path, 0777) == 0;
+}
+
 bool
 String::exists(void) const
 {
@@ -122,9 +138,6 @@ String::exists(void) const
         return false;
     }
 
-    if (!S_ISREG(st.st_mode))
-        return false;
-
     return true;
 }
 
@@ -173,8 +186,16 @@ log(const char *format, ...)
 #ifdef ANDROID
     __android_log_vprint(ANDROID_LOG_DEBUG, "apitrace", format, ap);
 #else
-    vfprintf(stderr, format, ap);
-    fflush(stderr);
+    static FILE *log = NULL;
+    if (!log) {
+        // Duplicate stderr file descriptor, to prevent applications from
+        // redirecting our debug messages to somewhere else.
+        //
+        // Another alternative would be to log to /dev/tty when available.
+        log = fdopen(dup(STDERR_FILENO), "at");
+    }
+    vfprintf(log, format, ap);
+    fflush(log);
 #endif
     va_end(ap);
     logging = false;
@@ -223,11 +244,11 @@ signalHandler(int sig, siginfo_t *info, void *context)
     if (recursion_count) {
         log("apitrace: warning: recursion handling signal %i\n", sig);
     } else {
-        if (gCallback) {
-            ++recursion_count;
+        ++recursion_count;
+        if (gCallback)
             gCallback();
-            --recursion_count;
-        }
+        os::dump_backtrace();
+        --recursion_count;
     }
 
     struct sigaction *old_action;
@@ -312,3 +333,4 @@ resetExceptionCallback(void)
 
 } /* namespace os */
 
+#endif // !defined(_WIN32)