diff options
Diffstat (limited to 'src/shared/leaktrace.cpp')
| -rw-r--r-- | src/shared/leaktrace.cpp | 78 |
1 files changed, 2 insertions, 76 deletions
diff --git a/src/shared/leaktrace.cpp b/src/shared/leaktrace.cpp index 729eb42e..0d99b82e 100644 --- a/src/shared/leaktrace.cpp +++ b/src/shared/leaktrace.cpp @@ -1,4 +1,5 @@ #include "leaktrace.h"
+#include "stackdata.h"
#include <Windows.h>
#include <DbgHelp.h>
#include <set>
@@ -8,82 +9,7 @@ #include <algorithm>
-static const int FRAMES_TO_SKIP = 3; // StackData::StackData(), __TraceData::regTrace(), TraceAlloc()
-static const int FRAMES_TO_CAPTURE = 10;
-
-
-void initDbgIfNecessary()
-{
- HANDLE process = ::GetCurrentProcess();
- static std::set<DWORD> initialized;
- if (initialized.find(::GetCurrentProcessId()) == initialized.end()) {
- static bool firstCall = true;
- if (firstCall) {
- ::SymSetOptions(SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS);
- firstCall = false;
- }
- if (!::SymInitialize(process, NULL, TRUE)) {
- printf("failed to initialize symbols: %d", ::GetLastError());
- }
- initialized.insert(::GetCurrentProcessId());
- }
-}
-
-
-class StackData {
- friend bool operator==(const StackData &LHS, const StackData &RHS);
- friend bool operator<(const StackData &LHS, const StackData &RHS);
-public:
-
- StackData()
- : m_Count(0), m_Hash(0UL), m_FunctionName("Dummy"), m_CodeLine(0)
- {}
- StackData(const char *functionName, int line) {
- m_Count = ::CaptureStackBackTrace(FRAMES_TO_SKIP, FRAMES_TO_CAPTURE, m_Stack, &m_Hash);
- m_FunctionName = functionName;
- m_CodeLine = line;
- if (m_Count == 0) {
- // TODO in this case the hash doesn't seem to be set. This is of course not a good solution
- m_Hash = reinterpret_cast<unsigned long>(m_FunctionName) + m_CodeLine;
- }
- }
- std::string toString() const {
- initDbgIfNecessary();
-
- char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)];
- PSYMBOL_INFO symbol = (PSYMBOL_INFO)buffer;
- symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
- symbol->MaxNameLen = MAX_SYM_NAME;
-
- std::ostringstream stackStream;
-
- stackStream << m_FunctionName << " [" << m_CodeLine << "]\n";
-
- for(unsigned int i = 0; i < m_Count; ++i) {
- DWORD64 displacement = 0;
- if (!::SymFromAddr(::GetCurrentProcess(), (DWORD64)m_Stack[i], &displacement, symbol)) {
- stackStream << m_Count - i - 1 << ": [" << m_Stack[i] << "]\n";
- } else {
- stackStream << m_Count - i - 1 << ": " << symbol->Name << "\n";
- }
- }
- return stackStream.str();
- }
-private:
- LPVOID m_Stack[FRAMES_TO_CAPTURE];
- USHORT m_Count;
- ULONG m_Hash;
- const char *m_FunctionName;
- int m_CodeLine;
-};
-
-bool operator==(const StackData &LHS, const StackData &RHS) {
- return LHS.m_Hash == RHS.m_Hash;
-}
-
-bool operator<(const StackData &LHS, const StackData &RHS) {
- return LHS.m_Hash < RHS.m_Hash;
-}
+using namespace MOShared;
static struct __TraceData {
|
