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