]> git.tdb.fi Git - gldbg.git/blob - source/gldbg.cpp
7990fabae61f6f9ed4b1daef344b5715ded4004f
[gldbg.git] / source / gldbg.cpp
1 /* $Id$
2
3 This file is part of gldbg
4 Copyright © 2009-2010  Mikko Rasa, Mikkosoft Productions
5 Distributed under the GPL
6 */
7
8 #include <cstdlib>
9 #include <fcntl.h>
10 #include <signal.h>
11 #include <sys/poll.h>
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>
18 #include "gldbg.h"
19 #include "gldecoder.h"
20 #include "tool.h"
21
22 using namespace std;
23 using namespace Msp;
24
25 Application::RegApp<GlDbg> GlDbg::reg;
26
27 GlDbg::GlDbg(int argc, char **argv):
28         cmd_interp(*this),
29         process(vector<string>(argv+1, argv+argc)),
30         buf_offset(0),
31         flushing(false),
32         got_sigchld(false)
33 {
34         FS::Path libdir = FS::get_sys_lib_dir(argv[0], "gldbg");
35         process.setenv("LD_PRELOAD", (libdir/"glwrap.so").str().c_str());
36
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));
40 }
41
42 GlDbg::~GlDbg()
43 {
44         for(list<Tool *>::iterator i=tools.begin(); i!=tools.end(); ++i)
45                 delete *i;
46 }
47
48 int GlDbg::main()
49 {
50         catch_signal(SIGINT);
51         catch_signal(SIGCHLD);
52         set_loop_mode(TICK_BUSY);
53
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");
57
58         Application::main();
59
60         return 0;
61 }
62
63 void GlDbg::launch()
64 {
65         if(process.get_state()!=Process::INACTIVE)
66                 throw InvalidState("Program is already running");
67
68         int fds[2];
69         socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
70         sock_fd = fds[0];
71
72         int flags = fcntl(sock_fd, F_GETFD);
73         fcntl(sock_fd, F_SETFD, flags|FD_CLOEXEC);
74
75         process.setenv("GLWRAP_FD", lexical_cast(fds[1]));
76         process.launch();
77         close(fds[1]);
78 }
79         
80 void GlDbg::quit(bool force)
81 {
82         if(!force && process.get_state()!=Process::INACTIVE)
83                 throw InvalidState("Program is still running");
84         exit(0);
85 }
86
87 void GlDbg::tick()
88 {
89         if(got_sigchld)
90         {
91                 got_sigchld = false;
92                 int ret = process.check();
93                 if(ret==0x100)
94                         IO::print("Target process exited normally\n");
95                 else if((ret>>8)==1)
96                         IO::print("Target process exited with status %d\n", ret&0xFF);
97                 else if((ret>>8)==2)
98                         IO::print("Target process terminated with signal %d\n", ret&0xFF);
99                 else if((ret>>8)==3)
100                 {
101                         IO::print("Target process stopped by signal %d\n", ret&0xFF);
102                         flushing = true;
103                 }
104         }
105
106         Process::State pstate = process.get_state();
107         if((pstate!=Process::INACTIVE && pstate!=Process::STOPPED) || flushing)
108                 read_stream();
109         else
110         {
111                 char *line = readline("gldbg> ");
112                 if(line)
113                 {
114                         try
115                         {
116                                 cmd_interp.execute(line);
117                         }
118                         catch(const Exception &e)
119                         {
120                                 IO::print("%s\n", e.what());
121                         }
122                         free(line);
123                 }
124                 else if(pstate==Process::INACTIVE)
125                         exit(0);
126         }
127 }
128
129 void GlDbg::read_stream()
130 {
131         pollfd pfd = { sock_fd, POLLIN, 0 };
132         int ret = poll(&pfd, 1, (flushing ? 0 : -1));
133         if(ret>0)
134         {
135                 char rbuf[1024];
136                 ret = read(sock_fd, rbuf, 1024);
137                 if(ret>0)
138                 {
139                         buffer.append(rbuf, ret);
140                         while(buffer.size()>buf_offset)
141                         {
142                                 const char *data = buffer.data()+buf_offset;
143                                 unsigned len = buffer.size()-buf_offset;
144                                 int size = gldecoder_decode(0, data, len);
145                                 if(size<0)
146                                         break;
147                                 for(list<Tool *>::iterator i=tools.begin(); i!=tools.end(); ++i)
148                                         (*i)->decode(data, size);
149                                 buf_offset += size;
150                         }
151                         if(buf_offset>8192)
152                         {
153                                 buffer.erase(0, buf_offset);
154                                 buf_offset = 0;
155                         }
156                 }
157         }
158         else if(flushing)
159                 flushing = false;
160 }
161
162 void GlDbg::sighandler(int sig)
163 {
164         if(sig==SIGCHLD)
165                 got_sigchld = true;
166 }