]> git.tdb.fi Git - gldbg.git/blob - source/commandinterpreter.cpp
c684979bae2061b49f9fddd06a8cead8c381e9d6
[gldbg.git] / source / commandinterpreter.cpp
1 /* $Id$
2
3 This file is part of gldbg
4 Copyright © 2009  Mikko Rasa, Mikkosoft Productions
5 Distributed under the GPL
6 */
7
8 #include <signal.h>
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"
16 #include "enums.h"
17 #include "gldbg.h"
18 #include "tracer.h"
19
20 using namespace std;
21 using namespace Msp;
22
23 CommandInterpreter::CommandInterpreter(GlDbg &d):
24         gldbg(d)
25 {
26         commands["help"] = Command(&CommandInterpreter::cmd_help,
27                 "Provides help on commands",
28                 "help\n"
29                 "  Displays a list of commands\n\n"
30                 "help COMMAND\n"
31                 "  Gives detailed information on a command\n");
32         commands["exit"] = Command(&CommandInterpreter::cmd_exit,
33                 "Ends the debugging session");
34
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",
43                 "signal NUM\n"
44                 "signal NAME\n"
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"
47                 "  and TERM.\n");
48
49         commands["trace"] = Command(&CommandInterpreter::cmd_trace,
50                 "Traces GL function calls",
51                 "trace\n"
52                 "  Send trace output to stdout.\n"
53                 "trace FILE\n"
54                 "  Send trace output to FILE (- for stdout).\n\n"
55                 "trace {off|on}\n"
56                 "  Temporarily suspend or resume trace without closing the file.\n\n"
57                 "trace end\n"
58                 "  Terminate trace, closing the file.\n");
59
60         commands["profile"] = Command(&CommandInterpreter::cmd_profile,
61                 "Profiles GL usage and performance",
62                 "profile {on|off}\n"
63                 "  Enables or disables profiling\n");
64
65         commands["state"] = Command(&CommandInterpreter::cmd_state,
66                 "Inspects general GL state",
67                 "state vertex\n"
68                 "  Print current vertex attributes\n\n"
69                 "state bind\n"
70                 "  Show current bindings\n");
71
72         commands["texture"] = Command(&CommandInterpreter::cmd_texture,
73                 "Inspect texture state",
74                 "texture\n"
75                 "  Lists texture objects\n\n"
76                 "texture ID\n"
77                 "  Print information about a texture object\n");
78
79         commands["buffer"] = Command(&CommandInterpreter::cmd_buffer,
80                 "Inspect buffer object state",
81                 "buffer\n"
82                 "  Lists buffer objects\n\n"
83                 "buffer ID\n"
84                 "  Print information about a buffer object\n");
85 }
86
87 void CommandInterpreter::execute(const string &cmd)
88 {
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);
94         if(i->first!=name)
95         {
96                 CommandMap::const_iterator j = i;
97                 if((++j)!=commands.end() && !j->first.compare(0, name.size(), name))
98                         throw KeyError("Ambiguous command", name);
99         }
100
101         string args;
102         if(space!=string::npos)
103                 args = cmd.substr(space+1);
104
105         (this->*(i->second.func))(args);
106 }
107
108 void CommandInterpreter::cmd_help(const string &args)
109 {
110         if(args.empty())
111         {
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);
114         }
115         else
116         {
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);
123         }
124 }
125
126 void CommandInterpreter::cmd_run(const string &)
127 {
128         gldbg.launch();
129 }
130
131 void CommandInterpreter::cmd_continue(const string &)
132 {
133         IO::print("Continuing.\n");
134         gldbg.get_process().resume();
135 }
136
137 void CommandInterpreter::cmd_signal(const string &args)
138 {
139         unsigned sig = 0;
140         if(args=="HUP" || args=="SIGHUP")
141                 sig = SIGHUP;
142         else if(args=="INT" || args=="SIGINT")
143                 sig = SIGINT;
144         else if(args=="ILL" || args=="SIGILL")
145                 sig = SIGILL;
146         else if(args=="SEGV" || args=="SIGSEGV")
147                 sig = SIGSEGV;
148         else if(args=="TERM" || args=="SIGTERM")
149                 sig = SIGTERM;
150         else if(isnumrc(args))
151                 sig = lexical_cast<unsigned>(args);
152         else
153                 throw InvalidParameterValue("Invalid signal specification");
154         gldbg.get_process().resume(sig);
155 }
156
157 void CommandInterpreter::cmd_kill(const string &)
158 {
159         gldbg.get_process().kill();
160 }
161
162 void CommandInterpreter::cmd_exit(const string &)
163 {
164         if(gldbg.get_process().get_state()!=Process::INACTIVE)
165         {
166                 IO::print("Program is still running.  Kill it?\n");
167                 char *answer = readline("[y/n] ");
168                 if(answer[0]=='y')
169                 {
170                         gldbg.get_process().kill();
171                         gldbg.quit(true);
172                 }
173                 else
174                         IO::print("Not confirmed.\n");
175         }
176         else
177                 gldbg.quit(false);
178 }
179
180 void CommandInterpreter::cmd_trace(const string &args)
181 {
182         Tracer &tracer = gldbg.get_tracer();
183         if(args.empty() || args=="-")
184         {
185                 tracer.set_output(IO::cout);
186                 IO::print("Tracing to stdout\n");
187         }
188         else if(args=="on")
189         {
190                 tracer.enable();
191                 IO::print("Tracing enabled\n");
192         }
193         else if(args=="off")
194         {
195                 tracer.disable();
196                 IO::print("Tracing disabled\n");
197         }
198         else if(args=="end")
199         {
200                 tracer.set_output(0);
201                 IO::print("Tracing terminated\n");
202         }
203         else
204         {
205                 tracer.set_output(new IO::File(args, IO::M_WRITE));
206                 IO::print("Tracing to %s\n", args);
207         }
208 }
209
210 void CommandInterpreter::cmd_profile(const string &args)
211 {
212         Profiler &profiler = gldbg.get_profiler();
213         if(args.empty() || args=="on")
214                 profiler.enable();
215         else if(args=="off")
216                 profiler.disable();
217         else
218                 throw InvalidParameterValue("Invalid argument");
219 }
220
221 void CommandInterpreter::cmd_state(const string &args)
222 {
223         const GlState &glstate = gldbg.get_glstate();
224         if(args=="vertex")
225         {
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)
230                 {
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);
233                 }
234                 const Vector3 &normal = glstate.get_normal();
235                 IO::print("  Normal:    [%05.3f, %05.3f, %05.3f]\n", normal.x, normal.y, normal.z);
236         }
237         else if(args=="bind")
238         {
239                 IO::print("Current bindings:\n");
240                 for(unsigned i=0; i<8; ++i)
241                 {
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));
246                 }
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));
252         }
253         else
254                 throw InvalidParameterValue("Invalid or missing argument");
255 }
256
257 void CommandInterpreter::cmd_texture(const string &args)
258 {
259         if(args.empty())
260         {
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)
264                 {
265                         const TextureState &tex = i->second;
266                         IO::print("  %d: %s, %d images\n", i->first, tex.describe(), tex.images.size());
267                 }
268         }
269         else
270         {
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)
277                 {
278                         const TexImageState &img = tex.images[i];
279                         IO::print("    Level %2d:      %s\n", i, img.describe());
280                 }
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);
288         }
289 }
290
291 void CommandInterpreter::cmd_buffer(const string &args)
292 {
293         if(args.empty())
294         {
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());
299         }
300         else
301         {
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, ""));
307         }
308 }
309
310
311 CommandInterpreter::Command::Command():
312         func(0)
313 { }
314
315 CommandInterpreter::Command::Command(Func f, const string &d):
316         func(f),
317         description(d)
318 { }
319
320 CommandInterpreter::Command::Command(Func f, const string &d, const string &h):
321         func(f),
322         description(d),
323         help(h)
324 { }