#include "stdafx.h" #include "../BasicCompiler_Common/DebugSection.h" #ifdef _AMD64_ #include "../compiler_x64/opcode.h" #else #include "../compiler_x86/opcode.h" #endif #define MDLFILE_VER 0x70000003 using namespace ActiveBasic::Compiler; 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.GetObjectModule().meta.GetClasses().FindEx( LexicalAnalyzer::FullNameToSymbol( szClassName ) ) ); } else{ type.SetIndex( *(LONG_PTR *)(buffer+(*p)) ); (*p)+=sizeof(LONG_PTR); } } CDebugSection::~CDebugSection(){ if(dwImageBase) DeleteDebugInfo(); if(buffer){ HeapDefaultFree(buffer); buffer=0; } } void CDebugSection::make(void){ int i2,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); // オブジェクトモジュール { // テキストデータにシリアライズ std::string textString; compiler.GetObjectModule().WriteString( textString ); // サイズ *(long *)(buffer+i2) = (long)textString.size(); i2+=sizeof(long); //バッファが足りない場合は再確保 if(BufferSizeGetSource() ); } // テキストデータにシリアライズ std::string textString; sources.WriteBinaryString( textString ); // サイズ *(long *)(buffer+i2) = (long)textString.size(); i2+=sizeof(long); //バッファが足りない場合は再確保 if(BufferSizeobjectModule.ReadString( textString ); compiler.SelectObjectModule( &this->objectModule ); } // オブジェクトモジュールリストに類似したソースコードリスト { // サイズ int size = *(long *)(buffer+i2); i2 += sizeof(long); // バッファ const std::string textString( (const char *)(buffer + i2), size ); i2 += size; // テキストデータからシリアライズ this->_sourcesLinkRelationalObjectModule.ReadBinaryString( textString ); } //コードと行番号の関係 int maxLineInfoNum = *(long *)(buffer+i2); i2+=sizeof(long); for(i3=0;i3pSub_DebugSys_EndProc=GetSubHash("_DebugSys_EndProc"); if( this->pSub_DebugSys_EndProc == NULL ) { MessageBox( 0, "_DebugSys_EndProcが見つからない", "ActiveBasic", MB_OK ); } SingleStepCodeBuffer=MakeSingleStepCode(); //ソースコード extern char *basbuf; basbuf = const_cast(compiler.GetObjectModule().GetSource().GetBuffer()); ///////////////////////////// // ブレークポイントを適用 ///////////////////////////// // オブジェクトモジュールリストに類似したソースコードリスト extern BasicSources sourcesLinkRelationalObjectModule; sourcesLinkRelationalObjectModule = this->_sourcesLinkRelationalObjectModule; //コードと行番号の関係 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; // オブジェクトモジュール compiler.SelectObjectModule( &this->objectModule ); //コードと行番号の関係 extern SourceLines oldSourceLines; oldSourceLines = this->_oldSourceLines; //グローバル実行領域のサイズ extern int GlobalOpBufferSize; GlobalOpBufferSize=this->GlobalOpBufferSize; extern const UserProc *pSub_DebugSys_EndProc; pSub_DebugSys_EndProc=this->pSub_DebugSys_EndProc; //ネイティブコードバッファ extern char *OpBuffer; OpBuffer=this->OpBuffer; } void CDebugSection::DeleteDebugInfo(void){ //コードバッファを解放 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;