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 stdout.\n"
53 " Send trace output to FILE (- for stdout).\n\n"
55 " Temporarily suspend or resume trace without closing the file.\n\n"
57 " Terminate trace, closing the file.\n");
59 commands["state"] = Command(&CommandInterpreter::cmd_state,
60 "Inspects general GL state",
62 " Print current vertex attributes\n\n"
64 " Show current bindings\n");
66 commands["texture"] = Command(&CommandInterpreter::cmd_texture,
67 "Inspect texture state",
69 " Lists texture objects\n\n"
71 " Print information about a texture object\n");
73 commands["buffer"] = Command(&CommandInterpreter::cmd_buffer,
74 "Inspect buffer object state",
76 " Lists buffer objects\n\n"
78 " Print information about a buffer object\n");
81 void CommandInterpreter::execute(const string &cmd)
83 unsigned space = cmd.find(' ');
84 string name = cmd.substr(0, space);
85 CommandMap::const_iterator i = commands.lower_bound(name);
86 if(i==commands.end() || i->first.compare(0, name.size(), name))
87 throw KeyError("Unknown command", name);
90 CommandMap::const_iterator j = i;
91 if((++j)!=commands.end() && !j->first.compare(0, name.size(), name))
92 throw KeyError("Ambiguous command", name);
96 if(space!=string::npos)
97 args = cmd.substr(space+1);
99 (this->*(i->second.func))(args);
102 void CommandInterpreter::cmd_help(const string &args)
106 for(map<string, Command>::const_iterator i=commands.begin(); i!=commands.end(); ++i)
107 IO::print("%-10s : %s\n", i->first, i->second.description);
111 map<string, Command>::const_iterator i = commands.find(args);
112 if(i==commands.end())
113 throw KeyError("Unknown command", args);
114 IO::print("%s : %s\n", i->first, i->second.description);
115 if(!i->second.help.empty())
116 IO::print("\n%s", i->second.help);
120 void CommandInterpreter::cmd_run(const string &)
125 void CommandInterpreter::cmd_continue(const string &)
127 IO::print("Continuing.\n");
128 gldbg.get_process().resume();
131 void CommandInterpreter::cmd_signal(const string &args)
134 if(args=="HUP" || args=="SIGHUP")
136 else if(args=="INT" || args=="SIGINT")
138 else if(args=="ILL" || args=="SIGILL")
140 else if(args=="SEGV" || args=="SIGSEGV")
142 else if(args=="TERM" || args=="SIGTERM")
144 else if(isnumrc(args))
145 sig = lexical_cast<unsigned>(args);
147 throw InvalidParameterValue("Invalid signal specification");
148 gldbg.get_process().resume(sig);
151 void CommandInterpreter::cmd_kill(const string &)
153 gldbg.get_process().kill();
156 void CommandInterpreter::cmd_exit(const string &)
161 void CommandInterpreter::cmd_trace(const string &args)
163 Tracer &tracer = gldbg.get_tracer();
164 if(args.empty() || args=="-")
166 tracer.set_output(IO::cout);
167 IO::print("Tracing to stdout\n");
172 IO::print("Tracing enabled\n");
177 IO::print("Tracing disabled\n");
181 tracer.set_output(0);
182 IO::print("Tracing terminated\n");
186 tracer.set_output(new IO::File(args, IO::M_WRITE));
187 IO::print("Tracing to %s\n", args);
191 void CommandInterpreter::cmd_state(const string &args)
193 const GlState &glstate = gldbg.get_glstate();
196 IO::print("Current vertex attributes:\n");
197 const Vector4 &color = glstate.get_color();
198 IO::print(" Color: [%05.3f, %05.3f, %05.3f, %05.3f]\n", color.r, color.g, color.b, color.a);
199 for(unsigned i=0; i<8; ++i)
201 const Vector4 &texcoord = glstate.get_texcoord(i);
202 IO::print(" TexCoord%d: [%05.3f, %05.3f, %05.3f, %05.3f]\n", i, texcoord.s, texcoord.t, texcoord.p, texcoord.q);
204 const Vector3 &normal = glstate.get_normal();
205 IO::print(" Normal: [%05.3f, %05.3f, %05.3f]\n", normal.x, normal.y, normal.z);
207 else if(args=="bind")
209 IO::print("Current bindings:\n");
210 for(unsigned i=0; i<8; ++i)
212 IO::print(" Texture unit %d:\n", i);
213 const TexUnitState &unit = glstate.get_texture_unit(i);
214 IO::print(" GL_TEXTURE_2D: %s\n", unit.describe_binding(GL_TEXTURE_2D));
215 IO::print(" GL_TEXTURE_3D: %s\n", unit.describe_binding(GL_TEXTURE_3D));
217 IO::print(" Buffers:\n");
218 const BufferState *buf = glstate.get_current_buffer(GL_ARRAY_BUFFER);
219 IO::print(" GL_ARRAY_BUFFER: %d\n", (buf ? buf->id : 0));
220 buf = glstate.get_current_buffer(GL_ELEMENT_ARRAY_BUFFER);
221 IO::print(" GL_ELEMENT_ARRAY_BUFFER: %d\n", (buf ? buf->id : 0));
224 throw InvalidParameterValue("Invalid or missing argument");
227 void CommandInterpreter::cmd_texture(const string &args)
231 const map<unsigned, TextureState> &textures = gldbg.get_glstate().get_textures();
232 IO::print("%d texture objects:\n", textures.size());
233 for(map<unsigned, TextureState>::const_iterator i = textures.begin(); i!=textures.end(); ++i)
235 const TextureState &tex = i->second;
236 IO::print(" %d: %s, %d images\n", i->first, tex.describe(), tex.images.size());
241 unsigned id = lexical_cast<unsigned>(args);
242 const TextureState &tex = gldbg.get_glstate().get_texture(id);
243 IO::print("Texture object %d\n", id);
244 IO::print(" Target: %s\n", describe_enum(tex.target, "TextureTarget"));
245 IO::print(" Images:\n");
246 for(unsigned i=0; i<tex.images.size(); ++i)
248 const TexImageState &img = tex.images[i];
249 IO::print(" Level %2d: %s\n", i, img.describe());
251 IO::print(" Min. filter: %s\n", describe_enum(tex.min_filter, "TextureMinFilter"));
252 IO::print(" Mag. filter: %s\n", describe_enum(tex.mag_filter, "TextureMagFilter"));
253 IO::print(" Wrap modes: S=%s / T=%s / R=%s\n", describe_enum(tex.wrap_s, "TextureWrapMode"),
254 describe_enum(tex.wrap_t, "TextureWrapMode"), describe_enum(tex.wrap_r, "TextureWrapMode"));
255 IO::print(" Compare mode: %s\n", describe_enum(tex.compare_mode, ""));
256 IO::print(" Compare func: %s\n", describe_enum(tex.compare_func, "DepthFunction"));
257 IO::print(" Generate mipmap: %s\n", tex.generate_mipmap);
261 void CommandInterpreter::cmd_buffer(const string &args)
265 const GlState::BufferMap &buffers = gldbg.get_glstate().get_buffers();
266 IO::print("%d buffers:\n", buffers.size());
267 for(GlState::BufferMap::const_iterator i=buffers.begin(); i!=buffers.end(); ++i)
268 IO::print(" %d: %s\n", i->first, i->second.describe());
272 unsigned id = lexical_cast<unsigned>(args);
273 const BufferState &buf = gldbg.get_glstate().get_buffer(id);
274 IO::print("Buffer %d:\n", id);
275 IO::print(" Size: %d bytes\n", buf.size);
276 IO::print(" Usage: %s\n", describe_enum(buf.usage, ""));
281 CommandInterpreter::Command::Command():
285 CommandInterpreter::Command::Command(Func f, const string &d):
290 CommandInterpreter::Command::Command(Func f, const string &d, const string &h):