private List <Instruction> CreateTraceReturnWithExceptionLoggingInstructions(VariableDefinition exceptionValue) { var instructions = new List <Instruction>(); VariableDefinition paramNamesDef = null; VariableDefinition paramValuesDef = null; paramNamesDef = MoveNextParamNamesVariable; paramValuesDef = MoveNextParamValuesVariable; instructions.AddRange(InitArray(paramNamesDef, 1, _typeReferenceProvider.String)); instructions.AddRange(InitArray(paramValuesDef, 1, _typeReferenceProvider.Object)); instructions.AddRange(StoreValueReadByInstructionsInArray(paramNamesDef, 0, Instruction.Create(OpCodes.Ldstr, ExceptionMarker))); instructions.AddRange(StoreVariableInObjectArray(paramValuesDef, 0, exceptionValue)); //build up Trace call instructions.Add(Instruction.Create(OpCodes.Ldsfld, TypeWeaver.CreateLoggerStaticField(_typeReferenceProvider, _methodReferenceProvider, _generatedType))); instructions.AddRange(LoadMethodNameOnStack()); //start ticks instructions.Add(Instruction.Create(OpCodes.Ldarg_0)); instructions.Add(Instruction.Create(OpCodes.Ldfld, _tickFieldRef)); //end ticks instructions.Add(Instruction.Create(OpCodes.Call, _methodReferenceProvider.GetTimestampReference())); instructions.Add(Instruction.Create(OpCodes.Ldloc, paramNamesDef)); instructions.Add(Instruction.Create(OpCodes.Ldloc, paramValuesDef)); instructions.Add(Instruction.Create(OpCodes.Callvirt, _methodReferenceProvider.GetTraceLeaveReference())); return(instructions); }
private void ChangeStaticLogCallWithoutParameter(Instruction oldInstruction) { var instructions = new List <Instruction>(); var methodReference = (MethodReference)oldInstruction.Operand; var methodReferenceInfo = new MethodReferenceInfo(methodReference); instructions.Add(Instruction.Create(OpCodes.Ldsfld, TypeWeaver.CreateLoggerStaticField(_typeReferenceProvider, _methodReferenceProvider, _generatedType))); if (!methodReferenceInfo.IsPropertyAccessor()) { instructions.AddRange(LoadMethodNameOnStack()); } instructions.Add(Instruction.Create(OpCodes.Callvirt, _methodReferenceProvider.GetInstanceLogMethod(methodReferenceInfo))); _moveNextBody.Replace(oldInstruction, instructions); }
private void ChangeStaticLogCallWithParameter(Instruction oldInstruction) { var instructions = new List <Instruction>(); var methodReference = (MethodReference)oldInstruction.Operand; var methodReferenceInfo = new MethodReferenceInfo(methodReference); if (methodReferenceInfo.IsPropertyAccessor() && methodReferenceInfo.IsSetter) { throw new Exception("Rewriting static property setters is not supported."); } var parameters = methodReference.Parameters; //create variables to store parameters and push values into them var variables = new VariableDefinition[parameters.Count]; for (int idx = 0; idx < parameters.Count; idx++) { variables[idx] = GetVariableDefinitionForType(parameters[idx].ParameterType, methodReference, _moveNextDefinition); _moveNextBody.Variables.Add(variables[idx]); } //store in reverse order for (int idx = parameters.Count - 1; idx >= 0; idx--) { instructions.Add(Instruction.Create(OpCodes.Stloc, variables[idx])); } //build-up instance call instructions.Add(Instruction.Create(OpCodes.Ldsfld, TypeWeaver.CreateLoggerStaticField(_typeReferenceProvider, _methodReferenceProvider, _generatedType))); instructions.AddRange(LoadMethodNameOnStack()); for (int idx = 0; idx < parameters.Count; idx++) { instructions.Add(Instruction.Create(OpCodes.Ldloc, variables[idx])); } instructions.Add(Instruction.Create(OpCodes.Callvirt, _methodReferenceProvider.GetInstanceLogMethod(methodReferenceInfo, parameters))); _moveNextBody.Replace(oldInstruction, instructions); }
private List <Instruction> CreateTraceReturnLoggingInstructions(VariableDefinition returnValueDef) { var instructions = new List <Instruction>(); VariableDefinition paramNamesDef = null; VariableDefinition paramValuesDef = null; bool hasReturnValue = HasReturnValue; if (hasReturnValue) { //Get local variables for the arrays or declare them if they not exist paramNamesDef = MoveNextParamNamesVariable; paramValuesDef = MoveNextParamValuesVariable; //init arrays instructions.AddRange(InitArray(paramNamesDef, 1, _typeReferenceProvider.String)); instructions.AddRange(InitArray(paramValuesDef, 1, _typeReferenceProvider.Object)); instructions.AddRange(StoreValueReadByInstructionsInArray(paramNamesDef, 0, Instruction.Create(OpCodes.Ldnull))); instructions.AddRange(StoreVariableInObjectArray(paramValuesDef, 0, returnValueDef)); } //build up Trace call instructions.Add(Instruction.Create(OpCodes.Ldsfld, TypeWeaver.CreateLoggerStaticField(_typeReferenceProvider, _methodReferenceProvider, _generatedType))); instructions.AddRange(LoadMethodNameOnStack()); //start ticks instructions.Add(Instruction.Create(OpCodes.Ldarg_0)); instructions.Add(Instruction.Create(OpCodes.Ldfld, _tickFieldRef)); //end ticks instructions.Add(Instruction.Create(OpCodes.Call, _methodReferenceProvider.GetTimestampReference())); instructions.Add(hasReturnValue ? Instruction.Create(OpCodes.Ldloc, paramNamesDef) : Instruction.Create(OpCodes.Ldnull)); instructions.Add(hasReturnValue ? Instruction.Create(OpCodes.Ldloc, paramValuesDef) : Instruction.Create(OpCodes.Ldnull)); instructions.Add(Instruction.Create(OpCodes.Callvirt, _methodReferenceProvider.GetTraceLeaveReference())); return(instructions); }