Sets the ZapDisable env according to the Alternative Sampling value of the coreprofiler
[platform/core/dotnet/launcher.git] / NativeLauncher / launcher / lib / injection.cc
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "log.h"
18 #include "utils.h"
19
20 #include <cstdlib>
21 #include <cstring>
22
23 #include <dlfcn.h>
24 #include <fstream>
25 #include <string>
26
27 #define HT_PATH "/home/owner/share/tmp/sdk_tools/heaptrack/"
28 #define HT_LIB_PATH HT_PATH "libprofiler.so"
29 #define HT_INJECTION_LIB_PATH HT_PATH "libheaptrack_inject.so"
30
31 #define CP_PATH "/home/owner/share/tmp/sdk_tools/coreprofiler/"
32 #define CP_LIB_PATH CP_PATH "libcoreprof.so"
33 #define CP_INHECTION_CONFIG CP_PATH "profiler.config"
34
35 static int injectHeaptrackLibrary()
36 {
37         typedef int inject_func();
38
39         int res = -1;
40         void *lib = nullptr;
41         inject_func *inject = nullptr;
42         const char *inject_sym = "dotnet_launcher_inject";
43
44         _INFO("Inject %s library", HT_INJECTION_LIB_PATH);
45
46         // Current implementation of heaptrack CLR profiler requires RTLD_GLOBAL
47         lib = dlopen(HT_INJECTION_LIB_PATH, RTLD_NOW | RTLD_GLOBAL);
48         if (lib == nullptr) {
49                 _ERR("%s", dlerror());
50                 return res;
51         }
52
53         inject = reinterpret_cast<inject_func*>(dlsym(lib, inject_sym));
54         if (inject == nullptr) {
55                 _ERR("%s is not found in the %s", inject_sym, HT_INJECTION_LIB_PATH);
56                 return res;
57         }
58
59         res = inject();
60         return res;
61 }
62
63 static int injectCoreprofilerConfig()
64 {
65         if (!exist(CP_INHECTION_CONFIG)) {
66                 return -1;
67         }
68
69         _INFO("Inject %s", CP_INHECTION_CONFIG);
70
71         std::ifstream ifs(CP_INHECTION_CONFIG);
72         std::string get_conf;
73         if (ifs.is_open()) {
74                 while (getline(ifs, get_conf)) {
75                         if (!strcmp(get_conf.c_str(), "PROF_USE_ALTERNATIVE_SAMPLING_EXECUTION_TRACING=1")) {
76                                 setenv("COMPlus_ZapDisable", "0", 1);
77                                 break;
78                         } else if (!strcmp(get_conf.c_str(), "PROF_USE_ALTERNATIVE_SAMPLING_EXECUTION_TRACING=0")) {
79                                 setenv("COMPlus_ZapDisable", "1", 1);
80                                 break;
81                         }
82                 }
83                 ifs.close();
84         }
85         return 0;
86 }
87
88 int checkProfilerInjection()
89 {
90         char *env = nullptr;
91
92         env = getenv("CORECLR_PROFILER_PATH");
93         if (env == nullptr) {
94                 return 0;
95         }
96
97         _INFO("##### Perform profiler injection #########");
98
99         // At the moment, this mechanism is used only when the Memory Profiler is started.
100         if (!strcmp(env, HT_LIB_PATH)) {
101                 if (injectHeaptrackLibrary() != 0) {
102                         _INFO("##### Heaptrack injection failed #########");
103                         return -1;
104                 }
105         } else if (!strcmp(env, CP_LIB_PATH)) {
106                 if (injectCoreprofilerConfig() != 0) {
107                         _INFO("##### Coreprofiler injection failed #########");
108                         return -1;
109                 }
110         }
111         _INFO("##### Injection profiler finished #########");
112         return 0;
113 }
114