annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
1 /* Ddbg - Win32 Debugger for the D programming language
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
2 * Copyright (c) 2007 Jascha Wetzel
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
3 * All rights reserved. See LICENSE.TXT for details.
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
4 */
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
5 module dbgthread;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
6
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
7 import util;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
8
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
9 import win32.winbase;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
10 import win32.windef;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
11
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
12 /**************************************************************************************************
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
13
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
14 **************************************************************************************************/
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
15 class DbgThread
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
16 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
17 HANDLE handle;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
18 uint id,
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
19 stack_base;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
20
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
21 /**********************************************************************************************
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
22
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
23 **********************************************************************************************/
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
24 this(HANDLE h, uint i)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
25 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
26 handle = h;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
27 id = i;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
28
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
29 // get current esp as stack base
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
30 CONTEXT ctx;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
31 if ( getContext(ctx, CONTEXT_CONTROL) )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
32 stack_base = ctx.Esp;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
33 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
34
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
35 int priority()
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
36 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
37 return GetThreadPriority(handle);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
38 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
39
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
40 bool priorityBoost()
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
41 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
42 int boost;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
43 GetThreadPriorityBoost(handle, &boost);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
44 return cast(bool)boost;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
45 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
46
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
47 void times(out ulong creation, out ulong exit, out ulong kernel, out ulong user)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
48 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
49 GetThreadTimes(handle, cast(FILETIME*)&creation, cast(FILETIME*)&exit, cast(FILETIME*)&kernel, cast(FILETIME*)&user);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
50 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
51
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
52 uint suspendCount()
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
53 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
54 uint count = suspend;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
55 resume;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
56 return count;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
57 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
58
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
59 uint suspend()
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
60 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
61 return SuspendThread(handle);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
62 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
63
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
64 uint resume()
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
65 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
66 return ResumeThread(handle);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
67 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
68
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
69 /**********************************************************************************************
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
70
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
71 **********************************************************************************************/
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
72 bool getContext(out CONTEXT ctx, uint flags=CONTEXT_FULL)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
73 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
74 ctx.ContextFlags = flags;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
75 suspend;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
76 bool res = cast(bool)GetThreadContext(handle, &ctx);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
77 resume;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
78 return res;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
79 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
80
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
81 /**********************************************************************************************
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
82
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
83 **********************************************************************************************/
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
84 bool setContext(ref CONTEXT ctx)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
85 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
86 suspend;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
87 auto res = cast(bool)SetThreadContext(handle, &ctx);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
88 resume;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
89 return res;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
90 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
91
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
92 /**********************************************************************************************
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
93
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
94 **********************************************************************************************/
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
95 size_t getDsBase()
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
96 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
97 CONTEXT ctx;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
98 getContext(ctx);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
99
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
100 LDT_ENTRY sel_entry;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
101 GetThreadSelectorEntry(handle, ctx.SegDs, &sel_entry);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
102 return (cast(size_t)sel_entry.BaseHi << 24) | (cast(size_t)sel_entry.BaseMid << 16) | cast(size_t)sel_entry.BaseLow;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
103 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
104
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
105 /**********************************************************************************************
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
106 Wrapper function for changing the thread's context. Calls the
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
107 modify(ctx) delegate that can do the actual change.
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
108 Returns: success
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
109 **********************************************************************************************/
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
110 bool changeContext(void delegate(ref CONTEXT ctx) modify, uint flags=CONTEXT_FULL)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
111 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
112 // if ( 0 > SuspendThread(thread) )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
113 // {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
114 // DbgIO.println("ERROR: Couldn't SuspendThread to reset instruction pointer: %s", getLastError);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
115 // return false;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
116 // }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
117
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
118 CONTEXT ctx;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
119 if ( getContext(ctx, flags) )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
120 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
121 modify(ctx);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
122 if ( !SetThreadContext(handle, &ctx) )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
123 DbgIO.println("ERROR: Couldn't SetThreadContext to reset instruction pointer: %s", lastError);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
124 } else
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
125 DbgIO.println("ERROR: Couldn't GetThreadContext to reset instruction pointer: %s", lastError);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
126
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
127 // if ( 0 > ResumeThread(thread) )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
128 // {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
129 // DbgIO.println("ERROR: Couldn't ResumeThread to reset instruction pointer: %s", getLastError);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
130 // return false;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
131 // }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
132
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
133 return true;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
134 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
135 }