stacktrace: remove usage of FILE in stacktrace
authorBrian Norris <banorris@uci.edu>
Thu, 18 Apr 2013 00:10:20 +0000 (17:10 -0700)
committerBrian Norris <banorris@uci.edu>
Thu, 18 Apr 2013 00:10:20 +0000 (17:10 -0700)
The original print_stacktrace() function required a FILE pointer, so we
manufactured one. We really should avoid the bugs that can come from the
allocation and buffering done with FILE, since this is used on error
paths.

common.cc
stacktrace.h

index 9cb7239d0a32d6bdcda8e41b1af252c190469d9e..66b563a8ba6223a32a2c7d08067406fec0914c4b 100644 (file)
--- a/common.cc
+++ b/common.cc
@@ -22,9 +22,7 @@ int model_out = STDOUT_FILENO;
 void print_trace(void)
 {
 #ifdef CONFIG_STACKTRACE
 void print_trace(void)
 {
 #ifdef CONFIG_STACKTRACE
-       FILE *file = fdopen(model_out, "w");
-       print_stacktrace(file);
-       fclose(file);
+       print_stacktrace(model_out);
 #else
        void *array[MAX_TRACE_LEN];
        char **strings;
 #else
        void *array[MAX_TRACE_LEN];
        char **strings;
index 01d31d2f33c2e72fcc54ce8d120baed359fead08..a3b035096e796f13809eb4eedcfeada7bc84c9ba 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;
        }
 
@@ -65,17 +68,17 @@ static inline void print_stacktrace(FILE *out = stderr, unsigned int max_frames
                                        funcname, &funcnamesize, &status);
                        if (status == 0) {
                                funcname = ret; // use possibly realloc()-ed string
                                        funcname, &funcnamesize, &status);
                        if (status == 0) {
                                funcname = ret; // use possibly realloc()-ed string
-                               fprintf(out, "  %s : %s+%s\n",
+                               dprintf(fd, "  %s : %s+%s\n",
                                                symbollist[i], funcname, begin_offset);
                        } else {
                                // demangling failed. Output function name as a C function with
                                // no arguments.
                                                symbollist[i], funcname, begin_offset);
                        } else {
                                // demangling failed. Output function name as a C function with
                                // no arguments.
-                               fprintf(out, "  %s : %s()+%s\n",
+                               dprintf(fd, "  %s : %s()+%s\n",
                                                symbollist[i], begin_name, begin_offset);
                        }
                } else {
                        // couldn't parse the line? print the whole line.
                                                symbollist[i], begin_name, begin_offset);
                        }
                } 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);
 }
 
+static inline void print_stacktrace(FILE *out, unsigned int max_frames = 63)
+{
+       print_stacktrace(fileno(out), max_frames);
+}
+
 #endif // __STACKTRACE_H__
 #endif // __STACKTRACE_H__