forked from SAPikachu/flash3kyuu_deband
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_dump.cpp
More file actions
250 lines (198 loc) · 5.81 KB
/
debug_dump.cpp
File metadata and controls
250 lines (198 loc) · 5.81 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include "debug_dump.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include <Shlobj.h>
#define DUMP_MAX_NAME_LENGTH 32
#define DUMP_MAX_STAGES 32
typedef struct _debug_dump_stage_t
{
FILE* fd;
int items_in_current_line;
TCHAR name[DUMP_MAX_NAME_LENGTH + 1];
} debug_dump_stage_t;
typedef struct _debug_dump_t
{
TCHAR dump_path[MAX_PATH];
int items_per_line;
debug_dump_stage_t stages[DUMP_MAX_STAGES];
} debug_dump_t;
const static TCHAR* DUMP_BASE_PATH = TEXT("%TEMP%\\f3kdb_dump\\");
#define FAIL_ON_ZERO(expr) if (!(expr)) abort()
__declspec(align(4))
static volatile DWORD _tls_slot = TLS_OUT_OF_INDEXES;
void ensure_tls_slot()
{
DWORD tls_slot = _tls_slot;
if (tls_slot == TLS_OUT_OF_INDEXES)
{
tls_slot = TlsAlloc();
if (InterlockedCompareExchange(&_tls_slot, tls_slot, TLS_OUT_OF_INDEXES) != TLS_OUT_OF_INDEXES)
{
// race failed, value initialized by other thread
TlsFree(tls_slot);
}
assert(_tls_slot != TLS_OUT_OF_INDEXES);
}
}
debug_dump_t* get_dump_handle(void)
{
ensure_tls_slot();
debug_dump_t* ret = (debug_dump_t*)TlsGetValue(_tls_slot);
assert(GetLastError() == ERROR_SUCCESS);
return ret;
}
debug_dump_t* create_dump_handle(void)
{
ensure_tls_slot();
debug_dump_t* ret = NULL;
size_t size = sizeof(debug_dump_t);
ret = (debug_dump_t*)malloc(size);
memset(ret, 0, size);
BOOL result = TlsSetValue(_tls_slot, ret);
assert(result);
return ret;
}
debug_dump_t* get_or_create_dump_handle(void)
{
debug_dump_t* ret = get_dump_handle();
if (!ret)
{
ret = create_dump_handle();
}
return ret;
}
void dump_init(const TCHAR* dump_base_name, int plane, int items_per_line)
{
debug_dump_t* handle = get_or_create_dump_handle();
handle->items_per_line = items_per_line;
assert(handle);
assert(dump_base_name);
FAIL_ON_ZERO(ExpandEnvironmentStrings(DUMP_BASE_PATH, handle->dump_path, MAX_PATH));
_tcscat(handle->dump_path, dump_base_name);
TCHAR plane_str[4];
memset(plane_str, 0, sizeof(plane_str));
_sntprintf(plane_str, ARRAYSIZE(plane_str) - 1, TEXT("%d"), plane);
_tcscat(handle->dump_path, TEXT("_"));
_tcscat(handle->dump_path, plane_str);
_tcscat(handle->dump_path, TEXT("\\"));
DWORD attributes = GetFileAttributes(handle->dump_path);
if (attributes == INVALID_FILE_ATTRIBUTES)
{
int ret = SHCreateDirectoryEx(NULL, handle->dump_path, NULL);
if (ret != ERROR_SUCCESS)
{
abort();
}
} else {
if ( (attributes & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY )
{
abort();
}
}
}
void dump_finish(void)
{
debug_dump_t* handle = get_dump_handle();
assert(handle);
for (int i = 0; i < DUMP_MAX_STAGES; i++)
{
if (handle->stages[i].fd)
{
fclose(handle->stages[i].fd);
memset(&(handle->stages[i]), 0, sizeof(debug_dump_stage_t));
}
}
free(handle);
TlsSetValue(_tls_slot, NULL);
}
void dump_next_line()
{
debug_dump_t* handle = get_dump_handle();
assert(handle);
const int dummy_value = 0xdeadbeef;
for (int i = 0; i < DUMP_MAX_STAGES; i++)
{
if (!handle->stages[i].fd)
{
return;
}
while (handle->stages[i].items_in_current_line < handle->items_per_line)
{
fwrite(&dummy_value, 4, 1, handle->stages[i].fd);
handle->stages[i].items_in_current_line++;
}
handle->stages[i].items_in_current_line = 0;
}
}
static debug_dump_stage_t* find_or_create_dump_stage(const TCHAR* dump_name)
{
debug_dump_t* handle = get_dump_handle();
assert(handle);
assert(dump_name);
assert(handle->dump_path[0] != 0);
assert(_tcslen(dump_name) <= DUMP_MAX_NAME_LENGTH);
for (int i = 0; i < DUMP_MAX_STAGES; i++)
{
if (!_tcscmp(dump_name, handle->stages[i].name))
{
return &handle->stages[i];
}
if (!handle->stages[i].fd)
{
// fd not found, create one
TCHAR file_name[MAX_PATH];
_tcscpy(file_name, handle->dump_path);
_tcscat(file_name, dump_name);
handle->stages[i].fd = _tfopen(file_name, TEXT("wb"));
if (!handle->stages[i].fd)
{
abort();
}
_tcscpy(handle->stages[i].name, dump_name);
return &handle->stages[i];
}
}
// too many stages
abort();
return NULL;
}
void dump_value(const TCHAR* dump_name, int value)
{
debug_dump_stage_t* stage = find_or_create_dump_stage(dump_name);
debug_dump_t* handle = get_dump_handle();
if (stage->items_in_current_line >= handle->items_per_line)
{
return;
}
fwrite(&value, 4, 1, stage->fd);
stage->items_in_current_line++;
}
void dump_value(const TCHAR* dump_name, __m128i value, int word_size_in_bytes, bool is_signed)
{
assert(word_size_in_bytes == 1 || word_size_in_bytes == 2 || word_size_in_bytes == 4);
debug_dump_stage_t* stage = find_or_create_dump_stage(dump_name);
debug_dump_t* handle = get_dump_handle();
__declspec(align(16))
char buffer[16];
_mm_store_si128((__m128i*)buffer, value);
int item;
for (int i = 0; i < 16 / word_size_in_bytes; i++)
{
if (stage->items_in_current_line >= handle->items_per_line)
{
return;
}
item = 0;
memcpy(&item, buffer + i * word_size_in_bytes, word_size_in_bytes);
if (is_signed)
{
int bits = (4 - word_size_in_bytes) * 8;
item <<= bits;
item >>= bits;
}
fwrite(&item, 4, 1, stage->fd);
stage->items_in_current_line++;
}
}