Repository navigation
Expand file tree
/
Copy pathcommon.c
More file actions
102 lines (87 loc) · 3.54 KB
/
Copy pathcommon.c
File metadata and controls
102 lines (87 loc) · 3.54 KB
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
#include "common.h"
/* ---------------------------------------------------------------
* AppendLog
* Append a timestamped message to the error log.
* Creates \Storage Card\BER\ if it does not exist.
* ------------------------------------------------------------- */
void AppendLog(LPCWSTR message)
{
HANDLE hFile;
SYSTEMTIME st;
WCHAR logLine[1024];
DWORD written;
GetLocalTime(&st);
wsprintf(logLine, L"[%04d-%02d-%02d %02d:%02d:%02d] %s\r\n",
st.wYear, st.wMonth, st.wDay,
st.wHour, st.wMinute, st.wSecond,
message);
/* Ensure log directory exists */
CreateDirectory(L"\\Storage Card\\BER", NULL);
hFile = CreateFile(ERROR_LOG_PATH,
GENERIC_WRITE, FILE_SHARE_READ,
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
return;
SetFilePointer(hFile, 0, NULL, FILE_END);
/* Write UTF-16LE (native on CE) */
WriteFile(hFile, logLine, lstrlenW(logLine) * sizeof(WCHAR), &written, NULL);
CloseHandle(hFile);
}
/* ---------------------------------------------------------------
* FormatDump
* Format a ContextDump_t into a human-readable string similar
* to a Linux kernel panic register dump.
* buf must be at least bufChars wide characters.
* ------------------------------------------------------------- */
static void AppendStr(WCHAR *buf, DWORD *pos, DWORD maxChars, LPCWSTR str)
{
DWORD len = (DWORD)lstrlenW(str);
if (*pos + len < maxChars)
{
lstrcpyW(buf + *pos, str);
*pos += len;
}
}
static LPCWSTR GetExceptionName(DWORD code)
{
switch (code)
{
case EXCEPTION_ACCESS_VIOLATION: return L"Access Violation";
case EXCEPTION_STACK_OVERFLOW: return L"Stack Overflow";
case EXCEPTION_ILLEGAL_INSTRUCTION: return L"Illegal Instruction";
case EXCEPTION_IN_PAGE_ERROR: return L"In-Page Error";
case EXCEPTION_INT_DIVIDE_BY_ZERO: return L"Integer Divide by Zero";
case EXCEPTION_PRIV_INSTRUCTION: return L"Privileged Instruction";
case EXCEPTION_DATATYPE_MISALIGNMENT: return L"Datatype Misalignment";
default: return L"Unknown";
}
}
void FormatDump(WCHAR *buf, DWORD bufChars, const BERExceptionContext_t *dump)
{
WCHAR tmp[128];
DWORD pos = 0;
const CONTEXT *c = &dump->threadContext;
#define APPEND(s) AppendStr(buf, &pos, bufChars, (s))
#define APPENDF(fmt, ...) do { wsprintf(tmp, (fmt), __VA_ARGS__); APPEND(tmp); } while (0)
APPEND(L"BER - Brain Exception Reporter\r\n");
APPEND(L"==============================\r\n\r\n");
APPENDF(L"Process: %s\r\n", dump->processImagePath);
APPENDF(L"PID: 0x%08X TID: 0x%08X\r\n", dump->processId, dump->threadId);
APPEND(L"\r\n");
APPENDF(L"Exception: 0x%08X (%s)\r\n",
dump->exceptionCode, GetExceptionName(dump->exceptionCode));
APPENDF(L"Address: 0x%08X\r\n", dump->exceptionAddress);
APPEND(L"\r\n");
APPEND(L"---- Register Dump ----\r\n");
APPENDF(L"R0 : %08X R1 : %08X R2 : %08X R3 : %08X\r\n",
c->R0, c->R1, c->R2, c->R3);
APPENDF(L"R4 : %08X R5 : %08X R6 : %08X R7 : %08X\r\n",
c->R4, c->R5, c->R6, c->R7);
APPENDF(L"R8 : %08X R9 : %08X R10: %08X R11: %08X\r\n",
c->R8, c->R9, c->R10, c->R11);
APPENDF(L"R12: %08X\r\n", c->R12);
APPENDF(L"SP : %08X LR : %08X PC : %08X\r\n", c->Sp, c->Lr, c->Pc);
APPENDF(L"PSR: %08X\r\n", c->Psr);
#undef APPEND
#undef APPENDF
}