Blender V5.3
creator_signals.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#ifndef WITH_PYTHON_MODULE
10
11# include <cerrno>
12# include <cstdlib>
13# include <mutex>
14
15# if defined(__linux__) && defined(__GNUC__)
16# ifndef _GNU_SOURCE
17# define _GNU_SOURCE
18# endif
19# include <cfenv>
20# endif
21
22# if (defined(__APPLE__) && (defined(__i386__) || defined(__x86_64__)))
23# define OSX_SSE_FPE
24# include <xmmintrin.h>
25# endif
26
27# ifdef WIN32
28# include <float.h>
29# include <windows.h>
30
31# include "BLI_winstuff.hh"
32
33# include "GPU_platform.hh"
34# endif
35
36# include "BLI_fileops.hh"
37# include "BLI_path_utils.hh"
38# include "BLI_string.hh"
39# include "BLI_system.hh"
40# include BLI_SYSTEM_PID_H
41
42# include "BKE_appdir.hh" /* #BKE_tempdir_session_purge. */
43# include "BKE_blender.hh"
44# include "BKE_blender_version.h"
45# include "BKE_global.hh"
46# include "BKE_main.hh"
47# include "BKE_report.hh"
48# include "BKE_wm_runtime.hh"
49
50# include <csignal>
51
52# ifdef WITH_PYTHON
53# include "BPY_extern_python.hh" /* #BPY_python_backtrace. */
54# endif
55
56# include "creator_intern.h" /* Own include. */
57
58namespace blender {
59
60# if defined(__linux__) || defined(_WIN32) || defined(OSX_SSE_FPE)
64static void sig_handle_fpe(int /*sig*/)
65{
66 fprintf(stderr, "debug: SIGFPE trapped\n");
67}
68# endif
69
70/* Handling `Ctrl-C` event in the console. */
71static void sig_handle_blender_esc(int sig)
72{
73 /* Forces render loop to read queue, not sure if its needed. */
74 G.is_break = true;
75
76 if (sig == 2) {
77 static int count = 0;
78 if (count) {
79 printf("\nBlender killed\n");
80 exit(2);
81 }
82 printf("\nSent an internal break event. Press ^C again to kill Blender\n");
83 count++;
84 }
85}
86
87static void crashlog_file_generate(const char *filepath, const void *os_info)
88{
89 /* Might be called after WM/Main exit, so needs to be careful about nullptr-checking before
90 * de-referencing. */
91
92 wmWindowManager *wm = G_MAIN ? G_MAIN->wm.first() : nullptr;
93
94 FILE *fp;
95 char header[512];
96 if (!app_state.signal.use_console_crash_handler) {
97 printf("Writing: %s\n", filepath);
98 }
99 fflush(stdout);
100
101# ifndef BUILD_DATE
102 SNPRINTF(header, "# " BLEND_VERSION_FMT ", Unknown revision\n", BLEND_VERSION_ARG);
103# else
104 SNPRINTF(header,
105 "# " BLEND_VERSION_FMT ", Commit date: %s %s, Hash %s\n",
109 build_hash);
110# endif
111
112 /* Open the crash log. */
113 errno = 0;
114 if (app_state.signal.use_console_crash_handler) {
115 fp = stderr;
116 }
117 else {
118 fp = BLI_fopen(filepath, "wb");
119 if (fp == nullptr) {
120 fprintf(stderr,
121 "Unable to save '%s': %s , falling back to console\n",
122 filepath,
123 errno ? strerror(errno) : "Unknown error opening file");
124 fp = stderr;
125 }
126 }
127
128 if (wm) {
129 BKE_report_write_file_fp(fp, &wm->runtime->reports, header);
130 }
131
132 fputs("\n# backtrace\n", fp);
134
135# ifdef WITH_PYTHON
136 /* Generate python back-trace if Python is currently active. */
138# endif
139 if (fp != stderr) {
140 fclose(fp);
141 }
142}
143
144static void sig_cleanup_and_terminate(int signum)
145{
146 /* Delete content of temp directory. */
148
149 /* Really crash. */
150 signal(signum, SIG_DFL);
151# ifndef WIN32
152 kill(getpid(), signum);
153# else
154 TerminateProcess(GetCurrentProcess(), signum);
155# endif
156}
157# if !defined(WIN32)
158static void sig_handle_crash_fn(int signum)
159{
160 auto crash_func = [&]() {
161 char filepath_crashlog[FILE_MAX];
162 BKE_blender_globals_crash_path_get(filepath_crashlog);
163 crashlog_file_generate(filepath_crashlog, nullptr);
165 };
166
167 /* The use of `std::call_once` ensures that the crash handling function is only executed once,
168 * even if multiple crashes occur simultaneously on different threads. */
169 static std::once_flag crash_func_once;
170 std::call_once(crash_func_once, crash_func);
171}
172# else
173extern LONG WINAPI windows_exception_handler(EXCEPTION_POINTERS *ExceptionInfo)
174{
175 /* If this is a stack overflow then we can't walk the stack, so just try to show
176 * where the error happened. */
177 if (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_STACK_OVERFLOW) {
178 HMODULE mod;
179 CHAR modulename[MAX_PATH];
180 LPVOID address = ExceptionInfo->ExceptionRecord->ExceptionAddress;
181 fprintf(stderr, "Error : EXCEPTION_STACK_OVERFLOW\n");
182 fprintf(stderr, "Address : 0x%p\n", address);
183 if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, LPCSTR(address), &mod)) {
184 if (GetModuleFileName(mod, modulename, MAX_PATH)) {
185 fprintf(stderr, "Module : %s\n", modulename);
186 }
187 }
188
190 }
191 else {
192 auto crash_func = [&]() {
193 char filepath_crashlog[FILE_MAX];
195 BKE_blender_globals_crash_path_get(filepath_crashlog);
196 crashlog_file_generate(filepath_crashlog, ExceptionInfo);
197
198 /* Disable popup in background mode to avoid blocking automation.
199 * (e.g., when used by a render farm; see #142314). */
200 if ((!G.background) && (!app_state.signal.use_console_crash_handler)) {
201 std::string version;
202# ifndef BUILD_DATE
203 const char *build_hash = G_MAIN ? G_MAIN->build_hash : "unknown";
204 version = std::string(BKE_blender_version_string()) + ", hash: `" + build_hash + "`";
205# else
206 version = std::string(BKE_blender_version_string()) +
207 ", Commit date: " + build_commit_date + " " + build_commit_time + ", hash: `" +
208 build_hash + "`";
209# endif
210
212 filepath_crashlog, G.filepath_last_blend, GPU_platform_gpu_name(), version.c_str());
213
215 }
216 };
217
218 /* The use of `std::call_once` ensures that the crash handling function is only executed once,
219 * even if multiple crashes occur simultaneously on different threads. */
220 static std::once_flag crash_func_once;
221 std::call_once(crash_func_once, crash_func);
222 }
223
224 return EXCEPTION_EXECUTE_HANDLER;
225}
226# endif
227
228static void sig_handle_abort(int /*signum*/)
229{
230 /* Delete content of temp directory. */
232}
233
235{
236 if (app_state.signal.use_crash_handler) {
237# ifdef WIN32
238 SetUnhandledExceptionFilter(windows_exception_handler);
239# else
240 /* After parsing arguments. */
241 signal(SIGSEGV, sig_handle_crash_fn);
242# endif
243 }
244
245# ifdef WIN32
246 /* Prevent any error mode dialogs from hanging the application. */
247 SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOALIGNMENTFAULTEXCEPT | SEM_NOGPFAULTERRORBOX |
248 SEM_NOOPENFILEERRORBOX);
249# endif
250
251 if (app_state.signal.use_abort_handler) {
252 signal(SIGABRT, sig_handle_abort);
253 }
254}
255
257{
258 /* for all platforms, even windows has it! */
259 BLI_assert(G.background);
260
261 /* Support pressing `Ctrl-C` to close Blender in background-mode.
262 * Useful to be able to cancel a render operation. */
263 signal(SIGINT, sig_handle_blender_esc);
264}
265
267{
268# if defined(__linux__) || defined(_WIN32) || defined(OSX_SSE_FPE)
269 /* Zealous but makes float issues a heck of a lot easier to find!
270 * Set breakpoints on #sig_handle_fpe. */
271 signal(SIGFPE, sig_handle_fpe);
272
273# if defined(__linux__) && defined(__GNUC__) && defined(HAVE_FEENABLEEXCEPT)
274 feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
275# endif /* defined(__linux__) && defined(__GNUC__) */
276# if defined(OSX_SSE_FPE)
277 /* OSX uses SSE for floating point by default, so here
278 * use SSE instructions to throw floating point exceptions. */
279 _MM_SET_EXCEPTION_MASK(_MM_MASK_MASK &
280 ~(_MM_MASK_OVERFLOW | _MM_MASK_INVALID | _MM_MASK_DIV_ZERO));
281# endif /* OSX_SSE_FPE */
282# if defined(_WIN32) && defined(_MSC_VER)
283 /* Enables all floating-point exceptions. */
284 _controlfp_s(nullptr, 0, _MCW_EM);
285 /* Hide the ones we don't care about. */
286 _controlfp_s(nullptr, _EM_DENORMAL | _EM_UNDERFLOW | _EM_INEXACT, _MCW_EM);
287# endif /* _WIN32 && _MSC_VER */
288# endif
289}
290
291} // namespace blender
292
293#endif /* WITH_PYTHON_MODULE */
Blender util stuff.
#define G_MAIN
File and directory operations.
#define FILE_MAX
#define SNPRINTF(dst, format,...)
Compatibility-like things for windows.
#define BLEND_VERSION_ARG
#define BLEND_VERSION_FMT
VecBase< float, D > constexpr mod(VecOp< float, D >, VecOp< float, D >) RET
constexpr toml::spec version
Definition recents.cc:33
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BKE_tempdir_session_purge()
Definition appdir.cc:1293
void BLI_windows_exception_print_message(const void *os_info)
static void crashlog_file_generate(const char *filepath, const void *os_info)
void BPY_python_backtrace(FILE *fp)
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void main_signal_setup(void)
void BLI_system_backtrace_with_os_info(FILE *fp, const void *os_info)
Definition system.cc:73
const char * GPU_platform_gpu_name()
ApplicationState app_state
Definition creator.cc:181
Global G
Definition blender.cc:57
char build_commit_time[]
Definition bpy_app.cc:72
void main_signal_setup_background(void)
static void sig_handle_blender_esc(int sig)
char build_hash[]
Definition bpy_app.cc:73
void BLI_windows_exception_show_dialog(const char *filepath_crashlog, const char *filepath_relaunch, const char *gpu_name, const char *build_version)
void main_signal_setup_fpe(void)
static void sig_handle_abort(int)
char build_commit_date[]
Definition bpy_app.cc:71
static void sig_cleanup_and_terminate(int signum)
const char * BKE_blender_version_string()
Definition blender.cc:147
bool BKE_report_write_file_fp(FILE *fp, ReportList *reports, const char *header)
Definition report.cc:408
static void sig_handle_crash_fn(int signum)
void BKE_blender_globals_crash_path_get(char *filepath)
struct blender::ApplicationState::@015013240256342036154333030232065173037361052222 signal