12 #include <vxl_config.h> 14 # include <vcl_msvc_warnings.h> 40 #pragma comment (lib, "dbghelp") 42 static bool vul_debug_core_dump_in_windows_seh(
const char * filename,
43 EXCEPTION_POINTERS* pep)
45 static char buffer[2048];
47 std::snprintf(buffer,
sizeof(buffer), filename, count++);
48 buffer[
sizeof(buffer)-1]=0;
50 HANDLE hFile = CreateFile( buffer, GENERIC_READ | GENERIC_WRITE,
51 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
53 if ( ( hFile == NULL ) || ( hFile == INVALID_HANDLE_VALUE ) )
55 std::cerr <<
"WARNING: vul_debug_core_dump: Unable to create core dump file: " << filename << std::endl;
59 MINIDUMP_EXCEPTION_INFORMATION mdei;
60 mdei.ThreadId = GetCurrentThreadId();
61 mdei.ExceptionPointers = pep;
62 mdei.ClientPointers = FALSE;
64 if (! MiniDumpWriteDump( GetCurrentProcess(), GetCurrentProcessId(),
65 hFile, MiniDumpWithFullMemory, (pep != 0) ? &mdei : 0, 0, 0 ))
66 std::cerr <<
"WARNING: vul_debug_core_dump: Unable to dump core: " << filename << std::endl;
72 bool vul_debug_core_dump_in_windows_seh(
const char * filename,
75 return vul_debug_core_dump_in_windows_seh(filename, (EXCEPTION_POINTERS*)pep);
80 #pragma warning (disable: 4535) 85 _se_translator_function current = _set_se_translator(0);
89 RaiseException(0xe0000000,0,0,0);
91 __except(vul_debug_core_dump_in_windows_seh(filename, GetExceptionInformation()),1)
93 _set_se_translator(current);
100 return static_cast<EXCEPTION_POINTERS*>(
ex_ptr_)->ExceptionRecord->ExceptionCode;
106 return static_cast<EXCEPTION_POINTERS*>(
ex_ptr_)->ExceptionRecord->ExceptionAddress;
111 static char buf[100];
112 std::sprintf(buf,
"Caught Windows Structured Exception. Code %lx. Address %lx",
code(),
address());
116 static const char* se_coredump_filename = 0;
118 void vul_debug_set_coredump_and_throw_on_windows_se_handler(
119 unsigned code, EXCEPTION_POINTERS * ex_ptr)
121 vul_debug_core_dump_in_windows_seh(se_coredump_filename, ex_ptr);
130 se_coredump_filename = filename;
131 _set_se_translator(vul_debug_set_coredump_and_throw_on_windows_se_handler);
135 # else //VXL_HAS_DBGHELP_H 137 bool vul_debug_core_dump_in_windows_seh(
const char *,
void*)
139 std::cerr <<
"WARNING: vul_debug_core_dump_in_windows_seh: Unable to core dump\n";
145 std::cerr <<
"WARNING: vul_debug_core_dump: Unable to core dump\n";
163 return "Caught Windows Exception on machine with old or no version of DbgHelp.";
171 std::cerr <<
"WARNING: No DbgHelp.h on this platform - can't set SE Handler.\n";
174 # endif // VXL_HAS_DBGHELP_H 179 #ifdef VXL_UNISTD_HAS_GETPID 186 static int count = 0;
187 #ifdef VXL_UNISTD_HAS_GETPID 188 std::string syscall =
"gcore -o ";
191 if (system(syscall.c_str())==0)
return true;
192 syscall =
"gcore -s -c ";
195 if (system(syscall.c_str())==0)
return true;
197 std::cerr <<
"WARNING: vul_debug_core_dump: Unable to core dump\n";
213 static const char* out_of_memory_coredump_filename =
nullptr;
222 throw std::bad_alloc();
229 out_of_memory_coredump_filename = filename;
A translated structured exception.
void vul_debug_set_coredump_and_throw_on_windows_se(const char *)
Setup the system to core dump and throw a C++ exception on detection of a Structured Exception.
void * address() const
Related execution address.
void vul_debug_set_coredump_and_throw_on_out_of_memory_handler()
const char * what() const override
bool vul_debug_core_dump(const char *filename)
Dump a core file.
unsigned code() const
Windows structured exception code.
C++ conforming replacement to the ANSI C functions sprintf and printf.
void vul_debug_set_coredump_and_throw_on_out_of_memory(const char *filename)
Setup the system to core dump and throw a C++ exception on detection of out of memory.
creates a formatted ANSI C++ string