source: dev/trunk/abdev/BasicCompiler_Common/Debug.cpp@ 266

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

BasicSourceのシリアライズがうまくいっていない

File size: 22.9 KB
Line 
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
11extern char *OpBuffer;
12
13extern DWORD dwStepRun;
14
15HANDLE hDebugProcess;
16
17DWORD _DebugSys_dwThreadID[MAX_DEBUG_THREAD];
18HANDLE array_hDebugThread[MAX_DEBUG_THREAD];
19
20int StepCursorObpSchedule=-1;
21int StepCursor_BackupChar;
22
23int NextStepThreadNum;
24
25
26void Debugger_StepIn(void){
27 dwStepRun=1;
28}
29void Debugger_StepOver(void){
30 dwStepRun=2;
31}
32
33void 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
83loop:
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}
112void 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}
123void 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
153void 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
184ULONG_PTR rva_to_real(DWORD p){
185 extern DWORD ImageBase;
186 extern int MemPos_CodeSection;
187 return p+ImageBase+MemPos_CodeSection;
188}
189
190void 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}
217void 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}
230UserProc *GetSubFromObp(ULONG_PTR pos){
231 compiler.GetObjectModule().meta.GetUserProcs().Iterator_Reset();
232 while( compiler.GetObjectModule().meta.GetUserProcs().Iterator_HasNext() )
233 {
234 UserProc *pUserProc = compiler.GetObjectModule().meta.GetUserProcs().Iterator_GetNext();
235
236 if(rva_to_real(pUserProc->GetBeginOpAddress()) <= pos &&
237 pos < rva_to_real(pUserProc->GetEndOpAddress()))
238 {
239 return pUserProc;
240 }
241 }
242 return NULL;
243}
244void 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
305void 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}
314void 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}
328void 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
342void 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 char ExeFilePathForDll[MAX_PATH];
353 if( compiler.IsDll() ){
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( !compiler.IsDll() ){
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)でアクセス違反がありました(EIP=&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 | CONTEXT_INTEGER;
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)のブレーク ポイント(EIP=&H%08X)。\r\n"
628 sprintf(temporary,
629 STRING_DEBUG_BREAKPOINT,
630 de.dwThreadId,
631 (ULONG_PTR)de.u.Exception.ExceptionRecord.ExceptionAddress,
632#ifdef _AMD64_
633 (ULONG_PTR)Context.Rsp
634#else
635 (ULONG_PTR)Context.Esp
636#endif
637 );
638 DebugMessage(temporary);
639 }
640
641 ShowVarList(&de,0);
642
643 //ステップ実行を解除
644 ReleaseSingleStep(i3,array_hDebugThread[i4],&Context);
645
646 if(dwStepRun){
647 //新たにシングルステップを行う場合
648
649 if(i4!=NextStepThreadNum){
650 //次回のステップ対象が別スレッドの場合
651 Context.ContextFlags=CONTEXT_CONTROL;
652 GetThreadContext(array_hDebugThread[NextStepThreadNum],&Context);
653 }
654
655 if(ImageBase+MemPos_CodeSection <= EIP_RIP(Context) &&
656 EIP_RIP(Context) < ImageBase+MemPos_CodeSection+FileSize_CodeSection){
657 //Debug命令語が続く場合はシングルステップは不要になるので、無効にする
658 //(オリジナルコード内のみ)
659 if(OpBuffer[EIP_RIP(Context)-ImageBase-MemPos_CodeSection]==(char)0xCC)
660 dwStepRun=0;
661 }
662 if(dwStepRun==1){
663 //ステップイン
664StepIn:
665 //シングルステップON
666 WriteProcessMemory(hDebugProcess,(void *)(ULONG_PTR)(ImageBase+MemPos_CodeSection),
667 pobj_DBDebugSection->pobj_now->SingleStepCodeBuffer,
668 FileSize_CodeSection,&lpAccBytes);
669
670 //次の命令語のブレーク命令は解除しておく(シングルステップ実行後のみ)
671 //(オリジナルコード内のみ)
672 if(i3&&ImageBase+MemPos_CodeSection<=EIP_RIP(Context)&&EIP_RIP(Context)<ImageBase+MemPos_CodeSection+FileSize_CodeSection)
673 WriteProcessMemory(hDebugProcess,(void *)EIP_RIP(Context),&OpBuffer[EIP_RIP(Context)-ImageBase-MemPos_CodeSection],1,&lpAccBytes);
674
675 //他のスレッドを一時中断
676 for(i4=0;i4<MAX_DEBUG_THREAD;i4++){
677 if(_DebugSys_dwThreadID[i4] && NextStepThreadNum!=i4)
678 SuspendThread(array_hDebugThread[i4]);
679 }
680 }
681 else if(dwStepRun==2){
682 //ステップオーバー
683
684 BOOL bRet;
685 bRet=ReadProcessMemory(hDebugProcess,
686 (void *)EIP_RIP(Context),
687 temporary,
688 5,
689 &lpAccBytes);
690 if(!bRet) MessageBox(hMainDlg,"プロセスメモリーの読み込みに失敗","error",MB_OK);
691
692 extern const UserProc *pSub_DebugSys_EndProc;
693 if((BYTE)temporary[0]==0xE8&&
694 *((long *)(temporary+1))+5==(long)rva_to_real(pSub_DebugSys_EndProc->GetBeginOpAddress())-(long)EIP_RIP(Context)){
695 //プロシージャの終端位置の場合はステップインを行う
696 goto StepIn;
697 }
698
699 extern int GlobalOpBufferSize;
700 if(ImageBase+MemPos_CodeSection<=pobj_dti->lplpObp[pobj_dti->iProcLevel]&&
701 pobj_dti->lplpObp[pobj_dti->iProcLevel]<ImageBase+MemPos_CodeSection+GlobalOpBufferSize){
702 //シングルステップON
703 WriteProcessMemory(hDebugProcess,
704 (void *)(ULONG_PTR)(ImageBase+MemPos_CodeSection),
705 pobj_DBDebugSection->pobj_now->SingleStepCodeBuffer,
706 GlobalOpBufferSize,
707 &lpAccBytes);
708 }
709 else{
710 //プロシージャを識別
711 pUserProc=GetSubFromObp(pobj_dti->lplpObp[pobj_dti->iProcLevel]);
712
713 //シングルステップON
714 WriteProcessMemory(hDebugProcess,
715 (void *)rva_to_real(pUserProc->GetBeginOpAddress()),
716 pobj_DBDebugSection->pobj_now->SingleStepCodeBuffer+pUserProc->GetBeginOpAddress(),
717 pUserProc->GetEndOpAddress()-pUserProc->GetBeginOpAddress(),
718 &lpAccBytes);
719 }
720
721 //次の命令語のブレーク命令は解除しておく(シングルステップ実行後のみ)
722 //(オリジナルコード内のみ)
723 if(i3&&ImageBase+MemPos_CodeSection<=EIP_RIP(Context)&&EIP_RIP(Context)<ImageBase+MemPos_CodeSection+FileSize_CodeSection)
724 WriteProcessMemory(hDebugProcess,(void *)EIP_RIP(Context),&OpBuffer[EIP_RIP(Context)-ImageBase-MemPos_CodeSection],1,&lpAccBytes);
725
726 //他のスレッドを一時中断
727 for(i4=0;i4<MAX_DEBUG_THREAD;i4++){
728 if(_DebugSys_dwThreadID[i4] && NextStepThreadNum!=i4)
729 SuspendThread(array_hDebugThread[i4]);
730 }
731 }
732 }
733 }
734 else if(de.u.Exception.ExceptionRecord.ExceptionCode==STATUS_INTEGER_DIVIDE_BY_ZERO){
735 //"0による除算が行われました。スレッド(&H%X) ブレーク ポイント(EIP=&H%08X)。\r\n"
736 sprintf(temporary,STRING_DEBUG_DIVIDE_BY_ZERO,de.dwThreadId,(ULONG_PTR)de.u.Exception.ExceptionRecord.ExceptionAddress);
737 DebugMessage(temporary);
738
739 ShowVarList(&de,1);
740 break;
741 }
742 else if(de.u.Exception.ExceptionRecord.ExceptionCode==STATUS_NO_MEMORY){
743 //"メモリ不足、またはヒープが壊れていることが原因で、メモリの割り当てに失敗しました。スレッド(&H%X) ブレーク ポイント(EIP=&H%08X)。\r\n"
744 sprintf(temporary,STRING_DEBUG_DIVIDE_NO_MEMORY,de.dwThreadId,(ULONG_PTR)de.u.Exception.ExceptionRecord.ExceptionAddress);
745 DebugMessage(temporary);
746
747 ShowVarList(&de,1);
748 break;
749 }
750 else{
751 //"例外処理\ncode:%X"
752 sprintf(temporary,STRING_DEBUG_ERROR,de.u.Exception.ExceptionRecord.ExceptionCode);
753 DebugMessage(temporary);
754 }
755 }
756NextContinue:
757 ContinueDebugEvent(de.dwProcessId,de.dwThreadId,DBG_CONTINUE);
758 }
759
760 extern HWND hDebugWnd;
761 if(hDebugWnd) SendMessage(hDebugWnd,WM_COMMAND,IDCANCEL,0);
762
763 //デバッグに利用した情報を解放
764 delete pobj_DBDebugSection;
765
766 //デバッグスレッド情報を解放
767 delete pobj_dti;
768
769 //ブレークポイントを解放
770 delete pobj_DBBreakPoint;
771
772 //"閉じる"
773 SetDlgItemText(hMainDlg,IDOK,STRING_CLOSE);
774
775 SendMessage(hOwnerEditor,WM_DESTROYDEBUGGERBASE,0,0);
776}
Note: See TracBrowser for help on using the repository browser.