Ejemplo n.º 1
0
 public static void DumpLineNumberTable(TextWriter writer, MonoSymbolFile file,
                                        Cecil.MethodDefinition mdef, C.MethodEntry entry)
 {
     try {
         DumpLineNumberTable_internal(writer, file, mdef, entry);
     } catch (Exception ex) {
         writer.WriteLine("DUMP LNT EX: {0}", ex);
     }
 }
Ejemplo n.º 2
0
        internal static string GetMethodName(Cecil.MethodDefinition mdef)
        {
            StringBuilder sb = new StringBuilder(GetTypeSignature(mdef.DeclaringType));

            if (mdef.DeclaringType.GenericParameters.Count > 0)
            {
                sb.Append('<');
                bool first = true;
                foreach (Cecil.GenericParameter p in mdef.DeclaringType.GenericParameters)
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        sb.Append(',');
                    }
                    sb.Append(p.Name);
                }
                sb.Append('>');
            }
            sb.Append('.');
            sb.Append(mdef.Name);
            if (mdef.GenericParameters.Count > 0)
            {
                sb.Append('<');
                bool first = true;
                foreach (Cecil.GenericParameter p in mdef.GenericParameters)
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        sb.Append(',');
                    }
                    sb.Append(p.Name);
                }
                sb.Append('>');
            }
            sb.Append(GetMethodSignature(mdef));
            return(sb.ToString());
        }
Ejemplo n.º 3
0
        internal static string GetMethodSignature(Cecil.MethodDefinition mdef)
        {
            StringBuilder sb    = new StringBuilder("(");
            bool          first = true;

            foreach (Cecil.ParameterDefinition p in mdef.Parameters)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    sb.Append(", ");
                }
                sb.Append(GetTypeSignature(p.ParameterType).Replace('+', '/'));
            }
            sb.Append(")");
            return(sb.ToString());
        }
Ejemplo n.º 4
0
        void get_methods(TargetMemoryAccess target)
        {
            if (methods_by_token != null)
            {
                return;
            }

            try {
                if (!MetadataHelper.MonoClassHasMethods(target, KlassAddress))
                {
                    return;
                }

                int count = MetadataHelper.MonoClassGetMethodCount(target, KlassAddress);

                methods_by_token = new Dictionary <int, TargetAddress> ();

                for (int i = 0; i < count; i++)
                {
                    TargetAddress address = MetadataHelper.MonoClassGetMethod(
                        target, KlassAddress, i);
                    int mtoken = MetadataHelper.MonoMethodGetToken(target, address);
                    if (mtoken != 0)
                    {
                        methods_by_token.Add(mtoken, address);
                    }
                }

                methods = new MonoMethodInfo [CecilType.Methods.Count];
                for (int i = 0; i < methods.Length; i++)
                {
                    Cecil.MethodDefinition m = CecilType.Methods [i];
                    methods [i] = MonoMethodInfo.Create(struct_type, i, m);
                }
            } catch {
                methods_by_token = null;
                methods          = null;
                throw;
            }
        }
Ejemplo n.º 5
0
        static void DumpLineNumberTable_internal(TextWriter writer, MonoSymbolFile file,
                                                 Cecil.MethodDefinition mdef, C.MethodEntry entry)
        {
            string full_name = MonoSymbolFile.GetMethodName(mdef);

            if (mdef.MetadataToken.TokenType != Cecil.Metadata.TokenType.Method)
            {
                writer.WriteLine("UNKNOWN METHOD: {0}", full_name);
                return;
            }

            writer.WriteLine();
            writer.WriteLine("Symfile Line Numbers (file / row / offset):");
            writer.WriteLine("-------------------------------------------");

            C.LineNumberEntry[] lnt;
            lnt = entry.GetLineNumberTable().LineNumbers;
            for (int i = 0; i < lnt.Length; i++)
            {
                C.LineNumberEntry lne = lnt [i];

                writer.WriteLine("{0,4} {1,4} {2,4} {3,4:x}{4}", i,
                                 lne.File, lne.Row, lne.Offset,
                                 lne.IsHidden ? " (hidden)" : "");
            }

            writer.WriteLine("-------------------------------------------");
            writer.WriteLine();

            List <string>         lines;
            Dictionary <int, int> offsets;

            if (!DisassembleMethod_internal(file.ImageFile, (int)mdef.MetadataToken.RID, out lines, out offsets))
            {
                writer.WriteLine("Cannot disassemble method: {0}", full_name);
                return;
            }

            writer.WriteLine("Disassembling {0}:\n\n{1}\n", full_name, String.Join("\n", lines.ToArray()));
        }
