如何调试通过 Windows Scheduler 运行的 exe?

发布于 2024-07-14 08:50:18 字数 172 浏览 10 评论 0原文

我正在使用 Windows Scheduler 来运行我编写的 exe。

当调度程序启动我的 exe 时,如何跳入调试会话?

更新 1。 我曾想过执行 Thread.Sleep 然后附加到进程。 当我尝试时,它说调试器已经附加到进程......

I'm using Windows Scheduler to run an exe I have written.

How can I jump into a debug session when the scheduler starts my exe?

Update 1.
I had thought of doing a Thread.Sleep and then Attach to Process. When I tried it, it says Debugger is already attached to process...

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

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

发布评论

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

评论(4

段念尘 2024-07-21 08:50:18

您只需在程序中调用 DebugBreak() 即可。

根据 MSDN 页面,DebugBreak 执行以下操作:下列的:

导致断点异常发生
在当前进程中。 这允许
调用线程发出信号
调试器来处理异常。

导致断点异常
另一个过程,使用
DebugBreakProcess 函数。

然后,您可以在此时附加调试器,并继续运行程序。

此解决方案的唯一问题是您需要将代码中的 DebugBreak() 设置为有条件的,这样它就不会在程序每次运行时都中断。 也许您可以通过环境变量、注册表设置或调度程序传递给程序以确保程序在启动时中断的参数来实现此目的。

示例代码

下面是一些读取环境变量的未经测试的示例代码:

int main()
{
    char *debugBreakChar = getenv("DEBUG_BREAK");
    int debugBreak = atoi(debugBreakChar);
    if (debugBreak)
    {
        DebugBreak();
    }

    // Rest of the program follows here
}

现在您需要做的就是将环境变量设置为系统变量,并确保可以从与调度程序相同的 shell 上下文中访问它(重新启动将实现此目的):

set DEBUG_BREAK=1

现在程序将在启动时中断,允许您附加调试器。 将环境变量更改为0,或者取消设置,程序就可以正常运行。

在这方面,环境变量有点复杂,因为它们是基于上下文的,并且您需要知道调度程序是从相同的环境上下文运行的。 注册表值比这更好,您可以使用 RegQueryValueEx 在您的代码中(您需要包含 windows.h 才能使用此函数)。

You could just call DebugBreak() from within your program.

According to the MSDN page, DebugBreak does the following:

Causes a breakpoint exception to occur
in the current process. This allows
the calling thread to signal the
debugger to handle the exception.

To cause a breakpoint exception in
another process, use the
DebugBreakProcess function.

You can then attach your debugger at this point, and continue running the program.

The only problem with this solution is that you need to make the DebugBreak() in the code conditional, so that it won't break every time the program is run. Maybe you achieve this through an environment variable, registry setting, or a parameter which the scheduler passes in to the program to ensure that it breaks when it starts up.

Example code

Here's some untested example code reading an environment variable:

int main()
{
    char *debugBreakChar = getenv("DEBUG_BREAK");
    int debugBreak = atoi(debugBreakChar);
    if (debugBreak)
    {
        DebugBreak();
    }

    // Rest of the program follows here
}

Now all you need to do is set the environment variable as a system variable, and ensure that it's accessible from the same shell context as the scheduler (rebooting will achieve this):

set DEBUG_BREAK=1

Now the program will break on startup, allowing you to attach a debugger. Changing the environment variable to 0, or un-setting it, will allow the program to run normally.

Environment variables are a bit fiddly in this regard, as they are context-based and you need to know that the scheduler runs from the same environmental context. Registry values are better than this, and you can read a registry value using RegQueryValueEx in your code instead (you'll need to include windows.h to use this function).

明媚殇 2024-07-21 08:50:18

附加到进程可以工作(在 Visual Studio 中),但如果它是一个快速进程,您可能需要在代码开头添加一个睡眠语句,以便您可以在它启动主逻辑之前附加。

Attach to Process will work (from within Visual Studio), although you may need to add a sleep statement at the beginning of your code if it is a fast process so that you can attach before it starts your main logic.

相权↑美人 2024-07-21 08:50:18

您可以在 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options 下设置一个键,以便在进程启动时将调试器附加到进程。 您可以在此知识库文章中了解如何执行此操作。

这种方法有几个问题:

要使用 VS 进行调试,您需要在可执行文件的 IFEO 选项中实际指定 VSJitDebugger.exe。 您还必须手动指定要使用的调试引擎。 更多详细信息请参见此处

You can set a key under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options that will attach debugger to the process when the process is launched. You can read how to do this in this KB Article.

There are couple of gotchas with this approach:

To debug using VS, you need to actually specifyg VSJitDebugger.exe in the IFEO options for your executable. You will also have to specify the debugging engine to use manually. More details here.

铁憨憨 2024-07-21 08:50:18

Visual Studio 的“调试”菜单中的“附加到进程”。

"Attach to Process" in Visual Studio's Debug menu.

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