private void DisassembleAndWriteSimpleMethod(JitWriteContext context, MethodBase method) { var handle = method.MethodHandle; RuntimeHelpers.PrepareMethod(handle); var clrMethod = context.Runtime.GetMethodByHandle((ulong)handle.Value.ToInt64()); var regions = FindNonEmptyHotColdInfo(clrMethod); if (clrMethod == null || regions == null) { context.Runtime.Flush(); clrMethod = context.Runtime.GetMethodByHandle((ulong)handle.Value.ToInt64()); regions = FindNonEmptyHotColdInfo(clrMethod); } if (clrMethod == null || regions == null) { var address = (ulong)handle.GetFunctionPointer().ToInt64(); clrMethod = context.Runtime.GetMethodByAddress(address); regions = FindNonEmptyHotColdInfo(clrMethod); } var writer = context.Writer; if (clrMethod != null) { writer.WriteLine(); writer.WriteLine(clrMethod.GetFullSignature()); } else { WriteSignatureFromReflection(context, method); } if (regions == null) { if (method.IsGenericMethod || (method.DeclaringType?.IsGenericType ?? false)) { writer.WriteLine(" ; Failed to find HotColdInfo for generic method (reference types?)."); writer.WriteLine(" ; If you know a solution, please comment at https://github.com/ashmind/SharpLab/issues/99."); return; } writer.WriteLine(" ; Failed to find HotColdRegions — please report at https://github.com/ashmind/SharpLab/issues."); return; } var methodAddress = regions.HotStart; var methodLength = regions.HotSize; var reader = new MemoryCodeReader(new IntPtr(unchecked ((long)methodAddress)), methodLength); var decoder = Decoder.Create(MapArchitectureToBitness(context.Runtime.DataTarget.Architecture), reader); var instructions = new InstructionList(); decoder.IP = methodAddress; while (decoder.IP < (methodAddress + methodLength)) { decoder.Decode(out instructions.AllocUninitializedElement()); } var resolver = new JitAsmSymbolResolver(context.Runtime, methodAddress, methodLength); var formatter = new IntelFormatter(FormatterOptions, resolver); var output = new StringOutput(); foreach (ref var instruction in instructions) { formatter.Format(instruction, output); writer.Write(" L"); writer.Write((instruction.IP - methodAddress).ToString("x4")); writer.Write(": "); writer.WriteLine(output.ToStringAndReset()); } }
public DiasmHelper(void *Address) { Reader = new MemoryCodeReader(Address); Decoder = Decoder.Create(32, Reader); }