Change initialize a bit
[c11tester.git] / stacktrace.h
index 01d31d2..2430818 100644 (file)
@@ -9,10 +9,13 @@
 #include <execinfo.h>
 #include <cxxabi.h>
 
 #include <execinfo.h>
 #include <cxxabi.h>
 
-/** Print a demangled stack backtrace of the caller function to FILE* out. */
-static inline void print_stacktrace(FILE *out = stderr, unsigned int max_frames = 63)
+/**
+ * @brief Print a demangled stack backtrace of the caller function to file
+ * descriptor fd.
+ */
+static inline void print_stacktrace(int fd = STDERR_FILENO, unsigned int max_frames = 63)
 {
 {
-       fprintf(out, "stack trace:\n");
+       dprintf(fd, "stack trace:\n");
 
        // storage array for stack trace address data
        void* addrlist[max_frames+1];
 
        // storage array for stack trace address data
        void* addrlist[max_frames+1];
@@ -21,7 +24,7 @@ static inline void print_stacktrace(FILE *out = stderr, unsigned int max_frames
        int addrlen = backtrace(addrlist, sizeof(addrlist) / sizeof(void*));
 
        if (addrlen == 0) {
        int addrlen = backtrace(addrlist, sizeof(addrlist) / sizeof(void*));
 
        if (addrlen == 0) {
-               fprintf(out, "  <empty, possibly corrupt>\n");
+               dprintf(fd, "  <empty, possibly corrupt>\n");
                return;
        }
 
                return;
        }
 
@@ -35,12 +38,12 @@ static inline void print_stacktrace(FILE *out = stderr, unsigned int max_frames
 
        // iterate over the returned symbol lines. skip the first, it is the
        // address of this function.
 
        // iterate over the returned symbol lines. skip the first, it is the
        // address of this function.
-       for (int i = 1; i < addrlen; i++) {
+       for (int i = 1;i < addrlen;i++) {
                char *begin_name = 0, *begin_offset = 0, *end_offset = 0;
 
                // find parentheses and +address offset surrounding the mangled name:
                // ./module(function+0x15c) [0x8048a6d]
                char *begin_name = 0, *begin_offset = 0, *end_offset = 0;
 
                // find parentheses and +address offset surrounding the mangled name:
                // ./module(function+0x15c) [0x8048a6d]
-               for (char *p = symbollist[i]; *p; ++p) {
+               for (char *p = symbollist[i];*p;++p) {
                        if (*p == '(')
                                begin_name = p;
                        else if (*p == '+')
                        if (*p == '(')
                                begin_name = p;
                        else if (*p == '+')
@@ -62,20 +65,20 @@ static inline void print_stacktrace(FILE *out = stderr, unsigned int max_frames
 
                        int status;
                        char* ret = abi::__cxa_demangle(begin_name,
 
                        int status;
                        char* ret = abi::__cxa_demangle(begin_name,
-                                       funcname, &funcnamesize, &status);
+                                                                                                                                                       funcname, &funcnamesize, &status);
                        if (status == 0) {
                        if (status == 0) {
-                               funcname = ret; // use possibly realloc()-ed string
-                               fprintf(out, "  %s : %s+%s\n",
-                                               symbollist[i], funcname, begin_offset);
+                               funcname = ret; // use possibly realloc()-ed string
+                               dprintf(fd, "  %s : %s+%s\n",
+                                                               symbollist[i], funcname, begin_offset);
                        } else {
                                // demangling failed. Output function name as a C function with
                                // no arguments.
                        } else {
                                // demangling failed. Output function name as a C function with
                                // no arguments.
-                               fprintf(out, "  %s : %s()+%s\n",
-                                               symbollist[i], begin_name, begin_offset);
+                               dprintf(fd, "  %s : %s()+%s\n",
+                                                               symbollist[i], begin_name, begin_offset);
                        }
                } else {
                        // couldn't parse the line? print the whole line.
                        }
                } else {
                        // couldn't parse the line? print the whole line.
-                       fprintf(out, "  %s\n", symbollist[i]);
+                       dprintf(fd, "  %s\n", symbollist[i]);
                }
        }
 
                }
        }
 
@@ -83,4 +86,9 @@ static inline void print_stacktrace(FILE *out = stderr, unsigned int max_frames
        free(symbollist);
 }
 
        free(symbollist);
 }
 
-#endif // __STACKTRACE_H__
+static inline void print_stacktrace(FILE *out, unsigned int max_frames = 63)
+{
+       print_stacktrace(fileno(out), max_frames);
+}
+
+#endif // __STACKTRACE_H__