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