3 This file is part of gldbg
4 Copyright © 2009-2011 Mikko Rasa, Mikkosoft Productions
5 Distributed under the GPL
16 #include <sys/socket.h>
17 #include <readline/readline.h>
18 #include "functions.h"
20 #include "gldecoder.h"
22 #include "strformat.h"
27 GlDbg *GlDbg::instance = 0;
29 GlDbg::GlDbg(int argc, char **argv):
31 process(vector<string>(argv+1, argv+argc)),
41 unsigned len = readlink("/proc/self/exe", buf, sizeof(buf));
43 string::size_type slash = exe.rfind('/');
44 process.setenv("LD_PRELOAD", (exe.substr(0, slash+1)+"glwrap.so"));
46 const list<Tool::Factory *> &factories = Tool::get_factories();
47 for(list<Tool::Factory *>::const_iterator i=factories.begin(); i!=factories.end(); ++i)
48 tools.push_back((*i)->create(*this));
53 for(ToolList::iterator i=tools.begin(); i!=tools.end(); ++i)
59 signal(SIGINT, &sighandler);
60 signal(SIGCHLD, &sighandler);
62 printf("GLdbg 0.0\n");
63 printf("Copyright © 2009-2010 Mikkosoft Productions\n");
64 printf("Type \"help\" for a list of commands\n");
74 if(process.get_state()!=Process::INACTIVE)
75 throw runtime_error("Program is already running");
78 socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
81 int flags = fcntl(sock_fd, F_GETFD);
82 fcntl(sock_fd, F_SETFD, flags|FD_CLOEXEC);
84 process.setenv("GLWRAP_FD", strformat("%d", fds[1]));
85 process.setenv("GLWRAP_CTRL_FD", strformat("%d", fds[1]));
90 for(ToolList::iterator i=tools.begin(); i!=tools.end(); ++i)
91 (*i)->process_started();
94 void GlDbg::send(GlPacket *pkt)
96 packet_send(pkt, sock_fd);
101 GlPacket *pkt = packet_begin(FUNC_GLDHOLD);
105 void GlDbg::set_breakpoint(unsigned short func, unsigned char flag, Tool *owner)
107 Breakpoint *bp = (func ? get_breakpoint(func, flag) : 0);
109 bp->add_owner(owner);
114 breakpoints.push_back(Breakpoint(func, flag));
115 breakpoints.back().add_owner(owner);
118 GlPacket *pkt = packet_begin(FUNC_GLDBREAK);
119 packet_write_short(pkt, func);
120 packet_write_char(pkt, flag);
121 packet_write_char(pkt, 0);
126 void GlDbg::clear_breakpoint(unsigned short func, unsigned char flag, Tool *owner)
128 Breakpoint *bp = get_breakpoint(func, flag);
131 bp->remove_owner(owner);
132 if(bp->owners.empty())
134 if(current_break==bp)
137 // XXX Inefficient. Should get_breakpoint() return an iterator?
138 for(BreakList::iterator i=breakpoints.begin(); i!=breakpoints.end(); ++i)
139 if(i->function==func && i->flag==flag)
141 breakpoints.erase(i);
145 GlPacket *pkt = packet_begin(FUNC_GLDBREAK);
146 packet_write_short(pkt, func);
147 packet_write_char(pkt, 0);
148 packet_write_char(pkt, flag);
154 void GlDbg::resume_from_break(Tool *tool)
159 ToolList::iterator i = find(break_holders.begin(), break_holders.end(), tool);
160 if(i!=break_holders.end())
161 break_holders.erase(i);
163 if(break_holders.empty())
167 void GlDbg::quit(bool force)
169 if(!force && process.get_state()!=Process::INACTIVE)
170 throw runtime_error("Program is still running");
179 stop_reason = process.check();
180 if((stop_reason>>8)==3)
184 Process::State pstate = process.get_state();
185 if((pstate!=Process::INACTIVE && pstate!=Process::STOPPED) || flushing)
191 if(stop_reason==0x100)
192 printf("Target process exited normally\n");
193 else if((stop_reason>>8)==1)
194 printf("Target process exited with status %d\n", stop_reason&0xFF);
195 else if((stop_reason>>8)==2)
196 printf("Target process terminated with signal %d\n", stop_reason&0xFF);
197 else if((stop_reason>>8)==3)
198 printf("Target process stopped by signal %d\n", stop_reason&0xFF);
203 char *line = readline("gldbg> ");
208 cmd_interp.execute(line);
210 catch(const exception &e)
212 printf("%s\n", e.what());
216 else if(pstate==Process::INACTIVE)
221 void GlDbg::read_stream()
223 pollfd pfd = { sock_fd, POLLIN, 0 };
224 int ret = poll(&pfd, 1, (flushing ? 0 : -1));
228 ret = read(sock_fd, rbuf, 1024);
231 buffer.append(rbuf, ret);
232 while(buffer.size()>buf_offset)
234 const char *data = buffer.data()+buf_offset;
235 unsigned len = buffer.size()-buf_offset;
236 GlPacket *pkt = packet_receive_str(data, &len);
241 packet_read_short(pkt, (short *)&func);
242 if(func==FUNC_GLDBREAK)
244 packet_read_short(pkt, (short *)&func);
246 packet_read_char(pkt, (char *)&flag);
248 current_break = get_breakpoint(func, flag);
249 bool announce = !current_break;
252 break_holders = current_break->owners;
253 announce = current_break->has_owner(0);
257 printf("Breakpoint: %s\n", get_function_name(func));
260 for(ToolList::iterator i=tools.begin(); i!=tools.end(); ++i)
261 (*i)->decode(data, len);
266 buffer.erase(0, buf_offset);
277 for(ToolList::iterator i=tools.begin(); i!=tools.end(); ++i)
278 (*i)->process_stopped(stop_reason);
280 if(process.get_state()==Process::RUNNING)
286 GlDbg::Breakpoint *GlDbg::get_breakpoint(unsigned short func, unsigned char flag)
288 for(BreakList::iterator i=breakpoints.begin(); i!=breakpoints.end(); ++i)
289 if(i->function==func && i->flag==flag)
295 void GlDbg::sighandler(int sig)
298 instance->got_sigchld = true;
302 GlDbg::Breakpoint::Breakpoint(unsigned short f, unsigned char l):
307 void GlDbg::Breakpoint::add_owner(Tool *t)
309 ToolList::iterator i = find(owners.begin(), owners.end(), t);
314 bool GlDbg::Breakpoint::has_owner(Tool *t) const
316 ToolList::const_iterator i = find(owners.begin(), owners.end(), t);
317 return i!=owners.end();
320 void GlDbg::Breakpoint::remove_owner(Tool *t)
322 ToolList::iterator i = find(owners.begin(), owners.end(), t);