Add COMPlus_DebugWriteToStdErr=1 to environment.
[platform/core/dotnet/launcher.git] / NativeLauncher / launcher / dotnet / dotnet_launcher.cc
index c7e28e8..2f946c9 100644 (file)
@@ -33,6 +33,7 @@
 #include <storage.h>
 #include <app_common.h>
 
+#include "injection.h"
 #include "utils.h"
 #include "log.h"
 #include "launcher.h"
@@ -127,12 +128,19 @@ struct sigaction sig_abrt_new;
 struct sigaction sig_abrt_old;
 
 static bool checkOnSigabrt = false;
+static bool checkOnTerminate = false;
 
 static void onSigabrt(int signum)
 {
        // use unused variable to avoid build warning
        ssize_t ret = write(STDERR_FILENO, "onSigabrt called\n", 17);
 
+       if (checkOnTerminate) {
+               ret = write(STDERR_FILENO, "onSigabrt called while terminate. go to exit\n", 45);
+               _unused(ret);
+               exit(0);
+       }
+
        if (checkOnSigabrt) {
                ret = write(STDERR_FILENO, "onSigabrt called again. go to exit\n", 35);
                _unused(ret);
@@ -250,6 +258,14 @@ CoreRuntime::~CoreRuntime()
 
 int CoreRuntime::initialize(bool standalone)
 {
+       // checkInjection checks dotnet-launcher run mode,
+       // if it contains DOTNET_LAUNCHER_INJECT variable, it injects library.
+       // At the moment, this mechanism is used only when the Memory Profiler is started.
+       int res = checkInjection();
+       if (res != 0) {
+               _ERR("Failed to initnialize Memory Profiler");
+               return -1;
+       }
 #define __XSTR(x) #x
 #define __STR(x) __XSTR(x)
 
@@ -278,6 +294,9 @@ int CoreRuntime::initialize(bool standalone)
        if (!standalone)
                putenv(const_cast<char *>("COMPlus_EnableDiagnostics=0"));
 
+       // Write Debug.WriteLine to stderr
+       putenv(const_cast<char *>("COMPlus_DebugWriteToStdErr=1"));
+
        // read string from external file and set them to environment value.
        setEnvFromFile();
 
@@ -403,6 +422,13 @@ bool CoreRuntime::initializeCoreClr(const char* appId,
 
 void CoreRuntime::dispose()
 {
+       // call plugin finalize function to notify finalize to plugin
+       // dlclose shoud be done after coreclr shutdown to avoid breaking signal chain
+       pluginFinalize();
+
+       // ignore the signal generated by an exception that occurred during shutdown
+       checkOnTerminate = true;
+
        if (__hostHandle != nullptr) {
                int st = shutdown(__hostHandle, __domainId);
                if (st < 0)
@@ -473,7 +499,10 @@ int CoreRuntime::launch(const char* appId, const char* root, const char* path, i
 
                if (!setEnvironmentVariable(u"XDG_DATA_HOME", envval)) {
                        _ERR("Failed to set XDG_DATA_HOME");
+
                }
+
+               free(localDataPath);
        }
 
        pluginBeforeExecute();