public static EndpointInfo FromIpcEndpointInfo(IpcEndpointInfo info)
        {
            var client = new DiagnosticsClient(info.Endpoint);

            ProcessInfo processInfo = null;

            try
            {
                // Primary motivation is to keep parity with the FromProcessId implementation,
                // which provides the additional process information because it already has
                // access to it.
                processInfo = client.GetProcessInfo();

                Debug.Assert(info.ProcessId == unchecked ((int)processInfo.ProcessId));
                Debug.Assert(info.RuntimeInstanceCookie == processInfo.RuntimeInstanceCookie);
            }
            catch (ServerErrorException)
            {
                // The runtime likely doesn't understand the GetProcessInfo command.
            }
            catch (TimeoutException)
            {
                // Runtime didn't respond within client timeout.
            }

            return(new EndpointInfo()
            {
                Endpoint = info.Endpoint,
                ProcessId = info.ProcessId,
                RuntimeInstanceCookie = info.RuntimeInstanceCookie,
                CommandLine = processInfo?.CommandLine,
                OperatingSystem = processInfo?.OperatingSystem,
                ProcessArchitecture = processInfo?.ProcessArchitecture
            });
        }
        public FilteredEndpointInfoSource(
            ServerEndpointInfoSource serverEndpointInfoSource,
            IOptions <DiagnosticPortOptions> portOptions,
            ILogger <ClientEndpointInfoSource> clientSourceLogger)
        {
            _portOptions = portOptions.Value;

            DiagnosticPortConnectionMode connectionMode = _portOptions.GetConnectionMode();

            switch (connectionMode)
            {
            case DiagnosticPortConnectionMode.Connect:
                _source = new ClientEndpointInfoSource(clientSourceLogger);
                break;

            case DiagnosticPortConnectionMode.Listen:
                _source = serverEndpointInfoSource;
                break;

            default:
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Strings.ErrorMessage_UnhandledConnectionMode, connectionMode));
            }

            // Filter out the current process based on the connection mode.
            if (RuntimeInfo.IsDiagnosticsEnabled)
            {
                int pid = Process.GetCurrentProcess().Id;

                // Regardless of connection mode, can use the runtime instance cookie to filter self out.
                try
                {
                    var  client = new DiagnosticsClient(pid);
                    Guid runtimeInstanceCookie = client.GetProcessInfo().RuntimeInstanceCookie;
                    if (Guid.Empty != runtimeInstanceCookie)
                    {
                        _runtimeInstanceCookieToFilterOut = runtimeInstanceCookie;
                    }
                }
                catch (Exception)
                {
                }

                // If connecting to runtime instances, filter self out. In listening mode, it's likely
                // that multiple processes have the same PID in multi-container scenarios.
                if (DiagnosticPortConnectionMode.Connect == connectionMode)
                {
                    _processIdToFilterOut = pid;
                }
            }
        }
Exemplo n.º 3
0
        private static void ListProcessInfo(int pid)
        {
            // 2 commands exist to get process information but the corresponding
            // DiagnosticsClient.GetProcessInfo(Async) methods are internal.
            // So,
            //      (1) copy the corresponding project folder from the Diagnostics repository
            //      (2) add the application assembly as <InternalsVisibleTo> in the copied .csproj
            //      (3) reference this project instead of the nuget package
            // and it works  :^)
            var client = new DiagnosticsClient(pid);
            var info   = client.GetProcessInfo(); // this method is internal

            Console.WriteLine($"              Command Line = {info.CommandLine}");
            Console.WriteLine($"              Architecture = {info.ProcessArchitecture}");
            Console.WriteLine($"      Entry point assembly = {info.ManagedEntrypointAssemblyName}");
            Console.WriteLine($"               CLR Version = {info.ClrProductVersionString}");
        }
Exemplo n.º 4
0
        public FilteredEndpointInfoSource(IOptions <DiagnosticPortConfiguration> configuration)
        {
            _configuration = configuration.Value;
            switch (_configuration.ConnectionMode)
            {
            case DiagnosticPortConnectionMode.Connect:
                _source = new ClientEndpointInfoSource();
                break;

            case DiagnosticPortConnectionMode.Listen:
                _source = new ServerEndpointInfoSource(_configuration.EndpointName);
                break;

            default:
                throw new InvalidOperationException($"Unhandled connection mode: {_configuration.ConnectionMode}");
            }

            // Filter out the current process based on the connection mode.
            if (RuntimeInfo.IsDiagnosticsEnabled)
            {
                int pid = Process.GetCurrentProcess().Id;

                // Regardless of connection mode, can use the runtime instance cookie to filter self out.
                try
                {
                    var  client = new DiagnosticsClient(pid);
                    Guid runtimeInstanceCookie = client.GetProcessInfo().RuntimeInstanceCookie;
                    if (Guid.Empty != runtimeInstanceCookie)
                    {
                        _runtimeInstanceCookieToFilterOut = runtimeInstanceCookie;
                    }
                }
                catch (Exception)
                {
                }

                // If connecting to runtime instances, filter self out. In listening mode, it's likely
                // that multiple processes have the same PID in multi-container scenarios.
                if (DiagnosticPortConnectionMode.Connect == configuration.Value.ConnectionMode)
                {
                    _processIdToFilterOut = pid;
                }
            }
        }
        public static EndpointInfo FromProcessId(int processId)
        {
            var client = new DiagnosticsClient(processId);

            ProcessInfo processInfo = null;

            try
            {
                // Primary motivation is to get the runtime instance cookie in order to
                // keep parity with the FromIpcEndpointInfo implementation; store the
                // remainder of the information since it already has access to it.
                processInfo = client.GetProcessInfo();

                Debug.Assert(processId == unchecked ((int)processInfo.ProcessId));
            }
            catch (ServerErrorException)
            {
                // The runtime likely doesn't understand the GetProcessInfo command.
            }
            catch (TimeoutException)
            {
                // Runtime didn't respond within client timeout.
            }

            // CONSIDER: Generate a runtime instance identifier based on the pipe name
            // for .NET Core 3.1 e.g. pid + disambiguator in GUID form.
            return(new EndpointInfo()
            {
                Endpoint = new PidIpcEndpoint(processId),
                ProcessId = processId,
                RuntimeInstanceCookie = processInfo?.RuntimeInstanceCookie ?? Guid.Empty,
                CommandLine = processInfo?.CommandLine,
                OperatingSystem = processInfo?.OperatingSystem,
                ProcessArchitecture = processInfo?.ProcessArchitecture
            });
        }