예제 #1
0
        public static string GetErrorInfo(this IVsUIShell shell)
        {
            string message;

            ErrorHandler.ThrowOnFailure(shell.GetErrorInfo(out message));
            return(message);
        }
예제 #2
0
        /// <summary>
        /// Gets the error info set on the thread. Returns empty string is none set (not null)
        /// </summary>
        public static string GetErrorInfo()
        {
            string     errText = null;
            IVsUIShell uiShell = (IVsUIShell)Package.GetGlobalService(typeof(IVsUIShell));

            if (uiShell != null)
            {
                uiShell.GetErrorInfo(out errText);
            }
            if (errText == null)
            {
                return(string.Empty);
            }
            return(errText);
        }
예제 #3
0
        public void StartDebugging(Project project, IMessageLogger msg)
        {
            string file   = GetOutputFile(project);
            string folder = GetOutputFolder(project);

            ServiceProvider serviceProvider = new ServiceProvider((Microsoft.VisualStudio.OLE.Interop.IServiceProvider)_dte);

            IntPtr debugInfo = GetDebugInfo(string.Empty, file, folder);

            try
            {
                IVsDebugger debugger     = (IVsDebugger)serviceProvider.GetService((typeof(SVsShellDebugger)));
                int         launchResult = debugger.LaunchDebugTargets(1, debugInfo);

                Marshal.ThrowExceptionForHR(launchResult);
            }
            catch (Exception ex)
            {
                IVsUIShell uiShell = (IVsUIShell)serviceProvider.GetService(typeof(SVsUIShell));

                string message;
                uiShell.GetErrorInfo(out message);

                if (!string.IsNullOrEmpty(message))
                {
                    msg.ErrorMessage("Unable to start debugger: \"" + message + "\"", "Debugger");
                }
                else if (!string.IsNullOrEmpty(ex.Message))
                {
                    msg.ErrorMessage("Unable to start debugger: \"" + ex.Message + "\"", "Debugger");
                }
            }
            finally
            {
                if (debugInfo != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(debugInfo);
                }
            }
        }
예제 #4
0
        /// <summary>
        /// Launch an executable using the VSNDK debug engine.
        /// </summary>
        /// <param name="pidString"> Process ID in string format. </param>
        /// <returns> TRUE if successful, False if not. </returns>
        private bool LaunchDebugTarget(string pidString)
        {
            Microsoft.VisualStudio.Shell.ServiceProvider sp =
                new Microsoft.VisualStudio.Shell.ServiceProvider((Microsoft.VisualStudio.OLE.Interop.IServiceProvider)_dte);

            IVsDebugger dbg = (IVsDebugger)sp.GetService(typeof(SVsShellDebugger));

            VsDebugTargetInfo info = new VsDebugTargetInfo();

            info.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(info);
            info.dlo    = Microsoft.VisualStudio.Shell.Interop.DEBUG_LAUNCH_OPERATION.DLO_CreateProcess;

            // Read debugger args from a file (it is set when the Deploy task is run)
            System.IO.StreamReader argsFile = null;
            try
            {
                string localAppData = Environment.GetEnvironmentVariable("AppData");
                argsFile = new System.IO.StreamReader(localAppData + @"\BlackBerry\vsndk-args-file.txt");
            }
            catch (Exception e)
            {
                Debug.Fail("Unexpected exception in LaunchDebugTarget");
            }

            // Store all debugger arguments in a string
            var nvc = new NameValueCollection();

            nvc.Add("pid", pidString);
            nvc.Add("targetIP", argsFile.ReadLine()); // The device (IP address)
            info.bstrExe = argsFile.ReadLine();       // The executable path
            nvc.Add("isSimulator", argsFile.ReadLine());
            nvc.Add("ToolsPath", argsFile.ReadLine());
            nvc.Add("PublicKeyPath", argsFile.ReadLine());

            // Decrypt stored password.
            byte[] data = Convert.FromBase64String(argsFile.ReadLine());
            if (data.Length > 0)
            {
                byte[] decrypted = ProtectedData.Unprotect(data, null, DataProtectionScope.LocalMachine);
                nvc.Add("Password", Encoding.Unicode.GetString(decrypted));
            }

            info.bstrArg = NameValueCollectionHelper.DumpToString(nvc);
            argsFile.Close();

            info.bstrRemoteMachine         = null;                                 // debug locally
            info.fSendStdoutToOutputWindow = 0;                                    // Let stdout stay with the application.
            info.clsidCustom = new Guid("{E5A37609-2F43-4830-AA85-D94CFA035DD2}"); // Set the launching engine the VSNDK engine guid
            info.grfLaunch   = 0;

            IntPtr pInfo = System.Runtime.InteropServices.Marshal.AllocCoTaskMem((int)info.cbSize);

            System.Runtime.InteropServices.Marshal.StructureToPtr(info, pInfo, false);

            try
            {
                int result = dbg.LaunchDebugTargets(1, pInfo);

                if (result != VSConstants.S_OK)
                {
                    string     msg;
                    IVsUIShell sh = (IVsUIShell)sp.GetService(typeof(SVsUIShell));
                    sh.GetErrorInfo(out msg);
                    Debug.WriteLine("LaunchDebugTargets: " + msg);

                    return(true);
                }
            }
            finally
            {
                if (pInfo != IntPtr.Zero)
                {
                    System.Runtime.InteropServices.Marshal.FreeCoTaskMem(pInfo);
                }
            }

            return(false);
        }
