Beispiel #1
0
            //Runtime.GetMethodByAddress((ulong)src.MethodHandle.GetFunctionPointer());

        Option<MethodDisassembly> Disassemble(MethodInfo method, Action<string> onError)
        {
            try
            {
                var clrMethod = GetRuntimeMethod(method);
                if(clrMethod == null || clrMethod.NativeCode == 0)
                {
                    onError($"Method {method.Name} not found");
                    return null;
                }
                
                var asmBody = DecodeAsm(method);
                var ilBytes = ReadCilBytes(clrMethod);            
                Claim.eq(ilBytes, method.GetMethodBody().GetILAsByteArray()) ;
                        
                var d = new MethodDisassembly
                {
                    MethodInfo = method,
                    NativeAddress = clrMethod.NativeCode,
                    MethodSig = method.MethodSig(),
                    CilData = ilBytes,
                    CilBody = MdIx.FindCil(method),
                    CilMap = MapCilToNative(clrMethod),
                    AsmBody =  asmBody,
                    NativeBody = asmBody.NativeBlocks
                };

                return d;            
            }
            catch(Exception e)
            {
                onError(e.ToString());
                return none<MethodDisassembly>();
            }
        }
Beispiel #2
0
        public static string FormatCil(this MethodDisassembly src)
        {
            var format = sbuild();

            format.AppendLine(src.MethodSig.Format());
            format.AppendLine(AsciSym.LBrace.ToString());
            format.AppendLine(src.CilBody.ToString());
            format.Append(AsciSym.RBrace.ToString());
            return(format.ToString());
        }
Beispiel #3
0
        public static AsmFuncInfo DistillAsm(this MethodDisassembly src)
        {
            var         asm      = src.AsmBody;
            var         inxcount = asm.Instructions.Length;
            var         code     = asm.NativeBlocks.Single().Data;
            Span <byte> codespan = code;
            var         inxs     = new AsmInstructionInfo[inxcount];
            var         inxsfmt  = asm.FormatInstructions();

            for (var i = 0; i < asm.Instructions.Length; i++)
            {
                var inx      = asm.Instructions[i];
                var operands = inx.DistillOperands(asm);
                var offset   = (ushort)(inx.IP - asm.StartAddress);
                var encoded  = codespan.Slice(offset, inx.ByteLength).ToArray();
                var mnemonic = inx.Mnemonic.ToString().ToUpper();
                var opcode   = inx.Code.ToString();
                var enckind  = inx.Encoding == EncodingKind.Legacy ? string.Empty : inx.Encoding.ToString();

                inxs[i] = new AsmInstructionInfo(offset, inxsfmt[i], mnemonic, opcode, operands, enckind, encoded);
            }

            return(new AsmFuncInfo(asm.StartAddress, asm.EndAddress, src.MethodSig, inxs, code));
        }