2013-01-19 20:16:57 +04:00
|
|
|
/***************************************************************************
|
2014-11-27 21:56:24 +03:00
|
|
|
* Copyright (C) 2005-09 by the Quassel Project *
|
|
|
|
* devel@quassel-irc.org *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) version 3. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
***************************************************************************/
|
2013-01-19 20:16:57 +04:00
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <dbghelp.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <QTextStream>
|
2014-11-28 01:18:34 +03:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
#include <cxxabi.h>
|
|
|
|
#endif
|
2013-01-19 20:16:57 +04:00
|
|
|
|
2014-11-27 21:56:24 +03:00
|
|
|
namespace straceWin
|
|
|
|
{
|
|
|
|
void loadHelpStackFrame(IMAGEHLP_STACK_FRAME&, const STACKFRAME64&);
|
|
|
|
BOOL CALLBACK EnumSymbolsCB(PSYMBOL_INFO, ULONG, PVOID);
|
|
|
|
BOOL CALLBACK EnumModulesCB(LPCSTR, DWORD64, PVOID);
|
|
|
|
const QString getBacktrace();
|
|
|
|
struct EnumModulesContext;
|
2014-11-28 01:18:34 +03:00
|
|
|
// Also works for MinGW64
|
|
|
|
#ifdef __MINGW32__
|
|
|
|
void demangle(QString& str);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __MINGW32__
|
|
|
|
void straceWin::demangle(QString& str)
|
|
|
|
{
|
|
|
|
char const* inStr = qPrintable("_" + str); // Really need that underline or demangling will fail
|
|
|
|
int status = 0;
|
|
|
|
size_t outSz = 0;
|
|
|
|
char* demangled_name = abi::__cxa_demangle(inStr, 0, &outSz, &status);
|
|
|
|
if (status == 0) {
|
|
|
|
str = QString::fromLocal8Bit(demangled_name);
|
|
|
|
if (outSz > 0)
|
|
|
|
free(demangled_name);
|
|
|
|
}
|
2013-01-19 20:16:57 +04:00
|
|
|
}
|
2014-11-28 01:18:34 +03:00
|
|
|
#endif
|
2013-01-19 20:16:57 +04:00
|
|
|
|
2014-11-27 21:56:24 +03:00
|
|
|
void straceWin::loadHelpStackFrame(IMAGEHLP_STACK_FRAME& ihsf, const STACKFRAME64& stackFrame)
|
|
|
|
{
|
|
|
|
ZeroMemory(&ihsf, sizeof(IMAGEHLP_STACK_FRAME));
|
|
|
|
ihsf.InstructionOffset = stackFrame.AddrPC.Offset;
|
|
|
|
ihsf.FrameOffset = stackFrame.AddrFrame.Offset;
|
2013-01-19 20:16:57 +04:00
|
|
|
}
|
|
|
|
|
2014-11-27 21:56:24 +03:00
|
|
|
BOOL CALLBACK straceWin::EnumSymbolsCB(PSYMBOL_INFO symInfo, ULONG size, PVOID user)
|
|
|
|
{
|
2014-11-28 01:18:34 +03:00
|
|
|
Q_UNUSED(size)
|
2014-11-27 21:56:24 +03:00
|
|
|
QStringList* params = (QStringList*)user;
|
|
|
|
if (symInfo->Flags & SYMFLAG_PARAMETER)
|
|
|
|
params->append(symInfo->Name);
|
|
|
|
return TRUE;
|
2013-01-19 20:16:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-27 21:56:24 +03:00
|
|
|
struct straceWin::EnumModulesContext
|
|
|
|
{
|
|
|
|
HANDLE hProcess;
|
|
|
|
QTextStream& stream;
|
|
|
|
EnumModulesContext(HANDLE hProcess, QTextStream& stream): hProcess(hProcess), stream(stream) {}
|
2013-01-19 20:16:57 +04:00
|
|
|
};
|
|
|
|
|
2014-11-27 21:56:24 +03:00
|
|
|
BOOL CALLBACK straceWin::EnumModulesCB(LPCSTR ModuleName, DWORD64 BaseOfDll, PVOID UserContext)
|
|
|
|
{
|
2014-11-28 01:18:34 +03:00
|
|
|
Q_UNUSED(ModuleName)
|
2014-11-27 21:56:24 +03:00
|
|
|
IMAGEHLP_MODULE64 mod;
|
|
|
|
EnumModulesContext* context = (EnumModulesContext*)UserContext;
|
|
|
|
mod.SizeOfStruct = sizeof(IMAGEHLP_MODULE64);
|
|
|
|
if(SymGetModuleInfo64(context->hProcess, BaseOfDll, &mod)) {
|
2014-11-27 22:23:15 +03:00
|
|
|
QString moduleBase = QString("0x%1").arg(BaseOfDll, 16, 16, QLatin1Char('0'));
|
2014-11-27 21:56:24 +03:00
|
|
|
QString line = QString("%1 %2 Image: %3")
|
2014-11-27 22:23:15 +03:00
|
|
|
.arg(mod.ModuleName, -25)
|
2014-11-27 21:56:24 +03:00
|
|
|
.arg(moduleBase, -13)
|
|
|
|
.arg(mod.LoadedImageName);
|
|
|
|
context->stream << line << '\n';
|
|
|
|
|
|
|
|
QString pdbName(mod.LoadedPdbName);
|
|
|
|
if(!pdbName.isEmpty()) {
|
|
|
|
QString line2 = QString("%1 %2")
|
|
|
|
.arg("", 35)
|
|
|
|
.arg(pdbName);
|
|
|
|
context->stream << line2 << '\n';
|
|
|
|
}
|
2013-01-19 20:16:57 +04:00
|
|
|
}
|
2014-11-27 21:56:24 +03:00
|
|
|
return TRUE;
|
2013-01-19 20:16:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined( _M_IX86 ) && defined(Q_CC_MSVC)
|
2014-11-27 21:56:24 +03:00
|
|
|
// Disable global optimization and ignore /GS waning caused by
|
|
|
|
// inline assembly.
|
|
|
|
// not needed with mingw cause we can tell mingw which registers we use
|
|
|
|
#pragma optimize("g", off)
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable : 4748)
|
2013-01-19 20:16:57 +04:00
|
|
|
#endif
|
2014-11-27 21:56:24 +03:00
|
|
|
const QString straceWin::getBacktrace()
|
|
|
|
{
|
|
|
|
DWORD MachineType;
|
|
|
|
CONTEXT Context;
|
|
|
|
STACKFRAME64 StackFrame;
|
2013-01-19 20:16:57 +04:00
|
|
|
|
|
|
|
#ifdef _M_IX86
|
|
|
|
ZeroMemory(&Context, sizeof(CONTEXT));
|
|
|
|
Context.ContextFlags = CONTEXT_CONTROL;
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef __MINGW32__
|
2014-11-27 21:56:24 +03:00
|
|
|
asm ("Label:\n\t"
|
|
|
|
"movl %%ebp,%0;\n\t"
|
|
|
|
"movl %%esp,%1;\n\t"
|
|
|
|
"movl $Label,%%eax;\n\t"
|
|
|
|
"movl %%eax,%2;\n\t"
|
|
|
|
: "=r" (Context.Ebp),"=r" (Context.Esp),"=r" (Context.Eip)
|
|
|
|
: //no input
|
|
|
|
: "eax");
|
2013-01-19 20:16:57 +04:00
|
|
|
#else
|
|
|
|
_asm {
|
2014-11-27 21:56:24 +03:00
|
|
|
Label:
|
|
|
|
mov [Context.Ebp], ebp;
|
|
|
|
mov [Context.Esp], esp;
|
|
|
|
mov eax, [Label];
|
|
|
|
mov [Context.Eip], eax;
|
2013-01-19 20:16:57 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
RtlCaptureContext(&Context);
|
|
|
|
#endif
|
|
|
|
|
2014-11-27 21:56:24 +03:00
|
|
|
ZeroMemory(&StackFrame, sizeof(STACKFRAME64));
|
2013-01-19 20:16:57 +04:00
|
|
|
#ifdef _M_IX86
|
2014-11-27 21:56:24 +03:00
|
|
|
MachineType = IMAGE_FILE_MACHINE_I386;
|
|
|
|
StackFrame.AddrPC.Offset = Context.Eip;
|
|
|
|
StackFrame.AddrPC.Mode = AddrModeFlat;
|
|
|
|
StackFrame.AddrFrame.Offset = Context.Ebp;
|
|
|
|
StackFrame.AddrFrame.Mode = AddrModeFlat;
|
|
|
|
StackFrame.AddrStack.Offset = Context.Esp;
|
|
|
|
StackFrame.AddrStack.Mode = AddrModeFlat;
|
2013-01-19 20:16:57 +04:00
|
|
|
#elif _M_X64
|
2014-11-27 21:56:24 +03:00
|
|
|
MachineType = IMAGE_FILE_MACHINE_AMD64;
|
|
|
|
StackFrame.AddrPC.Offset = Context.Rip;
|
|
|
|
StackFrame.AddrPC.Mode = AddrModeFlat;
|
|
|
|
StackFrame.AddrFrame.Offset = Context.Rsp;
|
|
|
|
StackFrame.AddrFrame.Mode = AddrModeFlat;
|
|
|
|
StackFrame.AddrStack.Offset = Context.Rsp;
|
|
|
|
StackFrame.AddrStack.Mode = AddrModeFlat;
|
2013-01-19 20:16:57 +04:00
|
|
|
#elif _M_IA64
|
2014-11-27 21:56:24 +03:00
|
|
|
MachineType = IMAGE_FILE_MACHINE_IA64;
|
|
|
|
StackFrame.AddrPC.Offset = Context.StIIP;
|
|
|
|
StackFrame.AddrPC.Mode = AddrModeFlat;
|
|
|
|
StackFrame.AddrFrame.Offset = Context.IntSp;
|
|
|
|
StackFrame.AddrFrame.Mode = AddrModeFlat;
|
|
|
|
StackFrame.AddrBStore.Offset = Context.RsBSP;
|
|
|
|
StackFrame.AddrBStore.Mode = AddrModeFlat;
|
|
|
|
StackFrame.AddrStack.Offset = Context.IntSp;
|
|
|
|
StackFrame.AddrStack.Mode = AddrModeFlat;
|
2013-01-19 20:16:57 +04:00
|
|
|
#else
|
2014-11-27 21:56:24 +03:00
|
|
|
#error "Unsupported platform"
|
2013-01-19 20:16:57 +04:00
|
|
|
#endif
|
|
|
|
|
2014-11-27 21:56:24 +03:00
|
|
|
QString log;
|
|
|
|
QTextStream logStream(&log);
|
|
|
|
logStream << "```\n";
|
|
|
|
|
|
|
|
HANDLE hProcess = GetCurrentProcess();
|
|
|
|
HANDLE hThread = GetCurrentThread();
|
|
|
|
SymInitialize(hProcess, NULL, TRUE);
|
|
|
|
|
|
|
|
DWORD64 dwDisplacement;
|
|
|
|
|
|
|
|
ULONG64 buffer[(sizeof(SYMBOL_INFO) +
|
|
|
|
MAX_SYM_NAME * sizeof(TCHAR) +
|
|
|
|
sizeof(ULONG64) - 1) / sizeof(ULONG64)];
|
|
|
|
PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)buffer;
|
|
|
|
pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
|
|
|
|
pSymbol->MaxNameLen = MAX_SYM_NAME;
|
|
|
|
|
|
|
|
IMAGEHLP_MODULE64 mod;
|
|
|
|
mod.SizeOfStruct = sizeof(IMAGEHLP_MODULE64);
|
|
|
|
|
|
|
|
IMAGEHLP_STACK_FRAME ihsf;
|
|
|
|
ZeroMemory(&ihsf, sizeof(IMAGEHLP_STACK_FRAME));
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
while(StackWalk64(MachineType, hProcess, hThread, &StackFrame, &Context, NULL, NULL, NULL, NULL)) {
|
|
|
|
if(i == 128)
|
|
|
|
break;
|
|
|
|
|
|
|
|
loadHelpStackFrame(ihsf, StackFrame);
|
|
|
|
if(StackFrame.AddrPC.Offset != 0) { // Valid frame.
|
|
|
|
|
|
|
|
QString fileName("???");
|
|
|
|
if(SymGetModuleInfo64(hProcess, ihsf.InstructionOffset, &mod)) {
|
|
|
|
fileName = QString(mod.ImageName);
|
|
|
|
int slashPos = fileName.lastIndexOf('\\');
|
|
|
|
if(slashPos != -1)
|
|
|
|
fileName = fileName.mid(slashPos + 1);
|
|
|
|
}
|
|
|
|
QString funcName;
|
2014-11-28 01:18:34 +03:00
|
|
|
if(SymFromAddr(hProcess, ihsf.InstructionOffset, &dwDisplacement, pSymbol)) {
|
2014-11-27 21:56:24 +03:00
|
|
|
funcName = QString(pSymbol->Name);
|
2014-11-28 01:18:34 +03:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
demangle(funcName);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else {
|
2014-11-27 21:56:24 +03:00
|
|
|
funcName = QString("0x%1").arg(ihsf.InstructionOffset, 8, 16, QLatin1Char('0'));
|
2014-11-28 01:18:34 +03:00
|
|
|
}
|
2014-11-27 21:56:24 +03:00
|
|
|
SymSetContext(hProcess, &ihsf, NULL);
|
2014-11-28 01:18:34 +03:00
|
|
|
#ifndef __MINGW32__
|
|
|
|
QStringList params;
|
2014-11-27 21:56:24 +03:00
|
|
|
SymEnumSymbols(hProcess, 0, NULL, EnumSymbolsCB, (PVOID)¶ms);
|
2014-11-28 01:18:34 +03:00
|
|
|
#endif
|
2014-11-27 21:56:24 +03:00
|
|
|
|
2014-11-27 22:23:15 +03:00
|
|
|
QString insOffset = QString("0x%1").arg(ihsf.InstructionOffset, 16, 16, QLatin1Char('0'));
|
2014-11-28 01:18:34 +03:00
|
|
|
QString formatLine = "#%1 %2 %3 %4";
|
|
|
|
#ifndef __MINGW32__
|
|
|
|
formatLine += "(%5)";
|
|
|
|
#endif
|
|
|
|
QString debugLine = formatLine
|
2014-11-27 21:56:24 +03:00
|
|
|
.arg(i, 3, 10)
|
|
|
|
.arg(fileName, -20)
|
|
|
|
.arg(insOffset, -11)
|
|
|
|
.arg(funcName)
|
2014-11-28 01:18:34 +03:00
|
|
|
#ifndef __MINGW32__
|
2014-11-27 21:56:24 +03:00
|
|
|
.arg(params.join(", "));
|
2014-11-28 01:18:34 +03:00
|
|
|
#else
|
|
|
|
;
|
|
|
|
#endif
|
2014-11-27 21:56:24 +03:00
|
|
|
logStream << debugLine << '\n';
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
break; // we're at the end.
|
|
|
|
}
|
2013-01-19 20:16:57 +04:00
|
|
|
}
|
|
|
|
|
2014-11-27 21:56:24 +03:00
|
|
|
logStream << "\n\nList of linked Modules:\n";
|
|
|
|
EnumModulesContext modulesContext(hProcess, logStream);
|
|
|
|
SymEnumerateModules64(hProcess, EnumModulesCB, (PVOID)&modulesContext);
|
|
|
|
logStream << "```";
|
|
|
|
return log;
|
2013-01-19 20:16:57 +04:00
|
|
|
}
|
|
|
|
#if defined(_M_IX86) && defined(Q_CC_MSVC)
|
2014-11-27 21:56:24 +03:00
|
|
|
#pragma warning(pop)
|
|
|
|
#pragma optimize("g", on)
|
2013-01-19 20:16:57 +04:00
|
|
|
#endif
|