Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debug feature broken on Android 9 with VS2019 & VS2022 #7658

Closed
jiro-san opened this issue Dec 30, 2022 · 25 comments
Closed

Debug feature broken on Android 9 with VS2019 & VS2022 #7658

jiro-san opened this issue Dec 30, 2022 · 25 comments
Assignees
Labels
Area: Mono Runtime Mono-related issues: BCL bugs, AOT issues, etc.

Comments

@jiro-san
Copy link

Android application type

Classic Xamarin.Android (MonoAndroid12.0, etc.)

Affected platform version

VS2022 17.4.3 / VS2019 16.9.3

Description

There are big stability issues when debugging a Xamarin.Android app with VS2022 or VS2019.

We are using Android x86 (Android version 9 - Pie) to debug our app in a VMware virtual machine because the app needs to be deployed on an embedded system that uses Android Pie.
Also we have the same issues with real android 9 device based on ARM processor so it is not linked to x86, I talk about it here because it is a good neutral environment to reproduce the issue.

We tried running with different development environment, in VS2019 and VS2022, and here are the issues we faced:

With VS2022

  • Exceptions filter do not work at all: either all exceptions are activated or none.
  • Also we need to check "Use Fast Deployment" to be able to launch the app at all. For some reason it makes launching the app unusually slow. And anyway the exceptions filters don't work so debugging is hard.

When Fast Deployment is unchecked in our app we get the following log in debug output:

