Commit 4cae5f5
authored
[One .NET] fix debugging (#4864)
If you tried to debug an app with:
dotnet build HelloAndroid/HelloAndroid.csproj -t:Install,_Run -p:AndroidAttachDebugger=true
The presence of `$(AndroidAttachDebugger)` causes a crash on startup:
E mono : Unhandled Exception:
E mono : System.ArgumentNullException: Value cannot be null. (Parameter 'meth')
E mono : at System.Reflection.Emit.ILGenerator.Emit(OpCode opcode, MethodInfo meth)
E mono : at Android.Runtime.JNINativeWrapper.CreateDelegate(Delegate dlg)
E mono : at Android.App.Activity.GetOnCreate_Landroid_os_Bundle_Handler()
E mono : at Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType nativeClass, Type type, String methods)
E mono : at Android.Runtime.JNIEnv.RegisterJniNatives(IntPtr typeName_ptr, Int32 typeName_len, IntPtr jniClass, IntPtr methods_ptr, Int32 methods_len)
E mono-rt : [ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentNullException: Value cannot be null. (Parameter 'meth')
E mono-rt : at System.Reflection.Emit.ILGenerator.Emit(OpCode opcode, MethodInfo meth)
E mono-rt : at Android.Runtime.JNINativeWrapper.CreateDelegate(Delegate dlg)
E mono-rt : at Android.App.Activity.GetOnCreate_Landroid_os_Bundle_Handler()
E mono-rt : at Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType nativeClass, Type type, String methods)
E mono-rt : at Android.Runtime.JNIEnv.RegisterJniNatives(IntPtr typeName_ptr, Int32 typeName_len, IntPtr jniClass, IntPtr methods_ptr, Int32 methods_len)
The cause of the crash is because we pass
`JNINativeWrapper.mono_unhandled_exception_method` to
[`ILGenerator.Emit(OpCode, MethodInfo)`][0], and
`mono_unhandled_exception_method` is `null`.
`mono_unhandled_exception_method` is a `MethodInfo` to
[`Debugger.Mono_UnhandledException()`][1], which does not exist in
.NET 5 Mono, which is why it's `null` within .NET 5.
To fix `dotnet build -t:_Run` support, cleanup
`JNINativeWrapper.get_runtime_types()` to instead check for
`JNINativeWrapper.exception_handler_method`, as
`exception_handler_method` is consistently initialized properly on
both "legacy" and .NET 5 environments.
Furthermore, update `get_runtime_types()` so that
`mono_unhandled_exception_method` is not passed to `ILGenerator.Emit()`
when it is `null`. This avoids the `ArgumentNullException`.
This surfaced another problem when using the `_Run` target by itself:
Xamarin.Android.Common.Debugging.targets(518,2): error MSB3073: The command ""adb" forward tcp:10000 tcp:10000" exited with code 9009.
The `$(_AndroidPlatformToolsDirectory)` property was blank when running
the `_Run` target by itself, but things work fine if you run
`Install,_Run` at the same time.
This behavior broke in commit fb637e0. I accidentally replaced the
`AndroidPrepareForBuild` MSBuild target with [the version][2] in
`Xamarin.Android.Bindings.Core.targets`.
To fix this, I can just move the binding project version of
`AndroidPrepareForBuild` target to `Xamarin.Android.Bindings.targets`.
I added a new test verifying that breakpoints work, when running
under .NET 5.
[0]: https://docs.microsoft.com/en-us/dotnet/api/system.reflection.emit.ilgenerator.emit?view=netcore-3.1#System_Reflection_Emit_ILGenerator_Emit_System_Reflection_Emit_OpCode_System_Reflection_MethodInfo_
[1]: https://github.com/mono/mono/blob/2ff424be2933f711207a8139c5792ab5d1b495a9/mcs/class/corlib/System.Diagnostics/Debugger.cs#L119-L127
[2]: https://github.com/xamarin/xamarin-android/blob/f99a3fad1b12d9674de2f624e243f296cfc40fc6/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.Bindings.Core.targets#L471 parent 73e91a9 commit 4cae5f5
File tree
4 files changed
+82
-6
lines changed- src
- Mono.Android/Android.Runtime
- Xamarin.Android.Build.Tasks
- MSBuild/Xamarin/Android
- tests/MSBuildDeviceIntegration/Tests
4 files changed
+82
-6
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
| 16 | + | |
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| |||
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
73 | | - | |
| 73 | + | |
74 | 74 | | |
75 | 75 | | |
76 | | - | |
| 76 | + | |
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
| |||
Lines changed: 0 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
47 | | - | |
48 | | - | |
49 | 47 | | |
50 | 48 | | |
51 | 49 | | |
| |||
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
| 78 | + | |
| 79 | + | |
78 | 80 | | |
79 | 81 | | |
80 | 82 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
| 5 | + | |
4 | 6 | | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
5 | 10 | | |
6 | 11 | | |
7 | 12 | | |
| |||
26 | 31 | | |
27 | 32 | | |
28 | 33 | | |
29 | | - | |
30 | 34 | | |
31 | 35 | | |
32 | 36 | | |
| |||
43 | 47 | | |
44 | 48 | | |
45 | 49 | | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
46 | 122 | | |
47 | 123 | | |
0 commit comments