add a segfault handler that writes out the info log into a crash file alongside the...
authorCampbell Barton <ideasman42@gmail.com>
Thu, 10 Jan 2013 16:37:48 +0000 (16:37 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Thu, 10 Jan 2013 16:37:48 +0000 (16:37 +0000)
source/blender/blenkernel/BKE_report.h
source/blender/blenkernel/intern/report.c
source/blender/blenloader/intern/runtime.c
source/blender/editors/space_file/file_ops.c
source/blender/render/intern/source/pipeline.c
source/blender/render/intern/source/render_result.c
source/creator/creator.c

index 4d69a01..84cc4b6 100644 (file)
@@ -72,7 +72,10 @@ void BKE_reports_print(ReportList *reports, ReportType level);
 Report *BKE_reports_last_displayable(ReportList *reports);
 
 int BKE_reports_contain(ReportList *reports, ReportType level);
-       
+
+// int BKE_report_write_file_fp(struct FILE *fp, ReportList *reports, const char *header);
+bool BKE_report_write_file(const char *filepath, ReportList *reports, const char *header);
+
 #ifdef __cplusplus
 }
 #endif
index 185aeac..3acb352 100644 (file)
  *  \ingroup bke
  */
 
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
 
 #include "MEM_guardedalloc.h"
 
 #include "BKE_report.h"
 #include "BKE_global.h" /* G.background only */
 
