{"id":9085,"date":"2017-05-02T14:00:00","date_gmt":"2017-05-02T14:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/premier_developer\/?p=9085"},"modified":"2019-02-21T10:56:19","modified_gmt":"2019-02-21T17:56:19","slug":"debugging-net-core-with-sos-everywhere","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/premier-developer\/debugging-net-core-with-sos-everywhere\/","title":{"rendered":"Debugging .NET core with SOS everywhere"},"content":{"rendered":"<p>Get some insight into debugging .NET code on non-Windows operating systems with this post from Premier Developer consultants <a href=\"https:\/\/www.linkedin.com\/in\/gustavovaro\/\" target=\"_blank\" rel=\"noopener\">Gustavo Varo<\/a> and <a href=\"https:\/\/www.linkedin.com\/in\/ben-williams-0310b73\/\" target=\"_blank\" rel=\"noopener\">Ben Williams<\/a>.<\/p>\n<hr \/>\n<p>I have been working more and more with .NET Core and one question I keep getting from our customers and partners is about the debugging experience when using operating systems other than Windows. It is not a secret to anyone that Windows has an incredible debugging experience, whether you like to use Visual Studio or you like to script your debugging tasks using WinDbg.\u00a0 But for everyone who is not using Windows, missing those tools is, in my opinion, one of the biggest sources of complaints when discussing the debugging experience.<\/p>\n<p>With the introduction of .NET Core, the community got really excited with the possibility of running .NET code either on Linux or Mac, but how could we try to improve the debugging experience on those platforms? Today we will talk a little bit on how we can do that on Linux using the SOS debug extension. That\u2019s correct, <strong>the SOS extension is now available on Linux as well.<\/strong><\/p>\n<h5>What is the SOS extension?<\/h5>\n<p>The SOS extension is a debugging extension shipped with Windows that allows developers to debug managed code within WinDbg, CDB or NTSD<\/p>\n<p>Remember that when you execute managed binaries, the runtime generates native code that is platform specific. The SOS extension allows you to debug that native code in a \u201cmanaged way\u201d.<\/p>\n<h5>How do I get started with SOS on Linux?<\/h5>\n<p>The very first step is to install a debugger. The debugger I will be using is called LLDB. LLDB is very similar to WinDbg and is also the debugger the SOS team has being using, so I have adopted it as my default debugger for Linux.<\/p>\n<p>You can install LLDB from the follow link:<\/p>\n<p><a href=\"https:\/\/lldb.llvm.org\/download.html\">https:\/\/lldb.llvm.org\/download.html<\/a><\/p>\n<p>or you can also install it by running the following command:<\/p>\n<p><code><strong>sudo apt install lldb-3.5<\/strong><\/code><\/p>\n<p>Once you have lldb installed in your Linux machine, open the Terminal, go to lldb\/Ubuntu\/bin and launch the debugger by typing the following command on the prompt:<\/p>\n<p><code>.\/lldb<\/code><\/p>\n<p>Make sure the debugger was launched properly.<\/p>\n<p>You will get the lldb prompt like the one below, if lldb was launched properly.<\/p>\n<p><img decoding=\"async\" width=\"48\" height=\"36\" class=\"wp-image-35311\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/05\/cidimage002-png01d2ac97-6b53d120.png\" alt=\"cid:image002.png@01D2AC97.6B53D120\" \/><\/p>\n<h5>Create a .NET Core application \u2026that crashes!<\/h5>\n<p>Now let\u2019s create and run our console app on Linux, for that create a new folder and on the Terminal prompt type:<\/p>\n<p><code>dotnet new <\/code>\n<code>dotnet restore<\/code><\/p>\n<p><code>dotnet run<\/code><\/p>\n<p>The result of this operation should be something like this:<\/p>\n<p><img decoding=\"async\" width=\"484\" height=\"276\" class=\"wp-image-35312\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/05\/cidimage003-jpg01d2ac97-6b53d120.jpeg\" alt=\"cid:image003.jpg@01D2AC97.6B53D120\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/05\/cidimage003-jpg01d2ac97-6b53d120.jpeg 484w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/05\/cidimage003-jpg01d2ac97-6b53d120-300x171.jpeg 300w\" sizes=\"(max-width: 484px) 100vw, 484px\" \/><\/p>\n<p>Let\u2019s make our app a little bit more interesting. Open the Program.cs file and change your file like that:<\/p>\n<p><img decoding=\"async\" width=\"338\" height=\"333\" class=\"wp-image-35313\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/05\/cidimage004-jpg01d2ac97-6b53d120.jpeg\" alt=\"cid:image004.jpg@01D2AC97.6B53D120\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/05\/cidimage004-jpg01d2ac97-6b53d120.jpeg 338w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/05\/cidimage004-jpg01d2ac97-6b53d120-300x296.jpeg 300w\" sizes=\"(max-width: 338px) 100vw, 338px\" \/><\/p>\n<p>Save the file and run again the app. Remember to press ENTER after you see the Hello World! message.<\/p>\n<p>You should see something like this:<\/p>\n<p><img decoding=\"async\" width=\"391\" height=\"232\" class=\"wp-image-35314\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/05\/cidimage005-jpg01d2ac97-6b53d120.jpeg\" alt=\"cid:image005.jpg@01D2AC97.6B53D120\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/05\/cidimage005-jpg01d2ac97-6b53d120.jpeg 391w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/05\/cidimage005-jpg01d2ac97-6b53d120-300x178.jpeg 300w\" sizes=\"(max-width: 391px) 100vw, 391px\" \/><\/p>\n<h5>Now it is time to debug it!<\/h5>\n<p>Run again the app, but at this time don\u2019t press ENTER after you see the Hello World! Message. Instead open a new Terminal window and get the process list in order to find the process id of the running app. You can do that by running the follow command on the prompt:<\/p>\n<p><code>ps -ejH<\/code><\/p>\n<p><img decoding=\"async\" width=\"361\" height=\"81\" class=\"wp-image-35315\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/05\/cidimage006-jpg01d2ac97-6b53d120.jpeg\" alt=\"cid:image006.jpg@01D2AC97.6B53D120\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/05\/cidimage006-jpg01d2ac97-6b53d120.jpeg 361w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/05\/cidimage006-jpg01d2ac97-6b53d120-300x67.jpeg 300w\" sizes=\"(max-width: 361px) 100vw, 361px\" \/><\/p>\n<p>Once you find it, start the debugger and attach the process by entering the following command on lldb:<\/p>\n<p><code>process attach -p [ProcessId]<\/code><\/p>\n<p><img decoding=\"async\" width=\"411\" height=\"396\" class=\"wp-image-35317\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/05\/cidimage007-jpg01d2ac97-6b53d120.jpeg\" alt=\"cid:image007.jpg@01D2AC97.6B53D120\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/05\/cidimage007-jpg01d2ac97-6b53d120.jpeg 411w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/05\/cidimage007-jpg01d2ac97-6b53d120-300x289.jpeg 300w\" sizes=\"(max-width: 411px) 100vw, 411px\" \/><\/p>\n<p>Once attached lldb should present the list of threads. You can also run use the <em>bt<\/em> command to retrieve the callstack of the current thread, however will be very hard to debug it since you can\u2019t see the stack in a \u201cmanaged way\u201d<\/p>\n<p><img decoding=\"async\" width=\"401\" height=\"387\" class=\"wp-image-35318\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/05\/cidimage008-jpg01d2ac97-6b53d120.jpeg\" alt=\"cid:image008.jpg@01D2AC97.6B53D120\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/05\/cidimage008-jpg01d2ac97-6b53d120.jpeg 401w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/05\/cidimage008-jpg01d2ac97-6b53d120-300x290.jpeg 300w\" sizes=\"(max-width: 401px) 100vw, 401px\" \/><\/p>\n<h5>Load the SOS Extension<\/h5>\n<p>Loading the SOS extension will make things easier for us.<\/p>\n<p>The first step is to find the extension, you can find it by running the following command:<\/p>\n<p><code>find \/usr\/share\/dotnet -name libsosplugin.so<\/code><\/p>\n<p><img decoding=\"async\" width=\"402\" height=\"19\" class=\"wp-image-35319\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/05\/cidimage009-jpg01d2ac97-6b53d120.jpeg\" alt=\"cid:image009.jpg@01D2AC97.6B53D120\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/05\/cidimage009-jpg01d2ac97-6b53d120.jpeg 402w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/05\/cidimage009-jpg01d2ac97-6b53d120-300x14.jpeg 300w\" sizes=\"(max-width: 402px) 100vw, 402px\" \/><\/p>\n<p>Once you find the location we can load the extension with the follow command:<\/p>\n<p><code>(lldb) plugin load \/usr\/share\/dotnet\/shared\/Microsoft.NETCore.App\/1.0.1\/libsosplugin.so<\/code><\/p>\n<p>Alright the extension is loaded and now you can use the managed commands to debug your application.<\/p>\n<p>To test that lets run the <em>clrstack<\/em> command and now we get a much better view of what is going on.<\/p>\n<p><img decoding=\"async\" width=\"624\" height=\"184\" class=\"wp-image-35320\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/05\/cidimage010-jpg01d2ac97-6b53d120.jpeg\" alt=\"cid:image010.jpg@01D2AC97.6B53D120\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/05\/cidimage010-jpg01d2ac97-6b53d120.jpeg 624w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/05\/cidimage010-jpg01d2ac97-6b53d120-300x88.jpeg 300w\" sizes=\"(max-width: 624px) 100vw, 624px\" \/><\/p>\n<p>We can also run other SOS commands like <em>Threads<\/em> to find out all the manage Threads, to do that we type:<\/p>\n<p><code>sos Threads<\/code><\/p>\n<p><img decoding=\"async\" width=\"625\" height=\"193\" class=\"wp-image-35321\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/05\/cidimage011-jpg01d2ac97-6b53d120.jpeg\" alt=\"cid:image011.jpg@01D2AC97.6B53D120\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/05\/cidimage011-jpg01d2ac97-6b53d120.jpeg 625w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2017\/05\/cidimage011-jpg01d2ac97-6b53d120-300x93.jpeg 300w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<p>If you want to try out other SOS commands on your own, they are listed in the .<a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/bb190764(v=vs.110).aspx\">NET Framework documentation<\/a><\/p>\n<p>That\u2019s it for now guys. Hope this can be useful for you!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Get some insight into debugging .NET code on non-Windows operating systems with this post from Premier Developer consultants Gustavo Varo and Ben Williams. I have been working more and more with .NET Core and one question I keep getting from our customers and partners is about the debugging experience when using operating systems other than [&hellip;]<\/p>\n","protected":false},"author":582,"featured_media":37840,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[83,114,118,228,27,345],"class_list":["post-9085","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-permierdev","tag-net-core","tag-ben-williams","tag-debugging","tag-gustavo-varo","tag-linux","tag-sos-extension"],"acf":[],"blog_post_summary":"<p>Get some insight into debugging .NET code on non-Windows operating systems with this post from Premier Developer consultants Gustavo Varo and Ben Williams. I have been working more and more with .NET Core and one question I keep getting from our customers and partners is about the debugging experience when using operating systems other than [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/9085","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/users\/582"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/comments?post=9085"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/9085\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media\/37840"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media?parent=9085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/categories?post=9085"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/tags?post=9085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}