2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
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"
35 static int injectHeaptrackLibrary()
37 typedef int inject_func();
41 inject_func *inject = nullptr;
42 const char *inject_sym = "dotnet_launcher_inject";
44 _INFO("Inject %s library", HT_INJECTION_LIB_PATH);
46 // Current implementation of heaptrack CLR profiler requires RTLD_GLOBAL
47 lib = dlopen(HT_INJECTION_LIB_PATH, RTLD_NOW | RTLD_GLOBAL);
49 _ERR("%s", dlerror());
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);
63 static int injectCoreprofilerConfig()
65 if (!exist(CP_INHECTION_CONFIG)) {
69 _INFO("Inject %s", CP_INHECTION_CONFIG);
71 std::ifstream ifs(CP_INHECTION_CONFIG);
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);
78 } else if (!strcmp(get_conf.c_str(), "PROF_USE_ALTERNATIVE_SAMPLING_EXECUTION_TRACING=0")) {
79 setenv("COMPlus_ZapDisable", "1", 1);
88 int checkProfilerInjection()
92 env = getenv("CORECLR_PROFILER_PATH");
97 _INFO("##### Perform profiler injection #########");
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 #########");
105 } else if (!strcmp(env, CP_LIB_PATH)) {
106 if (injectCoreprofilerConfig() != 0) {
107 _INFO("##### Coreprofiler injection failed #########");
111 _INFO("##### Injection profiler finished #########");