:::: MENU ::::

Friday, February 13, 2009

Attaching to a process is something that developers use very often to debug, but what happens when the process (A) you want to debug is launched from another process (B) and you want to debug is the initialization of process A,  Supposing that process A can only run within the context of process B in that case the only way to start process A is to do so from process so classic F5 won't help us to debug the initialization of A.

I had this scenario at work so at first I "trained" myself to do a very fast Alt+Ctrl+P select the process and press Attach button, of course this training didn't help much, after googling a little I found this macro

 Sub AttachAspNet()
        Dim process As EnvDTE.Process

        If Not (DTE.Debugger.DebuggedProcesses Is Nothing) Then
            For Each process In DTE.Debugger.DebuggedProcesses
                If (process.Name.IndexOf("aspnet_wp.exe") <> -1) Then
                    Exit Sub
                End If
            Next
        End If

        For Each process In DTE.Debugger.LocalProcesses
            If (process.Name.IndexOf("myprocess.exe") <> -1) Then
                process.Attach()
                Exit Sub
            End If
        Next
    End Sub 
but still it would have to wait for the process to start and then attach to it, so finally I created this Visual Studio plug-in, which have this option.

 

The main idea is to wait for the process to start and then attach to it:

 

        private AttachResult PessimisticAttach(AttachType attachType)

        {

            AttachResult res = Attach(attachType);

 

            DateTime timeout = DateTime.Now.AddSeconds(WaitTimeout);

 

            while(res == AttachResult.NotRunning && timeout > DateTime.Now)

            {

                res = Attach(attachType);

                System.Threading.Thread.Sleep(100);

            }

            return res;

        }

        private AttachResult Attach(AttachType attachType)

        {

            string engine =attachTypesMap[attachType];

 

            if(IsBeingDebugged())

            {

                return AttachResult.BeingDebugged;

            }

 

            Debugger2 dbg = dte.Debugger as Debugger2;

            Transport trans = dbg.Transports.Item("Default");

            Engine eng;

 

            eng = trans.Engines.Item(engine);

 

            EnvDTE80.Process2 proc = null;

 

            try

            {

                proc = dbg.GetProcesses(trans, "").Item(processName) as EnvDTE80.Process2;

            }

            catch(Exception ex)

            {

                if(ex.Message.Contains("Invalid index."))

                {

                    return AttachResult.NotRunning;

                }

            }

 

            proc.Attach2(eng);

 

            return AttachResult.Attached;

 

        }

 

 

I wrote the plug-in using Declarative Visual Studio addin buttons with icons which saved me a lot of time and pain.

 

You can download plug in source here

More