From 81f1ddee977603293d0c5710f2db69130dac6a96 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 12 Jan 2011 14:06:41 +0000 Subject: [PATCH] Remove dependencies to MSP libraries to make compiling on embedded platforms easier --- Makefile | 8 +- flavors/gl/source/bufferstate.cpp | 13 ++- flavors/gl/source/glstate.cpp | 13 ++- flavors/gl/source/inspector.cpp | 127 ++++++++++++++++------------- flavors/gl/source/profiler.cpp | 30 ++++--- flavors/gl/source/profiler.h | 9 +- flavors/gl/source/texturestate.cpp | 19 ++--- source/commandinterpreter.cpp | 40 ++++----- source/gldbg.cpp | 60 +++++++------- source/gldbg.h | 12 +-- source/grabber.cpp | 12 +-- source/main.cpp | 14 ++++ source/process.cpp | 16 ++-- source/strformat.cpp | 22 +++++ source/strformat.h | 15 ++++ source/tracer.cpp | 42 +++++----- source/tracer.h | 10 +-- 17 files changed, 272 insertions(+), 190 deletions(-) create mode 100644 source/main.cpp create mode 100644 source/strformat.cpp create mode 100644 source/strformat.h diff --git a/Makefile b/Makefile index 4b17197..2a7175a 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ CPPFLAGS := -I. -Isource -Iflavors/$(FLAVOR)/source CFLAGS := -ggdb -Wall -Wextra CXXFLAGS := $(CFLAGS) -PACKAGES_gldbg := mspcore mspstrings mspio mspfs libpng12 +PACKAGES_gldbg := libpng12 FLAVOR_ROOT := flavors/$(FLAVOR) VPATH := $(FLAVOR_ROOT) @@ -26,9 +26,11 @@ SOURCES_glwrap := source/glwrap.c \ source/packet.c \ source/tmpalloc.c SOURCES_gldump := source/gldump.c -SOURCES_gldbg := source/gldbg.cpp \ +SOURCES_gldbg := source/main.cpp \ + source/gldbg.cpp \ source/commandinterpreter.cpp \ source/process.cpp \ + source/strformat.cpp \ source/tool.cpp SOURCES_tracer := source/tracer.cpp SOURCES_profiler := source/profiler.cpp @@ -70,7 +72,7 @@ OBJECTS_all := $(call objs,$(SOURCES_all)) DEPS_all := $(call deps,$(SOURCES_all) $(TEMPLATES)) $(OBJECTS_gldbg): CXXFLAGS += $(shell pkg-config --cflags $(PACKAGES_gldbg)) -gldbg: LIBS += $(shell pkg-config --libs $(PACKAGES_gldbg)) -lreadline +gldbg: LIBS += $(shell pkg-config --libs $(PACKAGES_gldbg)) -lreadline -lrt gldump gldbg: LIBS += ./libgldbg.a glwrap.so: LDFLAGS += -s glwrap.so: LIBS += -ldl diff --git a/flavors/gl/source/bufferstate.cpp b/flavors/gl/source/bufferstate.cpp index c8b9f13..5060e15 100644 --- a/flavors/gl/source/bufferstate.cpp +++ b/flavors/gl/source/bufferstate.cpp @@ -1,18 +1,17 @@ /* $Id$ This file is part of gldbg -Copyright © 2009-2010 Mikko Rasa, Mikkosoft Productions +Copyright © 2009-2011 Mikko Rasa, Mikkosoft Productions Distributed under the GPL */ -#include #include "arraysize.h" #include "arraystate.h" #include "bufferstate.h" #include "enums.h" +#include "strformat.h" using namespace std; -using namespace Msp; BufferContent::BufferContent(): consistent(true), @@ -113,7 +112,7 @@ string BufferContent::describe() const if(!result.empty()) result += '_'; - result += format("%c%d%s", kind, i->size, type); + result += strformat("%c%d%s", kind, i->size, type); } return result; @@ -170,9 +169,9 @@ string BufferState::describe() const if(content.stride) { const char *what = (content.arrays.front().kind==GL_ELEMENT_ARRAY_BUFFER ? "indices" : "vertices"); - return format("%s, %d %s (%d bytes), %s", - content.describe(), size/content.stride, what, size, describe_enum(usage, "")); + return content.describe()+strformat(", %d %s (%d bytes), %s", + size/content.stride, what, size, describe_enum(usage, "")); } else - return format("%d bytes, %s", size, describe_enum(usage, "")); + return strformat("%d bytes, %s", size, describe_enum(usage, "")); } diff --git a/flavors/gl/source/glstate.cpp b/flavors/gl/source/glstate.cpp index e33e490..f910b49 100644 --- a/flavors/gl/source/glstate.cpp +++ b/flavors/gl/source/glstate.cpp @@ -1,15 +1,14 @@ /* $Id$ This file is part of gldbg -Copyright © 2009-2010 Mikko Rasa, Mikkosoft Productions +Copyright © 2009-2011 Mikko Rasa, Mikkosoft Productions Distributed under the GPL */ -#include +#include #include "glstate.h" using namespace std; -using namespace Msp; namespace { @@ -147,7 +146,7 @@ const TextureState &GlState::get_texture(unsigned id) const { TextureMap::const_iterator i = textures.find(id); if(i==textures.end()) - throw KeyError("Unknown texture"); + throw runtime_error("Unknown texture"); return i->second; } @@ -155,7 +154,7 @@ const BufferState &GlState::get_buffer(unsigned id) const { BufferMap::const_iterator i = buffers.find(id); if(i==buffers.end()) - throw KeyError("Unknown buffer"); + throw runtime_error("Unknown buffer"); return i->second; } @@ -204,7 +203,7 @@ const ArrayState &GlState::get_array(GLenum array) const else if(array==GL_TEXTURE_COORD_ARRAY) return texcoord_arrays[client_active_tex]; else - throw InvalidParameterValue("Invalid array"); + throw logic_error("Invalid array"); } const ArrayState &GlState::get_texture_coord_array(unsigned index) const @@ -219,7 +218,7 @@ const ArrayState &GlState::get_attrib_array(unsigned index) const return i->second; // XXX Return a dummy? - throw KeyError("Unknown attribute array"); + throw runtime_error("Unknown attribute array"); } void GlState::set_current_texture(GLenum target, unsigned id) diff --git a/flavors/gl/source/inspector.cpp b/flavors/gl/source/inspector.cpp index 5dcdf41..bbdcfed 100644 --- a/flavors/gl/source/inspector.cpp +++ b/flavors/gl/source/inspector.cpp @@ -1,17 +1,19 @@ /* $Id$ This file is part of gldbg -Copyright © 2010 Mikko Rasa, Mikkosoft Productions +Copyright © 2011 Mikko Rasa, Mikkosoft Productions Distributed under the GPL */ -#include +#include +#include +#include #include "enums.h" #include "gldbg.h" #include "inspector.h" +#include "strformat.h" using namespace std; -using namespace Msp; Inspector::Inspector(GlDbg &dbg) { @@ -49,35 +51,37 @@ void Inspector::cmd_state(const string &args) const GlState &glstate = state; if(args=="vertex") { - IO::print("Current vertex attributes:\n"); + printf("Current vertex attributes:\n"); const Vector4 &color = glstate.get_color(); - IO::print(" Color: [%05.3f, %05.3f, %05.3f, %05.3f]\n", color.r, color.g, color.b, color.a); + printf(" Color: [%05.3f, %05.3f, %05.3f, %05.3f]\n", color.r, color.g, color.b, color.a); for(unsigned i=0; i<8; ++i) { const Vector4 &texcoord = glstate.get_texcoord(i); - IO::print(" TexCoord%d: [%05.3f, %05.3f, %05.3f, %05.3f]\n", i, texcoord.s, texcoord.t, texcoord.p, texcoord.q); + printf(" TexCoord%d: [%05.3f, %05.3f, %05.3f, %05.3f]\n", i, texcoord.s, texcoord.t, texcoord.p, texcoord.q); } const Vector3 &normal = glstate.get_normal(); - IO::print(" Normal: [%05.3f, %05.3f, %05.3f]\n", normal.x, normal.y, normal.z); + printf(" Normal: [%05.3f, %05.3f, %05.3f]\n", normal.x, normal.y, normal.z); } else if(args=="bind") { - IO::print("Current bindings:\n"); + printf("Current bindings:\n"); for(unsigned i=0; i<8; ++i) { - IO::print(" Texture unit %d:\n", i); + printf(" Texture unit %d:\n", i); const TexUnitState &unit = glstate.get_texture_unit(i); - IO::print(" GL_TEXTURE_2D: %s\n", unit.describe_binding(GL_TEXTURE_2D)); - IO::print(" GL_TEXTURE_3D: %s\n", unit.describe_binding(GL_TEXTURE_3D)); + string descr = unit.describe_binding(GL_TEXTURE_2D); + printf(" GL_TEXTURE_2D: %s\n", descr.c_str()); + descr = unit.describe_binding(GL_TEXTURE_3D); + printf(" GL_TEXTURE_3D: %s\n", descr.c_str()); } - IO::print(" Buffers:\n"); + printf(" Buffers:\n"); const BufferState *buf = glstate.get_current_buffer(GL_ARRAY_BUFFER); - IO::print(" GL_ARRAY_BUFFER: %d\n", (buf ? buf->id : 0)); + printf(" GL_ARRAY_BUFFER: %d\n", (buf ? buf->id : 0)); buf = glstate.get_current_buffer(GL_ELEMENT_ARRAY_BUFFER); - IO::print(" GL_ELEMENT_ARRAY_BUFFER: %d\n", (buf ? buf->id : 0)); + printf(" GL_ELEMENT_ARRAY_BUFFER: %d\n", (buf ? buf->id : 0)); } else - throw InvalidParameterValue("Invalid or missing argument"); + throw runtime_error("Invalid or missing argument"); } void Inspector::cmd_texture(const string &args) @@ -85,32 +89,37 @@ void Inspector::cmd_texture(const string &args) if(args.empty()) { const map &textures = state.get_textures(); - IO::print("%d texture objects:\n", textures.size()); + printf("%d texture objects:\n", textures.size()); for(map::const_iterator i = textures.begin(); i!=textures.end(); ++i) { const TextureState &tex = i->second; - IO::print(" %d: %s, %d images\n", i->first, tex.describe(), tex.images.size()); + string descr = tex.describe(); + printf(" %d: %s, %d images\n", i->first, descr.c_str(), tex.images.size()); } } else { - unsigned id = lexical_cast(args); + char *end = 0; + unsigned id = strtoul(args.c_str(), &end, 0); + if(end && *end) + throw runtime_error("Invalid texture id"); + const TextureState &tex = state.get_texture(id); - IO::print("Texture object %d\n", id); - IO::print(" Target: %s\n", describe_enum(tex.target, "TextureTarget")); - IO::print(" Images:\n"); + printf("Texture object %d\n", id); + printf(" Target: %s\n", describe_enum(tex.target, "TextureTarget")); + printf(" Images:\n"); for(unsigned i=0; ifirst, i->second.describe()); + { + string descr = i->second.describe(); + printf(" %d: %s\n", i->first, descr.c_str()); + } } else { - unsigned id = lexical_cast(args); + char *end = 0; + unsigned id = strtoul(args.c_str(), &end, 0); + if(end && *end) + throw runtime_error("Invalid buffer id"); + const BufferState &buf = state.get_buffer(id); - IO::print("Buffer %d:\n", id); - IO::print(" Size: %d bytes\n", buf.size); - IO::print(" Usage: %s\n", describe_enum(buf.usage, "")); + printf("Buffer %d:\n", id); + printf(" Size: %d bytes\n", buf.size); + printf(" Usage: %s\n", describe_enum(buf.usage, "")); if(buf.content.arrays.size()==1 && buf.content.arrays.front().kind==GL_ELEMENT_ARRAY_BUFFER) { const BufferContent::Array &array = buf.content.arrays.front(); - IO::print(" Arrays:\n"); - IO::print(" 0: Element indices, 1 %s\n", describe_enum(array.type, "DataType")); + printf(" Arrays:\n"); + printf(" 0: Element indices, 1 %s\n", describe_enum(array.type, "DataType")); - IO::print(" Data:\n"); + printf(" Data:\n"); unsigned width = 1+buf.content.stride*2; - string fmt = format(" %%%du", width); + char fmt[6]; + snprintf(fmt, sizeof(fmt), " %%%du", width); unsigned n_elems = buf.size/buf.content.stride; string line; const char *ptr = buf.data; @@ -148,39 +165,39 @@ void Inspector::cmd_buffer(const string &args) line = " "; if(array.type==GL_UNSIGNED_BYTE) - line += format(fmt, *(reinterpret_cast(ptr)+i)); + line += strformat(fmt, *(reinterpret_cast(ptr)+i)); else if(array.type==GL_UNSIGNED_SHORT) - line += format(fmt, *(reinterpret_cast(ptr)+i)); + line += strformat(fmt, *(reinterpret_cast(ptr)+i)); else if(array.type==GL_UNSIGNED_INT) - line += format(fmt, *(reinterpret_cast(ptr)+i)); + line += strformat(fmt, *(reinterpret_cast(ptr)+i)); if(line.size()+1+width>79) { - IO::print("%s\n", line); + printf("%s\n", line.c_str()); line.clear(); } } if(!line.empty()) - IO::print("%s\n", line); + printf("%s\n", line.c_str()); } else if(buf.content.stride) { - IO::print(" Stride: %d bytes\n", buf.content.stride); + printf(" Stride: %d bytes\n", buf.content.stride); - IO::print(" Arrays:\n"); + printf(" Arrays:\n"); const vector &arrays = buf.content.arrays; for(vector::const_iterator i=arrays.begin(); i!=arrays.end(); ++i) { if(i->kind) - IO::print(" %2d: %s, %d %s\n", i->offset, + printf(" %2d: %s, %d %s\n", i->offset, describe_enum(i->kind, ""), i->size, describe_enum(i->type, "DataType")); else - IO::print(" %2d: Attrib %d, %d %s\n", i->offset, + printf(" %2d: Attrib %d, %d %s\n", i->offset, i->index, i->size, describe_enum(i->type, "DataType")); } - IO::print(" Data:\n"); + printf(" Data:\n"); string header; for(vector::const_iterator i=arrays.begin(); i!=arrays.end(); ++i) { @@ -204,9 +221,9 @@ void Inspector::cmd_buffer(const string &args) else if(!i->kind) { if(i->size==1) - label = format("A %d", i->index); + label = strformat("A %d", i->index); else - label = format("Attrib %d", i->index); + label = strformat("Attrib %d", i->index); } unsigned width = i->size; @@ -220,7 +237,7 @@ void Inspector::cmd_buffer(const string &args) header += label; header.append((width-label.size()+1)/2, ' '); } - IO::print(" %s\n", header); + printf(" %s\n", header.c_str()); unsigned n_verts = buf.size/buf.content.stride; for(unsigned i=0; isize; ++k) { if(j->type==GL_FLOAT) - line += format(" %5.2f", *(reinterpret_cast(base)+k)); + line += strformat(" %5.2f", *(reinterpret_cast(base)+k)); else if(j->type==GL_UNSIGNED_BYTE) - line += format(" %3u", *(reinterpret_cast(base)+k)); + line += strformat(" %3u", *(reinterpret_cast(base)+k)); } } - IO::print("%3d:%s\n", i, line); + printf("%3d:%s\n", i, line.c_str()); } } } diff --git a/flavors/gl/source/profiler.cpp b/flavors/gl/source/profiler.cpp index 37caa68..4b95208 100644 --- a/flavors/gl/source/profiler.cpp +++ b/flavors/gl/source/profiler.cpp @@ -1,18 +1,17 @@ /* $Id$ This file is part of gldbg -Copyright © 2010 Mikko Rasa, Mikkosoft Productions +Copyright © 2010-2011 Mikko Rasa, Mikkosoft Productions Distributed under the GPL */ -#include -#include -#include +#include +#include +#include #include "gldbg.h" #include "profiler.h" using namespace std; -using namespace Msp; Profiler::Profiler(GlDbg &dbg): decoder(gldecoder_new(this, 0)), @@ -44,12 +43,12 @@ void Profiler::cmd_profile(const string &args) draw_calls = 0; vertices = 0; triangles = 0; - start = Msp::Time::now(); + start = get_time(); } else if(args=="off") enabled = false; else - throw InvalidParameterValue("Invalid argument"); + throw runtime_error("Invalid argument"); } void Profiler::glDrawArrays(void *user_data, GLenum mode, int, int count) @@ -93,18 +92,25 @@ void Profiler::glXSwapBuffers(void *user_data, Display *, GLXDrawable) { Profiler *self = reinterpret_cast(user_data); - IO::print("%d draw calls, %d vertices, %d triangles\n", self->draw_calls, self->vertices, self->triangles); + printf("%d draw calls, %d vertices, %d triangles\n", self->draw_calls, self->vertices, self->triangles); self->draw_calls = 0; self->vertices = 0; self->triangles = 0; ++self->frames; - Msp::Time::TimeStamp t = Msp::Time::now(); - Msp::Time::TimeDelta dt = t-self->start; - if(dt>Msp::Time::sec) + Time t = get_time(); + float dt = (t-self->start)/1e6; + if(dt>1) { - Msp::IO::print("%d frames in %s seconds\n", self->frames, dt); + printf("%d frames in %.2f seconds\n", self->frames, dt); self->start = t; self->frames = 0; } } + +Profiler::Time Profiler::get_time() +{ + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + return ts.tv_sec*1000000+ts.tv_nsec/1000; +} diff --git a/flavors/gl/source/profiler.h b/flavors/gl/source/profiler.h index 4d5c5a8..547bd35 100644 --- a/flavors/gl/source/profiler.h +++ b/flavors/gl/source/profiler.h @@ -1,23 +1,24 @@ /* $Id$ This file is part of gldbg -Copyright © 2010 Mikko Rasa, Mikkosoft Productions +Copyright © 2011 Mikko Rasa, Mikkosoft Productions Distributed under the GPL */ #ifndef PROFILER_H_ #define PROFILER_H_ -#include #include "gldecoder.h" #include "tool.h" class Profiler: public RegisteredTool { private: + typedef unsigned Time; + GlDecoder *decoder; bool enabled; - Msp::Time::TimeStamp start; + Time start; unsigned frames; unsigned draw_calls; unsigned vertices; @@ -34,6 +35,8 @@ private: static void glDrawElements(void *, GLenum, int, GLenum, const void *); static void glDrawRangeElements(void *, GLenum, unsigned, unsigned, int, GLenum, const void *); static void glXSwapBuffers(void *, Display *, GLXDrawable); + + static Time get_time(); }; #endif diff --git a/flavors/gl/source/texturestate.cpp b/flavors/gl/source/texturestate.cpp index 187707c..2bb4a17 100644 --- a/flavors/gl/source/texturestate.cpp +++ b/flavors/gl/source/texturestate.cpp @@ -1,16 +1,15 @@ /* $Id$ This file is part of gldbg -Copyright © 2009 Mikko Rasa, Mikkosoft Productions +Copyright © 2009-2011 Mikko Rasa, Mikkosoft Productions Distributed under the GPL */ -#include #include "enums.h" +#include "strformat.h" #include "texturestate.h" using namespace std; -using namespace Msp; TexImageState::TexImageState(): width(0), @@ -29,14 +28,14 @@ void TexImageState::set_2d(GLenum ifmt, unsigned wd, unsigned ht) string TexImageState::describe() const { - string descr = format("%d", width); + string descr = strformat("%d", width); if(height) { - descr += format("x%d", height); + descr += strformat("x%d", height); if(depth) - descr += format("x%d", height); + descr += strformat("x%d", height); } - descr += format(", %s", describe_enum(internal_format, "PixelFormat")); + descr += strformat(", %s", describe_enum(internal_format, "PixelFormat")); return descr; } @@ -103,7 +102,7 @@ string TextureState::describe() const if(images.empty()) descr += ", undefined"; else - descr += format(", %s", images.front().describe()); + descr += ", "+images.front().describe(); return descr; } @@ -140,11 +139,11 @@ string TexUnitState::describe_binding(GLenum target) const { if(const TextureState *tex = get_current_texture(target)) { - string descr = format("%d ", tex->id); + string descr = strformat("%d ", tex->id); if(tex->images.empty()) descr += "(undefined)"; else - descr += format("(%s)", tex->images.front().describe()); + descr += "("+tex->images.front().describe()+")"; return descr; } else diff --git a/source/commandinterpreter.cpp b/source/commandinterpreter.cpp index fe0c3f1..3015350 100644 --- a/source/commandinterpreter.cpp +++ b/source/commandinterpreter.cpp @@ -1,16 +1,14 @@ /* $Id$ This file is part of gldbg -Copyright © 2009-2010 Mikko Rasa, Mikkosoft Productions +Copyright © 2009-2011 Mikko Rasa, Mikkosoft Productions Distributed under the GPL */ +#include +#include #include #include -#include -#include -#include -#include #include "breakpoint.h" #include "commandinterpreter.h" #include "enums.h" @@ -19,7 +17,6 @@ Distributed under the GPL #include "tracer.h" using namespace std; -using namespace Msp; CommandInterpreter::CommandInterpreter(GlDbg &d): gldbg(d) @@ -69,12 +66,12 @@ void CommandInterpreter::execute(const string &cmd) string name = cmd.substr(0, space); CommandMap::const_iterator i = commands.lower_bound(name); if(i==commands.end() || i->first.compare(0, name.size(), name)) - throw KeyError("Unknown command", name); + throw runtime_error("Unknown command "+name); if(i->first!=name) { CommandMap::const_iterator j = i; if((++j)!=commands.end() && !j->first.compare(0, name.size(), name)) - throw KeyError("Ambiguous command", name); + throw runtime_error("Ambiguous command "+name); } string args; @@ -90,21 +87,21 @@ void CommandInterpreter::cmd_help(const string &args) { for(CommandMap::const_iterator i=commands.begin(); i!=commands.end(); ++i) if(!dynamic_cast(i->second)) - IO::print("%-10s : %s\n", i->first, i->second->get_description()); + printf("%-10s : %s\n", i->first.c_str(), i->second->get_description().c_str()); } else { CommandMap::const_iterator i = commands.find(args); if(i==commands.end()) - throw KeyError("Unknown command", args); + throw runtime_error("Unknown command "+args); const Command *cmd = i->second; while(const CommandAlias *alias = dynamic_cast(cmd)) cmd = alias->get_target(); - IO::print("%s : %s\n", i->first, cmd->get_description()); + printf("%s : %s\n", i->first.c_str(), cmd->get_description().c_str()); if(!cmd->get_help().empty()) - IO::print("\n%s", cmd->get_help()); + printf("\n%s", cmd->get_help().c_str()); } } @@ -117,7 +114,7 @@ void CommandInterpreter::cmd_break(const string &args) { unsigned short func = get_function(args.c_str()); if(!func) - throw InvalidParameterValue("Unknown function"); + throw runtime_error("Unknown function"); gldbg.set_breakpoint(func, BREAK_CALL, 0); } @@ -126,7 +123,7 @@ void CommandInterpreter::cmd_unbreak(const string &args) { unsigned short func = get_function(args.c_str()); if(!func) - throw InvalidParameterValue("Unknown function"); + throw runtime_error("Unknown function"); gldbg.clear_breakpoint(func, BREAK_CALL, 0); } @@ -145,7 +142,7 @@ void CommandInterpreter::cmd_finish(const string &) void CommandInterpreter::cmd_continue(const string &) { - IO::print("Continuing.\n"); + printf("Continuing.\n"); gldbg.get_process().resume(); } @@ -162,10 +159,13 @@ void CommandInterpreter::cmd_signal(const string &args) sig = SIGSEGV; else if(args=="TERM" || args=="SIGTERM") sig = SIGTERM; - else if(isnumrc(args)) - sig = lexical_cast(args); else - throw InvalidParameterValue("Invalid signal specification"); + { + char *end; + sig = strtoul(args.c_str(), &end, 0); + if(end && *end) + throw runtime_error("Invalid signal specification"); + } gldbg.get_process().resume(sig); } @@ -178,7 +178,7 @@ void CommandInterpreter::cmd_exit(const string &) { if(gldbg.get_process().get_state()!=Process::INACTIVE) { - IO::print("Program is still running. Kill it?\n"); + printf("Program is still running. Kill it?\n"); char *answer = readline("[y/n] "); if(answer[0]=='y') { @@ -186,7 +186,7 @@ void CommandInterpreter::cmd_exit(const string &) gldbg.quit(true); } else - IO::print("Not confirmed.\n"); + printf("Not confirmed.\n"); } else gldbg.quit(false); diff --git a/source/gldbg.cpp b/source/gldbg.cpp index 001742d..a7abf31 100644 --- a/source/gldbg.cpp +++ b/source/gldbg.cpp @@ -1,31 +1,30 @@ /* $Id$ This file is part of gldbg -Copyright © 2009-2010 Mikko Rasa, Mikkosoft Productions +Copyright © 2009-2011 Mikko Rasa, Mikkosoft Productions Distributed under the GPL */ #include +#include #include +#include #include #include +#include #include #include #include -#include -#include -#include -#include #include "functions.h" #include "gldbg.h" #include "gldecoder.h" #include "packet.h" +#include "strformat.h" #include "tool.h" using namespace std; -using namespace Msp; -Application::RegApp GlDbg::reg; +GlDbg *GlDbg::instance = 0; GlDbg::GlDbg(int argc, char **argv): cmd_interp(*this), @@ -36,8 +35,13 @@ GlDbg::GlDbg(int argc, char **argv): 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()); + 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 &factories = Tool::get_factories(); for(list::const_iterator i=factories.begin(); i!=factories.end(); ++i) @@ -52,15 +56,15 @@ GlDbg::~GlDbg() int GlDbg::main() { - catch_signal(SIGINT); - catch_signal(SIGCHLD); - set_loop_mode(TICK_BUSY); + signal(SIGINT, &sighandler); + signal(SIGCHLD, &sighandler); - IO::print("GLdbg 0.0\n"); - IO::print("Copyright © 2009-2010 Mikkosoft Productions\n"); - IO::print("Type \"help\" for a list of commands\n"); + printf("GLdbg 0.0\n"); + printf("Copyright © 2009-2010 Mikkosoft Productions\n"); + printf("Type \"help\" for a list of commands\n"); - Application::main(); + while(1) + tick(); return 0; } @@ -68,7 +72,7 @@ int GlDbg::main() void GlDbg::launch() { if(process.get_state()!=Process::INACTIVE) - throw InvalidState("Program is already running"); + throw runtime_error("Program is already running"); int fds[2]; socketpair(AF_UNIX, SOCK_STREAM, 0, fds); @@ -77,8 +81,8 @@ void GlDbg::launch() int flags = fcntl(sock_fd, F_GETFD); 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.setenv("GLWRAP_FD", strformat("%d", fds[1])); + process.setenv("GLWRAP_CTRL_FD", strformat("%d", fds[1])); process.launch(); close(fds[1]); @@ -163,7 +167,7 @@ void GlDbg::resume_from_break(Tool *tool) void GlDbg::quit(bool force) { if(!force && process.get_state()!=Process::INACTIVE) - throw InvalidState("Program is still running"); + throw runtime_error("Program is still running"); exit(0); } @@ -185,13 +189,13 @@ void GlDbg::tick() if(stop_reason) { if(stop_reason==0x100) - IO::print("Target process exited normally\n"); + printf("Target process exited normally\n"); else if((stop_reason>>8)==1) - IO::print("Target process exited with status %d\n", stop_reason&0xFF); + printf("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); + printf("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); + printf("Target process stopped by signal %d\n", stop_reason&0xFF); stop_reason = 0; } @@ -203,9 +207,9 @@ void GlDbg::tick() { cmd_interp.execute(line); } - catch(const Exception &e) + catch(const exception &e) { - IO::print("%s\n", e.what()); + printf("%s\n", e.what()); } free(line); } @@ -250,7 +254,7 @@ void GlDbg::read_stream() } if(announce) - IO::print("Breakpoint: %s\n", get_function_name(func)); + printf("Breakpoint: %s\n", get_function_name(func)); } for(ToolList::iterator i=tools.begin(); i!=tools.end(); ++i) @@ -291,7 +295,7 @@ GlDbg::Breakpoint *GlDbg::get_breakpoint(unsigned short func, unsigned char flag void GlDbg::sighandler(int sig) { if(sig==SIGCHLD) - got_sigchld = true; + instance->got_sigchld = true; } diff --git a/source/gldbg.h b/source/gldbg.h index 99f0863..dd6d60c 100644 --- a/source/gldbg.h +++ b/source/gldbg.h @@ -1,24 +1,23 @@ /* $Id$ This file is part of gldbg -Copyright © 2009-2010 Mikko Rasa, Mikkosoft Productions +Copyright © 2009-2011 Mikko Rasa, Mikkosoft Productions Distributed under the GPL */ #ifndef GLDBG_H_ #define GLDBG_H_ +#include #include #include -#include -#include #include "commandinterpreter.h" #include "packet.h" #include "process.h" class Tool; -class GlDbg: public Msp::Application +class GlDbg { private: typedef std::list ToolList; @@ -51,7 +50,7 @@ private: const Breakpoint *current_break; ToolList break_holders; - static RegApp reg; + static GlDbg *instance; public: GlDbg(int, char **); @@ -72,7 +71,8 @@ private: void check_child(); void read_stream(); Breakpoint *get_breakpoint(unsigned short, unsigned char); - virtual void sighandler(int); + + static void sighandler(int); }; #endif diff --git a/source/grabber.cpp b/source/grabber.cpp index e56622f..5b40231 100644 --- a/source/grabber.cpp +++ b/source/grabber.cpp @@ -6,16 +6,16 @@ Distributed under the GPL */ #include +#include #include -#include #include "arraysize.h" #include "breakpoint.h" #include "functions.h" #include "gldbg.h" #include "grabber.h" +#include "strformat.h" using namespace std; -using namespace Msp; Grabber::Grabber(GlDbg &g): gldbg(g), @@ -124,7 +124,7 @@ void Grabber::cmd_autograb(const string &args) } } else - throw InvalidParameterValue("Invalid autograb mode"); + throw runtime_error("Invalid autograb mode"); } void Grabber::grab() @@ -220,13 +220,13 @@ void Grabber::glReadPixels(void *user_data, int, int, int width, int height, GLe { Grabber *self = reinterpret_cast(user_data); - string fn = Msp::format("grab-%04d-%04d.png", self->frame_num, self->grab_num); + string fn = strformat("grab-%04d-%04d.png", self->frame_num, self->grab_num); if(format==GL_RGBA && type==GL_UNSIGNED_BYTE) { if(self->write_png(fn, width, height, data)<0) - IO::print("Failed to write png file %s\n", fn); + printf("Failed to write png file %s\n", fn.c_str()); else - IO::print("Grabbed %dx%d png image to %s\n", width, height, fn); + printf("Grabbed %dx%d png image to %s\n", width, height, fn.c_str()); } ++self->grab_num; diff --git a/source/main.cpp b/source/main.cpp new file mode 100644 index 0000000..53722be --- /dev/null +++ b/source/main.cpp @@ -0,0 +1,14 @@ +/* $Id$ + +This file is part of gldbg +Copyright © 2011 Mikko Rasa, Mikkosoft Productions +Distributed under the GPL +*/ + +#include "gldbg.h" + +int main(int argc, char **argv) +{ + GlDbg gldbg(argc, argv); + return gldbg.main(); +} diff --git a/source/process.cpp b/source/process.cpp index 0fc9e98..e198df2 100644 --- a/source/process.cpp +++ b/source/process.cpp @@ -1,20 +1,20 @@ /* $Id$ This file is part of gldbg -Copyright © 2009 Mikko Rasa, Mikkosoft Productions +Copyright © 2009, 2011 Mikko Rasa, Mikkosoft Productions Distributed under the GPL */ +#include #include #include #include #include #include -#include #include "process.h" +#include "strformat.h" using namespace std; -using namespace Msp; Process::Process(const vector &a): args(a), @@ -30,7 +30,7 @@ void Process::setenv(const string &key, const string &value) void Process::launch() { if(state!=INACTIVE) - throw InvalidState("Program is already running"); + throw logic_error("Program is already running"); pid = fork(); if(pid==0) @@ -48,7 +48,7 @@ void Process::launch() else if(pid>0) state = STARTING; else - throw SystemError("Could not launch process", errno); + throw runtime_error(strformat("Could not launch process: %s", strerror(errno))); } int Process::check() @@ -90,7 +90,7 @@ int Process::check() void Process::resume(int sig) { if(state!=STOPPED) - throw InvalidState("Program is not stopped"); + throw logic_error("Program is not stopped"); ptrace(PTRACE_CONT, 0, (void *)sig); state = RUNNING; } @@ -98,7 +98,7 @@ void Process::resume(int sig) void Process::kill() { if(state==INACTIVE) - throw InvalidState("Program is not running"); + throw logic_error("Program is not running"); ptrace(PTRACE_KILL, 0, 0); // Make the debugger wait() for us state = RUNNING; @@ -108,6 +108,6 @@ long Process::ptrace(int req, void *addr, void *data) { int ret = ::ptrace((__ptrace_request)req, pid, addr, data); if(ret==-1 && ((req!=PTRACE_PEEKTEXT && req!=PTRACE_PEEKDATA && req!=PTRACE_PEEKUSER) || errno)) - throw SystemError("ptrace error", errno); + throw runtime_error(strformat("ptrace error: %s", strerror(errno))); return ret; } diff --git a/source/strformat.cpp b/source/strformat.cpp new file mode 100644 index 0000000..188b121 --- /dev/null +++ b/source/strformat.cpp @@ -0,0 +1,22 @@ +/* $Id$ + +This file is part of gldbg +Copyright © 2011 Mikko Rasa, Mikkosoft Productions +Distributed under the GPL +*/ + +#include +#include +#include "strformat.h" + +using namespace std; + +string strformat(const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + char buf[2048]; + vsnprintf(buf, sizeof(buf), fmt, args); + va_end(args); + return buf; +} diff --git a/source/strformat.h b/source/strformat.h new file mode 100644 index 0000000..94ce9a6 --- /dev/null +++ b/source/strformat.h @@ -0,0 +1,15 @@ +/* $Id$ + +This file is part of gldbg +Copyright © 2011 Mikko Rasa, Mikkosoft Productions +Distributed under the GPL +*/ + +#ifndef STRFORMAT_H_ +#define STRFORMAT_H_ + +#include + +std::string strformat(const char *, ...); + +#endif diff --git a/source/tracer.cpp b/source/tracer.cpp index 9f703f1..5a02354 100644 --- a/source/tracer.cpp +++ b/source/tracer.cpp @@ -1,23 +1,21 @@ /* $Id$ This file is part of gldbg -Copyright © 2009-2010 Mikko Rasa, Mikkosoft Productions +Copyright © 2009-2011 Mikko Rasa, Mikkosoft Productions Distributed under the GPL */ -#include -#include +#include #include "gldbg.h" #include "glprint.h" #include "tracer.h" using namespace std; -using namespace Msp; Tracer::Tracer(GlDbg &dbg): glprint(new GlPrint(0, 16384)), out(0), - delete_out(true), + close_out(true), enabled(true) { dbg.get_command_interpreter().register_command("trace", this, &Tracer::cmd_trace) @@ -35,16 +33,16 @@ Tracer::Tracer(GlDbg &dbg): Tracer::~Tracer() { delete glprint; - if(delete_out) - delete out; + if(close_out && out) + fclose(out); } -void Tracer::set_output(IO::Base *o, bool d) +void Tracer::set_output(FILE *o, bool d) { - if(delete_out) - delete out; + if(close_out && out) + fclose(out); out = o; - delete_out = d; + close_out = d; } void Tracer::decode(const char *data, unsigned len) @@ -57,7 +55,7 @@ void Tracer::decode(const char *data, unsigned len) { const char *buf = glprint->get_buffer(); if(buf[0]) - IO::print(*out, "%s\n", buf); + fprintf(out, "%s\n", buf); } } @@ -65,29 +63,33 @@ void Tracer::cmd_trace(const string &args) { if(args.empty() || args=="-") { - set_output(&IO::cout, false); - IO::print("Tracing to stdout\n"); + set_output(stdout, false); + printf("Tracing to stdout\n"); } else if(args=="on") { if(!out) - throw InvalidState("Output is not set"); + throw runtime_error("Output is not set"); enabled = true; - IO::print("Tracing enabled\n"); + printf("Tracing enabled\n"); } else if(args=="off") { enabled = false; - IO::print("Tracing disabled\n"); + printf("Tracing disabled\n"); } else if(args=="end") { set_output(0, true); - IO::print("Tracing terminated\n"); + printf("Tracing terminated\n"); } else { - set_output(new IO::File(args, IO::M_WRITE), true); - IO::print("Tracing to %s\n", args); + FILE *f = fopen(args.c_str(), "w"); + if(!f) + throw runtime_error("Could not open trace file"); + + set_output(f, true); + printf("Tracing to %s\n", args.c_str()); } } diff --git a/source/tracer.h b/source/tracer.h index 651bfc1..38ebc01 100644 --- a/source/tracer.h +++ b/source/tracer.h @@ -1,14 +1,14 @@ /* $Id$ This file is part of gldbg -Copyright © 2009-2010 Mikko Rasa, Mikkosoft Productions +Copyright © 2009-2011 Mikko Rasa, Mikkosoft Productions Distributed under the GPL */ #ifndef TRACER_H_ #define TRACER_H_ -#include +#include #include "tool.h" struct GlPrint; @@ -17,8 +17,8 @@ class Tracer: public RegisteredTool { private: GlPrint *glprint; - Msp::IO::Base *out; - bool delete_out; + FILE *out; + bool close_out; bool enabled; public: @@ -27,7 +27,7 @@ public: virtual void decode(const char *, unsigned); private: - void set_output(Msp::IO::Base *, bool); + void set_output(FILE *, bool); void cmd_trace(const std::string &); }; -- 2.45.2