1
0
Fork 0

DBG: fixed the ENABLE_MEM_TRACE option

This commit is contained in:
mrexodia 2016-11-03 06:12:49 +01:00
parent 5f6060f9df
commit b61a681f25
No known key found for this signature in database
GPG Key ID: FC89E0AAA0C1AAD8
6 changed files with 56 additions and 46 deletions

View File

@ -894,11 +894,11 @@ extern "C" DLL_EXPORT duint _dbg_sendmessage(DBGMSG type, void* param1, void* pa
else
assemblerEngine = AssemblerEngine::XEDParse;
Memory<char*> settingText(MAX_SETTING_SIZE + 1);
std::vector<char> settingText(MAX_SETTING_SIZE + 1, '\0');
dbgclearignoredexceptions();
if(BridgeSettingGet("Exceptions", "IgnoreRange", settingText()))
if(BridgeSettingGet("Exceptions", "IgnoreRange", settingText.data()))
{
auto entry = strtok(settingText(), ",");
auto entry = strtok(settingText.data(), ",");
while(entry)
{
unsigned long start;
@ -914,10 +914,10 @@ extern "C" DLL_EXPORT duint _dbg_sendmessage(DBGMSG type, void* param1, void* pa
}
}
if(BridgeSettingGet("Symbols", "CachePath", settingText()))
if(BridgeSettingGet("Symbols", "CachePath", settingText.data()))
{
// Trim the buffer to fit inside MAX_PATH
strncpy_s(szSymbolCachePath, settingText(), _TRUNCATE);
strncpy_s(szSymbolCachePath, settingText.data(), _TRUNCATE);
}
duint animateInterval;

View File

@ -21,7 +21,8 @@ static int emalloc_count = 0;
\brief Path for debugging, used to create an allocation trace file on emalloc() or efree(). Not used.
*/
static char alloctrace[MAX_PATH] = "";
static std::map<void*, int> alloctracemap;
static std::unordered_map<void*, int> alloctracemap;
static CRITICAL_SECTION criticalSection;
#endif
/**
@ -45,12 +46,14 @@ void* emalloc(size_t size, const char* reason)
}
emalloc_count++;
#ifdef ENABLE_MEM_TRACE
EnterCriticalSection(&criticalSection);
memset(a, 0, size + sizeof(void*));
FILE* file = fopen(alloctrace, "a+");
fprintf(file, "DBG%.5d: alloc:%p:%p:%s:%p\n", emalloc_count, a, _ReturnAddress(), reason, size);
fclose(file);
alloctracemap[_ReturnAddress()]++;
*(void**)a = _ReturnAddress();
LeaveCriticalSection(&criticalSection);
return a + sizeof(void*);
#else
memset(a, 0, size);
@ -85,6 +88,7 @@ void efree(void* ptr, const char* reason)
{
emalloc_count--;
#ifdef ENABLE_MEM_TRACE
EnterCriticalSection(&criticalSection);
char* ptr2 = (char*)ptr - sizeof(void*);
FILE* file = fopen(alloctrace, "a+");
fprintf(file, "DBG%.5d: free:%p:%p:%s\n", emalloc_count, ptr, *(void**)ptr2, reason);
@ -94,14 +98,17 @@ void efree(void* ptr, const char* reason)
if(--alloctracemap.at(*(void**)ptr2) < 0)
{
String str = StringUtils::sprintf("address %p, reason %s", *(void**)ptr2, reason);
MessageBoxA(0, str.c_str, "Free memory more than once", MB_OK);
MessageBoxA(0, str.c_str(), "Freed memory more than once", MB_OK);
__debugbreak();
}
}
else
{
String str = StringUtils::sprintf("address %p, reason %s", *(void**)ptr2, reason);
MessageBoxA(0, str.c_str(), "Trying to free a const memory", MB_OK);
MessageBoxA(0, str.c_str(), "Trying to free const memory", MB_OK);
__debugbreak();
}
LeaveCriticalSection(&criticalSection);
GlobalFree(ptr2);
#else
GlobalFree(ptr);
@ -133,14 +140,20 @@ void json_free(void* ptr)
int memleaks()
{
#ifdef ENABLE_MEM_TRACE
EnterCriticalSection(&criticalSection);
auto leaked = false;
for(auto & i : alloctracemap)
{
if(i.second != 0)
{
String str = StringUtils::sprintf("memory leak at %p : count %d", i.first, i.second);
MessageBoxA(0, str.c_str(), "memory leaks", MB_OK);
MessageBoxA(0, str.c_str(), "memory leak", MB_OK);
leaked = true;
}
}
if(leaked)
__debugbreak();
LeaveCriticalSection(&criticalSection);
#endif
return emalloc_count;
}
@ -152,6 +165,7 @@ int memleaks()
*/
void setalloctrace(const char* file)
{
InitializeCriticalSection(&criticalSection);
strcpy_s(alloctrace, file);
}
#endif //ENABLE_MEM_TRACE

View File

@ -101,10 +101,7 @@ void CmdLineCacheSave(JSON Root)
// Create a JSON array to store each sub-object with a breakpoint
const JSON jsonCmdLine = json_object();
json_object_set_new(jsonCmdLine, "cmdLine", json_string(commandLine));
json_object_set(Root, "commandLine", jsonCmdLine);
// Notify garbage collector
json_decref(jsonCmdLine);
json_object_set_new(Root, "commandLine", jsonCmdLine);
}
void CmdLineCacheLoad(JSON Root)

View File

@ -175,9 +175,8 @@ public:
auto jsonValue = json_object();
serializer.SetJson(jsonValue);
if(serializer.Save(itr.second))
json_array_append_new(jsonValues, jsonValue);
else
json_decref(jsonValue);
json_array_append(jsonValues, jsonValue);
json_decref(jsonValue);
}
if(json_array_size(jsonValues))
json_object_set(root, jsonKey(), jsonValues);

