我可以在包含的代码中设置断点吗?

发布于 2024-08-17 18:53:44 字数 571 浏览 2 评论 0原文

我有类似于以下代码片段的内容:

// file alpha.c
void function_A()
{
    int i;
    #include "code.h"
}

并且

//file beta.c
void function_B()
{
    int i;
    #include "code.h"
}

// file code.h
for(i = 1;i < 10;i++)
{
    // do some stuff
    if (very_rare_event)
    {
        // do something else
    }
}

的,我完全意识到它破坏了各种编码实践,但那是另一个故事(它是一个非常大,非常复杂,非常旧的系统的所有部分,并且它不会很快就会改变)。

我的问题是:是否可以在 code.h 中的“// do Something else”点处设置断点,以便您可以指定它是从 alpha.c 还是 beta.c 包含的实例?如果是这样,怎么办?

I have something analogous to the following code snippets:

// file alpha.c
void function_A()
{
    int i;
    #include "code.h"
}

and

//file beta.c
void function_B()
{
    int i;
    #include "code.h"
}

and

// file code.h
for(i = 1;i < 10;i++)
{
    // do some stuff
    if (very_rare_event)
    {
        // do something else
    }
}

Yes, I am fully aware that its breaking all sorts of coding practices, but that's another story (its all part of a very large, very complex, very old system and its not going to be changed any time soon).

My question is this: Is it possible to set a breakpoint within code.h at the "// do something else" point, in a way that you can specify whether it is the instance of being included from alpha.c or beta.c? And if so, how?

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

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

发布评论

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

