9 #include <sys/socket.h>
10 #include <readline/readline.h>
11 #include "functions.h"
13 #include "gldecoder.h"
15 #include "strformat.h"
20 GlDbg *GlDbg::instance = 0;
22 GlDbg::GlDbg(int argc, char **argv):
24 process(vector<string>(argv+1, argv+argc)),
34 unsigned len = readlink("/proc/self/exe", buf, sizeof(buf));
36 string::size_type slash = exe.rfind('/');
37 process.setenv("LD_PRELOAD", (exe.substr(0, slash+1)+"glwrap.so"));
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));
46 for(ToolList::iterator i=tools.begin(); i!=tools.end(); ++i)
52 signal(SIGINT, &sighandler);
53 signal(SIGCHLD, &sighandler);
55 printf("GLdbg 0.0\n");
56 printf("Copyright © 2009-2010 Mikkosoft Productions\n");
57 printf("Type \"help\" for a list of commands\n");
67 if(process.get_state()!=Process::INACTIVE)
68 throw runtime_error("Program is already running");
71 socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
74 int flags = fcntl(sock_fd, F_GETFD);
75 fcntl(sock_fd, F_SETFD, flags|FD_CLOEXEC);
77 process.setenv("GLWRAP_FD", strformat("%d", fds[1]));
78 process.setenv("GLWRAP_CTRL_FD", strformat("%d", fds[1]));
82 for(BreakList::iterator i=breakpoints.begin(); i!=breakpoints.end(); )
87 i->owners.push_back(0);
88 send_breakpoint(i->function, i->flag, 0);
92 breakpoints.erase(i++);
95 for(ToolList::iterator i=tools.begin(); i!=tools.end(); ++i)
96 (*i)->process_started();
99 void GlDbg::send(GlPacket *pkt)
101 if(process.get_state()==Process::INACTIVE)
102 throw runtime_error("Program is not running");
104 packet_send(pkt, sock_fd);
109 GlPacket *pkt = packet_begin(FUNC_GLDHOLD);
113 void GlDbg::send_breakpoint(unsigned short func, unsigned char set_flags, unsigned char clear_flags)
115 GlPacket *pkt = packet_begin(FUNC_GLDBREAK);
116 packet_write_short(pkt, func);
117 packet_write_char(pkt, set_flags);
118 packet_write_char(pkt, clear_flags);
122 void GlDbg::set_breakpoint(unsigned short func, unsigned char flag, Tool *owner)
124 Breakpoint *bp = (func ? get_breakpoint(func, flag) : 0);
126 bp->add_owner(owner);
131 breakpoints.push_back(Breakpoint(func, flag));
132 breakpoints.back().add_owner(owner);
135 if(process.get_state()>=Process::RUNNING)
136 send_breakpoint(func, flag, 0);
140 void GlDbg::clear_breakpoint(unsigned short func, unsigned char flag, Tool *owner)
142 Breakpoint *bp = get_breakpoint(func, flag);
145 bp->remove_owner(owner);
146 if(bp->owners.empty())
148 if(current_break==bp)
151 // XXX Inefficient. Should get_breakpoint() return an iterator?
152 for(BreakList::iterator i=breakpoints.begin(); i!=breakpoints.end(); ++i)
153 if(i->function==func && i->flag==flag)
155 breakpoints.erase(i);
159 if(process.get_state()>=Process::RUNNING)
160 send_breakpoint(func, 0, flag);
165 void GlDbg::resume_from_break(Tool *tool)
170 ToolList::iterator i = find(break_holders.begin(), break_holders.end(), tool);
171 if(i!=break_holders.end())
172 break_holders.erase(i);
174 if(break_holders.empty())
178 void GlDbg::quit(bool force)
180 if(!force && process.get_state()!=Process::INACTIVE)
181 throw runtime_error("Program is still running");
190 stop_reason = process.check();
191 if((stop_reason>>8)==3)
195 Process::State pstate = process.get_state();
196 if((pstate!=Process::INACTIVE && pstate!=Process::STOPPED) || flushing)
202 if(stop_reason==0x100)
203 printf("Target process exited normally\n");
204 else if((stop_reason>>8)==1)
205 printf("Target process exited with status %d\n", stop_reason&0xFF);
206 else if((stop_reason>>8)==2)
207 printf("Target process terminated with signal %d\n", stop_reason&0xFF);
208 else if((stop_reason>>8)==3)
209 printf("Target process stopped by signal %d\n", stop_reason&0xFF);
214 char *line = readline("gldbg> ");
219 cmd_interp.execute(line);
221 catch(const exception &e)
223 printf("%s\n", e.what());
227 else if(pstate==Process::INACTIVE)
232 void GlDbg::read_stream()
234 pollfd pfd = { sock_fd, POLLIN, 0 };
235 int ret = poll(&pfd, 1, (flushing ? 0 : -1));
239 ret = read(sock_fd, rbuf, 1024);
242 buffer.append(rbuf, ret);
243 while(buffer.size()>buf_offset)
245 const char *data = buffer.data()+buf_offset;
246 unsigned len = buffer.size()-buf_offset;
247 GlPacket *pkt = packet_receive_str(data, &len);
252 packet_read_short(pkt, (short *)&func);
253 if(func==FUNC_GLDBREAK)
255 packet_read_short(pkt, (short *)&func);
257 packet_read_char(pkt, (char *)&flag);
259 current_break = get_breakpoint(func, flag);
260 bool announce = !current_break;
263 break_holders = current_break->owners;
264 announce = current_break->has_owner(0);
268 printf("Breakpoint: %s\n", get_function_name(func));
271 for(ToolList::iterator i=tools.begin(); i!=tools.end(); ++i)
272 (*i)->decode(data, len);
277 buffer.erase(0, buf_offset);
288 for(ToolList::iterator i=tools.begin(); i!=tools.end(); ++i)
289 (*i)->process_stopped(stop_reason);
291 if(process.get_state()==Process::RUNNING)
297 GlDbg::Breakpoint *GlDbg::get_breakpoint(unsigned short func, unsigned char flag)
299 for(BreakList::iterator i=breakpoints.begin(); i!=breakpoints.end(); ++i)
300 if(i->function==func && i->flag==flag)
306 void GlDbg::sighandler(int sig)
309 instance->got_sigchld = true;
313 GlDbg::Breakpoint::Breakpoint(unsigned short f, unsigned char l):
318 void GlDbg::Breakpoint::add_owner(Tool *t)
320 ToolList::iterator i = find(owners.begin(), owners.end(), t);
325 bool GlDbg::Breakpoint::has_owner(Tool *t) const
327 ToolList::const_iterator i = find(owners.begin(), owners.end(), t);
328 return i!=owners.end();
331 void GlDbg::Breakpoint::remove_owner(Tool *t)
333 ToolList::iterator i = find(owners.begin(), owners.end(), t);