Exemple #1
0
        public override void DecompileMethod(ilspy::Mono.Cecil.MethodDefinition method, ICSharpCode.Decompiler.ITextOutput output, DecompilationOptions options)
        {
            var cmethod = GetCompiledMethod(method);

            if ((cmethod != null) && (cmethod.DexMethod != null))
            {
                try
                {
                    var f = new MethodBodyDisassemblyFormatter(cmethod.DexMethod, MapFile);
                    var formatOptions = FormatOptions.EmbedSourceCode | FormatOptions.ShowJumpTargets;
                    if(ShowFullNames) formatOptions |= FormatOptions.FullTypeNames;
                    if(DebugOperandTypes) formatOptions |= FormatOptions.DebugOperandTypes;
                    
                    var s = f.Format(formatOptions);
                    output.Write(s);
                }
                catch (Exception)
                {
                    output.Write("\n\n// Formatting error. Using Fallback.\n\n");
                    FallbackFormatting(output, cmethod);    
                }
                
            }
            else
            {
                output.Write("Method not found in dex");
                output.WriteLine();
            }
        }
        /// <summary>
        /// Load the text to display
        /// </summary>
        protected override string LoadText(ISpyContext settings)
        {
            var nl = Environment.NewLine;
            var sb = new StringBuilder();
            sb.AppendFormat("AccessFlags: {0}{1}", _methodDef.AccessFlags, nl);
            sb.AppendFormat("Prototype:   {0}{1}", _methodDef.Prototype, nl);

            var body = _methodDef.Body;
            if (body != null)
            {
                sb.AppendFormat("#Registers:  {0}{1}", body.Registers.Count, nl);
                sb.AppendFormat("#Incoming:   {0}{1}", body.IncomingArguments, nl);
                sb.AppendFormat("#Outgoing:   {0}{1}", body.OutgoingArguments, nl);
                sb.AppendLine();

                _formatter = _formatter ?? new MethodBodyDisassemblyFormatter(_methodDef, settings.MapFile);
                
                FormatOptions format = FormatOptions.Default;
                if(settings.EmbedSourceCode)
                    format |= FormatOptions.EmbedSourceCode;
                if (settings.EmbedSourceCodePositions)
                    format |= FormatOptions.EmbedSourcePositions;
                if (settings.ShowControlFlow)
                    format |= FormatOptions.ShowControlFlow;
                if (settings.FullTypeNames)
                    format |= FormatOptions.FullTypeNames;
                var code = _formatter.Format(format);
                sb.AppendLine(code);

#if DEBUG
                var debugInfo = body.DebugInfo;
                if (debugInfo != null)
                {
                    sb.AppendLine();
                    sb.AppendLine("DEX Debug info:");
                    sb.AppendFormat("\tStart line: {0}{1}", debugInfo.LineStart, nl);
                    int lineNumber = (int) debugInfo.LineStart;
                    var address = 0;
                    var index = 0;
                    foreach (var ins in debugInfo.DebugInstructions)
                    {
                        if (!ins.IsLowLevel)
                            sb.AppendFormat("\t{0:x4}\tline {1}, address {2:x4} -> {3}{4}", index, lineNumber, address, ins, nl);                        
                        ins.UpdateLineAndAddress(ref lineNumber, ref address);
                        index++;
                    }
                }
#endif
            }

            return sb.ToString();
        }