3 This file is part of gldbg
4 Copyright © 2009 Mikko Rasa, Mikkosoft Productions
5 Distributed under the GPL
9 #include <readline/readline.h>
10 #include <msp/core/except.h>
11 #include <msp/io/file.h>
12 #include <msp/io/print.h>
13 #include <msp/strings/lexicalcast.h>
14 #include <msp/strings/utils.h>
15 #include "commandinterpreter.h"
23 CommandInterpreter::CommandInterpreter(GlDbg &d):
26 commands["help"] = Command(&CommandInterpreter::cmd_help,
27 "Provides help on commands",
29 " Displays a list of commands\n\n"
31 " Gives detailed information on a command\n");
32 commands["exit"] = Command(&CommandInterpreter::cmd_exit,
33 "Ends the debugging session");
35 commands["run"] = Command(&CommandInterpreter::cmd_run,
36 "Starts the program");
37 commands["continue"] = Command(&CommandInterpreter::cmd_continue,
38 "Resumes program execution");
39 commands["kill"] = Command(&CommandInterpreter::cmd_kill,
40 "Terminates the program immediately");
41 commands["signal"] = Command(&CommandInterpreter::cmd_signal,
42 "Resumes execution with a signal",
45 " Sends the signal identified by NUM or NAME to the program and resumes\n"
46 " execution. Currently recognized signal names are HUP, INT, TERM, SEGV\n"
49 commands["trace"] = Command(&CommandInterpreter::cmd_trace,
50 "Traces GL function calls",
52 " Send trace output to stdout.\n"
54 " Send trace output to FILE (- for stdout).\n\n"
56 " Temporarily suspend or resume trace without closing the file.\n\n"
58 " Terminate trace, closing the file.\n");
60 commands["profile"] = Command(&CommandInterpreter::cmd_profile,
61 "Profiles GL usage and performance",
63 " Enables or disables profiling\n");
65 commands["state"] = Command(&CommandInterpreter::cmd_state,
66 "Inspects general GL state",
68 " Print current vertex attributes\n\n"
70 " Show current bindings\n");
72 commands["texture"] = Command(&CommandInterpreter::cmd_texture,
73 "Inspect texture state",
75 " Lists texture objects\n\n"
77 " Print information about a texture object\n");
79 commands["buffer"] = Command(&CommandInterpreter::cmd_buffer,
80 "Inspect buffer object state",
82 " Lists buffer objects\n\n"
84 " Print information about a buffer object\n");
87 void CommandInterpreter::execute(const string &cmd)
89 unsigned space = cmd.find(' ');
90 string name = cmd.substr(0, space);
91 CommandMap::const_iterator i = commands.lower_bound(name);
92 if(i==commands.end() || i->first.compare(0, name.size(), name))
93 throw KeyError("Unknown command", name);
96 CommandMap::const_iterator j = i;
97 if((++j)!=commands.end() && !j->first.compare(0, name.size(), name))
98 throw KeyError("Ambiguous command", name);
102 if(space!=string::npos)
103 args = cmd.substr(space+1);
105 (this->*(i->second.func))(args);
108 void CommandInterpreter::cmd_help(const string &args)
112 for(map<string, Command>::const_iterator i=commands.begin(); i!=commands.end(); ++i)
113 IO::print("%-10s : %s\n", i->first, i->second.description);
117 map<string, Command>::const_iterator i = commands.find(args);
118 if(i==commands.end())
119 throw KeyError("Unknown command", args);
120 IO::print("%s : %s\n", i->first, i->second.description);
121 if(!i->second.help.empty())
122 IO::print("\n%s", i->second.help);
126 void CommandInterpreter::cmd_run(const string &)
131 void CommandInterpreter::cmd_continue(const string &)
133 IO::print("Continuing.\n");
134 gldbg.get_process().resume();
137 void CommandInterpreter::cmd_signal(const string &args)
140 if(args=="HUP" || args=="SIGHUP")
142 else if(args=="INT" || args=="SIGINT")
144 else if(args=="ILL" || args=="SIGILL")
146 else if(args=="SEGV" || args=="SIGSEGV")
148 else if(args=="TERM" || args=="SIGTERM")
150 else if(isnumrc(args))
151 sig = lexical_cast<unsigned>(args);
153 throw InvalidParameterValue("Invalid signal specification");
154 gldbg.get_process().resume(sig);
157 void CommandInterpreter::cmd_kill(const string &)
159 gldbg.get_process().kill();
162 void CommandInterpreter::cmd_exit(const string &)
164 if(gldbg.get_process().get_state()!=Process::INACTIVE)
166 IO::print("Program is still running. Kill it?\n");
167 char *answer = readline("[y/n] ");
170 gldbg.get_process().kill();
174 IO::print("Not confirmed.\n");
180 void CommandInterpreter::cmd_trace(const string &args)
182 Tracer &tracer = gldbg.get_tracer();
183 if(args.empty() || args=="-")
185 tracer.set_output(IO::cout);
186 IO::print("Tracing to stdout\n");
191 IO::print("Tracing enabled\n");
196 IO::print("Tracing disabled\n");
200 tracer.set_output(0);
201 IO::print("Tracing terminated\n");
205 tracer.set_output(new IO::File(args, IO::M_WRITE));
206 IO::print("Tracing to %s\n", args);
210 void CommandInterpreter::cmd_profile(const string &args)
212 Profiler &profiler = gldbg.get_profiler();
213 if(args.empty() || args=="on")
218 throw InvalidParameterValue("Invalid argument");
221 void CommandInterpreter::cmd_state(const string &args)
223 const GlState &glstate = gldbg.get_glstate();
226 IO::print("Current vertex attributes:\n");
227 const Vector4 &color = glstate.get_color();
228 IO::print(" Color: [%05.3f, %05.3f, %05.3f, %05.3f]\n", color.r, color.g, color.b, color.a);
229 for(unsigned i=0; i<8; ++i)
231 const Vector4 &texcoord = glstate.get_texcoord(i);
232 IO::print(" TexCoord%d: [%05.3f, %05.3f, %05.3f, %05.3f]\n", i, texcoord.s, texcoord.t, texcoord.p, texcoord.q);
234 const Vector3 &normal = glstate.get_normal();
235 IO::print(" Normal: [%05.3f, %05.3f, %05.3f]\n", normal.x, normal.y, normal.z);
237 else if(args=="bind")
239 IO::print("Current bindings:\n");
240 for(unsigned i=0; i<8; ++i)
242 IO::print(" Texture unit %d:\n", i);
243 const TexUnitState &unit = glstate.get_texture_unit(i);
244 IO::print(" GL_TEXTURE_2D: %s\n", unit.describe_binding(GL_TEXTURE_2D));
245 IO::print(" GL_TEXTURE_3D: %s\n", unit.describe_binding(GL_TEXTURE_3D));
247 IO::print(" Buffers:\n");
248 const BufferState *buf = glstate.get_current_buffer(GL_ARRAY_BUFFER);
249 IO::print(" GL_ARRAY_BUFFER: %d\n", (buf ? buf->id : 0));
250 buf = glstate.get_current_buffer(GL_ELEMENT_ARRAY_BUFFER);
251 IO::print(" GL_ELEMENT_ARRAY_BUFFER: %d\n", (buf ? buf->id : 0));
254 throw InvalidParameterValue("Invalid or missing argument");
257 void CommandInterpreter::cmd_texture(const string &args)
261 const map<unsigned, TextureState> &textures = gldbg.get_glstate().get_textures();
262 IO::print("%d texture objects:\n", textures.size());
263 for(map<unsigned, TextureState>::const_iterator i = textures.begin(); i!=textures.end(); ++i)
265 const TextureState &tex = i->second;
266 IO::print(" %d: %s, %d images\n", i->first, tex.describe(), tex.images.size());
271 unsigned id = lexical_cast<unsigned>(args);
272 const TextureState &tex = gldbg.get_glstate().get_texture(id);
273 IO::print("Texture object %d\n", id);
274 IO::print(" Target: %s\n", describe_enum(tex.target, "TextureTarget"));
275 IO::print(" Images:\n");
276 for(unsigned i=0; i<tex.images.size(); ++i)
278 const TexImageState &img = tex.images[i];
279 IO::print(" Level %2d: %s\n", i, img.describe());
281 IO::print(" Min. filter: %s\n", describe_enum(tex.min_filter, "TextureMinFilter"));
282 IO::print(" Mag. filter: %s\n", describe_enum(tex.mag_filter, "TextureMagFilter"));
283 IO::print(" Wrap modes: S=%s / T=%s / R=%s\n", describe_enum(tex.wrap_s, "TextureWrapMode"),
284 describe_enum(tex.wrap_t, "TextureWrapMode"), describe_enum(tex.wrap_r, "TextureWrapMode"));
285 IO::print(" Compare mode: %s\n", describe_enum(tex.compare_mode, ""));
286 IO::print(" Compare func: %s\n", describe_enum(tex.compare_func, "DepthFunction"));
287 IO::print(" Generate mipmap: %s\n", tex.generate_mipmap);
291 void CommandInterpreter::cmd_buffer(const string &args)
295 const GlState::BufferMap &buffers = gldbg.get_glstate().get_buffers();
296 IO::print("%d buffers:\n", buffers.size());
297 for(GlState::BufferMap::const_iterator i=buffers.begin(); i!=buffers.end(); ++i)
298 IO::print(" %d: %s\n", i->first, i->second.describe());
302 unsigned id = lexical_cast<unsigned>(args);
303 const BufferState &buf = gldbg.get_glstate().get_buffer(id);
304 IO::print("Buffer %d:\n", id);
305 IO::print(" Size: %d bytes\n", buf.size);
306 IO::print(" Usage: %s\n", describe_enum(buf.usage, ""));
311 CommandInterpreter::Command::Command():
315 CommandInterpreter::Command::Command(Func f, const string &d):
320 CommandInterpreter::Command::Command(Func f, const string &d, const string &h):