View File

@ -1,6 +1,5 @@
#include "stringutils.h"
#include "value.h"
#include "dynamicmem.h"
#include <windows.h>
#include <cstdint>
@ -245,40 +244,40 @@ String StringUtils::sprintf(_Printf_format_string_ const char* format, ...)
{
va_list args;
va_start(args, format);
Memory<char*> buffer(256 * sizeof(char), "StringUtils::sprintf");
std::vector<char> buffer(256, '\0');
while(true)
{
int res = _vsnprintf_s(buffer(), buffer.size(), _TRUNCATE, format, args);
int res = _vsnprintf_s(buffer.data(), buffer.size(), _TRUNCATE, format, args);
if(res == -1)
{
buffer.realloc(buffer.size() * 2, "StringUtils::sprintf");
buffer.resize(buffer.size() * 2);
continue;
}
else
break;
}
va_end(args);
return String(buffer());
return String(buffer.data());
}
WString StringUtils::sprintf(_Printf_format_string_ const wchar_t* format, ...)
{
va_list args;
va_start(args, format);
Memory<wchar_t*> buffer(256 * sizeof(wchar_t), "StringUtils::sprintf");
std::vector<wchar_t> buffer(256, L'\0');
while(true)
{
int res = _vsnwprintf_s(buffer(), buffer.size(), _TRUNCATE, format, args);
int res = _vsnwprintf_s(buffer.data(), buffer.size(), _TRUNCATE, format, args);
if(res == -1)
{
buffer.realloc(buffer.size() * 2, "StringUtils::sprintf");
buffer.resize(buffer.size() * 2);
continue;
}
else
break;
}
va_end(args);
return WString(buffer());
return WString(buffer.data());
}
String StringUtils::ToLower(const String & s)

View File