예제 #5
0
        public static void AttachToProcess(Process[] processes, ChildDebuggingMode mode)
        {
            List <VsDebugTargetInfo2> targetList = new List <VsDebugTargetInfo2>();
            IntPtr targetsBuffer = IntPtr.Zero;
            int    targetSize    = Marshal.SizeOf(typeof(VsDebugTargetInfo2));
            int    guidSize      = Marshal.SizeOf(typeof(Guid));

            try {
                foreach (Process process in processes)
                {
                    NtProcess           ntproc  = new NtProcess(process.Id);
                    VsDebugTargetInfo2  target  = new VsDebugTargetInfo2();
                    DebugProcessOptions options = new DebugProcessOptions {
                        ChildDebuggingMode = mode
                    };
                    target.dwDebugEngineCount = 1;
                    target.dwProcessId        = (uint)process.Id;
                    target.dlo = (uint)DEBUG_LAUNCH_OPERATION.DLO_AlreadyRunning;
                    if (process.Threads.Count == 1)
                    {
                        // If this is a suspended process, then using DLO_AttachToSuspendedLaunchProcess will
                        // bypass the initial loader breakpoint, causing a seamless and transparent attach.
                        // This is usually the desired behavior, as child processes frequently startup and
                        // shutdown, and it is intrusive to be constantly breaking into the debugger.
                        ProcessThread mainThread = process.Threads[0];
                        if (mainThread.ThreadState == ThreadState.Wait && mainThread.WaitReason == ThreadWaitReason.Suspended)
                        {
                            target.dlo |= (uint)_DEBUG_LAUNCH_OPERATION4.DLO_AttachToSuspendedLaunchProcess;
                        }
                    }
                    target.bstrExe          = ntproc.Win32ProcessImagePath;
                    target.cbSize           = (uint)targetSize;
                    target.bstrCurDir       = null;
                    target.guidPortSupplier = DkmIntegration.Guids.PortSupplier.Default;
                    target.LaunchFlags      = (uint)(__VSDBGLAUNCHFLAGS.DBGLAUNCH_Silent | __VSDBGLAUNCHFLAGS.DBGLAUNCH_WaitForAttachComplete);
                    target.bstrOptions      = options.OptionsString;
                    target.pDebugEngines    = Marshal.AllocCoTaskMem(guidSize);
                    Marshal.StructureToPtr(DkmEngineId.NativeEng, target.pDebugEngines, false);
                    targetList.Add(target);
                }
                int elementSize = Marshal.SizeOf(typeof(VsDebugTargetInfo2));
                targetsBuffer = Marshal.AllocCoTaskMem(targetList.Count * elementSize);
                for (int i = 0; i < targetList.Count; ++i)
                {
                    IntPtr writeAddr = targetsBuffer + i * elementSize;
                    Marshal.StructureToPtr(targetList[i], writeAddr, false);
                }

                IVsDebugger2 debugger = (IVsDebugger2)VsPackage.GetGlobalService(typeof(SVsShellDebugger));
                Logger.LogInfo("Launching {0} debug targets", processes.Length);
                int hr = debugger.LaunchDebugTargets2((uint)processes.Length, targetsBuffer);
                if (hr != 0)
                {
                    IVsUIShell shell = (IVsUIShell)VsPackage.GetGlobalService(typeof(SVsUIShell));
                    string     error;
                    shell.GetErrorInfo(out error);
                    Logger.LogError("An error occured while attaching to process (hr = 0x{0:x}).  {1}", hr, error);
                }
            } finally {
                foreach (VsDebugTargetInfo2 target in targetList)
                {
                    if (target.pDebugEngines != IntPtr.Zero)
                    {
                        Marshal.FreeCoTaskMem(target.pDebugEngines);
                    }
                }

                if (targetsBuffer != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(targetsBuffer);
                }
            }
        }