]> git.tdb.fi Git - gldbg.git/commitdiff
Add gldbg program
authorMikko Rasa <tdb@tdb.fi>
Wed, 23 Sep 2009 05:23:03 +0000 (05:23 +0000)
committerMikko Rasa <tdb@tdb.fi>
Wed, 23 Sep 2009 05:23:03 +0000 (05:23 +0000)
Make gldecoder.h and glprint.h C++-safe

Makefile
source/gldbg.cpp [new file with mode: 0644]
source/gldbg.h [new file with mode: 0644]
source/gldecoder.h
source/glprint.h

index 93fe6813eec78a1eb54bc3b668a6d166d9125357..55aa80b3b50a4177231bc4354a72b0ca3e45e2c5 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,8 +1,13 @@
 # $Id$
 
 CFLAGS = -Igensrc -ggdb
+CXXFLAGS = -Igensrc -ggdb
 
-all: glwrap.so gldump
+PACKAGES_gldbg = mspcore mspstrings mspio mspfs
+CXXFLAGS_gldbg = $(shell pkg-config --cflags $(PACKAGES_gldbg))
+LIBS_gldbg = $(shell pkg-config --libs $(PACKAGES_gldbg))
+
+all: glwrap.so gldump gldbg
 
 glwrap.so: source/glwrap.o
        $(CC) -shared -o $@ $^ $(LIBS) $(LDFLAGS)
@@ -10,11 +15,17 @@ glwrap.so: source/glwrap.o
 gldump: source/gldecoder.o source/gldump.o source/glprint.o
        $(CC) -o $@ $^ $(LIBS) $(LDFLAGS)
 
+gldbg: source/gldbg.o source/gldecoder.o source/glprint.o
+       $(CXX) -o $@ $^ $(LIBS_gldbg) $(LIBS) $(LDFLAGS)
+
 source/glwrap.o: source/functions.h gensrc/functions.enum gensrc/glwrap.funcs
 source/gldecoder.o: source/functions.h gensrc/gldecoder.struct gensrc/gldecoder.funcs gensrc/gldecoder.funcs
 source/gldump.o: source/gldecoder.h gensrc/gldecoder.struct source/glprint.h
 source/glprint.o: gensrc/glprint.funcs gensrc/gldecoder.struct
 
+source/gldbg.o: source/gldbg.cpp source/gldbg.h source/gldecoder.h source/glprint.h
+       $(CXX) -c $(CXXFLAGS) $(CXXFLAGS_gldbg) -o $@ $<
+
 gensrc/functions.enum gensrc/gldecoder.funcs gensrc/gldecoder.struct gensrc/glwrap.funcs gensrc/glprint.funcs: gensrc/.created genwrap.py gl.spec gl.tm
        python ./genwrap.py
 
diff --git a/source/gldbg.cpp b/source/gldbg.cpp
new file mode 100644 (file)
index 0000000..f06496d
--- /dev/null
@@ -0,0 +1,110 @@
+/* $Id$
+
+This file is part of gldbg
+Copyright © 2009  Mikko Rasa, Mikkosoft Productions
+Distributed under the GPL
+*/
+
+#include <cstdlib>
+#include <cerrno>
+#include <cstring>
+#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 "gldbg.h"
+#include "glprint.h"
+
+using namespace std;
+using namespace Msp;
+
+Application::RegApp<GlDbg> GlDbg::reg;
+
+GlDbg::GlDbg(int argc, char **argv):
+       pid(0),
+       sock_fd(-1),
+       glprint(glprint_new(0, 16384))
+{
+       libdir = FS::get_sys_lib_dir(argv[0], "gldbg");
+       args.assign(argv+1, argv+argc);
+}
+
+int GlDbg::main()
+{
+       catch_signal(SIGINT);
+       launch();
+
+       Application::main();
+}
+
+void GlDbg::tick()
+{
+       int status;
+       int ret = waitpid(pid, &status, WNOHANG);
+       if(ret==pid)
+       {
+               if(WIFEXITED(status) || WIFSIGNALED(status))
+               {
+                       IO::print("Target process exited\n");
+                       exit(0);
+               }
+       }
+
+       pollfd pfd = { sock_fd, POLLIN, 0 };
+       ret = poll(&pfd, 1, -1);
+       if(ret>0)
+       {
+               char rbuf[1024];
+               ret = read(sock_fd, rbuf, 1024);
+               if(ret>0)
+               {
+                       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)
+                                       break;
+                               buf_offset += ret;
+                               IO::print("%s\n", glprint_get_buffer(glprint));
+                       }
+                       if(buf_offset>8192)
+                       {
+                               buffer.erase(0, buf_offset);
+                               buf_offset=0;
+                       }
+               }
+       }
+}
+
+void GlDbg::launch()
+{
+       int fds[2];
+       socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
+       sock_fd = fds[0];
+
+       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);
+       }
+}
diff --git a/source/gldbg.h b/source/gldbg.h
new file mode 100644 (file)
index 0000000..9f26de3
--- /dev/null
@@ -0,0 +1,38 @@
+/* $Id$
+
+This file is part of gldbg
+Copyright © 2009  Mikko Rasa, Mikkosoft Productions
+Distributed under the GPL
+*/
+
+#ifndef GLDBG_H_
+#define GLDBG_H_
+
+#include <string>
+#include <vector>
+#include <msp/core/application.h>
+#include <msp/fs/path.h>
+#include "glstate.h"
+
+class GlDbg: public Msp::Application
+{
+private:
+       Msp::FS::Path libdir;
+       int pid;
+       std::vector<std::string> args;
+       int sock_fd;
+       std::string buffer;
+       unsigned buf_offset;
+       GlDecoder *glprint;
+
+       static RegApp<GlDbg> reg;
+
+public:
+       GlDbg(int, char **);
+       int main();
+private:
+       void tick();
+       void launch();
+};
+
+#endif
index 2b76b408b1468ca7e96ef68c2ebb0f19c8030a08..d97c0b3ff45fd30bdc1232114123d6c84df64dce 100644 (file)
@@ -9,10 +9,19 @@ Distributed under the GPL
 #define GLDECODER_H_
 
 #include <GL/gl.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 #include "gldecoder.struct"
 
 GlDecoder *gldecoder_new(void *, void (*)(void *));
 void gldecoder_delete(GlDecoder *);
 int gldecoder_decode(GlDecoder *, const char *, unsigned);
 
+#ifdef __cplusplus
+}
+#endif
+
 #endif
index 6d9ce15606b858b45cc56cc44f3b76715fd385ec..64a34559b76866b6bde921f87836ca753ec4dc65 100644 (file)
@@ -10,7 +10,15 @@ Distributed under the GPL
 
 #include "gldecoder.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 GlDecoder *glprint_new(char *, unsigned);
 char *glprint_get_buffer(GlDecoder *);
 
+#ifdef __cplusplus
+}
+#endif
+
 #endif