#include "stdafx.h" #include #include #include #include #include "../BasicCompiler_Common/common.h" #include "../BasicCompiler_Common/DebugSection.h" #ifdef _AMD64_ #include "../BasicCompiler64/opcode.h" #else #include "../BasicCompiler32/opcode.h" #endif #define MDLFILE_VER 0x70000003 void SetLpIndex_DebugFile(char *buffer,int *p,const Type &type){ if(NATURAL_TYPE(type.GetBasicType())==DEF_OBJECT || NATURAL_TYPE(type.GetBasicType())==DEF_STRUCT){ lstrcpy(buffer+(*p),type.GetClass().GetName().c_str()); (*p)+=lstrlen(buffer+(*p))+1; } else{ *(LONG_PTR *)(buffer+(*p))=type.GetIndex(); (*p)+=sizeof(LONG_PTR); } } void GetLpIndex_DebugFile(char *buffer,int *p,Type &type){ if(NATURAL_TYPE(type.GetBasicType())==DEF_OBJECT || NATURAL_TYPE(type.GetBasicType())==DEF_STRUCT){ char szClassName[VN_SIZE]; lstrcpy(szClassName,buffer+(*p)); (*p)+=lstrlen(buffer+(*p))+1; type.SetClassPtr( compiler.objectModule.meta.GetClasses().Find(szClassName) ); } else{ type.SetIndex( *(LONG_PTR *)(buffer+(*p)) ); (*p)+=sizeof(LONG_PTR); } } CDebugSection::~CDebugSection(){ if(pobj_DBClass) DeleteDebugInfo(); if(buffer){ HeapDefaultFree(buffer); buffer=0; } } void CDebugSection::make(void){ extern INCLUDEFILEINFO IncludeFileInfo; int i2,i3,BufferSize; if(buffer){ HeapDefaultFree(buffer); buffer=0; } i2=0; extern char *basbuf; BufferSize=lstrlen(basbuf)+65535; buffer=(char *)HeapAlloc(hHeap,0,BufferSize); //デバッグ用ファイルのバージョン *(long *)(buffer+i2)=MDLFILE_VER; i2+=sizeof(long); //プラットフォームのビット数 *(long *)(buffer+i2)=PLATFORM; i2+=sizeof(long); //インクルード情報 *(long *)(buffer+i2)=IncludeFileInfo.FilesNum; i2+=sizeof(long); for(i3=0;i3GetName().c_str()); i2+=lstrlen(buffer+i2)+1; } ////////////////// // TypeDef情報 ////////////////// *(long *)(buffer+i2)=(int)compiler.objectModule.meta.GetTypeDefs().size(); i2+=sizeof(long); for(i3=0;i3<(int)compiler.objectModule.meta.GetTypeDefs().size();i3++){ lstrcpy(buffer+i2,compiler.objectModule.meta.GetTypeDefs()[i3].GetName().c_str() ); i2+=lstrlen(buffer+i2)+1; lstrcpy(buffer+i2,compiler.objectModule.meta.GetTypeDefs()[i3].GetBaseName().c_str() ); i2+=lstrlen(buffer+i2)+1; //バッファが足りない場合は再確保 if(BufferSizeGetName().c_str()); i2+=lstrlen(buffer+i2)+1; //型 *(long *)(buffer+i2)=pVar->GetType().GetBasicType(); i2+=sizeof(long); //型の拡張情報 SetLpIndex_DebugFile(buffer,&i2,pVar->GetType()); buffer[i2++] = pVar->IsRef() ? 1 : 0; buffer[i2++] = pVar->IsArray() ? 1 : 0; if(pVar->IsArray()){ *(long *)(buffer+i2)=(int)pVar->GetSubscripts().size(); i2+=sizeof(long); BOOST_FOREACH( int indexMax, pVar->GetSubscripts() ) { *(long *)(buffer+i2)=indexMax; i2+=sizeof(long); } } //レキシカルスコープ情報 *(long *)(buffer+i2)=pVar->GetScopeStartAddress(); i2+=sizeof(long); *(long *)(buffer+i2)=pVar->GetScopeEndAddress(); i2+=sizeof(long); *(long *)(buffer+i2)=pVar->GetScopeLevel(); i2+=sizeof(long); //メモリ位置 *(long *)(buffer+i2)=pVar->GetOffsetAddress(); i2+=sizeof(long); //バッファが足りない場合は再確保 if(BufferSizeGetParentClassPtr()){ lstrcpy(buffer+i2,pUserProc->GetParentClassPtr()->GetName().c_str()); i2+=lstrlen(buffer+i2)+1; } else{ lstrcpy(buffer+i2,""); i2+=lstrlen(buffer+i2)+1; } //ID *(long *)(buffer+i2)=pUserProc->GetId(); i2+=sizeof(long); //関数名 lstrcpy(buffer+i2,pUserProc->GetName().c_str()); i2+=lstrlen(buffer+i2)+1; *(long *)(buffer+i2)=pUserProc->_beginOpAddressOld; i2+=sizeof(long); *(long *)(buffer+i2)=pUserProc->_endOpAddressOld; i2+=sizeof(long); //ローカル変数情報 *(long *)(buffer+i2)=(int)pUserProc->GetLocalVars().size(); i2+=sizeof(long); //バッファが足りない場合は再確保 if(BufferSizeGetLocalVars() ){ lstrcpy(buffer+i2,pVar->GetName().c_str()); i2+=lstrlen(buffer+i2)+1; //型 *(long *)(buffer+i2)=pVar->GetType().GetBasicType(); i2+=sizeof(long); //型の拡張情報 SetLpIndex_DebugFile(buffer,&i2,pVar->GetType()); //参照型パラメータかどうか buffer[i2++] = pVar->IsRef() ? 1 : 0; //配列かどうか buffer[i2++] = pVar->IsArray() ? 1 : 0; //配列要素 if(pVar->IsArray()){ *(long *)(buffer+i2)=(int)pVar->GetSubscripts().size(); i2+=sizeof(long); BOOST_FOREACH( int indexMax, pVar->GetSubscripts() ) { *(long *)(buffer+i2)=indexMax; i2+=sizeof(long); } } //レキシカルスコープ情報 *(long *)(buffer+i2)=pVar->GetScopeStartAddress(); i2+=sizeof(long); *(long *)(buffer+i2)=pVar->GetScopeEndAddress(); i2+=sizeof(long); *(long *)(buffer+i2)=pVar->GetScopeLevel(); i2+=sizeof(long); //メモリ位置 *(long *)(buffer+i2)=pVar->GetOffsetAddress(); i2+=sizeof(long); //バッファが足りない場合は再確保 if(BufferSizeGetName().c_str()); i2+=lstrlen(buffer+i2)+1; //仮想関数の数 *(long *)(buffer+i2)=pobj_c->GetVtblNum(); i2+=sizeof(long); //アラインメント *(long *)(buffer+i2)=pobj_c->GetFixedAlignment(); i2+=sizeof(long); // 動的メンバ *(long *)(buffer+i2)=(int)pobj_c->GetDynamicMembers().size(); i2+=sizeof(long); BOOST_FOREACH( CMember *member, pobj_c->GetDynamicMembers() ){ // 名前 lstrcpy(buffer+i2,member->GetName().c_str()); i2+=lstrlen(buffer+i2)+1; // 型 *(long *)(buffer+i2)=member->GetType().GetBasicType(); i2+=sizeof(long); // 型の拡張情報 SetLpIndex_DebugFile(buffer,&i2,member->GetType()); // アクセシビリティ *(Prototype::Accessibility *)(buffer+i2)=member->GetAccessibility(); i2+=sizeof(Prototype::Accessibility); *(long *)(buffer+i2)=(int)member->GetSubscripts().size(); i2+=sizeof(long); BOOST_FOREACH( int indexMax, member->GetSubscripts() ) { *(long *)(buffer+i2)=indexMax; i2+=sizeof(long); } //バッファが足りない場合は再確保 if(BufferSizeGetMethods().size(); i2+=sizeof(long); BOOST_FOREACH( const CMethod *pMethod, pobj_c->GetMethods() ){ *(Prototype::Accessibility *)(buffer+i2)=pMethod->GetAccessibility(); i2+=sizeof(Prototype::Accessibility); if( pMethod->GetInheritsClassPtr() ){ lstrcpy(buffer+i2,pMethod->GetInheritsClassPtr()->GetName().c_str()); i2+=lstrlen(buffer+i2)+1; } else{ lstrcpy(buffer+i2,""); i2+=lstrlen(buffer+i2)+1; } lstrcpy(buffer+i2,pMethod->GetUserProc().GetName().c_str()); i2+=lstrlen(buffer+i2)+1; } //静的メンバ *(long *)(buffer+i2)=(long)pobj_c->GetStaticMembers().size(); i2+=sizeof(long); BOOST_FOREACH( CMember *member, pobj_c->GetStaticMembers() ){ // 名前 lstrcpy(buffer+i2,member->GetName().c_str()); i2+=lstrlen(buffer+i2)+1; // 型 *(long *)(buffer+i2)=member->GetType().GetBasicType(); i2+=sizeof(long); // 型の拡張情報 SetLpIndex_DebugFile(buffer,&i2,member->GetType()); // アクセシビリティ *(Prototype::Accessibility *)(buffer+i2)=member->GetAccessibility(); i2+=sizeof(Prototype::Accessibility); // 配列 *(long *)(buffer+i2)=(int)member->GetSubscripts().size(); i2+=sizeof(long); BOOST_FOREACH( int indexMax, member->GetSubscripts() ) { *(long *)(buffer+i2)=indexMax; i2+=sizeof(long); } //バッファが足りない場合は再確保 if(BufferSizepobj_DBClass=new Classes(); int iMaxClassCount; iMaxClassCount=*(long *)(buffer+i2); i2+=sizeof(long); for(i3=0;i3pobj_DBClass->Add(NamespaceScopes(),NamespaceScopesCollection(),buffer+i2,0); i2+=lstrlen(buffer+i2)+1; } compiler.objectModule.meta.SetClasses( this->pobj_DBClass ); ////////////////// // TypeDef情報 ////////////////// //初期化 compiler.objectModule.meta.GetTypeDefs().clear(); //個数を取得 num=*(long *)(buffer+i2); i2+=sizeof(long); for(i3=0;i3globalConsts = compiler.objectModule.meta.GetGlobalConsts(); this->globalConstMacros = compiler.objectModule.meta.GetGlobalConstMacros(); //グローバル変数情報 compiler.objectModule.meta.GetGlobalVars().clear(); int maxGlobalVars=*(long *)(buffer+i2); i2+=sizeof(long); for(i3=0;i3SetArray( subscripts ); } //レキシカルスコープ情報 pVar->SetScopeStartAddress( *(long *)(buffer+i2) ); i2+=sizeof(long); pVar->SetScopeEndAddress( *(long *)(buffer+i2) ); i2+=sizeof(long); pVar->SetScopeLevel( *(long *)(buffer+i2) ); i2+=sizeof(long); //メモリ位置 pVar->SetOffsetAddress( *(long *)(buffer+i2) ); i2+=sizeof(long); //変数を追加 compiler.objectModule.meta.GetGlobalVars().push_back( pVar ); } //グローバル実行領域のサイズ GlobalOpBufferSize=*(long *)(buffer+i2); i2+=sizeof(long); //プロシージャ情報 userProcs.Clear(); int subNum = *(long *)(buffer+i2); i2+=sizeof(long); for(int i6=0;i6SetParentClass( pClass ); pUserProc->_beginOpAddressOld = *(long *)(buffer+i2); i2+=sizeof(long); pUserProc->_endOpAddressOld = *(long *)(buffer+i2); i2+=sizeof(long); pUserProc->CompleteCompile(); //ローカル変数情報 pUserProc->GetLocalVars().clear(); int maxLocalVar=*(long *)(buffer+i2); i2+=sizeof(long); for(i3=0;i3SetArray( subscripts ); } //レキシカルスコープ情報 pVar->SetScopeStartAddress( *(long *)(buffer+i2) ); i2+=sizeof(long); pVar->SetScopeEndAddress( *(long *)(buffer+i2) ); i2+=sizeof(long); pVar->SetScopeLevel( *(long *)(buffer+i2) ); i2+=sizeof(long); //メモリ位置 pVar->SetOffsetAddress( *(long *)(buffer+i2) ); i2+=sizeof(long); //変数を追加 pUserProc->GetLocalVars().push_back( pVar ); } ///////////////////////////////// // 格納位置を計算してpUserProcをセット ///////////////////////////////// // ハッシュに追加 if( !userProcs.Insert( pUserProc, -1 ) ) { //return NULL; } } userProcs.Iterator_Init(); //クラス情報 CClass *pobj_c; for(i3=0;i3( compiler.objectModule.meta.GetClasses().Find(szClassName) ); //仮想関数の数 pobj_c->SetVtblNum( *(long *)(buffer+i2) ); i2+=sizeof(long); //アラインメント pobj_c->SetFixedAlignment( *(long *)(buffer+i2) ); i2+=sizeof(long); // 動的メンバ int nDynamicMember = *(long *)(buffer+i2); i2+=sizeof(long); for( i4=0; i4GetDynamicMembers().push_back( member ); } // 動的メソッド int nMethod = *(long *)(buffer+i2); i2+=sizeof(long); for( i4=0; i4GetName() == temp2 &&pUserProc->GetParentClassPtr()==pobj_temp_c) { break; } pUserProc=pUserProc->GetChainNext(); } CMethod *pMethod = new DynamicMethod( pUserProc, accessibility, 0,0,false, pobj_InheritsClass); pobj_c->GetMethods().push_back( pMethod ); } //静的メンバ int nStaticMember = *(long *)(buffer+i2); i2+=sizeof(long); for( i4=0; i4GetStaticMembers().push_back( member ); } } HeapDefaultFree(buffer); buffer=0; compiler.objectModule.meta.GetUserProcs() = userProcs; pSub_DebugSys_EndProc=GetSubHash("_DebugSys_EndProc"); SingleStepCodeBuffer=MakeSingleStepCode(); ///////////////////////////// // ブレークポイントを適用 ///////////////////////////// //インクルード情報 extern INCLUDEFILEINFO IncludeFileInfo; IncludeFileInfo=this->IncludeFileInfo; //コードと行番号の関係 extern SourceLines oldSourceLines; oldSourceLines = this->oldSourceLines; BreakStepCodeBuffer=pobj_DBBreakPoint->update(OpBuffer,SizeOf_CodeSection); //プロセスメモリにコピー extern HANDLE hDebugProcess; SIZE_T accessBytes; WriteProcessMemory(hDebugProcess,(void *)(ULONG_PTR)(dwImageBase+dwRVA_CodeSection), BreakStepCodeBuffer, SizeOf_CodeSection,&accessBytes); return 1; } BOOL CDebugSection::load(HMODULE hModule){ if(buffer){ HeapDefaultFree(buffer); buffer=0; } extern HANDLE hDebugProcess; SIZE_T accessBytes; IMAGE_DOS_HEADER ImageDosHeader; ReadProcessMemory(hDebugProcess,hModule,&ImageDosHeader,sizeof(IMAGE_DOS_HEADER),&accessBytes); int pe_size; #ifdef _AMD64_ IMAGE_NT_HEADERS64 pe_hdr; pe_size=sizeof(IMAGE_NT_HEADERS64); #else IMAGE_NT_HEADERS pe_hdr; pe_size=sizeof(IMAGE_NT_HEADERS); #endif ReadProcessMemory(hDebugProcess,(void *)(((ULONG_PTR)hModule)+ImageDosHeader.e_lfanew),&pe_hdr,pe_size,&accessBytes); IMAGE_SECTION_HEADER *pSectionHdr; pSectionHdr=(IMAGE_SECTION_HEADER *)HeapAlloc(hHeap,0,pe_hdr.FileHeader.NumberOfSections*sizeof(IMAGE_SECTION_HEADER)); ReadProcessMemory(hDebugProcess, (void *)(((ULONG_PTR)hModule)+ImageDosHeader.e_lfanew+pe_size), pSectionHdr, pe_hdr.FileHeader.NumberOfSections*sizeof(IMAGE_SECTION_HEADER), &accessBytes); int i; for(i=0;idwImageBase; //リライタブルセクションのRVA extern int MemPos_RWSection; MemPos_RWSection=this->dwRVA_RWSection; //コードセクションのRVAとサイズ extern int MemPos_CodeSection; extern int FileSize_CodeSection; MemPos_CodeSection=this->dwRVA_CodeSection; FileSize_CodeSection=this->SizeOf_CodeSection; //インクルード情報 extern INCLUDEFILEINFO IncludeFileInfo; IncludeFileInfo=this->IncludeFileInfo; //ソースコード Smoothie::Lexical::source = source; //コードと行番号の関係 extern SourceLines oldSourceLines; oldSourceLines = this->oldSourceLines; // クラス情報 compiler.objectModule.meta.SetClasses( this->pobj_DBClass ); //定数を取得 compiler.objectModule.meta.GetGlobalConsts() = this->globalConsts; compiler.objectModule.meta.GetGlobalConstMacros() = this->globalConstMacros; //グローバル実行領域のサイズ extern int GlobalOpBufferSize; GlobalOpBufferSize=this->GlobalOpBufferSize; //プロシージャ compiler.objectModule.meta.GetUserProcs() = userProcs; extern const UserProc *pSub_DebugSys_EndProc; pSub_DebugSys_EndProc=this->pSub_DebugSys_EndProc; //ネイティブコードバッファ extern char *OpBuffer; OpBuffer=this->OpBuffer; } void CDebugSection::DeleteDebugInfo(void){ int i2; //インクルード情報を解放 for(i2=0;i2pobj_DBClass; this->pobj_DBClass=0; //コードバッファを解放 free(OpBuffer); OpBuffer=0; HeapDefaultFree(SingleStepCodeBuffer); SingleStepCodeBuffer=0; HeapDefaultFree(BreakStepCodeBuffer); BreakStepCodeBuffer=0; } CDBDebugSection::CDBDebugSection(){ ppobj_ds=(CDebugSection **)HeapAlloc(hHeap,0,1); num=0; } CDBDebugSection::~CDBDebugSection(){ int i; for(i=0;iload(hModule)){ //デバッグ情報が存在しないとき delete pobj_d; return 0; } ppobj_ds=(CDebugSection **)HeapReAlloc(hHeap,0,ppobj_ds,(num+1)*sizeof(CDebugSection *)); ppobj_ds[num]=pobj_d; num++; return 1; } void CDBDebugSection::del(HMODULE hModule){ int i; for(i=0;idwImageBase==hModule){ delete ppobj_ds[i]; num--; for(;ichoice(); } CDBDebugSection *pobj_DBDebugSection;