We have Xamarin.Android and Xamarin.iOS apps that can catch and report native crashes by installing their own signal handlers like this:

Mono.Runtime.RemoveSignalHandlers();  
    NativeCrashListener.Initialize();  
finally  
    Mono.Runtime.InstallSignalHandlers();  

Migrating from Xamarin to .NET 6 I noticed that the Mono.Runtime type is not accessible any more, even though the Mono runtime is still there for Android and iOS, so obviously, you can't remove and install the handlers like this.

Mono suggests the following alternative:

IntPtr sigbus = Marshal.AllocHGlobal (512);  
IntPtr sigsegv = Marshal.AllocHGlobal (512);  
// Store Mono's SIGSEGV and SIGBUS handlers  
sigaction (Signal.SIGBUS, IntPtr.Zero, sigbus);  
sigaction (Signal.SIGSEGV, IntPtr.Zero, sigsegv);  
// Enable crash reporting libraries  
EnableCrashReporting ();  
// Restore Mono SIGSEGV and SIGBUS handlers  
sigaction (Signal.SIGBUS, sigbus, IntPtr.Zero);  
sigaction (Signal.SIGSEGV, sigsegv, IntPtr.Zero);  
Marshal.FreeHGlobal (sigbus);  
Marshal.FreeHGlobal (sigsegv);  

We tried both the way Mono recommends and getting rid of the remove/install code at all on our Xamarin apps to see if it makes a difference. Turns out that it does, and the number of crashes that we get significantly differs in both cases. It is hard to say which way is more correct though, since you can only see the difference on big numbers, but in any case the behavior is not the same.

I would still want to apply something similar to the .NET 6 apps, and I wonder whether there is an alternative API I didn't find. I know that there is some API to work with POSIX signals now in .NET 6, so maybe I'm just missing something.

Can anyone advise an alternative or say with confidence that .NET 6 doesn't need such handlers detach at all?

I know that there is some API to work with POSIX signals now in .NET 6, so maybe I'm just missing something.

In .NET 6, you could try this class: PosixSignalRegistration Class.

In .NET 6, you could try this class: PosixSignalRegistration Class.

Thanks for the suggestion. Although, I don't think it's going to work for me, because, first, it doesn't seem to handle all the signals I need, including SIGBUS and SIGSEGV, and second, it doesn't support Android or iOS at all:

[System.Runtime.Versioning.UnsupportedOSPlatform("android")]  
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]  
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]  
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]  
public static System.Runtime.InteropServices.PosixSignalRegistration Create (System.Runtime.InteropServices.PosixSignal signal, Action<System.Runtime.InteropServices.PosixSignalContext> handler);