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 packet_send(pkt, sock_fd);
106 GlPacket *pkt = packet_begin(FUNC_GLDHOLD);
110 void GlDbg::send_breakpoint(unsigned short func, unsigned char set_flags, unsigned char clear_flags)
112 GlPacket *pkt = packet_begin(FUNC_GLDBREAK);
113 packet_write_short(pkt, func);
114 packet_write_char(pkt, set_flags);
115 packet_write_char(pkt, clear_flags);
119 void GlDbg::set_breakpoint(unsigned short func, unsigned char flag, Tool *owner)
121 Breakpoint *bp = (func ? get_breakpoint(func, flag) : 0);
123 bp->add_owner(owner);
128 breakpoints.push_back(Breakpoint(func, flag));
129 breakpoints.back().add_owner(owner);
132 if(process.get_state()>=Process::RUNNING)
133 send_breakpoint(func, flag, 0);
137 void GlDbg::clear_breakpoint(unsigned short func, unsigned char flag, Tool *owner)
139 Breakpoint *bp = get_breakpoint(func, flag);
142 bp->remove_owner(owner);
143 if(bp->owners.empty())
145 if(current_break==bp)
148 // XXX Inefficient. Should get_breakpoint() return an iterator?
149 for(BreakList::iterator i=breakpoints.begin(); i!=breakpoints.end(); ++i)
150 if(i->function==func && i->flag==flag)
152 breakpoints.erase(i);
156 if(process.get_state()>=Process::RUNNING)
157 send_breakpoint(func, 0, flag);
162 void GlDbg::resume_from_break(Tool *tool)
167 ToolList::iterator i = find(break_holders.begin(), break_holders.end(), tool);
168 if(i!=break_holders.end())
169 break_holders.erase(i);
171 if(break_holders.empty())
175 void GlDbg::quit(bool force)
177 if(!force && process.get_state()!=Process::INACTIVE)
178 throw runtime_error("Program is still running");
187 stop_reason = process.check();
188 if((stop_reason>>8)==3)
192 Process::State pstate = process.get_state();
193 if((pstate!=Process::INACTIVE && pstate!=Process::STOPPED) || flushing)
199 if(stop_reason==0x100)
200 printf("Target process exited normally\n");
201 else if((stop_reason>>8)==1)
202 printf("Target process exited with status %d\n", stop_reason&0xFF);
203 else if((stop_reason>>8)==2)
204 printf("Target process terminated with signal %d\n", stop_reason&0xFF);
205 else if((stop_reason>>8)==3)
206 printf("Target process stopped by signal %d\n", stop_reason&0xFF);
211 char *line = readline("gldbg> ");
216 cmd_interp.execute(line);
218 catch(const exception &e)
220 printf("%s\n", e.what());
224 else if(pstate==Process::INACTIVE)
229 void GlDbg::read_stream()
231 pollfd pfd = { sock_fd, POLLIN, 0 };
232 int ret = poll(&pfd, 1, (flushing ? 0 : -1));
236 ret = read(sock_fd, rbuf, 1024);
239 buffer.append(rbuf, ret);
240 while(buffer.size()>buf_offset)
242 const char *data = buffer.data()+buf_offset;
243 unsigned len = buffer.size()-buf_offset;
244 GlPacket *pkt = packet_receive_str(data, &len);
249 packet_read_short(pkt, (short *)&func);
250 if(func==FUNC_GLDBREAK)
252 packet_read_short(pkt, (short *)&func);
254 packet_read_char(pkt, (char *)&flag);
256 current_break = get_breakpoint(func, flag);
257 bool announce = !current_break;
260 break_holders = current_break->owners;
261 announce = current_break->has_owner(0);
265 printf("Breakpoint: %s\n", get_function_name(func));
268 for(ToolList::iterator i=tools.begin(); i!=tools.end(); ++i)
269 (*i)->decode(data, len);
274 buffer.erase(0, buf_offset);
285 for(ToolList::iterator i=tools.begin(); i!=tools.end(); ++i)
286 (*i)->process_stopped(stop_reason);
288 if(process.get_state()==Process::RUNNING)
294 GlDbg::Breakpoint *GlDbg::get_breakpoint(unsigned short func, unsigned char flag)
296 for(BreakList::iterator i=breakpoints.begin(); i!=breakpoints.end(); ++i)
297 if(i->function==func && i->flag==flag)
303 void GlDbg::sighandler(int sig)
306 instance->got_sigchld = true;
310 GlDbg::Breakpoint::Breakpoint(unsigned short f, unsigned char l):
315 void GlDbg::Breakpoint::add_owner(Tool *t)
317 ToolList::iterator i = find(owners.begin(), owners.end(), t);
322 bool GlDbg::Breakpoint::has_owner(Tool *t) const
324 ToolList::const_iterator i = find(owners.begin(), owners.end(), t);
325 return i!=owners.end();
328 void GlDbg::Breakpoint::remove_owner(Tool *t)
330 ToolList::iterator i = find(owners.begin(), owners.end(), t);