view src/dbgthread.d @ 1:4a9dcbd9e54f

-files of 0.13 beta -fixes so that it now compiles with the current dmd version
author marton@basel.hu
date Tue, 05 Apr 2011 20:44:01 +0200
parents
children
line wrap: on
line source

/*  Ddbg - Win32 Debugger for the D programming language
 *  Copyright (c) 2007 Jascha Wetzel
 *  All rights reserved. See LICENSE.TXT for details.
 */
module dbgthread;

import util;

import win32.winbase;
import win32.windef;

/**************************************************************************************************

**************************************************************************************************/
class DbgThread
{
	HANDLE	handle;
	uint	id,
			stack_base;

    /**********************************************************************************************

    **********************************************************************************************/
	this(HANDLE h, uint i)
	{
		handle = h;
		id = i;

		// get current esp as stack base
		CONTEXT ctx;
		if ( getContext(ctx, CONTEXT_CONTROL) )
			stack_base = ctx.Esp;
	}

    int priority()
    {
        return GetThreadPriority(handle);
    }

    bool priorityBoost()
    {
        int boost;
        GetThreadPriorityBoost(handle, &boost);
        return cast(bool)boost;
    }

    void times(out ulong creation, out ulong exit, out ulong kernel, out ulong user)
    {
        GetThreadTimes(handle, cast(FILETIME*)&creation, cast(FILETIME*)&exit, cast(FILETIME*)&kernel, cast(FILETIME*)&user);
    }

    uint suspendCount()
    {
        uint count = suspend;
        resume;
        return count;
    }

    uint suspend()
    {
        return SuspendThread(handle);
    }

    uint resume()
    {
        return ResumeThread(handle);
    }

    /**********************************************************************************************

    **********************************************************************************************/
	bool getContext(out CONTEXT ctx, uint flags=CONTEXT_FULL)
	{
		ctx.ContextFlags = flags;
		suspend;
		bool res = cast(bool)GetThreadContext(handle, &ctx);
		resume;
		return res;
	}

    /**********************************************************************************************

    **********************************************************************************************/
	bool setContext(ref CONTEXT ctx)
	{
		suspend;
		auto res = cast(bool)SetThreadContext(handle, &ctx);
        resume;
        return res;
	}

    /**********************************************************************************************

    **********************************************************************************************/
    size_t getDsBase()
    {
        CONTEXT ctx;
        getContext(ctx);

        LDT_ENTRY sel_entry;
        GetThreadSelectorEntry(handle, ctx.SegDs, &sel_entry);
        return (cast(size_t)sel_entry.BaseHi << 24) | (cast(size_t)sel_entry.BaseMid << 16) | cast(size_t)sel_entry.BaseLow;
    }

    /**********************************************************************************************
        Wrapper function for changing the thread's context. Calls the
        modify(ctx) delegate that can do the actual change.
        Returns: success
    **********************************************************************************************/
	bool changeContext(void delegate(ref CONTEXT ctx) modify, uint flags=CONTEXT_FULL)
	{
//		if ( 0 > SuspendThread(thread) )
//		{
//			DbgIO.println("ERROR: Couldn't SuspendThread to reset instruction pointer: %s", getLastError);
//			return false;
//		}

		CONTEXT ctx;
		if ( getContext(ctx, flags) )
		{
			modify(ctx);
			if ( !SetThreadContext(handle, &ctx) )
				DbgIO.println("ERROR: Couldn't SetThreadContext to reset instruction pointer: %s", lastError);
		} else
			DbgIO.println("ERROR: Couldn't GetThreadContext to reset instruction pointer: %s", lastError);

//		if ( 0 > ResumeThread(thread) )
//		{
//			DbgIO.println("ERROR: Couldn't ResumeThread to reset instruction pointer: %s", getLastError);
//			return false;
//		}

		return true;
	}
}