]> git.tdb.fi Git - gldbg.git/blob - source/commandinterpreter.cpp
Prompt to terminate target process in exit command if it's still running
[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["state"] = Command(&CommandInterpreter::cmd_state,
61                 "Inspects general GL state",
62                 "state vertex\n"
63                 "  Print current vertex attributes\n\n"
64                 "state bind\n"
65                 "  Show current bindings\n");
66
67         commands["texture"] = Command(&CommandInterpreter::cmd_texture,
68                 "Inspect texture state",
69                 "texture\n"
70                 "  Lists texture objects\n\n"
71                 "texture ID\n"
72                 "  Print information about a texture object\n");
73
74         commands["buffer"] = Command(&CommandInterpreter::cmd_buffer,
75                 "Inspect buffer object state",
76                 "buffer\n"
77                 "  Lists buffer objects\n\n"
78                 "buffer ID\n"
79                 "  Print information about a buffer object\n");
80 }
81
82 void CommandInterpreter::execute(const string &cmd)
83 {
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);
89         if(i->first!=name)
90         {
91                 CommandMap::const_iterator j = i;
92                 if((++j)!=commands.end() && !j->first.compare(0, name.size(), name))
93                         throw KeyError("Ambiguous command", name);
94         }
95
96         string args;
97         if(space!=string::npos)
98                 args = cmd.substr(space+1);
99
100         (this->*(i->second.func))(args);
101 }
102
103 void CommandInterpreter::cmd_help(const string &args)
104 {
105         if(args.empty())
106         {
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);
109         }
110         else
111         {
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);
118         }
119 }
120
121 void CommandInterpreter::cmd_run(const string &)
122 {
123         gldbg.launch();
124 }
125
126 void CommandInterpreter::cmd_continue(const string &)
127 {
128         IO::print("Continuing.\n");
129         gldbg.get_process().resume();
130 }
131
132 void CommandInterpreter::cmd_signal(const string &args)
133 {
134         unsigned sig = 0;
135         if(args=="HUP" || args=="SIGHUP")
136                 sig = SIGHUP;
137         else if(args=="INT" || args=="SIGINT")
138                 sig = SIGINT;
139         else if(args=="ILL" || args=="SIGILL")
140                 sig = SIGILL;
141         else if(args=="SEGV" || args=="SIGSEGV")
142                 sig = SIGSEGV;
143         else if(args=="TERM" || args=="SIGTERM")
144                 sig = SIGTERM;
145         else if(isnumrc(args))
146                 sig = lexical_cast<unsigned>(args);
147         else
148                 throw InvalidParameterValue("Invalid signal specification");
149         gldbg.get_process().resume(sig);
150 }
151
152 void CommandInterpreter::cmd_kill(const string &)
153 {
154         gldbg.get_process().kill();
155 }
156
157 void CommandInterpreter::cmd_exit(const string &)
158 {
159         if(gldbg.get_process().get_state()!=Process::INACTIVE)
160         {
161                 IO::print("Program is still running.  Kill it?\n");
162                 char *answer = readline("[y/n] ");
163                 if(answer[0]=='y')
164                 {
165                         gldbg.get_process().kill();
166                         gldbg.quit(true);
167                 }
168                 else
169                         IO::print("Not confirmed.\n");
170         }
171         else
172                 gldbg.quit(false);
173 }
174
175 void CommandInterpreter::cmd_trace(const string &args)
176 {
177         Tracer &tracer = gldbg.get_tracer();
178         if(args.empty() || args=="-")
179         {
180                 tracer.set_output(IO::cout);
181                 IO::print("Tracing to stdout\n");
182         }
183         else if(args=="on")
184         {
185                 tracer.enable();
186                 IO::print("Tracing enabled\n");
187         }
188         else if(args=="off")
189         {
190                 tracer.disable();
191                 IO::print("Tracing disabled\n");
192         }
193         else if(args=="end")
194         {
195                 tracer.set_output(0);
196                 IO::print("Tracing terminated\n");
197         }
198         else
199         {
200                 tracer.set_output(new IO::File(args, IO::M_WRITE));
201                 IO::print("Tracing to %s\n", args);
202         }
203 }
204
205 void CommandInterpreter::cmd_state(const string &args)
206 {
207         const GlState &glstate = gldbg.get_glstate();
208         if(args=="vertex")
209         {
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)
214                 {
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);
217                 }
218                 const Vector3 &normal = glstate.get_normal();
219                 IO::print("  Normal:    [%05.3f, %05.3f, %05.3f]\n", normal.x, normal.y, normal.z);
220         }
221         else if(args=="bind")
222         {
223                 IO::print("Current bindings:\n");
224                 for(unsigned i=0; i<8; ++i)
225                 {
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));
230                 }
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));
236         }
237         else
238                 throw InvalidParameterValue("Invalid or missing argument");
239 }
240
241 void CommandInterpreter::cmd_texture(const string &args)
242 {
243         if(args.empty())
244         {
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)
248                 {
249                         const TextureState &tex = i->second;
250                         IO::print("  %d: %s, %d images\n", i->first, tex.describe(), tex.images.size());
251                 }
252         }
253         else
254         {
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)
261                 {
262                         const TexImageState &img = tex.images[i];
263                         IO::print("    Level %2d:      %s\n", i, img.describe());
264                 }
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);
272         }
273 }
274
275 void CommandInterpreter::cmd_buffer(const string &args)
276 {
277         if(args.empty())
278         {
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());
283         }
284         else
285         {
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, ""));
291         }
292 }
293
294
295 CommandInterpreter::Command::Command():
296         func(0)
297 { }
298
299 CommandInterpreter::Command::Command(Func f, const string &d):
300         func(f),
301         description(d)
302 { }
303
304 CommandInterpreter::Command::Command(Func f, const string &d, const string &h):
305         func(f),
306         description(d),
307         help(h)
308 { }