]> git.tdb.fi Git - gldbg.git/blob - source/commandinterpreter.cpp
36c3b3c18752e32ed2bb2197683a571fc5308ef4
[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 >FILE\n"
51                 "  Send trace output to FILE.  As a special case, - means stdout.\n\n"
52                 "trace {off|on}\n"
53                 "  Temporarily suspend or resume trace without closing the file.\n\n"
54                 "trace end\n"
55                 "  Terminate trace, closing the file.\n");
56
57         commands["state"] = Command(&CommandInterpreter::cmd_state,
58                 "Inspects general GL state",
59                 "state vertex\n"
60                 "  Print current vertex attributes\n\n"
61                 "state bind\n"
62                 "  Show current bindings\n");
63
64         commands["texture"] = Command(&CommandInterpreter::cmd_texture,
65                 "Inspect texture state",
66                 "texture\n"
67                 "  Lists texture objects\n\n"
68                 "texture ID\n"
69                 "  Print information about a texture object\n");
70
71         commands["buffer"] = Command(&CommandInterpreter::cmd_buffer,
72                 "Inspect buffer object state",
73                 "buffer\n"
74                 "  Lists buffer objects\n\n"
75                 "buffer ID\n"
76                 "  Print information about a buffer object\n");
77 }
78
79 void CommandInterpreter::execute(const string &cmd)
80 {
81         unsigned space = cmd.find(' ');
82         string name = cmd.substr(0, space);
83         CommandMap::const_iterator i = commands.lower_bound(name);
84         if(i==commands.end() || i->first.compare(0, name.size(), name))
85                 throw KeyError("Unknown command", name);
86         if(i->first!=name)
87         {
88                 CommandMap::const_iterator j = i;
89                 if((++j)!=commands.end() && !j->first.compare(0, name.size(), name))
90                         throw KeyError("Ambiguous command", name);
91         }
92
93         string args;
94         if(space!=string::npos)
95                 args = cmd.substr(space+1);
96
97         (this->*(i->second.func))(args);
98 }
99
100 void CommandInterpreter::cmd_help(const string &args)
101 {
102         if(args.empty())
103         {
104                 for(map<string, Command>::const_iterator i=commands.begin(); i!=commands.end(); ++i)
105                         IO::print("%-10s : %s\n", i->first, i->second.description);
106         }
107         else
108         {
109                 map<string, Command>::const_iterator i = commands.find(args);
110                 if(i==commands.end())
111                         throw KeyError("Unknown command", args);
112                 IO::print("%s : %s\n", i->first, i->second.description);
113                 if(!i->second.help.empty())
114                         IO::print("\n%s", i->second.help);
115         }
116 }
117
118 void CommandInterpreter::cmd_run(const string &)
119 {
120         gldbg.launch();
121 }
122
123 void CommandInterpreter::cmd_continue(const string &)
124 {
125         IO::print("Continuing.\n");
126         gldbg.get_process().resume();
127 }
128
129 void CommandInterpreter::cmd_signal(const string &args)
130 {
131         unsigned sig = 0;
132         if(args=="HUP" || args=="SIGHUP")
133                 sig = SIGHUP;
134         else if(args=="INT" || args=="SIGINT")
135                 sig = SIGINT;
136         else if(args=="ILL" || args=="SIGILL")
137                 sig = SIGILL;
138         else if(args=="SEGV" || args=="SIGSEGV")
139                 sig = SIGSEGV;
140         else if(args=="TERM" || args=="SIGTERM")
141                 sig = SIGTERM;
142         else if(isnumrc(args))
143                 sig = lexical_cast<unsigned>(args);
144         else
145                 throw InvalidParameterValue("Invalid signal specification");
146         gldbg.get_process().resume(sig);
147 }
148
149 void CommandInterpreter::cmd_kill(const string &)
150 {
151         gldbg.get_process().kill();
152 }
153
154 void CommandInterpreter::cmd_exit(const string &)
155 {
156         gldbg.quit();
157 }
158
159 void CommandInterpreter::cmd_trace(const string &args)
160 {
161         Tracer &tracer = gldbg.get_tracer();
162         if(args[0]=='>')
163         {
164                 string fn = args.substr(1);
165                 if(fn=="-")
166                 {
167                         tracer.set_output(IO::cout);
168                         IO::print("Tracing to stdout\n");
169                 }
170                 else
171                 {
172                         tracer.set_output(new IO::File(fn, IO::M_WRITE));
173                         IO::print("Tracing to %s\n", fn);
174                 }
175         }
176         else
177         {
178                 if(args=="on")
179                 {
180                         tracer.enable();
181                         IO::print("Tracing enabled\n");
182                 }
183                 else if(args=="off")
184                 {
185                         tracer.disable();
186                         IO::print("Tracing disabled\n");
187                 }
188                 else if(args=="end")
189                 {
190                         tracer.set_output(0);
191                         IO::print("Tracing terminated\n");
192                 }
193         }
194 }
195
196 void CommandInterpreter::cmd_state(const string &args)
197 {
198         const GlState &glstate = gldbg.get_glstate();
199         if(args=="vertex")
200         {
201                 IO::print("Current vertex attributes:\n");
202                 const Vector4 &color = glstate.get_color();
203                 IO::print("  Color:     [%05.3f, %05.3f, %05.3f, %05.3f]\n", color.r, color.g, color.b, color.a);
204                 for(unsigned i=0; i<8; ++i)
205                 {
206                         const Vector4 &texcoord = glstate.get_texcoord(i);
207                         IO::print("  TexCoord%d: [%05.3f, %05.3f, %05.3f, %05.3f]\n", i, texcoord.s, texcoord.t, texcoord.p, texcoord.q);
208                 }
209                 const Vector3 &normal = glstate.get_normal();
210                 IO::print("  Normal:    [%05.3f, %05.3f, %05.3f]\n", normal.x, normal.y, normal.z);
211         }
212         else if(args=="bind")
213         {
214                 IO::print("Current bindings:\n");
215                 for(unsigned i=0; i<8; ++i)
216                 {
217                         IO::print("  Texture unit %d:\n", i);
218                         const TexUnitState &unit = glstate.get_texture_unit(i);
219                         IO::print("    GL_TEXTURE_2D: %s\n", unit.describe_binding(GL_TEXTURE_2D));
220                         IO::print("    GL_TEXTURE_3D: %s\n", unit.describe_binding(GL_TEXTURE_3D));
221                 }
222                 IO::print("  Buffers:\n");
223                 const BufferState *buf = glstate.get_current_buffer(GL_ARRAY_BUFFER);
224                 IO::print("    GL_ARRAY_BUFFER:         %d\n", (buf ? buf->id : 0));
225                 buf = glstate.get_current_buffer(GL_ELEMENT_ARRAY_BUFFER);
226                 IO::print("    GL_ELEMENT_ARRAY_BUFFER: %d\n", (buf ? buf->id : 0));
227         }
228         else
229                 throw InvalidParameterValue("Invalid or missing argument");
230 }
231
232 void CommandInterpreter::cmd_texture(const string &args)
233 {
234         if(args.empty())
235         {
236                 const map<unsigned, TextureState> &textures = gldbg.get_glstate().get_textures();
237                 IO::print("%d texture objects:\n", textures.size());
238                 for(map<unsigned, TextureState>::const_iterator i = textures.begin(); i!=textures.end(); ++i)
239                 {
240                         const TextureState &tex = i->second;
241                         IO::print("  %d: %s, %d images\n", i->first, tex.describe(), tex.images.size());
242                 }
243         }
244         else
245         {
246                 unsigned id = lexical_cast<unsigned>(args);
247                 const TextureState &tex = gldbg.get_glstate().get_texture(id);
248                 IO::print("Texture object %d\n", id);
249                 IO::print("  Target:          %s\n", describe_enum(tex.target, "TextureTarget"));
250                 IO::print("  Images:\n");
251                 for(unsigned i=0; i<tex.images.size(); ++i)
252                 {
253                         const TexImageState &img = tex.images[i];
254                         IO::print("    Level %2d:      %s\n", i, img.describe());
255                 }
256                 IO::print("  Min. filter:     %s\n", describe_enum(tex.min_filter, "TextureMinFilter"));
257                 IO::print("  Mag. filter:     %s\n", describe_enum(tex.mag_filter, "TextureMagFilter"));
258                 IO::print("  Wrap modes:      S=%s / T=%s / R=%s\n", describe_enum(tex.wrap_s, "TextureWrapMode"),
259                         describe_enum(tex.wrap_t, "TextureWrapMode"), describe_enum(tex.wrap_r, "TextureWrapMode"));
260                 IO::print("  Compare mode:    %s\n", describe_enum(tex.compare_mode, ""));
261                 IO::print("  Compare func:    %s\n", describe_enum(tex.compare_func, "DepthFunction"));
262                 IO::print("  Generate mipmap: %s\n", tex.generate_mipmap);
263         }
264 }
265
266 void CommandInterpreter::cmd_buffer(const string &args)
267 {
268         if(args.empty())
269         {
270                 const GlState::BufferMap &buffers = gldbg.get_glstate().get_buffers();
271                 IO::print("%d buffers:\n", buffers.size());
272                 for(GlState::BufferMap::const_iterator i=buffers.begin(); i!=buffers.end(); ++i)
273                         IO::print("  %d: %s\n", i->first, i->second.describe());
274         }
275         else
276         {
277                 unsigned id = lexical_cast<unsigned>(args);
278                 const BufferState &buf = gldbg.get_glstate().get_buffer(id);
279                 IO::print("Buffer %d:\n", id);
280                 IO::print("  Size:  %d bytes\n", buf.size);
281                 IO::print("  Usage: %s\n", describe_enum(buf.usage, ""));
282         }
283 }
284
285
286 CommandInterpreter::Command::Command():
287         func(0)
288 { }
289
290 CommandInterpreter::Command::Command(Func f, const string &d):
291         func(f),
292         description(d)
293 { }
294
295 CommandInterpreter::Command::Command(Func f, const string &d, const string &h):
296         func(f),
297         description(d),
298         help(h)
299 { }