3 This file is part of gldbg
4 Copyright © 2009-2010 Mikko Rasa, Mikkosoft Productions
5 Distributed under the GPL
12 #include <sys/socket.h>
13 #include <readline/readline.h>
14 #include <msp/core/except.h>
15 #include <msp/fs/dir.h>
16 #include <msp/io/print.h>
17 #include <msp/strings/lexicalcast.h>
19 #include "gldecoder.h"
25 Application::RegApp<GlDbg> GlDbg::reg;
27 GlDbg::GlDbg(int argc, char **argv):
29 process(vector<string>(argv+1, argv+argc)),
34 FS::Path libdir = FS::get_sys_lib_dir(argv[0], "gldbg");
35 process.setenv("LD_PRELOAD", (libdir/"glwrap.so").str().c_str());
37 const list<Tool::Factory *> &factories = Tool::get_factories();
38 for(list<Tool::Factory *>::const_iterator i=factories.begin(); i!=factories.end(); ++i)
39 tools.push_back((*i)->create(*this));
44 for(list<Tool *>::iterator i=tools.begin(); i!=tools.end(); ++i)
51 catch_signal(SIGCHLD);
52 set_loop_mode(TICK_BUSY);
54 IO::print("GLdbg 0.0\n");
55 IO::print("Copyright © 2009-2010 Mikkosoft Productions\n");
56 IO::print("Type \"help\" for a list of commands\n");
65 if(process.get_state()!=Process::INACTIVE)
66 throw InvalidState("Program is already running");
69 socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
72 int flags = fcntl(sock_fd, F_GETFD);
73 fcntl(sock_fd, F_SETFD, flags|FD_CLOEXEC);
75 process.setenv("GLWRAP_FD", lexical_cast(fds[1]));
80 void GlDbg::quit(bool force)
82 if(!force && process.get_state()!=Process::INACTIVE)
83 throw InvalidState("Program is still running");
92 int ret = process.check();
94 IO::print("Target process exited normally\n");
96 IO::print("Target process exited with status %d\n", ret&0xFF);
98 IO::print("Target process terminated with signal %d\n", ret&0xFF);
101 IO::print("Target process stopped by signal %d\n", ret&0xFF);
106 Process::State pstate = process.get_state();
107 if((pstate!=Process::INACTIVE && pstate!=Process::STOPPED) || flushing)
111 char *line = readline("gldbg> ");
116 cmd_interp.execute(line);
118 catch(const Exception &e)
120 IO::print("%s\n", e.what());
124 else if(pstate==Process::INACTIVE)
129 void GlDbg::read_stream()
131 pollfd pfd = { sock_fd, POLLIN, 0 };
132 int ret = poll(&pfd, 1, (flushing ? 0 : -1));
136 ret = read(sock_fd, rbuf, 1024);
139 buffer.append(rbuf, ret);
140 while(buffer.size()>buf_offset)
142 const char *data = buffer.data()+buf_offset;
143 unsigned len = buffer.size()-buf_offset;
144 int size = gldecoder_decode(0, data, len);
147 for(list<Tool *>::iterator i=tools.begin(); i!=tools.end(); ++i)
148 (*i)->decode(data, size);
153 buffer.erase(0, buf_offset);
162 void GlDbg::sighandler(int sig)