线程的上下文保存在哪里,是否可以通过编程方式访问它(无需修改内核)?

Where is a thread's context saved and can it be accessed programmatically (without modifying the kernel)?

Windows Context Switching

The scheduler maintains a queue of executable threads for each priority level. These are known as ready threads. When a processor becomes available, the system performs a context switch. The steps in a context switch are:

  • Save the context of the thread that just finished executing.
  • Place the thread that just finished executing at the end of the queue for its priority.
  • Find the highest priority queue that contains ready threads.
  • Remove the thread at the head of the queue, load its context, and execute it.

我对这个话题还不是很了解,所以我不知道如何详细说明我的问题。线程的上下文保存在哪里,是否可以通过编程方式(不修改内核)访问(编辑:读取)?

如果您拥有具有所需访问权限的线程句柄,您可以挂起该线程,然后调用 GetThreadContext。当一个线程是 运行 时,值在真正的 CPU 寄存器中,当它不是 运行 时,上下文存储在用户模式无法访问的内存中。

上下文存储各种 CPU 寄存器的值,它仅对调试器和代码注入和错误记录等高级功能有用。