]> git.tdb.fi Git - gldbg.git/blob - source/commandinterpreter.cpp
Track vertex array state
[gldbg.git] / source / commandinterpreter.cpp
1 /* $Id$
2
3 This file is part of gldbg
4 Copyright © 2009-2010  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         commands["quit"] = Command(&commands["exit"]);
35
36         commands["run"] = Command(&CommandInterpreter::cmd_run,
37                 "Starts the program");
38         commands["continue"] = Command(&CommandInterpreter::cmd_continue,
39                 "Resumes program execution");
40         commands["kill"] = Command(&CommandInterpreter::cmd_kill,
41                 "Terminates the program immediately");
42         commands["signal"] = Command(&CommandInterpreter::cmd_signal,
43                 "Resumes execution with a signal",
44                 "signal NUM\n"
45                 "signal NAME\n"
46                 "  Sends the signal identified by NUM or NAME to the program and resumes\n"
47                 "  execution.  Currently recognized signal names are HUP, INT, TERM, SEGV\n"
48                 "  and TERM.\n");
49
50         commands["trace"] = Command(&CommandInterpreter::cmd_trace,
51                 "Traces GL function calls",
52                 "trace\n"
53                 "  Send trace output to stdout.\n\n"
54                 "trace FILE\n"
55                 "  Send trace output to FILE (- for stdout).\n\n"
56                 "trace {off|on}\n"
57                 "  Temporarily suspend or resume trace without closing the file.\n\n"
58                 "trace end\n"
59                 "  Terminate trace, closing the file.\n");
60
61         commands["profile"] = Command(&CommandInterpreter::cmd_profile,
62                 "Profiles GL usage and performance",
63                 "profile {on|off}\n"
64                 "  Enables or disables profiling\n");
65
66         commands["state"] = Command(&CommandInterpreter::cmd_state,
67                 "Inspects general GL state",
68                 "state vertex\n"
69                 "  Print current vertex attributes\n\n"
70                 "state bind\n"
71                 "  Show current bindings\n");
72
73         commands["texture"] = Command(&CommandInterpreter::cmd_texture,
74                 "Inspect texture state",
75                 "texture\n"
76                 "  Lists texture objects\n\n"
77                 "texture ID\n"
78                 "  Print information about a texture object\n");
79
80         commands["buffer"] = Command(&CommandInterpreter::cmd_buffer,
81                 "Inspect buffer object state",
82                 "buffer\n"
83                 "  Lists buffer objects\n\n"
84                 "buffer ID\n"
85                 "  Print information about a buffer object\n");
86 }
87
88 void CommandInterpreter::execute(const string &cmd)
89 {
90         unsigned space = cmd.find(' ');
91         string name = cmd.substr(0, space);
92         CommandMap::const_iterator i = commands.lower_bound(name);
93         if(i==commands.end() || i->first.compare(0, name.size(), name))
94                 throw KeyError("Unknown command", name);
95         if(i->first!=name)
96         {
97                 CommandMap::const_iterator j = i;
98                 if((++j)!=commands.end() && !j->first.compare(0, name.size(), name))
99                         throw KeyError("Ambiguous command", name);
100         }
101
102         string args;
103         if(space!=string::npos)
104                 args = cmd.substr(space+1);
105
106         (this->*(i->second.func))(args);
107 }
108
109 void CommandInterpreter::cmd_help(const string &args)
110 {
111         if(args.empty())
112         {
113                 for(map<string, Command>::const_iterator i=commands.begin(); i!=commands.end(); ++i)
114                         if(!i->second.alias_for)
115                                 IO::print("%-10s : %s\n", i->first, i->second.description);
116         }
117         else
118         {
119                 map<string, Command>::const_iterator i = commands.find(args);
120                 if(i==commands.end())
121                         throw KeyError("Unknown command", args);
122
123                 const Command *cmd = &i->second;
124                 while(cmd->alias_for)
125                         cmd = cmd->alias_for;
126
127                 IO::print("%s : %s\n", i->first, cmd->description);
128                 if(!i->second.help.empty())
129                         IO::print("\n%s", cmd->help);
130         }
131 }
132
133 void CommandInterpreter::cmd_run(const string &)
134 {
135         gldbg.launch();
136 }
137
138 void CommandInterpreter::cmd_continue(const string &)
139 {
140         IO::print("Continuing.\n");
141         gldbg.get_process().resume();
142 }
143
144 void CommandInterpreter::cmd_signal(const string &args)
145 {
146         unsigned sig = 0;
147         if(args=="HUP" || args=="SIGHUP")
148                 sig = SIGHUP;
149         else if(args=="INT" || args=="SIGINT")
150                 sig = SIGINT;
151         else if(args=="ILL" || args=="SIGILL")
152                 sig = SIGILL;
153         else if(args=="SEGV" || args=="SIGSEGV")
154                 sig = SIGSEGV;
155         else if(args=="TERM" || args=="SIGTERM")
156                 sig = SIGTERM;
157         else if(isnumrc(args))
158                 sig = lexical_cast<unsigned>(args);
159         else
160                 throw InvalidParameterValue("Invalid signal specification");
161         gldbg.get_process().resume(sig);
162 }
163
164 void CommandInterpreter::cmd_kill(const string &)
165 {
166         gldbg.get_process().kill();
167 }
168
169 void CommandInterpreter::cmd_exit(const string &)
170 {
171         if(gldbg.get_process().get_state()!=Process::INACTIVE)
172         {
173                 IO::print("Program is still running.  Kill it?\n");
174                 char *answer = readline("[y/n] ");
175                 if(answer[0]=='y')
176                 {
177                         gldbg.get_process().kill();
178                         gldbg.quit(true);
179                 }
180                 else
181                         IO::print("Not confirmed.\n");
182         }
183         else
184                 gldbg.quit(false);
185 }
186
187 void CommandInterpreter::cmd_trace(const string &args)
188 {
189         Tracer &tracer = gldbg.get_tracer();
190         if(args.empty() || args=="-")
191         {
192                 tracer.set_output(IO::cout);
193                 IO::print("Tracing to stdout\n");
194         }
195         else if(args=="on")
196         {
197                 tracer.enable();
198                 IO::print("Tracing enabled\n");
199         }
200         else if(args=="off")
201         {
202                 tracer.disable();
203                 IO::print("Tracing disabled\n");
204         }
205         else if(args=="end")
206         {
207                 tracer.set_output(0);
208                 IO::print("Tracing terminated\n");
209         }
210         else
211         {
212                 tracer.set_output(new IO::File(args, IO::M_WRITE));
213                 IO::print("Tracing to %s\n", args);
214         }
215 }
216
217 void CommandInterpreter::cmd_profile(const string &args)
218 {
219         Profiler &profiler = gldbg.get_profiler();
220         if(args.empty() || args=="on")
221                 profiler.enable();
222         else if(args=="off")
223                 profiler.disable();
224         else
225                 throw InvalidParameterValue("Invalid argument");
226 }
227
228 void CommandInterpreter::cmd_state(const string &args)
229 {
230         const GlState &glstate = gldbg.get_glstate();
231         if(args=="vertex")
232         {
233                 IO::print("Current vertex attributes:\n");
234                 const Vector4 &color = glstate.get_color();
235                 IO::print("  Color:     [%05.3f, %05.3f, %05.3f, %05.3f]\n", color.r, color.g, color.b, color.a);
236                 for(unsigned i=0; i<8; ++i)
237                 {
238                         const Vector4 &texcoord = glstate.get_texcoord(i);
239                         IO::print("  TexCoord%d: [%05.3f, %05.3f, %05.3f, %05.3f]\n", i, texcoord.s, texcoord.t, texcoord.p, texcoord.q);
240                 }
241                 const Vector3 &normal = glstate.get_normal();
242                 IO::print("  Normal:    [%05.3f, %05.3f, %05.3f]\n", normal.x, normal.y, normal.z);
243         }
244         else if(args=="bind")
245         {
246                 IO::print("Current bindings:\n");
247                 for(unsigned i=0; i<8; ++i)
248                 {
249                         IO::print("  Texture unit %d:\n", i);
250                         const TexUnitState &unit = glstate.get_texture_unit(i);
251                         IO::print("    GL_TEXTURE_2D: %s\n", unit.describe_binding(GL_TEXTURE_2D));
252                         IO::print("    GL_TEXTURE_3D: %s\n", unit.describe_binding(GL_TEXTURE_3D));
253                 }
254                 IO::print("  Buffers:\n");
255                 const BufferState *buf = glstate.get_current_buffer(GL_ARRAY_BUFFER);
256                 IO::print("    GL_ARRAY_BUFFER:         %d\n", (buf ? buf->id : 0));
257                 buf = glstate.get_current_buffer(GL_ELEMENT_ARRAY_BUFFER);
258                 IO::print("    GL_ELEMENT_ARRAY_BUFFER: %d\n", (buf ? buf->id : 0));
259         }
260         else
261                 throw InvalidParameterValue("Invalid or missing argument");
262 }
263
264 void CommandInterpreter::cmd_texture(const string &args)
265 {
266         if(args.empty())
267         {
268                 const map<unsigned, TextureState> &textures = gldbg.get_glstate().get_textures();
269                 IO::print("%d texture objects:\n", textures.size());
270                 for(map<unsigned, TextureState>::const_iterator i = textures.begin(); i!=textures.end(); ++i)
271                 {
272                         const TextureState &tex = i->second;
273                         IO::print("  %d: %s, %d images\n", i->first, tex.describe(), tex.images.size());
274                 }
275         }
276         else
277         {
278                 unsigned id = lexical_cast<unsigned>(args);
279                 const TextureState &tex = gldbg.get_glstate().get_texture(id);
280                 IO::print("Texture object %d\n", id);
281                 IO::print("  Target:          %s\n", describe_enum(tex.target, "TextureTarget"));
282                 IO::print("  Images:\n");
283                 for(unsigned i=0; i<tex.images.size(); ++i)
284                 {
285                         const TexImageState &img = tex.images[i];
286                         IO::print("    Level %2d:      %s\n", i, img.describe());
287                 }
288                 IO::print("  Min. filter:     %s\n", describe_enum(tex.min_filter, "TextureMinFilter"));
289                 IO::print("  Mag. filter:     %s\n", describe_enum(tex.mag_filter, "TextureMagFilter"));
290                 IO::print("  Wrap modes:      S=%s / T=%s / R=%s\n", describe_enum(tex.wrap_s, "TextureWrapMode"),
291                         describe_enum(tex.wrap_t, "TextureWrapMode"), describe_enum(tex.wrap_r, "TextureWrapMode"));
292                 IO::print("  Compare mode:    %s\n", describe_enum(tex.compare_mode, ""));
293                 IO::print("  Compare func:    %s\n", describe_enum(tex.compare_func, "DepthFunction"));
294                 IO::print("  Generate mipmap: %s\n", tex.generate_mipmap);
295         }
296 }
297
298 void CommandInterpreter::cmd_buffer(const string &args)
299 {
300         if(args.empty())
301         {
302                 const GlState::BufferMap &buffers = gldbg.get_glstate().get_buffers();
303                 IO::print("%d buffers:\n", buffers.size());
304                 for(GlState::BufferMap::const_iterator i=buffers.begin(); i!=buffers.end(); ++i)
305                         IO::print("  %d: %s\n", i->first, i->second.describe());
306         }
307         else
308         {
309                 unsigned id = lexical_cast<unsigned>(args);
310                 const BufferState &buf = gldbg.get_glstate().get_buffer(id);
311                 IO::print("Buffer %d:\n", id);
312                 IO::print("  Size:  %d bytes\n", buf.size);
313                 IO::print("  Usage: %s\n", describe_enum(buf.usage, ""));
314                 if(buf.content.stride)
315                 {
316                         IO::print("  Stride: %d bytes\n", buf.content.stride);
317
318                         IO::print("  Arrays:\n");
319                         const vector<BufferContent::Array> &arrays = buf.content.arrays;
320                         for(vector<BufferContent::Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
321                         {
322                                 if(i->kind)
323                                         IO::print("    %2d: %s, %d %s\n", i->offset,
324                                                 describe_enum(i->kind, ""), i->size, describe_enum(i->type, "DataType"));
325                                 else
326                                         IO::print("    %2d: Attrib %d, %d %s\n", i->offset,
327                                                 i->index, i->size, describe_enum(i->type, "DataType"));
328                         }
329
330                         IO::print("  Data:\n");
331                         string header;
332                         for(vector<BufferContent::Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
333                         {
334                                 if(!header.empty())
335                                         header += " | ";
336
337                                 string label;
338                                 if(i->kind==GL_VERTEX_ARRAY)
339                                         label = "Vertex";
340                                 else if(i->kind==GL_NORMAL_ARRAY)
341                                         label = "Normal";
342                                 else if(i->kind==GL_COLOR_ARRAY)
343                                         label = "Color";
344                                 else if(i->kind==GL_TEXTURE_COORD_ARRAY)
345                                 {
346                                         if(i->size==1)
347                                                 label = "TexC";
348                                         else
349                                                 label = "TexCoord";
350                                 }
351                                 else if(!i->kind)
352                                 {
353                                         if(i->size==1)
354                                                 label = format("A %d", i->index);
355                                         else
356                                                 label = format("Attrib %d", i->index);
357                                 }
358
359                                 unsigned width = i->size;
360                                 if(i->type==GL_FLOAT)
361                                         width *= 5;
362                                 else if(i->type==GL_UNSIGNED_BYTE)
363                                         width *= 3;
364                                 width += i->size-1;
365
366                                 header.append((width-label.size())/2, ' ');
367                                 header += label;
368                                 header.append((width-label.size()+1)/2, ' ');
369                         }
370                         IO::print("     %s\n", header);
371
372                         unsigned n_verts = buf.size/buf.content.stride;
373                         for(unsigned i=0; i<n_verts; ++i)
374                         {
375                                 const char *vertex = buf.data+i*buf.content.stride;
376
377                                 string line;
378                                 for(vector<BufferContent::Array>::const_iterator j=arrays.begin(); j!=arrays.end(); ++j)
379                                 {
380                                         if(!line.empty())
381                                                 line += " |";
382
383                                         const char *base = vertex+j->offset;
384                                         for(unsigned k=0; k<j->size; ++k)
385                                         {
386                                                 if(j->type==GL_FLOAT)
387                                                         line += format(" %5.2f", *(reinterpret_cast<const float *>(base)+k));
388                                                 else if(j->type==GL_UNSIGNED_BYTE)
389                                                         line += format(" %3u", *(reinterpret_cast<const unsigned char *>(base)+k));
390                                         }
391                                 }
392
393                                 IO::print("%3d:%s\n", i, line);
394                         }
395                 }
396         }
397 }
398
399
400 CommandInterpreter::Command::Command():
401         func(0),
402         alias_for(0)
403 { }
404
405 CommandInterpreter::Command::Command(Command *cmd):
406         func(cmd->func),
407         alias_for(cmd)
408 { }
409
410 CommandInterpreter::Command::Command(Func f, const string &d):
411         func(f),
412         description(d),
413         alias_for(0)
414 { }
415
416 CommandInterpreter::Command::Command(Func f, const string &d, const string &h):
417         func(f),
418         description(d),
419         help(h),
420         alias_for(0)
421 { }