From 3da5b95e5345e36c21712b9954143b2ba42df98e Mon Sep 17 00:00:00 2001 From: Igor Kotrasinski Date: Thu, 19 Jul 2018 14:48:24 +0200 Subject: [PATCH] Move launching gdbserver to separate function Change-Id: Ic8adfc816b895ffbffb86eebe960fc80c73deb96 Signed-off-by: Igor Kotrasinski --- simulatordaemon/inc/TAFactory.h | 5 +++- simulatordaemon/src/TAFactory.cpp | 58 +++++++++++++++++++++++---------------- 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/simulatordaemon/inc/TAFactory.h b/simulatordaemon/inc/TAFactory.h index 933b6d9..b44b2f2 100644 --- a/simulatordaemon/inc/TAFactory.h +++ b/simulatordaemon/inc/TAFactory.h @@ -56,7 +56,10 @@ private: TAFactory(); bool checkIfTARunning(TEEC_UUID TAUUID); TAInstancePtr createUninitalizedTAInstance(TEEC_UUID TAUUID, ISession* session); - bool launchTA(TEEC_UUID TAUUID, std::stringstream& str, bool debug, pid_t& pid); + bool launchTA(TEEC_UUID TAUUID, std::stringstream& str, pid_t& pid, bool &debug); + bool launchTAInDebugMode(uint32_t port, string image_path, + string argument, pthread_mutex_t &lock, pid_t &pid); + static void* waitForChild(void *pid); void cleanupTAInstance(pid_t PID); ~TAFactory(); diff --git a/simulatordaemon/src/TAFactory.cpp b/simulatordaemon/src/TAFactory.cpp index 30db8e3..89cc70b 100644 --- a/simulatordaemon/src/TAFactory.cpp +++ b/simulatordaemon/src/TAFactory.cpp @@ -171,8 +171,8 @@ TAInstancePtr TAFactory::createUninitalizedTAInstance(TEEC_UUID TAUUID, // Initialize PID to -1 pid_t pid = -1; // Set default values for TA configuration variables - bool debug = false; bool alive = false; + bool debug; bool singleInst = false; TEEC_Result result = TEEC_ERROR_COMMUNICATION; @@ -191,7 +191,7 @@ TAInstancePtr TAFactory::createUninitalizedTAInstance(TEEC_UUID TAUUID, str << uuidStr << "-"; str << InstID; - if (launchTA(TAUUID, str, debug, pid)) { + if (launchTA(TAUUID, str, pid, debug)) { // TA is launched successfully, Create a new instance of TAInstance class /* Check if TA is to be keep alive and accordingly set TAInstance's @@ -337,8 +337,7 @@ void TAFactory::cleanupTAInstance(pid_t PID) { * @param debug debug flag * @param pid PID to be update for launched TA */ -bool TAFactory::launchTA(TEEC_UUID TAUUID, std::stringstream& str, bool debug, - pid_t& pid) { +bool TAFactory::launchTA(TEEC_UUID TAUUID, std::stringstream& str, pid_t& pid, bool &debug) { int32_t result = -1; pthread_t thread; @@ -375,34 +374,19 @@ bool TAFactory::launchTA(TEEC_UUID TAUUID, std::stringstream& str, bool debug, boost::optional debugPort; try { debugPort = UUIDConf->at(TAUUID)->getDebugPort(); + debug = true; } catch (std::out_of_range) { debugPort = boost::none; + debug = false; } pthread_mutex_lock(&TABin->taLock); //Check if the TA is to be launched in debug mode or release mode if (debugPort != boost::none) { // DEBUG MODE - debug = true; - char *argv[5]; - string argvGDB = "/usr/bin/gdbserver"; - string argvHost = "localhost:" + std::to_string(*debugPort); - - argv[0] = &argvGDB[0]; - argv[1] = &argvHost[0]; - argv[2] = &argvPath[0]; - argv[3] = &argvName[0]; - argv[4] = NULL; - - // fork TA with GDB - pid = fork(); - if (0 == pid) { - LOGD(MODULE_SIM_DAEMON, "In Child Process"); - execv(argv[0], argv); - LOGE(MODULE_SIM_DAEMON, "Launching Trusted Application FAILED"); - pthread_mutex_unlock(&TABin->taLock); + bool launched = this->launchTAInDebugMode( + *debugPort, argvPath, argvName, TABin->taLock, pid); + if (!launched) return false; - } - LOGD(MODULE_SIM_DAEMON, "In Parent Process"); } else { //RELEASE MODE char *argv[3]; argv[0] = &argvPath[0]; @@ -458,6 +442,32 @@ bool TAFactory::launchTA(TEEC_UUID TAUUID, std::stringstream& str, bool debug, return true; } +bool TAFactory::launchTAInDebugMode(uint32_t port, string image_path, + string argument, pthread_mutex_t &lock, pid_t &pid) +{ + char *argv[5]; + string gdbserver = "/usr/bin/gdbserver"; + string port_str = "localhost:" + std::to_string(port); + + argv[0] = &gdbserver[0]; + argv[1] = &port_str[0]; + argv[2] = &image_path[0]; + argv[3] = &argument[0]; + argv[4] = NULL; + + // fork TA with GDB + pid = fork(); + if (0 == pid) { + LOGD(MODULE_SIM_DAEMON, "In Child Process"); + execv(argv[0], argv); + LOGE(MODULE_SIM_DAEMON, "Launching Trusted Application FAILED"); + pthread_mutex_unlock(&lock); + return false; + } + LOGD(MODULE_SIM_DAEMON, "In Parent Process"); + return true; +} + TAFactory::~TAFactory() { /* Destroy the locks created for TAInstance map (mTAInstanceMap), Instance * Id (InstID) and TA Factory Instance (instance) -- 2.7.4