public int Event(IDebugEngine2 engine, IDebugProcess2 process, IDebugProgram2 program,
            IDebugThread2 thread, IDebugEvent2 debugEvent, ref Guid riidEvent, uint attributes)
        {
            if (process == null)
                return VSConstants.S_OK;
            string processName;
            if (process.GetName((uint) enum_GETNAME_TYPE.GN_FILENAME, out processName) != VSConstants.S_OK)
                return VSConstants.S_OK;
            if (processName.EndsWith("vshost.exe"))
                return VSConstants.S_OK;

            var shortName = Path.GetFileName(processName);

            if (debugEvent is IDebugProcessCreateEvent2)
            {
                Log.Instance.SetStatus("[attaching...] {0}", shortName);
                Storage.Instance.SubscribeProcess(processName);
            }
            if (debugEvent is IDebugProcessDestroyEvent2)
            {
                Log.Instance.SetStatus("[detached] {0}", shortName);
                Log.Instance.AppendLine("[detached] {0}", shortName);
            }
            if (debugEvent is  IDebugLoadCompleteEvent2)
            {
                if (program != null)
                {
                    string engineName;
                    Guid engineId;
                    if (program.GetEngineInfo(out engineName, out engineId) == VSConstants.S_OK)
                    {
                        var fields = new PROCESS_INFO[1];
                        if (process.GetInfo((uint)enum_PROCESS_INFO_FIELDS.PIF_PROCESS_ID, fields) != VSConstants.S_OK)
                            return VSConstants.S_OK;
                        Storage.Instance.SubscribeEngine(processName, engineId);
                        AttachCenter.Instance.Freeze();

                        Log.Instance.SetStatus("[attached] {0}", shortName);
                        Log.Instance.AppendLine("[attached] {0} ({1}) / {2}", shortName, fields[0].ProcessId.dwProcessId, engineName);
                    }
                }
            }
            return VSConstants.S_OK;
        }
