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