Exemple #1
0
        public static SourceLocation GetSourceLocation(this ClrMethod method, int ilOffset)
        {
            PdbReader   reader   = GetReaderForMethod(method);
            PdbFunction function = reader.GetFunctionFromToken(method.MetadataToken);

            return(FindNearestLine(function, ilOffset));
        }
        public static SourceLocation GetSourceLocation(this ClrStackFrame frame)
        {
            PdbReader reader = GetReaderForMethod(frame.Method);

            if (reader == null)
            {
                return(null);
            }

            PdbFunction function = reader.GetFunctionFromToken(frame.Method.MetadataToken);
            int         ilOffset = FindIlOffset(frame);

            return(FindNearestLine(function, ilOffset));
        }
Exemple #3
0
        public static FileAndLineNumber FileAndLineNumber(this ClrStackFrame frame)
        {
            PdbReader reader = GetReaderForFrame(frame);

            if (reader == null)
            {
                return(new FileAndLineNumber());
            }

            PdbFunction function = reader.GetFunctionFromToken(frame.Method.MetadataToken);
            int         ilOffset = FindIlOffset(frame);

            return(FindNearestLine(function, ilOffset));
        }
        public static SDFileAndLineNumber GetSourceLocation(this ClrStackFrame frame)
        {
            try {
                PdbReader reader = GetReaderForFrame(frame);
                if (reader == null)
                {
                    return(null);
                }

                PdbFunction function = reader.GetFunctionFromToken(frame.Method.MetadataToken);
                int         ilOffset = FindIlOffset(frame);

                return(FindNearestLine(function, ilOffset));
            } catch (Exception e) {
                Console.WriteLine($"exception in {nameof(GetSourceLocation)}: {e}");
                return(null);
            }
        }
Exemple #5
0
        public void PdbSourceLineTest()
        {
            using (DataTarget dt = TestTargets.NestedException.LoadFullDump())
            {
                ClrRuntime runtime = dt.ClrVersions.Single().CreateRuntime();
                ClrThread  thread  = runtime.GetMainThread();

                HashSet <int> sourceLines = new HashSet <int>();
                using (PdbReader reader = new PdbReader(TestTargets.NestedException.Pdb))
                {
                    Assert.IsTrue(TestTargets.NestedException.Source.Equals(reader.Sources.Single().Name, StringComparison.OrdinalIgnoreCase));

                    var functions = from frame in thread.StackTrace
                                    where frame.Kind != ClrStackFrameType.Runtime
                                    select reader.GetFunctionFromToken(frame.Method.MetadataToken);

                    foreach (PdbFunction function in functions)
                    {
                        PdbSequencePointCollection sourceFile = function.SequencePoints.Single();

                        foreach (int line in sourceFile.Lines.Select(l => l.LineBegin))
                        {
                            sourceLines.Add(line);
                        }
                    }
                }


                int curr = 0;
                foreach (var line in File.ReadLines(TestTargets.NestedException.Source))
                {
                    curr++;
                    if (line.Contains("/* seq */"))
                    {
                        Assert.IsTrue(sourceLines.Contains(curr));
                    }
                }
            }
        }
Exemple #6
0
        public void PdbMethodTest()
        {
            // Ensure all methods in our source file is in the pdb.
            using (DataTarget dt = TestTargets.NestedException.LoadFullDump())
            {
                ClrRuntime runtime = dt.ClrVersions.Single().CreateRuntime();
                ClrModule  module  = runtime.Modules.Where(m => m.Name.Equals(TestTargets.NestedException.Executable, StringComparison.OrdinalIgnoreCase)).Single();
                ClrType    type    = module.GetTypeByName("Program");

                using (PdbReader pdb = new PdbReader(TestTargets.NestedException.Pdb))
                {
                    foreach (ClrMethod method in type.Methods)
                    {
                        // ignore inherited methods and constructors
                        if (method.Type != type || method.IsConstructor || method.IsClassConstructor)
                        {
                            continue;
                        }

                        Assert.IsNotNull(pdb.GetFunctionFromToken(method.MetadataToken));
                    }
                }
            }
        }
Exemple #7
0
        public static string GetSourceFromPdb <T>(Predicate <T> match)
        {
            int token = match.Method.MetadataToken;

            //construct symbols file path
            string module_path = match.Method.Module.FullyQualifiedName;
            string pdb_path    = Path.Combine(
                Path.GetDirectoryName(module_path),
                Path.GetFileNameWithoutExtension(module_path) + ".pdb"
                );

            StringBuilder sb     = new StringBuilder();
            PdbReader     reader = new PdbReader(pdb_path);

            using (reader)
            {
                //find method in symbols
                var func = reader.GetFunctionFromToken((uint)token);

                foreach (PdbSequencePointCollection coll in func.SequencePoints)
                {
                    //read source file
                    string[] lines = File.ReadAllLines(coll.File.Name, System.Text.Encoding.UTF8);

                    //find method beginning & end source lines
                    var points_sorted = coll.Lines.
                                        Where <PdbSequencePoint>((x) => x.LineBegin <= lines.Length && x.LineEnd <= lines.Length).
                                        OrderBy <PdbSequencePoint, uint>((x) => x.Offset);
                    PdbSequencePoint start = points_sorted.First();
                    PdbSequencePoint end   = points_sorted.Last();

                    bool reading = false;
                    int  index_start;
                    int  index_end;

                    //read method's code from source
                    for (int i = 1; i <= lines.Length; i++)
                    {
                        string line = lines[i - 1];
                        index_start = 0;
                        index_end   = line.Length;

                        if (!reading)
                        {
                            if (i >= start.LineBegin)
                            {
                                //first line
                                reading     = true;
                                index_start = start.ColBegin - 1;
                                if (index_start < 0)
                                {
                                    index_start = 0;
                                }
                            }
                        }

                        if (reading)
                        {
                            if (i >= end.LineEnd)
                            {
                                //last line
                                index_end = end.ColEnd - 1;
                                if (index_end > line.Length)
                                {
                                    index_end = line.Length;
                                }

                                sb.AppendLine(line.Substring(index_start, index_end - index_start));
                                break;
                            }

                            //read current line
                            sb.AppendLine(line.Substring(index_start, index_end - index_start));
                        }
                    }
                }
            }

            return(sb.ToString());
        }