1 /***************************************************************************
2 * Copyright (C) 2005-2014 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
29 #include <QTextStream>
31 void loadHelpStackFrame(IMAGEHLP_STACK_FRAME &ihsf, const STACKFRAME64 &stackFrame)
33 ZeroMemory(&ihsf, sizeof(IMAGEHLP_STACK_FRAME));
34 ihsf.InstructionOffset = stackFrame.AddrPC.Offset;
35 ihsf.FrameOffset = stackFrame.AddrFrame.Offset;
39 BOOL CALLBACK EnumSymbolsCB(PSYMBOL_INFO symInfo, ULONG size, PVOID user)
42 QStringList *params = (QStringList *)user;
43 if (symInfo->Flags & SYMFLAG_PARAMETER) {
44 params->append(symInfo->Name);
50 struct EnumModulesContext {
53 EnumModulesContext(HANDLE hProcess, QTextStream &stream) : hProcess(hProcess), stream(stream) {}
56 BOOL CALLBACK EnumModulesCB(LPCSTR ModuleName, DWORD64 BaseOfDll, PVOID UserContext)
59 IMAGEHLP_MODULE64 mod;
60 EnumModulesContext *context = (EnumModulesContext *)UserContext;
61 mod.SizeOfStruct = sizeof(IMAGEHLP_MODULE64);
62 if (SymGetModuleInfo64(context->hProcess, BaseOfDll, &mod)) {
63 QString line = QString("%1 0x%2 Image: %3").arg(mod.ModuleName, -14)
64 .arg(BaseOfDll, 8, 16, QLatin1Char('0'))
65 .arg(mod.LoadedImageName);
66 // qDebug() << qPrintable(line);
67 context->stream << line << '\n';
69 QString pdbName(mod.LoadedPdbName);
70 if (!pdbName.isEmpty()) {
71 QString line2 = QString("%1 %2").arg("", 32).arg(pdbName);
72 // qDebug() << qPrintable(line2);
73 context->stream << line2 << '\n';
80 #if defined(_M_IX86) && defined(Q_CC_MSVC)
81 // Disable global optimization and ignore /GS waning caused by
83 // not needed with mingw cause we can tell mingw which registers we use
84 #pragma optimize("g", off)
86 #pragma warning(disable : 4748)
88 void Quassel::logBacktrace(const QString &filename)
92 STACKFRAME64 StackFrame;
95 ZeroMemory(&Context, sizeof(CONTEXT));
96 Context.ContextFlags = CONTEXT_CONTROL;
102 "movl $Label,%%eax;\n\t"
104 : "=r" (Context.Ebp), "=r" (Context.Esp), "=r" (Context.Eip)
110 mov[Context.Ebp], ebp;
111 mov[Context.Esp], esp;
113 mov[Context.Eip], eax;
117 RtlCaptureContext(&Context);
120 ZeroMemory(&StackFrame, sizeof(STACKFRAME64));
122 MachineType = IMAGE_FILE_MACHINE_I386;
123 StackFrame.AddrPC.Offset = Context.Eip;
124 StackFrame.AddrPC.Mode = AddrModeFlat;
125 StackFrame.AddrFrame.Offset = Context.Ebp;
126 StackFrame.AddrFrame.Mode = AddrModeFlat;
127 StackFrame.AddrStack.Offset = Context.Esp;
128 StackFrame.AddrStack.Mode = AddrModeFlat;
129 #elif defined(_M_X64)
130 MachineType = IMAGE_FILE_MACHINE_AMD64;
131 StackFrame.AddrPC.Offset = Context.Rip;
132 StackFrame.AddrPC.Mode = AddrModeFlat;
133 StackFrame.AddrFrame.Offset = Context.Rsp;
134 StackFrame.AddrFrame.Mode = AddrModeFlat;
135 StackFrame.AddrStack.Offset = Context.Rsp;
136 StackFrame.AddrStack.Mode = AddrModeFlat;
137 #elif defined(_M_IA64)
138 MachineType = IMAGE_FILE_MACHINE_IA64;
139 StackFrame.AddrPC.Offset = Context.StIIP;
140 StackFrame.AddrPC.Mode = AddrModeFlat;
141 StackFrame.AddrFrame.Offset = Context.IntSp;
142 StackFrame.AddrFrame.Mode = AddrModeFlat;
143 StackFrame.AddrBStore.Offset = Context.RsBSP;
144 StackFrame.AddrBStore.Mode = AddrModeFlat;
145 StackFrame.AddrStack.Offset = Context.IntSp;
146 StackFrame.AddrStack.Mode = AddrModeFlat;
148 #error "Unsupported platform"
151 //EnterCriticalSection(&DbgHelpLock);
153 QFile logFile(filename);
154 logFile.open(QIODevice::Append);
155 QTextStream logStream(&logFile);
157 HANDLE hProcess = GetCurrentProcess();
158 HANDLE hThread = GetCurrentThread();
159 SymInitialize(hProcess, NULL, TRUE);
161 DWORD64 dwDisplacement;
163 ULONG64 buffer[(sizeof(SYMBOL_INFO) +
164 MAX_SYM_NAME*sizeof(TCHAR) +
165 sizeof(ULONG64) - 1) / sizeof(ULONG64)];
166 PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)buffer;
167 pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
168 pSymbol->MaxNameLen = MAX_SYM_NAME;
170 IMAGEHLP_MODULE64 mod;
171 mod.SizeOfStruct = sizeof(IMAGEHLP_MODULE64);
173 IMAGEHLP_STACK_FRAME ihsf;
174 ZeroMemory(&ihsf, sizeof(IMAGEHLP_STACK_FRAME));
177 while (StackWalk64(MachineType, hProcess, hThread, &StackFrame, &Context, NULL, NULL, NULL, NULL)) {
181 loadHelpStackFrame(ihsf, StackFrame);
182 if (StackFrame.AddrPC.Offset != 0) { // Valid frame.
183 QString fileName("???");
184 if (SymGetModuleInfo64(hProcess, ihsf.InstructionOffset, &mod)) {
185 fileName = QString(mod.ImageName);
186 int slashPos = fileName.lastIndexOf('\\');
188 fileName = fileName.mid(slashPos + 1);
191 if (SymFromAddr(hProcess, ihsf.InstructionOffset, &dwDisplacement, pSymbol)) {
192 funcName = QString(pSymbol->Name);
195 funcName = QString("0x%1").arg(ihsf.InstructionOffset, 8, 16, QLatin1Char('0'));
198 SymSetContext(hProcess, &ihsf, NULL);
199 SymEnumSymbols(hProcess, 0, NULL, EnumSymbolsCB, (PVOID)¶ms);
201 QString debugLine = QString("#%1 %2 0x%3 %4(%5)").arg(i, 3, 10)
203 .arg(ihsf.InstructionOffset, 8, 16, QLatin1Char('0'))
205 .arg(params.join(", "));
206 // qDebug() << qPrintable(debugLine);
207 logStream << debugLine << '\n';
211 break; // we're at the end.
215 // qDebug() << "List of linked Modules:";
216 logStream << "\n\nList of linked Modules:\n";
217 EnumModulesContext modulesContext(hProcess, logStream);
218 SymEnumerateModules64(hProcess, EnumModulesCB, (PVOID)&modulesContext);
224 #if defined(_M_IX86) && defined(Q_CC_MSVC)
226 #pragma optimize("g", on)