Example #2
0
        public int Event(IDebugEngine2 pEngine,
                         IDebugProcess2 pProcess,
                         IDebugProgram2 pProgram,
                         IDebugThread2 pThread,
                         IDebugEvent2 pEvent,
                         ref Guid riidEvent,
                         uint dwAttrib)
        {
            try
            {
                string threadName = null;
                if (pThread != null && pEvent != null)
                {
                    uint attributes;

                    pEvent.GetAttributes(out attributes);
                    if ((uint)enum_EVENTATTRIBUTES.EVENT_SYNC_STOP == attributes)
                    {
                        HandleException.PrintFrames(pThread);
                    }

                    pThread.GetName(out threadName);
                    //ExtractFrameContent(pThread);
                    //IEnumDebugFrameInfo2 ppEnum = null;
                }
                Trace.WriteLine(string.Format("Event {0} Thread {1}", riidEvent, threadName));


                if (typeof(IDebugInterceptExceptionCompleteEvent2).GUID == riidEvent)
                {
                    var interactionEvent = pEvent as IDebugInterceptExceptionCompleteEvent2;
                    HandleException.PrintFrames(pThread);
                }
                //HandleException.ProcessEvent(riidEvent, pThread);

                if (typeof(IDebugProgramCreateEvent2).GUID == riidEvent)
                {
                    // Add handle
                }
                if (false)
                {
                    IDebugSessionEvent2 ev = pEvent as IDebugSessionEvent2;
                    var pInfo = new PROCESS_INFO[1];
                    if (pProcess != null)
                    {
                        pProcess.GetInfo((uint)enum_PROCESS_INFO_FIELDS.PIF_SESSION_ID, pInfo);
                    }
                }
                if (typeof(IDebugBreakEvent2).GUID == riidEvent)
                {
                    // Might be interesting to get the statement line number here and emit.
                    var ev = pEvent as IDebugBreakEvent2;
                }

                if (riidEvent == typeof(IDebugEntryPointEvent2).GUID)
                {
                    // This is when execution is just about to the start.

                    // I can't get the reference to the engine, pEngine is always null, and
                    // there doesn't seem to be an interface to fetch it (there is a query interface in docs, but not in dll!)

                    //string engineStr; Guid engineGuid;
                    //pProgram.GetEngineInfo(out engineStr, out engineGuid);
                    //var superEngine = pEngine as IDebugEngine3;
                    //if (superEngine != null)
                    //{
                    //    superEngine.SetAllExceptions(enum_EXCEPTION_STATE.EXCEPTION_STOP_FIRST_CHANCE);
                    //}
                }
                else if (riidEvent == typeof(IDebugMessageEvent2).GUID)
                {
                    var ev = pEvent as IDebugMessageEvent2;
                    var str = ""; uint type; string helpFile; uint helpId;
                    //var suc = ev.GetMessage(new enum_MESSAGETYPE[] { enum_MESSAGETYPE.MT_REASON_EXCEPTION }, out str, out type, out helpFile, out helpId);
                    uint messageType;
                    var  suc = ev.GetMessage(out messageType, out str, out type, out helpFile, out helpId);
                    if (suc == VSConstants.S_OK)
                    {
                        if (str.StartsWith("A first chance exception of type"))
                        {
                            if (pThread != null)
                            {
                                //ExtractFrameContent(pThread);
                                HandleException.PrintFrames(pThread);
                            }
                            // First chance exception thrown...but can't figure out how to get stack trace :(
                        }
                    }
                }

                //  This interface is sent by the debug engine (DE) to the session debug manager (SDM) when a thread is created in a program being debugged.
                //if (riidEvent == Guid.Parse("{2090ccfc-70c5-491d-a5e8-bad2dd9ee3ea}"))
                {
                    if (pThread != null)
                    {
                        //    ExtractFrameContent(pThread);
                    }
                }

                // Process of exception handling.
                // http://msdn.microsoft.com/es-es/library/bb146610.aspx
                if (riidEvent == typeof(IDebugExceptionEvent2).GUID
                    )
                {
                    IDebugExceptionEvent2 ev = pEvent as IDebugExceptionEvent2;
                    if (ev != null)
                    {
                        var info = new EXCEPTION_INFO[1];
                        ev.GetException(info);
                        var name  = info[0].bstrExceptionName;
                        var state = info[0].dwState;
                        //state == enum_EXCEPTION_STATE.EXCEPTION_STOP_SECOND_CHANCE

                        //ExtractFrameContent(pThread);
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }

            if (pEngine != null)
            {
                Marshal.ReleaseComObject(pEngine);
            }
            if (pProcess != null)
            {
                Marshal.ReleaseComObject(pProcess);
            }
            if (pProgram != null)
            {
                Marshal.ReleaseComObject(pProgram);
            }
            if (pThread != null)
            {
                Marshal.ReleaseComObject(pThread);
            }
            if (pEvent != null)
            {
                Marshal.ReleaseComObject(pEvent);
            }

            return(VSConstants.S_OK);
        }
Example #3
0
        public int Event(IDebugEngine2 pEngine, 
            IDebugProcess2 pProcess, 
            IDebugProgram2 pProgram, 
            IDebugThread2 pThread, 
            IDebugEvent2 pEvent, 
            ref Guid riidEvent, 
            uint dwAttrib)
        {
            try
            {
                string threadName = null;
                if (pThread != null && pEvent != null)
                {
                    uint attributes;

                    pEvent.GetAttributes(out attributes);
                    if ( (uint)enum_EVENTATTRIBUTES.EVENT_SYNC_STOP == attributes)
                    {
                        HandleException.PrintFrames(pThread);
                    }

                    pThread.GetName(out threadName);
                    //ExtractFrameContent(pThread);
                    //IEnumDebugFrameInfo2 ppEnum = null;
                }
                Trace.WriteLine(string.Format("Event {0} Thread {1}",  riidEvent, threadName ));

                if (typeof(IDebugInterceptExceptionCompleteEvent2).GUID == riidEvent)
                {
                    var interactionEvent = pEvent as IDebugInterceptExceptionCompleteEvent2;
                    HandleException.PrintFrames(pThread);
                }
                //HandleException.ProcessEvent(riidEvent, pThread);

                if (typeof(IDebugProgramCreateEvent2).GUID == riidEvent)
                {
                    // Add handle

                }
                if( false )
                {
                    IDebugSessionEvent2 ev = pEvent as IDebugSessionEvent2;
                    var pInfo = new PROCESS_INFO[1];
                    if (pProcess != null)
                    {
                        pProcess.GetInfo((uint)enum_PROCESS_INFO_FIELDS.PIF_SESSION_ID, pInfo);
                    }
                }
                if (typeof(IDebugBreakEvent2).GUID == riidEvent)
                {
                    // Might be interesting to get the statement line number here and emit.
                    var ev = pEvent as IDebugBreakEvent2;
                }

                if (riidEvent == typeof(IDebugEntryPointEvent2).GUID)
                {
                    // This is when execution is just about to the start.

                    // I can't get the reference to the engine, pEngine is always null, and
                    // there doesn't seem to be an interface to fetch it (there is a query interface in docs, but not in dll!)

                    //string engineStr; Guid engineGuid;
                    //pProgram.GetEngineInfo(out engineStr, out engineGuid);
                    //var superEngine = pEngine as IDebugEngine3;
                    //if (superEngine != null)
                    //{
                    //    superEngine.SetAllExceptions(enum_EXCEPTION_STATE.EXCEPTION_STOP_FIRST_CHANCE);
                    //}
                }
                else if (riidEvent == typeof(IDebugMessageEvent2).GUID)
                {
                    var ev = pEvent as IDebugMessageEvent2;
                    var str = ""; uint type; string helpFile; uint helpId;
                    //var suc = ev.GetMessage(new enum_MESSAGETYPE[] { enum_MESSAGETYPE.MT_REASON_EXCEPTION }, out str, out type, out helpFile, out helpId);
                    uint messageType;
                    var suc = ev.GetMessage( out messageType, out str, out type, out helpFile, out helpId);
                    if (suc == VSConstants.S_OK)
                    {
                        if (str.StartsWith("A first chance exception of type"))
                        {
                            if (pThread != null)
                            {
                                //ExtractFrameContent(pThread);
                                HandleException.PrintFrames(pThread);
                            }
                            // First chance exception thrown...but can't figure out how to get stack trace :(
                        }
                    }
                }

                //  This interface is sent by the debug engine (DE) to the session debug manager (SDM) when a thread is created in a program being debugged.
                //if (riidEvent == Guid.Parse("{2090ccfc-70c5-491d-a5e8-bad2dd9ee3ea}"))
                {
                    if (pThread != null)
                    {
                    //    ExtractFrameContent(pThread);
                    }
                }

                // Process of exception handling.
                // http://msdn.microsoft.com/es-es/library/bb146610.aspx
                if (riidEvent == typeof(IDebugExceptionEvent2).GUID
                    )
                {
                    IDebugExceptionEvent2 ev = pEvent as IDebugExceptionEvent2;
                    if (ev != null)
                    {
                        var info = new EXCEPTION_INFO[1];
                        ev.GetException(info);
                        var name = info[0].bstrExceptionName;
                        var state = info[0].dwState;
                        //state == enum_EXCEPTION_STATE.EXCEPTION_STOP_SECOND_CHANCE

                        //ExtractFrameContent(pThread);
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }

            if (pEngine != null)
            {
                Marshal.ReleaseComObject(pEngine);
            }
            if (pProcess != null)
            {
                Marshal.ReleaseComObject(pProcess);
            }
            if (pProgram != null)
            {
                Marshal.ReleaseComObject(pProgram);
            }
            if (pThread != null)
            {
                Marshal.ReleaseComObject(pThread);
            }
            if (pEvent != null)
            {
                Marshal.ReleaseComObject(pEvent);
            }

            return VSConstants.S_OK;
        }