{"id":12645,"date":"2017-06-26T09:30:51","date_gmt":"2017-06-26T16:30:51","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/visualstudio\/?p=12645"},"modified":"2019-02-14T15:27:08","modified_gmt":"2019-02-14T23:27:08","slug":"7-lesser-known-hacks-for-debugging-in-visual-studio","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/visualstudio\/7-lesser-known-hacks-for-debugging-in-visual-studio\/","title":{"rendered":"7 lesser known hacks for debugging in Visual Studio"},"content":{"rendered":"<p>The Visual Studio debugger is a magical beast that can save you loads of time while finding and fixing issues in your application. It is chock-full of tools that can make debugging easier\u2026 if you know they exist, and where to find them! Let\u2019s look at 7 lesser known goodies you can use to help you <a target=\"_blank\" href=\"https:\/\/twitter.com\/hashtag\/SuperChargeYourDebugging?src=hash\">#SuperChargeYourDebugging<\/a>.<\/p>\n<h2>1. Click to Set Next Statement<\/h2>\n<p>Many of you may know about the context menu item <b>Set Next Statement (Ctrl+Shift+F10)<\/b> that moves the yellow arrow (the instruction pointer) to the target line of code. You may also know that you grab and drag the yellow arrow up and down in the gutter to move it. What you probably didn\u2019t know is that as of <a target=\"_blank\" href=\"https:\/\/www.visualstudio.com\/en-us\/news\/releasenotes\/vs2017-preview-relnotes\">Visual Studio 2017 version 15,3 Preview<\/a> there is an even easier way to target a line and Set Next Statement.<\/p>\n<p>1. Hover over the line of code where you want to move the yellow arrow.<\/p>\n<p>2. Hold the <b>CTRL key<\/b> and notice the <b>Run to Click (Run execution to here)<\/b> glyph changes into the <b>Set Next Statement<\/b> glyph.<\/p>\n<p>3. Click on that glyph and the yellow arrow will move to that line.<\/p>\n<p>4. This line will be the next statement to execute when taking a step or pressing Continue (F5).<\/p>\n<p><a target=\"_blank\" href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/4\/2019\/06\/SetNextStatement.gif\"><img decoding=\"async\" width=\"500\" height=\"284\" title=\"Set Next Statement\" alt=\"Set Next Statement\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/4\/2019\/06\/SetNextStatement.gif\" \/><\/a><\/p>\n<h2>2. Break when a value changes<\/h2>\n<p>Have you been in a situation while debugging where you inspect an object\u2019s property at one breakpoint and by the time you get to the next breakpoint that property has changed unexpectedly. You can set a breakpoint on the setter in the class, but this breaks for every instance of the object type! What if you only care about one problematic instance? When debugging C++ code, <a target=\"_blank\" href=\"https:\/\/blogs.msdn.microsoft.com\/devops\/2013\/10\/14\/data-breakpoints\/\">Data Breakpoints<\/a> can help you out. If you are debugging managed code, you can use <b>Make Object ID<\/b> plus a <b>Conditional Breakpoint<\/b> to narrow your search for the problem area.<\/p>\n<ol>\n<li>When you get to a breakpoint with the interesting instance right click on the object and select <b>Make Object ID<\/b>. This gives you a handle to that object in memory, referenced by<b> \u201c$1\u201d.<\/b><\/li>\n<li>Go to the setter of the property you care about and add a <a target=\"_blank\" href=\"https:\/\/devblogs.microsoft.com\/devops\/conditional-breakpoints\/\">condition to the breakpoint<\/a>,\n<b>\u201cthis == $1\u201d<\/b><\/li>\n<li>Press <b>Continue (F5)<\/b> and now you will break in the setter when that property changes for that instance.<\/li>\n<li>Look at the <b>Call Stack<\/b> and double click on the previous frame. This will take you to the line of code that is changing the property for this specific instance of the object.<\/li>\n<\/ol>\n<p><a target=\"_blank\" href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/4\/2019\/06\/BreakWhen.gif\"><img decoding=\"async\" width=\"500\" height=\"244\" title=\"Break when a value changes\" alt=\"Break when a value changes\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/4\/2019\/06\/BreakWhen.gif\" \/><\/a><\/p>\n<p><i>Note: The object ID refers to the object\u2019s address in memory and consequently will change with every new debug session. So, if you need to restart debugging, be sure to right click and re-create the object ID. The handle ($1) won\u2019t change, so you can leave your breakpoint as is between debug sessions.<\/i><\/p>\n<h2>3. Reattach to Process<\/h2>\n<p>This is a true time-saver introduced in Visual Stuido 2017 that many of you have yet to discover. It is extremely helpful when you are working on a project where you need to use \u201cAttach to Process\u201d but you find yourself consistently attaching to the same thing session after session.<\/p>\n<ol>\n<li>Start from the <b>Attach to Process<\/b> <b>dialog (Ctrl+Alt+P)<\/b> and select the process or processes that you want to debug and click <b>\u201cAttach\u201d<\/b>.<\/li>\n<li>When you terminate that debugging session go to the <b>Debug <\/b>menu on the toolbar.<\/li>\n<li>Click <b>Reattach to Process<\/b> or use shortcut key <b>(Shift +Alt+P)<\/b>.<\/li>\n<\/ol>\n<p><a target=\"_blank\" href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/4\/2019\/06\/reattach.gif\"><img decoding=\"async\" width=\"500\" height=\"913\" title=\"reattach to process\" alt=\"reattach to process\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/4\/2019\/06\/reattach.gif\" \/><\/a><\/p>\n<p>For more in-depth details about Reattach to Process <a target=\"_blank\" href=\"https:\/\/blogs.msdn.microsoft.com\/devops\/2017\/03\/07\/reattach-to-process-in-visual-studio-2017\/\">check out this blog post<\/a>.<\/p>\n<h2>4. Show Threads in Source<\/h2>\n<p>Debugging a multithreaded application is rarely easy, but when you can see in the editor what lines of code each thread in currently on, it gets a lot better.<\/p>\n<ol>\n<li>In the debugger toolbar, toggle the button <b>\u201cShow Threads in Source\u201d<\/b><\/li>\n<li>A glyph will appear in the breakpoint gutter next to each line of code where at least one thread is currently stopped.<\/li>\n<li>Hover over the thread marker icon to see the thread ids and names for all threads currently stopped on that line of code.<\/li>\n<li>Right click on the thread to see available actions you can perform like freezing and switching the active thread.<\/li>\n<\/ol>\n<p><a target=\"_blank\" href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/4\/2019\/06\/showThreads.gif\"><img decoding=\"async\" width=\"500\" height=\"461\" title=\"Show Threads in Source\" alt=\"Show Threads in Source\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/4\/2019\/06\/showThreads.gif\" \/><\/a><\/p>\n<p><i>Note: This functionality comes with some performance overhead and can feel like it slows down debugging. We recommend turning it off when you aren\u2019t actively using it. <\/i><\/p>\n<h2>5. Step through one single thread without jumping around<\/h2>\n<p>How often are you debugging multithreaded code, when you hit your first breakpoint, take a step, and then suddenly you are stopped with the yellow arrow on another thread? The unexpected behavior comes from the breakpoint still being set and consequently being hit. By default, the debugger will stop on a breakpoint any time it is hit. This means that when you take a step, all threads are allowed to run, and one of your running threads hit this breakpoint before the step completes on your current thread. Next time you get in this situation try this:<\/p>\n<ol>\n<li>Disable or delete the breakpoint that has been hit by the new thread the debugger switched to.<\/li>\n<li>Press <b>Continue (F5)<\/b><\/li>\n<li>Observe how your first initial step on that first thread completes and now is the active debugging context.<\/li>\n<li>Since your breakpoints are deleted or disabled, you can continue stepping on that single thread without interruption.<\/li>\n<\/ol>\n<p><a target=\"_blank\" href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/4\/2019\/06\/StepOverThread.gif\"><img decoding=\"async\" width=\"500\" height=\"216\" title=\"Step through one single thread without jumping around\" alt=\"Step through one single thread without jumping around\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/4\/2019\/06\/StepOverThread.gif\" \/><\/a><\/p>\n<h2>6. Debug.ListCallStacks -allThreads<\/h2>\n<p>When there are lots of threads, there can be lots of call stacks to figure out. You may need to inspect all of them to get a good picture of what state your application is in. You can always see a visual representation of the call stacks for each thread by using the <b>Parallel Stacks window (Debug\/Windows\/ Parallel Stacks)<\/b>. You can also see a text based, copy\/paste-able version of the call stack for each thread using the <b>Command window<\/b>.<\/p>\n<ol>\n<li>Open the <b>Command Window (View\/Other Windows\/Command Window).<\/b><\/li>\n<li>Type \u201c<b>Debug.ListCallStacks \u2013 allThreads<\/b>\u201d<\/li>\n<li>You can also use the popular WinDBG command \u201c<b>~*k<\/b>\u201d<\/li>\n<li>See how each thread is listed with its call stack displayed in the window.<\/li>\n<\/ol>\n<p><a target=\"_blank\" href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/4\/2019\/06\/ListCallStacks.gif\"><img decoding=\"async\" width=\"500\" height=\"201\" title=\"List Call Stacks\" alt=\"List Call Stacks\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/4\/2019\/06\/ListCallStacks.gif\" \/><\/a><\/p>\n<h2>7. Side Effect Free Function Evaluation \u201c, nse\u201d<\/h2>\n<p>Have you ever innocently typed an expression into the Watch window or Immediate window<b>,<\/b> then had to deal with the side effects of a debug session where you changed the state of the application without meaning to? Often this can happen when trying to evaluate an expression that calls a function in your program and it causes side effects (state changes to the program without running the actual application). While this may be okay if you know what functions will be called, what if you\u2019re not sure? Here is a way to evaluate expressions in C# without the risk of side effects corrupting your program.<\/p>\n<ol>\n<li>You can add \u201c<b>, nse<\/b>\u201d (stands for \u201cNo Side Effects\u201d) after any expression you type into the Watch window or Immediate window.<\/li>\n<li>This will use a sandbox of sorts that will interpret the expression without causing any side effects.<\/li>\n<li>If the expression can\u2019t be interpreted and can only be resolved by an evaluation, it will show you an error in the window.<\/li>\n<li>If you are sure that you want to evaluate it anyway, remove the \u201c, nse\u201d modifier and try again.<\/li>\n<\/ol>\n<p><a target=\"_blank\" href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/4\/2019\/06\/NSE.gif\"><img decoding=\"async\" width=\"500\" height=\"190\" title=\"NSE - No Side Effects\" alt=\"NSE - No Side Effects\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/4\/2019\/06\/NSE.gif\" \/><\/a><\/p>\n<h3>Learned Something? Let us know!<\/h3>\n<p>What is your favorite lesser known debugging feature? Comment below!<\/p>\n<table cellspacing=\"0\" cellpadding=\"2\" width=\"600\" border=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"150\"><img decoding=\"async\" width=\"200\" height=\"149\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/4\/2019\/06\/KayceeAnderson-e1498490195607.png\" \/><\/td>\n<td valign=\"top\" width=\"450\"><strong>Kaycee Anderson<\/strong>, Program manager, Visual Studio Debugger, and Diagnostics\n<a target=\"_blank\" href=\"https:\/\/twitter.com\/KayceeSue\">@KayceeSue<\/a><\/p>\n<p>Kaycee is a program manager working on the Visual Studio Debugger. She is passionate about delighting developers with core debugging experiences to help them find and fix issues in their code faster. She wants to help you #SuperChargeYourDebugging.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>The Visual Studio debugger is a magical beast that can save you loads of time while finding and fixing issues in your application. It is chock-full of tools that can make debugging easier\u2026 if you know they exist, and where to find them! Let\u2019s look at 7 lesser known goodies you can use to help [&hellip;]<\/p>\n","protected":false},"author":13,"featured_media":255385,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[155],"tags":[237,1383,9,287,156],"class_list":["post-12645","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-visual-studio","tag-net","tag-c","tag-debug","tag-tips-and-tricks","tag-visual-studio-2017"],"acf":[],"blog_post_summary":"<p>The Visual Studio debugger is a magical beast that can save you loads of time while finding and fixing issues in your application. It is chock-full of tools that can make debugging easier\u2026 if you know they exist, and where to find them! Let\u2019s look at 7 lesser known goodies you can use to help [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts\/12645","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/comments?post=12645"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts\/12645\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/media\/255385"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/media?parent=12645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/categories?post=12645"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/tags?post=12645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}