]> git.tdb.fi Git - gldbg.git/blobdiff - source/gldbg.cpp
Enable bidirectional communication between gldbg and glwrap.so
[gldbg.git] / source / gldbg.cpp
index c91e00a79ec8171dd6672fbc8a1b9333209fa7e4..43827378f4faed462e35efe2be4b3a2ca2dfffcb 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of gldbg
-Copyright © 2009  Mikko Rasa, Mikkosoft Productions
+Copyright © 2009-2010  Mikko Rasa, Mikkosoft Productions
 Distributed under the GPL
 */
 
@@ -15,8 +15,11 @@ Distributed under the GPL
 #include <msp/fs/dir.h>
 #include <msp/io/print.h>
 #include <msp/strings/lexicalcast.h>
+#include "functions.h"
 #include "gldbg.h"
-#include "glprint.h"
+#include "gldecoder.h"
+#include "packet.h"
+#include "tool.h"
 
 using namespace std;
 using namespace Msp;
@@ -26,11 +29,22 @@ Application::RegApp<GlDbg> GlDbg::reg;
 GlDbg::GlDbg(int argc, char **argv):
        cmd_interp(*this),
        process(vector<string>(argv+1, argv+argc)),
+       buf_offset(0),
        flushing(false),
        got_sigchld(false)
 {
        FS::Path libdir = FS::get_sys_lib_dir(argv[0], "gldbg");
        process.setenv("LD_PRELOAD", (libdir/"glwrap.so").str().c_str());
+
+       const list<Tool::Factory *> &factories = Tool::get_factories();
+       for(list<Tool::Factory *>::const_iterator i=factories.begin(); i!=factories.end(); ++i)
+               tools.push_back((*i)->create(*this));
+}
+
+GlDbg::~GlDbg()
+{
+       for(list<Tool *>::iterator i=tools.begin(); i!=tools.end(); ++i)
+               delete *i;
 }
 
 int GlDbg::main()
@@ -40,10 +54,12 @@ int GlDbg::main()
        set_loop_mode(TICK_BUSY);
 
        IO::print("GLdbg 0.0\n");
-       IO::print("Copyright © 2009 Mikkosoft Productions\n");
+       IO::print("Copyright © 2009-2010 Mikkosoft Productions\n");
        IO::print("Type \"help\" for a list of commands\n");
 
        Application::main();
+
+       return 0;
 }
 
 void GlDbg::launch()
@@ -59,13 +75,32 @@ void GlDbg::launch()
        fcntl(sock_fd, F_SETFD, flags|FD_CLOEXEC);
 
        process.setenv("GLWRAP_FD", lexical_cast(fds[1]));
+       process.setenv("GLWRAP_CTRL_FD", lexical_cast(fds[1]));
        process.launch();
        close(fds[1]);
 }
-       
-void GlDbg::quit()
+
+void GlDbg::set_breakpoint(unsigned short func, char flag)
 {
-       if(process.get_state()!=Process::INACTIVE)
+       GlPacket *pkt = packet_begin(FUNC_GLDBREAK);
+       packet_write_short(pkt, func);
+       packet_write_char(pkt, flag);
+       packet_write_char(pkt, 0);
+       packet_send(pkt, sock_fd);
+}
+
+void GlDbg::clear_breakpoint(unsigned short func, char flag)
+{
+       GlPacket *pkt = packet_begin(FUNC_GLDBREAK);
+       packet_write_short(pkt, func);
+       packet_write_char(pkt, 0);
+       packet_write_char(pkt, flag);
+       packet_send(pkt, sock_fd);
+}
+
+void GlDbg::quit(bool force)
+{
+       if(!force && process.get_state()!=Process::INACTIVE)
                throw InvalidState("Program is still running");
        exit(0);
 }
@@ -127,16 +162,26 @@ void GlDbg::read_stream()
                        {
                                const char *data = buffer.data()+buf_offset;
                                unsigned len = buffer.size()-buf_offset;
-                               int size = gldecoder_decode(0, data, len);
-                               if(size<0)
+                               GlPacket *pkt = packet_receive_str(data, &len);
+                               if(!pkt)
                                        break;
-                               tracer.decode(data, len);
-                               buf_offset += size;
+
+                               unsigned short func;
+                               packet_read_short(pkt, (short *)&func);
+                               if(func==FUNC_GLDBREAK)
+                               {
+                                       packet_read_short(pkt, (short *)&func);
+                                       IO::print("Breakpoint: %s\n", get_function_name(func));
+                               }
+
+                               for(list<Tool *>::iterator i=tools.begin(); i!=tools.end(); ++i)
+                                       (*i)->decode(data, len);
+                               buf_offset += len;
                        }
                        if(buf_offset>8192)
                        {
                                buffer.erase(0, buf_offset);
-                               buf_offset=0;
+                               buf_offset = 0;
                        }
                }
        }