How to put breakpoint in every function of .cpp fi

2019-01-26 05:24发布

Is there a macro that does it? Which DTE objects to use?

8条回答
手持菜刀,她持情操
2楼-- · 2019-01-26 05:54

Here's one way to do it (I warn you it is hacky):

EnvDTE.TextSelection textSelection = (EnvDTE.TextSelection)dte.ActiveWindow.Selection;
// I'm sure there's a better way to get the line count than this...
var lines = File.ReadAllLines(dte.ActiveDocument.FullName).Length;
var methods = new List<CodeElement>();
var oldLine = textSelection.AnchorPoint.Line;
var oldLineOffset = textSelection.AnchorPoint.LineCharOffset;
EnvDTE.CodeElement codeElement = null;
for (var i = 0; i < lines; i++)
{
    try
    {
        textSelection.MoveToLineAndOffset(i, 1);
        // I'm sure there's a better way to get a code element by point than this...
        codeElement =  textSelection.ActivePoint.CodeElement[vsCMElement.vsCMElementFunction];
        if (codeElement != null)
        {
            if (!methods.Contains(codeElement))
            {
                methods.Add(codeElement);
            }
        }
    }
    catch
    {
        //MessageBox.Show("Add error handling here.");
    }
}

// Restore cursor position
textSelection.MoveToLineAndOffset(oldLine, oldLineOffset);

// This could be in the for-loop above, but it's here instead just for
// clarity of the two separate jobs; find all methods, then add the
// breakpoints
foreach (var method in methods)
{
    dte.Debugger.Breakpoints.Add(
        Line: method.StartPoint.Line,
        File: dte.ActiveDocument.FullName);
}
查看更多
一纸荒年 Trace。
3楼-- · 2019-01-26 05:56

Here's how something similar could be achieved in WinDbg:

bm mymodule!CSpam::*

This puts breakpoint in every method of class (or namespace) CSpam in module mymodule.

I'm still looking for anything close to this functionality in Visual Studio.

查看更多
登录 后发表回答