[Random.App] Unexpected CPU variant for X86 using defaults: x86_64
[monodroid] Creating public update directory: /data/user/0/Some.Random.App/files/.__override__
[meZero.BillFis] Attempt to remove non-JNI local reference, dumping thread
[monodroid-debug] Trying to initialize the debugger with options: --debugger-agent=transport=dt_socket,loglevel=0,address=127.0.0.1:8822,server=y,embedding=1
[monodroid-assembly] Failed to load managed assembly 'mscorlib.dll'. Internal error
[monodroid-assembly] open_from_bundles: failed to load assembly mscorlib.dll
[mono] The assembly mscorlib.dll was not found or could not be loaded.
[mono] It should have been installed in the `/Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/sdks/out/android-x86-release/lib/mono/2.1/mscorlib.dll' directory.

With VS2019

  • When we use the exceptions filters, we cannot change them dynamically: when we do it the changes don't apply. So we are stuck using the filters we set when starting the debug session.
  • When debugging sometimes the debugger crashes with no error message when staying on a breakpoint for too long, or trying to display information about a local variable value.
  • When debugging sometimes the debugger crashes with an exception:

EXCEPTION: Mono.Debugger.Soft.VMNotSuspendedException: The vm is not suspended.

at Mono.Debugger.Soft.VirtualMachine.ErrorHandler(Object sender, ErrorHandlerEventArgs args) in C:\A\1\216\s\external\debugger-libs\Mono.Debugger.Soft\Mono.Debugger.Soft\VirtualMachine.cs:line 367

at Mono.Debugger.Soft.Connection.SendReceive(CommandSet command_set, Int32 command, PacketWriter packet) in C:\A\1\216\s\external\debugger-libs\Mono.Debugger.Soft\Mono.Debugger.Soft\Connection.cs:line 1805

at Mono.Debugger.Soft.VirtualMachine.Resume() in C:\A\1\216\s\external\debugger-libs\Mono.Debugger.Soft\Mono.Debugger.Soft\VirtualMachine.cs:line 139

at Mono.Debugging.Soft.SoftDebuggerSession.HandleEventSet(EventSet es) in C:\A\1\216\s\external\debugger-libs\Mono.Debugging.Soft\SoftDebuggerSession.cs:line 1714

at Mono.Debugging.Soft.SoftDebuggerSession.EventHandler() in C:\A\1\216\s\external\debugger-libs\Mono.Debugging.Soft\SoftDebuggerSession.cs:line 1615

Those issues with VS2019 & 2022 are hindering a lot our debugging efficiency, since the issues with VS2022 made us prefer VS2019, but often we crash just trying to get information about a variable.

Is there any way to work around these issues, or to solve them? The best would be that all works fine in VS2022 so we could forget about VS2019 all together.

Below are the version information:

VS2022

Microsoft Visual Studio Enterprise 2022
Version 17.4.3
VisualStudio.17.Release/17.4.3+33205.214
Microsoft .NET Framework
Version 4.8.04084

Installed Version: Enterprise

Visual C++ 2022   00482-20050-05733-AA863
Microsoft Visual C++ 2022

ADL Tools Service Provider   1.0
This package contains services used by Data Lake tools

ASA Service Provider   1.0

ASP.NET and Web Tools   17.4.326.54890
ASP.NET and Web Tools

Azure App Service Tools v3.0.0   17.4.326.54890
Azure App Service Tools v3.0.0

Azure Data Lake Tools for Visual Studio   2.6.5000.0
Microsoft Azure Data Lake Tools for Visual Studio

Azure Functions and Web Jobs Tools   17.4.326.54890
Azure Functions and Web Jobs Tools

Azure Stream Analytics Tools for Visual Studio   2.6.5000.0
Microsoft Azure Stream Analytics Tools for Visual Studio

C# Tools   4.4.0-6.22580.4+d7a61210a88b584ca0827585ec6e871c6b1c5a14
C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Common Azure Tools   1.10
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Cookiecutter   17.0.22263.6
Provides tools for finding, instantiating and customizing templates in cookiecutter format.

Extensibility Message Bus   1.4.1 (main@2ee106a)
Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration.

Linux Core Dump Debugging   1.0.9.33020
Enables debugging of Linux core dumps.

Microsoft Azure Hive Query Language Service   2.6.5000.0
Language service for Hive query

Microsoft Azure Stream Analytics Language Service   2.6.5000.0
Language service for Azure Stream Analytics

Microsoft Azure Tools for Visual Studio   2.9
Support for Azure Cloud Services projects

Microsoft JVM Debugger   1.0
Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Mono Debugging for Visual Studio   17.4.19 (8c0a575)
Support for debugging Mono processes with Visual Studio.

NuGet Package Manager   6.4.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

Python - Profiling support   17.0.22263.6
Profiling support for Python projects.

Python with Pylance   17.0.22263.6
Provides IntelliSense, projects, templates, debugging, interactive windows, and other support for Python developers.

Razor (ASP.NET Core)   17.0.0.2246202+61cc048d36a3fc9246d2f04625988b19a18ab8f0
Provides languages services for ASP.NET Core Razor.

SQL Server Data Tools   17.0.62207.28050
Microsoft SQL Server Data Tools

Test Adapter for Boost.Test   1.0
Enables Visual Studio's testing tools with unit tests written for Boost.Test.  The use terms and Third Party Notices are available in the extension installation directory.

Test Adapter for Google Test   1.0
Enables Visual Studio's testing tools with unit tests written for Google Test.  The use terms and Third Party Notices are available in the extension installation directory.

ToolWindowHostedEditor   1.0
Hosting json editor into a tool window

TypeScript Tools   17.0.10921.2001
TypeScript Tools for Microsoft Visual Studio

Visual Basic Tools   4.4.0-6.22580.4+d7a61210a88b584ca0827585ec6e871c6b1c5a14
Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual C++ for Linux Development   1.0.9.33020
Visual C++ for Linux Development

Visual F# Tools   17.4.0-beta.22512.4+525d5109e389341bb90b144c24e2ad1ceec91e7b
Microsoft Visual F# Tools

Visual Studio IntelliCode   2.2
AI-assisted development for Visual Studio.

VisualStudio.DeviceLog   1.0
Information about my package

VisualStudio.Mac   1.0
Mac Extension for Visual Studio

VSPackage Extension   1.0
VSPackage Visual Studio Extension Detailed Info

Xamarin   17.4.0.312 (d17-4@be7e8d1)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer   17.4.0.138 (remotes/origin/d17-4@d36bba3cc9)
Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin Templates   17.4.2 (c457c97)
Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms.

Xamarin.Android SDK   13.1.0.1 (d17-4/13ba222)
Xamarin.Android Reference Assemblies and MSBuild support.
    Mono: a96bde9
    Java.Interop: xamarin/java.interop/d17-4@fcc33ce2
    SQLite: xamarin/sqlite/3.39.3@23e1ae7
    Xamarin.Android Tools: xamarin/xamarin-android-tools/main@0be567a


Xamarin.iOS and Xamarin.Mac SDK   16.1.1.27 (933c6c2c9)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.

VS2019

Microsoft Visual Studio Enterprise 2019 (2)
Version 16.9.3
VisualStudio.16.Release/16.9.3+31129.286
Microsoft .NET Framework
Version 4.8.04084

Installed Version: Enterprise

Architecture Diagrams and Analysis Tools   00433-90100-10153-AA695
Microsoft Architecture Diagrams and Analysis Tools

Visual C++ 2019   00433-90100-10153-AA695
Microsoft Visual C++ 2019

.NET Portability Analyzer   1.1.10808.0
Evaluates portability of assemblies across .NET platforms.

ASP.NET and Web Tools 2019   16.9.693.2781
ASP.NET and Web Tools 2019

ASP.NET Core Razor Language Services   16.1.0.2112521+5741df381174d72f08e3632bb99f52e8635b6a1a
Provides languages services for ASP.NET Core Razor.

ASP.NET Web Frameworks and Tools 2019   16.9.693.2781
For additional information, visit https://www.asp.net/

Azure App Service Tools v3.0.0   16.9.693.2781
Azure App Service Tools v3.0.0

C# Tools   3.9.0-6.21160.10+59eedc33d35754759994155ea2f4e1012a9951e3
C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Common Azure Tools   1.10
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Extensibility Message Bus   1.2.6 (master@34d6af2)
Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration.

IntelliCode Extension   1.0
IntelliCode Visual Studio Extension Detailed Info

Microsoft Azure Tools   2.9
Microsoft Azure Tools for Microsoft Visual Studio 2019 - v2.9.40218.1

Microsoft Continuous Delivery Tools for Visual Studio   0.4
Simplifying the configuration of Azure DevOps pipelines from within the Visual Studio IDE.

Microsoft JVM Debugger   1.0
Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Microsoft Library Manager   2.1.113+g422d40002e.RR
Install client-side libraries easily to any web project

Microsoft MI-Based Debugger   1.0
Provides support for connecting Visual Studio to MI compatible debuggers

Microsoft Visual C++ Wizards   1.0
Microsoft Visual C++ Wizards

Microsoft Visual Studio Tools for Containers   1.1
Develop, run, validate your ASP.NET Core applications in the target environment. F5 your application directly into a container with debugging, or CTRL + F5 to edit & refresh your app without having to rebuild the container.

Microsoft Visual Studio VC Package   1.0
Microsoft Visual Studio VC Package

Mono Debugging for Visual Studio   16.9.7 (df23ba6)
Support for debugging Mono processes with Visual Studio.

NuGet Package Manager   5.9.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

Productivity Power Tools 2017/2019   16.0
Installs the individual extensions of Productivity Power Tools 2017/2019

ProjectServicesPackage Extension   1.0
ProjectServicesPackage Visual Studio Extension Detailed Info

SQL Server Data Tools   16.0.62103.10080
Microsoft SQL Server Data Tools

Syntax Visualizer   1.0
An extension for visualizing Roslyn SyntaxTrees.

Test Adapter for Boost.Test   1.0
Enables Visual Studio's testing tools with unit tests written for Boost.Test.  The use terms and Third Party Notices are available in the extension installation directory.

Test Adapter for Google Test   1.0
Enables Visual Studio's testing tools with unit tests written for Google Test.  The use terms and Third Party Notices are available in the extension installation directory.

TFS Source Control Explorer Extension   1.0
Visual Studio Extension for Team Foundation Server Source Control Explorer

TypeScript Tools   16.0.30201.2001
TypeScript Tools for Microsoft Visual Studio

Visual Basic Tools   3.9.0-6.21160.10+59eedc33d35754759994155ea2f4e1012a9951e3
Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual F# Tools   16.9.0-beta.21102.9+7ce7132f1459095e635194d09d6f73265352029a
Microsoft Visual F# Tools

Visual Studio Code Debug Adapter Host Package   1.0
Interop layer for hosting Visual Studio Code debug adapters in Visual Studio

Visual Studio Container Tools Extensions   1.0
View, manage, and diagnose containers within Visual Studio.

Visual Studio Tools for CMake   1.0
Visual Studio Tools for CMake

Visual Studio Tools for Containers   1.0
Visual Studio Tools for Containers

VisualStudio.DeviceLog   1.0
Information about my package

VisualStudio.Foo   1.0
Information about my package

VisualStudio.Mac   1.0
Mac Extension for Visual Studio

WiX Toolset Visual Studio Extension   1.0.0.18
WiX Toolset Visual Studio Extension version 1.0.0.18
Copyright (c) .NET Foundation and contributors. All rights reserved.

Xamarin   16.9.000.273 (d16-9@1bba9e0)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer   16.9.0.316 (remotes/origin/d16-9@fdbf64026)
Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin Templates   16.9.68 (8e9b569)
Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms.

Xamarin.Android SDK   11.2.2.1 (d16-9/877f572)
Xamarin.Android Reference Assemblies and MSBuild support.
    Mono: 5e9cb6d
    Java.Interop: xamarin/java.interop/d16-9@54f8c24
    ProGuard: Guardsquare/proguard/v7.0.1@912d149
    SQLite: xamarin/sqlite/3.34.1@daff8f4
    Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-9@d210f11


Xamarin.iOS and Xamarin.Mac SDK   14.14.2.5 (3836759d4)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.

Steps to Reproduce

With VS2022:

  1. Install Android 9.0 in VMware by using Android x86.
    (vmware image can be downloaded from here: https://www.osboxes.org/android-x86/#android-x86-9-0-r2-vmware)
  2. Create a Xamarin Android app and uncheck "Use Fast Deployment" in project properties.
  3. Run the application in Debug attached.

Result:

  1. The app won't launch if "Use Fast Deployment" is not checked
  2. If Fast Deployment is used, the app will take an usual long time to launch.
  3. The exception filters cannot be used.

Note: when launching the app there is a warning about exception filters:
This debug engine does not support exception conditions. The condition(s) will be ignored.

With VS2019:

  1. Install Android 9.0 in VMware by using Android x86.
    (vmware image can be downloaded from here: https://www.osboxes.org/android-x86/#android-x86-9-0-r2-vmware)
  2. Create a Xamarin Android app.
  3. Run the application in Debug attached and perform usual debug operations (stop on breakpoints, check variable contents like lists etc., use some async await in the application and check list contents (you can for example try to list tasks from an azure job asynchronously and check the enumeration contents)).

Result:
When performing normal debug operations like stopping on a breakpoint for some time or checking local variables or list contents the debugger crashes (resulting in application crash or freeze).

Did you find any workaround?

No response

Relevant log output

No response

@jiro-san jiro-san added Area: App Runtime Issues in `libmonodroid.so`. needs-triage Issues that need to be assigned. labels Dec 30, 2022
@dellis1972 dellis1972 self-assigned this Jan 3, 2023
@dellis1972 dellis1972 removed the needs-triage Issues that need to be assigned. label Jan 3, 2023
@dellis1972 dellis1972 added this to the Under Consideration milestone Jan 3, 2023
@grendello grendello added Area: Debugger Issues using or interacting with the debugger. and removed Area: App Runtime Issues in `libmonodroid.so`. labels Jan 3, 2023
@dellis1972
Copy link
Contributor

@jiro-san

I suspect your issue might be that the AndroidSupportedAbis value defaults to armeabi-v7a;arm64-v8a when you are not using Fast Deployment.

can you try adding the following to your Debug configuration in your main apps csproj?

<AndroidSupportedAbis>armeabi-v7a;x86;x86_64;arm64-v8a</AndroidSupportedAbis>

This will ensure that the x84 (or x86_64) native libraries are included in the applicastion package.

As for the Filter Exceptions you will need to report tht issue via the Visual Studio 'Report an issue' menu item since it is a different team which deals with that.

@jiro-san
Copy link
Author

jiro-san commented Jan 4, 2023

Our app is using .so files compiled for armeabi-v7a, and we don't have the version for arm64-v8a yet. If we enable arm64-v8a we get "[mono-rt] [ERROR] FATAL UNHANDLED EXCEPTION: System.NotSupportedException" when trying to initialize our so file.

We will try getting support for x64-v8a too, but since x64-v8a ARM CPUs are also compatible with armeabi-v7a libraries (I didn't have time yet to look for the official information from ARM but it works when we try our app, without debugging), I did not expect this issue.
Also it does not happen with the VS2019 environment.

So we will try to add x64-v8a support, but if you can please comment if you see it as a bug or as an expected behavior.

@dellis1972
Copy link
Contributor

So we are going to need some diagnsotic build logs since I am unable to repo the VMWare issue with the Android x86 Image you linked to.

So from a developer command prompt can you run the following command on your project after deleting the bin & obj directories. Do this against your Android 9 device , not the VMWare image.

msbuld <your app.csproj> /restore -t:Install -p: _FastDeploymentDiagnosticLogging=True -bl

This will produce a msbuild.binlog file which you can zip up and sent do us.

next from an Android Command Prompt (There should be a menu item in VS) run

adb logcat -c
adb logcat > adb.log

Then run the app on the device. Once the app crashes presss Ctrl-C to exit adb logcat and upload the adb.log file as well.
This should contain the info we need to investigte further.

@dellis1972 dellis1972 added the need-info Issues that need more information from the author. label Jan 4, 2023
@ghost
Copy link

ghost commented Jan 4, 2023

Hi @jiro-san. We have added the "need-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@jiro-san
Copy link
Author

jiro-san commented Jan 4, 2023

Hello, oh ok sorry i was not clear, you can reproduce the same issue with an app that uses x86 libraries (.so files) running on the VM i provided (the VM runs with x86_64 architecture, so what we said about armeabi-v7a and arm64-v8a also applies here).
On our side our app uses x86 and armeabi-v7a abis, so we need libs for x86_64 and arm64-v8a to test your proposal, but I am sure you can reproduce it if you deploy on x86_64 with an app that supports only x86 abi.

@ghost ghost added need-attention A xamarin-android contributor needs to review and removed need-info Issues that need more information from the author. labels Jan 4, 2023
@dellis1972
Copy link
Contributor

So I re testing again with the following settings on the x86_64 VMWare Image

    <EmbedAssembliesIntoApk>True</EmbedAssembliesIntoApk>
    <AndroidSupportedAbis>armeabi-v7a;x86</AndroidSupportedAbis>

This is with an vamilla Xamarin.Anroid app. This worked fine for me. The zip only contained the arm v7 and x86 runtimes for Xamarin.Android, and the app worked as expected.
It also worked correctly with EmbedAssembliesIntoApk set to false.

So it looks like I'm going to need those logs and the logcat output.

@dellis1972 dellis1972 added need-info Issues that need more information from the author. and removed need-attention A xamarin-android contributor needs to review labels Jan 4, 2023
@ghost
Copy link

ghost commented Jan 4, 2023

Hi @jiro-san. We have added the "need-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@jiro-san
Copy link
Author

jiro-san commented Jan 11, 2023

Hello again.
Sorry for the delay I was making a reduced sample to show you the issue with the sample app.
Also I collected the logs you asked with this sample (msbuild & logcat), please see the attachments.

Can you try it please?

It uses a native library libMatrix.so that does simple matrix multiplication.
When you run it with F5, it will not run properly in the VM unless "Use Fast Deployment" is checked.
It runs fine if the debugger is not attached.

You can see the configuration in the .csproj:

<EmbedAssembliesIntoApk>true</EmbedAssembliesIntoApk> <AndroidSupportedAbis>x86</AndroidSupportedAbis>

Attached files:
DebugCrashSample.zip: the sample
msbuild.zip: logs of msbuild
adb.zip: log from logcat
debugoutput: screencapture of the error reported in visual studio output view.

DebugCrashSample.zip
msbuild.zip
adb.zip
debugoutput

@ghost ghost added need-attention A xamarin-android contributor needs to review and removed need-info Issues that need more information from the author. labels Jan 11, 2023
@dellis1972
Copy link
Contributor

@jiro-san

I wanted to give you an update. I managed to repo the issue with your sample, thank you.

The core of the problem is this

[monodroid-assembly] open_from_update_dir: trying to open assembly: /data/user/0/com.test.debugcrashsample/files/.__override__/mscorlib.dll
[monodroid-assembly] individual_assemblies_open_from_bundles: looking for bundled name: 'mscorlib.dll'
[monodroid-assembly]                        mmap_start: 0xbefb3000  mmap_end: 0xc00e52d0  mmap_len:      4507828  file_start: 0xbefb3cb4  file_end: 0xc00e2cb4  file_len:      4504576      apk descriptor: 34  file: mscorlib.dll
[monodroid-assembly] file-offset:  3683cb4  start: 0xbefb3cb4  end: 0xbf3ff8b4  len:      4504576  zip-entry:  mscorlib.dll name: mscorlib.dll [MZ......]
[monodroid-assembly]                        mmap_start: 0xbee19000  mmap_end: 0xbf47df10  mmap_len:      1676228  file_start: 0xbee198f4  file_end: 0xbf47c434  file_len:      1673936      apk descriptor: 34  file: mscorlib.pdb
[Mono] Image addref mscorlib[0xdd571a20] (asmctx DEFAULT) -> mscorlib.dll[0xe3a8cd00]: 3
[Mono] Prepared to set up assembly 'mscorlib' (mscorlib.dll)
[monodroid-assembly] Failed to load managed assembly 'mscorlib.dll'. Internal error
[monodroid-assembly] open_from_bundles: failed to load assembly mscorlib.dll

For some reason we cannot load mscorlib.dll which is packaged. This is confusing because the app loads find when we don't attach the debugger. I will need to discuss this issue with my team to see if we can figure out what is going on.

@jiro-san
Copy link
Author

Any progress?

@dellis1972
Copy link
Contributor

@grendello when you get a moment can you take a look at this issue. Its very odd.

@grendello
Copy link
Contributor

@dellis1972 The error message's "Internal error" part is the error string we get from Mono, so I suppose the best course of action is to enable more logging from the Mono side.

@jiro-san can you run your app after issuing the following commands from the command line:

> adb shell setprop debug.mono.log default,assembly,mono_log_level=debug,mono_log_mask=all
> adb logcat -G 16M
> adb logcat -c
rem Start your application here and wait for it to crash
> adb logcat -d > log.txt

And then attach log.txt here, thanks!

@jiro-san
Copy link
Author

@grendello Ok I will take those logs, but please note that the issue is reproducible in a "neutral" environment with the sample app I provided to dellis1972 and the OS image of Android 9.0 I provided in the first post.

@jiro-san
Copy link
Author

@grendello
Here is the log.txt following your procedure
log.txt

@grendello
Copy link
Contributor

@grendello Ok I will take those logs, but please note that the issue is reproducible in a "neutral" environment with the sample app I provided to dellis1972 and the OS image of Android 9.0 I provided in the first post.

I appreciate that, thank you, however I don't work on Windows and I don't have VS readily available for testing. Thus the fastest course of action was to ask you for the logs, thank you for providing them.

Would you be able to do another test? I wonder if the app can be debugged if these steps are followed:

  1. The app is removed from the test device, making sure no traces of data are left (it might be best to just create a new VM instance)
  2. VS is closed
  3. All the bin and obj folders are removed from the application source tree
  4. VS is started, project opened and Use Fast Deployment is turned off
  5. Debug the app

@grendello
Copy link
Contributor

I've managed to reproduce the issue locally, and the problem appears to be this error:

01-20 18:48:17.783  3147  3147 V Mono    : mono_pe_file_map: Error opening file mscorlib.dll (3): No such file or directory
...
01-20 18:48:17.784  3147  3147 W monodroid-assembly: Failed to load managed assembly 'mscorlib.dll'. Internal error
01-20 18:48:17.784  3147  3147 W monodroid-assembly: open_from_bundles: failed to load assembly mscorlib.dll
01-20 18:48:17.784  3147  3147 D Mono    : Assembly Loader probing location: '/mnt/jenkins/workspace/archive-mono/2020-02/android/debug/sdks/out/android-x86-debug/lib/mono/2.1/mscorlib.dll'.
01-20 18:48:17.784  3147  3147 E mono    : The assembly mscorlib.dll was not found or could not be loaded.
01-20 18:48:17.784  3147  3147 E mono    : It should have been installed in the `/mnt/jenkins/workspace/archive-mono/2020-02/android/debug/sdks/out/android-x86-debug/lib/mono/2.1/mscorlib.dll' directory.

