[206] | 1 | #include "stdafx.h"
|
---|
| 2 |
|
---|
[4] | 3 | #include "common.h"
|
---|
[206] | 4 | #include "DebugSection.h"
|
---|
[4] | 5 |
|
---|
| 6 |
|
---|
| 7 | //デバッグ用
|
---|
| 8 | #include "../BasicCompiler_Common/debug.h"
|
---|
| 9 |
|
---|
| 10 |
|
---|
| 11 | extern HANDLE hDebugProcess;
|
---|
[741] | 12 | extern DebugSectionCollection debugSectionCollection;
|
---|
[4] | 13 |
|
---|
| 14 | CDebugThreadInfo *pobj_dti;
|
---|
| 15 |
|
---|
| 16 | CDebugThreadInfo::CDebugThreadInfo(){
|
---|
| 17 | memset(this,0,sizeof(CDebugThreadInfo));
|
---|
| 18 | }
|
---|
| 19 | CDebugThreadInfo::~CDebugThreadInfo(){
|
---|
| 20 | Free();
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | BOOL CDebugThreadInfo::Reflesh(int ThreadNum){
|
---|
[759] | 24 | #if defined _WIN64 || !defined BUILD_X64_COMPILER
|
---|
[4] | 25 | Free();
|
---|
| 26 |
|
---|
| 27 | ULONG_PTR lpData;
|
---|
| 28 | ULONG_PTR lpAccBytes;
|
---|
| 29 |
|
---|
| 30 | DWORD dwRWSectionPos;
|
---|
[741] | 31 | dwRWSectionPos=debugSectionCollection.debugSections[0]->dwImageBase+
|
---|
| 32 | debugSectionCollection.debugSections[0]->dwRVA_RWSection;
|
---|
[4] | 33 |
|
---|
| 34 | ReadProcessMemory(hDebugProcess,
|
---|
| 35 | (void *)(dwRWSectionPos+sizeof(DWORD)*256+ThreadNum*sizeof(DWORD)),
|
---|
| 36 | &iProcLevel,
|
---|
| 37 | sizeof(DWORD),&lpAccBytes);
|
---|
| 38 | lplpObp=(ULONG_PTR *)HeapAlloc(hHeap,0,(iProcLevel+1)*sizeof(ULONG_PTR));
|
---|
| 39 | lplpSpBase=(ULONG_PTR *)HeapAlloc(hHeap,0,(iProcLevel+1)*sizeof(ULONG_PTR));
|
---|
| 40 | lpdwCp=(DWORD *)HeapAlloc(hHeap,0,(iProcLevel+1)*sizeof(DWORD));
|
---|
[636] | 41 | this->relationalObjectModuleIndexes.resize( iProcLevel + 1 );
|
---|
[4] | 42 |
|
---|
| 43 | //lplpObp
|
---|
| 44 | ReadProcessMemory(hDebugProcess,
|
---|
| 45 | (void *)(dwRWSectionPos+sizeof(DWORD)*256*2+ThreadNum*sizeof(DWORD)),
|
---|
| 46 | &lpData,
|
---|
| 47 | sizeof(ULONG_PTR),&lpAccBytes);
|
---|
| 48 | ReadProcessMemory(hDebugProcess,
|
---|
| 49 | (void *)lpData,
|
---|
| 50 | lplpObp,
|
---|
| 51 | (iProcLevel+1)*sizeof(ULONG_PTR),&lpAccBytes);
|
---|
| 52 |
|
---|
| 53 | //lplpSpBase
|
---|
| 54 | ReadProcessMemory(hDebugProcess,
|
---|
| 55 | (void *)(dwRWSectionPos+sizeof(DWORD)*256*2+sizeof(ULONG_PTR)*256+ThreadNum*sizeof(ULONG_PTR)),
|
---|
| 56 | &lpData,
|
---|
| 57 | sizeof(ULONG_PTR),&lpAccBytes);
|
---|
| 58 | ReadProcessMemory(hDebugProcess,
|
---|
| 59 | (void *)lpData,
|
---|
| 60 | lplpSpBase,
|
---|
| 61 | (iProcLevel+1)*sizeof(ULONG_PTR),&lpAccBytes);
|
---|
| 62 |
|
---|
| 63 | //現在の実行情報
|
---|
| 64 | CONTEXT Context;
|
---|
| 65 | Context.ContextFlags=CONTEXT_CONTROL;
|
---|
| 66 | GetThreadContext(array_hDebugThread[ThreadNum],&Context);
|
---|
| 67 |
|
---|
| 68 |
|
---|
| 69 | //マッピングされているモジュールのイメージベースを取得
|
---|
| 70 | HMODULE array_hModule[1024];
|
---|
| 71 | DWORD cbReturned;
|
---|
| 72 | if(!EnumProcessModules( hDebugProcess, array_hModule, sizeof(HMODULE)*1024, &cbReturned )) return 0;
|
---|
| 73 |
|
---|
| 74 | int i;
|
---|
| 75 | HMODULE hTargetModule=0;
|
---|
| 76 | for(i=0;i<(int)(cbReturned/sizeof(HMODULE));i++){
|
---|
| 77 | if((LONG_PTR)array_hModule[i]<=(LONG_PTR)EIP_RIP(Context)){
|
---|
| 78 | if(hTargetModule<array_hModule[i]) hTargetModule=array_hModule[i];
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | extern DWORD ImageBase;
|
---|
| 83 | if((HMODULE)(ULONG_PTR)ImageBase!=hTargetModule){
|
---|
[741] | 84 | for(i=0;i<debugSectionCollection.debugSections.size();i++){
|
---|
| 85 | if((HMODULE)(ULONG_PTR)debugSectionCollection.debugSections[i]->dwImageBase==hTargetModule){
|
---|
| 86 | debugSectionCollection.choice(i);
|
---|
[4] | 87 | break;
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 |
|
---|
| 93 |
|
---|
| 94 | extern int MemPos_CodeSection;
|
---|
| 95 | extern int FileSize_CodeSection;
|
---|
| 96 |
|
---|
| 97 | if(ImageBase+MemPos_CodeSection <= EIP_RIP(Context) &&
|
---|
| 98 | EIP_RIP(Context) < ImageBase+MemPos_CodeSection+FileSize_CodeSection){
|
---|
| 99 | lplpObp[iProcLevel]=EIP_RIP(Context);
|
---|
| 100 | lplpSpBase[iProcLevel]=SP_BASE(Context);
|
---|
| 101 | }
|
---|
| 102 | //else API関数内などの場合は既存の lplpObp[iProcLevel] を利用する
|
---|
| 103 |
|
---|
| 104 | return 1;
|
---|
[759] | 105 | #else
|
---|
| 106 | return 0;
|
---|
| 107 | #endif
|
---|
[4] | 108 | }
|
---|
| 109 |
|
---|
| 110 | void CDebugThreadInfo::Free(void){
|
---|
| 111 | if(lplpObp){
|
---|
| 112 | HeapDefaultFree(lplpObp);
|
---|
| 113 | HeapDefaultFree(lplpSpBase);
|
---|
| 114 | HeapDefaultFree(lpdwCp);
|
---|
[636] | 115 | relationalObjectModuleIndexes.clear();
|
---|
[4] | 116 | }
|
---|
| 117 |
|
---|
| 118 | memset(this,0,sizeof(CDebugThreadInfo));
|
---|
| 119 | }
|
---|