There is now a partially functional block display in the aesalon GUI.
[aesalon.git] / src / monitor / Initializer.cpp
blob8273b498ecef51c8158f52aa19d1777ebb0dfbec
1 #include <string>
2 #include <iostream>
3 #include <fstream>
4 #include <unistd.h>
5 #include "misc/ArgumentParser.h"
6 #include "misc/ReferenceCounter.h"
7 #include "misc/StreamAsString.h"
8 #include "misc/String.h"
10 #include "platform/PlatformException.h"
12 #include "Initializer.h"
14 #ifndef DEFAULT_PORT
15 #define DEFAULT_PORT 6321
16 #endif
18 #ifndef LIBC_PATH
19 #define LIBC_PATH "/lib/libc.so.6"
20 #endif
22 namespace Aesalon {
23 namespace Monitor {
25 template<> Initializer *Misc::Singleton<Initializer>::instance = 0;
27 Initializer::Initializer(char *argv[]) : Misc::Singleton<Initializer>(), argv(argv) {
28 initialize();
31 Initializer::~Initializer() {
32 deinitialize();
35 void Initializer::initialize() {
36 new Misc::ReferenceCounter();
37 Misc::ArgumentParser *ap = new Misc::ArgumentParser();
39 ap->add_argument("usage", new Misc::BooleanArgument("--usage", 'h', "", 0, false));
40 ap->add_argument("logfile", new Misc::StringArgument("--log-file", 'l', ""));
41 ap->add_argument("tcp port", new Misc::StringArgument("--use-port", 0, Misc::StreamAsString() << DEFAULT_PORT));
42 ap->add_argument("wait", new Misc::BooleanArgument("--wait", 'w', "", 0, false));
43 ap->add_argument("libc path", new Misc::StringArgument("--libc-path", 0, LIBC_PATH));
45 ap->parse_argv(argv);
47 if(ap->get_argument("usage").to<Misc::BooleanArgument>()->get_status()) {
48 usage();
49 return;
52 if(ap->get_files()) {
53 int port;
54 Misc::String::to<int>(ap->get_argument("tcp port").to<Misc::StringArgument>()->get_value(), port);
55 server_socket = new Platform::TCPServerSocket(port);
57 Misc::SmartPointer<Platform::ArgumentList> al = new Platform::ArgumentList;
58 /*al->add_argument(ap->get_file(0)->get_filename());*/
59 for(std::size_t x = 0; x < ap->get_files(); x ++) {
60 al->add_argument(ap->get_file(x)->get_filename());
62 program_manager = new ProgramManager(al);
64 else {
65 usage();
66 return;
69 event_queue = new Platform::EventQueue();
71 /*symbol_manager = new Platform::SymbolManager();*/
73 /*symbol_manager->parse_from_executable(ap->get_file(0)->get_filename());*/
75 run();
77 server_socket->disconnect_all();
80 void Initializer::deinitialize() {
81 if(program_manager) delete program_manager;
82 /*if(symbol_manager) delete symbol_manager;*/
83 if(server_socket) delete server_socket;
84 if(event_queue) delete event_queue;
86 Misc::ArgumentParser::lock_mutex();
87 delete Misc::ArgumentParser::get_instance();
88 Misc::ReferenceCounter::lock_mutex();
89 delete Misc::ReferenceCounter::get_instance();
92 void Initializer::usage() {
93 std::cout << "aesalon program monitor, version " << AESALON_MAJOR_VERSION << "." << AESALON_MINOR_VERSION << "." << AESALON_PATCHLEVEL;
94 std::cout << ", copyright (C) 2009-2010 strange <kawk256@gmail.com>" << std::endl;
95 std::cout << "usage: " << argv[0] << " [arguments] executable [executable arguments]" << std::endl;
96 std::cout << "\t--usage, -h\t\tPrint this usage message." << std::endl;
97 std::cout << "\t--log-file, -l\t\tSet the file to log memory events to, for future reconstruction." << std::endl;
98 std::cout << "\t--use-port\t\tSet the port to listen on for connections. Defaults to " << DEFAULT_PORT << "." << std::endl;
99 std::cout << "\t--wait, -w\t\tWait for a TCP connection before executing. Defaults to false." << std::endl;
100 std::cout << "\t--libc-path\t\tThe path to the current version of libc being used. Defaults to " << LIBC_PATH << "." << std::endl;
103 void Initializer::run() {
104 program_manager->execute();
105 while(program_manager->is_running()) {
106 program_manager->wait();
107 if(event_queue->peek_event().is_valid()) {
108 std::cout << "Sending data from event queue . . ." << std::endl;
109 get_socket()->send_data(event_queue);
111 server_socket->accept_connections();
115 } // namespace Monitor
116 } // namespace Aesalon