Пример #1
0
        internal void LoadMethodProperties(Exception exception, System.Diagnostics.StackFrame stackFrame)
        {
            // Validation
            if (exception == null || exception.TargetSite == null || typeof(System.Reflection.MethodInfo).IsAssignableFrom(exception.TargetSite.GetType()) == false)
            {
                return;
            }

            // Get Reflection Information
            System.Reflection.MethodBase methodBase     = stackFrame.GetMethod();
            System.Reflection.MethodBody methodBody     = methodBase.GetMethodBody();
            System.Reflection.MethodInfo methodInfo     = (methodBase != null && typeof(System.Reflection.MethodInfo).IsAssignableFrom(methodBase.GetType()) == true) ? (System.Reflection.MethodInfo)methodBase : null;
            System.Reflection.MethodInfo targetSiteInfo = ((System.Reflection.MethodInfo)exception.TargetSite);

            // Get Return Information
            this.m_ReturnParameter = (targetSiteInfo != null && targetSiteInfo.ReturnParameter != null) ? targetSiteInfo.Name : "";
            this.m_ReturnType      = (targetSiteInfo != null && targetSiteInfo.ReturnType != null) ? targetSiteInfo.ReturnType.FullName : "";

            // Get Line Number
            int intLineNumber = (GetLineNumber(exception) == 0) ? stackFrame.GetFileLineNumber() : GetLineNumber(exception);

            this.m_LineNumber = (this.LineNumber == 0) ? stackFrame.GetFileLineNumber() : intLineNumber;

            if (methodInfo != null && methodInfo.GetParameters().Count() > 0)
            {
                this.m_Parameters = methodInfo.GetParameters().Select(param => new ParameterInformation(param.Name, param.ParameterType.ToString())).ToList();
            }
        }
Пример #2
0
        internal void LoadClassFileInformation(Exception exception, System.Diagnostics.StackFrame stackFrame)
        {
            // Get Reflection Information
            System.Reflection.MethodBase methodBase     = stackFrame.GetMethod();
            System.Reflection.MethodBody methodBody     = methodBase.GetMethodBody();
            System.Reflection.MethodInfo methodInfo     = (methodBase != null && typeof(System.Reflection.MethodInfo).IsAssignableFrom(methodBase.GetType()) == true) ? (System.Reflection.MethodInfo)methodBase : null;
            System.Reflection.MethodInfo targetSiteInfo = ((System.Reflection.MethodInfo)exception.TargetSite);

            // Load Method Properties
            this.Method.LoadMethodProperties(exception, stackFrame);

            this.m_Name     = (methodInfo != null && methodInfo.ReflectedType != null) ? methodInfo.ReflectedType.Name : "";
            this.m_FullName = methodBase.ReflectedType.FullName + " - " + this.Method.Name;
        }
Пример #3
0
        private void LoadError()
        {
            // Get Application Information
            this.m_Application = new ApplicationInformation();

            // Loop Stack Trace Frames
            System.Diagnostics.StackTrace CurrentStackTest = new System.Diagnostics.StackTrace(this.Exception, 0, true);
            for (int intIndex = 0; intIndex < CurrentStackTest.FrameCount; intIndex++)
            {
                // Get Reflection Information
                System.Diagnostics.StackFrame frame      = (CurrentStackTest != null && intIndex >= 0) ? CurrentStackTest.GetFrame(intIndex) : null;
                System.Reflection.MethodBase  methodBase = (frame != null) ? frame.GetMethod() : null;
                System.Reflection.MethodBody  methodBody = (methodBase != null) ? methodBase.GetMethodBody() : null;
                System.Reflection.MethodInfo  info       = (methodBase != null && typeof(System.Reflection.MethodInfo).IsAssignableFrom(methodBase.GetType()) == true) ? info = (System.Reflection.MethodInfo)methodBase : null;

                //// Validation
                if (this.m_Class != null && this.m_Class.Method.Name != System.Reflection.MethodBase.GetCurrentMethod().Name)
                {
                    // SET ATTEMPTED METHOD
                    System.Diagnostics.StackFrame previousFrame = (intIndex - 1 >= 0) ? CurrentStackTest.GetFrame(intIndex - 1) : null;
                    System.Reflection.MethodBase  previousBase  = (previousFrame != null) ? ((System.Reflection.MethodBase)previousFrame.GetMethod()) : null;

                    string strMethod = (previousBase != null && typeof(System.Reflection.MethodInfo).IsAssignableFrom(previousBase.GetType()) == true) ? previousBase.DeclaringType.FullName + " - " + ((System.Reflection.MethodInfo)previousBase).ToString() : info.Name;
                    this.m_Class.AttemptedMethod = (intIndex < 2 && strMethod != this.m_Class.Method.Name) ? strMethod : this.m_Class.AttemptedMethod;
                }

                // Validation
                if (intIndex > 2)
                {
                    continue;
                }

                // LOAD ASSEMBLY PROPERTIES
                this.m_Assembly = new AssemblyInformation(info);

                // LOAD METHOD PROPERTIES
                this.m_Class = new ClassFileInformation(this.Exception, frame);
            }
        }
