-/* $Id$
-
-This file is part of gldbg
-Copyright © 2010 Mikko Rasa, Mikkosoft Productions
-Distributed under the GPL
-*/
-
-#include <msp/io/print.h>
+#include <stdexcept>
+#include <cstdio>
+#include <cstdlib>
+#include "breakpoint.h"
#include "enums.h"
+#include "functions.h"
#include "gldbg.h"
#include "inspector.h"
+#include "strformat.h"
using namespace std;
-using namespace Msp;
-Inspector::Inspector(GlDbg &dbg)
+Inspector::Inspector(GlDbg &d):
+ gldbg(d),
+ decoder(gldecoder_new(this, NULL)),
+ query_state(0)
{
- CommandInterpreter &cmd_interp = dbg.get_command_interpreter();
+ CommandInterpreter &cmd_interp = gldbg.get_command_interpreter();
cmd_interp.register_command("state", this, &Inspector::cmd_state)
.set_help("Inspects general GL state",
"state vertex\n"
" Print current vertex attributes\n\n"
+ "state array\n"
+ " Show current vertex arrays\n\n"
"state bind\n"
" Show current bindings\n");
" Lists buffer objects\n\n"
"buffer ID\n"
" Print information about a buffer object\n");
+
+ cmd_interp.register_command("shader", this, &Inspector::cmd_shader)
+ .set_help("Inspect shader object state",
+ "shader\n"
+ " List shader objects\n\n"
+ "shader ID\n"
+ " Print information about a shader object\n");
+
+ cmd_interp.register_command("program", this, &Inspector::cmd_program)
+ .set_help("Inspect program object state",
+ "program\n"
+ " List program objects\n\n"
+ "program ID\n"
+ " Print information about a program object\n");
+
+ decoder->gldBreak = gldBreak;
+}
+
+Inspector::~Inspector()
+{
+ gldecoder_delete(decoder);
}
void Inspector::decode(const char *data, unsigned len)
{
+ if(query_state)
+ gldecoder_decode(decoder, data, len);
state.decode(data, len);
}
+void Inspector::process_started()
+{
+ gldbg.set_breakpoint(FUNC_GLXMAKECURRENT, BREAK_RETURN, this);
+ query_state = 1;
+}
+
+void Inspector::process_stopped(int reason)
+{
+ if((reason>>8)==3 && query_state==2)
+ {
+ GlPacket *pkt = packet_begin(FUNC_GLDQUERYLIMITS);
+ gldbg.send(pkt);
+ query_state = 0;
+ gldbg.clear_breakpoint(FUNC_GLXMAKECURRENT, BREAK_RETURN, this);
+ gldbg.resume_from_break(this);
+ }
+}
+
+void Inspector::gldBreak(void *user_data, unsigned short func, unsigned char flag)
+{
+ if(func==FUNC_GLXMAKECURRENT && flag==BREAK_RETURN)
+ ++reinterpret_cast<Inspector *>(user_data)->query_state;
+}
+
+void Inspector::print_indented(const string &str, unsigned indent)
+{
+ string spaces(indent, ' ');
+ string::size_type start = 0;
+ while(1)
+ {
+ string::size_type newline = str.find('\n', start);
+ string line = str.substr(start, newline-start);
+ if(newline!=string::npos || !line.empty())
+ printf("%s%s\n", spaces.c_str(), line.c_str());
+ if(newline==string::npos)
+ break;
+ start = newline+1;
+ }
+}
+
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);
- for(unsigned i=0; i<8; ++i)
+ printf(" Color: [%05.3f, %05.3f, %05.3f, %05.3f]\n", color.r, color.g, color.b, color.a);
+ unsigned count = glstate.get_max_texture_units();
+ for(unsigned i=0; i<count; ++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=="array")
+ {
+ printf("Current vertex arrays:\n");
+ string descr = glstate.get_array(GL_VERTEX_ARRAY).describe();
+ printf(" Vertex: %s\n", descr.c_str());
+ descr = glstate.get_array(GL_NORMAL_ARRAY).describe();
+ printf(" Normal: %s\n", descr.c_str());
+ descr = glstate.get_array(GL_COLOR_ARRAY).describe();
+ printf(" Color: %s\n", descr.c_str());
+ unsigned count = glstate.get_max_texture_units();
+ for(unsigned i=0; i<count; ++i)
+ {
+ descr = glstate.get_texture_coord_array(i).describe();
+ printf(" TexCoord%d: %s\n", i, descr.c_str());
+ }
+ count = glstate.get_max_vertex_attribs();
+ for(unsigned i=0; i<count; ++i)
+ {
+ descr = glstate.get_attrib_array(i).describe();
+ printf(" Attrib%d:%s %s\n", i, (i>=10 ? " " : " "), descr.c_str());
+ }
}
else if(args=="bind")
{
- IO::print("Current bindings:\n");
- for(unsigned i=0; i<8; ++i)
+ printf("Current bindings:\n");
+ unsigned count = glstate.get_max_texture_units();
+ for(unsigned i=0; i<count; ++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_1D);
+ printf(" GL_TEXTURE_1D: %s\n", descr.c_str());
+ 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());
+ descr = unit.describe_binding(GL_TEXTURE_CUBE_MAP);
+ printf(" GL_TEXTURE_CUBE_MAP: %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));
+ buf = glstate.get_current_buffer(GL_UNIFORM_BUFFER);
+ printf(" GL_UNIFORM_BUFFER: %d\n", (buf ? buf->id : 0));
+ count = glstate.get_max_uniform_buffer_bindings();
+ for(unsigned i=0; i<count; ++i)
+ {
+ const BufferBindingState &binding = glstate.get_buffer_binding(GL_UNIFORM_BUFFER, i);
+ if(binding.buffer)
+ printf(" %d: %d (%d bytes at %d)\n", i, binding.buffer->id, binding.size, binding.offset);
+ }
}
else
- throw InvalidParameterValue("Invalid or missing argument");
+ throw runtime_error("Invalid or missing argument");
}
void Inspector::cmd_texture(const string &args)
if(args.empty())
{
const map<unsigned, TextureState> &textures = state.get_textures();
- IO::print("%d texture objects:\n", textures.size());
+ printf("%lu texture objects:\n", textures.size());
for(map<unsigned, TextureState>::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, %lu images\n", i->first, descr.c_str(), tex.images.size());
}
}
else
{
- unsigned id = lexical_cast<unsigned>(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");
- for(unsigned i=0; i<tex.images.size(); ++i)
- {
- const TexImageState &img = tex.images[i];
- IO::print(" Level %2d: %s\n", i, img.describe());
- }
- IO::print(" Min. filter: %s\n", describe_enum(tex.min_filter, "TextureMinFilter"));
- IO::print(" Mag. filter: %s\n", describe_enum(tex.mag_filter, "TextureMagFilter"));
- IO::print(" Wrap modes: S=%s / T=%s / R=%s\n", describe_enum(tex.wrap_s, "TextureWrapMode"),
- describe_enum(tex.wrap_t, "TextureWrapMode"), describe_enum(tex.wrap_r, "TextureWrapMode"));
- IO::print(" Compare mode: %s\n", describe_enum(tex.compare_mode, ""));
- IO::print(" Compare func: %s\n", describe_enum(tex.compare_func, "DepthFunction"));
- IO::print(" Generate mipmap: %s\n", tex.generate_mipmap);
+ printf("Texture object %d\n", id);
+ printf(" Target: %s\n", describe_enum(tex.target, "TextureTarget"));
+ printf(" Images:\n");
+ if(tex.target==GL_TEXTURE_CUBE_MAP)
+ {
+ static const char *face_labels[6] = { "+X", "-X", "+Y", "-Y", "+Z", "-Z" };
+ for(unsigned i=0; i<6; ++i)
+ {
+ printf(" Face %s:\n", face_labels[i]);
+ for(unsigned j=i; j<tex.images.size(); j+=6)
+ {
+ string descr = tex.images[j].describe();
+ printf(" Level %2d: %s\n", j/6, descr.c_str());
+ }
+ }
+ }
+ else
+ {
+ for(unsigned i=0; i<tex.images.size(); ++i)
+ {
+ string descr = tex.images[i].describe();
+ printf(" Level %2d: %s\n", i, descr.c_str());
+ }
+ }
+ printf(" Min. filter: %s\n", describe_enum(tex.min_filter, "TextureMinFilter"));
+ printf(" Mag. filter: %s\n", describe_enum(tex.mag_filter, "TextureMagFilter"));
+ printf(" Wrap modes:\n");
+ printf(" S: %s\n", describe_enum(tex.wrap_s, "TextureWrapMode"));
+ printf(" T: %s\n", describe_enum(tex.wrap_t, "TextureWrapMode"));
+ printf(" R: %s\n", describe_enum(tex.wrap_r, "TextureWrapMode"));
+ printf(" Compare mode: %s\n", describe_enum(tex.compare_mode, ""));
+ printf(" Compare func: %s\n", describe_enum(tex.compare_func, "DepthFunction"));
+ printf(" Generate mipmap: %s\n", (tex.generate_mipmap ? "true" : "false"));
}
}
if(args.empty())
{
const GlState::BufferMap &buffers = state.get_buffers();
- IO::print("%d buffers:\n", buffers.size());
+ printf("%lu buffers:\n", buffers.size());
for(GlState::BufferMap::const_iterator i=buffers.begin(); i!=buffers.end(); ++i)
- IO::print(" %d: %s\n", i->first, i->second.describe());
+ {
+ string descr = i->second.describe();
+ printf(" %d: %s\n", i->first, descr.c_str());
+ }
}
else
{
- unsigned id = lexical_cast<unsigned>(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, ""));
- if(buf.content.stride)
+ 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();
+ printf(" Arrays:\n");
+ printf(" 0: Element indices, 1 %s\n", describe_enum(array.type, "DataType"));
+
+ printf(" Data:\n");
+ unsigned width = 1+buf.content.stride*2;
+ char fmt[6];
+ snprintf(fmt, sizeof(fmt), " %%%du", width);
+ unsigned n_elems = buf.size/buf.content.stride;
+ string line;
+ const char *ptr = buf.data;
+ for(unsigned i=0; i<n_elems; ++i)
+ {
+ if(line.empty())
+ line = " ";
+
+ if(array.type==GL_UNSIGNED_BYTE)
+ line += strformat(fmt, *(reinterpret_cast<const unsigned char *>(ptr)+i));
+ else if(array.type==GL_UNSIGNED_SHORT)
+ line += strformat(fmt, *(reinterpret_cast<const unsigned short *>(ptr)+i));
+ else if(array.type==GL_UNSIGNED_INT)
+ line += strformat(fmt, *(reinterpret_cast<const unsigned *>(ptr)+i));
+
+ if(line.size()+1+width>79)
+ {
+ printf("%s\n", line.c_str());
+ line.clear();
+ }
+ }
+
+ if(!line.empty())
+ 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<BufferContent::Array> &arrays = buf.content.arrays;
for(vector<BufferContent::Array>::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<BufferContent::Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
{
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;
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; i<n_verts; ++i)
for(unsigned k=0; k<j->size; ++k)
{
if(j->type==GL_FLOAT)
- line += format(" %5.2f", *(reinterpret_cast<const float *>(base)+k));
+ line += strformat(" %5.2f", *(reinterpret_cast<const float *>(base)+k));
else if(j->type==GL_UNSIGNED_BYTE)
- line += format(" %3u", *(reinterpret_cast<const unsigned char *>(base)+k));
+ line += strformat(" %3u", *(reinterpret_cast<const unsigned char *>(base)+k));
}
}
- IO::print("%3d:%s\n", i, line);
+ printf("%3d:%s\n", i, line.c_str());
}
}
}
}
+
+void Inspector::cmd_shader(const string &args)
+{
+ if(args.empty())
+ {
+ const GlState::ShaderMap &shaders = state.get_shaders();
+ printf("%lu shader objects:\n", shaders.size());
+ for(GlState::ShaderMap::const_iterator i=shaders.begin(); i!=shaders.end(); ++i)
+ {
+ string descr = i->second.describe();
+ printf(" %d: %s\n", i->first, descr.c_str());
+ }
+ }
+ else
+ {
+ char *end = 0;
+ unsigned id = strtoul(args.c_str(), &end, 0);
+ if(end && *end)
+ throw runtime_error("Invalid shader id");
+
+ const ShaderState &shader = state.get_shader(id);
+ printf("Shader %d:\n", shader.id);
+ printf(" Type: %s\n", describe_enum(shader.type, ""));
+ unsigned n = 0;
+ for(vector<string>::const_iterator i=shader.source.begin(); i!=shader.source.end(); ++i, ++n)
+ {
+ printf(" Source string %d:\n", n);
+ print_indented(*i, 4);
+ }
+ if(shader.source_changed)
+ printf(" Source changed since last compile\n");
+ printf(" Compile status: %d\n", shader.compile_status);
+ if(shader.info_log.empty())
+ printf(" Info log is empty\n");
+ else
+ {
+ printf(" Info log:\n");
+ print_indented(shader.info_log, 4);
+ }
+ if(shader.pending_delete)
+ printf(" Pending deletion\n");
+ }
+}
+
+void Inspector::cmd_program(const std::string &args)
+{
+ if(args.empty())
+ {
+ const GlState::ProgramMap &programs = state.get_programs();
+ printf("%lu program objects:\n", programs.size());
+ for(GlState::ProgramMap::const_iterator i=programs.begin(); i!=programs.end(); ++i)
+ {
+ string descr = i->second.describe();
+ printf(" %d: %s\n", i->first, descr.c_str());
+ }
+ }
+ else
+ {
+ char *end = 0;
+ unsigned id = strtoul(args.c_str(), &end, 0);
+ if(end && *end)
+ throw runtime_error("Invalid program id");
+
+ const ProgramState &program = state.get_program(id);
+ printf("Program %d:\n", program.id);
+ printf(" Attached shaders:\n");
+ for(vector<ShaderState *>::const_iterator i=program.shaders.begin(); i!=program.shaders.end(); ++i)
+ {
+ string descr = (*i)->describe();
+ printf(" %d: %s\n", (*i)->id, descr.c_str());
+ }
+ if(program.shaders_changed)
+ printf(" Shaders changed since last compile\n");
+ printf(" Link status: %d\n", program.link_status);
+ if(program.info_log.empty())
+ printf(" Info log is empty\n");
+ else
+ {
+ printf(" Info log:\n");
+ print_indented(program.info_log, 4);
+ }
+ }
+}