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["state"] = Command(&CommandInterpreter::cmd_state,
61 "Inspects general GL state",
63 " Print current vertex attributes\n\n"
65 " Show current bindings\n");
67 commands["texture"] = Command(&CommandInterpreter::cmd_texture,
68 "Inspect texture state",
70 " Lists texture objects\n\n"
72 " Print information about a texture object\n");
74 commands["buffer"] = Command(&CommandInterpreter::cmd_buffer,
75 "Inspect buffer object state",
77 " Lists buffer objects\n\n"
79 " Print information about a buffer object\n");
82 void CommandInterpreter::execute(const string &cmd)
84 unsigned space = cmd.find(' ');
85 string name = cmd.substr(0, space);
86 CommandMap::const_iterator i = commands.lower_bound(name);
87 if(i==commands.end() || i->first.compare(0, name.size(), name))
88 throw KeyError("Unknown command", name);
91 CommandMap::const_iterator j = i;
92 if((++j)!=commands.end() && !j->first.compare(0, name.size(), name))
93 throw KeyError("Ambiguous command", name);
97 if(space!=string::npos)
98 args = cmd.substr(space+1);
100 (this->*(i->second.func))(args);
103 void CommandInterpreter::cmd_help(const string &args)
107 for(map<string, Command>::const_iterator i=commands.begin(); i!=commands.end(); ++i)
108 IO::print("%-10s : %s\n", i->first, i->second.description);
112 map<string, Command>::const_iterator i = commands.find(args);
113 if(i==commands.end())
114 throw KeyError("Unknown command", args);
115 IO::print("%s : %s\n", i->first, i->second.description);
116 if(!i->second.help.empty())
117 IO::print("\n%s", i->second.help);
121 void CommandInterpreter::cmd_run(const string &)
126 void CommandInterpreter::cmd_continue(const string &)
128 IO::print("Continuing.\n");
129 gldbg.get_process().resume();
132 void CommandInterpreter::cmd_signal(const string &args)
135 if(args=="HUP" || args=="SIGHUP")
137 else if(args=="INT" || args=="SIGINT")
139 else if(args=="ILL" || args=="SIGILL")
141 else if(args=="SEGV" || args=="SIGSEGV")
143 else if(args=="TERM" || args=="SIGTERM")
145 else if(isnumrc(args))
146 sig = lexical_cast<unsigned>(args);
148 throw InvalidParameterValue("Invalid signal specification");
149 gldbg.get_process().resume(sig);
152 void CommandInterpreter::cmd_kill(const string &)
154 gldbg.get_process().kill();
157 void CommandInterpreter::cmd_exit(const string &)
159 if(gldbg.get_process().get_state()!=Process::INACTIVE)
161 IO::print("Program is still running. Kill it?\n");
162 char *answer = readline("[y/n] ");
165 gldbg.get_process().kill();
169 IO::print("Not confirmed.\n");
175 void CommandInterpreter::cmd_trace(const string &args)
177 Tracer &tracer = gldbg.get_tracer();
178 if(args.empty() || args=="-")
180 tracer.set_output(IO::cout);
181 IO::print("Tracing to stdout\n");
186 IO::print("Tracing enabled\n");
191 IO::print("Tracing disabled\n");
195 tracer.set_output(0);
196 IO::print("Tracing terminated\n");
200 tracer.set_output(new IO::File(args, IO::M_WRITE));
201 IO::print("Tracing to %s\n", args);
205 void CommandInterpreter::cmd_state(const string &args)
207 const GlState &glstate = gldbg.get_glstate();
210 IO::print("Current vertex attributes:\n");
211 const Vector4 &color = glstate.get_color();
212 IO::print(" Color: [%05.3f, %05.3f, %05.3f, %05.3f]\n", color.r, color.g, color.b, color.a);
213 for(unsigned i=0; i<8; ++i)
215 const Vector4 &texcoord = glstate.get_texcoord(i);
216 IO::print(" TexCoord%d: [%05.3f, %05.3f, %05.3f, %05.3f]\n", i, texcoord.s, texcoord.t, texcoord.p, texcoord.q);
218 const Vector3 &normal = glstate.get_normal();
219 IO::print(" Normal: [%05.3f, %05.3f, %05.3f]\n", normal.x, normal.y, normal.z);
221 else if(args=="bind")
223 IO::print("Current bindings:\n");
224 for(unsigned i=0; i<8; ++i)
226 IO::print(" Texture unit %d:\n", i);
227 const TexUnitState &unit = glstate.get_texture_unit(i);
228 IO::print(" GL_TEXTURE_2D: %s\n", unit.describe_binding(GL_TEXTURE_2D));
229 IO::print(" GL_TEXTURE_3D: %s\n", unit.describe_binding(GL_TEXTURE_3D));
231 IO::print(" Buffers:\n");
232 const BufferState *buf = glstate.get_current_buffer(GL_ARRAY_BUFFER);
233 IO::print(" GL_ARRAY_BUFFER: %d\n", (buf ? buf->id : 0));
234 buf = glstate.get_current_buffer(GL_ELEMENT_ARRAY_BUFFER);
235 IO::print(" GL_ELEMENT_ARRAY_BUFFER: %d\n", (buf ? buf->id : 0));
238 throw InvalidParameterValue("Invalid or missing argument");
241 void CommandInterpreter::cmd_texture(const string &args)
245 const map<unsigned, TextureState> &textures = gldbg.get_glstate().get_textures();
246 IO::print("%d texture objects:\n", textures.size());
247 for(map<unsigned, TextureState>::const_iterator i = textures.begin(); i!=textures.end(); ++i)
249 const TextureState &tex = i->second;
250 IO::print(" %d: %s, %d images\n", i->first, tex.describe(), tex.images.size());
255 unsigned id = lexical_cast<unsigned>(args);
256 const TextureState &tex = gldbg.get_glstate().get_texture(id);
257 IO::print("Texture object %d\n", id);
258 IO::print(" Target: %s\n", describe_enum(tex.target, "TextureTarget"));
259 IO::print(" Images:\n");
260 for(unsigned i=0; i<tex.images.size(); ++i)
262 const TexImageState &img = tex.images[i];
263 IO::print(" Level %2d: %s\n", i, img.describe());
265 IO::print(" Min. filter: %s\n", describe_enum(tex.min_filter, "TextureMinFilter"));
266 IO::print(" Mag. filter: %s\n", describe_enum(tex.mag_filter, "TextureMagFilter"));
267 IO::print(" Wrap modes: S=%s / T=%s / R=%s\n", describe_enum(tex.wrap_s, "TextureWrapMode"),
268 describe_enum(tex.wrap_t, "TextureWrapMode"), describe_enum(tex.wrap_r, "TextureWrapMode"));
269 IO::print(" Compare mode: %s\n", describe_enum(tex.compare_mode, ""));
270 IO::print(" Compare func: %s\n", describe_enum(tex.compare_func, "DepthFunction"));
271 IO::print(" Generate mipmap: %s\n", tex.generate_mipmap);
275 void CommandInterpreter::cmd_buffer(const string &args)
279 const GlState::BufferMap &buffers = gldbg.get_glstate().get_buffers();
280 IO::print("%d buffers:\n", buffers.size());
281 for(GlState::BufferMap::const_iterator i=buffers.begin(); i!=buffers.end(); ++i)
282 IO::print(" %d: %s\n", i->first, i->second.describe());
286 unsigned id = lexical_cast<unsigned>(args);
287 const BufferState &buf = gldbg.get_glstate().get_buffer(id);
288 IO::print("Buffer %d:\n", id);
289 IO::print(" Size: %d bytes\n", buf.size);
290 IO::print(" Usage: %s\n", describe_enum(buf.usage, ""));
295 CommandInterpreter::Command::Command():
299 CommandInterpreter::Command::Command(Func f, const string &d):
304 CommandInterpreter::Command::Command(Func f, const string &d, const string &h):