]> git.tdb.fi Git - gldbg.git/blobdiff - source/gldbg.cpp
Fix several problems reported by valgrind
[gldbg.git] / source / gldbg.cpp
index f06496dee576d04bf7c2ab2356c4e64e31e2c409..c13d5cc2fa6bbafb1b42c87b38139cf78fd58c10 100644 (file)
-/* $Id$
-
-This file is part of gldbg
-Copyright © 2009  Mikko Rasa, Mikkosoft Productions
-Distributed under the GPL
-*/
-
+#include <algorithm>
+#include <stdexcept>
 #include <cstdlib>
-#include <cerrno>
-#include <cstring>
+#include <climits>
+#include <fcntl.h>
+#include <signal.h>
+#include <unistd.h>
 #include <sys/poll.h>
 #include <sys/socket.h>
-#include <sys/wait.h>
-#include <msp/core/except.h>
-#include <msp/fs/dir.h>
-#include <msp/io/print.h>
-#include <msp/strings/lexicalcast.h>
+#include <readline/readline.h>
+#include "functions.h"
 #include "gldbg.h"
-#include "glprint.h"
+#include "gldecoder.h"
+#include "packet.h"
+#include "strformat.h"
+#include "tool.h"
 
 using namespace std;
-using namespace Msp;
 
-Application::RegApp<GlDbg> GlDbg::reg;
+GlDbg *GlDbg::instance = 0;
 
 GlDbg::GlDbg(int argc, char **argv):
-       pid(0),
+       done(false),
+       cmd_interp(*this),
+       process(vector<string>(argv+1, argv+argc)),
        sock_fd(-1),
-       glprint(glprint_new(0, 16384))
+       buf_offset(0),
+       flushing(false),
+       got_sigchld(false),
+       stop_reason(0),
+       current_break(0)
+{
+       instance = this;
+
+       char buf[PATH_MAX];
+       unsigned len = readlink("/proc/self/exe", buf, sizeof(buf));
+       string exe(buf, len);
+       string::size_type slash = exe.rfind('/');
+       process.setenv("LD_PRELOAD", (exe.substr(0, slash+1)+"glwrap.so"));
+
+       const list<Tool::Factory *> &factories = Tool::get_factories();
+       for(list<Tool::Factory *>::const_iterator i=factories.begin(); i!=factories.end(); ++i)
+               tools.push_back((*i)->create(*this));
+}
+
+GlDbg::~GlDbg()
 {
-       libdir = FS::get_sys_lib_dir(argv[0], "gldbg");
-       args.assign(argv+1, argv+argc);
+       for(ToolList::iterator i=tools.begin(); i!=tools.end(); ++i)
+               delete *i;
 }
 
 int GlDbg::main()
 {
-       catch_signal(SIGINT);
-       launch();
+       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::RUNNING)
+                       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;
+                               }
 
-       Application::main();
+                       if(process.get_state()>=Process::RUNNING)
+                               send_breakpoint(func, 0, flag);
+               }
+       }
+}
+
+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)
+               throw runtime_error("Program is still running");
+       done = true;
 }
 
 void GlDbg::tick()
 {
-       int status;
-       int ret = waitpid(pid, &status, WNOHANG);
-       if(ret==pid)
+       if(got_sigchld)
+       {
+               got_sigchld = false;
+               stop_reason = process.check();
+               if((stop_reason>>8)==3)
+                       flushing = true;
+       }
+
+       Process::State pstate = process.get_state();
+       if((pstate!=Process::INACTIVE && pstate!=Process::STOPPED) || flushing)
+               read_stream();
+       else
        {
-               if(WIFEXITED(status) || WIFSIGNALED(status))
+               if(stop_reason)
+               {
+                       if(stop_reason==0x100)
+                               printf("Target process exited normally\n");
+                       else if((stop_reason>>8)==1)
+                               printf("Target process exited with status %d\n", stop_reason&0xFF);
+                       else if((stop_reason>>8)==2)
+                               printf("Target process terminated with signal %d\n", stop_reason&0xFF);
+                       else if((stop_reason>>8)==3)
+                               printf("Target process stopped by signal %d\n", stop_reason&0xFF);
+
+                       stop_reason = 0;
+               }
+
+               char *line = readline("gldbg> ");
+               if(line)
                {
-                       IO::print("Target process exited\n");
-                       exit(0);
+                       try
+                       {
+                               cmd_interp.execute(line);
+                       }
+                       catch(const exception &e)
+                       {
+                               printf("%s\n", e.what());
+                       }
+                       free(line);
                }
+               else if(pstate==Process::INACTIVE)
+                       done = true;
        }
+}
 
+void GlDbg::read_stream()
+{
        pollfd pfd = { sock_fd, POLLIN, 0 };
-       ret = poll(&pfd, 1, -1);
+       int ret = poll(&pfd, 1, (flushing ? 0 : -1));
        if(ret>0)
        {
                char rbuf[1024];
@@ -64,47 +244,95 @@ void GlDbg::tick()
                        buffer.append(rbuf, ret);
                        while(buffer.size()>buf_offset)
                        {
-                               int ret = gldecoder_decode(glprint, buffer.data()+buf_offset, buffer.size()-buf_offset);
-                               if(ret<0)
+                               const char *data = buffer.data()+buf_offset;
+                               unsigned len = buffer.size()-buf_offset;
+                               GlPacket *pkt = packet_receive_str(data, &len);
+                               if(!pkt)
                                        break;
-                               buf_offset += ret;
-                               IO::print("%s\n", glprint_get_buffer(glprint));
+
+                               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)
+                                               printf("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;
+               }
+       }
 }
 
-void GlDbg::launch()
+GlDbg::Breakpoint *GlDbg::get_breakpoint(unsigned short func, unsigned char flag)
 {
-       int fds[2];
-       socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
-       sock_fd = fds[0];
+       for(BreakList::iterator i=breakpoints.begin(); i!=breakpoints.end(); ++i)
+               if(i->function==func && i->flag==flag)
+                       return &*i;
 
-       pid = fork();
-       if(pid==0)
-       {
-               string fd_str = lexical_cast(fds[1]);
-               setenv("LD_PRELOAD", (libdir/"glwrap.so").str().c_str(), true);
-               setenv("GLWRAP_FD", fd_str.c_str(), true);
-               close(fds[0]);
-               std::vector<char *> argv(args.size()+1);
-               for(unsigned i=0; i<args.size(); ++i)
-                       argv[i] = strdup(args[i].c_str());
-               argv[args.size()] = 0;
-               execvp(argv[0], &argv[0]);
-               ::exit(127);
-       }
-       else if(pid>0)
-       {
-               close(fds[1]);
-       }
-       else
-       {
-               throw SystemError("Could not launch process", errno);
-       }
+       return 0;
+}
+
+void GlDbg::sighandler(int sig)
+{
+       if(sig==SIGCHLD)
+               instance->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);
 }