2 * Copyright (c) 2020 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.
19 #include "coreclr_host.h"
23 static const char* CLR_PATH = "/usr/share/dotnet.tizen/netcoreapp";
24 static const char* TOOL_PATH = "/home/owner/share/.dotnet/tools";
25 static const char* DIAGNOSTICS_TOOL_PATH = "/home/owner/share/tmp/sdk_tools/coreclr-diagnostics";
29 "Execute a .NET application or command.\n\n"
30 "Usage: dotnet [options] [path-to-executable] [arguments]\n"
31 "Usage: dotnet [command] [arguments]\n\n"
33 "-h, --help show this help message\n"
34 "--clr-path <path> path to libcoreclr.so and runtime assemblies\n"
35 "--tool-path <path> path to the tool installation directory\n"
36 "--additionalprobingpath <path> path containing assemblies to probe for\n"
37 "--globalizationinvariant run in globalization invariant mode\n\n"
39 "counters monitor or collect performance counters\n"
40 "dump capture or analyze a coredump\n"
41 "gcdump capture a heapdump\n"
42 "trace collect or convert a diagnostic event trace\n"
43 "stack reports the managed stacks\n");
46 int main(int argc, const char* argv[]) {
48 // libunwind library is used to unwind stack frame, but libunwind for ARM
49 // does not support ARM vfpv3/NEON registers in DWARF format correctly.
50 // Therefore let's disable stack unwinding using DWARF information
51 // See https://github.com/dotnet/runtime/issues/6479
53 // libunwind use following methods to unwind stack frame.
54 // UNW_ARM_METHOD_ALL 0xFF
55 // UNW_ARM_METHOD_DWARF 0x01
56 // UNW_ARM_METHOD_FRAME 0x02
57 // UNW_ARM_METHOD_EXIDX 0x04
58 putenv(const_cast<char*>("UNW_ARM_UNWIND_METHOD=6"));
69 std::string clrFilesPath(CLR_PATH);
70 std::string toolDllsPath(clrFilesPath + "/SOS");
72 std::string managedAssemblyPath;
73 std::string additionalProbingPath;
74 bool globalizationInvariant = false;
77 std::string arg(argv[0]);
79 if (arg == "-?" || arg == "-h" || arg == "--help") {
82 } else if (arg == "--clr-path" && argc > 1) {
83 clrFilesPath = argv[1];
86 } else if (arg == "--tool-path" && argc > 1) {
87 toolDllsPath = argv[1];
90 } else if (arg == "--additionalprobingpath" && argc > 1) {
91 additionalProbingPath = getAbsolutePath(argv[1]);
94 } else if (arg == "--globalizationinvariant") {
95 globalizationInvariant = true;
96 } else if ((arg == "--runtimeconfig" || arg == "--depsfile") && argc > 1) {
97 // Just for compatibility with corefx tests.
98 // See ParseArguments() in coreclr/hosts/unixcorerun/corerun.cpp.
101 } else if (arg.at(0) == '-') {
102 _SERR("Unknown option %s.", argv[0]);
105 } else if (isManagedAssembly(arg) || isNativeImage(arg)) {
107 _SERR("The specified file does not exist.");
110 managedAssemblyPath = arg;
114 } else if (arg == "exec") {
115 // 'dotnet' and 'dotnet exec' can be alternatively used.
116 } else if (arg == "tool") {
117 _SERR("This command is not currently supported.");
120 std::string toolDll = "/dotnet-" + arg + ".dll";
121 std::string searchToolPath1 = toolDllsPath + toolDll;
122 std::string searchToolPath2 = std::string(TOOL_PATH) + toolDll;
123 std::string searchToolPath3 = std::string(DIAGNOSTICS_TOOL_PATH) + toolDll;
124 if (isFile(searchToolPath1)) {
125 managedAssemblyPath = searchToolPath1;
126 } else if (isFile(searchToolPath2)) {
127 managedAssemblyPath = searchToolPath2;
128 } else if (isFile(searchToolPath3)) {
129 managedAssemblyPath = searchToolPath3;
132 "Could not execute because dotnet-%s does not exist.\n"
133 "Go to https://developer.samsung.com/tizen to learn how to install tools.\n", argv[0]);
137 // Implicit compatibility mode for System.CommandLine.
138 std::string termValue(getenv("TERM"));
139 if (termValue == "linux") {
140 setenv("TERM", "xterm", 1);
152 std::string currentExeAbsolutePath = getAbsolutePath("/proc/self/exe");
153 if (currentExeAbsolutePath.empty()) {
154 _SERR("Failed to get the current executable's absolute path.");
158 std::string clrFilesAbsolutePath = getAbsolutePath(clrFilesPath);
159 if (clrFilesAbsolutePath.empty()) {
160 _SERR("Failed to resolve the full path to the CLR files.");
164 std::string managedAssemblyAbsolutePath = getAbsolutePath(managedAssemblyPath);
165 if (managedAssemblyAbsolutePath.empty()) {
166 _SERR("Failed to get the managed assembly's absolute path.");
170 std::string coreclrLibPath(clrFilesAbsolutePath + "/libcoreclr.so");
171 std::string appPath = getBaseName(managedAssemblyAbsolutePath);
172 std::string nativeDllSearchDirs(appPath);
173 nativeDllSearchDirs += ":" + additionalProbingPath;
174 nativeDllSearchDirs += ":" + clrFilesAbsolutePath;
176 std::string tpaList(managedAssemblyAbsolutePath);
177 // For now we don't parse .deps.json file but let application DLLs can override runtime DLLs.
178 std::vector<std::string> tpaDirs = { appPath, additionalProbingPath, clrFilesAbsolutePath };
179 addAssembliesFromDirectories(tpaDirs, tpaList);
181 void* coreclrLib = dlopen(coreclrLibPath.c_str(), RTLD_NOW | RTLD_LOCAL);
182 if (coreclrLib == nullptr) {
183 const char* error = dlerror();
184 _SERR("dlopen failed to open the libcoreclr.so with error %s", error);
188 coreclr_initialize_ptr initializeCoreCLR = (coreclr_initialize_ptr)dlsym(coreclrLib, "coreclr_initialize");
189 coreclr_execute_assembly_ptr executeAssembly = (coreclr_execute_assembly_ptr)dlsym(coreclrLib, "coreclr_execute_assembly");
190 coreclr_shutdown_ptr shutdownCoreCLR = (coreclr_shutdown_ptr)dlsym(coreclrLib, "coreclr_shutdown");
192 if (initializeCoreCLR == nullptr) {
193 _SERR("Function coreclr_initialize not found in the libcoreclr.so");
195 } else if (executeAssembly == nullptr) {
196 _SERR("Function coreclr_execute_assembly not found in the libcoreclr.so");
198 } else if (shutdownCoreCLR == nullptr) {
199 _SERR("Function coreclr_shutdown not found in the libcoreclr.so");
203 bool ncdbStartupHook = isNCDBStartupHookProvided();
205 const char* propertyKeys[] = {
206 "TRUSTED_PLATFORM_ASSEMBLIES",
209 "NATIVE_DLL_SEARCH_DIRECTORIES",
210 "System.Globalization.Invariant",
211 ncdbStartupHook ? "STARTUP_HOOKS" : "" // must be the last one
213 const char* propertyValues[] = {
217 nativeDllSearchDirs.c_str(),
218 globalizationInvariant ? "true" : "false",
219 ncdbStartupHook ? getNCDBStartupHook() : "" // must be the last one
223 unsigned int domainId;
225 int st = initializeCoreCLR(
226 currentExeAbsolutePath.c_str(),
228 sizeof(propertyKeys) / sizeof(propertyKeys[0]) - (ncdbStartupHook ? 0 : 1),
235 _SERR("coreclr_initialize failed - status: 0x%08x", st);
239 // Set the current process's command name.
240 std::string managedAssemblyName = getFileName(managedAssemblyAbsolutePath);
241 setCmdName(managedAssemblyName);
245 st = executeAssembly(
250 managedAssemblyAbsolutePath.c_str(),
251 (unsigned int*)&exitCode);
254 _SERR("coreclr_execute_assembly failed - status: 0x%08x", st);
258 st = shutdownCoreCLR(hostHandle, domainId);
260 _SERR("coreclr_shutdown failed - status: 0x%08x", st);