Example #1
0
        private void DisassembleNative(ILToNativeMap map)
        {
            ulong         nextInstr;
            StringBuilder disasmBuffer = new StringBuilder(512);
            uint          disasmSize;
            ulong         disasmAddress = map.StartAddress;

            while (true)
            {
                int hr = _control.Disassemble(disasmAddress, 0,
                                              disasmBuffer, disasmBuffer.Capacity, out disasmSize,
                                              out nextInstr);
                if (hr != 0)
                {
                    break;
                }
                _context.Write(disasmBuffer.ToString());

                if (nextInstr >= map.EndAddress)
                {
                    break;
                }
                disasmAddress = nextInstr;
            }
        }
Example #2
0
        internal static IEnumerable <Sharp> GetSource(ClrMethod method, ILToNativeMap map)
        {
            var sourceLocation = method.GetSourceLocation(map.ILOffset);

            if (sourceLocation == null)
            {
                yield break;
            }

            for (int line = sourceLocation.LineNumber; line <= sourceLocation.LineNumberEnd; ++line)
            {
                var sourceLine = ReadSourceLine(sourceLocation.FilePath, line);
                if (sourceLine == null)
                {
                    continue;
                }

                var text = sourceLine + Environment.NewLine
                           + GetSmartPointer(sourceLine,
                                             start: line == sourceLocation.LineNumber ? sourceLocation.ColStart - 1 : default(int?),
                                             end: line == sourceLocation.LineNumberEnd ? sourceLocation.ColEnd - 1 : default(int?));

                yield return(new Sharp
                {
                    Text = text,
                    InstructionPointer = map.StartAddress,
                    FilePath = sourceLocation.FilePath,
                    LineNumber = line
                });
            }
        }
        public void Have_The_Correct_To_String()
        {
            // arrange
            var sut = new ILToNativeMap
            {
                ILOffset     = 0x42,
                StartAddress = 0x1337,
                EndAddress   = 0x1338
            };

            // act
            // assert
            sut.ToString().Should().Be("42 - [1337-1338]");
        }
Example #4
0
        public ILToNativeMap[] GetILToNativeMap()
        {
            InitDelegate(ref _getILAddressMap, VTable->GetILAddressMap);

            int hr = _getILAddressMap(Self, 0, out uint needed, null);

            if (hr != S_OK)
            {
                return(null);
            }

            ILToNativeMap[] map = new ILToNativeMap[needed];
            hr = _getILAddressMap(Self, needed, out needed, map);

            return(hr == S_OK ? map : null);
        }
Example #5
0
        public ILToNativeMap[]? GetILToNativeMap()
        {
            InitDelegate(ref _getILAddressMap, VTable.GetILAddressMap);

            HResult hr = _getILAddressMap(Self, 0, out uint needed, null);

            if (!hr)
            {
                return(null);
            }

            ILToNativeMap[] map = new ILToNativeMap[needed];
            hr = _getILAddressMap(Self, needed, out needed, map);

            return(hr ? map : null);
        }
Example #6
0
        static IEnumerable <Asm> GetAsm(ILToNativeMap map, State state, int depth, ClrMethod currentMethod)
        {
            var   disasmBuffer  = new StringBuilder(512);
            ulong disasmAddress = map.StartAddress;

            while (true)
            {
                int hr = state.DebugControl.Disassemble(disasmAddress, 0,
                                                        disasmBuffer, disasmBuffer.Capacity, out uint disasmSize,
                                                        out ulong endOffset);
                if (hr != 0)
                {
                    break;
                }

                disasmBuffer.Replace("\n", string.Empty);

                var textRepresentation = disasmBuffer.ToString();

                string calledMethodName = null;

                if (textRepresentation.Contains("call"))
                {
                    calledMethodName = TryEnqueueCalledMethod(textRepresentation, state, depth, currentMethod);
                }

                yield return(new Asm
                {
                    TextRepresentation = disasmBuffer.ToString(),
                    Comment = calledMethodName,
                    StartAddress = disasmAddress,
                    EndAddress = endOffset,
                    SizeInBytes = (uint)(endOffset - disasmAddress)
                });

                if (endOffset >= map.EndAddress)
                {
                    break;
                }

                disasmAddress = endOffset;
            }
        }
