From 3c1dd3754e6c30e982a9430c671be222d667d67d Mon Sep 17 00:00:00 2001 From: Ethereal Date: Fri, 12 Nov 2010 00:35:11 -0700 Subject: [PATCH] Continued working on various aspects of the process-launching system. --- include/common/AssertionException.h | 27 +++++++++++++++ include/common/SharedMemoryHeader.h | 23 +++++++++++++ include/common/StringTo.h | 34 +++++++++++++++++++ include/monitor/Coordinator.h | 3 ++ include/monitor/program/Launcher.h | 3 +- .../program/{Launcher.h => ProcessMonitor.h} | 22 ++++++------- .../monitor/program/{Launcher.h => SharedMemory.h} | 24 +++++++------- monitor/src/Aesalon.cpp | 1 + monitor/src/Coordinator.cpp | 9 ++--- monitor/src/SConscript | 1 + monitor/src/program/Launcher.cpp | 13 ++++++-- monitor/src/program/ProcessMonitor.cpp | 30 +++++++++++++++++ monitor/src/program/SharedMemory.cpp | 38 ++++++++++++++++++++++ 13 files changed, 195 insertions(+), 33 deletions(-) create mode 100644 include/common/AssertionException.h create mode 100644 include/common/SharedMemoryHeader.h create mode 100644 include/common/StringTo.h copy include/monitor/program/{Launcher.h => ProcessMonitor.h} (55%) copy include/monitor/program/{Launcher.h => SharedMemory.h} (50%) create mode 100644 monitor/src/program/ProcessMonitor.cpp create mode 100644 monitor/src/program/SharedMemory.cpp diff --git a/include/common/AssertionException.h b/include/common/AssertionException.h new file mode 100644 index 0000000..ce86c18 --- /dev/null +++ b/include/common/AssertionException.h @@ -0,0 +1,27 @@ +/** + Aesalon, a tool to visualize a program's behaviour at run-time. + Copyright (C) 2010, Aesalon Development Team. + + Aesalon is distributed under the terms of the GNU GPLv3. For more + licensing information, see the file LICENSE included with the distribution. + + @file include/common/AssertionException.h + +*/ + +#ifndef AesalonCommon_AssertionException_H +#define AesalonCommon_AssertionException_H + +#include "Exception.h" + +namespace Common { + +class AssertionException : public Exception { +public: + AssertionException(std::string message) : Exception("Assertion failed: " + message) {} + virtual ~AssertionException() {} +}; + +} // namespace Common + +#endif diff --git a/include/common/SharedMemoryHeader.h b/include/common/SharedMemoryHeader.h new file mode 100644 index 0000000..e37f590 --- /dev/null +++ b/include/common/SharedMemoryHeader.h @@ -0,0 +1,23 @@ +/** + Aesalon, a tool to visualize a program's behaviour at run-time. + Copyright (C) 2010, Aesalon Development Team. + + Aesalon is distributed under the terms of the GNU GPLv3. For more + licensing information, see the file LICENSE included with the distribution. + + @file include/common/SharedMemoryHeader.h + +*/ + +#ifndef AesalonCommon_SharedMemoryHeader_H +#define AesalonCommon_SharedMemoryHeader_H + +#include + +struct SharedMemoryHeader { + uint32_t size; +}; + +#define SharedMemoryDataOffset (sizeof(SharedMemoryHeader) + 16) + +#endif diff --git a/include/common/StringTo.h b/include/common/StringTo.h new file mode 100644 index 0000000..524880f --- /dev/null +++ b/include/common/StringTo.h @@ -0,0 +1,34 @@ +/** + Aesalon, a tool to visualize a program's behaviour at run-time. + Copyright (C) 2010, Aesalon Development Team. + + Aesalon is distributed under the terms of the GNU GPLv3. For more + licensing information, see the file LICENSE included with the distribution. + + @file include/common/StringTo.h + +*/ + +#ifndef AesalonCommon_StringTo_H +#define AesalonCommon_StringTo_H + +#include +#include +#include + +namespace Common { + +/** Converts a string into a given type. Note that @a Type must have a default constructor. +*/ +template +Type StringTo(const std::string &string) { + if(string == "") return Type(); + std::istringstream ss(string); + Type t; + ss >> t; + return t; +} + +} // namespace Common + +#endif diff --git a/include/monitor/Coordinator.h b/include/monitor/Coordinator.h index 347b7b7..5e87c9d 100644 --- a/include/monitor/Coordinator.h +++ b/include/monitor/Coordinator.h @@ -24,6 +24,7 @@ private: int m_argcOffset; Config::Vault *m_vault; Module::List *m_moduleList; + int m_returnValue; public: static Coordinator *instance() { return m_instance; } Coordinator(char **argv); @@ -33,6 +34,8 @@ public: Config::Vault *vault() const { return m_vault; } int argcOffset() const { return m_argcOffset; } Module::List *moduleList() const { return m_moduleList; } + int returnValue() const { return m_returnValue; } + void setReturnValue(int newValue) { m_returnValue = newValue; } void run(); private: diff --git a/include/monitor/program/Launcher.h b/include/monitor/program/Launcher.h index 25e8b2b..660cc24 100644 --- a/include/monitor/program/Launcher.h +++ b/include/monitor/program/Launcher.h @@ -19,10 +19,9 @@ namespace Program { class Launcher { private: - Config::Vault *m_vault; char **m_argv; public: - Launcher(Config::Vault *vault, char **argv); + Launcher(char **argv); ~Launcher(); void startProcess(); diff --git a/include/monitor/program/Launcher.h b/include/monitor/program/ProcessMonitor.h similarity index 55% copy from include/monitor/program/Launcher.h copy to include/monitor/program/ProcessMonitor.h index 25e8b2b..c984529 100644 --- a/include/monitor/program/Launcher.h +++ b/include/monitor/program/ProcessMonitor.h @@ -5,27 +5,27 @@ Aesalon is distributed under the terms of the GNU GPLv3. For more licensing information, see the file LICENSE included with the distribution. - @file include/monitor/program/Launcher.h + @file include/monitor/program/ProcessMonitor.h */ -#ifndef AesalonMonitor_Program_Launcher_H -#define AesalonMonitor_Program_Launcher_H +#ifndef AesalonMonitor_Program_ProcessMonitor_H +#define AesalonMonitor_Program_ProcessMonitor_H -#include "config/Vault.h" +#include + +#include "SharedMemory.h" namespace Monitor { namespace Program { -class Launcher { +class ProcessMonitor { private: - Config::Vault *m_vault; - char **m_argv; + pid_t m_pid; + SharedMemory *m_sharedMemory; public: - Launcher(Config::Vault *vault, char **argv); - ~Launcher(); - - void startProcess(); + ProcessMonitor(pid_t pid); + ~ProcessMonitor(); }; } // namespace Program diff --git a/include/monitor/program/Launcher.h b/include/monitor/program/SharedMemory.h similarity index 50% copy from include/monitor/program/Launcher.h copy to include/monitor/program/SharedMemory.h index 25e8b2b..358fd0a 100644 --- a/include/monitor/program/Launcher.h +++ b/include/monitor/program/SharedMemory.h @@ -5,27 +5,29 @@ Aesalon is distributed under the terms of the GNU GPLv3. For more licensing information, see the file LICENSE included with the distribution. - @file include/monitor/program/Launcher.h + @file include/monitor/program/SharedMemory.h */ -#ifndef AesalonMonitor_Program_Launcher_H -#define AesalonMonitor_Program_Launcher_H +#ifndef AesalonMonitor_Program_SharedMemory_H +#define AesalonMonitor_Program_SharedMemory_H -#include "config/Vault.h" +#include + +#include "common/SharedMemoryHeader.h" namespace Monitor { namespace Program { -class Launcher { +class SharedMemory { private: - Config::Vault *m_vault; - char **m_argv; + int m_fd; + uint8_t *m_memory; + SharedMemoryHeader *m_header; + std::string m_identifier; public: - Launcher(Config::Vault *vault, char **argv); - ~Launcher(); - - void startProcess(); + SharedMemory(std::string identifier, uint32_t size); + ~SharedMemory(); }; } // namespace Program diff --git a/monitor/src/Aesalon.cpp b/monitor/src/Aesalon.cpp index 7128624..aedeee5 100644 --- a/monitor/src/Aesalon.cpp +++ b/monitor/src/Aesalon.cpp @@ -6,6 +6,7 @@ int main(int argc, char *argv[]) { try { Monitor::Coordinator coordinator(argv); coordinator.run(); + return coordinator.returnValue(); } catch(Common::Exception exception) { std::cout << exception.message() << std::endl; diff --git a/monitor/src/Coordinator.cpp b/monitor/src/Coordinator.cpp index ca9764e..177e579 100644 --- a/monitor/src/Coordinator.cpp +++ b/monitor/src/Coordinator.cpp @@ -40,14 +40,9 @@ void Coordinator::run() { return; } - /*if(m_argv[m_argumentEndpoint] == NULL || m_store->item("help")->boolValue() - || m_store->item("version")->boolValue()) { - - usage(!m_store->item("version")->boolValue()); - return; - } + Program::Launcher *launcher = new Program::Launcher(&m_argv[m_argcOffset]); - Program::Launcher *launcher = new Program::Launcher(m_store, &m_argv[m_argumentEndpoint]);*/ + launcher->startProcess(); } void Coordinator::parseConfigs() { diff --git a/monitor/src/SConscript b/monitor/src/SConscript index b38403e..e3271cd 100644 --- a/monitor/src/SConscript +++ b/monitor/src/SConscript @@ -2,5 +2,6 @@ Import("env") env.Append(CPPPATH = ["../../include/", "../../include/monitor/"]) env.Append(CCFLAGS = ["-W", "-Wall", "-g", "-DAesalonMonitor"]) +env.Append(LIBS = ["rt"]) env.Program(target = "aesalon", source = Glob("*.cpp") + Glob("*/*.cpp")) diff --git a/monitor/src/program/Launcher.cpp b/monitor/src/program/Launcher.cpp index 83753e5..8e90efe 100644 --- a/monitor/src/program/Launcher.cpp +++ b/monitor/src/program/Launcher.cpp @@ -9,12 +9,18 @@ */ +#include + #include "program/Launcher.h" +#include "common/AssertionException.h" +#include "Coordinator.h" +#include "common/StringTo.h" + namespace Monitor { namespace Program { -Launcher::Launcher(Config::Vault *vault, char **argv) : m_vault(vault), m_argv(argv) { +Launcher::Launcher(char **argv) : m_argv(argv) { } @@ -23,7 +29,10 @@ Launcher::~Launcher() { } void Launcher::startProcess() { - + std::string s = Coordinator::instance()->vault()->get("shmSize"); + std::cout << "shmSize: \"" << s << "\"\n"; + std::cout << "size:" << Common::StringTo(Coordinator::instance()->vault()->get("shmSize")) << std::endl; + throw Common::AssertionException("Size of shared memory must be a nonzero multiple of four."); } } // namespace Program diff --git a/monitor/src/program/ProcessMonitor.cpp b/monitor/src/program/ProcessMonitor.cpp new file mode 100644 index 0000000..075bbaf --- /dev/null +++ b/monitor/src/program/ProcessMonitor.cpp @@ -0,0 +1,30 @@ +/** + Aesalon, a tool to visualize a program's behaviour at run-time. + Copyright (C) 2010, Aesalon Development Team. + + Aesalon is distributed under the terms of the GNU GPLv3. For more + licensing information, see the file LICENSE included with the distribution. + + @file monitor/src/program/ProcessMonitor.cpp + +*/ + +#include "program/ProcessMonitor.h" +#include "Coordinator.h" +#include "common/StringTo.h" +#include "common/StreamAsString.h" + +namespace Monitor { +namespace Program { + +ProcessMonitor::ProcessMonitor(pid_t pid) : m_pid(pid) { + m_sharedMemory = new SharedMemory(Common::StreamAsString() << "aesalon-" << pid, + Common::StringTo(Coordinator::instance()->vault()->get("shmSize"))); +} + +ProcessMonitor::~ProcessMonitor() { + delete m_sharedMemory; +} + +} // namespace Program +} // namespace Monitor diff --git a/monitor/src/program/SharedMemory.cpp b/monitor/src/program/SharedMemory.cpp new file mode 100644 index 0000000..8a854fe --- /dev/null +++ b/monitor/src/program/SharedMemory.cpp @@ -0,0 +1,38 @@ +/** + Aesalon, a tool to visualize a program's behaviour at run-time. + Copyright (C) 2010, Aesalon Development Team. + + Aesalon is distributed under the terms of the GNU GPLv3. For more + licensing information, see the file LICENSE included with the distribution. + + @file monitor/src/program/SharedMemory.cpp + +*/ + +#include +#include + +#include "program/SharedMemory.h" +#include "common/AssertionException.h" + +namespace Monitor { +namespace Program { + +SharedMemory::SharedMemory(std::string identifier, uint32_t size) : m_identifier(identifier) { + if(size == 0 || (size % 4) != 0) + throw Common::AssertionException("Size of shared memory must be a nonzero multiple of four."); + + m_fd = shm_open(identifier.c_str(), O_RDWR, 0); + + m_memory = static_cast(mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, m_fd, 0)); + + // Yes, the reinterpret_cast<> is required. static_cast<> does not work for this. + m_header = reinterpret_cast(m_memory); +} + +SharedMemory::~SharedMemory() { + shm_unlink(m_identifier.c_str()); +} + +} // namespace Program +} // namespace Monitor -- 2.11.4.GIT