Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
zodiacon committed Sep 4, 2022
1 parent 43fd350 commit 0486803
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 115 deletions.
111 changes: 86 additions & 25 deletions DiaHelper/DiaHelper.h
Original file line number Diff line number Diff line change
@@ -1,38 +1,99 @@
#pragma once

#include "dia2.h"
#include "DiaSymbol.h"
#include <optional>
#include <vector>
#include <string>

enum class CompareOptions {
None = 0,
CaseSensitive = 0x1,
CaseInsensitive = 0x2,
FNameExt = 0x4,
RegularExpression = 0x8,
UndecoratedName = 0x10,
CaseInRegularExpression = (RegularExpression | CaseInsensitive)
None = 0,
CaseSensitive = 0x1,
CaseInsensitive = 0x2,
FNameExt = 0x4,
RegularExpression = 0x8,
UndecoratedName = 0x10,
CaseInRegularExpression = (RegularExpression | CaseInsensitive)
};
DEFINE_ENUM_FLAG_OPERATORS(CompareOptions);

class DiaSession {
public:
bool OpenImage(PCWSTR path);
bool OpenPdb(PCWSTR path);
void Close();
std::wstring LastError() const;
enum class SymbolTag {
Null,
Exe,
Compiland,
CompilandDetails,
CompilandEnv,
Function,
Block,
Data,
Annotation,
Label,
PublicSymbol,
UDT,
Enum,
FunctionType,
PointerType,
ArrayType,
BaseType,
Typedef,
BaseClass,
Friend,
FunctionArgType,
FuncDebugStart,
FuncDebugEnd,
UsingNamespace,
VTableShape,
VTable,
Custom,
Thunk,
CustomType,
ManagedType,
Dimension,
CallSite,
InlineSite,
BaseInterface,
VectorType,
MatrixType,
HLSLType,
Caller,
Callee,
Export,
HeapAllocationSite,
CoffGroup,
Inlinee,
Max
};

DiaSymbol GlobalScope() const;
std::vector<DiaSymbol> FindChildren(DiaSymbol const& parent, SymbolTag tag = SymbolTag::Null, PCWSTR name = nullptr,
CompareOptions options = CompareOptions::None) const;
enum class AccessMode {
Private = 1,
Protected,
Public
};

private:
bool OpenCommon(PCWSTR path, bool image);
enum class LocationKind {
Null,
Static,
TLS,
RegRel,
ThisRel,
Enregistered,
BitField,
Slot,
IlRel,
MetaData,
Constant,
RegRelAliasIndir,
Max
};

private:
CComPtr<IDiaSession> m_spSession;
CComPtr<IDiaDataSource> m_spSource;
enum class DataItemKind {
Unknown,
Local,
StaticLocal,
Param,
ObjectPtr,
FileStatic,
Global,
Member,
StaticMember,
Constant
};

#include "DiaSession.h"
#include "DiaSymbol.h"
3 changes: 2 additions & 1 deletion DiaHelper/DiaHelper.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,12 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="DiaHelper.h" />
<ClInclude Include="DiaSession.h" />
<ClInclude Include="DiaSymbol.h" />
<ClInclude Include="pch.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="DiaHelper.cpp" />
<ClCompile Include="DiaSession.cpp" />
<ClCompile Include="DiaSymbol.cpp" />
<ClCompile Include="pch.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
Expand Down
7 changes: 5 additions & 2 deletions DiaHelper/DiaHelper.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@
<ClInclude Include="pch.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="DiaHelper.h">
<ClInclude Include="DiaSession.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="DiaSymbol.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="DiaHelper.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="DiaHelper.cpp">
<ClCompile Include="DiaSession.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="pch.cpp">
Expand Down
File renamed without changes.
26 changes: 26 additions & 0 deletions DiaHelper/DiaSession.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include "DiaSymbol.h"

#include <vector>
#include <string>

class DiaSession {
public:
bool OpenImage(PCWSTR path);
bool OpenPdb(PCWSTR path);
void Close();
std::wstring LastError() const;

DiaSymbol GlobalScope() const;
std::vector<DiaSymbol> FindChildren(DiaSymbol const& parent, SymbolTag tag = SymbolTag::Null, PCWSTR name = nullptr,
CompareOptions options = CompareOptions::None) const;

private:
bool OpenCommon(PCWSTR path, bool image);

private:
CComPtr<IDiaSession> m_spSession;
CComPtr<IDiaDataSource> m_spSource;
};

45 changes: 44 additions & 1 deletion DiaHelper/DiaSymbol.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "pch.h"
#include "DiaSymbol.h"
#include "DiaHelper.h"

DiaSymbol::DiaSymbol(IDiaSymbol* sym) : m_spSym(sym) {
}
Expand All @@ -25,12 +25,36 @@ uint32_t DiaSymbol::Id() const {
return id;
}

uint32_t DiaSymbol::Age() const {
uint32_t age = 0;
m_spSym->get_age((DWORD*)&age);
return age;
}

int32_t DiaSymbol::Offset() const {
int32_t offset;
m_spSym->get_offset((LONG*)&offset);
return offset;
}

AccessMode DiaSymbol::Access() const {
DWORD access = 0;
m_spSym->get_access(&access);
return AccessMode(access);
}

DiaSymbol DiaSymbol::ClassParent() const {
CComPtr<IDiaSymbol> sym;
m_spSym->get_classParent(&sym);
return DiaSymbol(sym);
}

DiaSymbol DiaSymbol::LexicalParent() const {
CComPtr<IDiaSymbol> sym;
m_spSym->get_lexicalParent(&sym);
return DiaSymbol(sym);
}

DiaSymbol DiaSymbol::Type() const {
CComPtr<IDiaSymbol> sym;
m_spSym->get_type(&sym);
Expand All @@ -54,3 +78,22 @@ DataItemKind DiaSymbol::Kind() const {
m_spSym->get_dataKind(&kind);
return static_cast<DataItemKind>(kind);
}

std::vector<DiaSymbol> DiaSymbol::FindChildren(SymbolTag tag, PCWSTR name, CompareOptions options) const {
std::vector<DiaSymbol> symbols;
CComPtr<IDiaEnumSymbols> spEnum;
if (SUCCEEDED(m_spSym->findChildren((enum SymTagEnum)tag, name, (DWORD)options, &spEnum))) {
LONG count = 0;
spEnum->get_Count(&count);
ULONG ret;
for (LONG i = 0; i < count; i++) {
CComPtr<IDiaSymbol> sym;
spEnum->Next(1, &sym, &ret);
ATLASSERT(sym);
if (sym == nullptr)
break;
symbols.push_back(DiaSymbol(sym));
}
}
return symbols;
}
85 changes: 3 additions & 82 deletions DiaHelper/DiaSymbol.h
Original file line number Diff line number Diff line change
@@ -1,91 +1,10 @@
#pragma once

#include <string>
#include <vector>

struct IDiaSymbol;

enum class SymbolTag {
Null,
Exe,
Compiland,
CompilandDetails,
CompilandEnv,
Function,
Block,
Data,
Annotation,
Label,
PublicSymbol,
UDT,
Enum,
FunctionType,
PointerType,
ArrayType,
BaseType,
Typedef,
BaseClass,
Friend,
FunctionArgType,
FuncDebugStart,
FuncDebugEnd,
UsingNamespace,
VTableShape,
VTable,
Custom,
Thunk,
CustomType,
ManagedType,
Dimension,
CallSite,
InlineSite,
BaseInterface,
VectorType,
MatrixType,
HLSLType,
Caller,
Callee,
Export,
HeapAllocationSite,
CoffGroup,
Inlinee,
Max
};

enum class AccessMode {
Private = 1,
Protected,
Public
};

enum class LocationKind {
Null,
Static,
TLS,
RegRel,
ThisRel,
Enregistered,
BitField,
Slot,
IlRel,
MetaData,
Constant,
RegRelAliasIndir,
Max
};

enum class DataItemKind {
Unknown,
Local,
StaticLocal,
Param,
ObjectPtr,
FileStatic,
Global,
Member,
StaticMember,
Constant
};

class DiaSymbol {
friend class DiaSession;
public:
Expand All @@ -103,6 +22,8 @@ class DiaSymbol {
LocationKind Location() const;
SymbolTag Tag() const;
DataItemKind Kind() const;
std::vector<DiaSymbol> FindChildren(SymbolTag tag = SymbolTag::Null, PCWSTR name = nullptr,
CompareOptions options = CompareOptions::None) const;

private:
DiaSymbol(IDiaSymbol* sym);
Expand Down
2 changes: 1 addition & 1 deletion DiaHelper/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
#include <atlcomcli.h>
#include "Dia2.h"
#include <string>
#include <optional>
#include <vector>

#endif //PCH_H
5 changes: 2 additions & 3 deletions TotalPE/MainFrm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -675,9 +675,8 @@ DiaSession const& CMainFrame::GetSymbols() const {
}

LRESULT CMainFrame::OnShowWindow(UINT, WPARAM show, LPARAM, BOOL&) {
static bool shown = false;
if (show && !shown) {
shown = true;
if (show && !m_Shown) {
m_Shown = true;
auto wp = s_settings.MainWindowPlacement();
if (wp.showCmd != SW_HIDE) {
SetWindowPlacement(&wp);
Expand Down
1 change: 1 addition & 0 deletions TotalPE/MainFrm.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,6 @@ class CMainFrame :
inline static RecentFilesManager s_recentFiles;
inline static AppSettings s_settings;
DiaSession m_Symbols;
bool m_Shown{ false };
};

0 comments on commit 0486803

Please sign in to comment.