+ signal(SIGINT, &sighandler);
+ signal(SIGCHLD, &sighandler);
+
+ printf("GLdbg 0.0\n");
+ printf("Copyright © 2009-2010 Mikkosoft Productions\n");
+ printf("Type \"help\" for a list of commands\n");
+
+ while(!done)
+ tick();
+
+ return 0;
+}
+
+void GlDbg::launch()
+{
+ if(process.get_state()!=Process::INACTIVE)
+ throw runtime_error("Program is already running");
+
+ int fds[2];
+ socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
+ sock_fd = fds[0];
+
+ int flags = fcntl(sock_fd, F_GETFD);
+ fcntl(sock_fd, F_SETFD, flags|FD_CLOEXEC);
+
+ process.setenv("GLWRAP_FD", strformat("%d", fds[1]));
+ process.setenv("GLWRAP_CTRL_FD", strformat("%d", fds[1]));
+ process.launch();
+ close(fds[1]);
+
+ for(BreakList::iterator i=breakpoints.begin(); i!=breakpoints.end(); )
+ {
+ if(i->has_owner(0))
+ {
+ i->owners.clear();
+ i->owners.push_back(0);
+ send_breakpoint(i->function, i->flag, 0);
+ ++i;
+ }
+ else
+ breakpoints.erase(i++);
+ }
+
+ for(ToolList::iterator i=tools.begin(); i!=tools.end(); ++i)
+ (*i)->process_started();
+}
+
+void GlDbg::send(GlPacket *pkt)
+{
+ if(process.get_state()==Process::INACTIVE)
+ throw runtime_error("Program is not running");
+
+ packet_send(pkt, sock_fd);
+}
+
+void GlDbg::hold()
+{
+ GlPacket *pkt = packet_begin(FUNC_GLDHOLD);
+ send(pkt);
+}
+
+void GlDbg::send_breakpoint(unsigned short func, unsigned char set_flags, unsigned char clear_flags)
+{
+ GlPacket *pkt = packet_begin(FUNC_GLDBREAK);
+ packet_write_short(pkt, func);
+ packet_write_char(pkt, set_flags);
+ packet_write_char(pkt, clear_flags);
+ send(pkt);
+}
+
+void GlDbg::set_breakpoint(unsigned short func, unsigned char flag, Tool *owner)
+{
+ Breakpoint *bp = (func ? get_breakpoint(func, flag) : 0);
+ if(bp)
+ bp->add_owner(owner);
+ else
+ {
+ if(func)
+ {
+ breakpoints.push_back(Breakpoint(func, flag));
+ breakpoints.back().add_owner(owner);
+ }
+
+ if(process.get_state()!=Process::INACTIVE)
+ send_breakpoint(func, flag, 0);
+ }
+}
+
+void GlDbg::clear_breakpoint(unsigned short func, unsigned char flag, Tool *owner)
+{
+ Breakpoint *bp = get_breakpoint(func, flag);
+ if(bp)
+ {
+ bp->remove_owner(owner);
+ if(bp->owners.empty())
+ {
+ if(current_break==bp)
+ current_break = 0;
+
+ // XXX Inefficient. Should get_breakpoint() return an iterator?
+ for(BreakList::iterator i=breakpoints.begin(); i!=breakpoints.end(); ++i)
+ if(i->function==func && i->flag==flag)
+ {
+ breakpoints.erase(i);
+ break;
+ }