Ejemplo n.º 6
0
        public void PrintMethods(bool include_locals)
        {
            Message("Reading {0}, version {1}.{2}.", File.FileName, File.MajorVersion,
                    File.MinorVersion);

            for (int i = 0; i < File.MethodCount; i++)
            {
                if (Methods.Count > 0)
                {
                    if (!Methods.ContainsKey(i + 1))
                    {
                        continue;
                    }
                }

                MethodEntry method = File.GetMethod(i + 1);
                if (method == null)
                {
                    throw new MonoSymbolFileException("Cannot get method {0}.", i + 1);
                }

                Cecil.MethodDefinition mdef = (Cecil.MethodDefinition)Assembly.MainModule.LookupToken(
                    new Cecil.MetadataToken(Cecil.TokenType.Method, method.Token & 0xffffff));
                if ((mdef == null) || (mdef.Body == null))
                {
                    throw new MonoSymbolFileException("Method {0} (token {1:x}) not found in assembly.",
                                                      method.Index, method.Token);
                }

                Message("Method {0} ({1:x}) - {2}", method.Index, method.Token, GetMethodName(mdef));

                if (include_locals)
                {
                    PrintLocals(method);
                }
            }
        }
Ejemplo n.º 7
0
 public static int GetMethodToken(Cecil.MethodDefinition method)
 {
     return(method.MetadataToken.ToInt32());
 }
Ejemplo n.º 8
0
        protected void CheckMethod(MethodEntry method)
        {
            Cecil.MethodDefinition mdef = (Cecil.MethodDefinition)Assembly.MainModule.LookupToken(
                new Cecil.MetadataToken(Cecil.TokenType.Method, method.Token & 0xffffff));
            if ((mdef == null) || (mdef.Body == null))
            {
                throw new MonoSymbolFileException("Method {0} (token {1:x}) not found in assembly.",
                                                  method.Index, method.Token);
            }

            string name = String.Format("{0} ({1})", method.Index, GetMethodName(mdef));

            Debug("[Method {0} - {1} - {2}]", method.Index, method.CompileUnit.SourceFile.FileName,
                  GetMethodName(mdef));

            LineNumberTable lnt = method.GetLineNumberTable();

            if (lnt == null)
            {
                throw new MonoSymbolFileException("Cannot get LNT from method {0}.", name);
            }

            if (lnt.LineNumbers == null)
            {
                throw new MonoSymbolFileException("Cannot get LNT from method {0}.", name);
            }
            LineNumberEntry start, end;

            if (lnt.GetMethodBounds(out start, out end))
            {
                Debug("  Bounds: {0} {1}", start, end);
            }
            foreach (LineNumberEntry line in lnt.LineNumbers)
            {
                Debug("    Line: {0}", line);
            }

            CodeBlockEntry[] blocks = method.GetCodeBlocks() ?? new CodeBlockEntry [0];
            foreach (CodeBlockEntry block in blocks)
            {
                if ((block.Parent >= 0) && (block.Parent >= blocks.Length))
                {
                    throw new MonoSymbolFileException(
                              "Code block {0} in method {1} has invalid parent index {2} (valid is 0..{3}).",
                              block, name, block.Parent, blocks.Length);
                }
            }

            LocalVariableEntry[] locals = method.GetLocals() ?? new LocalVariableEntry [0];
            foreach (LocalVariableEntry local in locals)
            {
                if ((local.BlockIndex < 0) || ((local.BlockIndex > 0) && (local.BlockIndex > blocks.Length)))
                {
                    throw new MonoSymbolFileException(
                              "Local variable {0} in method {1} has invalid block index {2} (valid is 0..{3}).",
                              local, name, local.BlockIndex, blocks.Length);
                }

                Debug("  Local: {0}", local);
            }

            int num_locals = mdef.Body.Variables.Count;

            ScopeVariable[] scope_vars = method.GetScopeVariables() ?? new ScopeVariable [0];
            foreach (ScopeVariable var in scope_vars)
            {
                Debug("  Scope var: {0}", var);
                if ((mdef.IsStatic) && (var.Index < 0))
                {
                    throw new MonoSymbolFileException(
                              "Method {0} has invalid scope variable {1} (referencing `this' in static method).",
                              name, var);
                }
                if ((var.Index >= 0) && (var.Index >= num_locals))
                {
                    throw new MonoSymbolFileException(
                              "Method {0} has invalid scope variable {1} (index out of bounds: {2} / {3}).",
                              name, var, var.Index, num_locals);
                }
                if ((var.Scope > 0) && (File.GetAnonymousScope(var.Scope) == null))
                {
                    throw new MonoSymbolFileException(
                              "Method {0} has invalid scope variable {1} (can't find scope {2}).",
                              name, var, var.Scope);
                }
            }
        }
