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

Last change on this file since 741 was 741, checked in by dai, 16 years ago

デバッグセクション情報周りをリファクタリング

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
25DebugSectionCollection debugSectionCollection;
26
27void Debugger_StepIn(void){
28 dwStepRun=1;
29}
30void Debugger_StepOver(void){
31 dwStepRun=2;
32}
33
34void Debugger_StepCursor(void){
35 char temporary[MAX_PATH];
36 GetTempPath(MAX_PATH,temporary);
37 if(temporary[lstrlen(temporary)-1]!='\\') lstrcat(temporary,"\\");
38 lstrcat(temporary,"ab_breakpoint.tmp");
39
40 char *buffer;
41 buffer=ReadBuffer_NonErrMsg(temporary);
42
43 int i=0;
44 char szFilePath[MAX_PATH];
45 i=GetOneParameter(buffer,i,szFilePath);
46 RemoveStringQuotes(szFilePath);
47
48 int iLineNum;
49 i=GetOneParameter(buffer,i,temporary);
50 iLineNum=atoi(temporary);
51
52
53
54 /////////////////////////////////////////////////////////
55 // デバッグ中プロセスのネイティブバッファ領域を更新
56 /////////////////////////////////////////////////////////
57
58 int FileNum;
59 const IncludedFilesRelation *pIncludedFilesRelation = NULL;
60 const BasicSource *pNowSource = NULL;
61
62 extern BasicSources sourcesLinkRelationalObjectModule;
63 BOOST_FOREACH( const BasicSource &source, sourcesLinkRelationalObjectModule )
64 {
65 pIncludedFilesRelation = &source.GetIncludedFilesRelation();
66
67 for(FileNum=0;FileNum<pIncludedFilesRelation->GetFileCounts();FileNum++)
68 {
69 if(lstrcmpi(pIncludedFilesRelation->GetFilePathFromFileNumber(FileNum).c_str(),szFilePath)==0)
70 {
71 pNowSource = &source;
72 break;
73 }
74 }
75 if( FileNum == pIncludedFilesRelation->GetFileCounts() )
76 {
77 pIncludedFilesRelation = NULL;
78 }
79 else
80 {
81 break;
82 }
83 }
84
85 if( !pIncludedFilesRelation )
86 {
87 return;
88 }
89
90 for(i=0;;i++){
91 if( pIncludedFilesRelation->GetFileNumber( i ) == FileNum
92 || pIncludedFilesRelation->GetFileNumber( i ) == -1 )
93 {
94 break;
95 }
96 }
97 if( pIncludedFilesRelation->GetFileNumber( i ) == -1 )
98 {
99 return;
100 }
101
102 int FileBaseLine;
103 FileBaseLine=i;
104
105 int i2;
106 for(i2=0;;i++,i2++)
107 {
108 if( FileNum < pIncludedFilesRelation->GetFileNumber( i ) )
109 {
110 while( FileNum != pIncludedFilesRelation->GetFileNumber( i ) )
111 {
112 i++;
113 }
114 }
115
116 if(i2==iLineNum){
117 extern SourceLines oldSourceLines;
118
119loop:
120 int tempCp = GetSourceCodeIndexFromLine( pNowSource->GetBuffer(), FileBaseLine+i2 );
121
122 int i3;
123 for(i3=0;i3<(int)oldSourceLines.size()-1;i3++){
124 if(oldSourceLines[i3].GetSourceCodePosition().GetPos()==tempCp) break;
125 }
126 if(i3==oldSourceLines.size()-1){
127 i2--;
128 goto loop;
129 }
130
131 StepCursorObpSchedule=oldSourceLines[i3].GetNativeCodePos();
132 StepCursor_BackupChar=debugSectionCollection.GetCurrent().BreakStepCodeBuffer[StepCursorObpSchedule];
133
134 debugSectionCollection.GetCurrent().BreakStepCodeBuffer[StepCursorObpSchedule]=(char)0xCC;
135
136 break;
137 }
138 }
139
140 extern HWND hDebugWnd;
141 SendMessage(hDebugWnd,WM_DEBUG_CONTINUE,0,0);
142
143
144
145 HeapDefaultFree(buffer);
146}
147void Debugger_Stop(void){
148 //プロセスを終了
149 TerminateProcess(hDebugProcess,0);
150 hDebugProcess=0;
151
152 //デバッグダイアログを終了
153 extern HWND hDebugWnd;
154 if(hDebugWnd){
155 DestroyWindow(hDebugWnd);
156 }
157}
158void Debugger_Pause(void){
159 ///////////////////////////
160 // デバッグを一時中断
161 ///////////////////////////
162
163 //ターゲットプロセス内のスレッドを一時中断
164 int i;
165 for(i=0;i<MAX_DEBUG_THREAD;i++){
166 if(_DebugSys_dwThreadID[i])
167 SuspendThread(array_hDebugThread[i]);
168 }
169
170 //デバッグレジスタにフラグをセット
171 CONTEXT Context;
172 for(i=0;i<MAX_DEBUG_THREAD;i++){
173 if(_DebugSys_dwThreadID[i]){
174 Context.ContextFlags=CONTEXT_CONTROL|CONTEXT_DEBUG_REGISTERS;
175 GetThreadContext(array_hDebugThread[i],&Context);
176 Context.EFlags|=0x100;
177 SetThreadContext(array_hDebugThread[i],&Context);
178 }
179 }
180
181 //スレッドを再開
182 for(i=0;i<MAX_DEBUG_THREAD;i++){
183 if(_DebugSys_dwThreadID[i])
184 ResumeThread(array_hDebugThread[i]);
185 }
186}
187
188void ReleaseCPUSingleStep(void){
189 ////////////////////////////////////////
190 // CPU機構のシングルステップを解除
191 ////////////////////////////////////////
192
193 //ターゲットプロセス内のスレッドを一時中断
194 int i;
195 for(i=0;i<MAX_DEBUG_THREAD;i++){
196 if(_DebugSys_dwThreadID[i])
197 SuspendThread(array_hDebugThread[i]);
198 }
199
200 //デバッグレジスタにフラグをセット
201 CONTEXT Context;
202 for(i=0;i<MAX_DEBUG_THREAD;i++){
203 if(_DebugSys_dwThreadID[i]){
204 Context.ContextFlags=CONTEXT_CONTROL|CONTEXT_DEBUG_REGISTERS;
205 GetThreadContext(array_hDebugThread[i],&Context);
206 Context.EFlags&=~0x00000100;
207 SetThreadContext(array_hDebugThread[i],&Context);
208 }
209 }
210
211 //スレッドを再開
212 for(i=0;i<MAX_DEBUG_THREAD;i++){
213 if(_DebugSys_dwThreadID[i])
214 ResumeThread(array_hDebugThread[i]);
215 }
216}
217
218
219ULONG_PTR rva_to_real(DWORD p){
220 extern DWORD ImageBase;
221 extern int MemPos_CodeSection;
222 return p+ImageBase+MemPos_CodeSection;
223}
224
225void ShowVarList(DEBUG_EVENT *pde,BOOL bExit){
226 extern DWORD ImageBase;
227 extern int MemPos_RWSection;
228
229 //デバッグダイアログを表示
230 extern HINSTANCE hInst;
231 extern HWND hMainDlg;
232 extern HWND hDebugWnd;
233 dwStepRun=0;
234 if(!hDebugWnd) SendMessage(hMainDlg,WM_SHOWVARLIST,0,pde->dwThreadId);
235 else InitVarList(pde->dwThreadId);
236
237 if(bExit){
238 //"中断"
239 SetDlgItemText(hMainDlg,IDOK,STRING_CLOSE);
240
241 extern HWND hDebuggerToolbar;
242 SendMessage(hDebuggerToolbar,TB_SETSTATE,IDC_DEBUG_START,TBSTATE_INDETERMINATE);
243 SendMessage(hDebuggerToolbar,TB_SETSTATE,IDC_DEBUG_STEPOVER,TBSTATE_INDETERMINATE);
244 SendMessage(hDebuggerToolbar,TB_SETSTATE,IDC_DEBUG_STEPIN,TBSTATE_INDETERMINATE);
245 }
246 else{
247 //"継続"
248 SetDlgItemText(hDebugWnd,IDCANCEL,STRING_CONTINUE);
249 }
250 while(hDebugWnd&&dwStepRun==0) Sleep(1);
251}
252void DebugMessage(char *buffer){
253 extern HWND hMainDlg;
254 int pos;
255
256 if(!IsWindowEnabled(GetDlgItem(hMainDlg,IDC_DEBUGLIST))){
257 SetDlgItemText(hMainDlg,IDC_DEBUGLIST,"");
258 EnableWindow(GetDlgItem(hMainDlg,IDC_DEBUGLIST),1);
259 }
260
261 pos=GetWindowTextLength(GetDlgItem(hMainDlg,IDC_DEBUGLIST));
262 SendDlgItemMessage(hMainDlg,IDC_DEBUGLIST,EM_SETSEL,pos,pos);
263 SendDlgItemMessage(hMainDlg,IDC_DEBUGLIST,EM_REPLACESEL,0,(LPARAM)buffer);
264}
265UserProc *GetSubFromObp(ULONG_PTR pos)
266{
267 compiler.GetObjectModule().meta.GetUserProcs().Iterator_Reset();
268 while( compiler.GetObjectModule().meta.GetUserProcs().Iterator_HasNext() )
269 {
270 UserProc *pUserProc = compiler.GetObjectModule().meta.GetUserProcs().Iterator_GetNext();
271
272 if(rva_to_real(pUserProc->GetBeginOpAddress()) <= pos &&
273 pos < rva_to_real(pUserProc->GetEndOpAddress()))
274 {
275 return pUserProc;
276 }
277 }
278 return NULL;
279}
280void ReleaseSingleStep(DWORD dwBeforeStepRun,HANDLE hThread,CONTEXT *pContext){
281 //以前にシングルステップ実行をした場合
282 extern DWORD ImageBase;
283 extern int MemPos_CodeSection;
284 extern int FileSize_CodeSection;
285 int i2;
286 ULONG_PTR lpAccBytes;
287
288 ///////////////////////////
289 // シングルステップOFF
290 ///////////////////////////
291
292 //ユーザー指定のブレークポイントをセット
293 WriteProcessMemory(hDebugProcess,
294 (void *)(ULONG_PTR)(ImageBase+MemPos_CodeSection),
295 debugSectionCollection.GetCurrent().BreakStepCodeBuffer,
296 FileSize_CodeSection,&lpAccBytes);
297
298 //次に実行すべき命令はユーザー指定によるブレークポイントをはずしておく
299 if(ImageBase+MemPos_CodeSection<=EIP_RIP(*pContext)&&EIP_RIP(*pContext)<ImageBase+MemPos_CodeSection+FileSize_CodeSection){
300 if(OpBuffer[EIP_RIP(*pContext)-ImageBase-MemPos_CodeSection-1]!=
301 debugSectionCollection.GetCurrent().BreakStepCodeBuffer[EIP_RIP(*pContext)-ImageBase-MemPos_CodeSection-1]){
302 //直前のブレークポイントがユーザー指定によるものだったとき
303
304 //eip/ripを1だけ減少
305 EIP_RIP(*pContext)--;
306 SetThreadContext(hThread,pContext);
307
308 //ブレークポイントを解除
309 WriteProcessMemory(hDebugProcess,(void *)EIP_RIP(*pContext),&OpBuffer[EIP_RIP(*pContext)-ImageBase-MemPos_CodeSection],1,&lpAccBytes);
310
311
312 extern int StepCursorObpSchedule;
313 if(StepCursorObpSchedule==(int)(EIP_RIP(*pContext)-ImageBase-MemPos_CodeSection)){
314 //カーソル行までのステップ実行の場合
315 debugSectionCollection.GetCurrent().BreakStepCodeBuffer[StepCursorObpSchedule]=StepCursor_BackupChar;
316 StepCursorObpSchedule=-1;
317 }
318
319 return;
320 }
321 }
322
323
324 if(dwBeforeStepRun){
325 //直前にシングルステップを行った場合
326 if(ImageBase+MemPos_CodeSection <= EIP_RIP(*pContext) &&
327 EIP_RIP(*pContext) < ImageBase+MemPos_CodeSection+FileSize_CodeSection){
328 //オリジナルコード内のみ、シングルステップ用の"int 3"を考慮
329 EIP_RIP(*pContext)--;
330 SetThreadContext(hThread,pContext);
331 }
332
333 //他のスレッドのサスペンドを解除
334 for(i2=0;i2<MAX_DEBUG_THREAD;i2++){
335 if(array_hDebugThread[i2] && hThread!=array_hDebugThread[i2])
336 ResumeThread(array_hDebugThread[i2]);
337 }
338 }
339}
340
341void Set_DebugSys_dwThreadID()
342{
343 BOOST_FOREACH( DebugSection *pDebugSection, debugSectionCollection.debugSections )
344 {
345 ULONG_PTR lpAccBytes;
346 WriteProcessMemory(hDebugProcess,
347 (void *)(ULONG_PTR)(pDebugSection->dwImageBase + pDebugSection->dwRVA_RWSection),
348 _DebugSys_dwThreadID,
349 sizeof(DWORD)*MAX_DEBUG_THREAD,
350 &lpAccBytes
351 );
352 }
353}
354void AddThread(DWORD dwThreadId, HANDLE hThread){
355 int i;
356
357
358 for(i=0;i<MAX_DEBUG_THREAD;i++){
359 if(_DebugSys_dwThreadID[i]==0){
360 _DebugSys_dwThreadID[i]=dwThreadId;
361 array_hDebugThread[i]=hThread;
362 break;
363 }
364 }
365
366 Set_DebugSys_dwThreadID();
367}
368void DeleteThread(DWORD dwThreadId){
369 int i;
370
371 for(i=0;i<MAX_DEBUG_THREAD;i++){
372 if(_DebugSys_dwThreadID[i]==dwThreadId){
373 _DebugSys_dwThreadID[i]=0;
374 array_hDebugThread[i]=0;
375 break;
376 }
377 }
378
379 Set_DebugSys_dwThreadID();
380}
381
382void DebugProgram(void){
383 extern HWND hMainDlg;
384 extern DWORD ImageBase;
385 extern int MemPos_CodeSection;
386 extern int MemPos_RWSection;
387 extern int FileSize_CodeSection;
388 int i2,i3,i4;
389 char temporary[1024];
390
391 char ExeFilePathForDll[MAX_PATH];
392 if( compiler.IsDll() ){
393 //DLLをデバッグする場合
394 extern char szDebugExeForDll[1024];
395 if(szDebugExeForDll[0]){
396 //指定済み
397 lstrcpy(ExeFilePathForDll,szDebugExeForDll);
398 }
399 else{
400 //ユーザーに実行ファイルを選択させる
401 extern HWND hOwnerEditor;
402 extern LPSTR ExeFileFilter;
403 if(!GetFilePathDialog(hOwnerEditor,ExeFilePathForDll,ExeFileFilter,"デバッグ用の実行可能ファイルを指定してください。",1)) return;
404 }
405 }
406
407
408 //"中断"
409 SetDlgItemText(hMainDlg,IDOK,STRING_STOP);
410
411 SendMessage(hOwnerEditor,WM_SETDEBUGGERBASE,0,0);
412
413 //カレントディレクトリを設定
414 extern char BasicCurDir[MAX_PATH];
415 SetCurrentDirectory(BasicCurDir);
416
417 SendDlgItemMessage(hMainDlg,IDC_SHOWERROR,BM_SETCHECK,BST_UNCHECKED,0);
418 SendDlgItemMessage(hMainDlg,IDC_SHOWDEBUG,BM_SETCHECK,BST_CHECKED,0);
419 SendMessage(hMainDlg,WM_COMMAND,IDC_SHOWDEBUG,0);
420
421 //ブレークポイントを生成
422 pobj_DBBreakPoint=new BreakPointManager;
423
424
425 // デバッグをスタート
426 debugger.DebugStart();
427
428
429 if( program.IsAttach() )
430 {
431 //プロセスIDを元にハンドルを取得
432 HANDLE hProcess;
433 hProcess=OpenProcess(PROCESS_ALL_ACCESS,0, program.GetAttachProcessId() );
434 if(!hProcess) goto AttachError;
435
436 //そのプロセスにおける実行モジュールのインスタンスハンドルを取得
437 HINSTANCE hModule;
438 DWORD cbReturned;
439 if(!EnumProcessModules( hProcess, &hModule, sizeof(HINSTANCE), &cbReturned )) goto AttachError;
440
441 //実行ファイル名を取得
442 char tempOutputFileName[MAX_PATH];
443 GetModuleFileNameEx(hProcess,hModule,tempOutputFileName,MAX_PATH);
444 program.SetOutputFilePath( tempOutputFileName );
445
446 CloseHandle(hProcess);
447
448/*
449 //デバッグ用の拡張情報を取得
450 pobj_DebugSection->load(program.GetOutputFilePath().c_str());*/
451
452 if(!DebugActiveProcess( program.GetAttachProcessId() )){
453AttachError:
454 DebugMessage("アタッチに失敗しました。");
455 return;
456 }
457 }
458 else{
459 /*if(!pobj_DebugSection->load(program.GetOutputFilePath().c_str())){
460 extern BOOL bDebugCompile;
461 bDebugCompile=1;
462 Build();
463 pobj_DebugSection->load(program.GetOutputFilePath().c_str());
464 }*/
465
466 //スレッドを生成
467 extern char szDebugCmdLine[1024];
468 STARTUPINFO si;
469 PROCESS_INFORMATION pi;
470 memset(&si,0,sizeof(STARTUPINFO));
471 si.cb=sizeof(STARTUPINFO);
472 if( !compiler.IsDll() ){
473 //EXEファイルをデバッグ
474 CreateProcess(program.GetOutputFilePath().c_str(),szDebugCmdLine,NULL,NULL,0,NORMAL_PRIORITY_CLASS|DEBUG_ONLY_THIS_PROCESS|CREATE_NEW_CONSOLE,NULL,NULL,&si,&pi);
475 }
476 else{
477 //DLLファイルをデバッグ
478 CreateProcess(ExeFilePathForDll,szDebugCmdLine,NULL,NULL,0,NORMAL_PRIORITY_CLASS|DEBUG_ONLY_THIS_PROCESS|CREATE_NEW_CONSOLE,NULL,NULL,&si,&pi);
479 }
480
481 CloseHandle(pi.hProcess);
482 CloseHandle(pi.hThread);
483 }
484
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(debugSectionCollection.add((HMODULE)de.u.LoadDll.lpBaseOfDll)){
539 debugSectionCollection.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 debugSectionCollection.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(debugSectionCollection.add((HMODULE)de.u.CreateProcessInfo.lpBaseOfImage)){
560 debugSectionCollection.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 debugSectionCollection.GetCurrent().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 debugSectionCollection.GetCurrent().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 debugSectionCollection.GetCurrent().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_dti;
814
815 //ブレークポイントを解放
816 delete pobj_DBBreakPoint;
817
818 //"閉じる"
819 SetDlgItemText(hMainDlg,IDOK,STRING_CLOSE);
820
821 debugger.FinishDebug();
822
823 SendMessage(hOwnerEditor,WM_DESTROYDEBUGGERBASE,0,0);
824}
Note: See TracBrowser for help on using the repository browser.