-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathui_debug.cpp
299 lines (268 loc) · 7.26 KB
/
ui_debug.cpp
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// DyLua: SimpleGraphic
// (c) David Gowor, 2014
//
// Module: UI Debug
//
#include "ui_local.h"
// =======
// Classes
// =======
struct d_lineHit_s {
char* source;
char* name;
int line;
int count;
};
struct d_callHit_s {
char* source;
char* name;
int count;
int lineHitNum;
int lineHitSz;
d_lineHit_s* lineHits;
};
// ===================
// ui_IDebug Interface
// ===================
class ui_debug_c : public ui_IDebug, public thread_c {
public:
// Interface
void SetProfiling(bool enable) override;
void ToggleProfiling() override;
// Encapsulated
ui_debug_c(ui_main_c* ui);
~ui_debug_c();
ui_main_c* ui = nullptr;
volatile bool doRun = false;
volatile bool isRunning = false;
volatile bool profiling = false;
volatile bool hookHold = false;
volatile bool hookHolding = false;
volatile int lineHitNum = 0;
int lineHitSz = 0;
d_lineHit_s* lineHits = nullptr;
volatile int callHitNum = 0;
int callHitSz = 0;
int callHitInitCount = 0;
d_callHit_s* callHits = nullptr;
void ThreadProc();
};
ui_IDebug* ui_IDebug::GetHandle(ui_main_c* ui)
{
return new ui_debug_c(ui);
}
void ui_IDebug::FreeHandle(ui_IDebug* hnd)
{
delete (ui_debug_c*)hnd;
}
ui_debug_c::ui_debug_c(ui_main_c* ui)
: thread_c(ui->sys), ui(ui)
{
profiling = false;
hookHold = false;
hookHolding = false;
lineHitNum = 0;
lineHitSz = 16;
lineHits = new d_lineHit_s[lineHitSz];
callHitNum = 0;
callHitSz = 16;
callHitInitCount = 0;
callHits = new d_callHit_s[callHitSz];
doRun = true;
ThreadStart();
}
ui_debug_c::~ui_debug_c()
{
profiling = false;
while (lineHitNum || callHitNum);
doRun = false;
while (isRunning);
delete lineHits;
for (int i = 0; i < callHitInitCount; i++) {
delete callHits[i].lineHits;
}
delete callHits;
}
// ==============
// UI Debug Class
// ==============
// Grab UI main pointer from the registry
static ui_debug_c* GetDebugPtr(lua_State* L)
{
lua_rawgeti(L, LUA_REGISTRYINDEX, 0);
ui_main_c* ui = (ui_main_c*)lua_touserdata(L, -1);
lua_pop(L, 1);
return (ui_debug_c*)ui->debug;
}
static void debugHook(lua_State* L, lua_Debug* ar)
{
ui_debug_c* d = GetDebugPtr(L);
d->hookHolding = true;
while (d->hookHold);
d->hookHolding = false;
}
static int lineComp(const void* aVoid, const void* bVoid)
{
d_lineHit_s* a = (d_lineHit_s*)aVoid;
d_lineHit_s* b = (d_lineHit_s*)bVoid;
if (a->count == b->count) {
return 0;
}
else {
return a->count > b->count ? -1 : 1;
}
}
static int callComp(const void* aVoid, const void* bVoid)
{
d_callHit_s* a = (d_callHit_s*)aVoid;
d_callHit_s* b = (d_callHit_s*)bVoid;
if (a->count == b->count) {
return 0;
}
else {
return a->count > b->count ? -1 : 1;
}
}
void ui_debug_c::ThreadProc()
{
isRunning = true;
while (doRun) {
ui->sys->Sleep(1);
if (profiling) {
if (!ui->inLua) {
continue;
}
hookHold = true;
lua_sethook(ui->L, &debugHook, LUA_MASKLINE, 0);
while (profiling && !hookHolding);
lua_sethook(ui->L, &debugHook, 0, 0);
if (!profiling) {
hookHold = false;
continue;
}
lua_Debug dbg;
memset(&dbg, 0, sizeof(dbg));
if (lua_getstack(ui->L, 0, &dbg) && lua_getinfo(ui->L, "Sln", &dbg) && dbg.source) {
int l;
for (l = 0; l < lineHitNum; l++) {
if (dbg.currentline == lineHits[l].line && !strcmp(dbg.source, lineHits[l].source)) {
if (dbg.name && !lineHits[l].name) {
lineHits[l].name = AllocString(dbg.name);
}
lineHits[l].count++;
break;
}
}
if (l == lineHitNum) {
if (lineHitNum == lineHitSz) {
lineHitSz <<= 1;
trealloc(lineHits, lineHitSz);
}
lineHits[l].source = AllocString(dbg.source);
lineHits[l].name = AllocString(dbg.name);
lineHits[l].line = dbg.currentline;
lineHits[l].count = 1;
lineHitNum++;
}
const char* funcSource = dbg.source;
const char* funcName = dbg.name;
if (funcName && lua_getstack(ui->L, 1, &dbg) && lua_getinfo(ui->L, "Sln", &dbg) && dbg.source) {
int c;
for (c = 0; c < callHitNum; c++) {
if (!strcmp(funcSource, callHits[c].source) && !strcmp(funcName, callHits[c].name)) {
callHits[c].count++;
break;
}
}
if (c == callHitNum) {
if (callHitNum == callHitSz) {
callHitSz <<= 1;
trealloc(callHits, callHitSz);
}
if (callHitNum == callHitInitCount) {
callHits[c].lineHitSz = 16;
callHits[c].lineHits = new d_lineHit_s[16];
callHitInitCount++;
}
callHits[c].source = AllocString(funcSource);
callHits[c].name = AllocString(funcName);
callHits[c].count = 1;
callHits[c].lineHitNum = 0;
callHitNum++;
}
d_callHit_s* call = callHits + c;
int l;
for (l = 0; l < call->lineHitNum; l++) {
if (dbg.currentline == call->lineHits[l].line && !strcmp(dbg.source, call->lineHits[l].source)) {
if (dbg.name && !call->lineHits[l].name) {
call->lineHits[l].name = AllocString(dbg.name);
}
call->lineHits[l].count++;
break;
}
}
if (l == call->lineHitNum) {
if (call->lineHitNum == call->lineHitSz) {
call->lineHitSz <<= 1;
trealloc(call->lineHits, call->lineHitSz);
}
call->lineHits[l].source = AllocString(dbg.source);
call->lineHits[l].name = AllocString(dbg.name);
call->lineHits[l].line = dbg.currentline;
call->lineHits[l].count = 1;
call->lineHitNum++;
}
}
}
hookHold = false;
while (hookHolding);
}
else if (lineHitNum) {
ui->sys->con->Printf("Hot lines:\n");
qsort(lineHits, lineHitNum, sizeof(d_lineHit_s), lineComp);
for (int l = 0; l < lineHitNum; l++) {
if (l < 20) {
ui->sys->con->Printf("%s(%d) in '%s': %d\n", lineHits[l].source, lineHits[l].line, lineHits[l].name ? lineHits[l].name : "?", lineHits[l].count);
}
delete lineHits[l].source;
delete lineHits[l].name;
}
lineHitNum = 0;
ui->sys->con->Printf("Hot calls:\n");
qsort(callHits, callHitNum, sizeof(d_callHit_s), callComp);
for (int c = 0; c < callHitNum; c++) {
qsort(callHits[c].lineHits, callHits[c].lineHitNum, sizeof(d_lineHit_s), lineComp);
if (c < 10) {
ui->sys->con->Printf("%s in '%s': %d\n", callHits[c].source, callHits[c].name, callHits[c].count);
}
for (int l = 0; l < callHits[c].lineHitNum; l++) {
if (c < 10 && l < 5) {
ui->sys->con->Printf("\t%s(%d) in '%s': %d\n", callHits[c].lineHits[l].source, callHits[c].lineHits[l].line, callHits[c].lineHits[l].name ? callHits[c].lineHits[l].name : "?", callHits[c].lineHits[l].count);
}
delete callHits[c].lineHits[l].source;
delete callHits[c].lineHits[l].name;
}
delete callHits[c].source;
delete callHits[c].name;
}
callHitNum = 0;
}
}
isRunning = false;
}
void ui_debug_c::SetProfiling(bool enable)
{
if (enable) {
ui->sys->con->Printf("Profiling enabled.\n");
profiling = true;
}
else {
ui->sys->con->Printf("Profiling finished:\n");
profiling = false;
while (lineHitNum || callHitNum);
}
}
void ui_debug_c::ToggleProfiling()
{
SetProfiling(!profiling);
}