-#include <stdarg.h>
-#include <stdio.h>
-#include <string.h>
-
 static const char *report_type_str(int type)
 {
        switch (type) {
@@ -302,3 +302,38 @@ int BKE_reports_contain(ReportList *reports, ReportType level)
        return FALSE;
 }
 
+static bool BKE_report_write_file_fp(FILE *fp, ReportList *reports, const char *header)
+{
+       Report *report;
+
+       if (header) {
+               fputs(header, fp);
+       }
+
+       for (report = reports->list.first; report; report = report->next) {
+               fprintf((FILE *)fp, "%s  # %s\n", report->message, report->typestr);
+       }
+
+       return true;
+}
+
+bool BKE_report_write_file(const char *filepath, ReportList *reports, const char *header)
+{
+       FILE *fp;
+
+       /* first try create the file, if it exists call without 'O_CREAT',
+        * to avoid writing to a symlink - use 'O_EXCL' (CVE-2008-1103) */
+       errno = 0;
+       fp = BLI_fopen(filepath, "wb");
+       if (fp == NULL) {
+               fprintf(stderr, "Unable to save '%s': %s\n",
+                       filepath, errno ? strerror(errno) : "Unknown error opening file");
+               return false;
+       }
+
+       BKE_report_write_file_fp(fp, reports, header);
+
+       fclose(fp);
+
+       return true;
+}
index 5d8a865..cbbaf71 100644 (file)
 #include "BLO_readfile.h"
 #include "BLO_runtime.h"
 
+#include "BLI_blenlib.h"
+#include "BLI_utildefines.h"
+
 #include "BKE_blender.h"
 #include "BKE_report.h"
 
-#include "BLI_blenlib.h"
-
 /* Runtime reading */
 
 static int handle_read_msb_int(int handle)
index 763b187..9349abb 100644 (file)
@@ -28,6 +28,8 @@
  *  \ingroup spfile
  */
 
+#include "BLI_blenlib.h"
+#include "BLI_utildefines.h"
 
 #include "BKE_context.h"
 #include "BKE_screen.h"
@@ -35,9 +37,6 @@
 #include "BKE_report.h"
 #include "BKE_main.h"
 
-#include "BLI_blenlib.h"
-#include "BLI_utildefines.h"
-
 #ifdef WIN32
 #  include "BLI_winstuff.h"
 #endif
index 755c8f4..dec11f0 100644 (file)
 
 #include "MEM_guardedalloc.h"
 
+#include "BLI_math.h"
+#include "BLI_rect.h"
+#include "BLI_listbase.h"
+#include "BLI_string.h"
+#include "BLI_path_util.h"
+#include "BLI_fileops.h"
+#include "BLI_threads.h"
+#include "BLI_rand.h"
+#include "BLI_callbacks.h"
+
 #include "BKE_animsys.h"  /* <------ should this be here?, needed for sequencer update */
 #include "BKE_camera.h"
 #include "BKE_global.h"
 #include "BKE_sequencer.h"
 #include "BKE_writeavi.h"  /* <------ should be replaced once with generic movie module */
 
-#include "BLI_math.h"
-#include "BLI_rect.h"
-#include "BLI_listbase.h"
-#include "BLI_string.h"
-#include "BLI_path_util.h"
-#include "BLI_fileops.h"
-#include "BLI_threads.h"
-#include "BLI_rand.h"
-#include "BLI_callbacks.h"
-
 #include "PIL_time.h"
 #include "IMB_colormanagement.h"
 #include "IMB_imbuf.h"
index 6de5830..35b0638 100644 (file)
 
 #include "MEM_guardedalloc.h"
 
-#include "BKE_image.h"
-#include "BKE_global.h"
-#include "BKE_main.h"
-#include "BKE_report.h"
-
 #include "BLI_fileops.h"
 #include "BLI_listbase.h"
 #include "BLI_path_util.h"
 #include "BLI_threads.h"
 #include "BLI_utildefines.h"
 
+#include "BKE_image.h"
+#include "BKE_global.h"
+#include "BKE_main.h"
+#include "BKE_report.h"
+
 #include "IMB_imbuf.h"
 #include "IMB_imbuf_types.h"
 #include "IMB_colormanagement.h"
index 158ce8f..7dd4384 100644 (file)
 #  include <xmmintrin.h>
 #endif
 
+#ifdef WIN32
+#  include <process.h> /* getpid */
+#else
+#  include <unistd.h> /* getpid */
+#endif
+
 #ifdef WIN32
 #  include <Windows.h>
 #  include "utfconv.h"
@@ -157,6 +163,8 @@ static int print_version(int argc, const char **argv, void *data);
 /* Initialize callbacks for the modules that need them */
 static void setCallbacks(void); 
 
+static bool use_crash_handler = true;
+
 #ifndef WITH_PYTHON_MODULE
 
 /* set breakpoints here when running in debug mode, useful to catch floating point errors */
@@ -246,6 +254,7 @@ static int print_help(int UNUSED(argc), const char **UNUSED(argv), void *data)
        printf("Misc Options:\n");
        BLI_argsPrintArgDoc(ba, "--debug");
        BLI_argsPrintArgDoc(ba, "--debug-fpe");
+       BLI_argsPrintArgDoc(ba, "--disable-crash-handler");
 
 #ifdef WITH_FFMPEG
        BLI_argsPrintArgDoc(ba, "--debug-ffmpeg");
@@ -350,6 +359,12 @@ static int disable_python(int UNUSED(argc), const char **UNUSED(argv), void *UNU
        return 0;
 }
 
+static int disable_crash_handler(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
+{
+       use_crash_handler = false;
+       return 0;
+}
+
 static int background_mode(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
 {
        G.background = 1;
@@ -423,6 +438,56 @@ static int set_fpe(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(dat
        return 0;
 }
 
+static void blender_crash_handler(int signum)
+{
+
+#if 0
+       {
+               char fname[FILE_MAX];
+
+               if (!G.main->name[0]) {
+                       BLI_make_file_string("/", fname, BLI_temporary_dir(), "crash.blend");
+               }
+               else {
+                       BLI_strncpy(fname, G.main->name, sizeof(fname));
+                       BLI_replace_extension(fname, sizeof(fname), ".crash.blend");
+               }
+
+               printf("Writing: %s\n", fname);
+               fflush(stdout);
+
+               BKE_undo_save_file(fname);
+       }
+#endif
+
+       {
+               char header[512];
+               wmWindowManager *wm = G.main->wm.first;
+
+               char fname[FILE_MAX];
+
+               if (!G.main->name[0]) {
+                       BLI_make_file_string("/", fname, BLI_temporary_dir(), "blender.crash.txt");
+               }
+               else {
+                       BLI_strncpy(fname, G.main->name, sizeof(fname));
+                       BLI_replace_extension(fname, sizeof(fname), ".crash.txt");
+               }
+
+               printf("Writing: %s\n", fname);
+               fflush(stdout);
+
+               BLI_snprintf(header, sizeof(header), "# " BLEND_VERSION_STRING_FMT);
+
+               BKE_report_write_file(fname, &wm->reports, header);
+       }
+
+       /* really crash */
+       signal(signum, SIG_DFL);
+       kill(getpid(), signum);
+}
+
+
 static int set_factory_startup(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
 {
        G.factory_startup = 1;
@@ -1122,6 +1187,8 @@ static void setupArguments(bContext *C, bArgs *ba, SYS_SystemHandle *syshandle)
        BLI_argsAdd(ba, 1, "-y", "--enable-autoexec", "\n\tEnable automatic python script execution" PY_ENABLE_AUTO, enable_python, NULL);
        BLI_argsAdd(ba, 1, "-Y", "--disable-autoexec", "\n\tDisable automatic python script execution (pydrivers & startup scripts)" PY_DISABLE_AUTO, disable_python, NULL);
 
+       BLI_argsAdd(ba, 1, NULL, "--disable-crash-handler", "\n\tDisable the crash handler", disable_crash_handler, NULL);
+
 #undef PY_ENABLE_AUTO
 #undef PY_DISABLE_AUTO
        
@@ -1301,6 +1368,10 @@ int main(int argc, const char **argv)
        BLI_argsParse(ba, 1, NULL, NULL);
 #endif
 
+       if (use_crash_handler) {
+               /* after parsing args */
+               signal(SIGSEGV, blender_crash_handler);
+       }
 
        /* after level 1 args, this is so playanim skips RNA init */
        RNA_init();