-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_dump.h
119 lines (91 loc) · 3.3 KB
/
generate_dump.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#pragma once
#include "meta.h"
/*
Taken from: https://docs.microsoft.com/en-us/windows/win32/dxtecharts/crash-dump-analysis
Extension (c) 2020 by dbj.org -- LICENSE_DBJ
NOTE: SEH routines are intrinsic to the cl.exe
Usage:
inline void SomeFunction()
{
__try
{
int *pBadPtr = NULL;
*pBadPtr = 0;
}
__except(GenerateDump(GetExceptionInformation()))
{
}
}
*/
#include "win/windows_includer.h"
#include <assert.h>
#include <dbghelp.h>
#include <shellapi.h>
#include <shlobj.h>
#include <strsafe.h>
// StringCchPrintfA( szFileName, MAX_PATH, "%s%s", szPath, szAppName );
#undef win32_sprintf
#define win32_sprintf StringCchPrintfA
#pragma comment(lib, "Dbghelp.lib")
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
#define GENERATE_DUMP_MAX_PATH 1024
typedef struct generate_dump_last_run {
char dump_folder_name[GENERATE_DUMP_MAX_PATH] ;
char dump_file_name[GENERATE_DUMP_MAX_PATH] ;
BOOL finished_ok ;
} generate_dump_last_run;
static generate_dump_last_run dump_last_run ;
/*
DBJ: warning, currently this is a blocking call to an expensive function
*/
inline int GenerateDump(EXCEPTION_POINTERS* pExceptionPointers)
{
static generate_dump_last_run dump_last_run_clean = { {0}, {0}, FALSE } ;
dump_last_run = dump_last_run_clean ;
char szPath[GENERATE_DUMP_MAX_PATH] = {0};
char szFileName[GENERATE_DUMP_MAX_PATH] = {0};
const char * szAppName = DBJ_APP_NAME;
const char * szVersion = DBJ_APP_VERSION;
DWORD dwBufferSize = GENERATE_DUMP_MAX_PATH;
SYSTEMTIME stLocalTime;
MINIDUMP_EXCEPTION_INFORMATION ExpParam;
HANDLE hDumpFile = NULL ;
BOOL mini_dump_rezult = FALSE ;
GetLocalTime( &stLocalTime );
GetTempPathA( dwBufferSize, szPath );
win32_sprintf( szFileName, MAX_PATH, "%s%s", szPath, szAppName );
/*BOOL isDirCreated = */CreateDirectoryA( szFileName, NULL );
win32_sprintf( dump_last_run.dump_folder_name, MAX_PATH, "%s", szFileName );
win32_sprintf( szFileName, MAX_PATH, "%s%s\\%s-%04d%02d%02d-%02d%02d%02d-%ld-%ld.dmp",
szPath, szAppName, szVersion,
stLocalTime.wYear, stLocalTime.wMonth, stLocalTime.wDay,
stLocalTime.wHour, stLocalTime.wMinute, stLocalTime.wSecond,
GetCurrentProcessId(), GetCurrentThreadId());
hDumpFile = CreateFileA(szFileName, GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_WRITE|FILE_SHARE_READ, 0, CREATE_ALWAYS, 0, 0);
if (INVALID_HANDLE_VALUE == hDumpFile)
{
fprintf( stderr, "CreateFileA(%s) has failed with error code: %lu", szFileName, GetLastError());
goto exit;
}
win32_sprintf( dump_last_run.dump_file_name , MAX_PATH, "%s", szFileName );
ExpParam.ThreadId = GetCurrentThreadId();
ExpParam.ExceptionPointers = pExceptionPointers;
ExpParam.ClientPointers = TRUE;
mini_dump_rezult = MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(),
hDumpFile, MiniDumpWithDataSegs, &ExpParam, NULL, NULL);
#ifdef _DEBUG
if (mini_dump_rezult == 0)
{
fprintf( stderr, "MiniDumpWriteDump() has failed with error code: %lu", GetLastError());
}
#endif
dump_last_run.finished_ok = mini_dump_rezult;
exit:
return EXCEPTION_EXECUTE_HANDLER;
}
#ifdef __cplusplus
} // extern "C" {
#endif // __cplusplus