source: dev/trunk/ab5.0/abdev/BasicCompiler_Common/DebugMiddleFile.cpp@ 587

Last change on this file since 587 was 587, checked in by dai_9181, 16 years ago

[585][586]をリバース。NativeCodeクラスとMetaクラスは依存関係があるので分離しない方針とする。

File size: 10.1 KB
Line 
1#include "stdafx.h"
2
3#include <Compiler.h>
4#include <Class.h>
5#include <Variable.h>
6
7#include "../BasicCompiler_Common/common.h"
8#include "../BasicCompiler_Common/DebugSection.h"
9
10#ifdef _AMD64_
11#include "../compiler_x64/opcode.h"
12#else
13#include "../compiler_x86/opcode.h"
14#endif
15
16#define MDLFILE_VER 0x70000003
17
18
19void SetLpIndex_DebugFile(char *buffer,int *p,const Type &type){
20 if(NATURAL_TYPE(type.GetBasicType())==DEF_OBJECT || NATURAL_TYPE(type.GetBasicType())==DEF_STRUCT){
21 lstrcpy(buffer+(*p),type.GetClass().GetName().c_str());
22 (*p)+=lstrlen(buffer+(*p))+1;
23 }
24 else{
25 *(LONG_PTR *)(buffer+(*p))=type.GetIndex();
26 (*p)+=sizeof(LONG_PTR);
27 }
28}
29
30
31void GetLpIndex_DebugFile(char *buffer,int *p,Type &type){
32 if(NATURAL_TYPE(type.GetBasicType())==DEF_OBJECT || NATURAL_TYPE(type.GetBasicType())==DEF_STRUCT){
33 char szClassName[VN_SIZE];
34 lstrcpy(szClassName,buffer+(*p));
35 (*p)+=lstrlen(buffer+(*p))+1;
36
37 type.SetClassPtr( compiler.GetObjectModule().meta.GetClasses().FindEx(szClassName) );
38 }
39 else{
40 type.SetIndex( *(LONG_PTR *)(buffer+(*p)) );
41 (*p)+=sizeof(LONG_PTR);
42 }
43}
44
45
46
47CDebugSection::~CDebugSection(){
48 if(dwImageBase) DeleteDebugInfo();
49 if(buffer){
50 HeapDefaultFree(buffer);
51 buffer=0;
52 }
53}
54void CDebugSection::make(void){
55 int i2,BufferSize;
56
57 if(buffer){
58 HeapDefaultFree(buffer);
59 buffer=0;
60 }
61
62 i2=0;
63
64 extern char *basbuf;
65 BufferSize=lstrlen(basbuf)+65535;
66 buffer=(char *)HeapAlloc(hHeap,0,BufferSize);
67
68 //デバッグ用ファイルのバージョン
69 *(long *)(buffer+i2)=MDLFILE_VER;
70 i2+=sizeof(long);
71
72 //プラットフォームのビット数
73 *(long *)(buffer+i2)=PLATFORM;
74 i2+=sizeof(long);
75
76
77 // オブジェクトモジュール
78 {
79 // テキストデータにシリアライズ
80 std::string textString;
81 compiler.GetObjectModule().WriteString( textString );
82
83 // サイズ
84 *(long *)(buffer+i2) = (long)textString.size();
85 i2+=sizeof(long);
86
87 //バッファが足りない場合は再確保
88 if(BufferSize<i2+(int)textString.size()+32768){
89 while( BufferSize<i2+(int)textString.size()+32768 )
90 {
91 BufferSize+=32768;
92 }
93
94 buffer=(char *)HeapReAlloc(hHeap,0,buffer,BufferSize);
95 }
96
97 // バッファ
98 memcpy( buffer+i2, textString.c_str(), textString.size() );
99 i2 += (int)textString.size();
100 }
101
102
103 ////////////////////////
104 // コードと行番号の関係
105 ////////////////////////
106 extern SourceLines oldSourceLines;
107
108 *(long *)(buffer+i2)=(long)oldSourceLines.size();
109 i2+=sizeof(long);
110 BOOST_FOREACH( const SourceLine &sourceLine, oldSourceLines )
111 {
112 *(long *)(buffer+i2) = sourceLine.GetLineNum();
113 i2+=sizeof(long);
114
115 *(long *)(buffer+i2) = sourceLine.GetNativeCodePos();
116 i2+=sizeof(long);
117
118 *(long *)(buffer+i2) = sourceLine.GetSourceIndex();
119 i2+=sizeof(long);
120
121 *(long *)(buffer+i2) = sourceLine.GetSourceCodePos();
122 i2+=sizeof(long);
123
124 *(long *)(buffer+i2) = sourceLine.GetCodeType();
125 i2+=sizeof(long);
126
127 //バッファが足りない場合は再確保
128 if(BufferSize<i2+32768){
129 BufferSize+=32768;
130 buffer=(char *)HeapReAlloc(hHeap,0,buffer,BufferSize);
131 }
132 }
133
134 //グローバル実行領域のサイズ
135 extern int GlobalOpBufferSize;
136 *(long *)(buffer+i2)=GlobalOpBufferSize;
137 i2+=sizeof(long);
138
139 length=i2;
140}
141
142char *CDebugSection::MakeSingleStepCode(void){
143 char *buffer;
144 buffer=(char *)HeapAlloc(hHeap,0,SizeOf_CodeSection);
145
146 memcpy(buffer,OpBuffer,SizeOf_CodeSection);
147
148 BOOST_FOREACH( const SourceLine &sourceLine, _oldSourceLines )
149 {
150 if(!(
151 sourceLine.IsInSystemProc()
152 || sourceLine.IsInDebugProc() ) )
153 {
154 //int 3
155 buffer[sourceLine.GetNativeCodePos()]=(char)0xCC;
156 }
157 }
158
159 return buffer;
160}
161BOOL CDebugSection::__load(void){
162 int i2,i3;
163
164 compiler.ClearCompilingUserProcAndClass();
165
166 i2=0;
167
168 //デバッグ用ファイルのバージョンをチェック
169 if(*(long *)(buffer+i2)<MDLFILE_VER){
170 HeapDefaultFree(buffer);
171 return 0;
172 }
173 i2+=sizeof(long);
174
175 //プラットフォームのビット数をチェック
176 if(*(long *)(buffer+i2)!=PLATFORM){
177 HeapDefaultFree(buffer);
178 return 0;
179 }
180 i2+=sizeof(long);
181
182 // オブジェクトモジュール
183 {
184 // サイズ
185 int size = *(long *)(buffer+i2);
186 i2 += sizeof(long);
187
188 // バッファ
189 const std::string textString( (const char *)(buffer + i2), size );
190 i2 += size;
191
192 // テキストデータからシリアライズ
193 this->objectModule.ReadString( textString );
194
195 compiler.SelectObjectModule( this->objectModule );
196 }
197
198
199 //コードと行番号の関係
200 int maxLineInfoNum = *(long *)(buffer+i2);
201 i2+=sizeof(long);
202 for(i3=0;i3<maxLineInfoNum;i3++){
203 int lineNum = *(long *)(buffer+i2);
204 i2+=sizeof(long);
205
206 int nativeCodePos = *(long *)(buffer+i2);
207 i2+=sizeof(long);
208
209 int sourceIndex = *(long *)(buffer+i2);
210 i2+=sizeof(long);
211
212 int sourceCodePos = *(long *)(buffer+i2);
213 i2+=sizeof(long);
214
215 DWORD sourceLineType = *(DWORD *)(buffer+i2);
216 i2+=sizeof(long);
217
218 _oldSourceLines.push_back(
219 SourceLine(
220 lineNum,
221 nativeCodePos,
222 sourceIndex,
223 sourceCodePos,
224 sourceLineType
225 )
226 );
227 }
228
229 //グローバル実行領域のサイズ
230 GlobalOpBufferSize=*(long *)(buffer+i2);
231 i2+=sizeof(long);
232
233
234 HeapDefaultFree(buffer);
235 buffer=0;
236
237
238 this->pSub_DebugSys_EndProc=GetSubHash("_DebugSys_EndProc");
239 if( this->pSub_DebugSys_EndProc == NULL )
240 {
241 MessageBox( 0, "_DebugSys_EndProcが見つからない", "ActiveBasic", MB_OK );
242 }
243
244
245 SingleStepCodeBuffer=MakeSingleStepCode();
246
247 //ソースコード
248 extern char *basbuf;
249 basbuf = const_cast<char *>(compiler.GetObjectModule().GetSource(0).GetBuffer());
250
251
252 /////////////////////////////
253 // ブレークポイントを適用
254 /////////////////////////////
255
256 //コードと行番号の関係
257 extern SourceLines oldSourceLines;
258 oldSourceLines = this->_oldSourceLines;
259
260 BreakStepCodeBuffer=pobj_DBBreakPoint->update(OpBuffer,SizeOf_CodeSection);
261
262 //プロセスメモリにコピー
263 extern HANDLE hDebugProcess;
264 SIZE_T accessBytes;
265 WriteProcessMemory(hDebugProcess,(void *)(ULONG_PTR)(dwImageBase+dwRVA_CodeSection),
266 BreakStepCodeBuffer,
267 SizeOf_CodeSection,&accessBytes);
268
269
270 return 1;
271}
272
273BOOL CDebugSection::load(HMODULE hModule){
274 if(buffer){
275 HeapDefaultFree(buffer);
276 buffer=0;
277 }
278
279
280 extern HANDLE hDebugProcess;
281 SIZE_T accessBytes;
282 IMAGE_DOS_HEADER ImageDosHeader;
283 ReadProcessMemory(hDebugProcess,hModule,&ImageDosHeader,sizeof(IMAGE_DOS_HEADER),&accessBytes);
284
285 int pe_size;
286#ifdef _AMD64_
287 IMAGE_NT_HEADERS64 pe_hdr;
288 pe_size=sizeof(IMAGE_NT_HEADERS64);
289#else
290 IMAGE_NT_HEADERS pe_hdr;
291 pe_size=sizeof(IMAGE_NT_HEADERS);
292#endif
293 ReadProcessMemory(hDebugProcess,(void *)(((ULONG_PTR)hModule)+ImageDosHeader.e_lfanew),&pe_hdr,pe_size,&accessBytes);
294
295 IMAGE_SECTION_HEADER *pSectionHdr;
296 pSectionHdr=(IMAGE_SECTION_HEADER *)HeapAlloc(hHeap,0,pe_hdr.FileHeader.NumberOfSections*sizeof(IMAGE_SECTION_HEADER));
297 ReadProcessMemory(hDebugProcess,
298 (void *)(((ULONG_PTR)hModule)+ImageDosHeader.e_lfanew+pe_size),
299 pSectionHdr,
300 pe_hdr.FileHeader.NumberOfSections*sizeof(IMAGE_SECTION_HEADER),
301 &accessBytes);
302
303 int i;
304 for(i=0;i<pe_hdr.FileHeader.NumberOfSections;i++){
305
306 //リライタブルセクション内の情報
307 if(lstrcmp((char *)pSectionHdr[i].Name,".data")==0){
308 dwRVA_RWSection=pSectionHdr[i].VirtualAddress;
309 }
310
311 //コードセクション内の情報
312 if(lstrcmp((char *)pSectionHdr[i].Name,".text")==0){
313 dwRVA_CodeSection=pSectionHdr[i].VirtualAddress;
314 SizeOf_CodeSection=pSectionHdr[i].SizeOfRawData;
315 }
316
317 //デバッグセクション内の情報
318 if(lstrcmp((char *)pSectionHdr[i].Name,".debug")==0){
319 length=pSectionHdr[i].Misc.VirtualSize;
320 buffer=(char *)HeapAlloc(hHeap,0,length+1);
321
322 ReadProcessMemory(hDebugProcess,
323 (void *)(((ULONG_PTR)hModule)+pSectionHdr[i].VirtualAddress),
324 buffer,
325 length,
326 &accessBytes);
327 buffer[length]=0;
328 }
329
330 }
331 HeapDefaultFree(pSectionHdr);
332
333 if(!buffer) return 0;
334
335
336 dwImageBase=(DWORD)(ULONG_PTR)hModule;
337
338
339
340 if(OpBuffer) free(OpBuffer);
341 OpBuffer=(char *)malloc(SizeOf_CodeSection);
342
343 ReadProcessMemory(hDebugProcess,
344 (void *)(ULONG_PTR)(dwImageBase+dwRVA_CodeSection),OpBuffer,
345 SizeOf_CodeSection,&accessBytes);
346
347
348 return __load();
349}
350
351void CDebugSection::choice(void){
352 //イメージベース
353 extern DWORD ImageBase;
354 ImageBase=this->dwImageBase;
355
356 //リライタブルセクションのRVA
357 extern int MemPos_RWSection;
358 MemPos_RWSection=this->dwRVA_RWSection;
359
360 //コードセクションのRVAとサイズ
361 extern int MemPos_CodeSection;
362 extern int FileSize_CodeSection;
363 MemPos_CodeSection=this->dwRVA_CodeSection;
364 FileSize_CodeSection=this->SizeOf_CodeSection;
365
366 // オブジェクトモジュール
367 compiler.SelectObjectModule( this->objectModule );
368
369 //コードと行番号の関係
370 extern SourceLines oldSourceLines;
371 oldSourceLines = this->_oldSourceLines;
372
373 //グローバル実行領域のサイズ
374 extern int GlobalOpBufferSize;
375 GlobalOpBufferSize=this->GlobalOpBufferSize;
376
377 extern const UserProc *pSub_DebugSys_EndProc;
378 pSub_DebugSys_EndProc=this->pSub_DebugSys_EndProc;
379
380 //ネイティブコードバッファ
381 extern char *OpBuffer;
382 OpBuffer=this->OpBuffer;
383}
384
385void CDebugSection::DeleteDebugInfo(void){
386 //コードバッファを解放
387 free(OpBuffer);
388 OpBuffer=0;
389
390 HeapDefaultFree(SingleStepCodeBuffer);
391 SingleStepCodeBuffer=0;
392
393 HeapDefaultFree(BreakStepCodeBuffer);
394 BreakStepCodeBuffer=0;
395}
396
397
398
399CDBDebugSection::CDBDebugSection(){
400 ppobj_ds=(CDebugSection **)HeapAlloc(hHeap,0,1);
401 num=0;
402}
403CDBDebugSection::~CDBDebugSection(){
404 int i;
405 for(i=0;i<num;i++){
406 delete ppobj_ds[i];
407 }
408 HeapDefaultFree(ppobj_ds);
409}
410
411BOOL CDBDebugSection::add(HMODULE hModule){
412 CDebugSection *pobj_d;
413 pobj_d=new CDebugSection();
414 if(!pobj_d->load(hModule)){
415 //デバッグ情報が存在しないとき
416 delete pobj_d;
417 return 0;
418 }
419
420 ppobj_ds=(CDebugSection **)HeapReAlloc(hHeap,0,ppobj_ds,(num+1)*sizeof(CDebugSection *));
421 ppobj_ds[num]=pobj_d;
422 num++;
423
424 return 1;
425}
426
427void CDBDebugSection::del(HMODULE hModule){
428 int i;
429 for(i=0;i<num;i++){
430 if((HMODULE)(ULONG_PTR)ppobj_ds[i]->dwImageBase==hModule){
431 delete ppobj_ds[i];
432
433 num--;
434 for(;i<num;i++){
435 ppobj_ds[i]=ppobj_ds[i+1];
436 }
437 break;
438 }
439 }
440}
441
442void CDBDebugSection::choice(int index){
443 pobj_now=ppobj_ds[index];
444 pobj_now->choice();
445}
446
447
448
449CDBDebugSection *pobj_DBDebugSection;
Note: See TracBrowser for help on using the repository browser.