1
|
1 /* Ddbg - Win32 Debugger for the D programming language
|
|
2 * Copyright (c) 2007 Jascha Wetzel
|
|
3 * All rights reserved. See LICENSE.TXT for details.
|
|
4 */
|
|
5 module dbgprocess;
|
|
6
|
|
7 import win32.winbase;
|
|
8 import win32.windef;
|
|
9
|
|
10 import std.string;
|
|
11 import std.c.string;
|
|
12
|
|
13 import util;
|
|
14 import breakpoint;
|
|
15 import dbgthread;
|
|
16 import callstack;
|
|
17 import codeview.coff;
|
|
18
|
|
19 /**************************************************************************************************
|
|
20
|
|
21 **************************************************************************************************/
|
|
22 class DbgProcess
|
|
23 {
|
|
24 public:
|
|
25 HANDLE process_handle;
|
|
26 uint processId,
|
|
27 mainThreadId;
|
|
28 DLL[] loaded_dlls;
|
|
29
|
|
30 DbgThread[uint] threads;
|
|
31
|
|
32 /**********************************************************************************************
|
|
33
|
|
34 **********************************************************************************************/
|
|
35 DLL loadDLL(LOAD_DLL_DEBUG_INFO* lddi)
|
|
36 {
|
|
37 DLL dll = new DLL;
|
|
38 dll.filehandle = lddi.hFile;
|
|
39 dll.base = cast(uint)lddi.lpBaseOfDll;
|
|
40 dll.debug_info_offset = lddi.dwDebugInfoFileOffset;
|
|
41 dll.debug_info_size = lddi.nDebugInfoSize;
|
|
42 loaded_dlls ~= dll;
|
|
43
|
|
44 size_t filesize = GetFileSize(lddi.hFile, null);
|
|
45 if ( filesize == 0 )
|
|
46 {
|
|
47 debug DbgIO.println("Couldn't get DLL %s image size: %s", dll.image.name, lastError);
|
|
48 return dll;
|
|
49 }
|
|
50
|
|
51 ubyte[] buf;
|
|
52 buf.length = filesize;
|
|
53 if ( !ReadFile(lddi.hFile, cast(void*)buf.ptr, buf.length, &filesize, null) || filesize != buf.length )
|
|
54 {
|
|
55 debug DbgIO.println("Couldn't read DLL image for %s: %s", dll.image.name, lastError);
|
|
56 return dll;
|
|
57 }
|
|
58 dll.image = new COFFImage;
|
|
59 dll.image.load(buf);
|
|
60
|
|
61 return dll;
|
|
62 }
|
|
63
|
|
64 /**********************************************************************************************
|
|
65
|
|
66 **********************************************************************************************/
|
|
67 DLL findDLL(size_t vaddress)
|
|
68 {
|
|
69 foreach ( dll; loaded_dlls )
|
|
70 {
|
|
71 if ( vaddress < dll.base )
|
|
72 continue;
|
|
73 assert( dll !is null );
|
|
74 assert( dll.image !is null );
|
|
75 uint size_image = dll.image.imageSize;
|
|
76 if ( vaddress-dll.base > size_image )
|
|
77 continue;
|
|
78 return dll;
|
|
79 }
|
|
80 return null;
|
|
81 }
|
|
82
|
|
83 /**********************************************************************************************
|
|
84 Loads the given thread's stack and the index of the current frame pointer (ebp).
|
|
85 Returns: Arrays of uints.
|
|
86 **********************************************************************************************/
|
|
87 CallStack loadStack(DbgThread thread)
|
|
88 {
|
|
89 CONTEXT ctx;
|
|
90 if ( !thread.getContext(ctx, CONTEXT_CONTROL) )
|
|
91 throw new Exception("Couldn't get thread's context");
|
|
92
|
|
93 CallStack stack = new CallStack(thread.stack_base, ctx.Esp, ctx.Ebp);
|
|
94 uint read = readProcessMemory(ctx.Esp, stack.data.ptr, stack.data.length);
|
|
95 if ( read == 0 )
|
|
96 throw new Exception("Couldn't read thread's stack memory");
|
|
97 else if ( read < stack.data.length )
|
|
98 stack.data.length = read;
|
|
99 return stack;
|
|
100 }
|
|
101
|
|
102 /**********************************************************************************************
|
|
103 Read from debuggee's memory.
|
|
104 Returns: #bytes read
|
|
105 **********************************************************************************************/
|
|
106 size_t readProcessMemory(size_t address, void* data, size_t size, bool changeProtect=false)
|
|
107 {
|
|
108 uint oldprot;
|
|
109 if( changeProtect && !VirtualProtectEx(process_handle, cast(void*)address, size, PAGE_READONLY, &oldprot) ) {
|
|
110 debug DbgIO.println("readProcessMemory(): Failed to obtain read access to page at 0x%08x: %s", address, lastError);
|
|
111 return false;
|
|
112 }
|
|
113
|
|
114 size_t numbytes;
|
|
115 if( !ReadProcessMemory(process_handle, cast(void*)address, data, size, &numbytes) ) {
|
|
116 debug DbgIO.println("ReadProcessMemory() returned false reading address 0x%08x: %s", address, lastError);
|
|
117 return 0;
|
|
118 }
|
|
119 if ( numbytes != size ) {
|
|
120 debug DbgIO.println("readProcessMemory(): Failed to read at address 0x%08x: %s", address, lastError);
|
|
121 }
|
|
122
|
|
123 if( changeProtect && !VirtualProtectEx(process_handle, cast(void*)address, size, oldprot, &oldprot) ) {
|
|
124 DbgIO.println("writeProcessMemory(): Failed to restore access to page at 0x%08x: %s", address, lastError);
|
|
125 return 0;
|
|
126 }
|
|
127
|
|
128 return numbytes;
|
|
129 }
|
|
130
|
|
131 /**********************************************************************************************
|
|
132 Write to debuggee's memory.
|
|
133 Returns success.
|
|
134 **********************************************************************************************/
|
|
135 size_t writeProcessMemory(size_t address, void* data, size_t size)
|
|
136 {
|
|
137 uint oldprot;
|
|
138
|
|
139 if( !VirtualProtectEx(process_handle, cast(void*)address, size, PAGE_EXECUTE_READWRITE, &oldprot) )
|
|
140 {
|
|
141 DbgIO.println("writeProcessMemory(): Failed to obtain write access to page at 0x%08x: %s", address, lastError);
|
|
142 return false;
|
|
143 }
|
|
144
|
|
145 size_t numbytes;
|
|
146 if( !WriteProcessMemory(process_handle, cast(void*)address, data, size, &numbytes) )
|
|
147 DbgIO.println("writeProcessMemory(): Failed to write byte at 0x%08x: %s", address, lastError);
|
|
148
|
|
149 if( !VirtualProtectEx(process_handle, cast(void*)address, size, oldprot, &oldprot) )
|
|
150 {
|
|
151 DbgIO.println("writeProcessMemory(): Failed to restore access to page at 0x%08x: %s", address, lastError);
|
|
152 return false;
|
|
153 }
|
|
154
|
|
155 if ( !FlushInstructionCache(process_handle, cast(void*)address, numbytes) )
|
|
156 {
|
|
157 DbgIO.println("writeProcessMemory(): FlushInstructionCache failed for 0x%08x: %s", address, lastError());
|
|
158 return false;
|
|
159 }
|
|
160
|
|
161 return numbytes;
|
|
162 }
|
|
163
|
|
164 /**********************************************************************************************
|
|
165
|
|
166 **********************************************************************************************/
|
|
167 ClassInfo getClassInfo(size_t obj_ptr)
|
|
168 {
|
|
169 uint vtbl,
|
|
170 ci_ptr;
|
|
171 readProcessMemory(obj_ptr, &vtbl, size_t.sizeof);
|
|
172 readProcessMemory(vtbl, &ci_ptr, size_t.sizeof);
|
|
173 ubyte[] data;
|
|
174 data.length = ClassInfo.classinfo.init.length;
|
|
175 readProcessMemory(ci_ptr, data.ptr, data.length);
|
|
176 return cast(ClassInfo)data.ptr;
|
|
177 }
|
|
178
|
|
179 /**********************************************************************************************
|
|
180 simple check for invalidity of a memory block
|
|
181 **********************************************************************************************/
|
|
182 const size_t MEMCHECK_MIN = 0x1000;
|
|
183 bool isInvalidMem(size_t ptr, size_t len)
|
|
184 {
|
|
185 uint tmp;
|
|
186 if ( uint.sizeof != readProcessMemory(ptr, &tmp, uint.sizeof)
|
|
187 || uint.sizeof != readProcessMemory(ptr+len-uint.sizeof, &tmp, uint.sizeof) )
|
|
188 return true;
|
|
189 return false;
|
|
190 }
|
|
191
|
|
192 /**********************************************************************************************
|
|
193
|
|
194 **********************************************************************************************/
|
|
195 MEMORY_BASIC_INFORMATION[] walkMemory()
|
|
196 {
|
|
197 SYSTEM_INFO si;
|
|
198 MEMORY_BASIC_INFORMATION[] mbis;
|
|
199 GetSystemInfo(&si);
|
|
200 for ( void* ptr = si.lpMinimumApplicationAddress; ptr < si.lpMaximumApplicationAddress; )
|
|
201 {
|
|
202 mbis.length = mbis.length + 1;
|
|
203 VirtualQueryEx(process_handle, ptr, &mbis[$-1], MEMORY_BASIC_INFORMATION.sizeof);
|
|
204 ptr = mbis[$-1].BaseAddress + mbis[$-1].RegionSize;
|
|
205 }
|
|
206 return mbis;
|
|
207 }
|
|
208 }
|
|
209
|
|
210 /**************************************************************************************************
|
|
211
|
|
212 **************************************************************************************************/
|
|
213 class DLL
|
|
214 {
|
|
215 public:
|
|
216 HANDLE filehandle;
|
|
217 uint base,
|
|
218 debug_info_offset,
|
|
219 debug_info_size;
|
|
220 COFFImage image;
|
|
221 }
|