]> git.tdb.fi Git - gldbg.git/blob - source/gldbg.cpp
Replace per-file license notices with License.txt
[gldbg.git] / source / gldbg.cpp
1 #include <algorithm>
2 #include <stdexcept>
3 #include <cstdlib>
4 #include <climits>
5 #include <fcntl.h>
6 #include <signal.h>
7 #include <unistd.h>
8 #include <sys/poll.h>
9 #include <sys/socket.h>
10 #include <readline/readline.h>
11 #include "functions.h"
12 #include "gldbg.h"
13 #include "gldecoder.h"
14 #include "packet.h"
15 #include "strformat.h"
16 #include "tool.h"
17
18 using namespace std;
19
20 GlDbg *GlDbg::instance = 0;
21
22 GlDbg::GlDbg(int argc, char **argv):
23         cmd_interp(*this),
24         process(vector<string>(argv+1, argv+argc)),
25         buf_offset(0),
26         flushing(false),
27         got_sigchld(false),
28         stop_reason(0),
29         current_break(0)
30 {
31         instance = this;
32
33         char buf[PATH_MAX];
34         unsigned len = readlink("/proc/self/exe", buf, sizeof(buf));
35         string exe(buf, len);
36         string::size_type slash = exe.rfind('/');
37         process.setenv("LD_PRELOAD", (exe.substr(0, slash+1)+"glwrap.so"));
38
39         const list<Tool::Factory *> &factories = Tool::get_factories();
40         for(list<Tool::Factory *>::const_iterator i=factories.begin(); i!=factories.end(); ++i)
41                 tools.push_back((*i)->create(*this));
42 }
43
44 GlDbg::~GlDbg()
45 {
46         for(ToolList::iterator i=tools.begin(); i!=tools.end(); ++i)
47                 delete *i;
48 }
49
50 int GlDbg::main()
51 {
52         signal(SIGINT, &sighandler);
53         signal(SIGCHLD, &sighandler);
54
55         printf("GLdbg 0.0\n");
56         printf("Copyright © 2009-2010 Mikkosoft Productions\n");
57         printf("Type \"help\" for a list of commands\n");
58
59         while(1)
60                 tick();
61
62         return 0;
63 }
64
65 void GlDbg::launch()
66 {
67         if(process.get_state()!=Process::INACTIVE)
68                 throw runtime_error("Program is already running");
69
70         int fds[2];
71         socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
72         sock_fd = fds[0];
73
74         int flags = fcntl(sock_fd, F_GETFD);
75         fcntl(sock_fd, F_SETFD, flags|FD_CLOEXEC);
76
77         process.setenv("GLWRAP_FD", strformat("%d", fds[1]));
78         process.setenv("GLWRAP_CTRL_FD", strformat("%d", fds[1]));
79         process.launch();
80         close(fds[1]);
81
82         breakpoints.clear();
83         for(ToolList::iterator i=tools.begin(); i!=tools.end(); ++i)
84                 (*i)->process_started();
85 }
86
87 void GlDbg::send(GlPacket *pkt)
88 {
89         packet_send(pkt, sock_fd);
90 }
91
92 void GlDbg::hold()
93 {
94         GlPacket *pkt = packet_begin(FUNC_GLDHOLD);
95         send(pkt);
96 }
97
98 void GlDbg::set_breakpoint(unsigned short func, unsigned char flag, Tool *owner)
99 {
100         Breakpoint *bp = (func ? get_breakpoint(func, flag) : 0);
101         if(bp)
102                 bp->add_owner(owner);
103         else
104         {
105                 if(func)
106                 {
107                         breakpoints.push_back(Breakpoint(func, flag));
108                         breakpoints.back().add_owner(owner);
109                 }
110
111                 GlPacket *pkt = packet_begin(FUNC_GLDBREAK);
112                 packet_write_short(pkt, func);
113                 packet_write_char(pkt, flag);
114                 packet_write_char(pkt, 0);
115                 send(pkt);
116         }
117 }
118
119 void GlDbg::clear_breakpoint(unsigned short func, unsigned char flag, Tool *owner)
120 {
121         Breakpoint *bp = get_breakpoint(func, flag);
122         if(bp)
123         {
124                 bp->remove_owner(owner);
125                 if(bp->owners.empty())
126                 {
127                         if(current_break==bp)
128                                 current_break = 0;
129
130                         // XXX Inefficient.  Should get_breakpoint() return an iterator?
131                         for(BreakList::iterator i=breakpoints.begin(); i!=breakpoints.end(); ++i)
132                                 if(i->function==func && i->flag==flag)
133                                 {
134                                         breakpoints.erase(i);
135                                         break;
136                                 }
137
138                         GlPacket *pkt = packet_begin(FUNC_GLDBREAK);
139                         packet_write_short(pkt, func);
140                         packet_write_char(pkt, 0);
141                         packet_write_char(pkt, flag);
142                         send(pkt);
143                 }
144         }
145 }
146
147 void GlDbg::resume_from_break(Tool *tool)
148 {
149         if(!current_break)
150                 return;
151
152         ToolList::iterator i = find(break_holders.begin(), break_holders.end(), tool);
153         if(i!=break_holders.end())
154                 break_holders.erase(i);
155
156         if(break_holders.empty())
157                 process.resume();
158 }
159
160 void GlDbg::quit(bool force)
161 {
162         if(!force && process.get_state()!=Process::INACTIVE)
163                 throw runtime_error("Program is still running");
164         exit(0);
165 }
166
167 void GlDbg::tick()
168 {
169         if(got_sigchld)
170         {
171                 got_sigchld = false;
172                 stop_reason = process.check();
173                 if((stop_reason>>8)==3)
174                         flushing = true;
175         }
176
177         Process::State pstate = process.get_state();
178         if((pstate!=Process::INACTIVE && pstate!=Process::STOPPED) || flushing)
179                 read_stream();
180         else
181         {
182                 if(stop_reason)
183                 {
184                         if(stop_reason==0x100)
185                                 printf("Target process exited normally\n");
186                         else if((stop_reason>>8)==1)
187                                 printf("Target process exited with status %d\n", stop_reason&0xFF);
188                         else if((stop_reason>>8)==2)
189                                 printf("Target process terminated with signal %d\n", stop_reason&0xFF);
190                         else if((stop_reason>>8)==3)
191                                 printf("Target process stopped by signal %d\n", stop_reason&0xFF);
192
193                         stop_reason = 0;
194                 }
195
196                 char *line = readline("gldbg> ");
197                 if(line)
198                 {
199                         try
200                         {
201                                 cmd_interp.execute(line);
202                         }
203                         catch(const exception &e)
204                         {
205                                 printf("%s\n", e.what());
206                         }
207                         free(line);
208                 }
209                 else if(pstate==Process::INACTIVE)
210                         exit(0);
211         }
212 }
213
214 void GlDbg::read_stream()
215 {
216         pollfd pfd = { sock_fd, POLLIN, 0 };
217         int ret = poll(&pfd, 1, (flushing ? 0 : -1));
218         if(ret>0)
219         {
220                 char rbuf[1024];
221                 ret = read(sock_fd, rbuf, 1024);
222                 if(ret>0)
223                 {
224                         buffer.append(rbuf, ret);
225                         while(buffer.size()>buf_offset)
226                         {
227                                 const char *data = buffer.data()+buf_offset;
228                                 unsigned len = buffer.size()-buf_offset;
229                                 GlPacket *pkt = packet_receive_str(data, &len);
230                                 if(!pkt)
231                                         break;
232
233                                 unsigned short func;
234                                 packet_read_short(pkt, (short *)&func);
235                                 if(func==FUNC_GLDBREAK)
236                                 {
237                                         packet_read_short(pkt, (short *)&func);
238                                         unsigned char flag;
239                                         packet_read_char(pkt, (char *)&flag);
240
241                                         current_break = get_breakpoint(func, flag);
242                                         bool announce = !current_break;
243                                         if(current_break)
244                                         {
245                                                 break_holders = current_break->owners;
246                                                 announce = current_break->has_owner(0);
247                                         }
248
249                                         if(announce)
250                                                 printf("Breakpoint: %s\n", get_function_name(func));
251                                 }
252
253                                 for(ToolList::iterator i=tools.begin(); i!=tools.end(); ++i)
254                                         (*i)->decode(data, len);
255                                 buf_offset += len;
256                         }
257                         if(buf_offset>8192)
258                         {
259                                 buffer.erase(0, buf_offset);
260                                 buf_offset = 0;
261                         }
262                 }
263         }
264         else if(flushing)
265         {
266                 flushing = false;
267
268                 if(stop_reason)
269                 {
270                         for(ToolList::iterator i=tools.begin(); i!=tools.end(); ++i)
271                                 (*i)->process_stopped(stop_reason);
272
273                         if(process.get_state()==Process::RUNNING)
274                                 current_break = 0;
275                 }
276         }
277 }
278
279 GlDbg::Breakpoint *GlDbg::get_breakpoint(unsigned short func, unsigned char flag)
280 {
281         for(BreakList::iterator i=breakpoints.begin(); i!=breakpoints.end(); ++i)
282                 if(i->function==func && i->flag==flag)
283                         return &*i;
284
285         return 0;
286 }
287
288 void GlDbg::sighandler(int sig)
289 {
290         if(sig==SIGCHLD)
291                 instance->got_sigchld = true;
292 }
293
294
295 GlDbg::Breakpoint::Breakpoint(unsigned short f, unsigned char l):
296         function(f),
297         flag(l)
298 { }
299
300 void GlDbg::Breakpoint::add_owner(Tool *t)
301 {
302         ToolList::iterator i = find(owners.begin(), owners.end(), t);
303         if(i==owners.end())
304                 owners.push_back(t);
305 }
306
307 bool GlDbg::Breakpoint::has_owner(Tool *t) const
308 {
309         ToolList::const_iterator i = find(owners.begin(), owners.end(), t);
310         return i!=owners.end();
311 }
312
313 void GlDbg::Breakpoint::remove_owner(Tool *t)
314 {
315         ToolList::iterator i = find(owners.begin(), owners.end(), t);
316         if(i!=owners.end())
317                 owners.erase(i);
318 }