1 | #include "stdafx.h"
|
---|
2 |
|
---|
3 | #include <Compiler.h>
|
---|
4 |
|
---|
5 | #include "../BasicCompiler_Common/common.h"
|
---|
6 | #include "../BasicCompiler_Common/DebugSection.h"
|
---|
7 |
|
---|
8 | //デバッグ用
|
---|
9 | #include "../BasicCompiler_Common/debug.h"
|
---|
10 |
|
---|
11 | extern char *OpBuffer;
|
---|
12 |
|
---|
13 | extern DWORD dwStepRun;
|
---|
14 |
|
---|
15 | HANDLE hDebugProcess;
|
---|
16 |
|
---|
17 | DWORD _DebugSys_dwThreadID[MAX_DEBUG_THREAD];
|
---|
18 | HANDLE array_hDebugThread[MAX_DEBUG_THREAD];
|
---|
19 |
|
---|
20 | int StepCursorObpSchedule=-1;
|
---|
21 | int StepCursor_BackupChar;
|
---|
22 |
|
---|
23 | int NextStepThreadNum;
|
---|
24 |
|
---|
25 |
|
---|
26 | void Debugger_StepIn(void){
|
---|
27 | dwStepRun=1;
|
---|
28 | }
|
---|
29 | void Debugger_StepOver(void){
|
---|
30 | dwStepRun=2;
|
---|
31 | }
|
---|
32 |
|
---|
33 | void Debugger_StepCursor(void){
|
---|
34 | char temporary[MAX_PATH];
|
---|
35 | GetTempPath(MAX_PATH,temporary);
|
---|
36 | if(temporary[lstrlen(temporary)-1]!='\\') lstrcat(temporary,"\\");
|
---|
37 | lstrcat(temporary,"ab_breakpoint.tmp");
|
---|
38 |
|
---|
39 | char *buffer;
|
---|
40 | buffer=ReadBuffer_NonErrMsg(temporary);
|
---|
41 |
|
---|
42 | int i=0;
|
---|
43 | char szFilePath[MAX_PATH];
|
---|
44 | i=GetOneParameter(buffer,i,szFilePath);
|
---|
45 | RemoveStringQuotes(szFilePath);
|
---|
46 |
|
---|
47 | int iLineNum;
|
---|
48 | i=GetOneParameter(buffer,i,temporary);
|
---|
49 | iLineNum=atoi(temporary);
|
---|
50 |
|
---|
51 |
|
---|
52 |
|
---|
53 | /////////////////////////////////////////////////////////
|
---|
54 | // デバッグ中プロセスのネイティブバッファ領域を更新
|
---|
55 | /////////////////////////////////////////////////////////
|
---|
56 |
|
---|
57 | extern INCLUDEFILEINFO IncludeFileInfo;
|
---|
58 |
|
---|
59 | int FileNum;
|
---|
60 | for(FileNum=0;FileNum<IncludeFileInfo.FilesNum;FileNum++){
|
---|
61 | if(lstrcmpi(IncludeFileInfo.ppFileNames[FileNum],szFilePath)==0) break;
|
---|
62 | }
|
---|
63 | if(FileNum==IncludeFileInfo.FilesNum) return;
|
---|
64 |
|
---|
65 | for(i=0;;i++){
|
---|
66 | if(IncludeFileInfo.LineOfFile[i]==FileNum||
|
---|
67 | IncludeFileInfo.LineOfFile[i]==-1) break;
|
---|
68 | }
|
---|
69 | if(IncludeFileInfo.LineOfFile[i]==-1) return;
|
---|
70 |
|
---|
71 | int FileBaseLine;
|
---|
72 | FileBaseLine=i;
|
---|
73 |
|
---|
74 | int i2;
|
---|
75 | for(i2=0;;i++,i2++){
|
---|
76 | if(FileNum<IncludeFileInfo.LineOfFile[i]){
|
---|
77 | while(FileNum!=IncludeFileInfo.LineOfFile[i]) i++;
|
---|
78 | }
|
---|
79 |
|
---|
80 | if(i2==iLineNum){
|
---|
81 | extern SourceLines oldSourceLines;
|
---|
82 |
|
---|
83 | loop:
|
---|
84 | int tempCp;
|
---|
85 | tempCp=GetCpFromLine(FileBaseLine+i2);
|
---|
86 |
|
---|
87 | int i3;
|
---|
88 | for(i3=0;i3<(int)oldSourceLines.size()-1;i3++){
|
---|
89 | if(oldSourceLines[i3].GetSourceCodePos()==tempCp) break;
|
---|
90 | }
|
---|
91 | if(i3==oldSourceLines.size()-1){
|
---|
92 | i2--;
|
---|
93 | goto loop;
|
---|
94 | }
|
---|
95 |
|
---|
96 | StepCursorObpSchedule=oldSourceLines[i3].GetNativeCodePos();
|
---|
97 | StepCursor_BackupChar=pobj_DBDebugSection->pobj_now->BreakStepCodeBuffer[StepCursorObpSchedule];
|
---|
98 |
|
---|
99 | pobj_DBDebugSection->pobj_now->BreakStepCodeBuffer[StepCursorObpSchedule]=(char)0xCC;
|
---|
100 |
|
---|
101 | break;
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | extern HWND hDebugWnd;
|
---|
106 | SendMessage(hDebugWnd,WM_DEBUG_CONTINUE,0,0);
|
---|
107 |
|
---|
108 |
|
---|
109 |
|
---|
110 | HeapDefaultFree(buffer);
|
---|
111 | }
|
---|
112 | void Debugger_Stop(void){
|
---|
113 | //プロセスを終了
|
---|
114 | TerminateProcess(hDebugProcess,0);
|
---|
115 | hDebugProcess=0;
|
---|
116 |
|
---|
117 | //デバッグダイアログを終了
|
---|
118 | extern HWND hDebugWnd;
|
---|
119 | if(hDebugWnd){
|
---|
120 | DestroyWindow(hDebugWnd);
|
---|
121 | }
|
---|
122 | }
|
---|
123 | void Debugger_Pause(void){
|
---|
124 | ///////////////////////////
|
---|
125 | // デバッグを一時中断
|
---|
126 | ///////////////////////////
|
---|
127 |
|
---|
128 | //ターゲットプロセス内のスレッドを一時中断
|
---|
129 | int i;
|
---|
130 | for(i=0;i<MAX_DEBUG_THREAD;i++){
|
---|
131 | if(_DebugSys_dwThreadID[i])
|
---|
132 | SuspendThread(array_hDebugThread[i]);
|
---|
133 | }
|
---|
134 |
|
---|
135 | //デバッグレジスタにフラグをセット
|
---|
136 | CONTEXT Context;
|
---|
137 | for(i=0;i<MAX_DEBUG_THREAD;i++){
|
---|
138 | if(_DebugSys_dwThreadID[i]){
|
---|
139 | Context.ContextFlags=CONTEXT_CONTROL|CONTEXT_DEBUG_REGISTERS;
|
---|
140 | GetThreadContext(array_hDebugThread[i],&Context);
|
---|
141 | Context.EFlags|=0x100;
|
---|
142 | SetThreadContext(array_hDebugThread[i],&Context);
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | //スレッドを再開
|
---|
147 | for(i=0;i<MAX_DEBUG_THREAD;i++){
|
---|
148 | if(_DebugSys_dwThreadID[i])
|
---|
149 | ResumeThread(array_hDebugThread[i]);
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | void ReleaseCPUSingleStep(void){
|
---|
154 | ////////////////////////////////////////
|
---|
155 | // CPU機構のシングルステップを解除
|
---|
156 | ////////////////////////////////////////
|
---|
157 |
|
---|
158 | //ターゲットプロセス内のスレッドを一時中断
|
---|
159 | int i;
|
---|
160 | for(i=0;i<MAX_DEBUG_THREAD;i++){
|
---|
161 | if(_DebugSys_dwThreadID[i])
|
---|
162 | SuspendThread(array_hDebugThread[i]);
|
---|
163 | }
|
---|
164 |
|
---|
165 | //デバッグレジスタにフラグをセット
|
---|
166 | CONTEXT Context;
|
---|
167 | for(i=0;i<MAX_DEBUG_THREAD;i++){
|
---|
168 | if(_DebugSys_dwThreadID[i]){
|
---|
169 | Context.ContextFlags=CONTEXT_CONTROL|CONTEXT_DEBUG_REGISTERS;
|
---|
170 | GetThreadContext(array_hDebugThread[i],&Context);
|
---|
171 | Context.EFlags&=~0x00000100;
|
---|
172 | SetThreadContext(array_hDebugThread[i],&Context);
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | //スレッドを再開
|
---|
177 | for(i=0;i<MAX_DEBUG_THREAD;i++){
|
---|
178 | if(_DebugSys_dwThreadID[i])
|
---|
179 | ResumeThread(array_hDebugThread[i]);
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 |
|
---|
184 | ULONG_PTR rva_to_real(DWORD p){
|
---|
185 | extern DWORD ImageBase;
|
---|
186 | extern int MemPos_CodeSection;
|
---|
187 | return p+ImageBase+MemPos_CodeSection;
|
---|
188 | }
|
---|
189 |
|
---|
190 | void ShowVarList(DEBUG_EVENT *pde,BOOL bExit){
|
---|
191 | extern DWORD ImageBase;
|
---|
192 | extern int MemPos_RWSection;
|
---|
193 |
|
---|
194 | //デバッグダイアログを表示
|
---|
195 | extern HINSTANCE hInst;
|
---|
196 | extern HWND hMainDlg;
|
---|
197 | extern HWND hDebugWnd;
|
---|
198 | dwStepRun=0;
|
---|
199 | if(!hDebugWnd) SendMessage(hMainDlg,WM_SHOWVARLIST,0,pde->dwThreadId);
|
---|
200 | else InitVarList(pde->dwThreadId);
|
---|
201 |
|
---|
202 | if(bExit){
|
---|
203 | //"中断"
|
---|
204 | SetDlgItemText(hMainDlg,IDOK,STRING_CLOSE);
|
---|
205 |
|
---|
206 | extern HWND hDebuggerToolbar;
|
---|
207 | SendMessage(hDebuggerToolbar,TB_SETSTATE,IDC_DEBUG_START,TBSTATE_INDETERMINATE);
|
---|
208 | SendMessage(hDebuggerToolbar,TB_SETSTATE,IDC_DEBUG_STEPOVER,TBSTATE_INDETERMINATE);
|
---|
209 | SendMessage(hDebuggerToolbar,TB_SETSTATE,IDC_DEBUG_STEPIN,TBSTATE_INDETERMINATE);
|
---|
210 | }
|
---|
211 | else{
|
---|
212 | //"継続"
|
---|
213 | SetDlgItemText(hDebugWnd,IDCANCEL,STRING_CONTINUE);
|
---|
214 | }
|
---|
215 | while(hDebugWnd&&dwStepRun==0) Sleep(1);
|
---|
216 | }
|
---|
217 | void DebugMessage(char *buffer){
|
---|
218 | extern HWND hMainDlg;
|
---|
219 | int pos;
|
---|
220 |
|
---|
221 | if(!IsWindowEnabled(GetDlgItem(hMainDlg,IDC_DEBUGLIST))){
|
---|
222 | SetDlgItemText(hMainDlg,IDC_DEBUGLIST,"");
|
---|
223 | EnableWindow(GetDlgItem(hMainDlg,IDC_DEBUGLIST),1);
|
---|
224 | }
|
---|
225 |
|
---|
226 | pos=GetWindowTextLength(GetDlgItem(hMainDlg,IDC_DEBUGLIST));
|
---|
227 | SendDlgItemMessage(hMainDlg,IDC_DEBUGLIST,EM_SETSEL,pos,pos);
|
---|
228 | SendDlgItemMessage(hMainDlg,IDC_DEBUGLIST,EM_REPLACESEL,0,(LPARAM)buffer);
|
---|
229 | }
|
---|
230 | UserProc *GetSubFromObp(ULONG_PTR pos){
|
---|
231 | compiler.objectModule.meta.GetUserProcs().Iterator_Reset();
|
---|
232 | while( compiler.objectModule.meta.GetUserProcs().Iterator_HasNext() )
|
---|
233 | {
|
---|
234 | UserProc *pUserProc = compiler.objectModule.meta.GetUserProcs().Iterator_GetNext();
|
---|
235 |
|
---|
236 | if(rva_to_real(pUserProc->_beginOpAddressOld) <= pos &&
|
---|
237 | pos < rva_to_real(pUserProc->_endOpAddressOld))
|
---|
238 | {
|
---|
239 | return pUserProc;
|
---|
240 | }
|
---|
241 | }
|
---|
242 | return NULL;
|
---|
243 | }
|
---|
244 | void ReleaseSingleStep(DWORD dwBeforeStepRun,HANDLE hThread,CONTEXT *pContext){
|
---|
245 | //以前にシングルステップ実行をした場合
|
---|
246 | extern DWORD ImageBase;
|
---|
247 | extern int MemPos_CodeSection;
|
---|
248 | extern int FileSize_CodeSection;
|
---|
249 | int i2;
|
---|
250 | ULONG_PTR lpAccBytes;
|
---|
251 |
|
---|
252 | ///////////////////////////
|
---|
253 | // シングルステップOFF
|
---|
254 | ///////////////////////////
|
---|
255 |
|
---|
256 | //ユーザー指定のブレークポイントをセット
|
---|
257 | WriteProcessMemory(hDebugProcess,
|
---|
258 | (void *)(ULONG_PTR)(ImageBase+MemPos_CodeSection),
|
---|
259 | pobj_DBDebugSection->pobj_now->BreakStepCodeBuffer,
|
---|
260 | FileSize_CodeSection,&lpAccBytes);
|
---|
261 |
|
---|
262 | //次に実行すべき命令はユーザー指定によるブレークポイントをはずしておく
|
---|
263 | if(ImageBase+MemPos_CodeSection<=EIP_RIP(*pContext)&&EIP_RIP(*pContext)<ImageBase+MemPos_CodeSection+FileSize_CodeSection){
|
---|
264 | if(OpBuffer[EIP_RIP(*pContext)-ImageBase-MemPos_CodeSection-1]!=
|
---|
265 | pobj_DBDebugSection->pobj_now->BreakStepCodeBuffer[EIP_RIP(*pContext)-ImageBase-MemPos_CodeSection-1]){
|
---|
266 | //直前のブレークポイントがユーザー指定によるものだったとき
|
---|
267 |
|
---|
268 | //eip/ripを1だけ減少
|
---|
269 | EIP_RIP(*pContext)--;
|
---|
270 | SetThreadContext(hThread,pContext);
|
---|
271 |
|
---|
272 | //ブレークポイントを解除
|
---|
273 | WriteProcessMemory(hDebugProcess,(void *)EIP_RIP(*pContext),&OpBuffer[EIP_RIP(*pContext)-ImageBase-MemPos_CodeSection],1,&lpAccBytes);
|
---|
274 |
|
---|
275 |
|
---|
276 | extern int StepCursorObpSchedule;
|
---|
277 | if(StepCursorObpSchedule==(int)(EIP_RIP(*pContext)-ImageBase-MemPos_CodeSection)){
|
---|
278 | //カーソル行までのステップ実行の場合
|
---|
279 | pobj_DBDebugSection->pobj_now->BreakStepCodeBuffer[StepCursorObpSchedule]=StepCursor_BackupChar;
|
---|
280 | StepCursorObpSchedule=-1;
|
---|
281 | }
|
---|
282 |
|
---|
283 | return;
|
---|
284 | }
|
---|
285 | }
|
---|
286 |
|
---|
287 |
|
---|
288 | if(dwBeforeStepRun){
|
---|
289 | //直前にシングルステップを行った場合
|
---|
290 | if(ImageBase+MemPos_CodeSection <= EIP_RIP(*pContext) &&
|
---|
291 | EIP_RIP(*pContext) < ImageBase+MemPos_CodeSection+FileSize_CodeSection){
|
---|
292 | //オリジナルコード内のみ、シングルステップ用の"int 3"を考慮
|
---|
293 | EIP_RIP(*pContext)--;
|
---|
294 | SetThreadContext(hThread,pContext);
|
---|
295 | }
|
---|
296 |
|
---|
297 | //他のスレッドのサスペンドを解除
|
---|
298 | for(i2=0;i2<MAX_DEBUG_THREAD;i2++){
|
---|
299 | if(array_hDebugThread[i2] && hThread!=array_hDebugThread[i2])
|
---|
300 | ResumeThread(array_hDebugThread[i2]);
|
---|
301 | }
|
---|
302 | }
|
---|
303 | }
|
---|
304 |
|
---|
305 | void Set_DebugSys_dwThreadID(){
|
---|
306 | int i;
|
---|
307 | for(i=0;i<pobj_DBDebugSection->num;i++){
|
---|
308 | ULONG_PTR lpAccBytes;
|
---|
309 | WriteProcessMemory(hDebugProcess,
|
---|
310 | (void *)(ULONG_PTR)(pobj_DBDebugSection->ppobj_ds[i]->dwImageBase + pobj_DBDebugSection->ppobj_ds[i]->dwRVA_RWSection),
|
---|
311 | _DebugSys_dwThreadID,sizeof(DWORD)*MAX_DEBUG_THREAD,&lpAccBytes);
|
---|
312 | }
|
---|
313 | }
|
---|
314 | void AddThread(DWORD dwThreadId, HANDLE hThread){
|
---|
315 | int i;
|
---|
316 |
|
---|
317 |
|
---|
318 | for(i=0;i<MAX_DEBUG_THREAD;i++){
|
---|
319 | if(_DebugSys_dwThreadID[i]==0){
|
---|
320 | _DebugSys_dwThreadID[i]=dwThreadId;
|
---|
321 | array_hDebugThread[i]=hThread;
|
---|
322 | break;
|
---|
323 | }
|
---|
324 | }
|
---|
325 |
|
---|
326 | Set_DebugSys_dwThreadID();
|
---|
327 | }
|
---|
328 | void DeleteThread(DWORD dwThreadId){
|
---|
329 | int i;
|
---|
330 |
|
---|
331 | for(i=0;i<MAX_DEBUG_THREAD;i++){
|
---|
332 | if(_DebugSys_dwThreadID[i]==dwThreadId){
|
---|
333 | _DebugSys_dwThreadID[i]=0;
|
---|
334 | array_hDebugThread[i]=0;
|
---|
335 | break;
|
---|
336 | }
|
---|
337 | }
|
---|
338 |
|
---|
339 | Set_DebugSys_dwThreadID();
|
---|
340 | }
|
---|
341 |
|
---|
342 | void DebugProgram(void){
|
---|
343 | extern HWND hMainDlg;
|
---|
344 | extern char OutputFileName[MAX_PATH];
|
---|
345 | extern DWORD ImageBase;
|
---|
346 | extern int MemPos_CodeSection;
|
---|
347 | extern int MemPos_RWSection;
|
---|
348 | extern int FileSize_CodeSection;
|
---|
349 | int i2,i3,i4;
|
---|
350 | char temporary[1024];
|
---|
351 |
|
---|
352 | extern BOOL bDll;
|
---|
353 | char ExeFilePathForDll[MAX_PATH];
|
---|
354 | if(bDll){
|
---|
355 | //DLLをデバッグする場合
|
---|
356 | extern char szDebugExeForDll[1024];
|
---|
357 | if(szDebugExeForDll[0]){
|
---|
358 | //指定済み
|
---|
359 | lstrcpy(ExeFilePathForDll,szDebugExeForDll);
|
---|
360 | }
|
---|
361 | else{
|
---|
362 | //ユーザーに実行ファイルを選択させる
|
---|
363 | extern HWND hOwnerEditor;
|
---|
364 | extern LPSTR ExeFileFilter;
|
---|
365 | if(!GetFilePathDialog(hOwnerEditor,ExeFilePathForDll,ExeFileFilter,"デバッグ用の実行可能ファイルを指定してください。",1)) return;
|
---|
366 | }
|
---|
367 | }
|
---|
368 |
|
---|
369 |
|
---|
370 | //"中断"
|
---|
371 | SetDlgItemText(hMainDlg,IDOK,STRING_STOP);
|
---|
372 |
|
---|
373 | SendMessage(hOwnerEditor,WM_SETDEBUGGERBASE,0,0);
|
---|
374 |
|
---|
375 | //カレントディレクトリを設定
|
---|
376 | extern char BasicCurDir[MAX_PATH];
|
---|
377 | SetCurrentDirectory(BasicCurDir);
|
---|
378 |
|
---|
379 | SendDlgItemMessage(hMainDlg,IDC_SHOWERROR,BM_SETCHECK,BST_UNCHECKED,0);
|
---|
380 | SendDlgItemMessage(hMainDlg,IDC_SHOWDEBUG,BM_SETCHECK,BST_CHECKED,0);
|
---|
381 | SendMessage(hMainDlg,WM_COMMAND,IDC_SHOWDEBUG,0);
|
---|
382 |
|
---|
383 | //ブレークポイントを生成
|
---|
384 | pobj_DBBreakPoint=new CDBBreakPoint;
|
---|
385 |
|
---|
386 |
|
---|
387 | extern BOOL bAttach;
|
---|
388 | if(bAttach){
|
---|
389 | extern DWORD dwAttachProcessId;
|
---|
390 |
|
---|
391 | //プロセスIDを元にハンドルを取得
|
---|
392 | HANDLE hProcess;
|
---|
393 | hProcess=OpenProcess(PROCESS_ALL_ACCESS,0,dwAttachProcessId);
|
---|
394 | if(!hProcess) goto AttachError;
|
---|
395 |
|
---|
396 | //そのプロセスにおける実行モジュールのインスタンスハンドルを取得
|
---|
397 | HINSTANCE hModule;
|
---|
398 | DWORD cbReturned;
|
---|
399 | if(!EnumProcessModules( hProcess, &hModule, sizeof(HINSTANCE), &cbReturned )) goto AttachError;
|
---|
400 |
|
---|
401 | //実行ファイル名を取得
|
---|
402 | GetModuleFileNameEx(hProcess,hModule,OutputFileName,MAX_PATH);
|
---|
403 |
|
---|
404 | CloseHandle(hProcess);
|
---|
405 |
|
---|
406 | /*
|
---|
407 | //デバッグ用の拡張情報を取得
|
---|
408 | pobj_DebugSection->load(OutputFileName);*/
|
---|
409 |
|
---|
410 | if(!DebugActiveProcess(dwAttachProcessId)){
|
---|
411 | AttachError:
|
---|
412 | DebugMessage("アタッチに失敗しました。");
|
---|
413 | return;
|
---|
414 | }
|
---|
415 | }
|
---|
416 | else{
|
---|
417 | /*if(!pobj_DebugSection->load(OutputFileName)){
|
---|
418 | extern BOOL bDebugCompile;
|
---|
419 | bDebugCompile=1;
|
---|
420 | OutputExe();
|
---|
421 | pobj_DebugSection->load(OutputFileName);
|
---|
422 | }*/
|
---|
423 |
|
---|
424 | //スレッドを生成
|
---|
425 | extern char szDebugCmdLine[1024];
|
---|
426 | STARTUPINFO si;
|
---|
427 | PROCESS_INFORMATION pi;
|
---|
428 | memset(&si,0,sizeof(STARTUPINFO));
|
---|
429 | si.cb=sizeof(STARTUPINFO);
|
---|
430 | if(!bDll){
|
---|
431 | //EXEファイルをデバッグ
|
---|
432 | CreateProcess(OutputFileName,szDebugCmdLine,NULL,NULL,0,NORMAL_PRIORITY_CLASS|DEBUG_ONLY_THIS_PROCESS,NULL,NULL,&si,&pi);
|
---|
433 | }
|
---|
434 | else{
|
---|
435 | //DLLファイルをデバッグ
|
---|
436 | CreateProcess(ExeFilePathForDll,szDebugCmdLine,NULL,NULL,0,NORMAL_PRIORITY_CLASS|DEBUG_ONLY_THIS_PROCESS,NULL,NULL,&si,&pi);
|
---|
437 | }
|
---|
438 |
|
---|
439 | CloseHandle(pi.hProcess);
|
---|
440 | CloseHandle(pi.hThread);
|
---|
441 | }
|
---|
442 |
|
---|
443 |
|
---|
444 | //デバッグ情報データベースを生成
|
---|
445 | pobj_DBDebugSection=new CDBDebugSection();
|
---|
446 |
|
---|
447 | //デバッグスレッド情報(プロシージャの階層構造を管理)を生成
|
---|
448 | pobj_dti=new CDebugThreadInfo();
|
---|
449 |
|
---|
450 | UserProc *pUserProc;
|
---|
451 |
|
---|
452 | extern DWORD dwStepRun;
|
---|
453 | BOOL bFirstBreak=1;
|
---|
454 | CONTEXT Context;
|
---|
455 | HANDLE hMainThread;
|
---|
456 | ULONG_PTR lpAccBytes;
|
---|
457 |
|
---|
458 | DEBUG_EVENT de;
|
---|
459 | memset(&de,0,sizeof(DEBUG_EVENT));
|
---|
460 |
|
---|
461 | while(WaitForDebugEvent(&de,INFINITE)){
|
---|
462 | if(de.dwDebugEventCode==LOAD_DLL_DEBUG_EVENT){
|
---|
463 | WCHAR wcBuf[MAX_PATH];
|
---|
464 | LONG_PTR lpData;
|
---|
465 |
|
---|
466 | wcBuf[0]=0;
|
---|
467 | temporary[0]=0;
|
---|
468 |
|
---|
469 | if(de.u.LoadDll.lpImageName){
|
---|
470 | if(!ReadProcessMemory(hDebugProcess,de.u.LoadDll.lpImageName,&lpData,sizeof(LONG_PTR),&lpAccBytes)){
|
---|
471 | sprintf(temporary,"ロードされたDLLの名前取得(アドレス:&H%08x)に失敗しました。\r\n",(ULONG_PTR)de.u.LoadDll.lpImageName);
|
---|
472 | DebugMessage(temporary);
|
---|
473 | goto NextContinue;
|
---|
474 | }
|
---|
475 | if(!lpData) goto Attach_DllLoad;
|
---|
476 |
|
---|
477 | if(!ReadProcessMemory(hDebugProcess,(void *)lpData,wcBuf,sizeof(WCHAR)*MAX_PATH,&lpAccBytes)){
|
---|
478 | sprintf(temporary,"ロードされたDLLの名前取得(アドレス:&H%08x)に失敗しました。\r\n",lpData);
|
---|
479 | DebugMessage(temporary);
|
---|
480 | goto NextContinue;
|
---|
481 | }
|
---|
482 |
|
---|
483 | if(de.dwThreadId,de.u.LoadDll.fUnicode)
|
---|
484 | WideCharToMultiByte(CP_ACP,0,wcBuf,-1,temporary,255,NULL,NULL);
|
---|
485 | else lstrcpy(temporary,(char *)wcBuf);
|
---|
486 | }
|
---|
487 | else{
|
---|
488 | Attach_DllLoad:
|
---|
489 | //アタッチした場合
|
---|
490 | GetModuleFileNameEx(hDebugProcess,(HINSTANCE)de.u.LoadDll.lpBaseOfDll,temporary,MAX_PATH);
|
---|
491 | }
|
---|
492 |
|
---|
493 | char temp2[1024];
|
---|
494 | sprintf(temp2,"\"%s\" をロードしました。\r\n",temporary);
|
---|
495 | DebugMessage(temp2);
|
---|
496 |
|
---|
497 |
|
---|
498 | //可能であればデバッグ情報を読みとる
|
---|
499 | if(pobj_DBDebugSection->add((HMODULE)de.u.LoadDll.lpBaseOfDll)){
|
---|
500 | pobj_DBDebugSection->choice(0);
|
---|
501 | Set_DebugSys_dwThreadID();
|
---|
502 | }
|
---|
503 |
|
---|
504 |
|
---|
505 | /*if(lstrcmpi(temporary, OutputFileName)==0){
|
---|
506 | ImageBase=(DWORD)de.u.LoadDll.lpBaseOfDll;
|
---|
507 |
|
---|
508 | AddThread(de.dwThreadId,hMainThread);
|
---|
509 | }*/
|
---|
510 | }
|
---|
511 | else if(de.dwDebugEventCode==UNLOAD_DLL_DEBUG_EVENT){
|
---|
512 | //DLLのアンロード
|
---|
513 |
|
---|
514 | pobj_DBDebugSection->del((HMODULE)de.u.UnloadDll.lpBaseOfDll);
|
---|
515 | }
|
---|
516 | else if(de.dwDebugEventCode==CREATE_PROCESS_DEBUG_EVENT){
|
---|
517 | hDebugProcess=de.u.CreateProcessInfo.hProcess;
|
---|
518 | hMainThread=de.u.CreateProcessInfo.hThread;
|
---|
519 |
|
---|
520 | if(pobj_DBDebugSection->add((HMODULE)de.u.CreateProcessInfo.lpBaseOfImage)){
|
---|
521 | pobj_DBDebugSection->choice(0);
|
---|
522 | }
|
---|
523 |
|
---|
524 | AddThread(de.dwThreadId,de.u.CreateProcessInfo.hThread);
|
---|
525 | }
|
---|
526 | else if(de.dwDebugEventCode==CREATE_THREAD_DEBUG_EVENT){
|
---|
527 | AddThread(de.dwThreadId,de.u.CreateThread.hThread);
|
---|
528 | }
|
---|
529 | else if(de.dwDebugEventCode==EXIT_PROCESS_DEBUG_EVENT){
|
---|
530 | //デバッグダイアログを終了
|
---|
531 | SendMessage(hMainDlg,WM_CLOSE_DEBUGGER,0,0);
|
---|
532 |
|
---|
533 | DeleteThread(de.dwThreadId);
|
---|
534 |
|
---|
535 | //"スレッド(&H%X)はコード &H%X で終了しました。\r\n"
|
---|
536 | sprintf(temporary,STRING_DEBUG_THREADFINISH,de.dwThreadId,de.u.ExitProcess.dwExitCode);
|
---|
537 | DebugMessage(temporary);
|
---|
538 |
|
---|
539 | //"プログラムはコード &H%X で終了しました。\r\n"
|
---|
540 | sprintf(temporary,STRING_DEBUG_PROCESSFINISH,de.u.ExitProcess.dwExitCode);
|
---|
541 | DebugMessage(temporary);
|
---|
542 | break;
|
---|
543 | }
|
---|
544 | else if(de.dwDebugEventCode==EXIT_THREAD_DEBUG_EVENT){
|
---|
545 | //"スレッド(&H%X)はコード &H%X で終了しました。\r\n"
|
---|
546 | sprintf(temporary,STRING_DEBUG_THREADFINISH,de.dwThreadId,de.u.ExitThread.dwExitCode);
|
---|
547 | DebugMessage(temporary);
|
---|
548 |
|
---|
549 | //以前にシングルステップ実行をした場合
|
---|
550 | //ステップ実行を解除
|
---|
551 | if(dwStepRun){
|
---|
552 | extern HWND hDebugWnd;
|
---|
553 | if(hDebugWnd) SendMessage(hDebugWnd,WM_VARLIST_CLOSE,0,0);
|
---|
554 |
|
---|
555 | for(i4=0;i4<MAX_DEBUG_THREAD;i4++){
|
---|
556 | if(de.dwThreadId==_DebugSys_dwThreadID[i4]) break;
|
---|
557 | }
|
---|
558 | Context.ContextFlags=CONTEXT_CONTROL;
|
---|
559 | GetThreadContext(array_hDebugThread[i4],&Context);
|
---|
560 |
|
---|
561 | ReleaseSingleStep(dwStepRun,array_hDebugThread[i4],&Context);
|
---|
562 | }
|
---|
563 |
|
---|
564 | DeleteThread(de.dwThreadId);
|
---|
565 | }
|
---|
566 | else if(de.dwDebugEventCode==OUTPUT_DEBUG_STRING_EVENT){
|
---|
567 | ReadProcessMemory(hDebugProcess,(void *)de.u.DebugString.lpDebugStringData,temporary,de.u.DebugString.nDebugStringLength,&lpAccBytes);
|
---|
568 | DebugMessage(temporary);
|
---|
569 | }
|
---|
570 | else if(de.dwDebugEventCode==EXCEPTION_DEBUG_EVENT){
|
---|
571 |
|
---|
572 | //////////////////////////////////////
|
---|
573 | // モジュールを再確認
|
---|
574 | // ※DLLへのデバッグ分岐を可能にする
|
---|
575 | //////////////////////////////////////
|
---|
576 |
|
---|
577 |
|
---|
578 | //初回例外は無視
|
---|
579 | if(bFirstBreak){
|
---|
580 | bFirstBreak=0;
|
---|
581 | goto NextContinue;
|
---|
582 | }
|
---|
583 |
|
---|
584 | for(i2=0;;i2++){
|
---|
585 | if(_DebugSys_dwThreadID[i2]==de.dwThreadId) break;
|
---|
586 | }
|
---|
587 |
|
---|
588 | //スレッド情報をリフレッシュ
|
---|
589 | if(!pobj_dti->Reflesh(i2)){
|
---|
590 | MessageBox(hOwnerEditor,"デバッグ情報が壊れています。ターゲットファイルを再コンパイルしてください。","ActiveBasic",MB_OK|MB_ICONEXCLAMATION);
|
---|
591 |
|
---|
592 | break;
|
---|
593 | }
|
---|
594 |
|
---|
595 | if(de.u.Exception.ExceptionRecord.ExceptionCode==EXCEPTION_ACCESS_VIOLATION){
|
---|
596 |
|
---|
597 | //"スレッド(&H%X)でアクセス違反がありました(EIP=&H%08X)。\r\n"
|
---|
598 | sprintf(temporary,STRING_DEBUG_THREAD_ACCESSVIOLATION,de.dwThreadId,(ULONG_PTR)de.u.Exception.ExceptionRecord.ExceptionAddress);
|
---|
599 | DebugMessage(temporary);
|
---|
600 |
|
---|
601 | MessageBeep(MB_ICONEXCLAMATION);
|
---|
602 | ShowVarList(&de,1);
|
---|
603 | break;
|
---|
604 | }
|
---|
605 | else if(de.u.Exception.ExceptionRecord.ExceptionCode==EXCEPTION_BREAKPOINT||
|
---|
606 | de.u.Exception.ExceptionRecord.ExceptionCode==EXCEPTION_SINGLE_STEP){
|
---|
607 | /////////////////////////
|
---|
608 | // ブレークポイント
|
---|
609 | /////////////////////////
|
---|
610 |
|
---|
611 | if(de.u.Exception.ExceptionRecord.ExceptionCode==EXCEPTION_SINGLE_STEP){
|
---|
612 | //CPU機構のシングルステップによるブレークポイント
|
---|
613 | //※シングルステップを解除
|
---|
614 | ReleaseCPUSingleStep();
|
---|
615 | }
|
---|
616 |
|
---|
617 | i3=dwStepRun;
|
---|
618 |
|
---|
619 | for(i4=0;i4<MAX_DEBUG_THREAD;i4++){
|
---|
620 | if(de.dwThreadId==_DebugSys_dwThreadID[i4]) break;
|
---|
621 | }
|
---|
622 | Context.ContextFlags=CONTEXT_CONTROL | CONTEXT_INTEGER;
|
---|
623 | GetThreadContext(array_hDebugThread[i4],&Context);
|
---|
624 |
|
---|
625 | if(i3==0||
|
---|
626 | i3&&(!(ImageBase+MemPos_CodeSection<=EIP_RIP(Context)&&EIP_RIP(Context)<ImageBase+MemPos_CodeSection+FileSize_CodeSection))
|
---|
627 | ){
|
---|
628 | //"スレッド(&H%X)のブレーク ポイント(EIP=&H%08X)。\r\n"
|
---|
629 | sprintf(temporary,
|
---|
630 | STRING_DEBUG_BREAKPOINT,
|
---|
631 | de.dwThreadId,
|
---|
632 | (ULONG_PTR)de.u.Exception.ExceptionRecord.ExceptionAddress,
|
---|
633 | #ifdef _AMD64_
|
---|
634 | (ULONG_PTR)Context.Rsp
|
---|
635 | #else
|
---|
636 | (ULONG_PTR)Context.Esp
|
---|
637 | #endif
|
---|
638 | );
|
---|
639 | DebugMessage(temporary);
|
---|
640 | }
|
---|
641 |
|
---|
642 | ShowVarList(&de,0);
|
---|
643 |
|
---|
644 | //ステップ実行を解除
|
---|
645 | ReleaseSingleStep(i3,array_hDebugThread[i4],&Context);
|
---|
646 |
|
---|
647 | if(dwStepRun){
|
---|
648 | //新たにシングルステップを行う場合
|
---|
649 |
|
---|
650 | if(i4!=NextStepThreadNum){
|
---|
651 | //次回のステップ対象が別スレッドの場合
|
---|
652 | Context.ContextFlags=CONTEXT_CONTROL;
|
---|
653 | GetThreadContext(array_hDebugThread[NextStepThreadNum],&Context);
|
---|
654 | }
|
---|
655 |
|
---|
656 | if(ImageBase+MemPos_CodeSection <= EIP_RIP(Context) &&
|
---|
657 | EIP_RIP(Context) < ImageBase+MemPos_CodeSection+FileSize_CodeSection){
|
---|
658 | //Debug命令語が続く場合はシングルステップは不要になるので、無効にする
|
---|
659 | //(オリジナルコード内のみ)
|
---|
660 | if(OpBuffer[EIP_RIP(Context)-ImageBase-MemPos_CodeSection]==(char)0xCC)
|
---|
661 | dwStepRun=0;
|
---|
662 | }
|
---|
663 | if(dwStepRun==1){
|
---|
664 | //ステップイン
|
---|
665 | StepIn:
|
---|
666 | //シングルステップON
|
---|
667 | WriteProcessMemory(hDebugProcess,(void *)(ULONG_PTR)(ImageBase+MemPos_CodeSection),
|
---|
668 | pobj_DBDebugSection->pobj_now->SingleStepCodeBuffer,
|
---|
669 | FileSize_CodeSection,&lpAccBytes);
|
---|
670 |
|
---|
671 | //次の命令語のブレーク命令は解除しておく(シングルステップ実行後のみ)
|
---|
672 | //(オリジナルコード内のみ)
|
---|
673 | if(i3&&ImageBase+MemPos_CodeSection<=EIP_RIP(Context)&&EIP_RIP(Context)<ImageBase+MemPos_CodeSection+FileSize_CodeSection)
|
---|
674 | WriteProcessMemory(hDebugProcess,(void *)EIP_RIP(Context),&OpBuffer[EIP_RIP(Context)-ImageBase-MemPos_CodeSection],1,&lpAccBytes);
|
---|
675 |
|
---|
676 | //他のスレッドを一時中断
|
---|
677 | for(i4=0;i4<MAX_DEBUG_THREAD;i4++){
|
---|
678 | if(_DebugSys_dwThreadID[i4] && NextStepThreadNum!=i4)
|
---|
679 | SuspendThread(array_hDebugThread[i4]);
|
---|
680 | }
|
---|
681 | }
|
---|
682 | else if(dwStepRun==2){
|
---|
683 | //ステップオーバー
|
---|
684 |
|
---|
685 | BOOL bRet;
|
---|
686 | bRet=ReadProcessMemory(hDebugProcess,
|
---|
687 | (void *)EIP_RIP(Context),
|
---|
688 | temporary,
|
---|
689 | 5,
|
---|
690 | &lpAccBytes);
|
---|
691 | if(!bRet) MessageBox(hMainDlg,"プロセスメモリーの読み込みに失敗","error",MB_OK);
|
---|
692 |
|
---|
693 | extern const UserProc *pSub_DebugSys_EndProc;
|
---|
694 | if((BYTE)temporary[0]==0xE8&&
|
---|
695 | *((long *)(temporary+1))+5==(long)rva_to_real(pSub_DebugSys_EndProc->_beginOpAddressOld)-(long)EIP_RIP(Context)){
|
---|
696 | //プロシージャの終端位置の場合はステップインを行う
|
---|
697 | goto StepIn;
|
---|
698 | }
|
---|
699 |
|
---|
700 | extern int GlobalOpBufferSize;
|
---|
701 | if(ImageBase+MemPos_CodeSection<=pobj_dti->lplpObp[pobj_dti->iProcLevel]&&
|
---|
702 | pobj_dti->lplpObp[pobj_dti->iProcLevel]<ImageBase+MemPos_CodeSection+GlobalOpBufferSize){
|
---|
703 | //シングルステップON
|
---|
704 | WriteProcessMemory(hDebugProcess,
|
---|
705 | (void *)(ULONG_PTR)(ImageBase+MemPos_CodeSection),
|
---|
706 | pobj_DBDebugSection->pobj_now->SingleStepCodeBuffer,
|
---|
707 | GlobalOpBufferSize,
|
---|
708 | &lpAccBytes);
|
---|
709 | }
|
---|
710 | else{
|
---|
711 | //プロシージャを識別
|
---|
712 | pUserProc=GetSubFromObp(pobj_dti->lplpObp[pobj_dti->iProcLevel]);
|
---|
713 |
|
---|
714 | //シングルステップON
|
---|
715 | WriteProcessMemory(hDebugProcess,
|
---|
716 | (void *)rva_to_real(pUserProc->_beginOpAddressOld),
|
---|
717 | pobj_DBDebugSection->pobj_now->SingleStepCodeBuffer+pUserProc->_beginOpAddressOld,
|
---|
718 | pUserProc->_endOpAddressOld-pUserProc->_beginOpAddressOld,
|
---|
719 | &lpAccBytes);
|
---|
720 | }
|
---|
721 |
|
---|
722 | //次の命令語のブレーク命令は解除しておく(シングルステップ実行後のみ)
|
---|
723 | //(オリジナルコード内のみ)
|
---|
724 | if(i3&&ImageBase+MemPos_CodeSection<=EIP_RIP(Context)&&EIP_RIP(Context)<ImageBase+MemPos_CodeSection+FileSize_CodeSection)
|
---|
725 | WriteProcessMemory(hDebugProcess,(void *)EIP_RIP(Context),&OpBuffer[EIP_RIP(Context)-ImageBase-MemPos_CodeSection],1,&lpAccBytes);
|
---|
726 |
|
---|
727 | //他のスレッドを一時中断
|
---|
728 | for(i4=0;i4<MAX_DEBUG_THREAD;i4++){
|
---|
729 | if(_DebugSys_dwThreadID[i4] && NextStepThreadNum!=i4)
|
---|
730 | SuspendThread(array_hDebugThread[i4]);
|
---|
731 | }
|
---|
732 | }
|
---|
733 | }
|
---|
734 | }
|
---|
735 | else if(de.u.Exception.ExceptionRecord.ExceptionCode==STATUS_INTEGER_DIVIDE_BY_ZERO){
|
---|
736 | //"0による除算が行われました。スレッド(&H%X) ブレーク ポイント(EIP=&H%08X)。\r\n"
|
---|
737 | sprintf(temporary,STRING_DEBUG_DIVIDE_BY_ZERO,de.dwThreadId,(ULONG_PTR)de.u.Exception.ExceptionRecord.ExceptionAddress);
|
---|
738 | DebugMessage(temporary);
|
---|
739 |
|
---|
740 | ShowVarList(&de,1);
|
---|
741 | break;
|
---|
742 | }
|
---|
743 | else if(de.u.Exception.ExceptionRecord.ExceptionCode==STATUS_NO_MEMORY){
|
---|
744 | //"メモリ不足、またはヒープが壊れていることが原因で、メモリの割り当てに失敗しました。スレッド(&H%X) ブレーク ポイント(EIP=&H%08X)。\r\n"
|
---|
745 | sprintf(temporary,STRING_DEBUG_DIVIDE_NO_MEMORY,de.dwThreadId,(ULONG_PTR)de.u.Exception.ExceptionRecord.ExceptionAddress);
|
---|
746 | DebugMessage(temporary);
|
---|
747 |
|
---|
748 | ShowVarList(&de,1);
|
---|
749 | break;
|
---|
750 | }
|
---|
751 | else{
|
---|
752 | //"例外処理\ncode:%X"
|
---|
753 | sprintf(temporary,STRING_DEBUG_ERROR,de.u.Exception.ExceptionRecord.ExceptionCode);
|
---|
754 | DebugMessage(temporary);
|
---|
755 | }
|
---|
756 | }
|
---|
757 | NextContinue:
|
---|
758 | ContinueDebugEvent(de.dwProcessId,de.dwThreadId,DBG_CONTINUE);
|
---|
759 | }
|
---|
760 |
|
---|
761 | extern HWND hDebugWnd;
|
---|
762 | if(hDebugWnd) SendMessage(hDebugWnd,WM_COMMAND,IDCANCEL,0);
|
---|
763 |
|
---|
764 | //デバッグに利用した情報を解放
|
---|
765 | delete pobj_DBDebugSection;
|
---|
766 |
|
---|
767 | //デバッグスレッド情報を解放
|
---|
768 | delete pobj_dti;
|
---|
769 |
|
---|
770 | //ブレークポイントを解放
|
---|
771 | delete pobj_DBBreakPoint;
|
---|
772 |
|
---|
773 | //"閉じる"
|
---|
774 | SetDlgItemText(hMainDlg,IDOK,STRING_CLOSE);
|
---|
775 |
|
---|
776 | SendMessage(hOwnerEditor,WM_DESTROYDEBUGGERBASE,0,0);
|
---|
777 | }
|
---|