Example #7
0
        public unsafe ILToNativeMap[] GetILToNativeMapping()
        {
            int hr = obj.GetILToNativeMapping(0, out uint cMap, IntPtr.Zero);

            if (hr < 0)
            {
                return(Array.Empty <ILToNativeMap>());
            }
            var infos = new ILToNativeMap[cMap];

            if (cMap != 0)
            {
                fixed(void *p = &infos[0])
                hr = obj.GetILToNativeMapping(cMap, out cMap, new IntPtr(p));

                if (hr < 0)
                {
                    return(Array.Empty <ILToNativeMap>());
                }
            }
            return(infos);
        }
Example #8
0
        private static IEnumerable <Asm> Decode(ILToNativeMap map, State state, int depth, ClrMethod currentMethod)
        {
            ulong startAddress = map.StartAddress;
            uint  size         = (uint)(map.EndAddress - map.StartAddress);

            byte[] code = new byte[size];

            int totalBytesRead = 0;

            do
            {
                int bytesRead = state.Runtime.DataTarget.DataReader.Read(startAddress + (ulong)totalBytesRead, new Span <byte>(code, totalBytesRead, (int)size - totalBytesRead));
                if (bytesRead <= 0)
                {
                    throw new EndOfStreamException($"Tried to read {size} bytes for {currentMethod.Signature}, got only {totalBytesRead}");
                }
                totalBytesRead += bytesRead;
            } while (totalBytesRead != size);

            var reader  = new ByteArrayCodeReader(code, 0, (int)size);
            var decoder = Decoder.Create(state.Runtime.DataTarget.DataReader.PointerSize * 8, reader);

            decoder.IP = startAddress;

            while (reader.CanReadByte)
            {
                decoder.Decode(out var instruction);

                TryTranslateAddressToName(instruction, state, depth, currentMethod);

                yield return(new Asm
                {
                    InstructionPointer = instruction.IP,
                    Instruction = instruction
                });
            }
        }
Example #9
0
        static IEnumerable <Sharp> GetSource(ClrMethod method, ILToNativeMap map)
        {
            var sourceLocation = method.GetSourceLocation(map.ILOffset);

            if (sourceLocation == null)
            {
                yield break;
            }

            for (int line = sourceLocation.LineNumber; line <= sourceLocation.LineNumberEnd; ++line)
            {
                var sourceLine = ReadSourceLine(sourceLocation.FilePath, line);

                if (sourceLine != null)
                {
                    yield return(new Sharp
                    {
                        TextRepresentation = sourceLine + Environment.NewLine + new string(' ', sourceLocation.ColStart - 1) + new string('^', sourceLocation.ColEnd - sourceLocation.ColStart),
                        FilePath = sourceLocation.FilePath,
                        LineNumber = line
                    });
                }
            }
        }
