[mono][jit] Emit profiler enter after jit attach; leave before detach (#44345)
authorAleksey Kliger (λgeek) <alklig@microsoft.com>
Tue, 10 Nov 2020 20:11:20 +0000 (15:11 -0500)
committerGitHub <noreply@github.com>
Tue, 10 Nov 2020 20:11:20 +0000 (15:11 -0500)
commit04537c84cdaa25429f4abb1ecf4a77efe6aa2182
tree540aac7ffe7fb2125e987688943c1746cbd61a94
parentdcb04d7184859a25585a972e01f2e4995dba622d
[mono][jit] Emit profiler enter after jit attach; leave before detach (#44345)

* [jit] Emit profiler enter after jit attach; leave before detach

profiler enter code (such as mono_trace_enter_method) must not be
called before a thread attaches to the runtime - otherwise calls like
`mono_domain_get()` will return NULL unexpectedly and then crash.
When detaching, we should call the profiler leave code before the
detach, and suppress it on return.

Simple repro (that relies on pal_signal.c SignalHandlerLoop - which is
a background thread from System.Native that calls back into managed
when there's a SIGCHLD): compile and run this program with `MONO_ENV_OPTIONS=--trace`

```csharp
using System;
using System.Diagnostics;

namespace Repro
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            using (Process myProcess = new Process())
            {
                    myProcess.StartInfo.UseShellExecute = true;
                    myProcess.StartInfo.FileName = "echo";
                    myProcess.StartInfo.Arguments = "hello from shell";
                    myProcess.StartInfo.CreateNoWindow = true;
                    myProcess.Start();
                    myProcess.WaitForExit();
            }
            Console.ReadKey ();
        }
    }
}
```
src/mono/mono/mini/method-to-ir.c