public bool Attach(Process process, string dacFile = null, uint attachTimeout = DefaultAttachTimeout)
        {
            if (process == null)
            {
                throw new ArgumentNullException("process");
            }
            _process = process;

            if (_dataTarget != null)
            {
                _output.Error(String.Format("Already attached to process PID={0} Name={1}",
                    _process.Id, _process.ProcessName));
                return false;
            }

            _output.Info(String.Format("Attaching to process PID={0}",
                process.Id));

            try
            {
                _dataTarget = DataTarget.AttachToProcess(process.Id, attachTimeout);

                //make sure we dont kill the process on exit
            }

            catch (Exception exception)
            {
                //TODO: Be more specific, dont catch all exceptions
                _output.Error("Could not attach to the process.");
                throw;
            }

            if (_dataTarget == null)
            {
                //TODO: Be more specific, what exactly does it mean?
                _output.Error("Could not attach to the process.");
                _process = null;
                return false;
            }
            _dataTarget.DebuggerInterface.SetProcessOptions(DEBUG_PROCESS.DETACH_ON_EXIT);

            if (_dataTarget.ClrVersions.Count == 0)
            {
                var msg = String.Format(
                    "Process PID={0} Name={1} does not seem to have CLR loaded. Is it an unmanaged process?",
                    _process.Id, _process.ProcessName);
                _output.Error(msg);

                Detach();
                return false;
            }
            if (_dataTarget.ClrVersions.Count > 1)
            {
                //TODO: multiple CLRs found present user with choice?
                var msg = String.Format("Multiple CLR versions loaded. Proceeding with first version.");
                _output.Warning(msg);
            }
            _clrInfo = _dataTarget.ClrVersions[0];

            _output.Info(String.Format("Using CLR Version={0} DACFileName={1}",
                _clrInfo.Version, _clrInfo.DacInfo.FileName));

            string dacLocation;
            if(String.IsNullOrWhiteSpace(dacFile))
            {
                dacLocation = _clrInfo.TryGetDacLocation();
            }
            else
            {
                dacLocation = dacFile;
                _output.Info(String.Format("Using DacFile={0}", dacFile));
            }
            if (String.IsNullOrWhiteSpace(dacLocation))
            {
                //TODO: Check filepath, display meaningful message
                _output.Error("Could not automatically locate Data Access Component (mscordacwks.dll). This may mean that bitness or CLR versions do not match. " +
                              "You may specify file location manually eg. ClrDiag.Attach(PID, @\"C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\mscordacwks.dll\"");
                Detach();
                return false;
            }

            ClrRuntime runtime = _dataTarget.CreateRuntime(dacLocation);

            if (runtime == null)
            {
                //TODO: add more meaningful information
                _output.Error("Unable to get CLR information.");
                Detach();
            }
            _output.Success(String.Format("Succesfully attached to process PID={0} Name={1}",
                                           _process.Id, _process.ProcessName));
            Clr = runtime;
            return true;
        }
Exemplo n.º 2
0
        public string FindDac(ClrInfo clrInfo)
        {
            string dac = clrInfo.TryGetDacLocation();

            if (string.IsNullOrEmpty(dac))
                dac = FindDac(clrInfo.DacInfo.FileName, clrInfo.DacInfo.TimeStamp, clrInfo.DacInfo.FileSize);

            return dac;
        }