重新启动Windows进程就地保留进程ID和句柄 [英] Restart Windows process inplace preserving process ID and handles

查看:222
本文介绍了重新启动Windows进程就地保留进程ID和句柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Windows可执行文件,用作某些嵌入式设备的仿真器(所有业务逻辑与原始设备上的完全相同,并且仅存有与硬件相关的内容).

I have created a windows executable that serves as a simulaator for some embedded device (all the business logic is exactly the same as on original device and only HW related stuff is stubbed).

此仿真需要不时重置,在正常"用例中,它会执行以下操作:

This simulation needs to reset from time to time, and in "normal" use case it does something like that:

//some global environment
...

int main(int argc, char* argv[])
{
    __debugbreak();

    //... do some stuff 

    //if( restart needed ){
        printf("before _execv");
        _execv(argv[0], argv); //"reset" simulated device
    //}

    //... do some other testing stuff
    return 0;
}

注意:上面的代码只是为了说明主要思想,在实际应用中,execv调用实际上位于HW_Reset()存根中,该存根在原始代码中的多个位置被调用.

Note: code above is to just illustrate main idea, in real application that execv call is actually located in HW_Reset() stub, that is called from multiple places in original code.

问题是Windows上的_execv与Linux上的execv行为不完全相同: 当我在Visual Studio中调试此应用程序时,_execv不会将当前过程映像替换为重新启动"映像.相反,它只是创建一个具有新ID的新进程并终止当前进程,从而导致将其与Visual Studio分离,因此,为了保留所有断点,我需要一次又一次地重新连接到该新进程(在单个调试会话中有数十次重新启动) ).

The problem is that _execv on Windows does not behave exactly as execv on Linux: when I debug this application in Visual Studio the _execv does not replace current process image with "restarted" image. Instead it just creates a new process with new ID and terminates current process, causing detaching it from Visual Studio, so, to preserve all the breakpoints I need to reattach to that new process again and again (there are dozens of restarts in single debug session).

当前,我使用__debugbreak()作为解决方法. 另一种选择是通过重新初始化全局环境并使用setjmp/longjmp的某种组合来重置模拟-但是全局环境和相应的初始化器会散布在原始文件的数千个中,并且大多数都是静态的,因此无法手动处理此类重置(同样,我也不允许编辑原始文件.)

Currently I use __debugbreak() as a workaround. Other option is to reset simulation by reinitializing global environment and using some combination of setjmp/longjmp - but global environment and corresponding initializers are spread through thousends of original files, and most of them are static, so it is not possible to handle such reset manually (also I'm not allowed to edit original files).

所以问题是:是否存在一些Windows API/通用解决方法,通过重置所有全局(和静态)变量,导致当前进程重新启动就地",例如在可能的情况下要将相同的过程映像重新加载到相同的地址空间中,保留向外可观察到的过程ID,过程句柄以及与Visual Studio调试器的连接?

So the question is: is there some Windows API / generic workaround that causes current process to restart "inplace" by resetting all the global (and static) variables, like in case if it was possible to reload the same process image inside the same address space, preserving outwardly observable process ID, process handle and connection to visual studio debugger?

推荐答案

Windows不支持您的要求.您将必须重组您的main()代码以使其循环运行,例如:

Windows does not support what you are asking for. You will have to restructure your main() code to run in a loop instead, eg:

//some global environment
...

int main(int argc, char* argv[])
{
    __debugbreak();

    do
    {
        //... (re)initialize simulated device
        //... do some stuff
    }
    while (restart needed);

    //... do some other testing stuff
    return 0;
}

这篇关于重新启动Windows进程就地保留进程ID和句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