File indexing completed on 2024-12-01 13:33:59

0001 /******************************************************************
0002  *
0003  * kdbgwin - Helper application for DrKonqi
0004  *
0005  * This file is part of the KDE project
0006  *
0007  * SPDX-FileCopyrightText: 2010 Ilie Halip <lupuroshu@gmail.com>
0008  *
0009  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0010  *****************************************************************/
0011 
0012 #include "msvc_generator.h"
0013 
0014 MsvcGenerator::MsvcGenerator(const Process &process)
0015     : AbstractBTGenerator(process)
0016 {
0017 }
0018 
0019 bool MsvcGenerator::Init()
0020 {
0021     return true;
0022 }
0023 
0024 void MsvcGenerator::UnInit()
0025 {
0026 }
0027 
0028 QString MsvcGenerator::GetFunctionName()
0029 {
0030     PSYMBOL_INFO symbol = (PSYMBOL_INFO)malloc(sizeof(SYMBOL_INFO) + MAX_SYMBOL_NAME);
0031     ZeroMemory(symbol, sizeof(symbol) + MAX_SYMBOL_NAME);
0032     symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
0033     symbol->MaxNameLen = MAX_SYMBOL_NAME;
0034 
0035     DWORD64 dwDisplacement = 0;
0036 
0037     if (!SymFromAddr(m_process.GetHandle(), m_currentFrame.AddrPC.Offset, &dwDisplacement, symbol)) {
0038         qCCritical(DRKONQI_LOG) << "SymFromAddr() failed: " << GetLastError();
0039         return QString::fromLatin1(DEFAULT_FUNC);
0040     }
0041 
0042     char undecoratedName[MAX_PATH] = {0};
0043     if (!UnDecorateSymbolName(symbol->Name, undecoratedName, MAX_PATH, UNDNAME_COMPLETE)) {
0044         // if this fails, show the decorated name anyway, don't fail
0045         qCCritical(DRKONQI_LOG) << "UnDecorateSymbolName() failed: " << GetLastError();
0046         return QString::fromLatin1(symbol->Name);
0047     }
0048 
0049     return QString::fromLatin1(undecoratedName);
0050 }
0051 
0052 QString MsvcGenerator::GetFile()
0053 {
0054     IMAGEHLP_LINE64 line;
0055     ZeroMemory(&line, sizeof(line));
0056     line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
0057     DWORD dwDisplacement = 0;
0058 
0059     if (!SymGetLineFromAddr64(m_process.GetHandle(), m_currentFrame.AddrPC.Offset, &dwDisplacement, &line)) {
0060         qCCritical(DRKONQI_LOG) << "SymGetLineFromAddr64 failed: " << GetLastError();
0061         return QString::fromLatin1(DEFAULT_FILE);
0062     }
0063 
0064     return QString::fromLatin1(const_cast<char *>(line.FileName));
0065 }
0066 
0067 int MsvcGenerator::GetLine()
0068 {
0069     IMAGEHLP_LINE64 line;
0070     ZeroMemory(&line, sizeof(line));
0071     line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
0072     DWORD dwDisplacement = 0;
0073 
0074     if (!SymGetLineFromAddr64(m_process.GetHandle(), m_currentFrame.AddrPC.Offset, &dwDisplacement, &line)) {
0075         // qCCritical(DRKONQI_LOG) << "SymGetLineFromAddr64 failed: " << GetLastError();
0076         return DEFAULT_LINE;
0077     }
0078 
0079     return (int)line.LineNumber;
0080 }
0081 
0082 void MsvcGenerator::LoadSymbol(const QString &module, DWORD64 dwBaseAddr)
0083 {
0084     QString strOutput;
0085 
0086     IMAGEHLP_MODULE64 moduleInfo;
0087     ZeroMemory(&moduleInfo, sizeof(moduleInfo));
0088     moduleInfo.SizeOfStruct = sizeof(moduleInfo);
0089     SymGetModuleInfo64(m_process.GetHandle(), dwBaseAddr, &moduleInfo);
0090 
0091     m_symbolsMap[module] = false; // default
0092     QString symbolType;
0093     switch (moduleInfo.SymType) {
0094     case SymNone:
0095         symbolType = QString::fromLatin1("no symbols loaded");
0096         break;
0097     case SymCoff:
0098     case SymCv:
0099     case SymPdb:
0100     case SymSym:
0101     case SymDia:
0102         symbolType = QString::fromLatin1("symbols loaded");
0103         m_symbolsMap[module] = true;
0104         break;
0105     case SymExport:
0106         symbolType = QString::fromLatin1("export table only");
0107         break;
0108     case SymDeferred:
0109         symbolType = QString::fromLatin1("deferred (not loaded currently)");
0110         break;
0111     case SymVirtual:
0112         symbolType = QString::fromLatin1("virtual");
0113         break;
0114     }
0115 
0116     strOutput = QString::fromLatin1("Loaded %1 (%2)").arg(module).arg(symbolType);
0117 
0118     Q_EMIT DebugLine(strOutput);
0119 }