Пример #1
0
        public static ParallelStack Build(string dumpFile, string dacFilePath)
        {
            DataTarget    dataTarget = null;
            ParallelStack ps         = null;

            try
            {
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    dataTarget = DataTarget.LoadCrashDump(dumpFile);
                }
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    dataTarget = DataTarget.LoadCoreDump(dumpFile);
                }
                else
                {
                    throw new InvalidOperationException("Unsupported platform...");
                }

                var runtime = CreateRuntime(dataTarget, dacFilePath);
                if (runtime == null)
                {
                    return(null);
                }

                ps = ParallelStack.Build(runtime);
            }
            finally
            {
                dataTarget?.Dispose();
            }

            return(ps);
        }
        public static ParallelStack Build(int pid, string dacFilePath)
        {
            DataTarget    dataTarget = null;
            ParallelStack ps         = null;

            try
            {
#if ClrMD1
                const uint msecTimeout = 2000;

                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    dataTarget = DataTarget.AttachToProcess(pid, msecTimeout, AttachFlag.NonInvasive);
                }
                else
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    // ClrMD implementation for Linux is available only for Passive
                    dataTarget = DataTarget.AttachToProcess(pid, msecTimeout, AttachFlag.Passive);
                }
#else
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    dataTarget = DataTarget.AttachToProcess(pid, true);
                }
                else
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    // ClrMD implementation for Linux is available only for Passive
                    dataTarget = DataTarget.AttachToProcess(pid, true);
                }
#endif
                else
                {
                    throw new InvalidOperationException("Unsupported platform...");
                }

                var runtime = CreateRuntime(dataTarget, dacFilePath);
                if (runtime == null)
                {
                    return(null);
                }

                ps = ParallelStack.Build(runtime);
            }
            finally
            {
                dataTarget?.Dispose();
            }

            return(ps);
        }
Пример #3
0
        public static void Run(string name, string version, string[] args)
        {
            if (args.Length == 0)
            {
                ShowHelp(name, version, "Missing dump file path or process ID...");
                return;
            }

            string dacFilePath = (args.Length >= 2) ? args[1] : null;

            if (dacFilePath != null)
            {
                if (!File.Exists(dacFilePath))
                {
                    Console.WriteLine($"{dacFilePath} file does not exist...");
                    return;
                }
            }

            ParallelStack ps;

            var input = args[0];

            // attach to live process
            if (int.TryParse(input, out var pid))
            {
                try
                {
                    ps = ParallelStack.Build(pid, dacFilePath);
                }
                catch (InvalidOperationException x)
                {
                    Console.WriteLine($"Impossible to build call stacks: {x.Message}");
                    return;
                }
            }
            // open memory dump
            else
            {
                if (!File.Exists(input))
                {
                    Console.WriteLine($"'{input}' does not exist...");
                    return;
                }

                try
                {
                    ps = ParallelStack.Build(input, dacFilePath);
                }
                catch (InvalidOperationException x)
                {
                    Console.WriteLine($"Impossible to build call stacks: {x.Message}");
                    return;
                }
            }

            int threadIDsCountlimit = 4;
            var visitor             = new ConsoleRenderer(useDml: false, limit: threadIDsCountlimit);

            Console.WriteLine();
            foreach (var stack in ps.Stacks)
            {
                Console.Write("________________________________________________");
                stack.Render(visitor);
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine();
            }

            Console.WriteLine($"==> {ps.ThreadIds.Count} threads with {ps.Stacks.Count} roots{Environment.NewLine}");
        }