Пример #4
0
        public static ILgraph FromILbyteArray(System.Reflection.MethodBase MethodBase)
        {
            byte[] IL = MethodBase.GetMethodBody().GetILAsByteArray();

            ILgraph newGraph = new ILgraph();

            newGraph.targetOffsets.Add(0);

            //
            // Read IL instruction stream
            //

            for (int nextOffset, offset = 0; offset < IL.Length; offset = nextOffset)
            {
                CilInstruction newInst = CilInstructionReader.Read(IL, offset, out nextOffset, MethodBase);

                newGraph.targetOffsets.AddRange(newInst.BranchTargetOffsets);

                newGraph.ILinstList.Add(newInst);
            }

            // Verify that all jump targets are on instruction boundaries
            foreach (int offset in newGraph.targetOffsets)
            {
                if (newGraph.InstructionForOffset(offset) == null)
                {
                    throw new InvalidOperationException(string.Format("A jump instruction targets IL offset {0:X}, but it is not an instruction boundary.", offset));
                }
            }

            //
            // Convert instruction stream into basic blocks
            //

            {
                ILBB BB = null, FallThroughSource;
                newGraph.BBlist.Add(newGraph.BBentry = FallThroughSource = new ILBB());
                newGraph.BBlist.Add(newGraph.BBexit  = new ILBB());
                newGraph.BBlist.Add(newGraph.BBstart = FallThroughSource.FallThroughTarget = new ILBB());

                FallThroughSource = newGraph.BBstart;
                FallThroughSource.stackCountOnEntry = 0;

                foreach (CilInstruction inst in newGraph.ILinstList)
                {
                    if (BB == null || newGraph.targetOffsets.Contains(inst.Offset))
                    {
                        ILBB nextBB = new ILBB();
                        newGraph.BBlist.Add(nextBB);
                        nextBB.offset = inst.Offset;

                        if (newGraph.BBstart == null)
                        {
                            newGraph.BBstart         = nextBB;
                            nextBB.stackCountOnEntry = 0;
                        }

                        if (!newGraph.targetOffsets.Contains(nextBB.offset))
                        {
                            newGraph.targetOffsets.Add(nextBB.offset);
                        }

                        if (FallThroughSource != null)
                        {
                            FallThroughSource.FallThroughTarget = nextBB;
                        }

                        BB = nextBB;
                    }

                    BB.list.Add(inst);

                    switch (inst.Opcode.FlowControl)
                    {
                    default:
                    case System.Reflection.Emit.FlowControl.Meta:
                    case System.Reflection.Emit.FlowControl.Next:
                        FallThroughSource = BB;
                        break;

                    case System.Reflection.Emit.FlowControl.Cond_Branch:
                        BB.FinalTransfer  = inst;
                        FallThroughSource = BB;
                        BB = null;
                        break;

                    case System.Reflection.Emit.FlowControl.Branch:
                        BB.FinalTransfer  = inst;
                        FallThroughSource = null;
                        BB = null;
                        break;

                    case System.Reflection.Emit.FlowControl.Throw:
                        BB.FinalTransfer  = inst;
                        FallThroughSource = null;
                        BB = null;
                        break;

                    case System.Reflection.Emit.FlowControl.Return:
                        BB.FinalTransfer     = inst;
                        BB.FallThroughTarget = newGraph.BBexit;
                        FallThroughSource    = null;
                        BB = null;
                        break;
                    }
                }
            }

            System.Diagnostics.Debug.Assert(newGraph.BBlist[0].offset == 0);

            //
            // Check stack pointer
            //

            bool Changed;

            do
            {
                Changed = false;
                foreach (ILBB BB in newGraph.BBlist)
                {
                    if (!BB.stackCountOnEntry.HasValue || BB.stackCountOnExit.HasValue)
                    {
                        continue;
                    }

                    int CurStack = BB.stackCountOnEntry.Value;
                    for (int i = 0; i < BB.list.Count; i++)
                    {
                        CilInstruction inst = BB.list[i];

                        int consume = inst.StackConsumeCount;
                        int produce = inst.StackProduceCount;

                        if (consume > CurStack)
                        {
                            throw new InvalidOperationException(string.Format("BB[{0:X}]INST[{1}] @ IL offset {2:X}: stack overconsumption. avail = {3}, consume = {4}", BB.offset, i, inst.Offset, CurStack, consume));
                        }
                        CurStack = CurStack - consume + produce;
                    }
                    BB.stackCountOnExit = CurStack;

                    if (BB.FallThroughTarget != null)
                    {
                        if (!BB.FallThroughTarget.stackCountOnEntry.HasValue)
                        {
                            BB.FallThroughTarget.stackCountOnEntry = CurStack;
                            Changed = true;
                        }
                        else if (BB.FallThroughTarget.stackCountOnEntry.Value != CurStack)
                        {
                            throw new InvalidOperationException(string.Format("BB[{0:X}]: invalid stack count merge. new = {1}, old = {2}", BB.FallThroughTarget.offset, CurStack, BB.FallThroughTarget.stackCountOnEntry.Value));
                        }
                    }

                    if (BB.FinalTransfer != null)
                    {
                        foreach (int targetOffset in BB.FinalTransfer.BranchTargetOffsets)
                        {
                            ILBB targetBB = newGraph.BBForEntryOffset(targetOffset);
                            System.Diagnostics.Debug.Assert(targetBB != null);

                            if (!targetBB.stackCountOnEntry.HasValue)
                            {
                                targetBB.stackCountOnEntry = CurStack;
                                Changed = true;
                            }
                            else if (targetBB.stackCountOnEntry.Value != CurStack)
                            {
                                throw new InvalidOperationException(string.Format("BB[{0:X}]: invalid stack count merge. new = {1}, old = {2}", targetBB.offset, CurStack, targetBB.stackCountOnEntry.Value));
                            }
                        }
                    }
                }
            } while (Changed);

            return(newGraph);
        }