{"id":63,"date":"2015-01-16T15:00:00","date_gmt":"2015-01-16T03:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/visualstudioalm\/2015\/01\/16\/walkthrough-of-live-debugging-using-intellitrace-in-visual-studio-2015-text-editor\/"},"modified":"2022-05-25T01:18:24","modified_gmt":"2022-05-25T09:18:24","slug":"walkthrough-of-live-debugging-using-intellitrace-in-visual-studio-2015-text-editor","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/devops\/walkthrough-of-live-debugging-using-intellitrace-in-visual-studio-2015-text-editor\/","title":{"rendered":"Walkthrough of Live Debugging using IntelliTrace in Visual Studio 2015 (Text Editor)"},"content":{"rendered":"<p>In this blog post I am going to walk you through fixing a bug using IntelliTrace in Visual Studio 2015. If you haven\u2019t done so already, check out the <a href=\"http:\/\/blogs.msdn.com\/b\/visualstudioalm\/archive\/2015\/01\/16\/intellitrace-in-visual-studio-ultimate-2015.aspx\">announcement of IntelliTrace in Visual Studio 2015<\/a> which gives you an overview of IntelliTrace and its UI. You can see this walkthrough as a <a href=\"https:\/\/channel9.msdn.com\/Shows\/Visual-Studio-Toolbox\/Visual-Studio-2015-Diagnostic-Tools\">video here<\/a> (demo start at 4m5s).<\/p>\n<p>The application I am going to be working with is a simple text editor built using WPF (I have <a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/6\/2019\/02\/TextEditor.zip\">included the source code<\/a> in case you want to follow along):<\/p>\n<p><img decoding=\"async\" style=\"float: none;margin-left: auto;margin-right: auto;border-width: 0px\" title=\"clip_image001\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/0160.clip_image001_thumb_1CCC327B.png\" alt=\"clip_image001\" width=\"529\" height=\"354\" border=\"0\" \/><\/p>\n<p>The bug I want to fix has to do with saving the file after it has been edited, so let\u2019s open a file first:<\/p>\n<p><img decoding=\"async\" style=\"float: none;margin-left: auto;margin-right: auto;border-width: 0px\" title=\"clip_image002\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/6404.clip_image002_thumb_13D64748.png\" alt=\"clip_image002\" width=\"529\" height=\"354\" border=\"0\" \/><\/p>\n<p>The contents of the file are not important. The bug I want to fix is that if I make any changes and I click on the \u201cSave\u201d button, the scrollbar will move to the top and so will my cursor:<\/p>\n<p><img decoding=\"async\" style=\"float: none;margin-left: auto;margin-right: auto;border-width: 0px\" title=\"clip_image003\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/3348.clip_image003_thumb_285B96C6.png\" alt=\"clip_image003\" width=\"529\" height=\"354\" border=\"0\" \/><\/p>\n<p><img decoding=\"async\" style=\"float: none;margin-left: auto;margin-right: auto;border-width: 0px\" title=\"clip_image004\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/1033.clip_image004_thumb_2F0EA049.png\" alt=\"clip_image004\" width=\"529\" height=\"354\" border=\"0\" \/><\/p>\n<p>Let\u2019s start by trying to fix this bug using just the debugger with IntelliTrace turned off. Where would you start? What would be your next move if you were asked to find the root cause of this issue? When I ask this question, most people shout out \u201cthe event handler\u201d, meaning that they would start by putting a breakpoint at the first line of code in the event handler of the \u201cSave\u201d button.<\/p>\n<p>So let\u2019s go ahead and set that breakpoint, scroll the editor to the bottom and make another change:<\/p>\n<p><img decoding=\"async\" style=\"float: none;margin-left: auto;margin-right: auto;border-width: 0px\" title=\"clip_image005\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/3660.clip_image005_thumb_27EF63D1.png\" alt=\"clip_image005\" width=\"529\" height=\"354\" border=\"0\" \/><\/p>\n<p>I click the \u201cSave\u201d button and the debugger breaks at my breakpoint:<\/p>\n<p><img decoding=\"async\" style=\"float: none;margin-left: auto;margin-right: auto;border-width: 0px\" title=\"clip_image006\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/3683.clip_image006_thumb_4EBD7A11.png\" alt=\"clip_image006\" width=\"607\" height=\"346\" border=\"0\" \/><\/p>\n<p>None of the event handler\u2019s code has executed yet. Before I step through this code I want to make sure there is nothing obvious that stands out. From experience, many will point out that this.txtFileContents.Focus() looks suspicious, so I\u2019m going to comment that part out (using <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/x17d7wxw.aspx\">Edit &amp; Continue<\/a>) and continue stepping:<\/p>\n<p><img decoding=\"async\" style=\"float: none;margin-left: auto;margin-right: auto;border-width: 0px\" title=\"clip_image007\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/4762.clip_image007_thumb_0A7D12C5.png\" alt=\"clip_image007\" width=\"604\" height=\"340\" border=\"0\" \/><\/p>\n<p>There were no exceptions thrown and nothing else looks suspicious, so I\u2019m going to press F5 to continue running the app and see if the bug is fixed:<\/p>\n<p><img decoding=\"async\" style=\"float: none;margin-left: auto;margin-right: auto;border-width: 0px\" title=\"clip_image008\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/0574.clip_image008_thumb_4A46F94A.png\" alt=\"clip_image008\" width=\"529\" height=\"354\" border=\"0\" \/><\/p>\n<p>Nope, the editor still scrolls to the top and the bug is still there. Unfortunately I was wrong about the method call I commented out and nothing else stands out. There were also no exceptions thrown, so I no longer think the root cause of this bug is in the event handler of the Save button.<\/p>\n<p>What next? Where would you look now? When I ask this question, I usually have to wait a little bit, but eventually people start offering clever suggestions such as:<\/p>\n<ul>\n<li>Do a search for \u201ctxtFileContents\u201d and see if there is any code related to it that looks suspicious<\/li>\n<li>Figure out what the class scope variable \u201cthis.hasPendingChanges\u201d does<\/li>\n<\/ul>\n<p>The theme to recognize here is that we are not exactly sure what to do next. We looked at the obvious place but we didn\u2019t find anything, and now we are just guessing. Next we need to read the code and understand it a bit better before putting another breakpoint somewhere and reproduce the bug again.<\/p>\n<p>Instead, let\u2019s see how IntelliTrace can help. I start by stopping the current debugging session, undoing my changes to the code and removing the breakpoint:<\/p>\n<p><img decoding=\"async\" style=\"float: none;margin-left: auto;margin-right: auto;border-width: 0px\" title=\"clip_image009\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/5857.clip_image009_thumb_30DEF610.png\" alt=\"clip_image009\" width=\"607\" height=\"352\" border=\"0\" \/><\/p>\n<p>Then I make sure IntelliTrace is enabled by going to <strong>Tools > Options > IntelliTrace<\/strong> (it is enabled by default):<\/p>\n<p><img decoding=\"async\" style=\"float: none;margin-left: auto;margin-right: auto;border-width: 0px\" title=\"image\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/6708.image_thumb_0648EDA4.png\" alt=\"image\" width=\"644\" height=\"376\" border=\"0\" \/><\/p>\n<p>Before I press F5 again, I make sure the events I am interested in are being collected by IntelliTrace. I turn on the File and Gesture events because the bug occurs during saving a file after clicking on a button:<\/p>\n<p><img decoding=\"async\" style=\"float: none;margin-left: auto;margin-right: auto;border-width: 0px\" title=\"clip_image012\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/6740.clip_image012_thumb_0C4D688C.png\" alt=\"clip_image012\" width=\"628\" height=\"368\" border=\"0\" \/><\/p>\n<p>I don\u2019t mind some of the defaults being \u201con\u201d, such as ADO.NET and ASP.NET, because if the events don\u2019t happen they are not collected. I now press F5 again and make another change:<\/p>\n<p><img decoding=\"async\" style=\"float: none;margin-left: auto;margin-right: auto;border-width: 0px\" title=\"clip_image013\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/2148.clip_image013_thumb_052E2C14.png\" alt=\"clip_image013\" width=\"529\" height=\"354\" border=\"0\" \/><\/p>\n<p>I click \u201cSave\u201d again:<\/p>\n<p><img decoding=\"async\" style=\"float: none;margin-left: auto;margin-right: auto;border-width: 0px\" title=\"clip_image014\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/3618.clip_image014_thumb_1300720F.png\" alt=\"clip_image014\" width=\"529\" height=\"354\" border=\"0\" \/><\/p>\n<p>\u2026 and now that I have reproduced the issue I want to see what IntelliTrace has collected. The data only becomes visible when the debugger breaks execution, so I will go ahead and hit the \u201cBreak All\u201d button which will <strong>Break<\/strong> execution on <strong>All<\/strong> threads and allow me to inspect the application\u2019s state with the debugger:<\/p>\n<p><img decoding=\"async\" style=\"float: none;margin-left: auto;margin-right: auto;border-width: 0px\" title=\"clip_image015\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/5707.clip_image015_thumb_67BBDB07.png\" alt=\"clip_image015\" width=\"230\" height=\"93\" border=\"0\" \/><\/p>\n<p>As soon as I do that, the Diagnostic Tools window (which has automatically opened and docked next to our code) is updated with all of the data collected by IntelliTrace:<\/p>\n<p style=\"text-align: center\">\n  \u00a0\n<\/p>\n<p>I go through the list of events to find something I am familiar with so I can orient myself and I find the event that was me clicking the \u201cOpen\u201d button to open the text file:<\/p>\n<p style=\"text-align: center\">\n  <a href=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/2161._blahblah2.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/2161._blahblah2.png\" alt=\"\" border=\"0\" \/><\/a>\n<\/p>\n<p>Following it I see a File Access and File Close event, which makes sense because we are opening the file to read it and once we are done we close it again:<\/p>\n<p style=\"text-align: center\">\n  <a href=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/8176._blahblah3.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/8176._blahblah3.png\" alt=\"\" border=\"0\" \/><\/a>\n<\/p>\n<p>So far so good. Next I see the event from clicking the \u201cSave\u201d button, which I did after making my changes:<\/p>\n<p style=\"text-align: center\">\n  <a href=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/0361._blahblah4.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/0361._blahblah4.png\" alt=\"\" border=\"0\" \/><\/a>\n<\/p>\n<p>This is where it gets interesting. Following this event I see something that I did not quite expect, I see two sets of File Access and File Close events:<\/p>\n<p style=\"text-align: center\">\n  <a href=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/7848._blahblah5.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/7848._blahblah5.png\" alt=\"\" border=\"0\" \/><\/a>\n<\/p>\n<p>What IntelliTrace is telling us is that the file gets accessed twice after it is saved. This is strange and unexpected and likely to be the cause of our bug, but even if not we should investigate this unexpected behavior anyway. So without doing anything more than simply looking at the data collected by IntelliTrace, I already have a better idea of where the bug might be. What I want next is for IntelliTrace to tell me how the two sets of events are different and why they happened.<\/p>\n<p>To do this, I first select the File Access event (by clicking on it) and then I click on \u201cActivate Historical Debugging\u201d:<\/p>\n<p style=\"text-align: center\">\n  <a href=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/7633._blahblah6.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/7633._blahblah6.png\" alt=\"\" border=\"0\" \/><\/a>\n<\/p>\n<p>As a result, the code editor shows me the line of code responsible for generating this event and the debugger\u2019s time context is set back-in-time to the moment when the event was generated:<\/p>\n<p style=\"text-align: center\">\n  <a href=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/5023._blahblah7.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/5023._blahblah7.png\" alt=\"\" border=\"0\" \/><\/a>\n<\/p>\n<p>By looking at the editor I can tell that the event I activated Historical Debugging for is coming from the event handler of the \u201cSave\u201d button, which is what I expected. Similarly, the subsequent File Close event is the end of the using statement which closes the file (I haven\u2019t shown this here). Next I will look at the second File Access event after the \u201cSave\u201d button is clicked by activating Historical Debugging for that one:<\/p>\n<p style=\"text-align: center\">\n  <a href=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/0081._blahblah8.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/0081._blahblah8.png\" alt=\"\" border=\"0\" \/><\/a>\n<\/p>\n<p>This time what I see is not what I expected:<\/p>\n<p style=\"text-align: center\">\n  <a href=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/7142._blahblah9.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/7142._blahblah9.png\" alt=\"\" border=\"0\" \/><\/a>\n<\/p>\n<p>Apparently the second File Access event is coming from the ReadFile method, a method I never expected to get called when saving. I don\u2019t understand why this code is getting executed, so I will use the historical Call Stack to find out:<\/p>\n<p><img decoding=\"async\" style=\"float: none;margin-left: auto;margin-right: auto;border-width: 0px\" title=\"clip_image028\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/4274.clip_image028_thumb_64A97D12.png\" alt=\"clip_image028\" width=\"582\" height=\"177\" border=\"0\" \/><\/p>\n<p>Looking at the Call Stack, I can tell that the ReadFile method is called when a change is detected in the file, judging by the name of the method \u201cOnFileChanged\u201d. I don\u2019t really know why or when this gets called, so I double click on the frame with \u201cAnonymousMethod()\u201d in the Call Stack to take a look at the calling code:<\/p>\n<p><img decoding=\"async\" style=\"float: none;margin-left: auto;margin-right: auto;border-width: 0px\" title=\"clip_image029\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/4744.clip_image029_thumb_727BC30D.png\" alt=\"clip_image029\" width=\"608\" height=\"180\" border=\"0\" \/><\/p>\n<p>It looks like the application monitors the opened file for any changes made by other users and warns you to not overwrite their changes. Here is what this feature looks like in action:<\/p>\n<p><img decoding=\"async\" style=\"float: none;margin-left: auto;margin-right: auto;border-width: 0px\" title=\"clip_image030\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/3187.clip_image030_thumb_045856DB.png\" alt=\"clip_image030\" width=\"528\" height=\"355\" border=\"0\" \/><\/p>\n<p>Part of this feature is if there are no changes to the file and someone else edits it, the file is reloaded automatically so I have the most up to date copy. However, there is a bug in the \u201cOnFileChanged()\u201d method. It is supposed to ignore changes coming from myself, but instead the file is getting automatically reloaded in error:<\/p>\n<p><img decoding=\"async\" style=\"float: none;margin-left: auto;margin-right: auto;border-width: 0px\" title=\"clip_image031\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/5165.clip_image031_thumb_2B266D1B.png\" alt=\"clip_image031\" width=\"595\" height=\"300\" border=\"0\" \/><\/p>\n<h3>Wrapping up<\/h3>\n<p>By walking through this bug fix we have been reminded that even bugs that appear simple on the surface can have their root cause hiding in unexpected places. IntelliTrace saved us valuable time during debugging by allowing us to \u201ctravel in the past\u201d and examine the state of the application at interesting moments, avoiding wasteful iterations of setting breakpoints and repeating testing steps.<\/p>\n<p>We are always looking for feedback and comments for our features. You can leave general comments &amp; questions at the end of this blog post or via <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/zzszcehe.aspx\">Send-a-Smile<\/a>, and submit feature requests to our <a href=\"http:\/\/visualstudio.uservoice.com\/forums\/121579-visual-studio\/category\/91797-intellitrace\">IntelliTrace UserVoice<\/a>. You can also <a href=\"https:\/\/twitter.com\/IntelliTrace\">send us a tweet<\/a> or visit the <a href=\"http:\/\/social.msdn.microsoft.com\/Forums\/vstudio\/en-US\/home?forum=vsdebug\">MSDN Diagnostics forums<\/a>.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/6\/2019\/02\/TextEditor.zip\">TextEditor.zip<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog post I am going to walk you through fixing a bug using IntelliTrace in Visual Studio 2015. If you haven\u2019t done so already, check out the announcement of IntelliTrace in Visual Studio 2015 which gives you an overview of IntelliTrace and its UI. You can see this walkthrough as a video here [&hellip;]<\/p>\n","protected":false},"author":127,"featured_media":45953,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1,225],"tags":[],"class_list":["post-63","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops","category-git"],"acf":[],"blog_post_summary":"<p>In this blog post I am going to walk you through fixing a bug using IntelliTrace in Visual Studio 2015. If you haven\u2019t done so already, check out the announcement of IntelliTrace in Visual Studio 2015 which gives you an overview of IntelliTrace and its UI. You can see this walkthrough as a video here [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/63","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/users\/127"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/comments?post=63"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/63\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media\/45953"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media?parent=63"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/categories?post=63"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/tags?post=63"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}