Changed the path of diagnostics tool (#455)
[platform/core/dotnet/launcher.git] / NativeLauncher / launcher / exec / corerun.cc
index 2ac5d63..29b7234 100644 (file)
 #include <dlfcn.h>
 #include <string>
 #include "coreclr_host.h"
+#include "log.h"
 #include "utils.h"
 
 static const char* CLR_PATH = "/usr/share/dotnet.tizen/netcoreapp";
 static const char* TOOL_PATH = "/home/owner/share/.dotnet/tools";
 
 void DisplayUsage() {
-       fprintf(stdout,
+       _SOUT(
                "Execute a .NET application or command.\n\n"
                "Usage: dotnet [options] [path-to-executable] [arguments]\n"
                "Usage: dotnet [command] [arguments]\n\n"
@@ -37,7 +38,8 @@ void DisplayUsage() {
                "counters       monitor or collect performance counters\n"
                "dump           capture or analyze a coredump\n"
                "gcdump         capture a heapdump\n"
-               "trace          collect or convert a diagnostic event trace\n\n");
+               "trace          collect or convert a diagnostic event trace\n"
+               "stack          reports the managed stacks\n");
 }
 
 int main(int argc, const char* argv[]) {
@@ -64,7 +66,7 @@ int main(int argc, const char* argv[]) {
        }
 
        std::string clrFilesPath(CLR_PATH);
-       std::string toolDllsPath(TOOL_PATH);
+       std::string toolDllsPath(clrFilesPath + "/SOS");
 
        std::string managedAssemblyPath;
        std::string additionalProbingPath;
@@ -85,7 +87,7 @@ int main(int argc, const char* argv[]) {
                        argc--;
                        argv++;
                } else if (arg == "--additionalprobingpath" && argc > 1) {
-                       additionalProbingPath = absolutePath(argv[1]);
+                       additionalProbingPath = getAbsolutePath(argv[1]);
                        argc--;
                        argv++;
                } else if (arg == "--globalizationinvariant") {
@@ -96,12 +98,12 @@ int main(int argc, const char* argv[]) {
                        argc--;
                        argv++;
                } else if (arg.at(0) == '-') {
-                       fprintf(stderr, "Unknown option %s.\n", argv[0]);
+                       _SERR("Unknown option %s.", argv[0]);
                        DisplayUsage();
                        return -1;
                } else if (isManagedAssembly(arg) || isNativeImage(arg)) {
-                       if (!isFileExist(arg)) {
-                               fprintf(stderr, "The specified file does not exist.\n");
+                       if (!isFile(arg)) {
+                               _SERR("The specified file does not exist.");
                                return -1;
                        }
                        managedAssemblyPath = arg;
@@ -111,15 +113,20 @@ int main(int argc, const char* argv[]) {
                } else if (arg == "exec") {
                        // 'dotnet' and 'dotnet exec' can be alternatively used.
                } else if (arg == "tool") {
-                       fprintf(stderr, "This command is not currently supported.\n");
+                       _SERR("This command is not currently supported.");
                        return -1;
                } else {
-                       managedAssemblyPath = toolDllsPath + "/dotnet-" + arg + ".dll";
-
-                       if (!isFileExist(managedAssemblyPath)) {
-                               fprintf(stderr,
+                       std::string toolDll = "/dotnet-" + arg + ".dll";
+                       std::string searchToolPath1 = toolDllsPath + toolDll;
+                       std::string searchToolPath2 = std::string(TOOL_PATH) + toolDll;
+                       if (isFile(searchToolPath1)) {
+                               managedAssemblyPath = searchToolPath1;
+                       } else if (isFile(searchToolPath2)) {
+                               managedAssemblyPath = searchToolPath2;
+                       } else {
+                               _SERR(
                                        "Could not execute because dotnet-%s does not exist.\n"
-                                       "Go to https://developer.samsung.com/tizen to learn how to install tools.\n\n", argv[0]);
+                                       "Go to https://developer.samsung.com/tizen to learn how to install tools.\n", argv[0]);
                                return -1;
                        }
 
@@ -138,26 +145,26 @@ int main(int argc, const char* argv[]) {
                argv++;
        }
 
-       std::string currentExeAbsolutePath = absolutePath("/proc/self/exe");
+       std::string currentExeAbsolutePath = getAbsolutePath("/proc/self/exe");
        if (currentExeAbsolutePath.empty()) {
-               fprintf(stderr, "Failed to get the current executable's absolute path.\n");
+               _SERR("Failed to get the current executable's absolute path.");
                return -1;
        }
 
-       std::string clrFilesAbsolutePath = absolutePath(clrFilesPath);
+       std::string clrFilesAbsolutePath = getAbsolutePath(clrFilesPath);
        if (clrFilesAbsolutePath.empty()) {
-               fprintf(stderr, "Failed to resolve the full path to the CLR files.\n");
+               _SERR("Failed to resolve the full path to the CLR files.");
                return -1;
        }
 
-       std::string managedAssemblyAbsolutePath = absolutePath(managedAssemblyPath);
+       std::string managedAssemblyAbsolutePath = getAbsolutePath(managedAssemblyPath);
        if (managedAssemblyAbsolutePath.empty()) {
-               fprintf(stderr, "Failed to get the managed assembly's absolute path.\n");
+               _SERR("Failed to get the managed assembly's absolute path.");
                return -1;
        }
 
        std::string coreclrLibPath(clrFilesAbsolutePath + "/libcoreclr.so");
-       std::string appPath = baseName(managedAssemblyAbsolutePath);
+       std::string appPath = getBaseName(managedAssemblyAbsolutePath);
        std::string nativeDllSearchDirs(appPath);
        nativeDllSearchDirs += ":" + additionalProbingPath;
        nativeDllSearchDirs += ":" + clrFilesAbsolutePath;
@@ -170,7 +177,7 @@ int main(int argc, const char* argv[]) {
        void* coreclrLib = dlopen(coreclrLibPath.c_str(), RTLD_NOW | RTLD_LOCAL);
        if (coreclrLib == nullptr) {
                const char* error = dlerror();
-               fprintf(stderr, "dlopen failed to open the libcoreclr.so with error %s\n", error);
+               _SERR("dlopen failed to open the libcoreclr.so with error %s", error);
                return -1;
        }
 
@@ -179,22 +186,25 @@ int main(int argc, const char* argv[]) {
        coreclr_shutdown_ptr shutdownCoreCLR = (coreclr_shutdown_ptr)dlsym(coreclrLib, "coreclr_shutdown");
 
        if (initializeCoreCLR == nullptr) {
-               fprintf(stderr, "Function coreclr_initialize not found in the libcoreclr.so\n");
+               _SERR("Function coreclr_initialize not found in the libcoreclr.so");
                return -1;
        } else if (executeAssembly == nullptr) {
-               fprintf(stderr, "Function coreclr_execute_assembly not found in the libcoreclr.so\n");
+               _SERR("Function coreclr_execute_assembly not found in the libcoreclr.so");
                return -1;
        } else if (shutdownCoreCLR == nullptr) {
-               fprintf(stderr, "Function coreclr_shutdown not found in the libcoreclr.so\n");
+               _SERR("Function coreclr_shutdown not found in the libcoreclr.so");
                return -1;
        }
 
+       bool ncdbStartupHook = isNCDBStartupHookProvided();
+
        const char* propertyKeys[] = {
                "TRUSTED_PLATFORM_ASSEMBLIES",
                "APP_PATHS",
                "APP_NI_PATHS",
                "NATIVE_DLL_SEARCH_DIRECTORIES",
                "System.Globalization.Invariant",
+               ncdbStartupHook ? "STARTUP_HOOKS" : "" // must be the last one
        };
        const char* propertyValues[] = {
                tpaList.c_str(),
@@ -202,6 +212,7 @@ int main(int argc, const char* argv[]) {
                appPath.c_str(),
                nativeDllSearchDirs.c_str(),
                globalizationInvariant ? "true" : "false",
+               ncdbStartupHook ? getNCDBStartupHook() : "" // must be the last one
        };
 
        void* hostHandle;
@@ -210,14 +221,14 @@ int main(int argc, const char* argv[]) {
        int st = initializeCoreCLR(
                currentExeAbsolutePath.c_str(),
                "dotnet",
-               sizeof(propertyKeys) / sizeof(propertyKeys[0]),
+               sizeof(propertyKeys) / sizeof(propertyKeys[0]) - (ncdbStartupHook ? 0 : 1),
                propertyKeys,
                propertyValues,
                &hostHandle,
                &domainId);
 
        if (st < 0) {
-               fprintf(stderr, "coreclr_initialize failed - status: 0x%08x\n", st);
+               _SERR("coreclr_initialize failed - status: 0x%08x", st);
                return -1;
        }
 
@@ -236,13 +247,13 @@ int main(int argc, const char* argv[]) {
                (unsigned int*)&exitCode);
 
        if (st < 0) {
-               fprintf(stderr, "coreclr_execute_assembly failed - status: 0x%08x\n", st);
+               _SERR("coreclr_execute_assembly failed - status: 0x%08x", st);
                exitCode = -1;
        }
 
        st = shutdownCoreCLR(hostHandle, domainId);
        if (st < 0) {
-               fprintf(stderr, "coreclr_shutdown failed - status: 0x%08x\n", st);
+               _SERR("coreclr_shutdown failed - status: 0x%08x", st);
                return -1;
        }