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

Last change on this file since 264 was 264, checked in by dai_9181, 17 years ago

デバッグデータとしてオブジェクトモジュールのシリアライズを可能にした(その先の処理はまだ動かない)

File size: 27.6 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.objectModule.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(pobj_DBClass) DeleteDebugInfo();
51 if(buffer){
52 HeapDefaultFree(buffer);
53 buffer=0;
54 }
55}
56void CDebugSection::make(void){
57 extern INCLUDEFILEINFO IncludeFileInfo;
58 int i2,i3,BufferSize;
59
60 if(buffer){
61 HeapDefaultFree(buffer);
62 buffer=0;
63 }
64
65 i2=0;
66
67 extern char *basbuf;
68 BufferSize=lstrlen(basbuf)+65535;
69 buffer=(char *)HeapAlloc(hHeap,0,BufferSize);
70
71 //デバッグ用ファイルのバージョン
72 *(long *)(buffer+i2)=MDLFILE_VER;
73 i2+=sizeof(long);
74
75 //プラットフォームのビット数
76 *(long *)(buffer+i2)=PLATFORM;
77 i2+=sizeof(long);
78
79
80 // オブジェクトモジュール
81 {
82 // テキストデータにシリアライズ
83 std::string textString;
84 compiler.objectModule.WriteTextString( textString );
85
86 // サイズ
87 *(long *)(buffer+i2) = textString.size();
88 i2+=sizeof(long);
89
90 //バッファが足りない場合は再確保
91 if(BufferSize<i2+(int)textString.size()+32768){
92 while( BufferSize<i2+(int)textString.size()+32768 )
93 {
94 BufferSize+=32768;
95 }
96
97 buffer=(char *)HeapReAlloc(hHeap,0,buffer,BufferSize);
98 }
99
100 // バッファ
101 memcpy( buffer+i2, textString.c_str(), textString.size() );
102 i2 += textString.size();
103 }
104
105
106 //インクルード情報
107 *(long *)(buffer+i2)=IncludeFileInfo.FilesNum;
108 i2+=sizeof(long);
109 for(i3=0;i3<IncludeFileInfo.FilesNum;i3++){
110 lstrcpy(buffer+i2,IncludeFileInfo.ppFileNames[i3]);
111 i2+=lstrlen(buffer+i2)+1;
112 }
113 buffer[i2++]=0;
114 for(i3=0;;i3++){
115 buffer[i2++]=(char)IncludeFileInfo.LineOfFile[i3];
116 if(IncludeFileInfo.LineOfFile[i3]==-1) break;
117 }
118
119 //ソースコード
120 lstrcpy(buffer+i2,basbuf);
121 i2+=lstrlen(buffer+i2)+1;
122
123
124 ////////////////////////
125 // コードと行番号の関係
126 ////////////////////////
127 extern SourceLines oldSourceLines;
128
129 *(long *)(buffer+i2)=(long)oldSourceLines.size();
130 i2+=sizeof(long);
131 BOOST_FOREACH( const SourceLine &sourceLine, oldSourceLines )
132 {
133 *(long *)(buffer+i2) = sourceLine.GetLineNum();
134 i2+=sizeof(long);
135
136 *(long *)(buffer+i2) = sourceLine.GetNativeCodePos();
137 i2+=sizeof(long);
138
139 *(long *)(buffer+i2) = sourceLine.GetSourceCodePos();
140 i2+=sizeof(long);
141
142 *(long *)(buffer+i2) = sourceLine.GetCodeType();
143 i2+=sizeof(long);
144
145 //バッファが足りない場合は再確保
146 if(BufferSize<i2+32768){
147 BufferSize+=32768;
148 buffer=(char *)HeapReAlloc(hHeap,0,buffer,BufferSize);
149 }
150 }
151
152
153
154 ////////////////////////////////////////////
155 // クラス情報(名前のみ。詳細は後で保存)
156 ////////////////////////////////////////////
157
158 //イテレータをリセット
159 compiler.objectModule.meta.GetClasses().Iterator_Reset();
160
161 //個数
162 *(long *)(buffer+i2)=compiler.objectModule.meta.GetClasses().Iterator_GetMaxCount();
163 i2+=sizeof(long);
164
165 while(compiler.objectModule.meta.GetClasses().Iterator_HasNext()){
166 CClass *pobj_c;
167 pobj_c=compiler.objectModule.meta.GetClasses().Iterator_GetNext();
168
169 //クラス名
170 lstrcpy(buffer+i2,pobj_c->GetName().c_str());
171 i2+=lstrlen(buffer+i2)+1;
172 }
173
174
175
176 //////////////////
177 // TypeDef情報
178 //////////////////
179 *(long *)(buffer+i2)=(int)compiler.objectModule.meta.GetTypeDefs().size();
180 i2+=sizeof(long);
181 for(i3=0;i3<(int)compiler.objectModule.meta.GetTypeDefs().size();i3++){
182 lstrcpy(buffer+i2,compiler.objectModule.meta.GetTypeDefs()[i3].GetName().c_str() );
183 i2+=lstrlen(buffer+i2)+1;
184
185 lstrcpy(buffer+i2,compiler.objectModule.meta.GetTypeDefs()[i3].GetBaseName().c_str() );
186 i2+=lstrlen(buffer+i2)+1;
187
188 //バッファが足りない場合は再確保
189 if(BufferSize<i2+32768){
190 BufferSize+=32768;
191 buffer=(char *)HeapReAlloc(hHeap,0,buffer,BufferSize);
192 }
193 }
194
195
196 //グローバル変数情報
197 *(long *)(buffer+i2)=(int)compiler.objectModule.meta.GetGlobalVars().size();
198 i2+=sizeof(long);
199 BOOST_FOREACH( Variable *pVar, compiler.objectModule.meta.GetGlobalVars() ){
200 //変数名
201 lstrcpy(buffer+i2,pVar->GetName().c_str());
202 i2+=lstrlen(buffer+i2)+1;
203
204 //型
205 *(long *)(buffer+i2)=pVar->GetType().GetBasicType();
206 i2+=sizeof(long);
207
208 //型の拡張情報
209 SetLpIndex_DebugFile(buffer,&i2,pVar->GetType());
210
211 buffer[i2++] = pVar->IsRef() ? 1 : 0;
212
213 buffer[i2++] = pVar->IsArray() ? 1 : 0;
214
215 if(pVar->IsArray()){
216 *(long *)(buffer+i2)=(int)pVar->GetSubscripts().size();
217 i2+=sizeof(long);
218 BOOST_FOREACH( int indexMax, pVar->GetSubscripts() )
219 {
220 *(long *)(buffer+i2)=indexMax;
221 i2+=sizeof(long);
222 }
223 }
224
225 //レキシカルスコープ情報
226 *(long *)(buffer+i2)=pVar->GetScopeStartAddress();
227 i2+=sizeof(long);
228 *(long *)(buffer+i2)=pVar->GetScopeEndAddress();
229 i2+=sizeof(long);
230 *(long *)(buffer+i2)=pVar->GetScopeLevel();
231 i2+=sizeof(long);
232
233 //メモリ位置
234 *(long *)(buffer+i2)=pVar->GetOffsetAddress();
235 i2+=sizeof(long);
236
237 //バッファが足りない場合は再確保
238 if(BufferSize<i2+32768){
239 BufferSize+=32768;
240 buffer=(char *)HeapReAlloc(hHeap,0,buffer,BufferSize);
241 }
242 }
243
244 //グローバル実行領域のサイズ
245 extern int GlobalOpBufferSize;
246 *(long *)(buffer+i2)=GlobalOpBufferSize;
247 i2+=sizeof(long);
248
249 //プロシージャ情報
250 *(long *)(buffer+i2) = compiler.objectModule.meta.GetUserProcs().Iterator_GetMaxCount();
251 i2+=sizeof(long);
252 compiler.objectModule.meta.GetUserProcs().Iterator_Reset();
253 while( compiler.objectModule.meta.GetUserProcs().Iterator_HasNext() )
254 {
255 UserProc *pUserProc = compiler.objectModule.meta.GetUserProcs().Iterator_GetNext();
256
257 if(pUserProc->GetParentClassPtr()){
258 lstrcpy(buffer+i2,pUserProc->GetParentClassPtr()->GetName().c_str());
259 i2+=lstrlen(buffer+i2)+1;
260 }
261 else{
262 lstrcpy(buffer+i2,"");
263 i2+=lstrlen(buffer+i2)+1;
264 }
265
266 //ID
267 *(long *)(buffer+i2)=pUserProc->GetId();
268 i2+=sizeof(long);
269
270 //関数名
271 lstrcpy(buffer+i2,pUserProc->GetName().c_str());
272 i2+=lstrlen(buffer+i2)+1;
273
274 *(long *)(buffer+i2)=pUserProc->_beginOpAddressOld;
275 i2+=sizeof(long);
276 *(long *)(buffer+i2)=pUserProc->_endOpAddressOld;
277 i2+=sizeof(long);
278
279 //ローカル変数情報
280 *(long *)(buffer+i2)=(int)pUserProc->GetLocalVars().size();
281 i2+=sizeof(long);
282
283 //バッファが足りない場合は再確保
284 if(BufferSize<i2+32768){
285 BufferSize+=32768;
286 buffer=(char *)HeapReAlloc(hHeap,0,buffer,BufferSize);
287 }
288
289 BOOST_FOREACH( Variable *pVar, pUserProc->GetLocalVars() ){
290 lstrcpy(buffer+i2,pVar->GetName().c_str());
291 i2+=lstrlen(buffer+i2)+1;
292
293 //型
294 *(long *)(buffer+i2)=pVar->GetType().GetBasicType();
295 i2+=sizeof(long);
296
297 //型の拡張情報
298 SetLpIndex_DebugFile(buffer,&i2,pVar->GetType());
299
300 //参照型パラメータかどうか
301 buffer[i2++] = pVar->IsRef() ? 1 : 0;
302
303 //配列かどうか
304 buffer[i2++] = pVar->IsArray() ? 1 : 0;
305
306 //配列要素
307 if(pVar->IsArray()){
308 *(long *)(buffer+i2)=(int)pVar->GetSubscripts().size();
309 i2+=sizeof(long);
310 BOOST_FOREACH( int indexMax, pVar->GetSubscripts() )
311 {
312 *(long *)(buffer+i2)=indexMax;
313 i2+=sizeof(long);
314 }
315 }
316
317 //レキシカルスコープ情報
318 *(long *)(buffer+i2)=pVar->GetScopeStartAddress();
319 i2+=sizeof(long);
320 *(long *)(buffer+i2)=pVar->GetScopeEndAddress();
321 i2+=sizeof(long);
322 *(long *)(buffer+i2)=pVar->GetScopeLevel();
323 i2+=sizeof(long);
324
325 //メモリ位置
326 *(long *)(buffer+i2)=pVar->GetOffsetAddress();
327 i2+=sizeof(long);
328
329
330
331
332 //バッファが足りない場合は再確保
333 if(BufferSize<i2+32768){
334 BufferSize+=32768;
335 buffer=(char *)HeapReAlloc(hHeap,0,buffer,BufferSize);
336 }
337 }
338 }
339
340
341
342 ///////////////////
343 // クラス情報
344 ///////////////////
345
346 //イテレータをリセット
347 compiler.objectModule.meta.GetClasses().Iterator_Reset();
348
349 while(compiler.objectModule.meta.GetClasses().Iterator_HasNext()){
350 CClass *pobj_c;
351 pobj_c=compiler.objectModule.meta.GetClasses().Iterator_GetNext();
352
353
354 //クラス名
355 lstrcpy(buffer+i2,pobj_c->GetName().c_str());
356 i2+=lstrlen(buffer+i2)+1;
357
358 //仮想関数の数
359 *(long *)(buffer+i2)=pobj_c->GetVtblNum();
360 i2+=sizeof(long);
361
362 //アラインメント
363 *(long *)(buffer+i2)=pobj_c->GetFixedAlignment();
364 i2+=sizeof(long);
365
366 // 動的メンバ
367 *(long *)(buffer+i2)=(int)pobj_c->GetDynamicMembers().size();
368 i2+=sizeof(long);
369 BOOST_FOREACH( CMember *member, pobj_c->GetDynamicMembers() ){
370 // 名前
371 lstrcpy(buffer+i2,member->GetName().c_str());
372 i2+=lstrlen(buffer+i2)+1;
373
374 // 型
375 *(long *)(buffer+i2)=member->GetType().GetBasicType();
376 i2+=sizeof(long);
377
378 // 型の拡張情報
379 SetLpIndex_DebugFile(buffer,&i2,member->GetType());
380
381 // アクセシビリティ
382 *(Prototype::Accessibility *)(buffer+i2)=member->GetAccessibility();
383 i2+=sizeof(Prototype::Accessibility);
384
385 *(long *)(buffer+i2)=(int)member->GetSubscripts().size();
386 i2+=sizeof(long);
387 BOOST_FOREACH( int indexMax, member->GetSubscripts() )
388 {
389 *(long *)(buffer+i2)=indexMax;
390 i2+=sizeof(long);
391 }
392
393 //バッファが足りない場合は再確保
394 if(BufferSize<i2+32768){
395 BufferSize+=32768;
396 buffer=(char *)HeapReAlloc(hHeap,0,buffer,BufferSize);
397 }
398 }
399
400 // 動的メソッド
401 *(long *)(buffer+i2)=(long)pobj_c->GetMethods().size();
402 i2+=sizeof(long);
403 BOOST_FOREACH( const CMethod *pMethod, pobj_c->GetMethods() ){
404 *(Prototype::Accessibility *)(buffer+i2)=pMethod->GetAccessibility();
405 i2+=sizeof(Prototype::Accessibility);
406 if( pMethod->GetInheritsClassPtr() ){
407 lstrcpy(buffer+i2,pMethod->GetInheritsClassPtr()->GetName().c_str());
408 i2+=lstrlen(buffer+i2)+1;
409 }
410 else{
411 lstrcpy(buffer+i2,"");
412 i2+=lstrlen(buffer+i2)+1;
413 }
414 lstrcpy(buffer+i2,pMethod->GetUserProc().GetName().c_str());
415 i2+=lstrlen(buffer+i2)+1;
416 }
417
418 //静的メンバ
419 *(long *)(buffer+i2)=(long)pobj_c->GetStaticMembers().size();
420 i2+=sizeof(long);
421 BOOST_FOREACH( CMember *member, pobj_c->GetStaticMembers() ){
422 // 名前
423 lstrcpy(buffer+i2,member->GetName().c_str());
424 i2+=lstrlen(buffer+i2)+1;
425
426 // 型
427 *(long *)(buffer+i2)=member->GetType().GetBasicType();
428 i2+=sizeof(long);
429
430 // 型の拡張情報
431 SetLpIndex_DebugFile(buffer,&i2,member->GetType());
432
433 // アクセシビリティ
434 *(Prototype::Accessibility *)(buffer+i2)=member->GetAccessibility();
435 i2+=sizeof(Prototype::Accessibility);
436
437 // 配列
438 *(long *)(buffer+i2)=(int)member->GetSubscripts().size();
439 i2+=sizeof(long);
440 BOOST_FOREACH( int indexMax, member->GetSubscripts() )
441 {
442 *(long *)(buffer+i2)=indexMax;
443 i2+=sizeof(long);
444 }
445
446 //バッファが足りない場合は再確保
447 if(BufferSize<i2+32768){
448 BufferSize+=32768;
449 buffer=(char *)HeapReAlloc(hHeap,0,buffer,BufferSize);
450 }
451 }
452
453 //バッファが足りない場合は再確保
454 if(BufferSize<i2+32768){
455 BufferSize+=32768;
456 buffer=(char *)HeapReAlloc(hHeap,0,buffer,BufferSize);
457 }
458 }
459
460 length=i2;
461}
462
463char *CDebugSection::MakeSingleStepCode(void){
464 char *buffer;
465 buffer=(char *)HeapAlloc(hHeap,0,SizeOf_CodeSection);
466
467 memcpy(buffer,OpBuffer,SizeOf_CodeSection);
468
469 BOOST_FOREACH( const SourceLine &sourceLine, oldSourceLines )
470 {
471 if(!(
472 sourceLine.IsInSystemProc()
473 || sourceLine.IsInDebugProc() ) )
474 {
475 //int 3
476 buffer[sourceLine.GetNativeCodePos()]=(char)0xCC;
477 }
478 }
479
480 return buffer;
481}
482BOOL CDebugSection::__load(void){
483 int i2,i3,i4,num;
484 char temp2[MAX_PATH],*temp5;
485
486 compiler.pCompilingClass = NULL;
487
488 i2=0;
489
490 //デバッグ用ファイルのバージョンをチェック
491 if(*(long *)(buffer+i2)<MDLFILE_VER){
492 HeapDefaultFree(buffer);
493 return 0;
494 }
495 i2+=sizeof(long);
496
497 //プラットフォームのビット数をチェック
498 if(*(long *)(buffer+i2)!=PLATFORM){
499 HeapDefaultFree(buffer);
500 return 0;
501 }
502 i2+=sizeof(long);
503
504 MessageBox(0,"test","test",0);
505
506 // オブジェクトモジュール
507 {
508 // サイズ
509 int size = *(long *)(buffer+i2);
510 i2 += sizeof(long);
511
512 // バッファ
513 const std::string textString( (const char *)(buffer + i2), size );
514 i2 += textString.size();
515
516 // テキストデータからシリアライズ
517 this->objectModule.ReadTextString( textString );
518 }
519
520 //インクルード情報
521 IncludeFileInfo.FilesNum=*(long *)(buffer+i2);
522 i2+=sizeof(long);
523 IncludeFileInfo.ppFileNames=(char **)malloc(IncludeFileInfo.FilesNum*sizeof(char *));
524 for(i3=0;i3<IncludeFileInfo.FilesNum;i3++){
525 if(buffer[i2]=='\0') break;
526 IncludeFileInfo.ppFileNames[i3]=(char *)malloc(lstrlen(buffer+i2)+1);
527 lstrcpy(IncludeFileInfo.ppFileNames[i3],buffer+i2);
528 i2+=lstrlen(buffer+i2)+1;
529 }
530 for(i2++,i3=0;;i2++,i3++){
531 IncludeFileInfo.LineOfFile[i3]=(long)buffer[i2];
532 if(IncludeFileInfo.LineOfFile[i3]==-1) break;
533 }
534
535 //ソースコード
536 i2++;
537 source.SetBuffer( buffer + i2 );
538 i2+=lstrlen(buffer+i2)+1;
539
540 //コードと行番号の関係
541 int maxLineInfoNum;
542 maxLineInfoNum=*(long *)(buffer+i2);
543 i2+=sizeof(long);
544 for(i3=0;i3<maxLineInfoNum;i3++){
545 int lineNum = *(long *)(buffer+i2);
546 i2+=sizeof(long);
547
548 int nativeCodePos = *(long *)(buffer+i2);
549 i2+=sizeof(long);
550
551 int sourceCodePos = *(long *)(buffer+i2);
552 i2+=sizeof(long);
553
554 DWORD sourceLineType = *(DWORD *)(buffer+i2);
555 i2+=sizeof(long);
556
557 oldSourceLines.push_back( SourceLine( lineNum, nativeCodePos, sourceCodePos, sourceLineType ) );
558 }
559
560
561 ///////////////////////////////////////////
562 // クラス情報(名前のみ。詳細は後で取得)
563 ///////////////////////////////////////////
564
565 this->pobj_DBClass=new Classes();
566
567 int iMaxClassCount;
568 iMaxClassCount=*(long *)(buffer+i2);
569 i2+=sizeof(long);
570 for(i3=0;i3<iMaxClassCount;i3++){
571 //クラス名
572 // TODO: 名前空間が未完成
573 this->pobj_DBClass->Add(NamespaceScopes(),NamespaceScopesCollection(),buffer+i2,0);
574 i2+=lstrlen(buffer+i2)+1;
575 }
576
577 compiler.objectModule.meta.SetClasses( this->pobj_DBClass );
578
579
580 //////////////////
581 // TypeDef情報
582 //////////////////
583
584 //初期化
585 compiler.objectModule.meta.GetTypeDefs().clear();
586
587 //個数を取得
588 num=*(long *)(buffer+i2);
589 i2+=sizeof(long);
590 for(i3=0;i3<num;i3++){
591 temp5=buffer+i2;
592 i2+=lstrlen(buffer+i2)+1;
593
594 // 名前空間に未対応
595 compiler.objectModule.meta.GetTypeDefs().push_back( TypeDef( NamespaceScopes(), temp5, buffer+i2, -1 ) );
596
597 i2+=lstrlen(buffer+i2)+1;
598 }
599
600 //定数を取得
601 GetConstInfo();
602 this->globalConsts = compiler.objectModule.meta.GetGlobalConsts();
603 this->globalConstMacros = compiler.objectModule.meta.GetGlobalConstMacros();
604
605 //グローバル変数情報
606 compiler.objectModule.meta.GetGlobalVars().clear();
607 int maxGlobalVars=*(long *)(buffer+i2);
608 i2+=sizeof(long);
609 for(i3=0;i3<maxGlobalVars;i3++){
610
611 //変数名
612 char *name = buffer+i2;
613 i2+=lstrlen(buffer+i2)+1;
614
615 int basicType = *(long *)(buffer+i2);
616 i2+=sizeof(long);
617
618 Type type( basicType );
619 GetLpIndex_DebugFile(buffer,&i2,type);
620
621 bool isRef = (buffer[i2++]) ? true:false;
622
623 bool isArray = (buffer[i2++]) ? true:false;
624
625 Variable *pVar = new Variable( name, type, false, isRef, "" );
626
627 if(isArray){
628 Subscripts subscripts;
629 int nSubScriptsNum = *(long *)(buffer+i2);
630 i2+=sizeof(long);
631 for( int i=0; i<nSubScriptsNum; i++ )
632 {
633 subscripts.push_back( *(long *)(buffer+i2) );
634 i2+=sizeof(long);
635 }
636
637 pVar->SetArray( subscripts );
638 }
639
640 //レキシカルスコープ情報
641 pVar->SetScopeStartAddress( *(long *)(buffer+i2) );
642 i2+=sizeof(long);
643 pVar->SetScopeEndAddress( *(long *)(buffer+i2) );
644 i2+=sizeof(long);
645 pVar->SetScopeLevel( *(long *)(buffer+i2) );
646 i2+=sizeof(long);
647
648 //メモリ位置
649 pVar->SetOffsetAddress( *(long *)(buffer+i2) );
650 i2+=sizeof(long);
651
652 //変数を追加
653 compiler.objectModule.meta.GetGlobalVars().push_back( pVar );
654 }
655
656 //グローバル実行領域のサイズ
657 GlobalOpBufferSize=*(long *)(buffer+i2);
658 i2+=sizeof(long);
659
660 //プロシージャ情報
661 userProcs.Clear();
662 int subNum = *(long *)(buffer+i2);
663 i2+=sizeof(long);
664 for(int i6=0;i6<subNum;i6++){
665 char szParentClassName[VN_SIZE];
666 lstrcpy(szParentClassName,buffer+i2);
667 i2+=lstrlen(buffer+i2)+1;
668
669 const CClass *pClass = NULL;
670 if(szParentClassName[0]){
671 pClass=compiler.objectModule.meta.GetClasses().Find(szParentClassName);
672 }
673
674 //ID
675 int id=*(long *)(buffer+i2);
676 i2+=sizeof(long);
677
678 //名前
679 char *name = buffer+i2;
680 i2+=lstrlen(buffer+i2)+1;
681
682 // オブジェクトを生成
683 // TODO: 名前空間が未完成
684 UserProc *pUserProc = new UserProc( NamespaceScopes(), NamespaceScopesCollection(), name, Procedure::Function, false, false, false, id );
685 pUserProc->SetParentClass( pClass );
686
687 pUserProc->_beginOpAddressOld = *(long *)(buffer+i2);
688 i2+=sizeof(long);
689 pUserProc->_endOpAddressOld = *(long *)(buffer+i2);
690 i2+=sizeof(long);
691
692 pUserProc->CompleteCompile();
693
694 //ローカル変数情報
695 pUserProc->GetLocalVars().clear();
696 int maxLocalVar=*(long *)(buffer+i2);
697 i2+=sizeof(long);
698 for(i3=0;i3<maxLocalVar;i3++){
699 //変数名
700 char *name = buffer+i2;
701 i2+=lstrlen(buffer+i2)+1;
702
703 int basicType = *(long *)(buffer+i2);
704 i2+=sizeof(long);
705
706 Type type( basicType );
707 GetLpIndex_DebugFile(buffer,&i2,type);
708
709 bool isRef = (buffer[i2++]) ? true:false;
710
711 bool isArray = (buffer[i2++]) ? true:false;
712
713 Variable *pVar = new Variable( name, type, false, isRef, "" );
714
715 if(isArray){
716 Subscripts subscripts;
717 int nSubScriptsNum = *(long *)(buffer+i2);
718 i2+=sizeof(long);
719 for( int i=0; i<nSubScriptsNum; i++ )
720 {
721 subscripts.push_back( *(long *)(buffer+i2) );
722 i2+=sizeof(long);
723 }
724
725 pVar->SetArray( subscripts );
726 }
727
728 //レキシカルスコープ情報
729 pVar->SetScopeStartAddress( *(long *)(buffer+i2) );
730 i2+=sizeof(long);
731 pVar->SetScopeEndAddress( *(long *)(buffer+i2) );
732 i2+=sizeof(long);
733 pVar->SetScopeLevel( *(long *)(buffer+i2) );
734 i2+=sizeof(long);
735
736 //メモリ位置
737 pVar->SetOffsetAddress( *(long *)(buffer+i2) );
738 i2+=sizeof(long);
739
740 //変数を追加
741 pUserProc->GetLocalVars().push_back( pVar );
742 }
743
744
745 /////////////////////////////////
746 // 格納位置を計算してpUserProcをセット
747 /////////////////////////////////
748
749 // ハッシュに追加
750 if( !userProcs.Insert( pUserProc, -1 ) )
751 {
752 //return NULL;
753 }
754 }
755 userProcs.Iterator_Init();
756
757 //クラス情報
758 CClass *pobj_c;
759 for(i3=0;i3<iMaxClassCount;i3++){
760 //クラス名
761 char szClassName[VN_SIZE];
762 lstrcpy(szClassName,buffer+i2);
763 i2+=lstrlen(buffer+i2)+1;
764
765 pobj_c = const_cast<CClass *>( compiler.objectModule.meta.GetClasses().Find(szClassName) );
766
767 //仮想関数の数
768 pobj_c->SetVtblNum( *(long *)(buffer+i2) );
769 i2+=sizeof(long);
770
771 //アラインメント
772 pobj_c->SetFixedAlignment( *(long *)(buffer+i2) );
773 i2+=sizeof(long);
774
775 // 動的メンバ
776 int nDynamicMember = *(long *)(buffer+i2);
777 i2+=sizeof(long);
778 for( i4=0; i4<nDynamicMember; i4++ ){
779 // 名前
780 string name = (char *)(buffer+i2);
781 i2+=lstrlen(buffer+i2)+1;
782
783 // 型
784 Type type( *(long *)(buffer+i2) );
785 i2+=sizeof(long);
786
787 // 型の拡張情報
788 GetLpIndex_DebugFile(buffer,&i2,type);
789
790 // アクセシビリティ
791 Prototype::Accessibility accessibility = *(Prototype::Accessibility *)(buffer+i2);
792 i2+=sizeof(Prototype::Accessibility);
793
794 Subscripts subscripts;
795 int nSubScriptsNum = *(long *)(buffer+i2);
796 i2+=sizeof(long);
797 for( int i=0; i<nSubScriptsNum; i++ )
798 {
799 subscripts.push_back( *(long *)(buffer+i2) );
800 i2+=sizeof(long);
801 }
802
803 CMember *member=new CMember( accessibility, name, type, false, subscripts, "", "" );
804
805 pobj_c->GetDynamicMembers().push_back( member );
806 }
807
808 // 動的メソッド
809 int nMethod = *(long *)(buffer+i2);
810 i2+=sizeof(long);
811 for( i4=0; i4<nMethod; i4++ ){
812
813 Prototype::Accessibility accessibility=*(Prototype::Accessibility *)(buffer+i2);
814 i2+=sizeof(Prototype::Accessibility);
815
816 char szInherits[VN_SIZE];
817 lstrcpy(szInherits,buffer+i2);
818 i2+=lstrlen(buffer+i2)+1;
819
820 const CClass *pobj_InheritsClass = NULL;
821 if(szInherits[0]){
822 pobj_InheritsClass=compiler.objectModule.meta.GetClasses().Find(szInherits);
823 }
824
825 lstrcpy(temp2,buffer+i2);
826 i2+=lstrlen(buffer+i2)+1;
827
828 const CClass *pobj_temp_c;
829 pobj_temp_c=pobj_InheritsClass;
830 if(pobj_temp_c==0) pobj_temp_c=pobj_c;
831
832 UserProc *pUserProc = compiler.objectModule.meta.GetUserProcs().GetHashArrayElement( temp2 );
833 while(pUserProc){
834 if( pUserProc->GetName() == temp2 &&pUserProc->GetParentClassPtr()==pobj_temp_c)
835 {
836 break;
837 }
838
839 pUserProc=pUserProc->GetChainNext();
840 }
841
842 CMethod *pMethod = new DynamicMethod( pUserProc, accessibility, 0,0,false, pobj_InheritsClass);
843
844 pobj_c->GetMethods().push_back( pMethod );
845 }
846
847 //静的メンバ
848 int nStaticMember = *(long *)(buffer+i2);
849 i2+=sizeof(long);
850 for( i4=0; i4<nStaticMember; i4++ ){
851 // 名前
852 string name = (char *)(buffer+i2);
853 i2+=lstrlen(buffer+i2)+1;
854
855 // 型
856 Type type( *(long *)(buffer+i2) );
857 i2+=sizeof(long);
858
859 // 型の拡張情報
860 GetLpIndex_DebugFile(buffer,&i2,type);
861
862 // アクセシビリティ
863 Prototype::Accessibility accessibility = *(Prototype::Accessibility *)(buffer+i2);
864 i2+=sizeof(Prototype::Accessibility);
865
866 // 配列
867 Subscripts subscripts;
868 int nSubScriptsNum = *(long *)(buffer+i2);
869 i2+=sizeof(long);
870 for( int i=0; i<nSubScriptsNum; i++ )
871 {
872 subscripts.push_back( *(long *)(buffer+i2) );
873 i2+=sizeof(long);
874 }
875
876 CMember *member=new CMember( accessibility, name, type, false, subscripts, "", "" );
877
878 pobj_c->GetStaticMembers().push_back( member );
879 }
880 }
881
882 HeapDefaultFree(buffer);
883 buffer=0;
884
885
886
887
888 compiler.objectModule.meta.GetUserProcs() = userProcs;
889 pSub_DebugSys_EndProc=GetSubHash("_DebugSys_EndProc");
890
891
892 SingleStepCodeBuffer=MakeSingleStepCode();
893
894
895 /////////////////////////////
896 // ブレークポイントを適用
897 /////////////////////////////
898
899 //インクルード情報
900 extern INCLUDEFILEINFO IncludeFileInfo;
901 IncludeFileInfo=this->IncludeFileInfo;
902
903 //コードと行番号の関係
904 extern SourceLines oldSourceLines;
905 oldSourceLines = this->oldSourceLines;
906
907 BreakStepCodeBuffer=pobj_DBBreakPoint->update(OpBuffer,SizeOf_CodeSection);
908
909 //プロセスメモリにコピー
910 extern HANDLE hDebugProcess;
911 SIZE_T accessBytes;
912 WriteProcessMemory(hDebugProcess,(void *)(ULONG_PTR)(dwImageBase+dwRVA_CodeSection),
913 BreakStepCodeBuffer,
914 SizeOf_CodeSection,&accessBytes);
915
916
917 return 1;
918}
919
920BOOL CDebugSection::load(HMODULE hModule){
921 if(buffer){
922 HeapDefaultFree(buffer);
923 buffer=0;
924 }
925
926
927 extern HANDLE hDebugProcess;
928 SIZE_T accessBytes;
929 IMAGE_DOS_HEADER ImageDosHeader;
930 ReadProcessMemory(hDebugProcess,hModule,&ImageDosHeader,sizeof(IMAGE_DOS_HEADER),&accessBytes);
931
932 int pe_size;
933#ifdef _AMD64_
934 IMAGE_NT_HEADERS64 pe_hdr;
935 pe_size=sizeof(IMAGE_NT_HEADERS64);
936#else
937 IMAGE_NT_HEADERS pe_hdr;
938 pe_size=sizeof(IMAGE_NT_HEADERS);
939#endif
940 ReadProcessMemory(hDebugProcess,(void *)(((ULONG_PTR)hModule)+ImageDosHeader.e_lfanew),&pe_hdr,pe_size,&accessBytes);
941
942 IMAGE_SECTION_HEADER *pSectionHdr;
943 pSectionHdr=(IMAGE_SECTION_HEADER *)HeapAlloc(hHeap,0,pe_hdr.FileHeader.NumberOfSections*sizeof(IMAGE_SECTION_HEADER));
944 ReadProcessMemory(hDebugProcess,
945 (void *)(((ULONG_PTR)hModule)+ImageDosHeader.e_lfanew+pe_size),
946 pSectionHdr,
947 pe_hdr.FileHeader.NumberOfSections*sizeof(IMAGE_SECTION_HEADER),
948 &accessBytes);
949
950 int i;
951 for(i=0;i<pe_hdr.FileHeader.NumberOfSections;i++){
952
953 //リライタブルセクション内の情報
954 if(lstrcmp((char *)pSectionHdr[i].Name,".data")==0){
955 dwRVA_RWSection=pSectionHdr[i].VirtualAddress;
956 }
957
958 //コードセクション内の情報
959 if(lstrcmp((char *)pSectionHdr[i].Name,".text")==0){
960 dwRVA_CodeSection=pSectionHdr[i].VirtualAddress;
961 SizeOf_CodeSection=pSectionHdr[i].SizeOfRawData;
962 }
963
964 //デバッグセクション内の情報
965 if(lstrcmp((char *)pSectionHdr[i].Name,".debug")==0){
966 length=pSectionHdr[i].Misc.VirtualSize;
967 buffer=(char *)HeapAlloc(hHeap,0,length+1);
968
969 ReadProcessMemory(hDebugProcess,
970 (void *)(((ULONG_PTR)hModule)+pSectionHdr[i].VirtualAddress),
971 buffer,
972 length,
973 &accessBytes);
974 buffer[length]=0;
975 }
976
977 }
978 HeapDefaultFree(pSectionHdr);
979
980 if(!buffer) return 0;
981
982
983 dwImageBase=(DWORD)(ULONG_PTR)hModule;
984
985
986
987 if(OpBuffer) free(OpBuffer);
988 OpBuffer=(char *)malloc(SizeOf_CodeSection);
989
990 ReadProcessMemory(hDebugProcess,
991 (void *)(ULONG_PTR)(dwImageBase+dwRVA_CodeSection),OpBuffer,
992 SizeOf_CodeSection,&accessBytes);
993
994
995 return __load();
996}
997
998void CDebugSection::choice(void){
999 //イメージベース
1000 extern DWORD ImageBase;
1001 ImageBase=this->dwImageBase;
1002
1003 //リライタブルセクションのRVA
1004 extern int MemPos_RWSection;
1005 MemPos_RWSection=this->dwRVA_RWSection;
1006
1007 //コードセクションのRVAとサイズ
1008 extern int MemPos_CodeSection;
1009 extern int FileSize_CodeSection;
1010 MemPos_CodeSection=this->dwRVA_CodeSection;
1011 FileSize_CodeSection=this->SizeOf_CodeSection;
1012
1013 //インクルード情報
1014 extern INCLUDEFILEINFO IncludeFileInfo;
1015 IncludeFileInfo=this->IncludeFileInfo;
1016
1017 //ソースコード
1018 Smoothie::Lexical::source = source;
1019
1020 //コードと行番号の関係
1021 extern SourceLines oldSourceLines;
1022 oldSourceLines = this->oldSourceLines;
1023
1024 // クラス情報
1025 compiler.objectModule.meta.SetClasses( this->pobj_DBClass );
1026
1027 //定数を取得
1028 compiler.objectModule.meta.GetGlobalConsts() = this->globalConsts;
1029 compiler.objectModule.meta.GetGlobalConstMacros() = this->globalConstMacros;
1030
1031 //グローバル実行領域のサイズ
1032 extern int GlobalOpBufferSize;
1033 GlobalOpBufferSize=this->GlobalOpBufferSize;
1034
1035 //プロシージャ
1036 compiler.objectModule.meta.GetUserProcs() = userProcs;
1037
1038 extern const UserProc *pSub_DebugSys_EndProc;
1039 pSub_DebugSys_EndProc=this->pSub_DebugSys_EndProc;
1040
1041 //ネイティブコードバッファ
1042 extern char *OpBuffer;
1043 OpBuffer=this->OpBuffer;
1044}
1045
1046void CDebugSection::DeleteDebugInfo(void){
1047 int i2;
1048
1049 //インクルード情報を解放
1050 for(i2=0;i2<IncludeFileInfo.FilesNum;i2++)
1051 {
1052 free(IncludeFileInfo.ppFileNames[i2]);
1053 }
1054 free(IncludeFileInfo.ppFileNames);
1055
1056 //クラスに関するメモリを解放
1057 delete this->pobj_DBClass;
1058 this->pobj_DBClass=0;
1059
1060 //コードバッファを解放
1061 free(OpBuffer);
1062 OpBuffer=0;
1063
1064 HeapDefaultFree(SingleStepCodeBuffer);
1065 SingleStepCodeBuffer=0;
1066
1067 HeapDefaultFree(BreakStepCodeBuffer);
1068 BreakStepCodeBuffer=0;
1069}
1070
1071
1072
1073CDBDebugSection::CDBDebugSection(){
1074 ppobj_ds=(CDebugSection **)HeapAlloc(hHeap,0,1);
1075 num=0;
1076}
1077CDBDebugSection::~CDBDebugSection(){
1078 int i;
1079 for(i=0;i<num;i++){
1080 delete ppobj_ds[i];
1081 }
1082 HeapDefaultFree(ppobj_ds);
1083}
1084
1085BOOL CDBDebugSection::add(HMODULE hModule){
1086 CDebugSection *pobj_d;
1087 pobj_d=new CDebugSection();
1088 if(!pobj_d->load(hModule)){
1089 //デバッグ情報が存在しないとき
1090 delete pobj_d;
1091 return 0;
1092 }
1093
1094 ppobj_ds=(CDebugSection **)HeapReAlloc(hHeap,0,ppobj_ds,(num+1)*sizeof(CDebugSection *));
1095 ppobj_ds[num]=pobj_d;
1096 num++;
1097
1098 return 1;
1099}
1100
1101void CDBDebugSection::del(HMODULE hModule){
1102 int i;
1103 for(i=0;i<num;i++){
1104 if((HMODULE)(ULONG_PTR)ppobj_ds[i]->dwImageBase==hModule){
1105 delete ppobj_ds[i];
1106
1107 num--;
1108 for(;i<num;i++){
1109 ppobj_ds[i]=ppobj_ds[i+1];
1110 }
1111 break;
1112 }
1113 }
1114}
1115
1116void CDBDebugSection::choice(int index){
1117 pobj_now=ppobj_ds[index];
1118 pobj_now->choice();
1119}
1120
1121
1122
1123CDBDebugSection *pobj_DBDebugSection;
Note: See TracBrowser for help on using the repository browser.