source: dev/trunk/abdev/BasicCompiler_Common/DebugMiddleFile.cpp@ 287

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