From c91d74ca2f4f5e7673fe755429a43348b08a126f Mon Sep 17 00:00:00 2001 From: strange Date: Sun, 17 Jan 2010 00:29:15 -0700 Subject: [PATCH] Time for a major overhaul of aesalon, again. Things that need to be done: + The GUI is a mess. It should be redone, properly, with each ProgramDisplay using its own thread, etc. + The communication system needs to be rewritten, perhaps using UDP sockets instead of TCP sockets. The data reliability of TCP is nice, though. + The breakpoint-handling system in the monitor needs to be changed around. Rather than having observers that are called in order, have each breakpoint maintain a list of interested observers. Makes things simpler, and removes the ugly hacks in MallocObserver to boot. + Add GPL/other doc comments at the beginning of each source file For the moment, though, I'm going to enjoy the fact that I've pretty much duplicated what GDB was doing for me, and a lot faster, too. Hah! --- src/gui/Program.cpp | 30 +++++++++++++++++++++++++++++- src/gui/Program.h | 15 +++++++++++++++ src/gui/ProgramDisplay.cpp | 2 ++ src/monitor/Initializer.cpp | 5 ++++- src/monitor/elf/Parser.cpp | 1 + src/monitor/ptrace/MallocObserver.cpp | 19 +++++++++++++++---- src/monitor/ptrace/Portal.cpp | 2 ++ src/platform/TCPServerSocket.cpp | 4 +++- tests/CMakeLists.txt | 2 +- tests/sleep_malloc | Bin 0 -> 9485 bytes tests/sleep_malloc.c | 16 ++++++++++++++++ 11 files changed, 88 insertions(+), 8 deletions(-) create mode 100755 tests/sleep_malloc create mode 100644 tests/sleep_malloc.c diff --git a/src/gui/Program.cpp b/src/gui/Program.cpp index 7706e48..e994e55 100644 --- a/src/gui/Program.cpp +++ b/src/gui/Program.cpp @@ -1,4 +1,5 @@ #include +#include #include "Program.h" #include "MainWindow.h" @@ -8,6 +9,19 @@ namespace Aesalon { namespace GUI { +ProgramSocketThread::ProgramSocketThread(Misc::SmartPointer socket, + Misc::SmartPointer memory) : socket(socket), memory(memory) { +} + +void ProgramSocketThread::run() { + while(socket->is_valid()) { + std::cout << "ProgramSocketThread: checking data . . ." << std::endl; + std::cout << "Received \"" << socket->get_data() << "\"" << std::endl; + this->msleep(500); + } + this->exit(0); +} + Program::Program() { QSettings settings; @@ -23,7 +37,18 @@ Program::Program() { argument_list.from_string(arguments + " "); - bi_pipe = new Platform::BidirectionalPipe(argument_list, true); + /*bi_pipe = new Platform::BidirectionalPipe(argument_list, true);*/ + pid_t pid = fork(); + if(pid == 0) { + std::cout << "in child, execv'ing . . ." << std::endl; + std::cout << "\targument_list[0]: \"" << argument_list.get_argument(0) << "\"\n"; + std::cout << "\targument_list[1]: \"" << argument_list.get_argument(1) << "\"\n"; + std::cout << "\targument_list[2]: \"" << argument_list.get_argument(2) << "\"\n"; + std::cout << "\targument_list[3]: \"" << argument_list.get_argument(3) << "\"\n"; + std::cout << "\targument_list[4]: \"" << argument_list.get_argument(4) << "\"\n"; + execv(argument_list.get_argument(0).c_str(), argument_list.get_as_argv()); + throw Misc::Exception("Couldn't execute file . . ."); + } memory = new Platform::Memory(); @@ -36,6 +61,9 @@ Program::Program() { socket = NULL; } } + + socket_thread = new ProgramSocketThread(socket, memory); + socket_thread->start(QThread::HighPriority); } } // namespace GUI diff --git a/src/gui/Program.h b/src/gui/Program.h index ec0fed3..4d868a8 100644 --- a/src/gui/Program.h +++ b/src/gui/Program.h @@ -1,6 +1,8 @@ #ifndef AESALON_GUI_PROGRAM_H #define AESALON_GUI_PROGRAM_H +#include + #include "platform/EventQueue.h" #include "platform/TCPSocket.h" #include "platform/Memory.h" @@ -11,6 +13,18 @@ namespace Aesalon { namespace GUI { +class ProgramSocketThread : public QThread { +private: + Misc::SmartPointer socket; + Misc::SmartPointer memory; +public: + ProgramSocketThread(Misc::SmartPointer socket, Misc::SmartPointer memory); + virtual ~ProgramSocketThread() {} +protected: + void run(); +}; + + class Program { private: Misc::SmartPointer event_queue; @@ -23,6 +37,7 @@ private: bool in_xterm; Misc::SmartPointer bi_pipe; + Misc::SmartPointer socket_thread; public: Program(); virtual ~Program() {} diff --git a/src/gui/ProgramDisplay.cpp b/src/gui/ProgramDisplay.cpp index 36a7040..a3c35a8 100644 --- a/src/gui/ProgramDisplay.cpp +++ b/src/gui/ProgramDisplay.cpp @@ -107,6 +107,8 @@ void ProgramDisplay::create_running_widget() { void ProgramDisplay::begin_program() { create_running_widget(); + QSettings settings; + settings.setValue("Program/executable", launch_program_name->currentText()); program = new Program(); } diff --git a/src/monitor/Initializer.cpp b/src/monitor/Initializer.cpp index 0d70ac4..fb8be12 100644 --- a/src/monitor/Initializer.cpp +++ b/src/monitor/Initializer.cpp @@ -100,7 +100,10 @@ void Initializer::run() { program_manager->execute(); while(program_manager->is_running()) { program_manager->wait(); - if(event_queue->peek_event().is_valid()) get_socket()->send_data(event_queue); + if(event_queue->peek_event().is_valid()) { + std::cout << "Sending data from event queue . . ." << std::endl; + get_socket()->send_data(event_queue); + } } } diff --git a/src/monitor/elf/Parser.cpp b/src/monitor/elf/Parser.cpp index fb6773f..7a0ad48 100644 --- a/src/monitor/elf/Parser.cpp +++ b/src/monitor/elf/Parser.cpp @@ -11,6 +11,7 @@ namespace Monitor { namespace ELF { Parser::Parser(std::string filename) : filename(filename) { + std::cout << "ELF::Parser::Parser(): filename is \"" << filename << "\"\n"; file_fd = open(filename.c_str(), O_RDONLY); header = new Header(file_fd); diff --git a/src/monitor/ptrace/MallocObserver.cpp b/src/monitor/ptrace/MallocObserver.cpp index 484bf93..bfe13f4 100644 --- a/src/monitor/ptrace/MallocObserver.cpp +++ b/src/monitor/ptrace/MallocObserver.cpp @@ -1,5 +1,6 @@ #include "MallocObserver.h" #include "Initializer.h" +#include "platform/MemoryEvent.h" namespace Aesalon { namespace Monitor { @@ -9,16 +10,24 @@ bool MallocObserver::handle_breakpoint(Misc::SmartPointer breakpoint Misc::SmartPointer malloc_symbol = Initializer::get_instance()->get_program_manager()->get_libc_parser()->get_symbol("malloc"); Misc::SmartPointer portal = Initializer::get_instance()->get_program_manager()->get_ptrace_portal(); + static Word last_size = 0; + if(malloc_symbol.is_valid() && breakpoint->get_address() != (malloc_symbol->get_address() + Initializer::get_instance()->get_program_manager()->get_ptrace_portal()->get_libc_offset())) { breakpoint_set_t::iterator i = breakpoints.find(breakpoint->get_id()); - if(i != breakpoints.end()) { - std::cout << "Return value from malloc() is:" << std::hex << portal->get_register(ASM::Register::RAX) << std::endl; - } + if(i == breakpoints.end()) return false; + + std::cout << "Return value from malloc() is:" << std::hex << portal->get_register(ASM::Register::RAX) << std::endl; + breakpoint->set_valid(false); + Initializer::get_instance()->get_event_queue()->push_event(new Platform::MemoryBlockAllocEvent(portal->get_register(ASM::Register::RAX), last_size)); - return false; + return true; } + static int called_times = 0; + /* NOTE: malloc() calls malloc() for some reason, so skip it. */ + /* TODO: figure out why this happens, and find a workaround. */ + if(called_times++ % 2) return true; std::cout << "MallocObserver::handle_breakpoint(): malloc breakpoint found . . ." << std::endl; Word rbp = portal->get_register(ASM::Register::RBP); std::cout << "\tRBP is: " << std::hex << rbp << std::endl; @@ -28,6 +37,8 @@ bool MallocObserver::handle_breakpoint(Misc::SmartPointer breakpoint return_address = portal->read_memory(rbp-40); std::cout << "\tReturn address: " << return_address << std::endl; breakpoints.insert(portal->place_breakpoint(return_address)); + std::cout << "\tMemory block size will be " << portal->get_register(ASM::Register::RDI) << std::endl; + last_size = portal->get_register(ASM::Register::RDI); return true; } diff --git a/src/monitor/ptrace/Portal.cpp b/src/monitor/ptrace/Portal.cpp index ab2b8fa..fbf6de5 100644 --- a/src/monitor/ptrace/Portal.cpp +++ b/src/monitor/ptrace/Portal.cpp @@ -76,6 +76,8 @@ Word Portal::get_register(ASM::Register which) const { return registers.rax; case ASM::Register::RBX: return registers.rbx; + case ASM::Register::RDI: + return registers.rdi; case ASM::Register::RIP: return registers.rip; case ASM::Register::RBP: diff --git a/src/platform/TCPServerSocket.cpp b/src/platform/TCPServerSocket.cpp index 39ba002..fea8446 100644 --- a/src/platform/TCPServerSocket.cpp +++ b/src/platform/TCPServerSocket.cpp @@ -89,7 +89,9 @@ void TCPServerSocket::send_data(std::string data) { void TCPServerSocket::send_data(Misc::SmartPointer data) { while(data->peek_event()) { - send_data(data->peek_event().to()->serialize()); + std::string data_string = data->peek_event().to()->serialize(); + std::cout << "TCPServerSocket::send_data(): sending \"" << data_string << "\"\n"; + send_data(data_string); data->pop_event(); } } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index c12f30b..c5c2ed2 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,4 +1,4 @@ add_definitions(-g -O0) add_executable(malloc_test malloc_test.c) add_executable(sleep_test sleep_test.c) - +add_executable(sleep_malloc sleep_malloc.c) diff --git a/tests/sleep_malloc b/tests/sleep_malloc new file mode 100755 index 0000000000000000000000000000000000000000..fb6a7d09315264bdca2cc81ec2e1f7813bc37d1c GIT binary patch literal 9485 zcwW6(eQaCR6~FH}j-BSSY06j2`T%#Nj5z(GO9Kq2PFjbWwv?8oBI4!6eu-WDh5g*Q zsHj01RZUh1Xdeho5N*;x8h;@En7YB%RNa;#Z4(F$I^d5C7Ua?*QWz)#fp^dScz*Bt zxuE^AV?CdHe)pVv?$^8bynBxhgtyC*ggD9QdkCayPk_h)hIh7`Dj4fQUi|-UbRDY4 zvfH8;Kmi+{WkqxDWqL0B)ImaKfE=*#)ePobvT!%DQozQC8z8GgSw6|e57&2 z#yeR(dU$=9oy_cxGrQwVpYs`3u^FL#b?jNq%3Cc5Y`mZ8abCyxo&_Tc>66Q-d~#X9 z#(hkWb8dGhwyQ_qoOnIE6Y9z?kx~8MSTUXb7ybc8*56Xn{ z4>knf_}zO?90?Bm<3dyK&l)$5zWw3zJKvjq>u>KpefIvL)d!D0b>z1vF19pvy!-U| z*FUON4MLj`ZLdhs9>oD~tAYEGb2+^=@EtYq12yoz8u)_<>U1S4G>_QuUlIIb`AGr) zHN%e!_)!GysxD^|f!`K$9paQiFh|~syd!br_`LGWertD>g|Wrz4~}OXXx2rLd)g!9Kx?& z1m3hQD3dpGD3gt+j4_nd5{YyKl}{c^6FST6-VmeZmbiH zr__8-kK*j8W@yyPs*6=Y9?j)=o}R+mI%uOE;lXV|b)$cy|0c72viXxP@q@LfjW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Ijey@5g!z9OIs1JTKayNM-`*$qm1si1KOuMrDv0x$4A=@Z zEa1y);6B(V9?0i3ao9ui)iy~SgVYzZe zk*tw3@?&HE2vXJHzTJD(@Zeqh;3ih>w@J4LEkZP{jwjNiT0$jph!BIN$7?i z^>5kOvlT&x8jlvRv~>fb=Qz=PGI^N$8Q9fN%G$oS?~Vae0yjKe@e(n;2q$yuj_~en zePMO?_U(5K>{IvkZ3_<&(_C7OX{l&J=MCY7!f}(TWwY90daG*%*llnBfn9xf3o|2LjgH->TbXev$mOH`;KIEtEw{pv`jhQn}g|vh7#ea0?{bTbe<7uVyPQdGAfm IK&i@q0Xb05+5i9m literal 0 HcwPel00001 diff --git a/tests/sleep_malloc.c b/tests/sleep_malloc.c new file mode 100644 index 0000000..7330180 --- /dev/null +++ b/tests/sleep_malloc.c @@ -0,0 +1,16 @@ +#include +#include + +int main(int argc, char *argv[]) { + char *memory = 0; + sleep(1); + printf("**** sleep_malloc: about to allocate one byte of memory . . .\n"); + memory = malloc(1); + sleep(1); + printf("**** sleep_malloc: memory allocated (address is %p), freeing . . .\n", memory); + free(memory); + printf("**** sleep_malloc: memory freed, exiting . . .\n"); + sleep(1); + memory = 0; + return 0; +} -- 2.11.4.GIT