X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fgldbg.cpp;h=001742d39599b85942702f797d8e8ed383d7a16d;hb=6d297b506314c07bff3d77c2853a5f59380cfcb0;hp=34e616f4ac37caa5d30da1708892206bc3dea597;hpb=2f49929fc383eab718b5fb64d966535b753e7024;p=gldbg.git diff --git a/source/gldbg.cpp b/source/gldbg.cpp index 34e616f..001742d 100644 --- a/source/gldbg.cpp +++ b/source/gldbg.cpp @@ -1,10 +1,11 @@ /* $Id$ This file is part of gldbg -Copyright © 2009 Mikko Rasa, Mikkosoft Productions +Copyright © 2009-2010 Mikko Rasa, Mikkosoft Productions Distributed under the GPL */ +#include #include #include #include @@ -15,8 +16,11 @@ Distributed under the GPL #include #include #include +#include "functions.h" #include "gldbg.h" -#include "glprint.h" +#include "gldecoder.h" +#include "packet.h" +#include "tool.h" using namespace std; using namespace Msp; @@ -28,10 +32,22 @@ GlDbg::GlDbg(int argc, char **argv): process(vector(argv+1, argv+argc)), buf_offset(0), flushing(false), - got_sigchld(false) + got_sigchld(false), + stop_reason(0), + current_break(0) { FS::Path libdir = FS::get_sys_lib_dir(argv[0], "gldbg"); process.setenv("LD_PRELOAD", (libdir/"glwrap.so").str().c_str()); + + const list &factories = Tool::get_factories(); + for(list::const_iterator i=factories.begin(); i!=factories.end(); ++i) + tools.push_back((*i)->create(*this)); +} + +GlDbg::~GlDbg() +{ + for(ToolList::iterator i=tools.begin(); i!=tools.end(); ++i) + delete *i; } int GlDbg::main() @@ -41,7 +57,7 @@ int GlDbg::main() set_loop_mode(TICK_BUSY); IO::print("GLdbg 0.0\n"); - IO::print("Copyright © 2009 Mikkosoft Productions\n"); + IO::print("Copyright © 2009-2010 Mikkosoft Productions\n"); IO::print("Type \"help\" for a list of commands\n"); Application::main(); @@ -62,10 +78,88 @@ void GlDbg::launch() fcntl(sock_fd, F_SETFD, flags|FD_CLOEXEC); process.setenv("GLWRAP_FD", lexical_cast(fds[1])); + process.setenv("GLWRAP_CTRL_FD", lexical_cast(fds[1])); process.launch(); close(fds[1]); + + breakpoints.clear(); + for(ToolList::iterator i=tools.begin(); i!=tools.end(); ++i) + (*i)->process_started(); +} + +void GlDbg::send(GlPacket *pkt) +{ + packet_send(pkt, sock_fd); +} + +void GlDbg::hold() +{ + GlPacket *pkt = packet_begin(FUNC_GLDHOLD); + 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); + } + + GlPacket *pkt = packet_begin(FUNC_GLDBREAK); + packet_write_short(pkt, func); + packet_write_char(pkt, flag); + packet_write_char(pkt, 0); + send(pkt); + } +} + +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; + } + + GlPacket *pkt = packet_begin(FUNC_GLDBREAK); + packet_write_short(pkt, func); + packet_write_char(pkt, 0); + packet_write_char(pkt, flag); + send(pkt); + } + } +} + +void GlDbg::resume_from_break(Tool *tool) +{ + if(!current_break) + return; + + ToolList::iterator i = find(break_holders.begin(), break_holders.end(), tool); + if(i!=break_holders.end()) + break_holders.erase(i); + + if(break_holders.empty()) + process.resume(); } - + void GlDbg::quit(bool force) { if(!force && process.get_state()!=Process::INACTIVE) @@ -78,18 +172,9 @@ void GlDbg::tick() if(got_sigchld) { got_sigchld = false; - int ret = process.check(); - if(ret==0x100) - IO::print("Target process exited normally\n"); - else if((ret>>8)==1) - IO::print("Target process exited with status %d\n", ret&0xFF); - else if((ret>>8)==2) - IO::print("Target process terminated with signal %d\n", ret&0xFF); - else if((ret>>8)==3) - { - IO::print("Target process stopped by signal %d\n", ret&0xFF); + stop_reason = process.check(); + if((stop_reason>>8)==3) flushing = true; - } } Process::State pstate = process.get_state(); @@ -97,6 +182,20 @@ void GlDbg::tick() read_stream(); else { + if(stop_reason) + { + if(stop_reason==0x100) + IO::print("Target process exited normally\n"); + else if((stop_reason>>8)==1) + IO::print("Target process exited with status %d\n", stop_reason&0xFF); + else if((stop_reason>>8)==2) + IO::print("Target process terminated with signal %d\n", stop_reason&0xFF); + else if((stop_reason>>8)==3) + IO::print("Target process stopped by signal %d\n", stop_reason&0xFF); + + stop_reason = 0; + } + char *line = readline("gldbg> "); if(line) { @@ -130,22 +229,63 @@ void GlDbg::read_stream() { const char *data = buffer.data()+buf_offset; unsigned len = buffer.size()-buf_offset; - int size = gldecoder_decode(0, data, len); - if(size<0) + GlPacket *pkt = packet_receive_str(data, &len); + if(!pkt) break; - tracer.decode(data, len); - glstate.decode(data, len); - buf_offset += size; + + unsigned short func; + packet_read_short(pkt, (short *)&func); + if(func==FUNC_GLDBREAK) + { + packet_read_short(pkt, (short *)&func); + unsigned char flag; + packet_read_char(pkt, (char *)&flag); + + current_break = get_breakpoint(func, flag); + bool announce = !current_break; + if(current_break) + { + break_holders = current_break->owners; + announce = current_break->has_owner(0); + } + + if(announce) + IO::print("Breakpoint: %s\n", get_function_name(func)); + } + + for(ToolList::iterator i=tools.begin(); i!=tools.end(); ++i) + (*i)->decode(data, len); + buf_offset += len; } if(buf_offset>8192) { buffer.erase(0, buf_offset); - buf_offset=0; + buf_offset = 0; } } } else if(flushing) + { flushing = false; + + if(stop_reason) + { + for(ToolList::iterator i=tools.begin(); i!=tools.end(); ++i) + (*i)->process_stopped(stop_reason); + + if(process.get_state()==Process::RUNNING) + current_break = 0; + } + } +} + +GlDbg::Breakpoint *GlDbg::get_breakpoint(unsigned short func, unsigned char flag) +{ + for(BreakList::iterator i=breakpoints.begin(); i!=breakpoints.end(); ++i) + if(i->function==func && i->flag==flag) + return &*i; + + return 0; } void GlDbg::sighandler(int sig) @@ -153,3 +293,29 @@ void GlDbg::sighandler(int sig) if(sig==SIGCHLD) got_sigchld = true; } + + +GlDbg::Breakpoint::Breakpoint(unsigned short f, unsigned char l): + function(f), + flag(l) +{ } + +void GlDbg::Breakpoint::add_owner(Tool *t) +{ + ToolList::iterator i = find(owners.begin(), owners.end(), t); + if(i==owners.end()) + owners.push_back(t); +} + +bool GlDbg::Breakpoint::has_owner(Tool *t) const +{ + ToolList::const_iterator i = find(owners.begin(), owners.end(), t); + return i!=owners.end(); +} + +void GlDbg::Breakpoint::remove_owner(Tool *t) +{ + ToolList::iterator i = find(owners.begin(), owners.end(), t); + if(i!=owners.end()) + owners.erase(i); +}