public static ExceptionStackInformation PrintFrames(IDebugThread2 pThread) { int hr = 0; uint threadID = 0; ExceptionStackInformation exceptionStackInfo = null; hr = pThread.GetThreadId(out threadID); IEnumDebugFrameInfo2 enumDebugFrameInfo2; hr = pThread.EnumFrameInfo(enum_FRAMEINFO_FLAGS.FIF_FRAME, 0, out enumDebugFrameInfo2); if (hr == 0) { FRAMEINFO[] frameInfo = new FRAMEINFO[1]; uint fetched = 0; hr = enumDebugFrameInfo2.Reset(); exceptionStackInfo = new ExceptionStackInformation(); while (enumDebugFrameInfo2.Next(1, frameInfo, ref fetched) == VSConstants.S_OK) { IDebugStackFrame3 stackFrame = frameInfo[0].m_pFrame as IDebugStackFrame3; //IDebugThread2 debugThread; IDebugThread2 debugThread; hr = stackFrame.GetThread(out debugThread); uint sfThreadID = 0; hr = debugThread.GetThreadId(out sfThreadID); if (sfThreadID == threadID) { System.Diagnostics.Debug.WriteLine("We got the right stackframe!!!"); if (stackFrame != null) { StackFrameInformation stackFrameInfo = new StackFrameInformation(); IDebugDocumentContext2 docContext; hr = stackFrame.GetDocumentContext(out docContext); TEXT_POSITION[] startPos = new TEXT_POSITION[1]; TEXT_POSITION[] endPos = new TEXT_POSITION[1]; if (docContext == null) continue; hr = docContext.GetStatementRange(startPos, endPos); var advancedThread = pThread as IDebugThread3; if (advancedThread != null && advancedThread.IsCurrentException() == 0) { var message = ExtractExceptionMessage(stackFrame); if (message != null) { System.Diagnostics.Debug.WriteLine("EXCEPTION: " + message); exceptionStackInfo.ExceptionMessage = message; } var type = ExtractExceptionType(stackFrame); if (type != null) { System.Diagnostics.Debug.WriteLine("EXCEPTION TYPE: " + type); type = type.Replace("\"", ""); exceptionStackInfo.ExceptionKind = type; } } // not for cs...debugging js //IDebugDocument2 document; //docContext.GetDocument(out document); //IDebugDocumentText2 documentText = document as IDebugDocumentText2; //var line = GetDocumentText(documentText, startPos[0]); var line = GetTextLine(docContext, startPos[0], endPos[0]); System.Diagnostics.Debug.WriteLine(string.Format("Line {0}", line )); string fileName = ""; stackFrame.GetName(out fileName); System.Diagnostics.Debug.WriteLine(string.Format("File {0} Function {1}", fileName, frameInfo[0].m_bstrFuncName)); string t = string.Format("start: line({0}) col({1})", startPos[0].dwLine.ToString(), startPos[0].dwColumn.ToString()); System.Diagnostics.Debug.WriteLine(t); t = string.Format("end: line({0}) col({1})", endPos[0].dwLine.ToString(), endPos[0].dwColumn.ToString()); System.Diagnostics.Debug.WriteLine(t); stackFrameInfo.File = fileName; stackFrameInfo.LineNumber = (int)startPos[0].dwLine; stackFrameInfo.ColumnNumber = (int)startPos[0].dwColumn; stackFrameInfo.FramePath = frameInfo[0].m_bstrFuncName; stackFrameInfo.Line = line; exceptionStackInfo.Frames.Add(stackFrameInfo); } } } } return exceptionStackInfo; }
public static ExceptionStackInformation PrintFrames(IDebugThread2 pThread) { int hr = 0; uint threadID = 0; ExceptionStackInformation exceptionStackInfo = null; hr = pThread.GetThreadId(out threadID); IEnumDebugFrameInfo2 enumDebugFrameInfo2; hr = pThread.EnumFrameInfo(enum_FRAMEINFO_FLAGS.FIF_FRAME, 0, out enumDebugFrameInfo2); if (hr == 0) { FRAMEINFO[] frameInfo = new FRAMEINFO[1]; uint fetched = 0; hr = enumDebugFrameInfo2.Reset(); exceptionStackInfo = new ExceptionStackInformation(); while (enumDebugFrameInfo2.Next(1, frameInfo, ref fetched) == VSConstants.S_OK) { IDebugStackFrame3 stackFrame = frameInfo[0].m_pFrame as IDebugStackFrame3; //IDebugThread2 debugThread; IDebugThread2 debugThread; hr = stackFrame.GetThread(out debugThread); uint sfThreadID = 0; hr = debugThread.GetThreadId(out sfThreadID); if (sfThreadID == threadID) { System.Diagnostics.Debug.WriteLine("We got the right stackframe!!!"); if (stackFrame != null) { StackFrameInformation stackFrameInfo = new StackFrameInformation(); IDebugDocumentContext2 docContext; hr = stackFrame.GetDocumentContext(out docContext); TEXT_POSITION[] startPos = new TEXT_POSITION[1]; TEXT_POSITION[] endPos = new TEXT_POSITION[1]; if (docContext == null) { continue; } hr = docContext.GetStatementRange(startPos, endPos); var advancedThread = pThread as IDebugThread3; if (advancedThread != null && advancedThread.IsCurrentException() == 0) { var message = ExtractExceptionMessage(stackFrame); if (message != null) { System.Diagnostics.Debug.WriteLine("EXCEPTION: " + message); exceptionStackInfo.ExceptionMessage = message; } var type = ExtractExceptionType(stackFrame); if (type != null) { System.Diagnostics.Debug.WriteLine("EXCEPTION TYPE: " + type); type = type.Replace("\"", ""); exceptionStackInfo.ExceptionKind = type; } } // not for cs...debugging js //IDebugDocument2 document; //docContext.GetDocument(out document); //IDebugDocumentText2 documentText = document as IDebugDocumentText2; //var line = GetDocumentText(documentText, startPos[0]); var line = GetTextLine(docContext, startPos[0], endPos[0]); System.Diagnostics.Debug.WriteLine(string.Format("Line {0}", line)); string fileName = ""; stackFrame.GetName(out fileName); System.Diagnostics.Debug.WriteLine(string.Format("File {0} Function {1}", fileName, frameInfo[0].m_bstrFuncName)); string t = string.Format("start: line({0}) col({1})", startPos[0].dwLine.ToString(), startPos[0].dwColumn.ToString()); System.Diagnostics.Debug.WriteLine(t); t = string.Format("end: line({0}) col({1})", endPos[0].dwLine.ToString(), endPos[0].dwColumn.ToString()); System.Diagnostics.Debug.WriteLine(t); stackFrameInfo.File = fileName; stackFrameInfo.LineNumber = (int)startPos[0].dwLine; stackFrameInfo.ColumnNumber = (int)startPos[0].dwColumn; stackFrameInfo.FramePath = frameInfo[0].m_bstrFuncName; stackFrameInfo.Line = line; exceptionStackInfo.Frames.Add(stackFrameInfo); } } } } return(exceptionStackInfo); }