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
|
|
6 import win32.dbghelp;
|
|
7 import win32.windef;
|
|
8 import win32.winbase;
|
|
9
|
|
10 import std.string;
|
|
11
|
|
12 import dbgprocess;
|
|
13 import util;
|
|
14
|
|
15 /**************************************************************************************************
|
|
16
|
|
17 **************************************************************************************************/
|
|
18 class MiniDump
|
|
19 {
|
|
20 enum UserStreamType : uint
|
|
21 {
|
|
22 DExceptionInfo = 0x10000,
|
|
23 ThreadInfo
|
|
24 }
|
|
25
|
|
26 align(1) struct ThreadInfo
|
|
27 {
|
|
28 uint mainThreadId,
|
|
29 currentThreadId;
|
|
30 }
|
|
31
|
|
32 static typeof(&win32.dbghelp.MiniDumpWriteDump) MiniDumpWriteDump;
|
|
33 static typeof(&win32.dbghelp.MiniDumpReadDumpStream) MiniDumpReadDumpStream;
|
|
34
|
|
35 private void* map;
|
|
36 private HANDLE file,
|
|
37 fileMapping;
|
|
38
|
|
39 MINIDUMP_THREAD[] threads;
|
|
40 uint selectedThread;
|
|
41 MINIDUMP_MEMORY_DESCRIPTOR[] memoryDescriptors;
|
|
42 EXCEPTION_RECORD* exceptionRecord;
|
|
43
|
|
44 ThreadInfo* threadInfo;
|
|
45
|
|
46 /**********************************************************************************************
|
|
47
|
|
48 **********************************************************************************************/
|
|
49 static this()
|
|
50 {
|
|
51 // TODO: load from exe dir if not XP
|
|
52 HINSTANCE dll = LoadLibrary(toStringz("dbghelp.dll"));
|
|
53 if ( dll !is null ) {
|
|
54 MiniDumpWriteDump = cast(typeof(MiniDumpWriteDump))GetProcAddress(dll, toStringz("MiniDumpWriteDump"));
|
|
55 MiniDumpReadDumpStream = cast(typeof(MiniDumpReadDumpStream))GetProcAddress(dll, toStringz("MiniDumpReadDumpStream"));
|
|
56 }
|
|
57 }
|
|
58
|
|
59 /**********************************************************************************************
|
|
60
|
|
61 **********************************************************************************************/
|
|
62 this(string filename)
|
|
63 {
|
|
64 file = CreateFile(toStringz(filename), FILE_ALL_ACCESS, 0, null, OPEN_EXISTING, 0, null);
|
|
65 if ( file is null )
|
|
66 throw new Exception("Couldn't read \""~filename~"\": "~lastError);
|
|
67 fileMapping = CreateFileMapping(file, null, PAGE_READONLY, 0, 0, null);
|
|
68 if ( fileMapping is null )
|
|
69 throw new Exception("Couldn't create file mapping for \""~filename~"\": "~lastError);
|
|
70 map = MapViewOfFile(fileMapping, FILE_MAP_READ, 0, 0, 0);
|
|
71 if ( map is null )
|
|
72 throw new Exception("Couldn't map view of file \""~filename~"\": "~lastError);
|
|
73
|
|
74 MINIDUMP_DIRECTORY* mdDir;
|
|
75 void *streamPtr;
|
|
76 size_t streamSize;
|
|
77
|
|
78 // load thread info
|
|
79 if ( !MiniDumpReadDumpStream(map, MINIDUMP_STREAM_TYPE.ThreadListStream, &mdDir, &streamPtr, &streamSize) )
|
|
80 throw new Exception("Error no ThreadList in minidump \""~filename~"\": "~lastError);
|
|
81 MINIDUMP_THREAD_LIST* threadList = cast(MINIDUMP_THREAD_LIST*)streamPtr;
|
|
82 threads = (cast(MINIDUMP_THREAD*)(cast(ubyte*)streamPtr+4))[0..threadList.NumberOfThreads];
|
|
83
|
|
84 if ( !MiniDumpReadDumpStream(map, UserStreamType.ThreadInfo, &mdDir, &streamPtr, &streamSize) )
|
|
85 throw new Exception("Error no Ddbg ThreadInfo in minidump \""~filename~"\": "~lastError);
|
|
86 threadInfo = cast(ThreadInfo*)streamPtr;
|
|
87
|
|
88 // load memory info
|
|
89 if ( MiniDumpReadDumpStream(map, MINIDUMP_STREAM_TYPE.MemoryListStream, &mdDir, &streamPtr, &streamSize) )
|
|
90 {
|
|
91 MINIDUMP_MEMORY_LIST* mml = cast(MINIDUMP_MEMORY_LIST*)streamPtr;
|
|
92 memoryDescriptors = (cast(MINIDUMP_MEMORY_DESCRIPTOR*)(cast(ubyte*)streamPtr+4))[0..mml.NumberOfMemoryRanges];
|
|
93 }
|
|
94
|
|
95 // load exception info
|
|
96 if ( MiniDumpReadDumpStream(map, MINIDUMP_STREAM_TYPE.ExceptionStream, &mdDir, &streamPtr, &streamSize) )
|
|
97 {
|
|
98 MINIDUMP_EXCEPTION_STREAM* mes = cast(MINIDUMP_EXCEPTION_STREAM*)streamPtr;
|
|
99 foreach ( i, t; threads )
|
|
100 {
|
|
101 if ( t.ThreadId == mes.ThreadId ) {
|
|
102 selectedThread = i;
|
|
103 break;
|
|
104 }
|
|
105 }
|
|
106 exceptionRecord = new EXCEPTION_RECORD;
|
|
107 exceptionRecord.ExceptionCode = mes.ExceptionRecord.ExceptionCode;
|
|
108 exceptionRecord.ExceptionFlags = mes.ExceptionRecord.ExceptionFlags;
|
|
109 exceptionRecord.ExceptionAddress = cast(void*)mes.ExceptionRecord.ExceptionAddress;
|
|
110 exceptionRecord.NumberParameters = 4;
|
|
111 exceptionRecord.ExceptionInformation[0] = 0;
|
|
112 exceptionRecord.ExceptionInformation[1] = 0;
|
|
113 exceptionRecord.ExceptionInformation[2] = 0;
|
|
114 exceptionRecord.ExceptionInformation[3] = 0;
|
|
115 }
|
|
116
|
|
117 if ( exceptionRecord !is null && MiniDumpReadDumpStream(map, UserStreamType.DExceptionInfo, &mdDir, &streamPtr, &streamSize) )
|
|
118 {
|
|
119 string className,
|
|
120 message;
|
|
121 void* ptr = streamPtr;
|
|
122 className = (cast(char*)(ptr+size_t.sizeof))[0..*cast(size_t*)ptr];
|
|
123 ptr += className.length+size_t.sizeof;
|
|
124 message = (cast(char*)(ptr+size_t.sizeof))[0..*cast(size_t*)ptr];
|
|
125
|
|
126 exceptionRecord.NumberParameters = 4;
|
|
127 exceptionRecord.ExceptionInformation[0] = className.length;
|
|
128 exceptionRecord.ExceptionInformation[1] = cast(size_t)className.ptr;
|
|
129 exceptionRecord.ExceptionInformation[2] = message.length;
|
|
130 exceptionRecord.ExceptionInformation[3] = cast(size_t)message.ptr;
|
|
131 }
|
|
132 }
|
|
133
|
|
134 /**********************************************************************************************
|
|
135
|
|
136 **********************************************************************************************/
|
|
137 ~this()
|
|
138 {
|
|
139 UnmapViewOfFile(map);
|
|
140 CloseHandle(fileMapping);
|
|
141 CloseHandle(file);
|
|
142 }
|
|
143
|
|
144 static extern(Windows) bool callback(PVOID CallbackParam, PMINIDUMP_CALLBACK_INPUT CallbackInput, PMINIDUMP_CALLBACK_OUTPUT CallbackOutput)
|
|
145 {
|
|
146 DbgIO.println("MINIDUMP CALLBACK");
|
|
147 return true;
|
|
148 }
|
|
149
|
|
150 /**********************************************************************************************
|
|
151
|
|
152 **********************************************************************************************/
|
|
153 static bool haveMiniDump()
|
|
154 {
|
|
155 return MiniDumpWriteDump !is null;
|
|
156 }
|
|
157
|
|
158 /**********************************************************************************************
|
|
159
|
|
160 **********************************************************************************************/
|
|
161 void* rvaToVa(size_t rva)
|
|
162 {
|
|
163 return map+rva;
|
|
164 }
|
|
165
|
|
166 ubyte[] getMemory(MINIDUMP_LOCATION_DESCRIPTOR mld)
|
|
167 {
|
|
168 return (cast(ubyte*)rvaToVa(mld.Rva))[0..mld.DataSize];
|
|
169 }
|
|
170
|
|
171 CONTEXT* getContext()
|
|
172 {
|
|
173 return cast(CONTEXT*)rvaToVa(threads[selectedThread].ThreadContext.Rva);
|
|
174 }
|
|
175
|
|
176 MINIDUMP_THREAD* thread()
|
|
177 {
|
|
178 return &threads[selectedThread];
|
|
179 }
|
|
180
|
|
181 ubyte[] readMemory(size_t ptr, size_t size)
|
|
182 {
|
|
183 foreach ( md; memoryDescriptors )
|
|
184 {
|
|
185 size_t mdSize = md.Memory.DataSize,
|
|
186 mdBase = cast(size_t)md.StartOfMemoryRange;
|
|
187 debug DbgIO.println("MiniDump: Memory block 0x%x 0x%x", mdBase, mdSize);
|
|
188
|
|
189 if ( ptr >= mdBase && ptr+size <= mdBase+mdSize )
|
|
190 return getMemory(md.Memory)[ptr-mdBase..ptr-mdBase+size];
|
|
191 }
|
|
192 debug DbgIO.println("Memory block 0x%x - 0x%x not available in minidump", ptr, ptr+size);
|
|
193 return null;
|
|
194 }
|
|
195
|
|
196 void selectThread(size_t threadId)
|
|
197 {
|
|
198 foreach ( i, thread; threads )
|
|
199 {
|
|
200 if ( threadId == thread.ThreadId ) {
|
|
201 selectedThread = i;
|
|
202 break;
|
|
203 }
|
|
204 }
|
|
205 }
|
|
206
|
|
207 /**********************************************************************************************
|
|
208
|
|
209 **********************************************************************************************/
|
|
210 static bool writeMiniDump(string filename, DbgProcess process, uint threadId, EXCEPTION_RECORD* exrec=null, string exClassName=null, string exMsg=null)
|
|
211 {
|
|
212 if ( !haveMiniDump )
|
|
213 return false;
|
|
214
|
|
215 // create exception information
|
|
216 MINIDUMP_EXCEPTION_INFORMATION* eiPtr;
|
|
217 MINIDUMP_EXCEPTION_INFORMATION ei;
|
|
218
|
|
219 MINIDUMP_USER_STREAM[] ustreams;
|
|
220
|
|
221 if ( exrec !is null && (threadId in process.threads) !is null )
|
|
222 {
|
|
223 EXCEPTION_POINTERS ep;
|
|
224 ep.ExceptionRecord = exrec;
|
|
225 CONTEXT ctx;
|
|
226 process.threads[threadId].getContext(ctx, CONTEXT_FULL | CONTEXT_FLOATING_POINT | CONTEXT_EXTENDED_REGISTERS);
|
|
227 ep.ContextRecord = &ctx;
|
|
228
|
|
229 eiPtr = &ei;
|
|
230 ei.ExceptionPointers = &ep;
|
|
231 ei.ThreadId = threadId;
|
|
232 ei.ClientPointers = false;
|
|
233
|
|
234 ustreams.length = ustreams.length+1;
|
|
235 ustreams[$-1].Type = UserStreamType.DExceptionInfo;
|
|
236 ubyte[] data;
|
|
237 size_t len = exClassName.length;
|
|
238 data ~= (cast(ubyte*)&len)[0..size_t.sizeof];
|
|
239 data ~= cast(ubyte[])exClassName;
|
|
240 len = exMsg.length;
|
|
241 data ~= (cast(ubyte*)&len)[0..size_t.sizeof];
|
|
242 data ~= cast(ubyte[])exMsg;
|
|
243
|
|
244 ustreams[$-1].Buffer = data.ptr;
|
|
245 ustreams[$-1].BufferSize = data.length;
|
|
246 }
|
|
247
|
|
248 // write custom thread info
|
|
249 ustreams.length = ustreams.length+1;
|
|
250 ustreams[$-1].Type = UserStreamType.ThreadInfo;
|
|
251
|
|
252 ThreadInfo ti;
|
|
253 ti.mainThreadId = process.mainThreadId;
|
|
254 ti.currentThreadId = threadId;
|
|
255 ustreams[$-1].Buffer = &ti;
|
|
256 ustreams[$-1].BufferSize = ThreadInfo.sizeof;
|
|
257
|
|
258 // write minidump
|
|
259 MINIDUMP_USER_STREAM_INFORMATION usi;
|
|
260 usi.UserStreamCount = ustreams.length;
|
|
261 usi.UserStreamArray = ustreams.ptr;
|
|
262
|
|
263 auto file = CreateFile(toStringz(filename), FILE_ALL_ACCESS, 0, null, CREATE_ALWAYS, 0, null);
|
|
264 bool res = cast(bool)MiniDumpWriteDump(process.process_handle, process.processId, file, MINIDUMP_TYPE.MiniDumpNormal, eiPtr, &usi, null);
|
|
265 CloseHandle(file);
|
|
266 return res;
|
|
267 }
|
|
268 }
|