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"
15 #define DEFAULT_PORT 6321
19 #define LIBC_PATH "/lib/libc.so.6"
25 template<> Initializer
*Misc::Singleton
<Initializer
>::instance
= 0;
27 Initializer::Initializer(char *argv
[]) : Misc::Singleton
<Initializer
>(), argv(argv
) {
31 Initializer::~Initializer() {
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
));
47 if(ap
->get_argument("usage").to
<Misc::BooleanArgument
>()->get_status()) {
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
);
69 event_queue
= new Platform::EventQueue();
71 /*symbol_manager = new Platform::SymbolManager();*/
73 /*symbol_manager->parse_from_executable(ap->get_file(0)->get_filename());*/
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