{"id":2463,"date":"2013-10-07T14:10:00","date_gmt":"2013-10-07T14:10:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/visualstudioalm\/2013\/10\/07\/hit-count-breakpoints\/"},"modified":"2022-07-19T00:18:54","modified_gmt":"2022-07-19T08:18:54","slug":"hit-count-breakpoints","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/devops\/hit-count-breakpoints\/","title":{"rendered":"Hit Count Breakpoints"},"content":{"rendered":"<p><span style=\"font-family: arial,helvetica,sans-serif\"><span style=\"color: #424242;line-height: 107%;font-size: 9.5pt\" lang=\"EN\">This blog post is part of a series on <\/span><span style=\"line-height: 107%;font-size: 11pt\"><a href=\"http:\/\/blogs.msdn.com\/b\/visualstudioalm\/archive\/2013\/10\/07\/breakpoints-in-visual-studio-2013.aspx\"><span style=\"line-height: 107%;font-size: 9.5pt\" lang=\"EN\">breakpoints<\/span><\/a><\/span><span style=\"color: #424242;line-height: 107%;font-size: 9.5pt\" lang=\"EN\"> in the Visual Studio debugger and has been updated to reflect the experience of using Visual Studio 2015. If you are interested in details about setting hit count breakpoints using earlier versions of Visual Studio please see the <\/span><span style=\"line-height: 107%;font-size: 11pt\"><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/vstudio\/yy96wbwd(v=vs.100).aspx\"><span style=\"line-height: 107%;font-size: 9.5pt\" lang=\"EN\">MSDN documentation<\/span><\/a><\/span><span style=\"color: #424242;line-height: 107%;font-size: 9.5pt\" lang=\"EN\">.<\/span><\/span><\/p>\n<p><span style=\"font-family: arial,helvetica,sans-serif\"><span style=\"color: #424242;line-height: 107%;font-size: 9.5pt\" lang=\"EN\">As mentioned in <\/span><span style=\"line-height: 107%;font-size: 11pt\"><a href=\"http:\/\/blogs.msdn.com\/b\/visualstudioalm\/archive\/2013\/10\/07\/breakpoints-in-visual-studio-2013.aspx\"><span style=\"color: #00749e;line-height: 107%;font-size: 9.5pt;text-decoration: none\" lang=\"EN\">an earlier post<\/span><\/a><\/span><span style=\"color: #424242;line-height: 107%;font-size: 9.5pt\" lang=\"EN\">, we will be writing about all of the different breakpoint functionality available in Visual Studio.\u00a0In this first part, we discuss the hit count breakpoint.\u00a0 If you wish to follow along, the code samples below are <\/span><span style=\"line-height: 107%;font-size: 11pt\"><a title=\"available for download\" href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/6\/2019\/02\/ManagedBreakpointSamples.zip\"><span style=\"color: #00749e;line-height: 107%;font-size: 9.5pt;text-decoration: none\" lang=\"EN\">available for download<\/span><\/a><\/span><span style=\"color: #424242;line-height: 107%;font-size: 9.5pt\" lang=\"EN\">.<\/span><\/span><\/p>\n<h2>Sample Problem<\/h2>\n<p><span style=\"color: #424242;line-height: 107%;font-family: arial,helvetica,sans-serif;font-size: 9.5pt\" lang=\"EN\"><span style=\"color: #424242;line-height: 107%;font-size: 9.5pt\" lang=\"EN\">Consider the following method that applies a basic shift cipher to a string. A <\/span><span style=\"line-height: 107%;font-size: 11pt\"><a href=\"http:\/\/en.wikipedia.org\/wiki\/Caesar_cipher\"><span style=\"color: #00749e;line-height: 107%;font-size: 9.5pt;text-decoration: none\" lang=\"EN\">shift cipher (or Caesar cipher)<\/span><\/a><\/span><span style=\"color: #424242;line-height: 107%;font-size: 9.5pt\" lang=\"EN\"> simply shift the letters of a message by a fixed amount.<\/span><\/span><\/p>\n<p>\u00a0<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/3632.1.png\" alt=\"\" border=\"0\" \/><\/p>\n<p><span style=\"font-family: arial,helvetica,sans-serif\">We have a breakpoint set on the return <\/span><span style=\"font-family: arial,helvetica,sans-serif\">statement of the method. Now let\u2019s see what happens when we run the following <\/span><span style=\"font-family: arial,helvetica,sans-serif\">code:<\/span><\/p>\n<p>\u00a0<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/6567.2.png\" alt=\"\" border=\"0\" \/><\/p>\n<p>When we are stopped at the breakpoint, we inspect shiftArray and see that we have a problem.<\/p>\n<p><img decoding=\"async\" style=\"border: 0px\" title=\"clip_image003\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/8030.clip_image003_thumb_3C84409E.png\" alt=\"clip_image003\" width=\"395\" height=\"638\" border=\"0\" \/><\/p>\n<p><span style=\"color: #424242;line-height: 107%;font-family: 'Segoe UI','sans-serif';font-size: 9.5pt\" lang=\"EN\">While the rest of the encoded message does not make it obvious that there is a problem, we can see that we have \u2018{\u2018 in the 35<sup>th <\/sup>position, which is obviously wrong. We suspect that the problem is in the loop that is encoding each letter, but we really want to examine the 36<sup>th <\/sup>iteration of the loop. If we just set a breakpoint in the loop, we would have to sit there and continue it 35 times before we can really get down to debugging. This is where using hit count breakpoints comes in handy. <\/span>Creating the Hit Count Breakpoint. To create the hit count breakpoint, first start by simply creating a regular breakpoint in the loop you want to examine.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/5340.3.png\" alt=\"\" border=\"0\" \/><\/p>\n<p>Then hover over the breakpoint to bring up the breakpoint\u2019s toolbar and click the \u201cSettings\u2026\u201d icon.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/6371.4.png\" alt=\"\" border=\"0\" \/><\/p>\n<p>You can also right click on the breakpoint to bring up the context menu and click on \u201cConditions\u2026\u201d, or press Alt+F9, C to use the keyboard shortcut.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/7103.Conditions20Actions20Menu.png\" alt=\"\" border=\"0\" \/><\/p>\n<p>This will bring up the Breakpoint Settings peek window.\u00a0 (Tip: Using the context menu or shortcut key will open the window with the Conditions box already checked.)<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/7127.Breakpoint20peek20plain20with20Close20button.png\" alt=\"\" border=\"0\" \/><\/p>\n<p><span style=\"color: #424242;line-height: 107%;font-family: 'Segoe UI','sans-serif';font-size: 9.5pt\" lang=\"EN\">Select\u00a0\u201cConditions\u201d\u00a0 by checking the box and change the type of condition in the first dropdown from \u201cConditional Expression\u201d to \u201cHit Count\u201d. <\/span><\/p>\n<p><span style=\"color: #424242;line-height: 107%;font-family: 'Segoe UI','sans-serif';font-size: 9.5pt\" lang=\"EN\">Then enter 36 as the hit value and press the \u201cEnter\u201d key. Notice that after you enter a value, the condition is saved automatically when you hit \u201cEnter\u201d or click outside of the edit space.<\/span><\/p>\n<p><span style=\"color: #424242;line-height: 107%;font-family: 'Segoe UI','sans-serif';font-size: 9.5pt\" lang=\"EN\"><a href=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/8004.Breakpoint20peek20HitCount20edit.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/8004.Breakpoint20peek20HitCount20edit.png\" alt=\"\" border=\"0\" \/><\/a><\/span><\/p>\n<p><span style=\"color: #424242;line-height: 107%;font-family: 'Segoe UI','sans-serif';font-size: 9.5pt\" lang=\"EN\"><span style=\"color: #424242;line-height: 107%;font-family: 'Segoe UI','sans-serif';font-size: 9.5pt\" lang=\"EN\">You\u2019ll also notice that the breakpoint now has a \u2018+\u2019 inside indicating that this is an advanced breakpoint. <\/span><\/span><\/p>\n<p><span style=\"color: #424242;line-height: 107%;font-family: 'Segoe UI','sans-serif';font-size: 9.5pt\" lang=\"EN\"><span style=\"color: #424242;line-height: 107%;font-family: 'Segoe UI','sans-serif';font-size: 9.5pt\" lang=\"EN\"><a href=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/5141.Breakpoint20peek20HitCount20saved.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/5141.Breakpoint20peek20HitCount20saved.png\" alt=\"\" border=\"0\" \/><\/a><\/span><\/span><\/p>\n<p>Now when I start the application under the debugger, I stop right where the code is changing the value of shiftArray[35].<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/6332.10.png\" alt=\"\" border=\"0\" \/><\/p>\n<p>And as I step through, I can see the problem. First the letter is shifted off the end of the alphabet.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/4064.11.png\" alt=\"\" border=\"0\" \/><\/p>\n<p>But then when I step again, I can see that it skips the code that is supposed to push the letter back into the alphabet range.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/4331.12.png\" alt=\"\" border=\"0\" \/><\/p>\n<p><span style=\"color: #424242;line-height: 107%;font-family: 'Segoe UI','sans-serif';font-size: 9.5pt\" lang=\"EN\">Now I have found my problem. The condition should be shiftArray[i] >= 123 instead of shiftArray[i] > 123. And I found this more quickly by using a hit count breakpoint than I would otherwise.<\/span><\/p>\n<h2>Skipping Ahead<\/h2>\n<p>Hit Count breakpoints can also be used when you need to skip ahead in the execution of your code, but you don\u2019t quite know exactly how far ahead you want to go.<\/p>\n<p>Consider the following recursive function that calculated the greatest common divisor (GCD) of two numbers.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/2364.13.png\" alt=\"\" border=\"0\" \/><\/p>\n<p>\u00a0<\/p>\n<p>As a developer, you may want to debug through this function in order to see the intermediate values during the GCD calculation. Given two large numbers, you know that this function is likely to run for a long time, but you don\u2019t know quite how long. In this case you can use hit count breakpoints to save you some time.<\/p>\n<p>As before, you can bring up the Breakpoint Settings peek window by clicking the \u201cSettings\u2026\u201d icon on the breakpoint\u2019s toolbar, by using the keyboard shortcut Alt+F9, C, or by right-clicking on the breakpoint and selecting \u201cSettings\u2026\u201d This time still select Hit Count as the condition, but select the \u201c>=\u201d option and enter a value of 10.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/6165.Breakpoint20peek20HitCount20greaterequal.png\" alt=\"\" border=\"0\" \/><\/p>\n<p>This will allow you to skip through the first 10 calls to the function to get closer. Now consider calling the function with the following two integers,<\/p>\n<p><img decoding=\"async\" style=\"border: 0px\" title=\"clip_image014\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/4113.clip_image014_thumb_055AB2A5.png\" alt=\"clip_image014\" width=\"331\" height=\"29\" border=\"0\" \/><\/p>\n<p>Running to the breakpoint gets me down to small numbers for x and y so that I can follow the rest of the calculation.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/3302.15.png\" alt=\"\" border=\"0\" \/><\/p>\n<p>And if I continue the application, the application will continue to stop at the breakpoint each subsequent time it hits it.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/4331.16.png\" alt=\"\" border=\"0\" \/><\/p>\n<p>To view the current hit count, I can hover over the breakpoint and read the tooltip.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/1016.17.png\" alt=\"\" border=\"0\" \/><\/p>\n<p>If I open the Breakpoint Settings peek window, I can also view the current hit count for the breakpoint, and click the reset button to avoid stopping on the breakpoint for another 10 iterations.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/8015.Breakpoint20peek20HitCount20current.png\" alt=\"\" border=\"0\" \/><\/p>\n<h2>Counting Iterations<\/h2>\n<p>Finally, hit count breakpoints can also be used simply to count the number of times the breakpoint is hit. Let\u2019s say we wanted to count the number of iterations in our GCD example. As a before, you want to create a hit count breakpoint and select the option \u201c>=\u201d. This time, instead of choosing a number to break on, instead choose an arbitrarily large number like 99999.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/3632.Breakpoint20peek20HitCount20large.png\" alt=\"\" border=\"0\" \/><\/p>\n<p>With a number this large, the breakpoint will never be hit. Now set another breakpoint somewhere after all of the iterations of GCD will already be complete. When you stop at this breakpoint, you can see the hit count in the tooltip for the breakpoint, as well as the Breakpoint Settings peek window.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/8551.Breakpoint20peek20HitCount20large20current.png\" alt=\"\" border=\"0\" \/><\/p>\n<p>You can always see current hit count information for all of your breakpoints in the Breakpoints Window (available from the Debug -> Windows menu).<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/7838.22.png\" alt=\"\" border=\"0\" \/><\/p>\n<p>So you know that the program went through the GCD function 14 times.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This blog post is part of a series on breakpoints in the Visual Studio debugger and has been updated to reflect the experience of using Visual Studio 2015. If you are interested in details about setting hit count breakpoints using earlier versions of Visual Studio please see the MSDN documentation. As mentioned in an earlier [&hellip;]<\/p>\n","protected":false},"author":75,"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-2463","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops","category-git"],"acf":[],"blog_post_summary":"<p>This blog post is part of a series on breakpoints in the Visual Studio debugger and has been updated to reflect the experience of using Visual Studio 2015. If you are interested in details about setting hit count breakpoints using earlier versions of Visual Studio please see the MSDN documentation. As mentioned in an earlier [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/2463","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\/75"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/comments?post=2463"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/2463\/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=2463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/categories?post=2463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/tags?post=2463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}