1#include "detect.h"
2
3#if defined(CONF_CRASHDUMP)
4#if !defined(CONF_FAMILY_WINDOWS)
5#error crash dumping not implemented
6#else
7
8#include "log.h"
9#include "system.h"
10#include "windows.h"
11
12#include <windows.h>
13
14static const char *CRASHDUMP_LIB = "exchndl.dll";
15static const char *CRASHDUMP_FN = "ExcHndlSetLogFileNameW";
16void crashdump_init_if_available(const char *log_file_path)
17{
18 HMODULE pCrashdumpLib = LoadLibraryA(CRASHDUMP_LIB);
19 if(pCrashdumpLib == nullptr)
20 {
21 const DWORD LastError = GetLastError();
22 const std::string ErrorMsg = windows_format_system_message(LastError);
23 log_error("crashdump", "failed to load crashdump library '%s' (error %ld %s)", CRASHDUMP_LIB, LastError, ErrorMsg.c_str());
24 return;
25 }
26 const std::wstring wide_log_file_path = windows_utf8_to_wide(log_file_path);
27 // Intentional
28#ifdef __MINGW32__
29#pragma GCC diagnostic push
30#pragma GCC diagnostic ignored "-Wcast-function-type"
31#endif
32 auto exception_log_file_path_func = (BOOL(APIENTRY *)(const WCHAR *))(GetProcAddress(pCrashdumpLib, CRASHDUMP_FN));
33#ifdef __MINGW32__
34#pragma GCC diagnostic pop
35#endif
36 if(exception_log_file_path_func == nullptr)
37 {
38 const DWORD LastError = GetLastError();
39 const std::string ErrorMsg = windows_format_system_message(LastError);
40 log_error("exception_handling", "could not find function '%s' in exception handling library (error %ld %s)", CRASHDUMP_FN, LastError, ErrorMsg.c_str());
41 return;
42 }
43
44 exception_log_file_path_func(wide_log_file_path.c_str());
45}
46#endif
47#else
48void crashdump_init_if_available(const char *log_file_path)
49{
50 (void)log_file_path;
51}
52#endif
53