57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync * VBoxDTrace - Wrapper that selects the right dtrace implemetation and adds
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync * our library to the search path.
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync * Copyright (C) 2016 Oracle Corporation
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync * available from http://www.virtualbox.org. This file is free software;
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync * you can redistribute it and/or modify it under the terms of the Common
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync * Development and Distribution License Version 1.0 (CDDL) only, as it
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync * comes in the "COPYING.CDDL" file of the VirtualBox OSE distribution.
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync * VirtualBox OSE is distributed in the hope that it will be useful, but
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync * WITHOUT ANY WARRANTY of any kind.
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync/*******************************************************************************
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync* Header Files *
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync*******************************************************************************/
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync#include "../../Main/include/ExtPackUtil.h"
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync/*******************************************************************************
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync* Defined Constants And Macros *
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync*******************************************************************************/
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync/** The VBoxDTrace extension pack name. */
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync#define VBOX_EXTPACK_VBOXDTRACE_NAME "Oracle VBoxDTrace Extension Pack"
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync/** The mangled version of VBOX_EXTPACK_VBOXDTRACE_NAME (also in Config.kmk). */
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync#define VBOX_EXTPACK_VBOXDTRACE_MANGLED_NAME "Oracle_VBoxDTrace_Extension_Pack"
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync/*******************************************************************************
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync* Structures and Typedefs *
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync*******************************************************************************/
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync/** The main function of VBoxDTrace.so/dylib/dll. */
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsynctypedef int (RTCALL *PFNVBOXDTRACEMAIN)(int argc, char **argv);
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync * Init IPRT.
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync * Locate a native DTrace command binary.
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync#if defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD) || defined(RT_OS_LINUX) || defined(RT_OS_SOLARIS)
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync * 1. Try native first on platforms where it's applicable.
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync for (uint32_t i = 0; i < RT_ELEMENTS(s_apszNativeDTrace); i++)
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync /** @todo Warn if the dtrace modules haven't been loaded or vboxdrv isn't
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync * compiled against them. */
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync * 2. VBoxDTrace extension pack installed?
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync * Note! We cannot use the COM API here because this program is usually
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync * run thru sudo or directly as root, even if the target
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync * VirtualBox process is running as regular user. This is due to
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync * the privileges required to run dtrace scripts on a host.
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync rc = RTPathAppPrivateArch(szDTraceCmd, sizeof(szDTraceCmd));
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync rc = RTPathAppend(szDTraceCmd, sizeof(szDTraceCmd),
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync VBOX_EXTPACK_INSTALL_DIR RTPATH_SLASH_STR VBOX_EXTPACK_VBOXDTRACE_MANGLED_NAME);
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync rc = RTPathAppend(szDTraceCmd, sizeof(szDTraceCmd), RTBldCfgTargetDotArch());
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync rc = RTPathAppend(szDTraceCmd, sizeof(szDTraceCmd), "VBoxDTraceCmd");
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync rc = RTStrCat(szDTraceCmd, sizeof(szDTraceCmd), RTLdrGetSuff());
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync return RTMsgErrorExit(RTEXITCODE_FAILURE, "Error constructing extension pack path: %Rrc", rc);
a62c8c9b57bafc96313f5e3817f57065595ee8ebvboxsync "Unable to find a DTrace implementation. VBoxDTrace Extension Pack installed?");
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync * Construct a new command line that includes our libary.
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync rc = RTPathAppPrivateNoArch(szDTraceLibDir, sizeof(szDTraceLibDir));
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync rc = RTPathAppend(szDTraceLibDir, sizeof(szDTraceLibDir), "dtrace" RTPATH_SLASH_STR "lib");
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync rc = RTPathAppend(szDTraceLibDir, sizeof(szDTraceLibDir), RTBldCfgTargetArch());
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync return RTMsgErrorExit(RTEXITCODE_FAILURE, "Error constructing dtrace library path for VBox: %Rrc", rc);
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync char **papszArgs = (char **)RTMemAlloc((argc + 3) * sizeof(char *));
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync return RTMsgErrorExit(RTEXITCODE_FAILURE, "No memory for argument list.");
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync papszArgs[0] = fIsNativeDTrace ? szDTraceCmd : argv[0];
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync * The native DTrace we execute as a sub-process and wait for.
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync rc = RTProcCreate(szDTraceCmd, papszArgs, RTENV_DEFAULT, 0, &hProc);
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync rc = RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &Status);
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Error waiting for child process: %Rrc", rc);
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Error executing '%s': %Rrc", szDTraceCmd, rc);
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync * While the VBoxDTrace we load and call the main function of.
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync rc = SUPR3HardenedLdrLoadPlugIn(szDTraceCmd, &hMod, RTErrInfoInitStatic(&ErrInfo));
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync rc = RTLdrGetSymbol(hMod, "VBoxDTraceMain", (void **)&pfnVBoxDTraceMain);
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync rcExit = (RTEXITCODE)pfnVBoxDTraceMain(cArgs, papszArgs);
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Error locating 'VBoxDTraceMain' in '%s': %Rrc", szDTraceCmd, rc);
57a7aeb014cdd1900c8bfecb0f9dcc7b96a61cc9vboxsync rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Error loading '%s': %Rrc (%s)", szDTraceCmd, rc, ErrInfo.szMsg);