Sets the ZapDisable env according to the Alternative Sampling value of the coreprofiler
[platform/core/dotnet/launcher.git] / NativeLauncher / launcher / lib / injection.cc
index 819af59..2c722a4 100644 (file)
  */
 
 #include "log.h"
+#include "utils.h"
 
 #include <cstdlib>
 #include <cstring>
 
 #include <dlfcn.h>
+#include <fstream>
+#include <string>
 
 #define HT_PATH "/home/owner/share/tmp/sdk_tools/heaptrack/"
 #define HT_LIB_PATH HT_PATH "libprofiler.so"
 #define HT_INJECTION_LIB_PATH HT_PATH "libheaptrack_inject.so"
 
-static int injectLibrary(const char path[])
+#define CP_PATH "/home/owner/share/tmp/sdk_tools/coreprofiler/"
+#define CP_LIB_PATH CP_PATH "libcoreprof.so"
+#define CP_INHECTION_CONFIG CP_PATH "profiler.config"
+
+static int injectHeaptrackLibrary()
 {
        typedef int inject_func();
 
@@ -34,10 +41,10 @@ static int injectLibrary(const char path[])
        inject_func *inject = nullptr;
        const char *inject_sym = "dotnet_launcher_inject";
 
-       _INFO("Inject %s library", path);
+       _INFO("Inject %s library", HT_INJECTION_LIB_PATH);
 
        // Current implementation of heaptrack CLR profiler requires RTLD_GLOBAL
-       lib = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
+       lib = dlopen(HT_INJECTION_LIB_PATH, RTLD_NOW | RTLD_GLOBAL);
        if (lib == nullptr) {
                _ERR("%s", dlerror());
                return res;
@@ -45,7 +52,7 @@ static int injectLibrary(const char path[])
 
        inject = reinterpret_cast<inject_func*>(dlsym(lib, inject_sym));
        if (inject == nullptr) {
-               _ERR("%s is not found in the %s", inject_sym, path);
+               _ERR("%s is not found in the %s", inject_sym, HT_INJECTION_LIB_PATH);
                return res;
        }
 
@@ -53,7 +60,32 @@ static int injectLibrary(const char path[])
        return res;
 }
 
-int checkInjection()
+static int injectCoreprofilerConfig()
+{
+       if (!exist(CP_INHECTION_CONFIG)) {
+               return -1;
+       }
+
+       _INFO("Inject %s", CP_INHECTION_CONFIG);
+
+       std::ifstream ifs(CP_INHECTION_CONFIG);
+       std::string get_conf;
+       if (ifs.is_open()) {
+               while (getline(ifs, get_conf)) {
+                       if (!strcmp(get_conf.c_str(), "PROF_USE_ALTERNATIVE_SAMPLING_EXECUTION_TRACING=1")) {
+                               setenv("COMPlus_ZapDisable", "0", 1);
+                               break;
+                       } else if (!strcmp(get_conf.c_str(), "PROF_USE_ALTERNATIVE_SAMPLING_EXECUTION_TRACING=0")) {
+                               setenv("COMPlus_ZapDisable", "1", 1);
+                               break;
+                       }
+               }
+               ifs.close();
+       }
+       return 0;
+}
+
+int checkProfilerInjection()
 {
        char *env = nullptr;
 
@@ -62,18 +94,21 @@ int checkInjection()
                return 0;
        }
 
-       // At the moment, this mechanism is used only when the Memory Profiler is started.
-       if (strcmp(env, HT_LIB_PATH) != 0) {
-               return 0;
-       }
-
-       _INFO("##### Perform injection #########");
+       _INFO("##### Perform profiler injection #########");
 
-       if (injectLibrary(HT_INJECTION_LIB_PATH) != 0) {
-               _INFO("##### Injection failed #########");
-               return -1;
+       // At the moment, this mechanism is used only when the Memory Profiler is started.
+       if (!strcmp(env, HT_LIB_PATH)) {
+               if (injectHeaptrackLibrary() != 0) {
+                       _INFO("##### Heaptrack injection failed #########");
+                       return -1;
+               }
+       } else if (!strcmp(env, CP_LIB_PATH)) {
+               if (injectCoreprofilerConfig() != 0) {
+                       _INFO("##### Coreprofiler injection failed #########");
+                       return -1;
+               }
        }
-
-       _INFO("##### Injection finished #########");
+       _INFO("##### Injection profiler finished #########");
        return 0;
 }
+