查看器窗口
在调试窗口不仅仅可以查看普通的变量值,甚至可以输入$handles 去追踪打开的句柄数量,$err 去查看函数的错误代码(然后使用 Tools->Error 查看错误代码的描述)或者输入 @eax(在 64 位下是@rax )查看包含函数返回值的寄存器值。
堆栈信息的线程窗口
调试多线程代码是很痛苦的。或许也是有趣的。这取决于你的调试器。Visual studio 一个很赞的功能就是在线程窗口查看线程的堆栈信息。你能很方便的直接看到所有线程以及他们的堆栈信息。
条件断点
内存窗口
一些 bug 是由错误的结构定义、缺少对齐属性等原因引起的。查看每行内存的内容很容易定位和解决这些 bug. Visual studio 的内存窗口可以把数据翻译成8/16/32/64-bit 数字或者浮点数。你可以在编辑窗口直接改变数值。
Debugging Multithreaded Program
As of now, what I have discussed is all about fundamentals of debugging, knowing debugging tools and their uses. Now let's have a look into the multithreaded scenarios. Here you will see how to work with multithreaded program debugging, where is your current thread, what is the thread execution sequence, what is the state of thread. Before continuing with the demo, let's consider you have the following piece of code which you want to debug.
class ThreadTest
{
static void Main()
{
Thread t = new Thread(new ThreadStart(Go));
t.Name = "Thread 1";
Thread t1 = new Thread(new ThreadStart(Go));
t1.Name = "Thread 2";
t.Start();
t1.Start();
Go();
}
static void Go()
{
Console.WriteLine("hello!");
}
}
In the sample code, you have three different threads - Main Thread, Thread 1, Thread 2. I have given a thread name to make you understand better. Now set a breakpoint inside "Go()" and run the application. When debugger hits the breakpoint, Press Ctrl+D,T or Navigate through Debug > Window > Threads. Threads window will appeared on the screen.
Exploring Threads Window
After selecting the thread window from debug menu, the following screen will come:
Figure: Detail view of Thread window
By default thread window having ID, Managed ID, Category, Name, Location and Priority column. At the start, execution pauses at "Main Thread". "Yellow Arrow" indicates the current executable thread. Category column indicates the category of threads, like main thread or worker thread. If you check the thread location, it is nothing but Namespace > Class > Method name. In the diagram, it is showing that the Main Thread will be executed next. Now to explore the next step by just pressing "F5" and see what are the changes in thread window.
So after pressing F5, it jumped to the next step to thread 1. you can also check the current location for Main Thread. It says "Sleep/ Wait / Join" , means waiting for something to complete. Similarly the next step will move you to thread 2. From the Thread window, you can understand how easy it is to monitor your threads using this debugger tool.
There is another great feature available within the thread window. You can expand/collapse the Thread Locationand can see what is next. For example, if you expand the location for "Main Thread", it will look like the diagram given below:
Flag Just My Code
The sample code which I have explained for the thread debugging is very simple. What will happen if you have a huge code block with multiple number of threads. Then it will be very difficult for you to identify which thread is part of your code or which ones are not related. Thread window gives you very easy features to set the "Flag" for all the threads which are part of your code. For that, you need to just flag your thread by option "Flag Just My Code".
Break Point Filter - Multithread Debugging
While discussing about breakpoint filter in breakpoint section, I said that breakpoint filter is very much helpful for Multithreaded debugging mode. Now this is the time to explore it. In our current example, we have three threads Main Thread, Thread1 and Thread 2. Now what if you want breakpoint to hit only for "Thread 2". What will you do ? Here is the use of breakpoint filter. Right click on the breakpoint, select "Filter" from the context menu. Now in breakpoint filter window, you need to fill the filter criteria. As per your requirement, you need to specify "ThreadName="Thread 2"
.
Here ThreadName
was one of the criteria by which you can filter, but you can filter on multiple clauses likeThreadID
, ProcessName
, ProcessID
, etc. After setting the breakpoint filter, run the application and open the "Threads" window.
You will find your program execution has only paused during the execution of "Thread 2" .
This is all about the debugging with multithreaded application. Hope you have learned something from it. Let's start with another most important topic "Parallel Debugging".
Shortcut Keys | Descriptions |
Ctrl-Alt-V, A | Displays the Auto window |
Ctrl-Alt-B | Displays the Breakpoints dialog |
Ctrl-Alt-C | Displays the Call Stack |
Ctrl-Shift-F9 | Clears all of the breakpoints in the project |
Ctrl-F9 | Enables or disables the breakpoint on the current line of code |
Ctrl-Alt-E | Displays the Exceptions dialog |
Ctrl-Alt-I | Displays the Immediate window |
Ctrl-Alt-V, L | Displays the Locals window |
Ctrl-Alt-Q | Displays the Quick Watch dialog |
Ctrl-Shift-F5 | Terminates the current debugging session, rebuilds if necessary, and starts a new debugging session. |
Ctrl-F10 | Starts or resumes execution of your code and then halts execution when it reaches the selected statement. |
Ctrl-Shift-F10 | Sets the execution point to the line of code you choose |
Alt-NUM * | Highlights the next statement |
F5 | If not currently debugging, this runs the startup project or projects and attaches the debugger. |
Ctrl-F5 | Runs the code without invoking the debugger |
F11 | Step Into |
Shift-F11 | Executes the remaining lines out from procedure |
F10 | Executes the next line of code but does not step into any function calls |
Shift-F5 | Available in break and run modes, this terminates the debugging session |
Ctrl-Alt-H | Displays the Threads window to view all of the threads for the current process |
F9 | Sets or removes a breakpoint at the current line |
Ctrl-Alt-W, 1 | Displays the Watch 1 window to view the values of variables or watch expressions |
Ctrl-Alt-P | Displays the Processes dialog, which allows you to attach or detach the debugger to one or more running processes |
Ctrl-D,V | IntelliTrace Event |
Further Study
- Debugging Task-Based Parallel Applications in Visual Studio 2010 By Daniel Moth and Stephen Toub
- Debugging With IntelliTrace