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

时间:2023-01-14 18:57:29

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

有没有一个宏呢?使用哪个DTE对象?

8 个解决方案

#1


18  

(This is not quite what you're asking for, but almost:)

(这不是你要求的,但差不多:)

You can put a breakpoint on every member function of a class in Visual Studio by bringing up the New Breakpoint dialog and entering:

您可以通过调出New Breakpoint对话框并输入以下内容,在Visual Studio中的类的每个成员函数上放置一个断点:

CMyClass::*

See 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 for more details.

见http://blogs.msdn.com/b/habibh/archive/2009/09/10/class-breakpoint-how-to-set-a-breakpoint-on-ac-class-in-the-visual-studio -debugger.aspx了解更多详情。

#2


5  

Here's a quick implementation of 1800 INFORMATION's idea:

这是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

#3


2  

I don't know what DTE functions to use, but you could very simply record a macro that could pretty much do it:

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

  1. Go to the top of the file
  2. 转到文件顶部

  3. ctrl - shift - R (start recording)
  4. ctrl - shift - R(开始录制)

  5. ctrl - I (incremental search)
  6. ctrl - I(增量搜索)

  7. { (search for the first { character).
  8. {(搜索第一个{字符)。

  9. F9 (set breakpoint)
  10. F9(设定断点)

  11. ctrl - ] (go to matching } character)
  12. ctrl - ](转到匹配}字符)

  13. ctrl - shift - R (stop recording)
  14. ctrl - shift - R(停止录制)

Now just run this over and over (ctrl - shift P repeatedly) until you reach the end of the file.

现在只需反复运行(ctrl - 反复移位P),直到到达文件末尾。

If you have namespaces, then change 4. to:

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

  1. ( (search for "(" at the start of the function definition)
  2. ((在函数定义的开头搜索“(”)

  3. esc (stop incremental search)
  4. esc(停止增量搜索)

  5. ctrl - I (incremental search again)
  6. ctrl - I(再次增量搜索)

  7. { (start of function body)
  8. {(功能体的开头)

This kind of thing can be infinitely modified to suit your codebase

这种东西可以无限修改,以适应您的代码库

#4


2  

Like Constantin's method... This seems like windbg territory.

像康斯坦丁的方法......这看起来像windbg领土。

Since you have the cpp, (even if you didn't you could script something to get by), it should be no problem to use logger part of the debugging tools for windows... it's a very handy tool, shame so few people use it.

既然你有cpp,(即使你没有你可以编写脚本的东西),使用Windows的调试工具的记录器部分也应该没问题...这是一个非常方便的工具,很少有人感到羞耻用它。

logger debug's C/COM/C++ easily, with rich symbolic info, hooks/profiling/flexible instrumentation;

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."

激活Logger的一种方法是启动CDB或WinDbg并像往常一样连接到用户模式目标应用程序。然后,使用!logexts.logi或!logexts.loge扩展命令。这将在当前断点处插入代码,该代码将跳转到在目标应用程序进程中加载​​和初始化Logexts.dll的例程。这被称为“将Logger注入目标应用程序”。

#5


1  

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

以下是WinDbg中可以实现的类似方法:

bm mymodule!CSpam::*

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

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

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

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

#6


0  

Put this at the top of the file:

把它放在文件的顶部:

#define WANT_BREAK_IN_EVERY_FUNCTION

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

then insert DEBUG_BREAK in the beginning of every function, like this:

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

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

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

When you no longer want the debug breaks, comment the line

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

// #define WANT_BREAK_IN_EVERY_FUNCTION

at the top of the file.

在文件的顶部。

#7


0  

There is a macro, but I tested it only with c#.

有一个宏,但我只用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

#8


0  

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

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

#1


18  

(This is not quite what you're asking for, but almost:)

(这不是你要求的,但差不多:)

You can put a breakpoint on every member function of a class in Visual Studio by bringing up the New Breakpoint dialog and entering:

您可以通过调出New Breakpoint对话框并输入以下内容,在Visual Studio中的类的每个成员函数上放置一个断点:

CMyClass::*

See 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 for more details.

见http://blogs.msdn.com/b/habibh/archive/2009/09/10/class-breakpoint-how-to-set-a-breakpoint-on-ac-class-in-the-visual-studio -debugger.aspx了解更多详情。

#2


5  

Here's a quick implementation of 1800 INFORMATION's idea:

这是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

#3


2  

I don't know what DTE functions to use, but you could very simply record a macro that could pretty much do it:

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

  1. Go to the top of the file
  2. 转到文件顶部

  3. ctrl - shift - R (start recording)
  4. ctrl - shift - R(开始录制)

  5. ctrl - I (incremental search)
  6. ctrl - I(增量搜索)

  7. { (search for the first { character).
  8. {(搜索第一个{字符)。

  9. F9 (set breakpoint)
  10. F9(设定断点)

  11. ctrl - ] (go to matching } character)
  12. ctrl - ](转到匹配}字符)

  13. ctrl - shift - R (stop recording)
  14. ctrl - shift - R(停止录制)

Now just run this over and over (ctrl - shift P repeatedly) until you reach the end of the file.

现在只需反复运行(ctrl - 反复移位P),直到到达文件末尾。

If you have namespaces, then change 4. to:

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

  1. ( (search for "(" at the start of the function definition)
  2. ((在函数定义的开头搜索“(”)

  3. esc (stop incremental search)
  4. esc(停止增量搜索)

  5. ctrl - I (incremental search again)
  6. ctrl - I(再次增量搜索)

  7. { (start of function body)
  8. {(功能体的开头)

This kind of thing can be infinitely modified to suit your codebase

这种东西可以无限修改,以适应您的代码库

#4


2  

Like Constantin's method... This seems like windbg territory.

像康斯坦丁的方法......这看起来像windbg领土。

Since you have the cpp, (even if you didn't you could script something to get by), it should be no problem to use logger part of the debugging tools for windows... it's a very handy tool, shame so few people use it.

既然你有cpp,(即使你没有你可以编写脚本的东西),使用Windows的调试工具的记录器部分也应该没问题...这是一个非常方便的工具,很少有人感到羞耻用它。

logger debug's C/COM/C++ easily, with rich symbolic info, hooks/profiling/flexible instrumentation;

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."

激活Logger的一种方法是启动CDB或WinDbg并像往常一样连接到用户模式目标应用程序。然后,使用!logexts.logi或!logexts.loge扩展命令。这将在当前断点处插入代码,该代码将跳转到在目标应用程序进程中加载​​和初始化Logexts.dll的例程。这被称为“将Logger注入目标应用程序”。

#5


1  

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

以下是WinDbg中可以实现的类似方法:

bm mymodule!CSpam::*

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

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

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

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

#6


0  

Put this at the top of the file:

把它放在文件的顶部:

#define WANT_BREAK_IN_EVERY_FUNCTION

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

then insert DEBUG_BREAK in the beginning of every function, like this:

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

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

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

When you no longer want the debug breaks, comment the line

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

// #define WANT_BREAK_IN_EVERY_FUNCTION

at the top of the file.

在文件的顶部。

#7


0  

There is a macro, but I tested it only with c#.

有一个宏,但我只用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

#8


0  

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

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