source: dev/trunk/ab5.0/abdev/BasicCompiler_Common/Debug.cpp@ 636

Last change on this file since 636 was 636, checked in by dai_9181, 16 years ago

libファイルを跨ったテンプレート展開に対応。

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