Example #10
0
        internal ILToNativeMap[] GetILMap(Address ip)
        {
            List<ILToNativeMap> list = null;
            ILToNativeMap[] tmp = null;

            ulong handle;
            int res = _dacInterface.StartEnumMethodInstancesByAddress(ip, null, out handle);
            if (res < 0)
                return null;

            object objMethod;
            res = _dacInterface.EnumMethodInstanceByAddress(ref handle, out objMethod);

            while (res == 0)
            {
                IXCLRDataMethodInstance method = (IXCLRDataMethodInstance)objMethod;

                uint needed = 0;
                res = method.GetILAddressMap(0, out needed, null);

                if (res == 0)
                {
                    tmp = new ILToNativeMap[needed];
                    res = method.GetILAddressMap(needed, out needed, tmp);

                    for (int i = 0; i < tmp.Length; i++)
                    {
                        // There seems to be a bug in IL to native mappings where a throw statement
                        // may end up with an end address lower than the start address.  This is a
                        // workaround for that issue.
                        if (tmp[i].StartAddress > tmp[i].EndAddress)
                        {
                            if (i + 1 == tmp.Length)
                                tmp[i].EndAddress = tmp[i].StartAddress + 0x20;
                            else
                                tmp[i].EndAddress = tmp[i + 1].StartAddress - 1;
                        }
                    }

                    if (res != 0)
                        tmp = null;
                }

                res = _dacInterface.EnumMethodInstanceByAddress(ref handle, out objMethod);
                if (res == 0 && tmp != null)
                {
                    if (list == null)
                        list = new List<ILToNativeMap>();

                    list.AddRange(tmp);
                }
            }

            if (list != null)
            {
                list.AddRange(tmp);
                return list.ToArray();
            }

            _dacInterface.EndEnumMethodInstancesByAddress(handle);
            return tmp;
        }
Example #11
0
        internal ILToNativeMap[] GetILMap(Address ip)
        {
            List <ILToNativeMap> list = null;

            ILToNativeMap[] tmp = null;

            ulong handle;
            int   res = _dacInterface.StartEnumMethodInstancesByAddress(ip, null, out handle);

            if (res < 0)
            {
                return(null);
            }

            object objMethod;

            res = _dacInterface.EnumMethodInstanceByAddress(ref handle, out objMethod);

            while (res == 0)
            {
                IXCLRDataMethodInstance method = (IXCLRDataMethodInstance)objMethod;

                uint needed = 0;
                res = method.GetILAddressMap(0, out needed, null);

                if (res == 0)
                {
                    tmp = new ILToNativeMap[needed];
                    res = method.GetILAddressMap(needed, out needed, tmp);

                    for (int i = 0; i < tmp.Length; i++)
                    {
                        // There seems to be a bug in IL to native mappings where a throw statement
                        // may end up with an end address lower than the start address.  This is a
                        // workaround for that issue.
                        if (tmp[i].StartAddress > tmp[i].EndAddress)
                        {
                            if (i + 1 == tmp.Length)
                            {
                                tmp[i].EndAddress = tmp[i].StartAddress + 0x20;
                            }
                            else
                            {
                                tmp[i].EndAddress = tmp[i + 1].StartAddress - 1;
                            }
                        }
                    }

                    if (res != 0)
                    {
                        tmp = null;
                    }
                }

                res = _dacInterface.EnumMethodInstanceByAddress(ref handle, out objMethod);
                if (res == 0 && tmp != null)
                {
                    if (list == null)
                    {
                        list = new List <ILToNativeMap>();
                    }

                    list.AddRange(tmp);
                }
            }

            if (list != null)
            {
                list.AddRange(tmp);
                return(list.ToArray());
            }

            _dacInterface.EndEnumMethodInstancesByAddress(handle);
            return(tmp);
        }
Example #12
0
        internal ILToNativeMap[] GetILMap(Address ip)
        {
            ILToNativeMap[] result = null;

            ulong handle;
            int res = _dacInterface.StartEnumMethodInstancesByAddress(ip, null, out handle);
            if (res < 0)
                return null;

            object objMethod;
            res = _dacInterface.EnumMethodInstanceByAddress(ref handle, out objMethod);

            if (res == 0)
            {
                IXCLRDataMethodInstance method = (IXCLRDataMethodInstance)objMethod;

                uint needed = 0;
                res = method.GetILAddressMap(0, out needed, null);

                if (res == 0)
                {
                    result = new ILToNativeMap[needed];
                    res = method.GetILAddressMap(needed, out needed, result);

                    if (res != 0)
                        result = null;
                }

                _dacInterface.EndEnumMethodInstancesByAddress(handle);
            }

            return result;
        }