2022-02-10 23:07:11 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2016-02-27 19:10:00 +01:00
|
|
|
|
2019-02-17 22:08:12 +01:00
|
|
|
/** \file
|
|
|
|
* \ingroup creator
|
2016-02-27 19:10:00 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef WITH_PYTHON_MODULE
|
|
|
|
|
|
|
|
# if defined(__linux__) && defined(__GNUC__)
|
|
|
|
# define _GNU_SOURCE
|
|
|
|
# include <fenv.h>
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# if (defined(__APPLE__) && (defined(__i386__) || defined(__x86_64__)))
|
|
|
|
# define OSX_SSE_FPE
|
|
|
|
# include <xmmintrin.h>
|
2019-04-17 06:17:24 +02:00
|
|
|
# endif
|
|
|
|
|
2016-02-27 19:10:00 +01:00
|
|
|
# ifdef WIN32
|
|
|
|
# include <float.h>
|
2020-03-19 09:33:03 +01:00
|
|
|
# include <windows.h>
|
2019-04-17 06:17:24 +02:00
|
|
|
# endif
|
2016-02-27 19:10:00 +01:00
|
|
|
|
2020-03-19 09:33:03 +01:00
|
|
|
# include <errno.h>
|
2016-02-27 19:10:00 +01:00
|
|
|
# include <stdlib.h>
|
|
|
|
# include <string.h>
|
|
|
|
|
2018-03-19 14:17:59 +01:00
|
|
|
# include "BLI_sys_types.h"
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-02-27 19:10:00 +01:00
|
|
|
# ifdef WIN32
|
|
|
|
# include "BLI_winstuff.h"
|
|
|
|
# endif
|
|
|
|
# include "BLI_fileops.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
# include "BLI_path_util.h"
|
|
|
|
# include "BLI_string.h"
|
2016-02-27 19:10:00 +01:00
|
|
|
# include "BLI_system.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
# include "BLI_utildefines.h"
|
2016-02-27 19:10:00 +01:00
|
|
|
# include BLI_SYSTEM_PID_H
|
|
|
|
|
|
|
|
# include "BKE_appdir.h" /* BKE_tempdir_base */
|
2016-04-24 14:42:41 +02:00
|
|
|
# include "BKE_blender_version.h"
|
2016-02-27 19:10:00 +01:00
|
|
|
# include "BKE_global.h"
|
|
|
|
# include "BKE_main.h"
|
|
|
|
# include "BKE_report.h"
|
|
|
|
|
|
|
|
# include <signal.h>
|
|
|
|
|
2020-08-06 07:34:55 +02:00
|
|
|
# ifdef WITH_PYTHON
|
2020-08-17 09:20:12 +02:00
|
|
|
# include "BPY_extern_python.h" /* BPY_python_backtrace */
|
2020-08-06 07:34:55 +02:00
|
|
|
# endif
|
|
|
|
|
2016-02-27 19:10:00 +01:00
|
|
|
# include "creator_intern.h" /* own include */
|
|
|
|
|
2018-03-19 14:17:59 +01:00
|
|
|
// #define USE_WRITE_CRASH_BLEND
|
|
|
|
# ifdef USE_WRITE_CRASH_BLEND
|
|
|
|
# include "BKE_undo_system.h"
|
|
|
|
# include "BLO_undofile.h"
|
|
|
|
# endif
|
|
|
|
|
2016-02-27 19:10:00 +01:00
|
|
|
/* set breakpoints here when running in debug mode, useful to catch floating point errors */
|
|
|
|
# if defined(__linux__) || defined(_WIN32) || defined(OSX_SSE_FPE)
|
|
|
|
static void sig_handle_fpe(int UNUSED(sig))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "debug: SIGFPE trapped\n");
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
|
2021-09-27 13:01:48 +02:00
|
|
|
/* Handling `Ctrl-C` event in the console. */
|
2016-02-27 19:10:00 +01:00
|
|
|
static void sig_handle_blender_esc(int sig)
|
|
|
|
{
|
|
|
|
G.is_break = true; /* forces render loop to read queue, not sure if its needed */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-02-27 19:10:00 +01:00
|
|
|
if (sig == 2) {
|
2022-08-26 04:51:46 +02:00
|
|
|
static int count = 0;
|
2016-02-27 19:10:00 +01:00
|
|
|
if (count) {
|
|
|
|
printf("\nBlender killed\n");
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
printf("\nSent an internal break event. Press ^C again to kill Blender\n");
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sig_handle_crash_backtrace(FILE *fp)
|
|
|
|
{
|
|
|
|
fputs("\n# backtrace\n", fp);
|
|
|
|
BLI_system_backtrace(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sig_handle_crash(int signum)
|
|
|
|
{
|
2020-01-15 18:08:34 +01:00
|
|
|
/* Might be called after WM/Main exit, so needs to be careful about NULL-checking before
|
2020-01-25 10:09:20 +01:00
|
|
|
* de-referencing. */
|
2020-01-15 18:08:34 +01:00
|
|
|
|
|
|
|
wmWindowManager *wm = G_MAIN ? G_MAIN->wm.first : NULL;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-03-19 14:17:59 +01:00
|
|
|
# ifdef USE_WRITE_CRASH_BLEND
|
2020-01-15 18:08:34 +01:00
|
|
|
if (wm && wm->undo_stack) {
|
2018-03-19 14:17:59 +01:00
|
|
|
struct MemFile *memfile = BKE_undosys_stack_memfile_get_active(wm->undo_stack);
|
|
|
|
if (memfile) {
|
2023-05-03 06:13:27 +02:00
|
|
|
char filepath[FILE_MAX];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-12-13 06:22:19 +01:00
|
|
|
if (!(G_MAIN && G_MAIN->filepath[0])) {
|
2023-05-03 06:13:27 +02:00
|
|
|
BLI_path_join(filepath, sizeof(filepath), BKE_tempdir_base(), "crash.blend");
|
2018-03-19 14:17:59 +01:00
|
|
|
}
|
|
|
|
else {
|
2023-05-03 06:13:27 +02:00
|
|
|
STRNCPY(filepath, G_MAIN->filepath);
|
|
|
|
BLI_path_extension_replace(filepath, sizeof(filepath), ".crash.blend");
|
2018-03-19 14:17:59 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-05-03 06:13:27 +02:00
|
|
|
printf("Writing: %s\n", filepath);
|
2018-03-19 14:17:59 +01:00
|
|
|
fflush(stdout);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-05-03 06:13:27 +02:00
|
|
|
BLO_memfile_write_file(memfile, filepath);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2018-03-19 14:17:59 +01:00
|
|
|
}
|
2016-02-27 19:10:00 +01:00
|
|
|
# endif
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-02-27 19:10:00 +01:00
|
|
|
FILE *fp;
|
|
|
|
char header[512];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-05-03 06:13:27 +02:00
|
|
|
char filepath[FILE_MAX];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-12-13 06:22:19 +01:00
|
|
|
if (!(G_MAIN && G_MAIN->filepath[0])) {
|
2023-05-03 06:13:27 +02:00
|
|
|
BLI_path_join(filepath, sizeof(filepath), BKE_tempdir_base(), "blender.crash.txt");
|
2016-02-27 19:10:00 +01:00
|
|
|
}
|
|
|
|
else {
|
2023-05-03 06:13:27 +02:00
|
|
|
BLI_path_join(
|
|
|
|
filepath, sizeof(filepath), BKE_tempdir_base(), BLI_path_basename(G_MAIN->filepath));
|
|
|
|
BLI_path_extension_replace(filepath, sizeof(filepath), ".crash.txt");
|
2016-02-27 19:10:00 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-05-03 06:13:27 +02:00
|
|
|
printf("Writing: %s\n", filepath);
|
2016-02-27 19:10:00 +01:00
|
|
|
fflush(stdout);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-02-27 19:10:00 +01:00
|
|
|
# ifndef BUILD_DATE
|
2023-05-09 04:50:37 +02:00
|
|
|
SNPRINTF(header, "# " BLEND_VERSION_FMT ", Unknown revision\n", BLEND_VERSION_ARG);
|
2016-02-27 19:10:00 +01:00
|
|
|
# else
|
|
|
|
BLI_snprintf(header,
|
|
|
|
sizeof(header),
|
|
|
|
"# " BLEND_VERSION_FMT ", Commit date: %s %s, Hash %s\n",
|
|
|
|
BLEND_VERSION_ARG,
|
|
|
|
build_commit_date,
|
|
|
|
build_commit_time,
|
|
|
|
build_hash);
|
|
|
|
# endif
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-02-27 19:10:00 +01:00
|
|
|
/* open the crash log */
|
|
|
|
errno = 0;
|
2023-05-03 06:13:27 +02:00
|
|
|
fp = BLI_fopen(filepath, "wb");
|
2016-02-27 19:10:00 +01:00
|
|
|
if (fp == NULL) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Unable to save '%s': %s\n",
|
2023-05-03 06:13:27 +02:00
|
|
|
filepath,
|
2016-02-27 19:10:00 +01:00
|
|
|
errno ? strerror(errno) : "Unknown error opening file");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (wm) {
|
|
|
|
BKE_report_write_file_fp(fp, &wm->reports, header);
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-02-27 19:10:00 +01:00
|
|
|
sig_handle_crash_backtrace(fp);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-08-06 07:34:55 +02:00
|
|
|
# ifdef WITH_PYTHON
|
|
|
|
/* Generate python back-trace if Python is currently active. */
|
|
|
|
BPY_python_backtrace(fp);
|
|
|
|
# endif
|
|
|
|
|
2016-02-27 19:10:00 +01:00
|
|
|
fclose(fp);
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-02-27 19:10:00 +01:00
|
|
|
/* Delete content of temp dir! */
|
|
|
|
BKE_tempdir_session_purge();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-02-27 19:10:00 +01:00
|
|
|
/* really crash */
|
|
|
|
signal(signum, SIG_DFL);
|
|
|
|
# ifndef WIN32
|
|
|
|
kill(getpid(), signum);
|
|
|
|
# else
|
|
|
|
TerminateProcess(GetCurrentProcess(), signum);
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
|
|
|
|
# ifdef WIN32
|
2020-05-01 15:37:48 +02:00
|
|
|
extern LONG WINAPI windows_exception_handler(EXCEPTION_POINTERS *ExceptionInfo)
|
2016-02-27 19:10:00 +01:00
|
|
|
{
|
2020-05-01 15:37:48 +02:00
|
|
|
/* If this is a stack overflow then we can't walk the stack, so just try to show
|
2016-02-27 19:10:00 +01:00
|
|
|
* where the error happened */
|
2020-05-01 15:37:48 +02:00
|
|
|
if (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_STACK_OVERFLOW) {
|
2020-05-03 05:42:49 +02:00
|
|
|
HMODULE mod;
|
|
|
|
CHAR modulename[MAX_PATH];
|
|
|
|
LPVOID address = ExceptionInfo->ExceptionRecord->ExceptionAddress;
|
|
|
|
fprintf(stderr, "Error : EXCEPTION_STACK_OVERFLOW\n");
|
|
|
|
fprintf(stderr, "Address : 0x%p\n", address);
|
|
|
|
if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, address, &mod)) {
|
|
|
|
if (GetModuleFileName(mod, modulename, MAX_PATH)) {
|
|
|
|
fprintf(stderr, "Module : %s\n", modulename);
|
2017-11-07 15:57:59 +01:00
|
|
|
}
|
2020-05-03 05:42:49 +02:00
|
|
|
}
|
2020-05-01 15:37:48 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
BLI_windows_handle_exception(ExceptionInfo);
|
2016-02-27 19:10:00 +01:00
|
|
|
sig_handle_crash(SIGSEGV);
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-02-27 19:10:00 +01:00
|
|
|
return EXCEPTION_EXECUTE_HANDLER;
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
|
|
|
|
static void sig_handle_abort(int UNUSED(signum))
|
|
|
|
{
|
|
|
|
/* Delete content of temp dir! */
|
|
|
|
BKE_tempdir_session_purge();
|
|
|
|
}
|
|
|
|
|
|
|
|
void main_signal_setup(void)
|
|
|
|
{
|
|
|
|
if (app_state.signal.use_crash_handler) {
|
|
|
|
# ifdef WIN32
|
|
|
|
SetUnhandledExceptionFilter(windows_exception_handler);
|
|
|
|
# else
|
|
|
|
/* after parsing args */
|
|
|
|
signal(SIGSEGV, sig_handle_crash);
|
|
|
|
# endif
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-09-29 20:42:02 +02:00
|
|
|
# ifdef WIN32
|
|
|
|
/* Prevent any error mode dialogs from hanging the application. */
|
|
|
|
SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOALIGNMENTFAULTEXCEPT | SEM_NOGPFAULTERRORBOX |
|
|
|
|
SEM_NOOPENFILEERRORBOX);
|
|
|
|
# endif
|
|
|
|
|
2016-02-27 19:10:00 +01:00
|
|
|
if (app_state.signal.use_abort_handler) {
|
|
|
|
signal(SIGABRT, sig_handle_abort);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void main_signal_setup_background(void)
|
|
|
|
{
|
2019-07-31 14:25:09 +02:00
|
|
|
/* for all platforms, even windows has it! */
|
2016-02-27 19:10:00 +01:00
|
|
|
BLI_assert(G.background);
|
|
|
|
|
2021-10-18 02:16:24 +02:00
|
|
|
/* Support pressing `Ctrl-C` to close Blender in background-mode.
|
|
|
|
* Useful to be able to cancel a render operation. */
|
|
|
|
signal(SIGINT, sig_handle_blender_esc);
|
2016-02-27 19:10:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void main_signal_setup_fpe(void)
|
|
|
|
{
|
|
|
|
# if defined(__linux__) || defined(_WIN32) || defined(OSX_SSE_FPE)
|
|
|
|
/* zealous but makes float issues a heck of a lot easier to find!
|
|
|
|
* set breakpoints on sig_handle_fpe */
|
|
|
|
signal(SIGFPE, sig_handle_fpe);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-08-18 00:04:56 +02:00
|
|
|
# if defined(__linux__) && defined(__GNUC__) && defined(HAVE_FEENABLEEXCEPT)
|
2016-02-27 19:10:00 +01:00
|
|
|
feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
|
|
|
|
# endif /* defined(__linux__) && defined(__GNUC__) */
|
|
|
|
# if defined(OSX_SSE_FPE)
|
|
|
|
/* OSX uses SSE for floating point by default, so here
|
|
|
|
* use SSE instructions to throw floating point exceptions */
|
|
|
|
_MM_SET_EXCEPTION_MASK(_MM_MASK_MASK &
|
|
|
|
~(_MM_MASK_OVERFLOW | _MM_MASK_INVALID | _MM_MASK_DIV_ZERO));
|
|
|
|
# endif /* OSX_SSE_FPE */
|
|
|
|
# if defined(_WIN32) && defined(_MSC_VER)
|
2019-01-15 13:25:45 +01:00
|
|
|
/* enables all fp exceptions */
|
|
|
|
_controlfp_s(NULL, 0, _MCW_EM);
|
|
|
|
/* hide the ones we don't care about */
|
|
|
|
_controlfp_s(NULL, _EM_DENORMAL | _EM_UNDERFLOW | _EM_INEXACT, _MCW_EM);
|
2016-02-27 19:10:00 +01:00
|
|
|
# endif /* _WIN32 && _MSC_VER */
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
|
2018-03-19 14:17:59 +01:00
|
|
|
#endif /* WITH_PYTHON_MODULE */
|