@ -591,22 +591,6 @@ extern "C" DLL_EXPORT const char* _dbg_dbginit()
static_assert(sizeof(TITAN_ENGINE_CONTEXT_t) == sizeof(REGISTERCONTEXT), "Invalid REGISTERCONTEXT alignment!");
dputs(QT_TRANSLATE_NOOP("DBG", "Initializing wait objects..."));
waitinitialize();
dputs(QT_TRANSLATE_NOOP("DBG", "Initializing debugger..."));
dbginit();
dputs(QT_TRANSLATE_NOOP("DBG", "Initializing debugger functions..."));
dbgfunctionsinit();
#ifdef ENABLE_MEM_TRACE
dputs(QT_TRANSLATE_NOOP("DBG", "Setting JSON memory management functions..."));
json_set_alloc_funcs(json_malloc, json_free);
#endif //ENABLE_MEM_TRACE
dputs(QT_TRANSLATE_NOOP("DBG", "Initializing capstone..."));
Capstone::GlobalInitialize();
dputs(QT_TRANSLATE_NOOP("DBG", "Initializing Yara..."));
if(yr_initialize() != ERROR_SUCCESS)
return "Failed to initialize Yara!";
dputs(QT_TRANSLATE_NOOP("DBG", "Getting directory information..."));
wchar_t wszDir[deflen] = L"";
if(!GetModuleFileNameW(hInst, wszDir, deflen))
return "GetModuleFileNameW failed!";
@ -621,6 +605,24 @@ extern "C" DLL_EXPORT const char* _dbg_dbginit()
DeleteFileW(StringUtils::Utf8ToUtf16(alloctrace).c_str());
setalloctrace(alloctrace);
#endif //ENABLE_MEM_TRACE
dputs(QT_TRANSLATE_NOOP("DBG", "Initializing wait objects..."));
waitinitialize();
dputs(QT_TRANSLATE_NOOP("DBG", "Initializing debugger..."));
dbginit();
dputs(QT_TRANSLATE_NOOP("DBG", "Initializing debugger functions..."));
dbgfunctionsinit();
//#ifdef ENABLE_MEM_TRACE
dputs(QT_TRANSLATE_NOOP("DBG", "Setting JSON memory management functions..."));
json_set_alloc_funcs(json_malloc, json_free);
//#endif //ENABLE_MEM_TRACE
dputs(QT_TRANSLATE_NOOP("DBG", "Initializing capstone..."));
Capstone::GlobalInitialize();
dputs(QT_TRANSLATE_NOOP("DBG", "Initializing Yara..."));
if(yr_initialize() != ERROR_SUCCESS)
return "Failed to initialize Yara!";
dputs(QT_TRANSLATE_NOOP("DBG", "Getting directory information..."));
strcpy_s(scriptDllDir, szProgramDir);
strcat_s(scriptDllDir, "\\scripts\\");
initDataInstMap();
@ -739,11 +741,6 @@ extern "C" DLL_EXPORT void _dbg_dbgexitsignal()
varfree();
yr_finalize();
Capstone::GlobalFinalize();
dputs(QT_TRANSLATE_NOOP("DBG", "Checking for mem leaks..."));
if(auto memleakcount = memleaks())
dprintf(QT_TRANSLATE_NOOP("DBG", "%d memory leak(s) found!\n"), memleakcount);
else
DeleteFileW(StringUtils::Utf8ToUtf16(alloctrace).c_str());
dputs(QT_TRANSLATE_NOOP("DBG", "Cleaning up wait objects..."));
waitdeinitialize();
dputs(QT_TRANSLATE_NOOP("DBG", "Cleaning up debugger threads..."));
@ -759,6 +756,10 @@ extern "C" DLL_EXPORT void _dbg_dbgexitsignal()
else
DeleteFileW(StringUtils::Utf8ToUtf16(notesFile).c_str());
dputs(QT_TRANSLATE_NOOP("DBG", "Exit signal processed successfully!"));
#ifdef ENABLE_MEM_TRACE
if(!memleaks())
DeleteFileW(StringUtils::Utf8ToUtf16(alloctrace).c_str());
#endif //ENABLE_MEM_TRACE
bIsStopped = true;
}