Bug 33952 - Debugger.Break() causes app to crash if Debugger is not attached,
Summary: Debugger.Break() causes app to crash if Debugger is not attached,
Alias: None
Product: iOS
Classification: Xamarin
Component: Debugger ()
Version: XI 8.10
Hardware: Macintosh Mac OS
: --- enhancement
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2015-09-14 22:13 UTC by Jon Goldberger [MSFT]
Modified: 2015-09-15 15:08 UTC (History)
5 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Jon Goldberger [MSFT] 2015-09-14 22:13:03 UTC
## Description

According to MSDN docs [1], Debugger.Break() should pause the app execution and allow one to connect the debugger if the debugger is not connected. However in Xamarin Studio (and Visual Studio) the app just crashes. 

The workaround is to check if the debugger is attached before calling this method:

>     Debugger.Break();

[1] https://msdn.microsoft.com/en-us/library/system.diagnostics.debugger.break(v=vs.110).aspx
"If no debugger is attached, users are asked if they want to attach a debugger. "

## Steps to reproduce:

1. Create a new iOS Single View project in Xamarin Studio.

2. Override the ViewDidAppear method in the ViewController.cs file  and add this line to it:

2. Select Debug configuration

3. Launch to device/simulator

4. When the app breaks at the line added in step 2, press Stop to close the app and disconnect the debugger.

5 Launch the app from the home screen of the device simulator.

Expected result: You will be asked if you want to attach the debugger. 

Actual result: App crashes. 

## Note

I realize that it would be best to wrap the Debugger.Break call in the above check to see if the debugger is attached, or at least a compiler directive so the method is not called in release builds, but I am filing this in case I am incorrect and this method should either be ignored if the debugger is not attached or should break and ask if the developer wants to attach the debugger as indicated in the MSDN doc [1] linked above. 

## Version Info:

=== Xamarin Studio ===

Version 5.10 (build 704)
Installation UUID: 964c531b-d928-456b-a9ae-e1f82266b360
	Mono 4.2.0 (explicit/2701b19)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 402000207

=== Xamarin.Profiler ===

Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Apple Developer Tools ===

Xcode 6.4 (7720)
Build 6E35b

=== Xamarin.Mac ===

Version: (Business Edition)

=== Xamarin.Android ===

Version: (Business Edition)
Android SDK: /Users/apple/Library/Developer/Xamarin/android-sdk-mac_x86
	Supported Android versions:
		4.0.3 (API level 15)
		4.1   (API level 16)
		4.2   (API level 17)
		4.3   (API level 18)
		4.4   (API level 19)
		5.0   (API level 21)
		5.1   (API level 22)
		6.0   (API level 23)

SDK Tools Version: 24.3.4
SDK Platform Tools Version: 23.0.1
SDK Build Tools Version: 23

Java SDK: /usr
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)

=== Xamarin Android Player ===

Version: 0.5.6
Location: /Applications/Xamarin Android Player.app

=== Xamarin.iOS ===

Version: (Business Edition)
Hash: 79f8535
Branch: master
Build date: 2015-08-31 15:32:33-0400

=== Build Information ===

Release ID: 510000704
Git revision: c16e63aeeaa63ea912638f958ae1547905aed4e5
Build date: 2015-08-31 15:47:00-04
Xamarin addins: a7a74af8bcc6bca9a02f34afb66025c0a3fa18cb
Build lane: monodevelop-lion-master

=== Operating System ===

Mac OS X 10.10.5
Darwin Jons-iMac.local 14.5.0 Darwin Kernel Version 14.5.0
    Wed Jul 29 02:26:53 PDT 2015
    root:xnu-2782.40.9~1/RELEASE_X86_64 x86_64
Comment 1 Brendan Zagaeski (Xamarin Team, assistant) 2015-09-14 22:49:46 UTC
## Counter-example of Windows console C# app in Visual Studio (it also contradicts the MSDN docs)

The following program crashes if you choose "Run -> Start Without Debugging" in Visual Studio 2013 (or compile it and then double-click the `.exe` in Explorer):

namespace Foo
    class Program
        static void Main(string[] args)

## It appears Xamarin's behavior is consistent with the actual in-practice behavior of desktop .NET

This enhancement isn't quite "resolved upstream" since the Mono soft debugger would need its own special handling for this scenario. But it might be "RESOLVED INVALID" since the current behavior of the Mono soft debugger is consistent with the behavior of a Windows console C# app in Visual Studio.

For the moment, I will simply change this report to be an enhancement. If the original reporting customer can demonstrate a use case for `Debugger.Break()` that succeeds in a Windows app in Visual Studio but fails in Xamarin apps, we can re-evaluate that assignment.
Comment 2 Brendan Zagaeski (Xamarin Team, assistant) 2015-09-15 02:04:05 UTC
Small correction to my previous comment: rather than "RESOLVED INVALID", "RESOLVED NOT_ON_ROADMAP" would probably be the better, more descriptive status for this enhancement if the developers decide to resolve it without changing Xamarin's behavior.
Comment 3 David Karlaš 2015-09-15 02:26:02 UTC
In case of Android/iOS... how/where should "You will be asked if you want to attach the debugger." appear? In Android/iOS app as popup? Also... we currently don't have support for attaching debugger to running process.

Since this is mostly runtime thing(crashing). I'm moving bug to runtime.
Comment 4 Zoltan Varga 2015-09-15 15:07:43 UTC
Fixed in mono master 15bee580fd88814e5baa77c181e8bb06b8efe7fa/4.2 11ce499c406bc09c4c3b4a2def21618cbdb8696d.

The fix will be in the next major Xamarin.ios release.