source: dev/BasicCompiler_Common/Debug.cpp@ 75

Last change on this file since 75 was 75, checked in by dai_9181, 17 years ago

TYPEINFO→Typeへのリファクタリングを実施。64bitはほぼ完了。32bitが全般的に未完成。

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