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

发布于 2024-07-05 19:34:55 字数 29 浏览 6 评论 0原文

有宏可以做到吗? 使用哪些 DTE 对象?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

假面具 2024-07-12 19:34:55

以下是 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

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

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
最舍不得你 2024-07-12 19:34:55

(这不完全是您所要求的,但几乎是:)

您可以通过调出新断点类的每个成员函数上放置断点> 对话框并输入:

CMyClass::*

请参阅 链接 了解更多详细信息。

(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:

CMyClass::*

See Link for more details.

你的笑 2024-07-12 19:34:55

就像康斯坦丁的方法一样......这似乎是windbg的领域。

既然你有cpp,(即使你没有,你也可以编写一些脚本来解决),使用 logger Windows调试工具的一部分...这是一个非常方便的工具,可惜很少有人使用它。

记录器轻松调试 C/COM/C++,具有丰富的符号信息、钩子/分析/灵活的检测;

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

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

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.

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

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

回忆追雨的时光 2024-07-12 19:34:55

我不知道要使用什么 DTE 函数,但您可以非常简单地录制一个几乎可以做到这一点的宏:

  1. 转到文件顶部
  2. ctrl - shift - R(开始记录)
  3. ctrl - I(增量搜索)
  4. { (搜索第一个 { 字符)。
  5. F9(设置断点)
  6. ctrl - ](转到匹配的 } 字符)
  7. ctrl - shift - R(停止录制)

现在只需一遍又一遍地运行此命令(ctrl - 反复shift P)直到到达文件末尾。

如果有命名空间,则将4.改为:

  1. ((在函数定义开头搜索“(”)
  2. esc(停止增量搜索)
  3. ctrl - I(再次增量搜索)
  4. {(函数体开始)

这种可以无限修改以适合您的代码库

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

  1. Go to the top of the file
  2. ctrl - shift - R (start recording)
  3. ctrl - I (incremental search)
  4. { (search for the first { character).
  5. F9 (set breakpoint)
  6. ctrl - ] (go to matching } character)
  7. ctrl - shift - R (stop recording)

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

If you have namespaces, then change 4. to:

  1. ( (search for "(" at the start of the function definition)
  2. esc (stop incremental search)
  3. ctrl - I (incremental search again)
  4. { (start of function body)

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

Hello爱情风 2024-07-12 19:34:55

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

There is a macro, but I tested it only with 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
飘落散花 2024-07-12 19:34:55

下面是在 WinDbg 中实现类似功能的方法:

bm mymodule!CSpam::*

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

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

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.

jJeQQOZ5 2024-07-12 19:34:55

这是一种方法(我警告你这很糟糕):

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);
}

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);
}
红颜悴 2024-07-12 19:34:55

将其放在文件顶部:

#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

文件顶部的行。

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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文