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]));
83 for(ToolList::iterator i=tools.begin(); i!=tools.end(); ++i)
84 (*i)->process_started();
87 void GlDbg::send(GlPacket *pkt)
89 packet_send(pkt, sock_fd);
94 GlPacket *pkt = packet_begin(FUNC_GLDHOLD);
98 void GlDbg::set_breakpoint(unsigned short func, unsigned char flag, Tool *owner)
100 Breakpoint *bp = (func ? get_breakpoint(func, flag) : 0);
102 bp->add_owner(owner);
107 breakpoints.push_back(Breakpoint(func, flag));
108 breakpoints.back().add_owner(owner);
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);
119 void GlDbg::clear_breakpoint(unsigned short func, unsigned char flag, Tool *owner)
121 Breakpoint *bp = get_breakpoint(func, flag);
124 bp->remove_owner(owner);
125 if(bp->owners.empty())
127 if(current_break==bp)
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)
134 breakpoints.erase(i);
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);
147 void GlDbg::resume_from_break(Tool *tool)
152 ToolList::iterator i = find(break_holders.begin(), break_holders.end(), tool);
153 if(i!=break_holders.end())
154 break_holders.erase(i);
156 if(break_holders.empty())
160 void GlDbg::quit(bool force)
162 if(!force && process.get_state()!=Process::INACTIVE)
163 throw runtime_error("Program is still running");
172 stop_reason = process.check();
173 if((stop_reason>>8)==3)
177 Process::State pstate = process.get_state();
178 if((pstate!=Process::INACTIVE && pstate!=Process::STOPPED) || flushing)
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);
196 char *line = readline("gldbg> ");
201 cmd_interp.execute(line);
203 catch(const exception &e)
205 printf("%s\n", e.what());
209 else if(pstate==Process::INACTIVE)
214 void GlDbg::read_stream()
216 pollfd pfd = { sock_fd, POLLIN, 0 };
217 int ret = poll(&pfd, 1, (flushing ? 0 : -1));
221 ret = read(sock_fd, rbuf, 1024);
224 buffer.append(rbuf, ret);
225 while(buffer.size()>buf_offset)
227 const char *data = buffer.data()+buf_offset;
228 unsigned len = buffer.size()-buf_offset;
229 GlPacket *pkt = packet_receive_str(data, &len);
234 packet_read_short(pkt, (short *)&func);
235 if(func==FUNC_GLDBREAK)
237 packet_read_short(pkt, (short *)&func);
239 packet_read_char(pkt, (char *)&flag);
241 current_break = get_breakpoint(func, flag);
242 bool announce = !current_break;
245 break_holders = current_break->owners;
246 announce = current_break->has_owner(0);
250 printf("Breakpoint: %s\n", get_function_name(func));
253 for(ToolList::iterator i=tools.begin(); i!=tools.end(); ++i)
254 (*i)->decode(data, len);
259 buffer.erase(0, buf_offset);
270 for(ToolList::iterator i=tools.begin(); i!=tools.end(); ++i)
271 (*i)->process_stopped(stop_reason);
273 if(process.get_state()==Process::RUNNING)
279 GlDbg::Breakpoint *GlDbg::get_breakpoint(unsigned short func, unsigned char flag)
281 for(BreakList::iterator i=breakpoints.begin(); i!=breakpoints.end(); ++i)
282 if(i->function==func && i->flag==flag)
288 void GlDbg::sighandler(int sig)
291 instance->got_sigchld = true;
295 GlDbg::Breakpoint::Breakpoint(unsigned short f, unsigned char l):
300 void GlDbg::Breakpoint::add_owner(Tool *t)
302 ToolList::iterator i = find(owners.begin(), owners.end(), t);
307 bool GlDbg::Breakpoint::has_owner(Tool *t) const
309 ToolList::const_iterator i = find(owners.begin(), owners.end(), t);
310 return i!=owners.end();
313 void GlDbg::Breakpoint::remove_owner(Tool *t)
315 ToolList::iterator i = find(owners.begin(), owners.end(), t);