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

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

NativeSectionクラスを追加(64bit版だけ一旦コミット)。

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