【问题标题】:c++ & Context structurec++ & 上下文结构
【发布时间】:2010-12-15 19:42:33
【问题描述】:

有人知道我在哪里可以找到有关 C++ 中 CONTEXT 结构的字段的一些信息吗?

【问题讨论】:

  • 你在说这个吗:msdn.microsoft.com/en-us/library/ms679284(VS.85).aspx。如果是,请注意这不完全是 C++,而是 Windows API。与 msdn 文档相关,请检查 winnt.h
  • 是的,但我没有看到有关其字段的任何详细信息...
  • 这些字段对应于处理器寄存器,因此如果您想了解更多详细信息,例如,MMX 寄存器是什么,或者如何解释段寄存器,则需要向您的处理器供应商咨询。对于大多数合理的配置,intel.com/products/processor/manuals 应该可以帮助您,除非您使用的是 Itanium 或其他陌生设备。

标签: c++ windows dbghelp


【解决方案1】:

正如@bcsanches 所说,这是一个 Windows API。它将完全依赖于处理器。在您自己的标题中查找结构。

在我的标题上显示为:

//
// Context Frame
//
//  This frame has a several purposes: 1) it is used as an argument to
//  NtContinue, 2) is is used to constuct a call frame for APC delivery,
//  and 3) it is used in the user level thread creation routines.
//
//
// The flags field within this record controls the contents of a CONTEXT
// record.
//
// If the context record is used as an input parameter, then for each
// portion of the context record controlled by a flag whose value is
// set, it is assumed that that portion of the context record contains
// valid context. If the context record is being used to modify a threads
// context, then only that portion of the threads context is modified.
//
// If the context record is used as an output parameter to capture the
// context of a thread, then only those portions of the thread's context
// corresponding to set flags will be returned.
//
// CONTEXT_CONTROL specifies SegSs, Rsp, SegCs, Rip, and EFlags.
//
// CONTEXT_INTEGER specifies Rax, Rcx, Rdx, Rbx, Rbp, Rsi, Rdi, and R8-R15.
//
// CONTEXT_SEGMENTS specifies SegDs, SegEs, SegFs, and SegGs.
//
// CONTEXT_DEBUG_REGISTERS specifies Dr0-Dr3 and Dr6-Dr7.
//
// CONTEXT_MMX_REGISTERS specifies the floating point and extended registers
//     Mm0/St0-Mm7/St7 and Xmm0-Xmm15).
//

typedef struct DECLSPEC_ALIGN(16) _CONTEXT {

    //
    // Register parameter home addresses.
    //
    // N.B. These fields are for convience - they could be used to extend the
    //      context record in the future.
    //

    DWORD64 P1Home;
    DWORD64 P2Home;
    DWORD64 P3Home;
    DWORD64 P4Home;
    DWORD64 P5Home;
    DWORD64 P6Home;

    //
    // Control flags.
    //

    DWORD ContextFlags;
    DWORD MxCsr;

    //
    // Segment Registers and processor flags.
    //

    WORD   SegCs;
    WORD   SegDs;
    WORD   SegEs;
    WORD   SegFs;
    WORD   SegGs;
    WORD   SegSs;
    DWORD EFlags;

    //
    // Debug registers
    //

    DWORD64 Dr0;
    DWORD64 Dr1;
    DWORD64 Dr2;
    DWORD64 Dr3;
    DWORD64 Dr6;
    DWORD64 Dr7;

    //
    // Integer registers.
    //

    DWORD64 Rax;
    DWORD64 Rcx;
    DWORD64 Rdx;
    DWORD64 Rbx;
    DWORD64 Rsp;
    DWORD64 Rbp;
    DWORD64 Rsi;
    DWORD64 Rdi;
    DWORD64 R8;
    DWORD64 R9;
    DWORD64 R10;
    DWORD64 R11;
    DWORD64 R12;
    DWORD64 R13;
    DWORD64 R14;
    DWORD64 R15;

    //
    // Program counter.
    //

    DWORD64 Rip;

    //
    // Floating point state.
    //

    union {
        XMM_SAVE_AREA32 FltSave;
        struct {
            M128A Header[2];
            M128A Legacy[8];
            M128A Xmm0;
            M128A Xmm1;
            M128A Xmm2;
            M128A Xmm3;
            M128A Xmm4;
            M128A Xmm5;
            M128A Xmm6;
            M128A Xmm7;
            M128A Xmm8;
            M128A Xmm9;
            M128A Xmm10;
            M128A Xmm11;
            M128A Xmm12;
            M128A Xmm13;
            M128A Xmm14;
            M128A Xmm15;
        };
    };

    //
    // Vector registers.
    //

    M128A VectorRegister[26];
    DWORD64 VectorControl;

    //
    // Special debug control registers.
    //

    DWORD64 DebugControl;
    DWORD64 LastBranchToRip;
    DWORD64 LastBranchFromRip;
    DWORD64 LastExceptionToRip;
    DWORD64 LastExceptionFromRip;
} CONTEXT, *PCONTEXT;

但您的平台可能会有所不同。请注意,如果您编写的代码依赖于任何特定版本的 CONTEXT 结构,则当您针对另一个平台(例如尝试从 x64 代码构建 x86 或反之亦然)时,您的代码将无法编译。

你应该把这个结构当作一个不透明的对象,除非你有一个非常好的(注意:大多数原因不是非常好的)理由不这样做。这些东西应该包含在hal.dll、ntoskrnl.exe 和ntdll.dll 的内部,而不是你的代码中。

【讨论】:

  • 比利回答得很好。
【解决方案2】:

它在文档中说得对:

包含特定于处理器的寄存器数据。系统使用 CONTEXT 结构来执行各种内部操作。 请参阅头文件 WinNT.h 了解每个处理器架构的此结构的定义。

所以您需要检查您的平台。在 WinNT.h 中找到定义。

【讨论】:

    【解决方案3】:

    除非您正在用汇编语言编写代码,然后想用汇编语言编写异常处理程序,否则您应该不需要或不需要有关CONTEXT 结构内容的信息。

    如果您确实想用汇编代码编写逻辑并想从 C++ 中调用它,并且您想使用异常处理(例如捕获除以零或 SSE 异常,这可能应该通过数据验证等方案来防止)无论如何),那么您最好将异常处理留给调用您的汇编语言过程的 C++ 代码。

    这样就不会那么混乱了,因为这样您就不必担心 C++ 代码在异常处理方面的作用。让你的汇编代码逻辑工作起来要容易得多,这样它就可以防止抛出异常。除此之外,它应该使您的代码在处理问题方面更加高效和优雅。

    请注意,异常处理机制的发明是为了充当安全网,并作为最后的措施,以防止软件在发生奇怪和意外的事情时简单地崩溃和烧毁。抛出异常应该是异常而不是规则,因此它们被命名为“异常”。

    【讨论】:

      猜你喜欢
      • 2012-12-28
      • 1970-01-01
      • 2019-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-12
      • 2012-10-20
      • 1970-01-01
      相关资源
      最近更新 更多