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