From 8a1580721a939c3edea2ce1b342148e94ad2b814 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 23 Sep 2009 05:23:03 +0000 Subject: [PATCH] Add gldbg program Make gldecoder.h and glprint.h C++-safe --- Makefile | 13 +++++- source/gldbg.cpp | 110 +++++++++++++++++++++++++++++++++++++++++++++ source/gldbg.h | 38 ++++++++++++++++ source/gldecoder.h | 9 ++++ source/glprint.h | 8 ++++ 5 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 source/gldbg.cpp create mode 100644 source/gldbg.h diff --git a/Makefile b/Makefile index 93fe681..55aa80b 100644 --- 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 index 0000000..f06496d --- /dev/null +++ b/source/gldbg.cpp @@ -0,0 +1,110 @@ +/* $Id$ + +This file is part of gldbg +Copyright © 2009 Mikko Rasa, Mikkosoft Productions +Distributed under the GPL +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "gldbg.h" +#include "glprint.h" + +using namespace std; +using namespace Msp; + +Application::RegApp 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 argv(args.size()+1); + for(unsigned i=0; i0) + { + 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 index 0000000..9f26de3 --- /dev/null +++ b/source/gldbg.h @@ -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 +#include +#include +#include +#include "glstate.h" + +class GlDbg: public Msp::Application +{ +private: + Msp::FS::Path libdir; + int pid; + std::vector args; + int sock_fd; + std::string buffer; + unsigned buf_offset; + GlDecoder *glprint; + + static RegApp reg; + +public: + GlDbg(int, char **); + int main(); +private: + void tick(); + void launch(); +}; + +#endif diff --git a/source/gldecoder.h b/source/gldecoder.h index 2b76b40..d97c0b3 100644 --- a/source/gldecoder.h +++ b/source/gldecoder.h @@ -9,10 +9,19 @@ Distributed under the GPL #define GLDECODER_H_ #include + +#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 diff --git a/source/glprint.h b/source/glprint.h index 6d9ce15..64a3455 100644 --- a/source/glprint.h +++ b/source/glprint.h @@ -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 -- 2.45.2