forked from markhc/ReClass.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymbolReader.cs
More file actions
204 lines (172 loc) · 4.51 KB
/
Copy pathSymbolReader.cs
File metadata and controls
204 lines (172 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System;
using System.Diagnostics.Contracts;
using System.Text;
using Dia2Lib;
using ReClassNET.Memory;
using ReClassNET.Util;
namespace ReClassNET.Symbols
{
public class SymbolReader : IDisposable
{
private ComDisposableWrapper<DiaSource> diaSource;
private ComDisposableWrapper<IDiaSession> diaSession;
private readonly string searchPath;
public SymbolReader(string searchPath)
{
diaSource = new ComDisposableWrapper<DiaSource>(new DiaSource());
this.searchPath = searchPath;
}
protected virtual void Dispose(bool disposing)
{
if (diaSource != null)
{
diaSource.Dispose();
diaSource = null;
if (diaSession != null)
{
diaSession.Dispose();
diaSession = null;
}
}
}
~SymbolReader()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public static void TryResolveSymbolsForModule(RemoteProcess.Module module, string searchPath)
{
Contract.Requires(module != null);
using (var diaSource = new ComDisposableWrapper<DiaSource>(new DiaSource()))
{
diaSource.Interface.loadDataForExe(module.Path, searchPath, null);
}
}
public static SymbolReader FromModule(RemoteProcess.Module module, string searchPath)
{
Contract.Requires(module != null);
var reader = new SymbolReader(searchPath);
reader.diaSource.Interface.loadDataForExe(module.Path, searchPath, null);
reader.CreateSession();
return reader;
}
public static SymbolReader FromDatabase(string path)
{
Contract.Requires(path != null);
var reader = new SymbolReader(null);
reader.diaSource.Interface.loadDataFromPdb(path);
reader.CreateSession();
return reader;
}
private void CreateSession()
{
IDiaSession session;
diaSource.Interface.openSession(out session);
diaSession = new ComDisposableWrapper<IDiaSession>(session);
}
public string GetSymbolString(IntPtr address, RemoteProcess.Module module)
{
Contract.Requires(module != null);
var rva = address.Sub(module.Start);
IDiaSymbol diaSymbol;
diaSession.Interface.findSymbolByRVA((uint)rva.ToInt32(), SymTagEnum.SymTagNull, out diaSymbol);
if (diaSymbol != null)
{
using (var symbol = new ComDisposableWrapper<IDiaSymbol>(diaSymbol))
{
var sb = new StringBuilder();
ReadSymbol(diaSymbol, sb);
return sb.ToString();
}
}
return null;
}
private void ReadSymbol(IDiaSymbol symbol, StringBuilder sb)
{
Contract.Requires(symbol != null);
Contract.Requires(sb != null);
/*switch ((SymTagEnum)symbol.symTag)
{
case SymTagEnum.SymTagData:
ReadData(symbol, sb);
break;
case SymTagEnum.SymTagFunction:
sb.Append(symbol.callingConvention.ToString());
ReadName(symbol, sb);
break;
case SymTagEnum.SymTagBlock:
sb.AppendFormat("len({0:X08}) ", symbol.length);
ReadName(symbol, sb);
break;
}
ReadSymbolType(symbol, sb);*/
ReadName(symbol, sb);
}
private void ReadSymbolType(IDiaSymbol symbol, StringBuilder sb)
{
Contract.Requires(symbol != null);
Contract.Requires(sb != null);
if (symbol.type != null)
{
using (var type = new ComDisposableWrapper<IDiaSymbol>(symbol.type))
{
ReadType(type.Interface, sb);
}
}
}
private void ReadType(IDiaSymbol symbol, StringBuilder sb)
{
Contract.Requires(symbol != null);
Contract.Requires(sb != null);
throw new NotImplementedException();
}
private void ReadName(IDiaSymbol symbol, StringBuilder sb)
{
Contract.Requires(symbol != null);
Contract.Requires(sb != null);
if (string.IsNullOrEmpty(symbol.name))
{
return;
}
if (!string.IsNullOrEmpty(symbol.undecoratedName))
{
// If symbol.name equals symbol.undecoratedName there is some extra stuff which can't get undecorated. Try to fix it.
if (symbol.name == symbol.undecoratedName)
{
var name = symbol.name;
if (name.StartsWith("@ILT+"))
{
var start = name.IndexOf('(');
if (start != -1)
{
name = name.Substring(start + 1, name.Length - 1 - start - 1);
}
}
else if (!name.StartsWith("?"))
{
name = '?' + name;
}
sb.Append(NativeMethods.UnDecorateSymbolName(name).TrimStart('?', ' '));
}
else
{
sb.Append(symbol.undecoratedName);
}
}
else
{
sb.Append(symbol.name);
}
}
private void ReadData(IDiaSymbol symbol, StringBuilder sb)
{
Contract.Requires(symbol != null);
Contract.Requires(sb != null);
throw new NotImplementedException();
}
}
}