問題描述
在 Visual Studio(在我的例子中是 2005)中調試程序時讓我煩惱的一件事是,當我使用step over"(通過按 F10)執行到下一行代碼時,我經常最終在與我正在查看的線程完全不同的線程中到達特定的代碼行.這意味著我正在做的事情的所有上下文都丟失了.
我該如何解決這個問題?
如果這可以在更高版本的 Visual Studio 中實現,我也想了解一下.
在下一行代碼上設置一個斷點,它有條件只為這個線程中斷不是我正在尋找的答案,因為它對我來說太有用了:)
我認為您的問題只有一個答案,您認為這是工作量太大".但是,我相信這是因為您以錯誤的方式進行操作.讓我介紹在線程 ID 上添加條件斷點的步驟,這些步驟非常簡單,但在您了解它們之前并不明顯.
在您要繼續調試的正確線程中停止調試器(我猜這通常是到達那里的第一個線程).
在監視窗口中輸入
$TID
.添加一個斷點,條件為
$TID == <
觀察窗口中$TID的值>
,
示例:$TID == 0x000016a0
繼續執行.
$TID
是 Microsoft 編譯器(至少從 Visual Studio 2003 起)的魔術變量,具有當前線程 ID 的值.它比查看 (FS+0x18)[0x24] 容易得多.=D
話雖如此,您可以使用一些簡單的宏獲得與調試器的 One-Shot 斷點相同的行為.當您跳過時,調試器在后臺設置一個斷點,運行到該斷點,然后將其刪除.保持用戶界面一致的關鍵是在遇到任何斷點時刪除這些斷點.
以下兩個宏為當前線程提供Step Over 和Run To Cursor.這是以與調試器相同的方式完成的,執行后斷點將被刪除,無論命中哪個斷點.
您需要分配一個組合鍵來運行它們.
<塊引用>注意:一個警告——Step Over 宏只有在光標位于您想要跳過的行上時才能正常工作.這是因為它通過光標位置確定當前位置,并簡單地將行號加一.您也許可以用當前執行點的信息替換位置計算,但我無法從宏 IDE 中找到該信息.
他們來了,祝你好運!
<塊引用>要在 Visual Studio 中使用這些宏:
1. 打開宏 IDE(從菜單中選擇:Tools->Macros->Macro IDE...)
2. 添加一個新的代碼文件(從菜單中:選擇:Project->Add New Item...,選擇Code File,然后點擊Addem> )
3. 粘貼此代碼.
4. 保存文件.
添加組合鍵以在 Visual Studio 中運行這些宏:
1. 打開選項(從菜單中選擇:工具->選項)
2. 展開到Environment->Keyboard
3. 在 Show commands contains: 中,輸入 Macros. 以查看您的所有宏.
4. 選擇一個宏,然后點擊按快捷鍵:
5. 輸入您要使用的組合(退格鍵刪除輸入的組合)
6. 點擊Assign設置運行所選宏的快捷方式.
導入系統進口 EnvDTE進口 EnvDTE80導入系統.診斷公共模塊 DebugHelperFunctions子 RunToCursorInMyThread()Dim textSelection As EnvDTE.TextSelection將 myThread 調暗為 EnvDTE.ThreadDim bp As EnvDTE.BreakpointDim bps As EnvDTE.Breakpoints' 對于 Breakpoints.Add()將文件名變暗為字符串Dim LineNumber 作為整數Dim ThreadID 作為字符串' 獲取本地參考以方便使用myThread = DTE.Debugger.CurrentThreadtextSelection = DTE.ActiveDocument.SelectionLineNumber = textSelection.ActivePoint.Line文件名 = textSelection.DTE.ActiveDocument.FullName線程ID = myThread.ID' 在當前文件的當前行中為當前線程添加一個一次性"斷點bps = DTE.Debugger.Breakpoints.Add("", FileName, LineNumber, 1, "$TID == " & ThreadID)'跑到下一站DTE.Debugger.Go(真)' 刪除我們的一次性"斷點對于每個 bp 在 bpsbp.Delete()下一個結束子子 StepOverInMyThread()Dim textSelection As EnvDTE.TextSelection將 myThread 調暗為 EnvDTE.ThreadDim bp As EnvDTE.BreakpointDim bps As EnvDTE.Breakpoints' 對于 Breakpoints.Add()將文件名變暗為字符串Dim LineNumber 作為整數Dim ThreadID 作為字符串' 獲取本地參考以方便使用myThread = DTE.Debugger.CurrentThreadtextSelection = DTE.ActiveDocument.SelectionLineNumber = textSelection.ActivePoint.Line文件名 = textSelection.DTE.ActiveDocument.FullName線程ID = myThread.ID行號 = 行號 + 1' 在當前文件的當前行中為當前線程添加一個一次性"斷點bps = DTE.Debugger.Breakpoints.Add("", FileName, LineNumber, 1, "$TID == " & ThreadID)'跑到下一站DTE.Debugger.Go(真)' 刪除我們的一次性"斷點對于每個 bp 在 bpsbp.Delete()下一個結束子終端模塊
<塊引用>
免責聲明:我在 Visual Studio 2005 中編寫了這些宏.您可能可以在 Visual Studio 2008 中很好地使用它們.它們可能需要針對 Visual Studio 2003 及更早版本進行修改.
One thing that annoys me when debugging programs in Visual Studio (2005 in my case) is that when I use "step over" (by pressing F10) to execute to the next line of code, I often end up reaching that particular line of code in a totally different thread than the one I was looking at. This means that all the context of what I was doing was lost.
How do I work around this?
If this is possible to do in later versions of Visual Studio, I'd like to hear about it as well.
Setting a breakpoint on the next line of code which has a conditional to only break for this thread is not the answer I'm looking for since it is way too much work to be useful for me :)
I think there is only one answer to your question, which you have discounted as being 'way too much work.' However, I believe that is because you are going about it the wrong way. Let me present steps for adding a conditional breakpoint on Thread ID, which are extremely easy, but not obvious until you know them.
Stop the debugger at a point where you are in the correct thread you want to continue debugging in (which I would guess is usually the first thread that gets there).
Enter
$TID
into the watch window.Add a break point with the condition
$TID == <
value of $TID from Watch Window>
,
Example:$TID == 0x000016a0
Continue Execution.
$TID
is a magic variable for Microsoft compilers (since at least Visual Studio 2003) that has the value of the current Thread ID. It makes it much easier than looking at (FS+0x18)[0x24]. =D
That being said, you can get the same behavior as the debugger's One-Shot breakpoints with some simple macros. When you step over, the debugger, behind the scenes, sets a breakpoint, runs to that breakpoint and then removes it. The key to a consistent user interface is removing those breakpoints if ANY breakpoint is hit.
The following two macros provide Step Over and Run To Cursor for the current thread. This is accomplished in the same manner as the debugger, with the breakpoints being removed after execution, regardless of which breakpoint is hit.
You will want to assign a key combination to run them.
NOTE: One caveat -- The Step Over macro only works correctly if the cursor is on the line you want to step over. This is because it determines the current location by the cursor location, and simply adds one to the line number. You may be able to replace the location calculation with information on the current execution point, though I was unable to locate that information from the Macro IDE.
Here they are and good luck bug hunting!!
To use these macros in Visual Studio:
1. Open the Macro IDE ( from the Menu, select: Tools->Macros->Macro IDE... )
2. Add a new Code File ( from the Menu: select: Project->Add New Item..., choose Code File, and click Add )
3. Paste in this code.
4. Save the file.To add key combinations for running these macros in Visual Studio:
1. Open Options (from the Menu, select: Tools->Options )
2. Expand to Environment->Keyboard
3. In Show commands containing:, type Macros. to see all your macros.
4. Select a macro, then click in Press shortcut keys:
5. Type the combo you want to use (backspace deletes typed combos)
6. click Assign to set your shortcut to run the selected macro.
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics
Public Module DebugHelperFunctions
Sub RunToCursorInMyThread()
Dim textSelection As EnvDTE.TextSelection
Dim myThread As EnvDTE.Thread
Dim bp As EnvDTE.Breakpoint
Dim bps As EnvDTE.Breakpoints
' For Breakpoints.Add()
Dim FileName As String
Dim LineNumber As Integer
Dim ThreadID As String
' Get local references for ease of use
myThread = DTE.Debugger.CurrentThread
textSelection = DTE.ActiveDocument.Selection
LineNumber = textSelection.ActivePoint.Line
FileName = textSelection.DTE.ActiveDocument.FullName
ThreadID = myThread.ID
' Add a "One-Shot" Breakpoint in current file on current line for current thread
bps = DTE.Debugger.Breakpoints.Add("", FileName, LineNumber, 1, "$TID == " & ThreadID)
' Run to the next stop
DTE.Debugger.Go(True)
' Remove our "One-Shot" Breakpoint
For Each bp In bps
bp.Delete()
Next
End Sub
Sub StepOverInMyThread()
Dim textSelection As EnvDTE.TextSelection
Dim myThread As EnvDTE.Thread
Dim bp As EnvDTE.Breakpoint
Dim bps As EnvDTE.Breakpoints
' For Breakpoints.Add()
Dim FileName As String
Dim LineNumber As Integer
Dim ThreadID As String
' Get local references for ease of use
myThread = DTE.Debugger.CurrentThread
textSelection = DTE.ActiveDocument.Selection
LineNumber = textSelection.ActivePoint.Line
FileName = textSelection.DTE.ActiveDocument.FullName
ThreadID = myThread.ID
LineNumber = LineNumber + 1
' Add a "One-Shot" Breakpoint in current file on current line for current thread
bps = DTE.Debugger.Breakpoints.Add("", FileName, LineNumber, 1, "$TID == " & ThreadID)
' Run to the next stop
DTE.Debugger.Go(True)
' Remove our "One-Shot" Breakpoint
For Each bp In bps
bp.Delete()
Next
End Sub
End Module
Disclaimer: I wrote these macros in Visual Studio 2005. You can probably use them fine in Visual Studio 2008. They may require modification for Visual Studio 2003 and before.
這篇關于“跨過"在 Visual Studio 中調試多線程程序時的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!