{"id":35132,"date":"2018-04-04T14:38:59","date_gmt":"2018-04-04T21:38:59","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=35132"},"modified":"2019-04-04T08:34:59","modified_gmt":"2019-04-04T15:34:59","slug":"debugging-xamarin-mono","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/debugging-xamarin-mono\/","title":{"rendered":"Debugging Xamarin Open Source Frameworks: Part 3 &#8211; Mono"},"content":{"rendered":"<p>\t\t\t\tThis is the third post of four in the Debugging Xamarin Open Source Frameworks series, debugging Mono. <a href=\"https:\/\/blog.xamarin.com\/debugging-xamarin-forms\/\">Part 1<\/a> covers building Xamarin.Forms from the source and debugging that code, <a href=\"https:\/\/blog.xamarin.com\/debugging-xamarin-ios\/\">Part 2<\/a> covers Xamarin.iOS, and Part 4 steps into the source code to build and debug Xamarin.Android.<\/p>\n<h2>Debugging in Mono<\/h2>\n<p>If you work with Xamarin to develop awesome mobile apps, at some point you may have received an exception or error from the Mono runtime or BCL (Base Class Library). You may have wanted to step through that code to see if the error is something you can resolve, or if it&#8217;s a bug in the Mono code. You might even have wanted to file a pull request to merge a fix into the framework itself. Since Mono and the Xamarin frameworks (i.e. Xamarin.iOS, Xamarin.Android, and Xamarin.Forms) are all open source, this post will show you how you can help contribute to debugging!<\/p>\n<p><em>For this post, a Mac computer will be used to <a href=\"http:\/\/www.mono-project.com\/docs\/compiling-mono\/mac\/\"><em>build Mono from the source code<\/em><\/a>. You can also use <a href=\"http:\/\/www.mono-project.com\/docs\/compiling-mono\/windows\/\"><em>Windows<\/em><\/a> or <a href=\"http:\/\/www.mono-project.com\/docs\/compiling-mono\/linux\/\"><em>Linux<\/em><\/a>.<\/em><\/p>\n<h3 id=\"get-source\">Getting the Mono source code<\/h3>\n<p>There are a number of ways to get the Mono source code. The official Mono repository on GitHub is located at: <a href=\"https:\/\/github.com\/mono\/mono\">https:\/\/github.com\/mono\/mono<\/a>. If you&#8217;re already familiar with using git and GitHub, you can get the source code using your preferred method and skip to the next section.<\/p>\n<p>Use your Github account to fork the Mono repository and then clone that fork to your computer. If you do not have a GitHub account, just clone the official Mono repository directly to your computer.<\/p>\n<p>In the Mono repository linked above, you can fork the official repository to your own GitHub repository by hitting the <strong>Fork<\/strong> button in the upper right, which will create a new repository under your account:\n<img decoding=\"async\" class=\"size-full wp-image-35133 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/Fork.png\" alt=\"\" width=\"800\" height=\"307\" \/><\/p>\n<p>Whether you fork the repository or not, you can just select the <strong>Clone<\/strong> or <strong>download<\/strong> button, either in your fork or on the official Xamarin.iOS repo. Clicking the <strong>Clone or download<\/strong> button gives you two options:<\/p>\n<ol>\n<li><strong>Open in Desktop<\/strong>: Opens the <a href=\"https:\/\/desktop.github.com\"> GitHub Desktop app<\/a> on your Mac (<a href=\"https:\/\/desktop.github.com\">get it here<\/a>) and clone the repository to the directory that you specify:\n<ol>\n<li>Make commits<\/li>\n<li>Start pull requests<\/li>\n<li>Switch branches using the GitHub Desktop app, no command line necessary.<\/li>\n<\/ol>\n<p><em><strong>Note:<\/strong> only the master branch on GitHub will be downloaded. On the GitHub Desktop, you can switch branches and then that branch will be downloaded onto your computer as well (this may take some time).<\/em><\/li>\n<li><strong>Download ZIP<\/strong>: Downloads the currently selected branch as a .zip file. When you unzip, you will note that this is not a GIT repository, so it isn&#8217;t ready to do commits, pull requests, switch branches, etc. If you only want to work with one specific branch, this may be the right option for you.<\/li>\n<\/ol>\n<h3>Building Mono Dependencies<\/h3>\n<p>Once you have the source downloaded to your machine, use <a href=\"http:\/\/www.mono-project.com\/docs\/compiling-mono\/mac\/\">these instructions<\/a> as a guide to build Mono on your Mac, with some caveats noted below. It is recommended to read this entire section before starting.<\/p>\n<p>By default, you build the master branch of Mono. Verify that the master branch is building successfully by checking the <a href=\"https:\/\/github.com\/mono\/mono#build-status\"><strong>Build Status<\/strong><\/a> OS X section on the main page of the GitHub repository. If OS X shows as <em>failing<\/em> or <em>unstable<\/em>, get a different branch to build.<\/p>\n<p>In the <a href=\"http:\/\/www.mono-project.com\/docs\/compiling-mono\/mac\/#building-mono-dependencies\"><em>Building Mono Dependencies<\/em><\/a> section of the instructions linked above, you&#8217;ll see a reference to <a href=\"https:\/\/brew.sh\"><em>Homebrew<\/em><\/a>, a macOS package manager. Get it <a href=\"https:\/\/brew.sh\">here<\/a> to install dependencies like <strong>autoconf<\/strong> and <strong>cmake<\/strong>.<\/p>\n<h2>Build and Install Mono<\/h2>\n<p>Once you&#8217;re done installing your Mono dependencies, you&#8217;re ready to build and install Mono using the instructions for <a href=\"http:\/\/www.mono-project.com\/docs\/compiling-mono\/mac\/#building-mono-from-a-git-source-code-checkout\"><em>Building Mono From a Git Source Code Checkout<\/em><\/a>. It&#8217;s important to set the <strong>PREFIX<\/strong>, which will be the path for your Mono install. Use <strong>\/usr\/local<\/strong>, as in the <a href=\"http:\/\/www.mono-project.com\/docs\/compiling-mono\/mac\/#building-mono-from-a-release-package\">build instructions for a release package<\/a>.<\/p>\n<p>In this case the Mono repo is at path:<\/p>\n<pre><code>\/Users\/\/git\/mono<\/code><\/pre>\n<p>Create the following folder for the Mono install:<\/p>\n<pre><code>\/Users\/\/git\/mono-install<\/code><\/pre>\n<p>We do not need to run the <strong>git clone &#8230;<\/strong> command since the repository is already cloned. Instead, do the following:<\/p>\n<ol>\n<li>Open Terminal and navigate to your Mono repository (replace <strong>\/path\/to\/mono<\/strong> with the actual path to your Mono repository):\n<pre><code>cd \/path\/to\/mono<\/pre>\n<p><\/code><\/li>\n<li>Set the <strong>PREFIX<\/strong> variable (replace <strong>\/path\/to\/mono-install<\/strong> with the actual path to your Mono install folder):\n<pre><code>PREFIX=\/path\/to\/mono-install<\/pre>\n<p><\/code><\/li>\n<li>Add <strong>PREFIX\/bin<\/strong> to your <strong>$PATH<\/strong> variable:\n<pre><code>PATH=$PREFIX\/bin:$PATH<\/pre>\n<p><\/code><\/li>\n<li>Run the following commands to build in 64 bit mode:\n<pre><code>.\/autogen.sh --prefix=$PREFIX --disable-nls<\/pre>\n<p><\/code><\/p>\n<ol>\n<li>Make<\/li>\n<li>Make install<\/li>\n<\/ol>\n<\/ol>\n<p>Congratulations, you have now successfully built Mono! See the <a href=\"http:\/\/www.mono-project.com\/docs\/compiling-mono\/mac\/#troubleshooting\">troubleshooting<\/a> section for any possible issues.<\/p>\n<h2>Bring Mono to Visual Studio for Mac<\/h2>\n<p>There are a couple of things to do in Visual Studio for Mac to make your shiny new Mono build available and start stepping into the Mono source code:<\/p>\n<ol>\n<li>Open Visual Studio for Mac.<\/li>\n<li>Open <strong>Visual Studio &gt; Preferences<\/strong>.<\/li>\n<li>Select <strong>Projects &gt; Debugger<\/strong> on the left.<\/li>\n<li>Uncheck <strong>\"Debug project code only; do not step into framework code\"<\/strong>.\n<p><img decoding=\"async\" class=\"size-full wp-image-35159 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/DebugProjectCode.png\" alt=\"\" width=\"800\" height=\"584\" \/><\/li>\n<li>Select <strong>Projects &gt; .NET Runtimes<\/strong> on the left.<\/li>\n<li>Click the <strong>Add<\/strong> button.<\/li>\n<li>Browse to the folder where you installed Mono earlier.<\/li>\n<li>Select and <strong>Open<\/strong>.<\/li>\n<p><img decoding=\"async\" class=\"size-full wp-image-35160 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/AdddMono.png\" alt=\"\" width=\"800\" height=\"581\" \/><\/li>\n<\/ol>\n<p>Once you have a solution loaded, select your new build of Mono using the <strong>Project &gt; Active Runtime<\/strong> menu. You can now step into the Mono source code while debugging.<\/p>\n<h2>Debugging Mono Source Code<\/h2>\n<ol>\n<li>Open Visual Studio for Mac.<\/li>\n<li>Create a new .NET Console project (<strong>File &gt; New Solution &gt; Other &gt; .NET &gt; Console Project<\/strong>).\n<p><img decoding=\"async\" class=\"size-full wp-image-35162 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/NETConsole.png\" alt=\"\" width=\"800\" height=\"582\" \/><\/li>\n<li>Select the Mono runtime you just built as the active runtime:\na. Select menu <strong>Project &gt; Active Runtime.<\/strong>\nb. You will see your Mono runtime along with others installed on the system. Select the runtime that matches the path where Mono is installed.<\/p>\n<p><img decoding=\"async\" class=\"size-full wp-image-35163 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/SelectMonoRuntime.png\" alt=\"\" width=\"800\" height=\"248\" \/><\/li>\n<li>By default, <strong>MSBuild<\/strong> is <strong>not included<\/strong> when building mono from source. Use <strong>xbuild<\/strong> instead of <strong>MSBuild<\/strong> for this console project:\na. Right click on the console project in the solution explorer and select <strong>Options<\/strong>.\nb. Select <strong>Build &gt; General<\/strong> on the left.\nc. Uncheck the <strong>Use MSBuild build engine...<\/strong> option.<\/p>\n<p><img decoding=\"async\" class=\"size-full wp-image-35166 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/UseXbuild.png\" alt=\"\" width=\"800\" height=\"585\" \/><\/li>\n<li>Open the <strong>Program.cs<\/strong> file.<\/li>\n<li>Place a breakpoint on the <strong>Console.WriteLine<\/strong> statement.<\/li>\n<li>Run the project with debugging.<\/li>\n<li>When the program pauses on the <strong>Console.WriteLine<\/strong> statement, step into the code.<\/li>\n<\/ol>\n<p><img decoding=\"async\" class=\"size-full wp-image-35135 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/StepInto.png\" alt=\"\" width=\"800\" height=\"434\" \/><\/p>\n<p>Congratulations, you're now stepping into and through Mono source code! Right click on the tab for the Mono source code file that opens in Visual Studio for Mac, and select <strong>Reveal in Finder<\/strong>.<\/p>\n<p><img decoding=\"async\" class=\"size-full wp-image-35170 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/RevealinFinder.png\" alt=\"\" width=\"800\" height=\"294\" \/><\/p>\n<p>A Finder window will open with the source code file highlighted so you can see that this is the source code file from your cloned Mono repository. Assuming you cloned the repo instead of downloading a .zip archive, you can also edit the code and make a new commit or pull request using GitHub Desktop.<\/p>\n<h2>Summary<\/h2>\n<p>Through this post, you now have the ability to use, build, and install Mono from source code onto your computer. Additionally, you learned how to make your build of Mono available to Visual Studio for Mac and use it in a Console project to step into and through the Mono source code. <\/p>\n<p>If you fixed a bug or made some improvement during this process that you would like to share, <a href=\"https:\/\/github.com\/mono\/mono#contributing-to-mono\">please contribute to debugging Mono<\/a>.<\/p>\n<p>Enjoy this post? <a href=\"https:\/\/forums.xamarin.com\/124959\/debugging-xamarin-open-source-frameworks-mono#latest\">Discuss it on the forums<\/a>!\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is the third post of four in the Debugging Xamarin Open Source Frameworks series, debugging Mono. Part 1 covers building Xamarin.Forms from the source and debugging that code, Part 2 covers Xamarin.iOS, and Part 4 steps into the source code to build and debug Xamarin.Android. Debugging in Mono If you work with Xamarin to [&hellip;]<\/p>\n","protected":false},"author":554,"featured_media":39167,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[4],"class_list":["post-35132","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-xamarin-platform"],"acf":[],"blog_post_summary":"<p>This is the third post of four in the Debugging Xamarin Open Source Frameworks series, debugging Mono. Part 1 covers building Xamarin.Forms from the source and debugging that code, Part 2 covers Xamarin.iOS, and Part 4 steps into the source code to build and debug Xamarin.Android. Debugging in Mono If you work with Xamarin to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/35132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/554"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=35132"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/35132\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/39167"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=35132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=35132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=35132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}