如何在.cpp文件的每个函数中放置断点?

时间:2008-10-01 22:20:38

标签: debugging visual-c++ visual-studio-2005 macros envdte

是否有宏可以做到这一点?哪个DTE对象可以使用?

8 个答案:

答案 0 :(得分:18)

(这不是你要求的,但几乎是:)

您可以通过调出 New Breakpoint 对话框并输入:

,在Visual Studio中的类的每个成员函数上放置一个断点。
CMyClass::*

有关详细信息,请参阅http://blogs.msdn.com/b/habibh/archive/2009/09/10/class-breakpoint-how-to-set-a-breakpoint-on-a-c-class-in-the-visual-studio-debugger.aspx

答案 1 :(得分:5)

以下是1800 INFORMATION的想法的快速实现:

Sub TemporaryMacro()
    DTE.ActiveDocument.Selection.StartOfDocument()
    Dim returnValue As vsIncrementalSearchResult
    While True
        DTE.ActiveDocument.ActiveWindow.Object.ActivePane.IncrementalSearch.StartForward()
        returnValue = DTE.ActiveDocument.ActiveWindow.Object.ActivePane.IncrementalSearch.AppendCharAndSearch(AscW("{"))
        DTE.ActiveDocument.ActiveWindow.Object.ActivePane.IncrementalSearch.Exit()
        If Not (returnValue = vsIncrementalSearchResult.vsIncrementalSearchResultFound) Then
            Return
        End If
        DTE.ExecuteCommand("Debug.ToggleBreakpoint")
        DTE.ExecuteCommand("Edit.GotoBrace")
        DTE.ActiveDocument.Selection.CharRight()
    End While
End Sub

答案 2 :(得分:2)

我不知道使用什么DTE功能,但你可以非常简单地录制一个几乎可以做到的宏:

  1. 转到文件顶部
  2. ctrl - shift - R(开始录制)
  3. ctrl - I(增量搜索)
  4. {(搜索第一个{字符)。
  5. F9(设定断点)
  6. ctrl - ](转到匹配}字符)
  7. ctrl - shift - R(停止录制)
  8. 现在只需反复运行(ctrl - 反复移位P),直到到达文件末尾。

    如果您有名称空间,请将4.更改为:

    1. ((搜索“(函数定义开头的”)
    2. esc(停止增量搜索)
    3. ctrl - I(再次增量搜索)
    4. {(功能体的开头)
    5. 这种事情可以无限修改,以适应您的代码库

答案 3 :(得分:2)

像康斯坦丁的方法一样......这看起来像是windbg领域。

因为你有cpp,(即使你没有,你可以编写脚本来获取),使用logger部分调试工具的Windows应该没问题...这是一个非常方便的工具,很遗憾,很少有人使用它。

logger debug的C / COM / C ++,具有丰富的符号信息,钩子/分析/灵活的仪器;

One way to activate Logger is to start CDB or WinDbg and attach to a user-mode target application as usual. Then, use the !logexts.logi or !logexts.loge extension command. This will insert code at the current breakpoint that will jump off to a routine that loads and initializes Logexts.dll in the target application process. This is referred to as "injecting Logger into the target application."

答案 4 :(得分:1)

以下是WinDbg中可以实现类似的功能:

bm mymodule!CSpam::*

这会在模块CSpam中的类(或命名空间)mymodule的每个方法中放置断点。

我仍在寻找Visual Studio中与此功能相近的任何内容。

答案 5 :(得分:0)

将它放在文件的顶部:

#define WANT_BREAK_IN_EVERY_FUNCTION

#ifdef WANT_BREAK_IN_EVERY_FUNCTION
#define DEBUG_BREAK DebugBreak();
#else
#define DEBUG_BREAK 
#endif

然后在每个函数的开头插入DEBUG_BREAK,如下所示:

void function1()
{
    DEBUG_BREAK
    // the rest of the function
}

void function2()
{
    DEBUG_BREAK
    // the rest of the function
}

当您不再需要调试中断时,请注释该行

// #define WANT_BREAK_IN_EVERY_FUNCTION

位于文件顶部。

答案 6 :(得分:0)

有一个宏,但我只用c#测试它。

Sub BreakAtEveryFunction()
For Each project In DTE.Solution.Projects
    SetBreakpointOnEveryFunction(project)
Next project
End Sub


Sub SetBreakpointOnEveryFunction(ByVal project As Project)
Dim cm = project.CodeModel

' Look for all the namespaces and classes in the 
' project.
Dim list As List(Of CodeFunction)
list = New List(Of CodeFunction)
Dim ce As CodeElement
For Each ce In cm.CodeElements
    If (TypeOf ce Is CodeNamespace) Or (TypeOf ce Is CodeClass) Then
        ' Determine whether that namespace or class 
        ' contains other classes.
        GetClass(ce, list)
    End If
Next

For Each cf As CodeFunction In list

    DTE.Debugger.Breakpoints.Add(cf.FullName)
Next

End Sub

Sub GetClass(ByVal ct As CodeElement, ByRef list As List(Of CodeFunction))

' Determine whether there are nested namespaces or classes that 
' might contain other classes.
Dim aspace As CodeNamespace
Dim ce As CodeElement
Dim cn As CodeNamespace
Dim cc As CodeClass
Dim elements As CodeElements
If (TypeOf ct Is CodeNamespace) Then
    cn = CType(ct, CodeNamespace)
    elements = cn.Members
Else
    cc = CType(ct, CodeClass)
    elements = cc.Members
End If
Try
    For Each ce In elements
        If (TypeOf ce Is CodeNamespace) Or (TypeOf ce Is CodeClass) Then
            GetClass(ce, list)
        End If
        If (TypeOf ce Is CodeFunction) Then
            list.Add(ce)
        End If
    Next
Catch
End Try
End Sub

答案 7 :(得分:0)

这是一种方法(我警告你这是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);
}