Ejemplo n.º 9
0
        public void PrintLineNumberTables()
        {
            Message("Reading {0}, version {1}.{2}.", File.FileName, File.MajorVersion,
                    File.MinorVersion);

            for (int i = 0; i < File.MethodCount; i++)
            {
                if (LineNumberTables.Count > 0)
                {
                    if (!LineNumberTables.ContainsKey(i + 1))
                    {
                        continue;
                    }
                }

                MethodEntry method = File.GetMethod(i + 1);
                if (method == null)
                {
                    throw new MonoSymbolFileException("Cannot get method {0}.", i + 1);
                }

                Cecil.MethodDefinition mdef = (Cecil.MethodDefinition)Assembly.MainModule.LookupToken(
                    new Cecil.MetadataToken(Cecil.TokenType.Method, method.Token & 0xffffff));
                if ((mdef == null) || (mdef.Body == null))
                {
                    throw new MonoSymbolFileException("Method {0} (token {1:x}) not found in assembly.",
                                                      method.Index, method.Token);
                }

                string name = String.Format("{0} ({1})", method.Index, GetMethodName(mdef));
                Message("Method {0} - {1}", method.Index, GetMethodName(mdef));

                LineNumberTable lnt = method.GetLineNumberTable();
                if (lnt == null)
                {
                    throw new MonoSymbolFileException("Cannot get LNT from method {0}.", name);
                }

                if (lnt.LineNumbers == null)
                {
                    throw new MonoSymbolFileException("Cannot get LNT from method {0}.", name);
                }

                Dictionary <int, bool> seen_files = new Dictionary <int, bool> ();

                LineNumberEntry start, end;
                if (lnt.GetMethodBounds(out start, out end))
                {
                    Message("  Bounds: {0} {1}", start, end);
                }
                foreach (LineNumberEntry line in lnt.LineNumbers)
                {
                    if (!line.IsHidden && !seen_files.ContainsKey(line.File))
                    {
                        SourceFileEntry file = File.GetSourceFile(line.File);
                        Message("  File {0}{2}: {1}", file.Index, file.FileName,
                                file.AutoGenerated ? " (auto-generated)" : "");
                        seen_files.Add(line.File, true);
                    }

                    string range = "";
                    if (line.SourceRange != null)
                    {
                        SourceRangeEntry sre = (SourceRangeEntry)line.SourceRange;
                        range = String.Format(" - {0} {1} {2} {3}", sre.StartLine, sre.EndLine,
                                              sre.StartColumn, sre.EndColumn);
                    }

                    Message("    Line {0}:{1}:{2:x}{3}{4}", line.File, line.Row, line.Offset,
                            line.IsHidden ? " (hidden)" : "", range);
                }
            }
        }
Ejemplo n.º 10
0
            public MonoMethodSource(MonoSymbolFile file, SourceFile source_file,
						 C.MethodEntry method, Cecil.MethodDefinition mdef,
						 MonoClassType klass, MonoFunctionType function)
            {
                this.file = file;
                this.source_file = source_file;
                this.method = method;
                this.mdef = mdef;
                this.function = function;
                this.klass = klass;

                full_name = method.GetRealName ();
                if (full_name == null)
                    full_name = MonoSymbolFile.GetMethodName (mdef);

                C.LineNumberEntry start, end;
                C.LineNumberTable lnt = method.GetLineNumberTable ();
                if (lnt.GetMethodBounds (out start, out end))
                    start_row = start.Row; end_row = end.Row;
            }
Ejemplo n.º 11
0
            public MonoMethod(MonoSymbolFile file, MethodSource source, int domain,
					   C.MethodEntry method, Cecil.MethodDefinition mdef)
                : base(source.Name, file.ImageFile, file.Module)
            {
                this.file = file;
                this.source = source;
                this.domain = domain;
                this.method = method;
                this.mdef = mdef;

                foreach (Cecil.CustomAttribute cattr in mdef.CustomAttributes) {
                    string cname = cattr.Constructor.DeclaringType.FullName;
                    if ((cname == "System.Diagnostics.DebuggerHiddenAttribute") ||
                        (cname == "System.Runtime.CompilerServices.CompilerGeneratedAttribute"))
                        is_compiler_generated = true;
                }
            }