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

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

ppobj_dsを、debugSectionsに書き直した。

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
61 extern BasicSources sourcesLinkRelationalObjectModule;
62 BOOST_FOREACH( const BasicSource &source, sourcesLinkRelationalObjectModule )
63 {
64 pIncludedFilesRelation = &source.GetIncludedFilesRelation();
65
66 for(FileNum=0;FileNum<pIncludedFilesRelation->GetFileCounts();FileNum++)
67 {
68 if(lstrcmpi(pIncludedFilesRelation->GetFilePathFromFileNumber(FileNum).c_str(),szFilePath)==0)
69 {
70 pNowSource = &source;
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].GetSourceCodePosition().GetPos()==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{
266 compiler.GetObjectModule().meta.GetUserProcs().Iterator_Reset();
267 while( compiler.GetObjectModule().meta.GetUserProcs().Iterator_HasNext() )
268 {
269 UserProc *pUserProc = compiler.GetObjectModule().meta.GetUserProcs().Iterator_GetNext();
270
271 if(rva_to_real(pUserProc->GetBeginOpAddress()) <= pos &&
272 pos < rva_to_real(pUserProc->GetEndOpAddress()))
273 {
274 return pUserProc;
275 }
276 }
277 return NULL;
278}
279void ReleaseSingleStep(DWORD dwBeforeStepRun,HANDLE hThread,CONTEXT *pContext){
280 //以前にシングルステップ実行をした場合
281 extern DWORD ImageBase;
282 extern int MemPos_CodeSection;
283 extern int FileSize_CodeSection;
284 int i2;
285 ULONG_PTR lpAccBytes;
286
287 ///////////////////////////
288 // シングルステップOFF
289 ///////////////////////////
290
291 //ユーザー指定のブレークポイントをセット
292 WriteProcessMemory(hDebugProcess,
293 (void *)(ULONG_PTR)(ImageBase+MemPos_CodeSection),
294 pobj_DBDebugSection->pobj_now->BreakStepCodeBuffer,
295 FileSize_CodeSection,&lpAccBytes);
296
297 //次に実行すべき命令はユーザー指定によるブレークポイントをはずしておく
298 if(ImageBase+MemPos_CodeSection<=EIP_RIP(*pContext)&&EIP_RIP(*pContext)<ImageBase+MemPos_CodeSection+FileSize_CodeSection){
299 if(OpBuffer[EIP_RIP(*pContext)-ImageBase-MemPos_CodeSection-1]!=
300 pobj_DBDebugSection->pobj_now->BreakStepCodeBuffer[EIP_RIP(*pContext)-ImageBase-MemPos_CodeSection-1]){
301 //直前のブレークポイントがユーザー指定によるものだったとき
302
303 //eip/ripを1だけ減少
304 EIP_RIP(*pContext)--;
305 SetThreadContext(hThread,pContext);
306
307 //ブレークポイントを解除
308 WriteProcessMemory(hDebugProcess,(void *)EIP_RIP(*pContext),&OpBuffer[EIP_RIP(*pContext)-ImageBase-MemPos_CodeSection],1,&lpAccBytes);
309
310
311 extern int StepCursorObpSchedule;
312 if(StepCursorObpSchedule==(int)(EIP_RIP(*pContext)-ImageBase-MemPos_CodeSection)){
313 //カーソル行までのステップ実行の場合
314 pobj_DBDebugSection->pobj_now->BreakStepCodeBuffer[StepCursorObpSchedule]=StepCursor_BackupChar;
315 StepCursorObpSchedule=-1;
316 }
317
318 return;
319 }
320 }
321
322
323 if(dwBeforeStepRun){
324 //直前にシングルステップを行った場合
325 if(ImageBase+MemPos_CodeSection <= EIP_RIP(*pContext) &&
326 EIP_RIP(*pContext) < ImageBase+MemPos_CodeSection+FileSize_CodeSection){
327 //オリジナルコード内のみ、シングルステップ用の"int 3"を考慮
328 EIP_RIP(*pContext)--;
329 SetThreadContext(hThread,pContext);
330 }
331
332 //他のスレッドのサスペンドを解除
333 for(i2=0;i2<MAX_DEBUG_THREAD;i2++){
334 if(array_hDebugThread[i2] && hThread!=array_hDebugThread[i2])
335 ResumeThread(array_hDebugThread[i2]);
336 }
337 }
338}
339
340void Set_DebugSys_dwThreadID()
341{
342 BOOST_FOREACH( CDebugSection *pDebugSection, pobj_DBDebugSection->debugSections )
343 {
344 ULONG_PTR lpAccBytes;
345 WriteProcessMemory(hDebugProcess,
346 (void *)(ULONG_PTR)(pDebugSection->dwImageBase + pDebugSection->dwRVA_RWSection),
347 _DebugSys_dwThreadID,
348 sizeof(DWORD)*MAX_DEBUG_THREAD,
349 &lpAccBytes
350 );
351 }
352}
353void AddThread(DWORD dwThreadId, HANDLE hThread){
354 int i;
355
356
357 for(i=0;i<MAX_DEBUG_THREAD;i++){
358 if(_DebugSys_dwThreadID[i]==0){
359 _DebugSys_dwThreadID[i]=dwThreadId;
360 array_hDebugThread[i]=hThread;
361 break;
362 }
363 }
364
365 Set_DebugSys_dwThreadID();
366}
367void DeleteThread(DWORD dwThreadId){
368 int i;
369
370 for(i=0;i<MAX_DEBUG_THREAD;i++){
371 if(_DebugSys_dwThreadID[i]==dwThreadId){
372 _DebugSys_dwThreadID[i]=0;
373 array_hDebugThread[i]=0;
374 break;
375 }
376 }
377
378 Set_DebugSys_dwThreadID();
379}
380
381void DebugProgram(void){
382 extern HWND hMainDlg;
383 extern DWORD ImageBase;
384 extern int MemPos_CodeSection;
385 extern int MemPos_RWSection;
386 extern int FileSize_CodeSection;
387 int i2,i3,i4;
388 char temporary[1024];
389
390 char ExeFilePathForDll[MAX_PATH];
391 if( compiler.IsDll() ){
392 //DLLをデバッグする場合
393 extern char szDebugExeForDll[1024];
394 if(szDebugExeForDll[0]){
395 //指定済み
396 lstrcpy(ExeFilePathForDll,szDebugExeForDll);
397 }
398 else{
399 //ユーザーに実行ファイルを選択させる
400 extern HWND hOwnerEditor;
401 extern LPSTR ExeFileFilter;
402 if(!GetFilePathDialog(hOwnerEditor,ExeFilePathForDll,ExeFileFilter,"デバッグ用の実行可能ファイルを指定してください。",1)) return;
403 }
404 }
405
406
407 //"中断"
408 SetDlgItemText(hMainDlg,IDOK,STRING_STOP);
409
410 SendMessage(hOwnerEditor,WM_SETDEBUGGERBASE,0,0);
411
412 //カレントディレクトリを設定
413 extern char BasicCurDir[MAX_PATH];
414 SetCurrentDirectory(BasicCurDir);
415
416 SendDlgItemMessage(hMainDlg,IDC_SHOWERROR,BM_SETCHECK,BST_UNCHECKED,0);
417 SendDlgItemMessage(hMainDlg,IDC_SHOWDEBUG,BM_SETCHECK,BST_CHECKED,0);
418 SendMessage(hMainDlg,WM_COMMAND,IDC_SHOWDEBUG,0);
419
420 //ブレークポイントを生成
421 pobj_DBBreakPoint=new BreakPointManager;
422
423
424 // デバッグをスタート
425 debugger.DebugStart();
426
427
428 if( program.IsAttach() )
429 {
430 //プロセスIDを元にハンドルを取得
431 HANDLE hProcess;
432 hProcess=OpenProcess(PROCESS_ALL_ACCESS,0, program.GetAttachProcessId() );
433 if(!hProcess) goto AttachError;
434
435 //そのプロセスにおける実行モジュールのインスタンスハンドルを取得
436 HINSTANCE hModule;
437 DWORD cbReturned;
438 if(!EnumProcessModules( hProcess, &hModule, sizeof(HINSTANCE), &cbReturned )) goto AttachError;
439
440 //実行ファイル名を取得
441 char tempOutputFileName[MAX_PATH];
442 GetModuleFileNameEx(hProcess,hModule,tempOutputFileName,MAX_PATH);
443 program.SetOutputFilePath( tempOutputFileName );
444
445 CloseHandle(hProcess);
446
447/*
448 //デバッグ用の拡張情報を取得
449 pobj_DebugSection->load(program.GetOutputFilePath().c_str());*/
450
451 if(!DebugActiveProcess( program.GetAttachProcessId() )){
452AttachError:
453 DebugMessage("アタッチに失敗しました。");
454 return;
455 }
456 }
457 else{
458 /*if(!pobj_DebugSection->load(program.GetOutputFilePath().c_str())){
459 extern BOOL bDebugCompile;
460 bDebugCompile=1;
461 Build();
462 pobj_DebugSection->load(program.GetOutputFilePath().c_str());
463 }*/
464
465 //スレッドを生成
466 extern char szDebugCmdLine[1024];
467 STARTUPINFO si;
468 PROCESS_INFORMATION pi;
469 memset(&si,0,sizeof(STARTUPINFO));
470 si.cb=sizeof(STARTUPINFO);
471 if( !compiler.IsDll() ){
472 //EXEファイルをデバッグ
473 CreateProcess(program.GetOutputFilePath().c_str(),szDebugCmdLine,NULL,NULL,0,NORMAL_PRIORITY_CLASS|DEBUG_ONLY_THIS_PROCESS|CREATE_NEW_CONSOLE,NULL,NULL,&si,&pi);
474 }
475 else{
476 //DLLファイルをデバッグ
477 CreateProcess(ExeFilePathForDll,szDebugCmdLine,NULL,NULL,0,NORMAL_PRIORITY_CLASS|DEBUG_ONLY_THIS_PROCESS|CREATE_NEW_CONSOLE,NULL,NULL,&si,&pi);
478 }
479
480 CloseHandle(pi.hProcess);
481 CloseHandle(pi.hThread);
482 }
483
484
485 //デバッグ情報データベースを生成
486 pobj_DBDebugSection=new CDBDebugSection();
487
488 //デバッグスレッド情報(プロシージャの階層構造を管理)を生成
489 pobj_dti=new CDebugThreadInfo();
490
491 UserProc *pUserProc;
492
493 extern DWORD dwStepRun;
494 BOOL bFirstBreak=1;
495 CONTEXT Context;
496 HANDLE hMainThread;
497 ULONG_PTR lpAccBytes;
498
499 DEBUG_EVENT de;
500 memset(&de,0,sizeof(DEBUG_EVENT));
501
502 while(WaitForDebugEvent(&de,INFINITE)){
503 if(de.dwDebugEventCode==LOAD_DLL_DEBUG_EVENT){
504 WCHAR wcBuf[MAX_PATH];
505 LONG_PTR lpData;
506
507 wcBuf[0]=0;
508 temporary[0]=0;
509
510 if(de.u.LoadDll.lpImageName){
511 if(!ReadProcessMemory(hDebugProcess,de.u.LoadDll.lpImageName,&lpData,sizeof(LONG_PTR),&lpAccBytes)){
512 sprintf(temporary,"ロードされたDLLの名前取得(アドレス:&H%08x)に失敗しました。\r\n",(ULONG_PTR)de.u.LoadDll.lpImageName);
513 DebugMessage(temporary);
514 goto NextContinue;
515 }
516 if(!lpData) goto Attach_DllLoad;
517
518 if(!ReadProcessMemory(hDebugProcess,(void *)lpData,wcBuf,sizeof(WCHAR)*MAX_PATH,&lpAccBytes)){
519 sprintf(temporary,"ロードされたDLLの名前取得(アドレス:&H%08x)に失敗しました。\r\n",lpData);
520 DebugMessage(temporary);
521 goto NextContinue;
522 }
523
524 if(de.dwThreadId,de.u.LoadDll.fUnicode)
525 WideCharToMultiByte(CP_ACP,0,wcBuf,-1,temporary,255,NULL,NULL);
526 else lstrcpy(temporary,(char *)wcBuf);
527 }
528 else{
529Attach_DllLoad:
530 //アタッチした場合
531 GetModuleFileNameEx(hDebugProcess,(HINSTANCE)de.u.LoadDll.lpBaseOfDll,temporary,MAX_PATH);
532 }
533
534 char temp2[1024];
535 sprintf(temp2,"\"%s\" をロードしました。\r\n",temporary);
536 DebugMessage(temp2);
537
538
539 //可能であればデバッグ情報を読みとる
540 if(pobj_DBDebugSection->add((HMODULE)de.u.LoadDll.lpBaseOfDll)){
541 pobj_DBDebugSection->choice(0);
542 Set_DebugSys_dwThreadID();
543 }
544
545
546 /*if(lstrcmpi(temporary, program.GetOutputFilePath().c_str())==0){
547 ImageBase=(DWORD)de.u.LoadDll.lpBaseOfDll;
548
549 AddThread(de.dwThreadId,hMainThread);
550 }*/
551 }
552 else if(de.dwDebugEventCode==UNLOAD_DLL_DEBUG_EVENT){
553 //DLLのアンロード
554
555 pobj_DBDebugSection->del((HMODULE)de.u.UnloadDll.lpBaseOfDll);
556 }
557 else if(de.dwDebugEventCode==CREATE_PROCESS_DEBUG_EVENT){
558 hDebugProcess=de.u.CreateProcessInfo.hProcess;
559 hMainThread=de.u.CreateProcessInfo.hThread;
560
561 if(pobj_DBDebugSection->add((HMODULE)de.u.CreateProcessInfo.lpBaseOfImage)){
562 pobj_DBDebugSection->choice(0);
563 }
564
565 AddThread(de.dwThreadId,de.u.CreateProcessInfo.hThread);
566 }
567 else if(de.dwDebugEventCode==CREATE_THREAD_DEBUG_EVENT){
568 AddThread(de.dwThreadId,de.u.CreateThread.hThread);
569 }
570 else if(de.dwDebugEventCode==EXIT_PROCESS_DEBUG_EVENT){
571 //デバッグダイアログを終了
572 SendMessage(hMainDlg,WM_CLOSE_DEBUGGER,0,0);
573
574 DeleteThread(de.dwThreadId);
575
576 //"スレッド(&H%X)はコード &H%X で終了しました。\r\n"
577 sprintf(temporary,STRING_DEBUG_THREADFINISH,de.dwThreadId,de.u.ExitProcess.dwExitCode);
578 DebugMessage(temporary);
579
580 //"プログラムはコード &H%X で終了しました。\r\n"
581 sprintf(temporary,STRING_DEBUG_PROCESSFINISH,de.u.ExitProcess.dwExitCode);
582 DebugMessage(temporary);
583 break;
584 }
585 else if(de.dwDebugEventCode==EXIT_THREAD_DEBUG_EVENT){
586 //"スレッド(&H%X)はコード &H%X で終了しました。\r\n"
587 sprintf(temporary,STRING_DEBUG_THREADFINISH,de.dwThreadId,de.u.ExitThread.dwExitCode);
588 DebugMessage(temporary);
589
590 //以前にシングルステップ実行をした場合
591 //ステップ実行を解除
592 if(dwStepRun){
593 extern HWND hDebugWnd;
594 if(hDebugWnd) SendMessage(hDebugWnd,WM_VARLIST_CLOSE,0,0);
595
596 for(i4=0;i4<MAX_DEBUG_THREAD;i4++){
597 if(de.dwThreadId==_DebugSys_dwThreadID[i4]) break;
598 }
599 Context.ContextFlags=CONTEXT_CONTROL;
600 GetThreadContext(array_hDebugThread[i4],&Context);
601
602 ReleaseSingleStep(dwStepRun,array_hDebugThread[i4],&Context);
603 }
604
605 DeleteThread(de.dwThreadId);
606 }
607 else if(de.dwDebugEventCode==OUTPUT_DEBUG_STRING_EVENT){
608 ReadProcessMemory(hDebugProcess,(void *)de.u.DebugString.lpDebugStringData,temporary,de.u.DebugString.nDebugStringLength,&lpAccBytes);
609 DebugMessage(temporary);
610 }
611 else if(de.dwDebugEventCode==EXCEPTION_DEBUG_EVENT){
612
613 //////////////////////////////////////
614 // モジュールを再確認
615 // ※DLLへのデバッグ分岐を可能にする
616 //////////////////////////////////////
617
618
619 //初回例外は無視
620 if(bFirstBreak){
621 bFirstBreak=0;
622 goto NextContinue;
623 }
624
625 for(i2=0;;i2++){
626 if(_DebugSys_dwThreadID[i2]==de.dwThreadId) break;
627 }
628
629 //スレッド情報をリフレッシュ
630 if(!pobj_dti->Reflesh(i2)){
631 MessageBox(hOwnerEditor,"デバッグ情報が壊れています。ターゲットファイルを再コンパイルしてください。","ActiveBasic",MB_OK|MB_ICONEXCLAMATION);
632
633 break;
634 }
635
636 if(de.u.Exception.ExceptionRecord.ExceptionCode==EXCEPTION_ACCESS_VIOLATION){
637
638 //"スレッド(&H%X)でアクセス違反がありました(EIP=&H%08X)。\r\n"
639 sprintf(temporary,
640 STRING_DEBUG_THREAD_ACCESSVIOLATION,
641 de.dwThreadId,
642 (ULONG_PTR)de.u.Exception.ExceptionRecord.ExceptionAddress,
643#ifdef _AMD64_
644 (ULONG_PTR)Context.Rsp
645#else
646 (ULONG_PTR)Context.Esp
647#endif
648 );
649 DebugMessage(temporary);
650
651 MessageBeep(MB_ICONEXCLAMATION);
652 ShowVarList(&de,1);
653 break;
654 }
655 else if(de.u.Exception.ExceptionRecord.ExceptionCode==EXCEPTION_BREAKPOINT||
656 de.u.Exception.ExceptionRecord.ExceptionCode==EXCEPTION_SINGLE_STEP){
657 /////////////////////////
658 // ブレークポイント
659 /////////////////////////
660
661 if(de.u.Exception.ExceptionRecord.ExceptionCode==EXCEPTION_SINGLE_STEP){
662 //CPU機構のシングルステップによるブレークポイント
663 //※シングルステップを解除
664 ReleaseCPUSingleStep();
665 }
666
667 i3=dwStepRun;
668
669 for(i4=0;i4<MAX_DEBUG_THREAD;i4++){
670 if(de.dwThreadId==_DebugSys_dwThreadID[i4]) break;
671 }
672 Context.ContextFlags=CONTEXT_CONTROL | CONTEXT_INTEGER;
673 GetThreadContext(array_hDebugThread[i4],&Context);
674
675 if(i3==0||
676 i3&&(!(ImageBase+MemPos_CodeSection<=EIP_RIP(Context)&&EIP_RIP(Context)<ImageBase+MemPos_CodeSection+FileSize_CodeSection))
677 ){
678 //"スレッド(&H%X)のブレーク ポイント(EIP=&H%08X)。\r\n"
679 sprintf(temporary,
680 STRING_DEBUG_BREAKPOINT,
681 de.dwThreadId,
682 (ULONG_PTR)de.u.Exception.ExceptionRecord.ExceptionAddress,
683#ifdef _AMD64_
684 (ULONG_PTR)Context.Rsp
685#else
686 (ULONG_PTR)Context.Esp
687#endif
688 );
689 DebugMessage(temporary);
690 }
691
692 ShowVarList(&de,0);
693
694 //ステップ実行を解除
695 ReleaseSingleStep(i3,array_hDebugThread[i4],&Context);
696
697 if(dwStepRun){
698 //新たにシングルステップを行う場合
699
700 if(i4!=NextStepThreadNum){
701 //次回のステップ対象が別スレッドの場合
702 Context.ContextFlags=CONTEXT_CONTROL;
703 GetThreadContext(array_hDebugThread[NextStepThreadNum],&Context);
704 }
705
706 if(ImageBase+MemPos_CodeSection <= EIP_RIP(Context) &&
707 EIP_RIP(Context) < ImageBase+MemPos_CodeSection+FileSize_CodeSection){
708 //Debug命令語が続く場合はシングルステップは不要になるので、無効にする
709 //(オリジナルコード内のみ)
710 if(OpBuffer[EIP_RIP(Context)-ImageBase-MemPos_CodeSection]==(char)0xCC)
711 dwStepRun=0;
712 }
713 if(dwStepRun==1){
714 //ステップイン
715StepIn:
716 //シングルステップON
717 WriteProcessMemory(hDebugProcess,(void *)(ULONG_PTR)(ImageBase+MemPos_CodeSection),
718 pobj_DBDebugSection->pobj_now->SingleStepCodeBuffer,
719 FileSize_CodeSection,&lpAccBytes);
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 else if(dwStepRun==2){
733 //ステップオーバー
734
735 BOOL bRet;
736 bRet=ReadProcessMemory(hDebugProcess,
737 (void *)EIP_RIP(Context),
738 temporary,
739 5,
740 &lpAccBytes);
741 if(!bRet) MessageBox(hMainDlg,"プロセスメモリーの読み込みに失敗","error",MB_OK);
742
743 extern const UserProc *pSub_DebugSys_EndProc;
744 if((BYTE)temporary[0]==0xE8&&
745 *((long *)(temporary+1))+5==(long)rva_to_real(pSub_DebugSys_EndProc->GetBeginOpAddress())-(long)EIP_RIP(Context)){
746 //プロシージャの終端位置の場合はステップインを行う
747 goto StepIn;
748 }
749
750 extern int GlobalOpBufferSize;
751 if(ImageBase+MemPos_CodeSection<=pobj_dti->lplpObp[pobj_dti->iProcLevel]&&
752 pobj_dti->lplpObp[pobj_dti->iProcLevel]<ImageBase+MemPos_CodeSection+GlobalOpBufferSize){
753 //シングルステップON
754 WriteProcessMemory(hDebugProcess,
755 (void *)(ULONG_PTR)(ImageBase+MemPos_CodeSection),
756 pobj_DBDebugSection->pobj_now->SingleStepCodeBuffer,
757 GlobalOpBufferSize,
758 &lpAccBytes);
759 }
760 else{
761 //プロシージャを識別
762 pUserProc=GetSubFromObp(pobj_dti->lplpObp[pobj_dti->iProcLevel]);
763
764 //シングルステップON
765 WriteProcessMemory(hDebugProcess,
766 (void *)rva_to_real(pUserProc->GetBeginOpAddress()),
767 pobj_DBDebugSection->pobj_now->SingleStepCodeBuffer+pUserProc->GetBeginOpAddress(),
768 pUserProc->GetEndOpAddress()-pUserProc->GetBeginOpAddress(),
769 &lpAccBytes);
770 }
771
772 //次の命令語のブレーク命令は解除しておく(シングルステップ実行後のみ)
773 //(オリジナルコード内のみ)
774 if(i3&&ImageBase+MemPos_CodeSection<=EIP_RIP(Context)&&EIP_RIP(Context)<ImageBase+MemPos_CodeSection+FileSize_CodeSection)
775 WriteProcessMemory(hDebugProcess,(void *)EIP_RIP(Context),&OpBuffer[EIP_RIP(Context)-ImageBase-MemPos_CodeSection],1,&lpAccBytes);
776
777 //他のスレッドを一時中断
778 for(i4=0;i4<MAX_DEBUG_THREAD;i4++){
779 if(_DebugSys_dwThreadID[i4] && NextStepThreadNum!=i4)
780 SuspendThread(array_hDebugThread[i4]);
781 }
782 }
783 }
784 }
785 else if(de.u.Exception.ExceptionRecord.ExceptionCode==STATUS_INTEGER_DIVIDE_BY_ZERO){
786 //"0による除算が行われました。スレッド(&H%X) ブレーク ポイント(EIP=&H%08X)。\r\n"
787 sprintf(temporary,STRING_DEBUG_DIVIDE_BY_ZERO,de.dwThreadId,(ULONG_PTR)de.u.Exception.ExceptionRecord.ExceptionAddress);
788 DebugMessage(temporary);
789
790 ShowVarList(&de,1);
791 break;
792 }
793 else if(de.u.Exception.ExceptionRecord.ExceptionCode==STATUS_NO_MEMORY){
794 //"メモリ不足、またはヒープが壊れていることが原因で、メモリの割り当てに失敗しました。スレッド(&H%X) ブレーク ポイント(EIP=&H%08X)。\r\n"
795 sprintf(temporary,STRING_DEBUG_DIVIDE_NO_MEMORY,de.dwThreadId,(ULONG_PTR)de.u.Exception.ExceptionRecord.ExceptionAddress);
796 DebugMessage(temporary);
797
798 ShowVarList(&de,1);
799 break;
800 }
801 else{
802 //"例外処理\ncode:%X"
803 sprintf(temporary,STRING_DEBUG_ERROR,de.u.Exception.ExceptionRecord.ExceptionCode);
804 DebugMessage(temporary);
805 }
806 }
807NextContinue:
808 ContinueDebugEvent(de.dwProcessId,de.dwThreadId,DBG_CONTINUE);
809 }
810
811 extern HWND hDebugWnd;
812 if(hDebugWnd) SendMessage(hDebugWnd,WM_COMMAND,IDCANCEL,0);
813
814 //デバッグに利用した情報を解放
815 delete pobj_DBDebugSection;
816
817 //デバッグスレッド情報を解放
818 delete pobj_dti;
819
820 //ブレークポイントを解放
821 delete pobj_DBBreakPoint;
822
823 //"閉じる"
824 SetDlgItemText(hMainDlg,IDOK,STRING_CLOSE);
825
826 debugger.FinishDebug();
827
828 SendMessage(hOwnerEditor,WM_DESTROYDEBUGGERBASE,0,0);
829}
Note: See TracBrowser for help on using the repository browser.