예제 #1
0
        private void Inject(CodeMethod method)
        {
            Logger.Current.Debug("Method >> " + method.MethodDefinition.Name);

            if (!ValidMethod(method))
            {
                return;
            }

            method.MethodDefinition.Body.Simplify();

            AddStartMethodStatement(method, method.MethodDefinition.Body.Instructions[0], "Begin");

            foreach (CodeInstruction ins in method.Instructions)
            {
                if (ins.Instruction.OpCode.Equals(OpCodes.Ret))
                {
                    AddEndMethodStatement(method, ins.Instruction, "End");
                }
            }

//            int instructionCount = method.MethodDefinition.Body.Instructions.Count;
//            AddEndMethodStatement(method, method.MethodDefinition.Body.Instructions[instructionCount - 2], "End");

            method.MethodDefinition.Body.Optimize();
        }
예제 #2
0
        private void InsertStatement(CodeMethod method, Instruction lastInstruction, Instruction endSentence)
        {
            Instruction callLoggerDebug = method.MethodDefinition.Body.CilWorker.Create(OpCodes.Call, importedTraceMethod);

            method.MethodDefinition.Body.CilWorker.InsertBefore(lastInstruction, endSentence);
            method.MethodDefinition.Body.CilWorker.InsertAfter(endSentence, callLoggerDebug);
            method.MethodDefinition.Body.CilWorker.InsertAfter(callLoggerDebug, method.MethodDefinition.Body.CilWorker.Create(OpCodes.Nop));
        }
예제 #3
0
        private void AddEndMethodStatement(CodeMethod method, Instruction instruction, string prefix)
        {
            Instruction endSentence = method.MethodDefinition.Body.CilWorker.Create(OpCodes.Ldstr,
                                                                                    prefix + "-" +
                                                                                    method.MethodDefinition.
                                                                                    DeclaringType.FullName + "-" +
                                                                                    method.MethodDefinition.Name);

            InsertStatement(method, instruction, endSentence);
        }
예제 #4
0
        private static bool ValidMethod(CodeMethod method)
        {
            if (null == method.MethodDefinition.Body)
            {
                return(false);
            }

            foreach (CustomAttribute customAttribute in method.MethodDefinition.CustomAttributes)
            {
                // TODO: Make following configurable
                if (Equals(customAttribute.Constructor.DeclaringType.FullName,
                           "System.Diagnostics.DebuggerNonUserCodeAttribute") ||
                    Equals(customAttribute.Constructor.DeclaringType.FullName,
                           "System.Runtime.CompilerServices.CompilerGeneratedAttribute"))
                {
                    return(false);
                }
            }

            return(true);
        }