In Visual Studio Extension, get the range of lines of the function in which the debugger is disabled

I have a Visual Studio extension that catches debugging events. When the debugger stops in a line of code, my callback IDebugEventCallback2 is called to me, and I can find out the file name and line number where the debugger stopped through IDebugThread2::EnumFrameInfo .

I would like to know the range of lines of source code that spans the current function.

I hope to get the information I need from the debugger interfaces - the debugger should know the linear range of functions. If this is not possible, I am open to any other methods. In an ideal world, a solution will work without a project system - many people, including me, use Visual Studio as a standalone debugger without using a project system. (Also, I cannot rely on Roslyn - it should work on existing versions of Visual Studio.)

Edit : The Carlos method using FileCodeModel works well if the file is part of a project. I would still like to know if there is a method that does not require a project system.

+6
source share
1 answer

Given the FRAMEINFO obtained with IEnumDebugFrameInfo2.Next , you can use the following code to get the file name, the first line of code of the current frame, and the current line of code:

 IDebugStackFrame2 stackFrame = frmInfo.m_pFrame; if (stackFrame != null) { TEXT_POSITION[] begin = new TEXT_POSITION[1]; TEXT_POSITION[] end = new TEXT_POSITION[1]; IDebugDocumentContext2 debugDocumentContext2; stackFrame.GetDocumentContext(out debugDocumentContext2); if (debugDocumentContext2 != null) { string fileName; debugDocumentContext2.GetName((uint)enum_GETNAME_TYPE.GN_FILENAME, out fileName); debugDocumentContext2.GetSourceRange(begin, end); } } 

FWIW, the IDebugDocumentContext2 interface has a Seek method that allows you to push lines or code instructions in a stack frame. I think you can move on until you get the final line of code for the stack frame.

To get information about code elements and start / end points using the project system (and without Roslyn), you need to use the automation model (EnvDTE.ProjectItem.FileCodeModel). Given the EnvDTE.ProjectItem object and the line of code, you can use, for example: HOWTO: Get the code element in the cursor from the Visual Studio.NET macro or add -to

0
source

Source: https://habr.com/ru/post/989212/


All Articles