]> git.tdb.fi Git - gldbg.git/blob - source/commandinterpreter.cpp
9660aaa0ec8940487feae23a8c1aa74258d8fd9e
[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 <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"
15 #include "enums.h"
16 #include "gldbg.h"
17 #include "tracer.h"
18
19 using namespace std;
20 using namespace Msp;
21
22 CommandInterpreter::CommandInterpreter(GlDbg &d):
23         gldbg(d)
24 {
25         commands["help"] = Command(&CommandInterpreter::cmd_help,
26                 "Provides help on commands",
27                 "help\n"
28                 "  Displays a list of commands\n\n"
29                 "help COMMAND\n"
30                 "  Gives detailed information on a command\n");
31         commands["exit"] = Command(&CommandInterpreter::cmd_exit,
32                 "Ends the debugging session");
33
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",
42                 "signal NUM\n"
43                 "signal NAME\n"
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"
46                 "  and TERM.\n");
47
48         commands["trace"] = Command(&CommandInterpreter::cmd_trace,
49                 "Traces GL function calls",
50                 "trace\n"
51                 "  Send trace output to stdout.\n"
52                 "trace FILE\n"
53                 "  Send trace output to FILE (- for stdout).\n\n"
54                 "trace {off|on}\n"
55                 "  Temporarily suspend or resume trace without closing the file.\n\n"
56                 "trace end\n"
57                 "  Terminate trace, closing the file.\n");
58
59         commands["state"] = Command(&CommandInterpreter::cmd_state,
60                 "Inspects general GL state",
61                 "state vertex\n"
62                 "  Print current vertex attributes\n\n"
63                 "state bind\n"
64                 "  Show current bindings\n");
65
66         commands["texture"] = Command(&CommandInterpreter::cmd_texture,
67                 "Inspect texture state",
68                 "texture\n"
69                 "  Lists texture objects\n\n"
70                 "texture ID\n"
71                 "  Print information about a texture object\n");
72
73         commands["buffer"] = Command(&CommandInterpreter::cmd_buffer,
74                 "Inspect buffer object state",
75                 "buffer\n"
76                 "  Lists buffer objects\n\n"
77                 "buffer ID\n"
78                 "  Print information about a buffer object\n");
79 }
80
81 void CommandInterpreter::execute(const string &cmd)
82 {
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);
88         if(i->first!=name)
89         {
90                 CommandMap::const_iterator j = i;
91                 if((++j)!=commands.end() && !j->first.compare(0, name.size(), name))
92                         throw KeyError("Ambiguous command", name);
93         }
94
95         string args;
96         if(space!=string::npos)
97                 args = cmd.substr(space+1);
98
99         (this->*(i->second.func))(args);
100 }
101
102 void CommandInterpreter::cmd_help(const string &args)
103 {
104         if(args.empty())
105         {
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);
108         }
109         else
110         {
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);
117         }
118 }
119
120 void CommandInterpreter::cmd_run(const string &)
121 {
122         gldbg.launch();
123 }
124
125 void CommandInterpreter::cmd_continue(const string &)
126 {
127         IO::print("Continuing.\n");
128         gldbg.get_process().resume();
129 }
130
131 void CommandInterpreter::cmd_signal(const string &args)
132 {
133         unsigned sig = 0;
134         if(args=="HUP" || args=="SIGHUP")
135                 sig = SIGHUP;
136         else if(args=="INT" || args=="SIGINT")
137                 sig = SIGINT;
138         else if(args=="ILL" || args=="SIGILL")
139                 sig = SIGILL;
140         else if(args=="SEGV" || args=="SIGSEGV")
141                 sig = SIGSEGV;
142         else if(args=="TERM" || args=="SIGTERM")
143                 sig = SIGTERM;
144         else if(isnumrc(args))
145                 sig = lexical_cast<unsigned>(args);
146         else
147                 throw InvalidParameterValue("Invalid signal specification");
148         gldbg.get_process().resume(sig);
149 }
150
151 void CommandInterpreter::cmd_kill(const string &)
152 {
153         gldbg.get_process().kill();
154 }
155
156 void CommandInterpreter::cmd_exit(const string &)
157 {
158         gldbg.quit();
159 }
160
161 void CommandInterpreter::cmd_trace(const string &args)
162 {
163         Tracer &tracer = gldbg.get_tracer();
164         if(args.empty() || args=="-")
165         {
166                 tracer.set_output(IO::cout);
167                 IO::print("Tracing to stdout\n");
168         }
169         else if(args=="on")
170         {
171                 tracer.enable();
172                 IO::print("Tracing enabled\n");
173         }
174         else if(args=="off")
175         {
176                 tracer.disable();
177                 IO::print("Tracing disabled\n");
178         }
179         else if(args=="end")
180         {
181                 tracer.set_output(0);
182                 IO::print("Tracing terminated\n");
183         }
184         else
185         {
186                 tracer.set_output(new IO::File(args, IO::M_WRITE));
187                 IO::print("Tracing to %s\n", args);
188         }
189 }
190
191 void CommandInterpreter::cmd_state(const string &args)
192 {
193         const GlState &glstate = gldbg.get_glstate();
194         if(args=="vertex")
195         {
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)
200                 {
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);
203                 }
204                 const Vector3 &normal = glstate.get_normal();
205                 IO::print("  Normal:    [%05.3f, %05.3f, %05.3f]\n", normal.x, normal.y, normal.z);
206         }
207         else if(args=="bind")
208         {
209                 IO::print("Current bindings:\n");
210                 for(unsigned i=0; i<8; ++i)
211                 {
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));
216                 }
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));
222         }
223         else
224                 throw InvalidParameterValue("Invalid or missing argument");
225 }
226
227 void CommandInterpreter::cmd_texture(const string &args)
228 {
229         if(args.empty())
230         {
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)
234                 {
235                         const TextureState &tex = i->second;
236                         IO::print("  %d: %s, %d images\n", i->first, tex.describe(), tex.images.size());
237                 }
238         }
239         else
240         {
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)
247                 {
248                         const TexImageState &img = tex.images[i];
249                         IO::print("    Level %2d:      %s\n", i, img.describe());
250                 }
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);
258         }
259 }
260
261 void CommandInterpreter::cmd_buffer(const string &args)
262 {
263         if(args.empty())
264         {
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());
269         }
270         else
271         {
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, ""));
277         }
278 }
279
280
281 CommandInterpreter::Command::Command():
282         func(0)
283 { }
284
285 CommandInterpreter::Command::Command(Func f, const string &d):
286         func(f),
287         description(d)
288 { }
289
290 CommandInterpreter::Command::Command(Func f, const string &d, const string &h):
291         func(f),
292         description(d),
293         help(h)
294 { }