按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
breakpoint page fault code page
( )。當除錯器企圖寫入斷點指令,會樱l個 (因為 擁
有唯讀屬性)。作業系統看到這個page fault ,首先斷定是除錯器企圖讀記憶體內容,
這是合法的。然而,隨後寫入到共享記憶體的動作就不被允許了。系統於是會先將受
影響的各頁拷貝份,並改變被除錯者的 page table ,使映射關係轉變到這份拷貝版。
旦記憶體被拷貝並被映射,系統就可以讓寫入動作過關了。寫入動作只影響拷貝內容,
923
…………………………………………………………Page 986……………………………………………………………
第五篇 附錄
不影響原先內容。
Copy on write 機制的最大好處就是儘可能讓記憶體獲得共享效益。只有在必要時刻,系
統才會對共享記憶體做出新的拷貝。
MFC TRACE afxDump
在 程式,所有的詳嘀噶罨蚓藜òā 。┦聦嵍剂鹘泜名為
的物件之,那是個 CDumpContext 物件。所有的詳鄤幼鞫歼M入
CDumpContext::OutputString 成員函式,然後才進入全域函式 AfxOutputDebugString ,把
字串送往除錯器。
攔截除錯器是很困難的啦,但是你知道,字串也可以被送往檔案。如果我們能夠把送往
檔案的字串攔截來,大功就完成了半。這個通往檔案的奧秘在哪裡呢?看看 MFC 的
圖一 m_pFile m_pFile
原始碼( )。啊哈,我們發現,如果 有所指定,字串就流往檔案。
是個 CFile 物件(圖二)。
// in MFC 4。x DUMPCONT。CPP
#0001 void CDumpContext::OutputString(LPCTSTR lpsz)
#0002 {
#0003 #ifdef _DEBUG
#0004 // all CDumpContext output is controlled by afxTraceEnabled
#0005 if (!afxTraceEnabled)
#0006 return;
#0007 #endif
#0008
#0009 // use C…runtime/OutputDebugString when m_pFile is NULL
#0010 if (m_pFile == NULL)
#0011 {
#0012 AfxOutputDebugString(lpsz);
#0013 return;
#0014 }
#0015
#0016 // otherwise; write the string to the file
#0017 m_pFile…》Write(lpsz; lstrlen(lpsz)*sizeof(TCHAR));
#0018 }
圖一 CDumpContext::OutputString 原始碼
924
…………………………………………………………Page 987……………………………………………………………
附錄D 以MFC 重建DBWIN
// in MFC 4。x AFX。H
#0001 class CDumpContext
#0002 {
#0003 。。。
#0004 public:
#0005 CFile* m_pFile;
#0006 };
圖二 CDumpContext 原始碼
CMfxTrace 圖三 CFile
好,如果我們能夠設計個類別 ( ),衍生自 ,然後為它設計
個初始化成員函式,令函式之檢查 afxDump。m_pFile 內容,並且如果是 NULL ,就將
它指向我們的新類別,那麼 CDumpContext::OutputString #17 行的 m_pFile…》Write 就會
CMfxTrace Write
因此指向新類別 的 函式,於是我們就可以在其予取予求啦。
注意,theTracer 是個 static 成員變數,需要做初始化動作(請參考深入湷錾钊霚出MFC (侯
深入湷錾钊霚出
/
俊傑 松崗)第2章「靜態成員」節),因此你必須在類別之外的任何方加這行:
CMfxTrace CMfxTrace::theTracer;
#0001 class CMfxTrace : public CFile
#0002 {
#0003 static CMfxTrace theTracer; // one…and…only tracing object
#0004 CMfxTrace(); // private constructor
#0005 public:
#0006 virtual void Write(const void* lpBuf; UINT nCount);
#0007 static void Init();
#0008 };
#0001 // Initialize tracing。 Replace global afxDump。m_pFile with me。
#0002 void CMfxTrace::Init()
#0003 {
#0004 if (afxDump。m_pFile == NULL) {
#0005 afxDump。m_pFile = &theTracer;
#0006 } else if (afxDump。m_pFile != &theTracer) {
#0007 TRACE(〃afxDump is already using a file: TRACEWIN not installed。n〃);
#0008 }
#0009 }
圖三 CMfxTrace
925
…………………………………………………………Page 988……………………………………………………………
第五篇 附錄
InterProcess munication IPC
行程通訊( , )
CMfxTrace Write Write
把 TRACE 輸出字串攔截到 :: 之後,我們得想辦法在 函式把字
串丟給 Tracewin 程式視窗。在 Windows 3。1 之這不是難事,因為所有的行程
process
( )共同在同個位址空間生存,直接把字串指標(代表個邏輯位址)丟給
對方就是了。在 Windows 95 和 Windows NT 困難度就高了許多,因為每個行程擁
有自己的位址空間,你把字串指標丟給別的行程,對別的行程而言是洠в杏玫摹H绻恪
Memory Map File Win32
為此使用到記憶體映射檔( , 之唯的種行程通訊方法),
又似乎有點小睿笞鳌!
Paul DiLascia global atom atom Win32
的方法是,利用所謂的 。 並不是 的新產物,有
Win16 DDE Dynamic Data Exchange atom
過 ( ,動態資料交換)程式經驗的就知道, 被
handle global atom
用來當作行程之間傳遞字串的識別物。說穿了它就是個 。 可以跨越
CMfxTrace Write
Win32 行程間的位址空間隔離。面是 :: 函式的動作步驟:
1。 利用 FindWindow 找出 Tracewin 視窗。
2。 利用 GlobalAddAtom 把字串轉換為個 global atom 。
3。 Tracewin gloabl atom
送出個特定訊息給 ,並以述的 為參數。
4。 刪除 global atom 。
5。 Tracewin :: OutputDebugString TRACE
萬洠в姓业健 ∫暣埃艚小 。尅 【藜瘬碛小
原本該有的行為。
圖四 CMfxTrace Write
就是 :: 函式碼。其的兩個常數分別定義為:
#define TRACEWND_CLASSNAME 〃MfxTraceWindow〃 // 視窗類別名稱
#define TRACEWND_MESSAGE 〃*WM_TRACE_MSG〃 // 自行裕缘摹indows 訊息
#0001 // Override Write function to Write to TRACEWIN applet instead of file。
#0002 //
#0003 void CMfxTrace::Write(const void* lpBuf; UINT nCount)
#0004 {
#0005 if (!afxTraceEnabled)
926
…………………………………………………………Page 989……………………………………………………………