{"id":2443,"date":"2013-10-09T14:00:00","date_gmt":"2013-10-09T14:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/visualstudioalm\/2013\/10\/09\/filter-breakpoints\/"},"modified":"2022-07-19T00:03:55","modified_gmt":"2022-07-19T08:03:55","slug":"filter-breakpoints","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/devops\/filter-breakpoints\/","title":{"rendered":"Filter Breakpoints"},"content":{"rendered":"<p><span style=\"font-family: arial,helvetica,sans-serif;font-size: small\">This blog post is part of a series on <a href=\"http:\/\/blogs.msdn.com\/b\/visualstudioalm\/archive\/2013\/10\/07\/breakpoints-in-visual-studio-2013.aspx\">breakpoints<\/a> in the Visual Studio <\/span><span style=\"font-family: arial,helvetica,sans-serif;font-size: small\">debugger and has been updated to reflect the experience of using Visual Studio 2015<\/span><span style=\"font-family: arial,helvetica,sans-serif;font-size: small\">. If you are interested in details about setting conditional breakpoints <\/span><span style=\"font-family: arial,helvetica,sans-serif;font-size: small\">using earlier versions of Visual Studio please see the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/vstudio\/wyakk529(v=vs.100).aspx\">MSDN documentation<\/a>.\u00a0 The code used in <\/span><span style=\"font-family: arial,helvetica,sans-serif;font-size: small\">the examples below is <a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/6\/2019\/02\/ManagedBreakpointSamples.zip\">available to download<\/a>.<\/span><\/p>\n<p><span style=\"font-family: arial,helvetica,sans-serif;font-size: small\">Filter breakpoints can be extremely helpful <\/span><span style=\"font-family: arial,helvetica,sans-serif;font-size: small\">when your application is running the same method many times in parallel. <\/span><span style=\"font-family: arial,helvetica,sans-serif;font-size: small\">Consider the example where we are calling FindPrime() 4 times in parallel. <\/span><span style=\"font-family: arial,helvetica,sans-serif;font-size: small\">You\u2019ll notice that FindPrime() calls IsPrime() several times. To illustrate <\/span><span style=\"font-family: arial,helvetica,sans-serif;font-size: small\">this problem, let\u2019s see what happens when we set a conventional breakpoint in <\/span><span style=\"font-family: arial,helvetica,sans-serif;font-size: small\">IsPrime(). At first we hit the breakpoint in this function as expected.<\/span><\/p>\n<p><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/4578.1.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/4578.1.png\" alt=\"\" border=\"0\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: arial,helvetica,sans-serif;font-size: small\">But that as we start to step through the function, we notice some strange behavior.<\/span><\/p>\n<p><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/5417.2.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/5417.2.png\" alt=\"\" border=\"0\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/3755.3.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/3755.3.png\" alt=\"\" border=\"0\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/4338.4.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/4338.4.png\" alt=\"\" border=\"0\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/8712.5.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/8712.5.png\" alt=\"\" border=\"0\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/7534.6.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/7534.6.png\" alt=\"\" border=\"0\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/4265.7.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/4265.7.png\" alt=\"\" border=\"0\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: arial,helvetica,sans-serif\">We seem to just be jumping around. This is because different threads are hitting the same breakpoint while you are stepping. Then the debugger is also stopping when each step is completing. The order of these events is not deterministic so it will likely be different the next time you debug.<\/span><\/p>\n<p>Using a filter breakpoint, you can instead just focus on a single thread. To create the filter breakpoint, hover over the breakpoint to bring up the breakpoint\u2019s toolbar and click the \u201cSettings\u2026\u201d icon.<\/p>\n<p><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/4478.8.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/4478.8.png\" alt=\"\" border=\"0\" \/><\/a><\/span><\/p>\n<p>\u00a0You can also right-click on the breakpoint to bring up the context menu, and select \u201cConditions\u2026\u201d or use the keyboard shortcut Alt+F9, C<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/4846.Conditions20Actions20Menu.png\" alt=\"\" border=\"0\" \/><\/p>\n<p>This will bring up the Breakpoint Settings window. Now check the box to add \u201cConditions\u201d and change the type of condition in the first dropdown from \u201cConditional Expression\u201d to \u201cFilter\u201d. \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\/2352.Filter20Menu.png\" alt=\"\" border=\"0\" \/><\/p>\n<p>You\u2019ll notice that there are 5 different ways to filter the breakpoint: MachineName, ProcessId, ProcessName, ThreadId, and ThreadName.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/6710.Filter20Menu20dropdown.png\" alt=\"\" border=\"0\" \/><\/p>\n<p>Since we want to focus on a single thread that is running through IsPrime(), we will filter on ThreadId. If you had code running in parallel across different processes or machines, you could use this same technique using those filters as well.<\/p>\n<p>First we must figure out which thread we want to stop on. To do this, we will set a conventional breakpoint on FindPrime(). When we stop at this breakpoint, we can use the Threads window to see the ID of our thread is 6036.<\/p>\n<p><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/3312.12.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/3312.12.png\" alt=\"\" border=\"0\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: arial,helvetica,sans-serif\">Now we can go back to our breakpoint on IsPrime and setup the filter.<\/span><\/p>\n<p><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/3568.Threadwindowpng.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/3568.Threadwindowpng.png\" alt=\"\" border=\"0\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: arial,helvetica,sans-serif\">And then we can see that stepping through the function behaves as we would expect.<\/span><\/p>\n<p><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/3060.14.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/3060.14.png\" alt=\"\" border=\"0\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/1256.15.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/1256.15.png\" alt=\"\" border=\"0\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/5270.17.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/5270.17.png\" alt=\"\" border=\"0\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/2251.18.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/2251.18.png\" alt=\"\" border=\"0\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: arial,helvetica,sans-serif\">One disadvantage to this approach is that you need to find the Thread ID each time you run the application. To avoid this, you can instead name your threads.<\/span><\/p>\n<p><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/6735.19.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/6735.19.png\" alt=\"\" border=\"0\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: arial,helvetica,sans-serif\">And then apply the filter to the name instead as this will not change between debug sessions.<\/span><\/p>\n<p><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/7457.ThreadName.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/10\/7457.ThreadName.png\" alt=\"\" border=\"0\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: arial,helvetica,sans-serif\">The caveat here is that this may not work if you are using tasks or thread pool threads as threads can only be named once.<\/span><\/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 conditional breakpoints using earlier versions of Visual Studio please see the MSDN documentation.\u00a0 The code used in the examples [&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-2443","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 conditional breakpoints using earlier versions of Visual Studio please see the MSDN documentation.\u00a0 The code used in the examples [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/2443","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=2443"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/2443\/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=2443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/categories?post=2443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/tags?post=2443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}