评论(5

老街孤人 2024-08-24 18:53:44

将 #defines 放入主文件中:

// file alpha.c
#define BREAK_FOR_ME 'A'
void function_A()
{
    int i;
    #include "code.h"
}

// file beta.c
#define BREAK_FOR_ME 'B'
void function_B()
{
    int i;
    #include "code.h"
}

然后放入 code.h 中:

// file code.h
for(i = 1;i < 10;i++)
{
    // do some stuff
    if (very_rare_event)
    {

    #ifdef BREAK_FOR_ME
        switch (BREAK_FOR_ME)
        {
        case 'A':
            // set breakpoint here...
            break;

        case 'B':
            // or here...
            break;
        }
    #endif

        // do something else
    }
}

Put #defines in the main files:

// file alpha.c
#define BREAK_FOR_ME 'A'
void function_A()
{
    int i;
    #include "code.h"
}

// file beta.c
#define BREAK_FOR_ME 'B'
void function_B()
{
    int i;
    #include "code.h"
}

Then in code.h:

// file code.h
for(i = 1;i < 10;i++)
{
    // do some stuff
    if (very_rare_event)
    {

    #ifdef BREAK_FOR_ME
        switch (BREAK_FOR_ME)
        {
        case 'A':
            // set breakpoint here...
            break;

        case 'B':
            // or here...
            break;
        }
    #endif

        // do something else
    }
}
鹿港小镇 2024-08-24 18:53:44

好吧,我想用很多不同的方式告诉你为什么需要改变系统,但你已经表示你知道这不好并且不会改变,所以我会尽量避免这样做。

您是否可以按照以下方式做一些事情:

// file alpha.c
void function_A()
{
    int i;
    const int foo = 1;
    #include "code.h"
}

并在 code.h 中使用条件 foo == 1 设置条件断点?做同样的事情,但设置 foo = 2 以使函数 B 在那里中断。

Ok, I would like to tell you in lots of different ways why you need to change the system, but you've indicated that you are aware that this is not good and that it won't change so I'll try to refrain from doing that.

Could you perhaps do something along the lines of this:

// file alpha.c
void function_A()
{
    int i;
    const int foo = 1;
    #include "code.h"
}

and set a conditional breakpoint in code.h with the condition foo == 1 ? Do the same thing but set foo = 2 for function B to break there.

喜你已久 2024-08-24 18:53:44

function_Afunction_B 中设置断点,然后单步执行所包含的代码。然后您可以在需要的地方设置断点。

这假设您已禁用大多数编译器和链接器优化。

Set a breakpoint in function_A or function_B, then Step Into the included code. You can then set a breakpoint where you need it.

This assumes that you've disabled most compiler and linker optimizations.

只是我以为 2024-08-24 18:53:44

在 .c 文件中使用 #define,在 .h 文件中使用 #ifdef。将一些非操作(可能递增然后递减计数器)放入 #ifdef 中,并在那里设置断点。请注意,您需要在那里进行一些操作,因为您只能在实际操作上设置断点。

所以:

// file alpha.c
#define FILE_ALPHA
void function_A()
{
    int i;
    #include "code.h"
}
#undef FILE_ALPHA

//file beta.c
#define FILE_BETA
void function_B()
{
    int i;
    #include "code.h"
}
#undef FILE_BETA

// file code.h
for(i = 1;i < 10;i++)
{
    // do some stuff
    if (very_rare_event)
    {
        // do something else
        #if defined FILE_ALPHA
            i++; //Set breakpoint here...
            i--;
        #elif defined FILE_BETA
            i++; //...or here...
            i--;
        #else
            i++; //...or here.
            i--;
        #endif
    }
}

Use #defines in your .c files, and #ifdefs in the .h file. Put some non-operation (perhaps increment then decrement a counter) inside the #ifdef, and set your breakpoint there. Note that you need to have some operation in there, as you can only set a breakpoint on an actual operation.

So:

// file alpha.c
#define FILE_ALPHA
void function_A()
{
    int i;
    #include "code.h"
}
#undef FILE_ALPHA

//file beta.c
#define FILE_BETA
void function_B()
{
    int i;
    #include "code.h"
}
#undef FILE_BETA

// file code.h
for(i = 1;i < 10;i++)
{
    // do some stuff
    if (very_rare_event)
    {
        // do something else
        #if defined FILE_ALPHA
            i++; //Set breakpoint here...
            i--;
        #elif defined FILE_BETA
            i++; //...or here...
            i--;
        #else
            i++; //...or here.
            i--;
        #endif
    }
}
岁月无声 2024-08-24 18:53:44

由于类似这样的东西可能很少需要,因此弄清楚如何在 C 源代码级别上完成它或跳过一堆麻烦可能是没有代价的。

因此,我可能只会查看我想要中断的函数的反汇编,并在汇编级别设置断点。

作为实验,我通常在 code.h 中的行上设置一个断点,调试器的“断点”窗口会显示一个断点树:

o  code.h, line 9
|
+-- o   code.h, line 9
|
+-- o   code.h, line 9

每个条目都可以被禁用。没有简单的方法可以通过查看来找出哪个对应于 function_A() 哪个对应于 function_B() - 我必须在启用两者的情况下运行程序,并关闭第一次点击时我不感兴趣的那个。这不是一个很好的可用性体验,但考虑到这种情况可能非常罕见,也不足为奇。

如果您需要更频繁地执行此操作,Visual C/C++ 应该允许您使用 "Context Operator"

{[function],[source],[module] } location

但我一直无法弄清楚如何在调试器 IDE 中实际使用此语法。如果有人有详细信息,我将不胜感激。

Windows 调试工具中的 WinDBG 也支持此语法(或其他语法)非常相似),但我目前无法确切验证它是如何(或是否)工作的)。

Since something like this is probably needed only rarely, it probably wouldn't pay to figure out how to do it at the C source level or to jump through a bunch of hoops.

So, I'd probably just look at the disassembly for the function I wanted to break in and set the breakpoint at the assembly level.

As an experiment, I set a breakpoint on the line in code.h normally, and the debugger's Breakpoint window showed up with a tree of breakpoints:

o  code.h, line 9
|
+-- o   code.h, line 9
|
+-- o   code.h, line 9

Each entry could be disabled. There was no easy way to figure out which one corresponded to function_A() and which was for function_B() just by looking - I had to run the program with both enabled, and turn off the one I wasn't interested in when it was first hit. Not a great usability experience, but not too surprising given how rare the situation probably is.

If you need to do this more regularly, Visual C/C++ is supposed to let you fully specify a breakpoint using a "Context Operator":

{[function],[source],[module] } location

but I haven't been able to figure out how to actually use this syntax in the debugger IDE. If anyone has details, I'd appreciate a comment.

WinDBG in the Debugging tools for Windows also supports this syntax (or something very similar), but I can't verify exactly how (or if) it works at the moment).

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