吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 4351|回复: 3
收起左侧

[C&C++ 转载] 【笔记】线程的context结构

[复制链接]
2016wapjpc 发表于 2017-7-23 01:54
    每个线程内核对象都维护着一个CONTEXT结构,里面保存了线程运行的状态,线程也就是eip。
    使得CPU可以记得上次运行该线程运行到哪里了,该从哪里开始运行,该线程内部数据如何如何。
该结构是与CPU有关的,特定的CPU对应着特定的CONTEXT结构。
x86类型CPU对应的CONTEXT结构文档如下:
typedef struct _CONTEXT {

    //
    // The flag values within this flag control 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 thread's context, only that
    // portion of the thread's context will be modified.
    //
    // If the context record is used as an IN OUT parameter to capture
    // the context of a thread, only those portions of the thread's
    // context corresponding to set flags will be returned.
    //
    // The context record is never used as an OUT only parameter.
    //

    DWORD ContextFlags;

    //
    // This section is specified/returned if CONTEXT_DEBUG_REGISTERS is
    // set in ContextFlags. Note that CONTEXT_DEBUG_REGISTERS is NOT
    // included in CONTEXT_FULL.
    //

    DWORD   Dr0;
    DWORD   Dr1;
    DWORD   Dr2;
    DWORD   Dr3;
    DWORD   Dr6;
    DWORD   Dr7;

    //
    // This section is specified/returned if the
    // ContextFlags word contains the flag CONTEXT_FLOATING_POINT.
    //

    FLOATING_SAVE_AREA FloatSave;

    //
    // This section is specified/returned if the
    // ContextFlags word contains the flag CONTEXT_SEGMENTS.
    //

    DWORD   SegGs;
    DWORD   SegFs;
    DWORD   SegEs;
    DWORD   SegDs;

    //
    // This section is specified/returned if the
    // ContextFlags word contains the flag CONTEXT_INTEGER.
    //

    DWORD   Edi;
    DWORD   Esi;
    DWORD   Ebx;
    DWORD   Edx;
    DWORD   Ecx;
    DWORD   Eax;

    //
    // This section is specified/returned if the
    // ContextFlags word contains the flag CONTEXT_CONTROL.
    //

    DWORD   Ebp;
    DWORD   Eip;
    DWORD   SegCs;              // MUST BE SANITIZED
    DWORD   EFlags;             // MUST BE SANITIZED
    DWORD   Esp;
    DWORD   SegSs;

    //
    // This section is specified/returned if the ContextFlags word
    // contains the flag CONTEXT_EXTENDED_REGISTERS.
    // The format and contexts are processor specific
    //

    BYTE    ExtendedRegisters[MAXIMUM_SUPPORTED_EXTENSION];

} CONTEXT;
可见,该CONTEXT结构中,保存着直接和CPU有关的信息:

1、ContextFlags,在查询的时候需要设置该字段,表示查询哪些其他的CONTEXT结构字段。

2、调试寄存器组

3、FLOATING_SAVE_AREA FloatSave —— 浮点寄存器

4、段寄存器

5、通用数据寄存器(整型寄存器)组

6、控制寄存器组——比如CS、BP、SP之类的保存基址指针和堆栈指针、程序计数器。

7、BYTE ExtendedRegisters[MAXIMUM_SUPPORTED_EXTENSION] —— 展寄存器组

可以查询CONTEXT结构中的内容。如上所述,通过设置CONTEXT中的ContextFlags字段,选择要查询的内容:

1、CONTEXT_DEBUG_REGISTERS,查询调式寄存器

2、CONTEXT_FLOATING_POINT,查询浮点寄存器

3、CONTEXT_SEGMENTS,查询段寄存器

4、CONTEXT_INTEGER,查询通用数据寄存器

5、CONTEXT_CONTROL,查询控制寄存器组

6、CONTEXT_EXTENDED_REGISTERS,扩展寄存器组


在查询之前,先呼叫SuspendThread函数暂停一个线程的执行,然后呼叫GetThreadContext函数取得CONTEXT结构中相关内容。

下面代码查询一个线程的控制寄存器组信息:



CONTEXT Context;
SuspendThread(hThread);   
Context.ContextFlags = CONTEXT_CONTROL;
GetThreadContext(hThread, &Context);   
Context.Eip = 0x00010000;     
Context.ContextFlags = CONTEXT_CONTROL;     
SetThreadContext(hThread, &Context);   
ResumeThread(hThread);   

也就是说线程也是一个结构体,只是在高2g的内存中,我们无法访问,线程也就是eip,当20毫秒的时间片用完时,切换到其它线程,
当运行完时,在context结构中取出保存的第一个线程执行结束时的状态,继续执行。

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

馨香迎怀袖 发表于 2017-7-23 06:11 来自手机
楼主辛苦了,谢谢
6767 发表于 2017-7-23 08:36 来自手机
bxb6077095 发表于 2017-9-7 17:23
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-3-29 05:12

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表