3 This file is part of gldbg
4 Copyright © 2009 Mikko Rasa, Mikkosoft Productions
5 Distributed under the GPL
9 #include <msp/core/except.h>
10 #include <msp/io/file.h>
11 #include <msp/io/print.h>
12 #include <msp/strings/lexicalcast.h>
13 #include <msp/strings/utils.h>
14 #include "commandinterpreter.h"
22 CommandInterpreter::CommandInterpreter(GlDbg &d):
25 commands["help"] = Command(&CommandInterpreter::cmd_help,
26 "Provides help on commands",
28 " Displays a list of commands\n\n"
30 " Gives detailed information on a command\n");
31 commands["exit"] = Command(&CommandInterpreter::cmd_exit,
32 "Ends the debugging session");
34 commands["run"] = Command(&CommandInterpreter::cmd_run,
35 "Starts the program");
36 commands["continue"] = Command(&CommandInterpreter::cmd_continue,
37 "Resumes program execution");
38 commands["kill"] = Command(&CommandInterpreter::cmd_kill,
39 "Terminates the program immediately");
40 commands["signal"] = Command(&CommandInterpreter::cmd_signal,
41 "Resumes execution with a signal",
44 " Sends the signal identified by NUM or NAME to the program and resumes\n"
45 " execution. Currently recognized signal names are HUP, INT, TERM, SEGV\n"
48 commands["trace"] = Command(&CommandInterpreter::cmd_trace,
49 "Traces GL function calls",
51 " Send trace output to FILE. As a special case, - means stdout.\n\n"
53 " Temporarily suspend or resume trace without closing the file.\n\n"
55 " Terminate trace, closing the file.\n");
57 commands["state"] = Command(&CommandInterpreter::cmd_state,
58 "Inspects general GL state",
60 " Print current vertex attributes\n\n"
62 " Show current bindings\n");
64 commands["texture"] = Command(&CommandInterpreter::cmd_texture,
65 "Inspect texture state",
67 " Lists texture objects\n\n"
69 " Print information about a texture object\n");
71 commands["buffer"] = Command(&CommandInterpreter::cmd_buffer,
72 "Inspect buffer object state",
74 " Lists buffer objects\n\n"
76 " Print information about a buffer object\n");
79 void CommandInterpreter::execute(const string &cmd)
81 unsigned space = cmd.find(' ');
82 string name = cmd.substr(0, space);
83 CommandMap::const_iterator i = commands.lower_bound(name);
84 if(i==commands.end() || i->first.compare(0, name.size(), name))
85 throw KeyError("Unknown command", name);
88 CommandMap::const_iterator j = i;
89 if((++j)!=commands.end() && !j->first.compare(0, name.size(), name))
90 throw KeyError("Ambiguous command", name);
94 if(space!=string::npos)
95 args = cmd.substr(space+1);
97 (this->*(i->second.func))(args);
100 void CommandInterpreter::cmd_help(const string &args)
104 for(map<string, Command>::const_iterator i=commands.begin(); i!=commands.end(); ++i)
105 IO::print("%-10s : %s\n", i->first, i->second.description);
109 map<string, Command>::const_iterator i = commands.find(args);
110 if(i==commands.end())
111 throw KeyError("Unknown command", args);
112 IO::print("%s : %s\n", i->first, i->second.description);
113 if(!i->second.help.empty())
114 IO::print("\n%s", i->second.help);
118 void CommandInterpreter::cmd_run(const string &)
123 void CommandInterpreter::cmd_continue(const string &)
125 IO::print("Continuing.\n");
126 gldbg.get_process().resume();
129 void CommandInterpreter::cmd_signal(const string &args)
132 if(args=="HUP" || args=="SIGHUP")
134 else if(args=="INT" || args=="SIGINT")
136 else if(args=="ILL" || args=="SIGILL")
138 else if(args=="SEGV" || args=="SIGSEGV")
140 else if(args=="TERM" || args=="SIGTERM")
142 else if(isnumrc(args))
143 sig = lexical_cast<unsigned>(args);
145 throw InvalidParameterValue("Invalid signal specification");
146 gldbg.get_process().resume(sig);
149 void CommandInterpreter::cmd_kill(const string &)
151 gldbg.get_process().kill();
154 void CommandInterpreter::cmd_exit(const string &)
159 void CommandInterpreter::cmd_trace(const string &args)
161 Tracer &tracer = gldbg.get_tracer();
164 string fn = args.substr(1);
167 tracer.set_output(IO::cout);
168 IO::print("Tracing to stdout\n");
172 tracer.set_output(new IO::File(fn, IO::M_WRITE));
173 IO::print("Tracing to %s\n", fn);
181 IO::print("Tracing enabled\n");
186 IO::print("Tracing disabled\n");
190 tracer.set_output(0);
191 IO::print("Tracing terminated\n");
196 void CommandInterpreter::cmd_state(const string &args)
198 const GlState &glstate = gldbg.get_glstate();
201 IO::print("Current vertex attributes:\n");
202 const Vector4 &color = glstate.get_color();
203 IO::print(" Color: [%05.3f, %05.3f, %05.3f, %05.3f]\n", color.r, color.g, color.b, color.a);
204 for(unsigned i=0; i<8; ++i)
206 const Vector4 &texcoord = glstate.get_texcoord(i);
207 IO::print(" TexCoord%d: [%05.3f, %05.3f, %05.3f, %05.3f]\n", i, texcoord.s, texcoord.t, texcoord.p, texcoord.q);
209 const Vector3 &normal = glstate.get_normal();
210 IO::print(" Normal: [%05.3f, %05.3f, %05.3f]\n", normal.x, normal.y, normal.z);
212 else if(args=="bind")
214 IO::print("Current bindings:\n");
215 for(unsigned i=0; i<8; ++i)
217 IO::print(" Texture unit %d:\n", i);
218 const TexUnitState &unit = glstate.get_texture_unit(i);
219 IO::print(" GL_TEXTURE_2D: %s\n", unit.describe_binding(GL_TEXTURE_2D));
220 IO::print(" GL_TEXTURE_3D: %s\n", unit.describe_binding(GL_TEXTURE_3D));
222 IO::print(" Buffers:\n");
223 const BufferState *buf = glstate.get_current_buffer(GL_ARRAY_BUFFER);
224 IO::print(" GL_ARRAY_BUFFER: %d\n", (buf ? buf->id : 0));
225 buf = glstate.get_current_buffer(GL_ELEMENT_ARRAY_BUFFER);
226 IO::print(" GL_ELEMENT_ARRAY_BUFFER: %d\n", (buf ? buf->id : 0));
229 throw InvalidParameterValue("Invalid or missing argument");
232 void CommandInterpreter::cmd_texture(const string &args)
236 const map<unsigned, TextureState> &textures = gldbg.get_glstate().get_textures();
237 IO::print("%d texture objects:\n", textures.size());
238 for(map<unsigned, TextureState>::const_iterator i = textures.begin(); i!=textures.end(); ++i)
240 const TextureState &tex = i->second;
241 IO::print(" %d: %s, %d images\n", i->first, tex.describe(), tex.images.size());
246 unsigned id = lexical_cast<unsigned>(args);
247 const TextureState &tex = gldbg.get_glstate().get_texture(id);
248 IO::print("Texture object %d\n", id);
249 IO::print(" Target: %s\n", describe_enum(tex.target, "TextureTarget"));
250 IO::print(" Images:\n");
251 for(unsigned i=0; i<tex.images.size(); ++i)
253 const TexImageState &img = tex.images[i];
254 IO::print(" Level %2d: %s\n", i, img.describe());
256 IO::print(" Min. filter: %s\n", describe_enum(tex.min_filter, "TextureMinFilter"));
257 IO::print(" Mag. filter: %s\n", describe_enum(tex.mag_filter, "TextureMagFilter"));
258 IO::print(" Wrap modes: S=%s / T=%s / R=%s\n", describe_enum(tex.wrap_s, "TextureWrapMode"),
259 describe_enum(tex.wrap_t, "TextureWrapMode"), describe_enum(tex.wrap_r, "TextureWrapMode"));
260 IO::print(" Compare mode: %s\n", describe_enum(tex.compare_mode, ""));
261 IO::print(" Compare func: %s\n", describe_enum(tex.compare_func, "DepthFunction"));
262 IO::print(" Generate mipmap: %s\n", tex.generate_mipmap);
266 void CommandInterpreter::cmd_buffer(const string &args)
270 const GlState::BufferMap &buffers = gldbg.get_glstate().get_buffers();
271 IO::print("%d buffers:\n", buffers.size());
272 for(GlState::BufferMap::const_iterator i=buffers.begin(); i!=buffers.end(); ++i)
273 IO::print(" %d: %s\n", i->first, i->second.describe());
277 unsigned id = lexical_cast<unsigned>(args);
278 const BufferState &buf = gldbg.get_glstate().get_buffer(id);
279 IO::print("Buffer %d:\n", id);
280 IO::print(" Size: %d bytes\n", buf.size);
281 IO::print(" Usage: %s\n", describe_enum(buf.usage, ""));
286 CommandInterpreter::Command::Command():
290 CommandInterpreter::Command::Command(Func f, const string &d):
295 CommandInterpreter::Command::Command(Func f, const string &d, const string &h):