Repository navigation
Expand file tree
/
Copy pathMethodProcessor.cs
More file actions
310 lines (258 loc) · 12.4 KB
/
Copy pathMethodProcessor.cs
File metadata and controls
310 lines (258 loc) · 12.4 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
using System;
using System.Collections.Generic;
using System.Linq;
using Fody;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Cecil.Rocks;
public partial class MethodProcessor
{
public ModuleWeaver ModuleWeaver;
public MethodDefinition Method;
MethodBody body;
VariableDefinition startTimestampVar;
VariableDefinition endTimestampVar;
VariableDefinition durationTimestampVar;
VariableDefinition durationTimespanVar;
ParameterFormattingProcessor parameterFormattingProcessor = new();
public void Process()
{
try
{
InnerProcess();
}
catch (Exception exception)
{
throw new WeavingException($"An error occurred processing '{Method.FullName}'. Error: {exception.Message}");
}
}
void InnerProcess()
{
body = Method.Body;
body.SimplifyMacros();
var firstInstruction = FirstInstructionSkipCtor();
var indexOf = body.Instructions.IndexOf(firstInstruction);
var returnInstruction = FixReturns();
startTimestampVar = InjectStartTimestamp(body, indexOf);
endTimestampVar = InjectEndTimestamp(body);
durationTimestampVar = InjectDurationTimestamp(body);
durationTimespanVar = InjectDurationTimespan(body);
var beforeReturn = Instruction.Create(OpCodes.Nop);
body.InsertBefore(returnInstruction, beforeReturn);
InjectIlForFinally(returnInstruction);
var handler = new ExceptionHandler(ExceptionHandlerType.Finally)
{
TryStart = firstInstruction,
TryEnd = beforeReturn,
HandlerStart = beforeReturn,
HandlerEnd = returnInstruction,
};
body.ExceptionHandlers.Add(handler);
body.InitLocals = true;
body.OptimizeMacros();
}
Instruction FirstInstructionSkipCtor()
{
if (Method.IsInstanceConstructor())
{
foreach (var instruction in body.Instructions)
{
if (instruction.OpCode != OpCodes.Call)
{
continue;
}
if (instruction.Operand is not MethodReference methodReference)
{
continue;
}
if (methodReference.Name != ".ctor")
{
continue;
}
if (methodReference.DeclaringType != Method.DeclaringType.BaseType)
{
continue;
}
return instruction.Next;
}
}
return body.Instructions.First();
}
Instruction FixReturns()
{
var instructions = body.Instructions;
if (Method.ReturnType.FullName == ModuleWeaver.TypeSystem.VoidReference.FullName)
{
var lastRet = Instruction.Create(OpCodes.Ret);
foreach (var instruction in instructions)
{
if (instruction.OpCode == OpCodes.Ret)
{
instruction.OpCode = OpCodes.Leave;
instruction.Operand = lastRet;
}
}
instructions.Add(lastRet);
return lastRet;
}
var returnVariable = new VariableDefinition(Method.ReturnType);
body.Variables.Add(returnVariable);
var lastLd = Instruction.Create(OpCodes.Ldloc, returnVariable);
for (var index = 0; index < instructions.Count; index++)
{
var instruction = instructions[index];
if (instruction.OpCode == OpCodes.Ret)
{
instruction.OpCode = OpCodes.Stloc;
instruction.Operand = returnVariable;
index++;
instructions.Insert(index, Instruction.Create(OpCodes.Leave, lastLd));
}
}
instructions.Add(lastLd);
instructions.Add(Instruction.Create(OpCodes.Ret));
return lastLd;
}
void InjectIlForFinally(Instruction beforeThis)
{
foreach (var instruction in GetWriteTimeInstruction(startTimestampVar, endTimestampVar,
durationTimestampVar, durationTimespanVar, Method))
{
body.InsertBefore(beforeThis, instruction);
}
body.InsertBefore(beforeThis, Instruction.Create(OpCodes.Endfinally));
}
public IEnumerable<Instruction> GetWriteTimeInstruction(VariableDefinition startTimestampVar,
VariableDefinition endTimestampVar, VariableDefinition durationTimestampVar,
VariableDefinition durationTimespanVar, MethodDefinition methodDefinition)
{
// endTimestamp = Stopwatch.GetTimestamp();
yield return Instruction.Create(OpCodes.Call, ModuleWeaver.Stopwatch_GetTimestampMethod);
yield return Instruction.Create(OpCodes.Stloc, endTimestampVar);
// durationTimestamp = endTimestampVar - startTimestampVar;
yield return Instruction.Create(OpCodes.Ldloc, endTimestampVar);
yield return Instruction.Create(OpCodes.Ldloc, startTimestampVar);
yield return Instruction.Create(OpCodes.Sub);
yield return Instruction.Create(OpCodes.Stloc, durationTimestampVar);
// durationTimespanVar = new TimeSpan((long)(MethodTimerHelper.TimestampToTicks * (double)durationTimestamp));
yield return Instruction.Create(OpCodes.Ldloca, durationTimespanVar);
yield return Instruction.Create(OpCodes.Ldsfld, ModuleWeaver.MethodTimerHelper_TimestampToTicks);
yield return Instruction.Create(OpCodes.Ldloc, durationTimestampVar);
yield return Instruction.Create(OpCodes.Conv_R8);
yield return Instruction.Create(OpCodes.Mul);
yield return Instruction.Create(OpCodes.Conv_I8);
yield return Instruction.Create(OpCodes.Call, ModuleWeaver.TimeSpan_ConstructorMethod);
var logWithMessageMethodUsingLong = ModuleWeaver.LogWithMessageMethodUsingLong;
var logWithMessageMethodUsingTimeSpan = ModuleWeaver.LogWithMessageMethodUsingTimeSpan;
var logMethodUsingLong = ModuleWeaver.LogMethodUsingLong;
var logMethodUsingTimeSpan = ModuleWeaver.LogMethodUsingTimeSpan;
if (logWithMessageMethodUsingLong is null && logWithMessageMethodUsingTimeSpan is null)
{
if (logMethodUsingLong is null && logMethodUsingTimeSpan is null)
{
// var elapsedMillisecondsVariable = (long)durationTimespanVar.TotalMilliseconds;
// Trace.WriteLine(string.Concat(methodName, elapsedMillisecondsVariable.ToString(), "ms"))
var elapsedMillisecondsVariable = new VariableDefinition(ModuleWeaver.TypeSystem.Int64Reference);
methodDefinition.Body.Variables.Add(elapsedMillisecondsVariable);
yield return Instruction.Create(OpCodes.Ldstr, methodDefinition.MethodName());
yield return Instruction.Create(OpCodes.Ldloca, durationTimespanVar);
yield return Instruction.Create(OpCodes.Call, ModuleWeaver.TimeSpan_TotalMillisecondsMethod);
yield return Instruction.Create(OpCodes.Conv_I8);
yield return Instruction.Create(OpCodes.Stloc, elapsedMillisecondsVariable);
yield return Instruction.Create(OpCodes.Ldloca, elapsedMillisecondsVariable);
yield return Instruction.Create(OpCodes.Call, ModuleWeaver.Int64ToString);
yield return Instruction.Create(OpCodes.Ldstr, "ms");
yield return Instruction.Create(OpCodes.Call, ModuleWeaver.ConcatMethod);
yield return Instruction.Create(OpCodes.Call, ModuleWeaver.TraceWriteLineMethod);
yield break;
}
// push the MethodBase
yield return Instruction.Create(OpCodes.Ldtoken, methodDefinition);
yield return Instruction.Create(OpCodes.Ldtoken, methodDefinition.DeclaringType);
yield return Instruction.Create(OpCodes.Call, ModuleWeaver.GetMethodFromHandle);
if (logMethodUsingTimeSpan is not null)
{
// logMethod(MethodBase, TimeSpan)
yield return Instruction.Create(OpCodes.Ldloc, durationTimespanVar);
yield return Instruction.Create(OpCodes.Call, logMethodUsingTimeSpan);
yield break;
}
// logMethod(MethodBase, long)
yield return Instruction.Create(OpCodes.Ldloca, durationTimespanVar);
yield return Instruction.Create(OpCodes.Call, ModuleWeaver.TimeSpan_TotalMillisecondsMethod);
yield return Instruction.Create(OpCodes.Conv_I8);
yield return Instruction.Create(OpCodes.Call, logMethodUsingLong);
yield break;
}
var formattedVariableDefinition = new VariableDefinition(ModuleWeaver.TypeSystem.StringReference);
methodDefinition.Body.Variables.Add(formattedVariableDefinition);
// formattedVariableDefinition = <message to log>
foreach (var instruction in ProcessTimeAttribute(methodDefinition, formattedVariableDefinition))
{
yield return instruction;
}
// push the MethodBase
yield return Instruction.Create(OpCodes.Ldtoken, methodDefinition);
yield return Instruction.Create(OpCodes.Ldtoken, methodDefinition.DeclaringType);
yield return Instruction.Create(OpCodes.Call, ModuleWeaver.GetMethodFromHandle);
if (logWithMessageMethodUsingTimeSpan is null)
{
// logMethod(MethodBase, long, string)
yield return Instruction.Create(OpCodes.Ldloca, durationTimespanVar);
yield return Instruction.Create(OpCodes.Call, ModuleWeaver.TimeSpan_TotalMillisecondsMethod);
yield return Instruction.Create(OpCodes.Conv_I8);
yield return Instruction.Create(OpCodes.Ldloc, formattedVariableDefinition);
yield return Instruction.Create(OpCodes.Call, logWithMessageMethodUsingLong);
yield break;
}
// logMethod(MethodBase, TimeSpan, string)
yield return Instruction.Create(OpCodes.Ldloc, durationTimespanVar);
yield return Instruction.Create(OpCodes.Ldloc, formattedVariableDefinition);
yield return Instruction.Create(OpCodes.Call, logWithMessageMethodUsingTimeSpan);
}
IEnumerable<Instruction> ProcessTimeAttribute(MethodDefinition methodDefinition, VariableDefinition formattedVariableDefinition)
{
// Load everything for a string format
var timeAttribute = methodDefinition.GetTimeAttribute();
if (timeAttribute == null)
{
yield break;
}
var value = timeAttribute.ConstructorArguments.FirstOrDefault().Value as string;
if (string.IsNullOrWhiteSpace(value))
{
// Load a null string
yield return Instruction.Create(OpCodes.Ldnull);
}
else
{
var info = parameterFormattingProcessor.ParseParameterFormatting(value);
yield return Instruction.Create(OpCodes.Ldstr, info.Format);
yield return Instruction.Create(OpCodes.Ldc_I4, info.ParameterNames.Count);
yield return Instruction.Create(OpCodes.Newarr, ModuleWeaver.TypeSystem.ObjectReference);
for (var i = 0; i < info.ParameterNames.Count; i++)
{
// Note: no need to validate, already done in AssemblyProcessor::ProcessMethod
var parameterName = info.ParameterNames[i];
yield return Instruction.Create(OpCodes.Dup);
yield return Instruction.Create(OpCodes.Ldc_I4, i);
if (string.Equals(parameterName, "this"))
{
yield return Instruction.Create(OpCodes.Ldarg_0);
}
else
{
var parameter = methodDefinition.Parameters.First(p => p.Name.Equals(parameterName));
yield return Instruction.Create(OpCodes.Ldarg, parameter);
if (parameter.ParameterType.IsBoxingRequired(ModuleWeaver.TypeSystem.ObjectReference))
{
yield return Instruction.Create(OpCodes.Box, ModuleWeaver.ModuleDefinition.ImportReference(parameter.ParameterType));
}
}
yield return Instruction.Create(OpCodes.Stelem_Ref);
}
yield return Instruction.Create(OpCodes.Call, ModuleWeaver.StringFormatWithArray);
}
yield return Instruction.Create(OpCodes.Stloc, formattedVariableDefinition);
}
}