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"
22 static const char* CLR_PATH = "/usr/share/dotnet.tizen/netcoreapp";
23 static const char* TOOL_PATH = "/home/owner/share/.dotnet/tools";
27 "Execute a .NET application or command.\n\n"
28 "Usage: dotnet [options] [path-to-executable] [arguments]\n"
29 "Usage: dotnet [command] [arguments]\n\n"
31 "-h, --help show this help message\n"
32 "--clr-path <path> path to libcoreclr.so and runtime assemblies\n"
33 "--tool-path <path> path to the tool installation directory\n"
34 "--additionalprobingpath <path> path containing assemblies to probe for\n"
35 "--globalizationinvariant run in globalization invariant mode\n\n"
37 "counters monitor or collect performance counters\n"
38 "dump capture or analyze a coredump\n"
39 "gcdump capture a heapdump\n"
40 "trace collect or convert a diagnostic event trace\n"
41 "stack reports the managed stacks\n\n");
44 int main(int argc, const char* argv[]) {
46 // libunwind library is used to unwind stack frame, but libunwind for ARM
47 // does not support ARM vfpv3/NEON registers in DWARF format correctly.
48 // Therefore let's disable stack unwinding using DWARF information
49 // See https://github.com/dotnet/runtime/issues/6479
51 // libunwind use following methods to unwind stack frame.
52 // UNW_ARM_METHOD_ALL 0xFF
53 // UNW_ARM_METHOD_DWARF 0x01
54 // UNW_ARM_METHOD_FRAME 0x02
55 // UNW_ARM_METHOD_EXIDX 0x04
56 putenv(const_cast<char*>("UNW_ARM_UNWIND_METHOD=6"));
67 std::string clrFilesPath(CLR_PATH);
68 std::string toolDllsPath(TOOL_PATH);
70 std::string managedAssemblyPath;
71 std::string additionalProbingPath;
72 bool globalizationInvariant = false;
75 std::string arg(argv[0]);
77 if (arg == "-?" || arg == "-h" || arg == "--help") {
80 } else if (arg == "--clr-path" && argc > 1) {
81 clrFilesPath = argv[1];
84 } else if (arg == "--tool-path" && argc > 1) {
85 toolDllsPath = argv[1];
88 } else if (arg == "--additionalprobingpath" && argc > 1) {
89 additionalProbingPath = getAbsolutePath(argv[1]);
92 } else if (arg == "--globalizationinvariant") {
93 globalizationInvariant = true;
94 } else if ((arg == "--runtimeconfig" || arg == "--depsfile") && argc > 1) {
95 // Just for compatibility with corefx tests.
96 // See ParseArguments() in coreclr/hosts/unixcorerun/corerun.cpp.
99 } else if (arg.at(0) == '-') {
100 fprintf(stderr, "Unknown option %s.\n", argv[0]);
103 } else if (isManagedAssembly(arg) || isNativeImage(arg)) {
105 fprintf(stderr, "The specified file does not exist.\n");
108 managedAssemblyPath = arg;
112 } else if (arg == "exec") {
113 // 'dotnet' and 'dotnet exec' can be alternatively used.
114 } else if (arg == "tool") {
115 fprintf(stderr, "This command is not currently supported.\n");
118 managedAssemblyPath = toolDllsPath + "/dotnet-" + arg + ".dll";
120 if (!isFile(managedAssemblyPath)) {
122 "Could not execute because dotnet-%s does not exist.\n"
123 "Go to https://developer.samsung.com/tizen to learn how to install tools.\n\n", argv[0]);
127 // Implicit compatibility mode for System.CommandLine.
128 std::string termValue(getenv("TERM"));
129 if (termValue == "linux") {
130 setenv("TERM", "xterm", 1);
142 std::string currentExeAbsolutePath = getAbsolutePath("/proc/self/exe");
143 if (currentExeAbsolutePath.empty()) {
144 fprintf(stderr, "Failed to get the current executable's absolute path.\n");
148 std::string clrFilesAbsolutePath = getAbsolutePath(clrFilesPath);
149 if (clrFilesAbsolutePath.empty()) {
150 fprintf(stderr, "Failed to resolve the full path to the CLR files.\n");
154 std::string managedAssemblyAbsolutePath = getAbsolutePath(managedAssemblyPath);
155 if (managedAssemblyAbsolutePath.empty()) {
156 fprintf(stderr, "Failed to get the managed assembly's absolute path.\n");
160 std::string coreclrLibPath(clrFilesAbsolutePath + "/libcoreclr.so");
161 std::string appPath = getBaseName(managedAssemblyAbsolutePath);
162 std::string nativeDllSearchDirs(appPath);
163 nativeDllSearchDirs += ":" + additionalProbingPath;
164 nativeDllSearchDirs += ":" + clrFilesAbsolutePath;
166 std::string tpaList(managedAssemblyAbsolutePath);
167 // For now we don't parse .deps.json file but let application DLLs can override runtime DLLs.
168 std::vector<std::string> tpaDirs = { appPath, additionalProbingPath, clrFilesAbsolutePath };
169 addAssembliesFromDirectories(tpaDirs, tpaList);
171 void* coreclrLib = dlopen(coreclrLibPath.c_str(), RTLD_NOW | RTLD_LOCAL);
172 if (coreclrLib == nullptr) {
173 const char* error = dlerror();
174 fprintf(stderr, "dlopen failed to open the libcoreclr.so with error %s\n", error);
178 coreclr_initialize_ptr initializeCoreCLR = (coreclr_initialize_ptr)dlsym(coreclrLib, "coreclr_initialize");
179 coreclr_execute_assembly_ptr executeAssembly = (coreclr_execute_assembly_ptr)dlsym(coreclrLib, "coreclr_execute_assembly");
180 coreclr_shutdown_ptr shutdownCoreCLR = (coreclr_shutdown_ptr)dlsym(coreclrLib, "coreclr_shutdown");
182 if (initializeCoreCLR == nullptr) {
183 fprintf(stderr, "Function coreclr_initialize not found in the libcoreclr.so\n");
185 } else if (executeAssembly == nullptr) {
186 fprintf(stderr, "Function coreclr_execute_assembly not found in the libcoreclr.so\n");
188 } else if (shutdownCoreCLR == nullptr) {
189 fprintf(stderr, "Function coreclr_shutdown not found in the libcoreclr.so\n");
193 const char* propertyKeys[] = {
194 "TRUSTED_PLATFORM_ASSEMBLIES",
197 "NATIVE_DLL_SEARCH_DIRECTORIES",
198 "System.Globalization.Invariant",
200 const char* propertyValues[] = {
204 nativeDllSearchDirs.c_str(),
205 globalizationInvariant ? "true" : "false",
209 unsigned int domainId;
211 int st = initializeCoreCLR(
212 currentExeAbsolutePath.c_str(),
214 sizeof(propertyKeys) / sizeof(propertyKeys[0]),
221 fprintf(stderr, "coreclr_initialize failed - status: 0x%08x\n", st);
225 // Set the current process's command name.
226 std::string managedAssemblyName = getFileName(managedAssemblyAbsolutePath);
227 setCmdName(managedAssemblyName);
231 st = executeAssembly(
236 managedAssemblyAbsolutePath.c_str(),
237 (unsigned int*)&exitCode);
240 fprintf(stderr, "coreclr_execute_assembly failed - status: 0x%08x\n", st);
244 st = shutdownCoreCLR(hostHandle, domainId);
246 fprintf(stderr, "coreclr_shutdown failed - status: 0x%08x\n", st);