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

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

・コンパイルビューへの出力を標準出力にも行うようにした。
#164への対応。コンパイルを中断すると高確率で強制終了してしまうバグを修正。
・ProjectEditorをVista用Microsoft SDKにてビルドできるようにした(WINVER定数を0x0501に指定した)。
#168への対応。エディタ上のDelegateキーワードを青色で表示するようにした。

File size: 23.7 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().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 char OutputFileName[MAX_PATH];
380 extern DWORD ImageBase;
381 extern int MemPos_CodeSection;
382 extern int MemPos_RWSection;
383 extern int FileSize_CodeSection;
384 int i2,i3,i4;
385 char temporary[1024];
386
387 char ExeFilePathForDll[MAX_PATH];
388 if( compiler.IsDll() ){
389 //DLLをデバッグする場合
390 extern char szDebugExeForDll[1024];
391 if(szDebugExeForDll[0]){
392 //指定済み
393 lstrcpy(ExeFilePathForDll,szDebugExeForDll);
394 }
395 else{
396 //ユーザーに実行ファイルを選択させる
397 extern HWND hOwnerEditor;
398 extern LPSTR ExeFileFilter;
399 if(!GetFilePathDialog(hOwnerEditor,ExeFilePathForDll,ExeFileFilter,"デバッグ用の実行可能ファイルを指定してください。",1)) return;
400 }
401 }
402
403
404 //"中断"
405 SetDlgItemText(hMainDlg,IDOK,STRING_STOP);
406
407 SendMessage(hOwnerEditor,WM_SETDEBUGGERBASE,0,0);
408
409 //カレントディレクトリを設定
410 extern char BasicCurDir[MAX_PATH];
411 SetCurrentDirectory(BasicCurDir);
412
413 SendDlgItemMessage(hMainDlg,IDC_SHOWERROR,BM_SETCHECK,BST_UNCHECKED,0);
414 SendDlgItemMessage(hMainDlg,IDC_SHOWDEBUG,BM_SETCHECK,BST_CHECKED,0);
415 SendMessage(hMainDlg,WM_COMMAND,IDC_SHOWDEBUG,0);
416
417 //ブレークポイントを生成
418 pobj_DBBreakPoint=new CDBBreakPoint;
419
420
421 extern BOOL bAttach;
422 if(bAttach){
423 extern DWORD dwAttachProcessId;
424
425 //プロセスIDを元にハンドルを取得
426 HANDLE hProcess;
427 hProcess=OpenProcess(PROCESS_ALL_ACCESS,0,dwAttachProcessId);
428 if(!hProcess) goto AttachError;
429
430 //そのプロセスにおける実行モジュールのインスタンスハンドルを取得
431 HINSTANCE hModule;
432 DWORD cbReturned;
433 if(!EnumProcessModules( hProcess, &hModule, sizeof(HINSTANCE), &cbReturned )) goto AttachError;
434
435 //実行ファイル名を取得
436 GetModuleFileNameEx(hProcess,hModule,OutputFileName,MAX_PATH);
437
438 CloseHandle(hProcess);
439
440/*
441 //デバッグ用の拡張情報を取得
442 pobj_DebugSection->load(OutputFileName);*/
443
444 if(!DebugActiveProcess(dwAttachProcessId)){
445AttachError:
446 DebugMessage("アタッチに失敗しました。");
447 return;
448 }
449 }
450 else{
451 /*if(!pobj_DebugSection->load(OutputFileName)){
452 extern BOOL bDebugCompile;
453 bDebugCompile=1;
454 OutputExe();
455 pobj_DebugSection->load(OutputFileName);
456 }*/
457
458 //スレッドを生成
459 extern char szDebugCmdLine[1024];
460 STARTUPINFO si;
461 PROCESS_INFORMATION pi;
462 memset(&si,0,sizeof(STARTUPINFO));
463 si.cb=sizeof(STARTUPINFO);
464 if( !compiler.IsDll() ){
465 //EXEファイルをデバッグ
466 CreateProcess(OutputFileName,szDebugCmdLine,NULL,NULL,0,NORMAL_PRIORITY_CLASS|DEBUG_ONLY_THIS_PROCESS|CREATE_NEW_CONSOLE,NULL,NULL,&si,&pi);
467 }
468 else{
469 //DLLファイルをデバッグ
470 CreateProcess(ExeFilePathForDll,szDebugCmdLine,NULL,NULL,0,NORMAL_PRIORITY_CLASS|DEBUG_ONLY_THIS_PROCESS|CREATE_NEW_CONSOLE,NULL,NULL,&si,&pi);
471 }
472
473 CloseHandle(pi.hProcess);
474 CloseHandle(pi.hThread);
475 }
476
477
478 //デバッグ情報データベースを生成
479 pobj_DBDebugSection=new CDBDebugSection();
480
481 //デバッグスレッド情報(プロシージャの階層構造を管理)を生成
482 pobj_dti=new CDebugThreadInfo();
483
484 UserProc *pUserProc;
485
486 extern DWORD dwStepRun;
487 BOOL bFirstBreak=1;
488 CONTEXT Context;
489 HANDLE hMainThread;
490 ULONG_PTR lpAccBytes;
491
492 DEBUG_EVENT de;
493 memset(&de,0,sizeof(DEBUG_EVENT));
494
495 while(WaitForDebugEvent(&de,INFINITE)){
496 if(de.dwDebugEventCode==LOAD_DLL_DEBUG_EVENT){
497 WCHAR wcBuf[MAX_PATH];
498 LONG_PTR lpData;
499
500 wcBuf[0]=0;
501 temporary[0]=0;
502
503 if(de.u.LoadDll.lpImageName){
504 if(!ReadProcessMemory(hDebugProcess,de.u.LoadDll.lpImageName,&lpData,sizeof(LONG_PTR),&lpAccBytes)){
505 sprintf(temporary,"ロードされたDLLの名前取得(アドレス:&H%08x)に失敗しました。\r\n",(ULONG_PTR)de.u.LoadDll.lpImageName);
506 DebugMessage(temporary);
507 goto NextContinue;
508 }
509 if(!lpData) goto Attach_DllLoad;
510
511 if(!ReadProcessMemory(hDebugProcess,(void *)lpData,wcBuf,sizeof(WCHAR)*MAX_PATH,&lpAccBytes)){
512 sprintf(temporary,"ロードされたDLLの名前取得(アドレス:&H%08x)に失敗しました。\r\n",lpData);
513 DebugMessage(temporary);
514 goto NextContinue;
515 }
516
517 if(de.dwThreadId,de.u.LoadDll.fUnicode)
518 WideCharToMultiByte(CP_ACP,0,wcBuf,-1,temporary,255,NULL,NULL);
519 else lstrcpy(temporary,(char *)wcBuf);
520 }
521 else{
522Attach_DllLoad:
523 //アタッチした場合
524 GetModuleFileNameEx(hDebugProcess,(HINSTANCE)de.u.LoadDll.lpBaseOfDll,temporary,MAX_PATH);
525 }
526
527 char temp2[1024];
528 sprintf(temp2,"\"%s\" をロードしました。\r\n",temporary);
529 DebugMessage(temp2);
530
531
532 //可能であればデバッグ情報を読みとる
533 if(pobj_DBDebugSection->add((HMODULE)de.u.LoadDll.lpBaseOfDll)){
534 pobj_DBDebugSection->choice(0);
535 Set_DebugSys_dwThreadID();
536 }
537
538
539 /*if(lstrcmpi(temporary, OutputFileName)==0){
540 ImageBase=(DWORD)de.u.LoadDll.lpBaseOfDll;
541
542 AddThread(de.dwThreadId,hMainThread);
543 }*/
544 }
545 else if(de.dwDebugEventCode==UNLOAD_DLL_DEBUG_EVENT){
546 //DLLのアンロード
547
548 pobj_DBDebugSection->del((HMODULE)de.u.UnloadDll.lpBaseOfDll);
549 }
550 else if(de.dwDebugEventCode==CREATE_PROCESS_DEBUG_EVENT){
551 hDebugProcess=de.u.CreateProcessInfo.hProcess;
552 hMainThread=de.u.CreateProcessInfo.hThread;
553
554 if(pobj_DBDebugSection->add((HMODULE)de.u.CreateProcessInfo.lpBaseOfImage)){
555 pobj_DBDebugSection->choice(0);
556 }
557
558 AddThread(de.dwThreadId,de.u.CreateProcessInfo.hThread);
559 }
560 else if(de.dwDebugEventCode==CREATE_THREAD_DEBUG_EVENT){
561 AddThread(de.dwThreadId,de.u.CreateThread.hThread);
562 }
563 else if(de.dwDebugEventCode==EXIT_PROCESS_DEBUG_EVENT){
564 //デバッグダイアログを終了
565 SendMessage(hMainDlg,WM_CLOSE_DEBUGGER,0,0);
566
567 DeleteThread(de.dwThreadId);
568
569 //"スレッド(&H%X)はコード &H%X で終了しました。\r\n"
570 sprintf(temporary,STRING_DEBUG_THREADFINISH,de.dwThreadId,de.u.ExitProcess.dwExitCode);
571 DebugMessage(temporary);
572
573 //"プログラムはコード &H%X で終了しました。\r\n"
574 sprintf(temporary,STRING_DEBUG_PROCESSFINISH,de.u.ExitProcess.dwExitCode);
575 DebugMessage(temporary);
576 break;
577 }
578 else if(de.dwDebugEventCode==EXIT_THREAD_DEBUG_EVENT){
579 //"スレッド(&H%X)はコード &H%X で終了しました。\r\n"
580 sprintf(temporary,STRING_DEBUG_THREADFINISH,de.dwThreadId,de.u.ExitThread.dwExitCode);
581 DebugMessage(temporary);
582
583 //以前にシングルステップ実行をした場合
584 //ステップ実行を解除
585 if(dwStepRun){
586 extern HWND hDebugWnd;
587 if(hDebugWnd) SendMessage(hDebugWnd,WM_VARLIST_CLOSE,0,0);
588
589 for(i4=0;i4<MAX_DEBUG_THREAD;i4++){
590 if(de.dwThreadId==_DebugSys_dwThreadID[i4]) break;
591 }
592 Context.ContextFlags=CONTEXT_CONTROL;
593 GetThreadContext(array_hDebugThread[i4],&Context);
594
595 ReleaseSingleStep(dwStepRun,array_hDebugThread[i4],&Context);
596 }
597
598 DeleteThread(de.dwThreadId);
599 }
600 else if(de.dwDebugEventCode==OUTPUT_DEBUG_STRING_EVENT){
601 ReadProcessMemory(hDebugProcess,(void *)de.u.DebugString.lpDebugStringData,temporary,de.u.DebugString.nDebugStringLength,&lpAccBytes);
602 DebugMessage(temporary);
603 }
604 else if(de.dwDebugEventCode==EXCEPTION_DEBUG_EVENT){
605
606 //////////////////////////////////////
607 // モジュールを再確認
608 // ※DLLへのデバッグ分岐を可能にする
609 //////////////////////////////////////
610
611
612 //初回例外は無視
613 if(bFirstBreak){
614 bFirstBreak=0;
615 goto NextContinue;
616 }
617
618 for(i2=0;;i2++){
619 if(_DebugSys_dwThreadID[i2]==de.dwThreadId) break;
620 }
621
622 //スレッド情報をリフレッシュ
623 if(!pobj_dti->Reflesh(i2)){
624 MessageBox(hOwnerEditor,"デバッグ情報が壊れています。ターゲットファイルを再コンパイルしてください。","ActiveBasic",MB_OK|MB_ICONEXCLAMATION);
625
626 break;
627 }
628
629 if(de.u.Exception.ExceptionRecord.ExceptionCode==EXCEPTION_ACCESS_VIOLATION){
630
631 //"スレッド(&H%X)でアクセス違反がありました(EIP=&H%08X)。\r\n"
632 sprintf(temporary,
633 STRING_DEBUG_THREAD_ACCESSVIOLATION,
634 de.dwThreadId,
635 (ULONG_PTR)de.u.Exception.ExceptionRecord.ExceptionAddress,
636#ifdef _AMD64_
637 (ULONG_PTR)Context.Rsp
638#else
639 (ULONG_PTR)Context.Esp
640#endif
641 );
642 DebugMessage(temporary);
643
644 MessageBeep(MB_ICONEXCLAMATION);
645 ShowVarList(&de,1);
646 break;
647 }
648 else if(de.u.Exception.ExceptionRecord.ExceptionCode==EXCEPTION_BREAKPOINT||
649 de.u.Exception.ExceptionRecord.ExceptionCode==EXCEPTION_SINGLE_STEP){
650 /////////////////////////
651 // ブレークポイント
652 /////////////////////////
653
654 if(de.u.Exception.ExceptionRecord.ExceptionCode==EXCEPTION_SINGLE_STEP){
655 //CPU機構のシングルステップによるブレークポイント
656 //※シングルステップを解除
657 ReleaseCPUSingleStep();
658 }
659
660 i3=dwStepRun;
661
662 for(i4=0;i4<MAX_DEBUG_THREAD;i4++){
663 if(de.dwThreadId==_DebugSys_dwThreadID[i4]) break;
664 }
665 Context.ContextFlags=CONTEXT_CONTROL | CONTEXT_INTEGER;
666 GetThreadContext(array_hDebugThread[i4],&Context);
667
668 if(i3==0||
669 i3&&(!(ImageBase+MemPos_CodeSection<=EIP_RIP(Context)&&EIP_RIP(Context)<ImageBase+MemPos_CodeSection+FileSize_CodeSection))
670 ){
671 //"スレッド(&H%X)のブレーク ポイント(EIP=&H%08X)。\r\n"
672 sprintf(temporary,
673 STRING_DEBUG_BREAKPOINT,
674 de.dwThreadId,
675 (ULONG_PTR)de.u.Exception.ExceptionRecord.ExceptionAddress,
676#ifdef _AMD64_
677 (ULONG_PTR)Context.Rsp
678#else
679 (ULONG_PTR)Context.Esp
680#endif
681 );
682 DebugMessage(temporary);
683 }
684
685 ShowVarList(&de,0);
686
687 //ステップ実行を解除
688 ReleaseSingleStep(i3,array_hDebugThread[i4],&Context);
689
690 if(dwStepRun){
691 //新たにシングルステップを行う場合
692
693 if(i4!=NextStepThreadNum){
694 //次回のステップ対象が別スレッドの場合
695 Context.ContextFlags=CONTEXT_CONTROL;
696 GetThreadContext(array_hDebugThread[NextStepThreadNum],&Context);
697 }
698
699 if(ImageBase+MemPos_CodeSection <= EIP_RIP(Context) &&
700 EIP_RIP(Context) < ImageBase+MemPos_CodeSection+FileSize_CodeSection){
701 //Debug命令語が続く場合はシングルステップは不要になるので、無効にする
702 //(オリジナルコード内のみ)
703 if(OpBuffer[EIP_RIP(Context)-ImageBase-MemPos_CodeSection]==(char)0xCC)
704 dwStepRun=0;
705 }
706 if(dwStepRun==1){
707 //ステップイン
708StepIn:
709 //シングルステップON
710 WriteProcessMemory(hDebugProcess,(void *)(ULONG_PTR)(ImageBase+MemPos_CodeSection),
711 pobj_DBDebugSection->pobj_now->SingleStepCodeBuffer,
712 FileSize_CodeSection,&lpAccBytes);
713
714 //次の命令語のブレーク命令は解除しておく(シングルステップ実行後のみ)
715 //(オリジナルコード内のみ)
716 if(i3&&ImageBase+MemPos_CodeSection<=EIP_RIP(Context)&&EIP_RIP(Context)<ImageBase+MemPos_CodeSection+FileSize_CodeSection)
717 WriteProcessMemory(hDebugProcess,(void *)EIP_RIP(Context),&OpBuffer[EIP_RIP(Context)-ImageBase-MemPos_CodeSection],1,&lpAccBytes);
718
719 //他のスレッドを一時中断
720 for(i4=0;i4<MAX_DEBUG_THREAD;i4++){
721 if(_DebugSys_dwThreadID[i4] && NextStepThreadNum!=i4)
722 SuspendThread(array_hDebugThread[i4]);
723 }
724 }
725 else if(dwStepRun==2){
726 //ステップオーバー
727
728 BOOL bRet;
729 bRet=ReadProcessMemory(hDebugProcess,
730 (void *)EIP_RIP(Context),
731 temporary,
732 5,
733 &lpAccBytes);
734 if(!bRet) MessageBox(hMainDlg,"プロセスメモリーの読み込みに失敗","error",MB_OK);
735
736 extern const UserProc *pSub_DebugSys_EndProc;
737 if((BYTE)temporary[0]==0xE8&&
738 *((long *)(temporary+1))+5==(long)rva_to_real(pSub_DebugSys_EndProc->GetBeginOpAddress())-(long)EIP_RIP(Context)){
739 //プロシージャの終端位置の場合はステップインを行う
740 goto StepIn;
741 }
742
743 extern int GlobalOpBufferSize;
744 if(ImageBase+MemPos_CodeSection<=pobj_dti->lplpObp[pobj_dti->iProcLevel]&&
745 pobj_dti->lplpObp[pobj_dti->iProcLevel]<ImageBase+MemPos_CodeSection+GlobalOpBufferSize){
746 //シングルステップON
747 WriteProcessMemory(hDebugProcess,
748 (void *)(ULONG_PTR)(ImageBase+MemPos_CodeSection),
749 pobj_DBDebugSection->pobj_now->SingleStepCodeBuffer,
750 GlobalOpBufferSize,
751 &lpAccBytes);
752 }
753 else{
754 //プロシージャを識別
755 pUserProc=GetSubFromObp(pobj_dti->lplpObp[pobj_dti->iProcLevel]);
756
757 //シングルステップON
758 WriteProcessMemory(hDebugProcess,
759 (void *)rva_to_real(pUserProc->GetBeginOpAddress()),
760 pobj_DBDebugSection->pobj_now->SingleStepCodeBuffer+pUserProc->GetBeginOpAddress(),
761 pUserProc->GetEndOpAddress()-pUserProc->GetBeginOpAddress(),
762 &lpAccBytes);
763 }
764
765 //次の命令語のブレーク命令は解除しておく(シングルステップ実行後のみ)
766 //(オリジナルコード内のみ)
767 if(i3&&ImageBase+MemPos_CodeSection<=EIP_RIP(Context)&&EIP_RIP(Context)<ImageBase+MemPos_CodeSection+FileSize_CodeSection)
768 WriteProcessMemory(hDebugProcess,(void *)EIP_RIP(Context),&OpBuffer[EIP_RIP(Context)-ImageBase-MemPos_CodeSection],1,&lpAccBytes);
769
770 //他のスレッドを一時中断
771 for(i4=0;i4<MAX_DEBUG_THREAD;i4++){
772 if(_DebugSys_dwThreadID[i4] && NextStepThreadNum!=i4)
773 SuspendThread(array_hDebugThread[i4]);
774 }
775 }
776 }
777 }
778 else if(de.u.Exception.ExceptionRecord.ExceptionCode==STATUS_INTEGER_DIVIDE_BY_ZERO){
779 //"0による除算が行われました。スレッド(&H%X) ブレーク ポイント(EIP=&H%08X)。\r\n"
780 sprintf(temporary,STRING_DEBUG_DIVIDE_BY_ZERO,de.dwThreadId,(ULONG_PTR)de.u.Exception.ExceptionRecord.ExceptionAddress);
781 DebugMessage(temporary);
782
783 ShowVarList(&de,1);
784 break;
785 }
786 else if(de.u.Exception.ExceptionRecord.ExceptionCode==STATUS_NO_MEMORY){
787 //"メモリ不足、またはヒープが壊れていることが原因で、メモリの割り当てに失敗しました。スレッド(&H%X) ブレーク ポイント(EIP=&H%08X)。\r\n"
788 sprintf(temporary,STRING_DEBUG_DIVIDE_NO_MEMORY,de.dwThreadId,(ULONG_PTR)de.u.Exception.ExceptionRecord.ExceptionAddress);
789 DebugMessage(temporary);
790
791 ShowVarList(&de,1);
792 break;
793 }
794 else{
795 //"例外処理\ncode:%X"
796 sprintf(temporary,STRING_DEBUG_ERROR,de.u.Exception.ExceptionRecord.ExceptionCode);
797 DebugMessage(temporary);
798 }
799 }
800NextContinue:
801 ContinueDebugEvent(de.dwProcessId,de.dwThreadId,DBG_CONTINUE);
802 }
803
804 extern HWND hDebugWnd;
805 if(hDebugWnd) SendMessage(hDebugWnd,WM_COMMAND,IDCANCEL,0);
806
807 //デバッグに利用した情報を解放
808 delete pobj_DBDebugSection;
809
810 //デバッグスレッド情報を解放
811 delete pobj_dti;
812
813 //ブレークポイントを解放
814 delete pobj_DBBreakPoint;
815
816 //"閉じる"
817 SetDlgItemText(hMainDlg,IDOK,STRING_CLOSE);
818
819 SendMessage(hOwnerEditor,WM_DESTROYDEBUGGERBASE,0,0);
820}
Note: See TracBrowser for help on using the repository browser.