forked from xiaozai511/LuaMemorySnapShot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemoryrefrenceinfo.cpp
More file actions
453 lines (386 loc) · 10.9 KB
/
memoryrefrenceinfo.cpp
File metadata and controls
453 lines (386 loc) · 10.9 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#include "memoryrefrenceinfo.h"
#include "common.h"
#include <QTime>
#include <QStringList>
QByteArray MemoryRefrenceInfo::TYPE_TABLE("table:");
QByteArray MemoryRefrenceInfo::TYPE_FUNCTION("function:");
QByteArray MemoryRefrenceInfo::TYPE_STRING("string:");
QByteArray MemoryRefrenceInfo::TYPE_USERDATA("userdata:");
QByteArray MemoryRefrenceInfo::TYPE_THREAD("thread:");
MemoryRefrenceInfo::MemoryRefrenceInfo() :
m_type("")
, m_name("")
, m_tree("")
// , m_value("")
, m_refCount(0)
, m_parent(nullptr)
{
}
MemoryRefrenceInfo::~MemoryRefrenceInfo()
{
}
const QList<MemoryRefrenceInfo*>& MemoryRefrenceInfo::getChildren()
{
return m_children;
}
void MemoryRefrenceInfo::addChild(MemoryRefrenceInfo* info)
{
if(info && !m_children.contains(info)) {
m_children.append(info);
info->setParent(this);
}
}
MemoryRefrenceInfo* MemoryRefrenceInfo::getParent()
{
return m_parent;
}
void MemoryRefrenceInfo::setParent(MemoryRefrenceInfo* info)
{
if(info) {
m_parent = info;
}
}
void MemoryRefrenceInfo::setType(const QString& _type)
{
if(_type == MemoryRefrenceInfo::TYPE_FUNCTION) {
m_type = "Function";
} else if(_type == MemoryRefrenceInfo::TYPE_TABLE) {
m_type = "Table";
} else if(_type == MemoryRefrenceInfo::TYPE_STRING) {
m_type = "String";
} else if(_type == MemoryRefrenceInfo::TYPE_USERDATA) {
m_type = "UserData";
} else if(_type == MemoryRefrenceInfo::TYPE_THREAD) {
m_type = "Thread";
} else {
qDebug() << "Error Type!" << _type;
}
}
const QString& MemoryRefrenceInfo::getType()
{
return m_type;
}
void MemoryRefrenceInfo::setName(const QString& _name)
{
m_name = _name;
}
const QString& MemoryRefrenceInfo::getName()
{
return m_name;
}
void MemoryRefrenceInfo::setRefTree(const QString& _tree)
{
m_tree = _tree;
}
const QString& MemoryRefrenceInfo::getRefTree()
{
return m_tree;
}
void MemoryRefrenceInfo::setValue(const QString& _value)
{
// m_value = _value;
if(!m_value.contains(_value)) {
m_value.append(_value);
}
}
const QList<QString>& MemoryRefrenceInfo::getValue()
{
return m_value;
}
QString MemoryRefrenceInfo::getJoinValue()
{
// return m_value;
return m_value.join(",");
}
void MemoryRefrenceInfo::setRefCount(int _count)
{
m_refCount = _count;
}
int MemoryRefrenceInfo::getRefCount()
{
return m_refCount;
}
int MemoryRefrenceInfo::getSumRefCount()
{
int count = m_refCount;
for(auto child : m_children) {
count += child->getSumRefCount();
}
return count;
}
MemoryRefrenceInfo* MemoryRefrenceInfo::getRoot()
{
return m_parent ? m_parent->getRoot() : this;
}
void MemoryRefrenceInfo::sort(bool bUpper)
{
if(m_children.empty()) {
return;
}
static auto lessThan = [](MemoryRefrenceInfo* info1, MemoryRefrenceInfo* info2)->bool {
return info1->getSumRefCount() < info2->getSumRefCount();
};
static auto greaterThan = [](MemoryRefrenceInfo* info1, MemoryRefrenceInfo* info2)->bool {
return info1->getSumRefCount() > info2->getSumRefCount();
};
auto comFunc = bUpper ? lessThan : greaterThan;
qSort(m_children.begin(), m_children.end(), comFunc);
for(auto child : m_children) {
child->sort(bUpper);
}
}
//-----------------------------------------------------------------------
//MemoryRefrenceInfoMgr* MemoryRefrenceInfoMgr::sm_instance = nullptr;
MemoryRefrenceInfoMgr::MemoryRefrenceInfoMgr() :
m_filePath("")
, m_infoMgrBase(nullptr)
, m_bRunning(false)
{
}
MemoryRefrenceInfoMgr::~MemoryRefrenceInfoMgr()
{
for(auto iter = m_cacheInfos.begin(); iter != m_cacheInfos.end(); ++iter) {
if(iter->second) {
delete iter->second;
}
}
m_cacheInfos.clear();
}
void MemoryRefrenceInfoMgr::parse(const QString& strInfo, MemoryRefrenceInfoMgr* infoMgrBase)
{
auto time1 = QTime::currentTime();
//split string
//QStringList infos = strInfo.split(QRegExp("[ \t]"), QString::KeepEmptyParts);
QStringList infos;
QString tmpStr;
bool bQuot = false;
for(int i = 0; i < strInfo.length(); ++i) {
QChar ch = strInfo.at(i);
if(ch == "\"") {
bQuot = !bQuot;
tmpStr.append(ch);
} else if(!bQuot && (ch == " " || ch == "\t")) {
if(tmpStr.length() > 0) {
infos.push_back(tmpStr);
}
tmpStr.clear();
} else {
tmpStr.append(ch);
}
}
if(tmpStr.length() > 0) {
infos.push_back(tmpStr);
}
auto time2 = QTime::currentTime();
if(infos.empty() || infos.size() < 4 || infos.size() > 5) {
return;
}
QString strType = infos[0];
QString strValue = infos[1];
//解析函数、变量名、引用树
QString strRefTree;
if(infos[0] == MemoryRefrenceInfo::TYPE_FUNCTION) {
strRefTree = infos[2];
QString strDef;
int index = strRefTree.indexOf("[line:");
if(index != -1) {
strDef = strRefTree.right(strRefTree.length() - index);
strRefTree = strRefTree.left(index);
}
if(infos.size() == 5) { //去掉文件定义段,如[line:88@file:[string \"scripts/engine/baseclass.lua\"]]
strDef.append(infos[3]);
infos.removeAt(3);
}
strValue.append(strDef);
} else if(infos[0] == MemoryRefrenceInfo::TYPE_TABLE) {
strRefTree = infos[2];
} else if(infos[0] == MemoryRefrenceInfo::TYPE_STRING) {
strRefTree = infos[2];
strRefTree = strRefTree.section("[string]", 0, 0);
} else if(infos[0] == MemoryRefrenceInfo::TYPE_USERDATA) {
strRefTree = infos[2];
} else if(infos[0] == MemoryRefrenceInfo::TYPE_THREAD) {
strRefTree = infos[2];
} else {
qDebug() << "Type error, must be \"Table/Function/String\"!" << strInfo;
return;
}
// if(!strRefTree.startsWith("registry")) {
// //字符串内有空格时,解析有问题,暂时return掉,后面再修复
// return;
// }
//register
static auto _getInfoName = [](QString& strRefTree, QString& strName) {
int index = strRefTree.lastIndexOf(".");
if(index == -1) {
strName = strRefTree;
strRefTree.clear();
} else {
strName = strRefTree.right(strRefTree.length() - index - 1);
strRefTree = strRefTree.left(index);
}
};
auto _getAndCreateInfo = [=](QString& strRefTree, const QString& strValue = "", MemoryRefrenceInfoMgr* infoMgrBase = nullptr)->MemoryRefrenceInfo* {
QString strName;
_getInfoName(strRefTree, strName);
if(infoMgrBase && infoMgrBase->getByValue(strName, strValue)) {
return nullptr;
} else {
return get(strName, strRefTree, true);
}
};
//create cur info
MemoryRefrenceInfo* info = _getAndCreateInfo(strRefTree, strValue, infoMgrBase);
auto time3 = QTime::currentTime();
if(!info) {
return;
}
info->setType(strType);
info->setValue(strValue);
info->setRefCount(infos[3].toInt() + info->getRefCount());
//register parent
MemoryRefrenceInfo* pChild = info;
while(strRefTree.length() > 0) {
MemoryRefrenceInfo* pParent = _getAndCreateInfo(strRefTree);
if(pParent) {
pParent->addChild(pChild);
pChild = pParent;
}
}
auto time4 = QTime::currentTime();
qDebug() << m_cacheInfos.size()
<< time1.msecsTo(time2)
<< time2.msecsTo(time3)
<< time3.msecsTo(time4);
}
void MemoryRefrenceInfoMgr::parsFile(const QString& filepath, MemoryRefrenceInfoMgr* infoMgrBase)
{
m_filePath = filepath;
m_infoMgrBase = infoMgrBase;
m_cacheInfos.clear();
this->start();
}
MemoryRefrenceInfo* MemoryRefrenceInfoMgr::get(const QString& name, const QString& refTree, bool bCreate)
{
MemoryRefrenceInfo* info = nullptr;
auto range = m_cacheInfos.equal_range(name);
for(auto iter = range.first; iter != range.second; ++iter) {
if(iter->second->getRefTree() == refTree) {
info = iter->second;
break;
}
}
if(!info && bCreate) {
info = new MemoryRefrenceInfo;
info->setName(name);
info->setRefTree(refTree);
add(info);
}
return info;
}
MemoryRefrenceInfo* MemoryRefrenceInfoMgr::getByValue(const QString& name, const QString& value)
{
MemoryRefrenceInfo* info = nullptr;
auto range = m_cacheInfos.equal_range(name);
for(auto iter = range.first; iter != range.second; ++iter) {
const QList<QString>& values = iter->second->getValue();
if((value.length() == 0 && values.empty()) || values.contains(value)) {
info = iter->second;
break;
}
}
return info;
}
void MemoryRefrenceInfoMgr::add(MemoryRefrenceInfo* info)
{
if(info) {
// m_cacheInfos.insert(info->getName(), info);
m_cacheInfos.insert(std::make_pair(info->getName(), info));
}
}
void MemoryRefrenceInfoMgr::clear()
{
m_cacheInfos.clear();
}
void MemoryRefrenceInfoMgr::remove(MemoryRefrenceInfo* info)
{
if(info) {
//m_cacheInfos.remove(info->getName());
for(auto iter = m_cacheInfos.begin(); iter != m_cacheInfos.end(); ++iter) {
if(iter->second == info) {
m_cacheInfos.erase(iter);
break;
}
}
}
}
void MemoryRefrenceInfoMgr::sortAll(QList<MemoryRefrenceInfo*>& infos, bool bUpper)
{
static auto lessThan = [](MemoryRefrenceInfo* info1, MemoryRefrenceInfo* info2)->bool {
return info1->getSumRefCount() < info2->getSumRefCount();
};
static auto greaterThan = [](MemoryRefrenceInfo* info1, MemoryRefrenceInfo* info2)->bool {
return info1->getSumRefCount() > info2->getSumRefCount();
};
auto comFunc = bUpper ? lessThan : greaterThan;
qSort(infos.begin(), infos.end(), comFunc);
for(auto info : infos) {
info->sort(bUpper);
}
}
QList<MemoryRefrenceInfo*> MemoryRefrenceInfoMgr::getAllRootInfos()
{
QList<MemoryRefrenceInfo*> rootInfos;
for(auto iter = m_cacheInfos.begin(); iter != m_cacheInfos.end(); ++iter) {
MemoryRefrenceInfo* root = iter->second->getRoot();
if(!rootInfos.contains(root)) {
rootInfos.append(root);
}
}
return rootInfos;
}
void MemoryRefrenceInfoMgr::start(Priority priority)
{
m_bRunning = true;
QThread::start(priority);
}
void MemoryRefrenceInfoMgr::setRunning(bool bRun)
{
m_bRunning = bRun;
}
void MemoryRefrenceInfoMgr::run()
{
#if 0
//test
this->parse("function: 000000A4E124E570 registry.2[_G].RankListModule.[metatable].__newindex.[ups:table:vtbl].Instance.delete_me[line:88@file:[string \"scripts/engine/baseclass.lua\"]] 2780");
this->parse("function: 00007FFA73E43530 registry.2[_G].StringUtils.split.[ups:function:strSub][line:-1@file:[C]] 7");
this->parse("string: \"equip_ icon_10007\" registry.2[_G].ItemConfig.data.913207.Icon[string] 6");
this->parse("table: 000000A4F47047B0 registry.2[_G].WorldEnterController.__init.[ups:table:MOVE_TYPE] 6");
#else
auto _timeBegin = QTime::currentTime();
std::string filePath = m_filePath.toLocal8Bit().data();
FILE* pFile = fopen(filePath.c_str(), "r");
if(pFile) {
fseek(pFile, 0, SEEK_END);
long fileSize = ftell(pFile);
fseek(pFile, 0, SEEK_SET);
static const int maxLen = 1024 * 10;
char line[maxLen] = {0};
while (m_bRunning && fgets(line, maxLen, pFile)) {
this->parse(line, m_infoMgrBase);
int progress = ftell(pFile) * 100 / fileSize;
emit parseRunning(m_filePath,
_timeBegin.msecsTo(QTime::currentTime()),
progress);
}
fclose(pFile);
}
#endif
//中止时不发事件
if (m_bRunning) {
emit parseFinished(m_filePath);
}
//解析单次有效,解析完置空
m_filePath.clear();
m_infoMgrBase = nullptr;
}