Note the path from the build bot (/mnt/*) being used. Examining the mono_pe_file_map source code in Mono appears to confirm that, indeed, the function insists on finding the file on disk somewhere, ignoring the pointer we pass to Mono when mmapping the assembly from inside the apk. What's surprising here is that this code has been around for almost exactly 4 years and, apparently, it's been working fine all that time.

@lambdageek any idea who would be able to look into it on Mono side?

@lambdageek lambdageek self-assigned this Jan 20, 2023
@lambdageek
Copy link
Member

lambdageek commented Jan 20, 2023

The issue is that mono_assembly_load_from_full which is called by Xamarin.Android does not set MonoImageOpenStatus *status to MONO_IMAGE_OK (0) on success. It just leaves with whatever value it happened to have previously. In XA, when mono_assembly_load_from_full is called, the passed in status is an uninitialized local variable. so it gets some garbage stack value (usually 0).

The fix is that the underlying mono_assembly_request_load_from needs to set *status = MONO_IMAGE_OK in the successful case.


The mono_pe_file_map stuff is a red herring - the relevant code will fail gracefully if the path on disk is nonsensical. Nonetheless we should disable it on Android. (in mono/mono only; mono_image_load_time_date_stamp is gone on netcore)


This affects mono/mono main and 2020-02 and .NET 8 and .NET 7. Prior to .NET 7, XA did not check the status returned by mono_assembly_load_from_full, so as long as the return value was a non-null assembly, everything was fine.

I will open the various PRs

lambdageek added a commit to lambdageek/runtime that referenced this issue Jan 20, 2023
Emebedders that call `mono_assembly_load_from_full` may observe a
non-NULL return value and an uninitialized MonoImageOpenStatus, which
may lead to incorrect diagnostics in code like:

```
MonoImageOpenStatus status;
MonoAssembly *assembly = mono_assembly_load_from_full (image, name,
status, refonly);
if (!assembly || status != MONO_IMAGE_OK) {
   fprintf(stderr, "Failure due to: %s\n", mono_image_strerror (status));
   abort();
}
```
Which will print `Failure due to: Internal error`

Addresses dotnet/android#7658
github-actions bot pushed a commit to dotnet/runtime that referenced this issue Jan 20, 2023
Emebedders that call `mono_assembly_load_from_full` may observe a
non-NULL return value and an uninitialized MonoImageOpenStatus, which
may lead to incorrect diagnostics in code like:

```
MonoImageOpenStatus status;
MonoAssembly *assembly = mono_assembly_load_from_full (image, name,
status, refonly);
if (!assembly || status != MONO_IMAGE_OK) {
   fprintf(stderr, "Failure due to: %s\n", mono_image_strerror (status));
   abort();
}
```
Which will print `Failure due to: Internal error`

Addresses dotnet/android#7658
lambdageek added a commit to lambdageek/mono that referenced this issue Jan 20, 2023
Manual backport of dotnet/runtime#80949 to mono/mono

Emebedders that call `mono_assembly_load_from_full` may observe a non-NULL return value and an uninitialized MonoImageOpenStatus, which may lead to incorrect diagnostics in code like:

```
MonoImageOpenStatus status;
MonoAssembly *assembly = mono_assembly_load_from_full (image, name, status, refonly);
if (!assembly || status != MONO_IMAGE_OK) {
   fprintf(stderr, "Failure due to: %s\n", mono_image_strerror (status));
   abort();
}
```
Which will print `Failure due to: Internal error`

Addresses dotnet/android#7658
github-actions bot pushed a commit to mono/mono that referenced this issue Jan 20, 2023
Manual backport of dotnet/runtime#80949 to mono/mono

Emebedders that call `mono_assembly_load_from_full` may observe a non-NULL return value and an uninitialized MonoImageOpenStatus, which may lead to incorrect diagnostics in code like:

```
MonoImageOpenStatus status;
MonoAssembly *assembly = mono_assembly_load_from_full (image, name, status, refonly);
if (!assembly || status != MONO_IMAGE_OK) {
   fprintf(stderr, "Failure due to: %s\n", mono_image_strerror (status));
   abort();
}
```
Which will print `Failure due to: Internal error`

Addresses dotnet/android#7658
@lambdageek lambdageek added Area: Mono Runtime Mono-related issues: BCL bugs, AOT issues, etc. and removed Area: Debugger Issues using or interacting with the debugger. need-attention A xamarin-android contributor needs to review labels Jan 20, 2023
lewing pushed a commit to dotnet/runtime that referenced this issue Jan 21, 2023
Emebedders that call `mono_assembly_load_from_full` may observe a
non-NULL return value and an uninitialized MonoImageOpenStatus, which
may lead to incorrect diagnostics in code like:

```
MonoImageOpenStatus status;
MonoAssembly *assembly = mono_assembly_load_from_full (image, name,
status, refonly);
if (!assembly || status != MONO_IMAGE_OK) {
   fprintf(stderr, "Failure due to: %s\n", mono_image_strerror (status));
   abort();
}
```
Which will print `Failure due to: Internal error`

Addresses dotnet/android#7658
mdh1418 pushed a commit to mdh1418/runtime that referenced this issue Jan 24, 2023
Emebedders that call `mono_assembly_load_from_full` may observe a
non-NULL return value and an uninitialized MonoImageOpenStatus, which
may lead to incorrect diagnostics in code like:

```
MonoImageOpenStatus status;
MonoAssembly *assembly = mono_assembly_load_from_full (image, name,
status, refonly);
if (!assembly || status != MONO_IMAGE_OK) {
   fprintf(stderr, "Failure due to: %s\n", mono_image_strerror (status));
   abort();
}
```
Which will print `Failure due to: Internal error`

Addresses dotnet/android#7658
lambdageek added a commit to mono/mono that referenced this issue Jan 25, 2023
* [mono][loader] Set status on success

Manual backport of dotnet/runtime#80949 to mono/mono

Emebedders that call `mono_assembly_load_from_full` may observe a non-NULL return value and an uninitialized MonoImageOpenStatus, which may lead to incorrect diagnostics in code like:

```
MonoImageOpenStatus status;
MonoAssembly *assembly = mono_assembly_load_from_full (image, name, status, refonly);
if (!assembly || status != MONO_IMAGE_OK) {
   fprintf(stderr, "Failure due to: %s\n", mono_image_strerror (status));
   abort();
}
```
Which will print `Failure due to: Internal error`

Addresses dotnet/android#7658

* [loader] Make mono_image_laod_time_date_stamp a no-op on Android

Avoid an mmap that will fail since Android uses a custom
loader and the assemblies aren't on disk
carlossanlop pushed a commit to dotnet/runtime that referenced this issue Feb 8, 2023
Emebedders that call `mono_assembly_load_from_full` may observe a
non-NULL return value and an uninitialized MonoImageOpenStatus, which
may lead to incorrect diagnostics in code like:

```
MonoImageOpenStatus status;
MonoAssembly *assembly = mono_assembly_load_from_full (image, name,
status, refonly);
if (!assembly || status != MONO_IMAGE_OK) {
   fprintf(stderr, "Failure due to: %s\n", mono_image_strerror (status));
   abort();
}
```
Which will print `Failure due to: Internal error`

Addresses dotnet/android#7658

Co-authored-by: Aleksey Kliger <alklig@microsoft.com>
@jlinker7
Copy link

I am experiencing the same issue (though running on a custom android device running Android 7.1.1). Any idea when the proposed fix will be released in an update to Mono.Android?

@teo-tsirpanis
Copy link

Can we close it? The issue has been fixed and backported.

jonpryor added a commit to jonpryor/xamarin-android that referenced this issue May 19, 2023
Fixes: dotnet#7658

Changes: mono/mono@6dd9def...73df89a

  * mono/mono@73df89a73d2: Fix xar url again
  * mono/mono@36fd1837d0d: Switch back to original xar version
  * mono/mono@5bbc709e5dc: Fix xar download url
  * mono/mono@3cb47d8b4dc: [mono] Use `unsigned char` when computing UTF8 string hashes (mono/mono#21633)
  * mono/mono@a102a350e53: [2020-02] [mono][loader] Set status on success; avoid mmap on Android (mono/mono#21610)
  * mono/mono@74f85c2ac33: Fix url to gtksharp msi
  * mono/mono@2f7d584ad2b: Fix build on macOS 13 / Xcode 14 (mono/mono#21597)
  * mono/mono@03b09960787: Disable a failing test
@jonpryor
Copy link
Member

I can't believe I forgot about this! #8054

@teo-tsirpanis: .NET 7 has had the fix backported. Classic Xamarin.Android has not (oops), and #8054 does so.

@jlinker7
Copy link

@jonpryor Do we have any idea when this will be released? It's killing me not being able to debug my app...

jonpryor added a commit that referenced this issue May 25, 2023
Fixes: #7658

Changes: mono/mono@6dd9def...73df89a

  * mono/mono@73df89a73d2: Fix xar url again
  * mono/mono@36fd1837d0d: Switch back to original xar version
  * mono/mono@5bbc709e5dc: Fix xar download url
  * mono/mono@3cb47d8b4dc: [mono] Use `unsigned char` when computing UTF8 string hashes (mono/mono#21633)
  * mono/mono@a102a350e53: [2020-02] [mono][loader] Set status on success; avoid mmap on Android (mono/mono#21610)
  * mono/mono@74f85c2ac33: Fix url to gtksharp msi
  * mono/mono@2f7d584ad2b: Fix build on macOS 13 / Xcode 14 (mono/mono#21597)
  * mono/mono@03b09960787: Disable a failing test
@chrisz99
Copy link

@jonpryor Do we have any idea when this will be released? It's killing me not being able to debug my app...

Same

ThomasKuehne pushed a commit to ThomasKuehne/mono that referenced this issue Mar 23, 2024
* [mono][loader] Set status on success

Manual backport of dotnet/runtime#80949 to mono/mono

Emebedders that call `mono_assembly_load_from_full` may observe a non-NULL return value and an uninitialized MonoImageOpenStatus, which may lead to incorrect diagnostics in code like:

```
MonoImageOpenStatus status;
MonoAssembly *assembly = mono_assembly_load_from_full (image, name, status, refonly);
if (!assembly || status != MONO_IMAGE_OK) {
   fprintf(stderr, "Failure due to: %s\n", mono_image_strerror (status));
   abort();
}
```
Which will print `Failure due to: Internal error`

Addresses dotnet/android#7658

* [loader] Make mono_image_laod_time_date_stamp a no-op on Android

Avoid an mmap that will fail since Android uses a custom
loader and the assemblies aren't on disk
@jpobst
Copy link
Contributor

jpobst commented May 17, 2024

This seems to be fixed in .NET 8+.

@jpobst jpobst closed this as completed May 17, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jun 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Area: Mono Runtime Mono-related issues: BCL bugs, AOT issues, etc.
Projects
None yet
Development

No branches or pull requests

9 participants