]> git.tdb.fi Git - gldbg.git/blob - source/gldbg.cpp
Add framework necessary to support more modular tools
[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 "glprint.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 int GlDbg::main()
43 {
44         catch_signal(SIGINT);
45         catch_signal(SIGCHLD);
46         set_loop_mode(TICK_BUSY);
47
48         IO::print("GLdbg 0.0\n");
49         IO::print("Copyright © 2009 Mikkosoft Productions\n");
50         IO::print("Type \"help\" for a list of commands\n");
51
52         Application::main();
53
54         return 0;
55 }
56
57 void GlDbg::launch()
58 {
59         if(process.get_state()!=Process::INACTIVE)
60                 throw InvalidState("Program is already running");
61
62         int fds[2];
63         socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
64         sock_fd = fds[0];
65
66         int flags = fcntl(sock_fd, F_GETFD);
67         fcntl(sock_fd, F_SETFD, flags|FD_CLOEXEC);
68
69         process.setenv("GLWRAP_FD", lexical_cast(fds[1]));
70         process.launch();
71         close(fds[1]);
72 }
73         
74 void GlDbg::quit(bool force)
75 {
76         if(!force && process.get_state()!=Process::INACTIVE)
77                 throw InvalidState("Program is still running");
78         exit(0);
79 }
80
81 void GlDbg::tick()
82 {
83         if(got_sigchld)
84         {
85                 got_sigchld = false;
86                 int ret = process.check();
87                 if(ret==0x100)
88                         IO::print("Target process exited normally\n");
89                 else if((ret>>8)==1)
90                         IO::print("Target process exited with status %d\n", ret&0xFF);
91                 else if((ret>>8)==2)
92                         IO::print("Target process terminated with signal %d\n", ret&0xFF);
93                 else if((ret>>8)==3)
94                 {
95                         IO::print("Target process stopped by signal %d\n", ret&0xFF);
96                         flushing = true;
97                 }
98         }
99
100         Process::State pstate = process.get_state();
101         if((pstate!=Process::INACTIVE && pstate!=Process::STOPPED) || flushing)
102                 read_stream();
103         else
104         {
105                 char *line = readline("gldbg> ");
106                 if(line)
107                 {
108                         try
109                         {
110                                 cmd_interp.execute(line);
111                         }
112                         catch(const Exception &e)
113                         {
114                                 IO::print("%s\n", e.what());
115                         }
116                         free(line);
117                 }
118                 else if(pstate==Process::INACTIVE)
119                         exit(0);
120         }
121 }
122
123 void GlDbg::read_stream()
124 {
125         pollfd pfd = { sock_fd, POLLIN, 0 };
126         int ret = poll(&pfd, 1, (flushing ? 0 : -1));
127         if(ret>0)
128         {
129                 char rbuf[1024];
130                 ret = read(sock_fd, rbuf, 1024);
131                 if(ret>0)
132                 {
133                         buffer.append(rbuf, ret);
134                         while(buffer.size()>buf_offset)
135                         {
136                                 const char *data = buffer.data()+buf_offset;
137                                 unsigned len = buffer.size()-buf_offset;
138                                 int size = gldecoder_decode(0, data, len);
139                                 if(size<0)
140                                         break;
141                                 for(list<Tool *>::iterator i=tools.begin(); i!=tools.end(); ++i)
142                                         (*i)->decode(data, size);
143                                 tracer.decode(data, len);
144                                 glstate.decode(data, len);
145                                 profiler.decode(data, len);
146                                 buf_offset += size;
147                         }
148                         if(buf_offset>8192)
149                         {
150                                 buffer.erase(0, buf_offset);
151                                 buf_offset = 0;
152                         }
153                 }
154         }
155         else if(flushing)
156                 flushing = false;
157 }
158
159 void GlDbg::sighandler(int sig)
160 {
161         if(sig==SIGCHLD)
162                 got_sigchld = true;
163 }