]> git.tdb.fi Git - gldbg.git/blob - source/commandinterpreter.cpp
Enable bidirectional communication between gldbg and glwrap.so
[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/print.h>
12 #include <msp/strings/lexicalcast.h>
13 #include <msp/strings/utils.h>
14 #include "breakpoint.h"
15 #include "commandinterpreter.h"
16 #include "enums.h"
17 #include "functions.h"
18 #include "gldbg.h"
19 #include "tracer.h"
20
21 using namespace std;
22 using namespace Msp;
23
24 CommandInterpreter::CommandInterpreter(GlDbg &d):
25         gldbg(d)
26 {
27         register_command("help", this, &CommandInterpreter::cmd_help)
28                 .set_help("Provides help on commands",
29                         "help\n"
30                         "  Displays a list of commands\n\n"
31                         "help COMMAND\n"
32                         "  Gives detailed information on a command\n");
33         register_command("exit", this, &CommandInterpreter::cmd_exit)
34                 .set_help("Ends the debugging session");
35         commands["quit"] = new CommandAlias(commands["exit"]);
36
37         register_command("run", this, &CommandInterpreter::cmd_run)
38                 .set_help("Starts the program");
39         register_command("break", this, &CommandInterpreter::cmd_break)
40                 .set_help("Sets a breakpoint",
41                         "break FUNC\n"
42                         "  Makes program execution stop at FUNC\n");
43         register_command("unbreak", this, &CommandInterpreter::cmd_unbreak)
44                 .set_help("Removes a breakpoint",
45                         "unbreak FUNC\n"
46                         "  Makes program execution no longer stop at FUNC\n");
47         register_command("next", this, &CommandInterpreter::cmd_next)
48                 .set_help("Resumes program execution until the next function call");
49         commands["step"] = new CommandAlias(commands["next"]);
50         register_command("finish", this, &CommandInterpreter::cmd_finish)
51                 .set_help("Resumes program execution until the next function return");
52         register_command("continue", this, &CommandInterpreter::cmd_continue)
53                 .set_help("Resumes program execution");
54         register_command("kill", this, &CommandInterpreter::cmd_kill)
55                 .set_help("Terminates the program immediately");
56         register_command("signal", this, &CommandInterpreter::cmd_signal)
57                 .set_help("Resumes execution with a signal",
58                         "signal NUM\n"
59                         "signal NAME\n"
60                         "  Sends the signal identified by NUM or NAME to the program and resumes\n"
61                         "  execution.  Currently recognized signal names are HUP, INT, TERM, SEGV\n"
62                         "  and TERM.\n");
63
64 }
65
66 void CommandInterpreter::execute(const string &cmd)
67 {
68         unsigned space = cmd.find(' ');
69         string name = cmd.substr(0, space);
70         CommandMap::const_iterator i = commands.lower_bound(name);
71         if(i==commands.end() || i->first.compare(0, name.size(), name))
72                 throw KeyError("Unknown command", name);
73         if(i->first!=name)
74         {
75                 CommandMap::const_iterator j = i;
76                 if((++j)!=commands.end() && !j->first.compare(0, name.size(), name))
77                         throw KeyError("Ambiguous command", name);
78         }
79
80         string args;
81         if(space!=string::npos)
82                 args = cmd.substr(space+1);
83
84         i->second->execute(args);
85 }
86
87 void CommandInterpreter::cmd_help(const string &args)
88 {
89         if(args.empty())
90         {
91                 for(CommandMap::const_iterator i=commands.begin(); i!=commands.end(); ++i)
92                         if(!dynamic_cast<const CommandAlias *>(i->second))
93                                 IO::print("%-10s : %s\n", i->first, i->second->get_description());
94         }
95         else
96         {
97                 CommandMap::const_iterator i = commands.find(args);
98                 if(i==commands.end())
99                         throw KeyError("Unknown command", args);
100
101                 const Command *cmd = i->second;
102                 while(const CommandAlias *alias = dynamic_cast<const CommandAlias *>(cmd))
103                         cmd = alias->get_target();
104
105                 IO::print("%s : %s\n", i->first, cmd->get_description());
106                 if(!cmd->get_help().empty())
107                         IO::print("\n%s", cmd->get_help());
108         }
109 }
110
111 void CommandInterpreter::cmd_run(const string &)
112 {
113         gldbg.launch();
114 }
115
116 void CommandInterpreter::cmd_break(const string &args)
117 {
118         unsigned short func = get_function(args.c_str());
119         if(!func)
120                 throw InvalidParameterValue("Unknown function");
121
122         gldbg.set_breakpoint(func, BREAK_CALL);
123 }
124
125 void CommandInterpreter::cmd_unbreak(const string &args)
126 {
127         unsigned short func = get_function(args.c_str());
128         if(!func)
129                 throw InvalidParameterValue("Unknown function");
130
131         gldbg.clear_breakpoint(func, BREAK_CALL);
132 }
133
134 void CommandInterpreter::cmd_next(const string &)
135 {
136         gldbg.set_breakpoint(0, BREAK_CALL);
137         gldbg.get_process().resume();
138 }
139
140 void CommandInterpreter::cmd_finish(const string &)
141 {
142         gldbg.set_breakpoint(0, BREAK_RETURN);
143         gldbg.get_process().resume();
144 }
145
146 void CommandInterpreter::cmd_continue(const string &)
147 {
148         IO::print("Continuing.\n");
149         gldbg.get_process().resume();
150 }
151
152 void CommandInterpreter::cmd_signal(const string &args)
153 {
154         unsigned sig = 0;
155         if(args=="HUP" || args=="SIGHUP")
156                 sig = SIGHUP;
157         else if(args=="INT" || args=="SIGINT")
158                 sig = SIGINT;
159         else if(args=="ILL" || args=="SIGILL")
160                 sig = SIGILL;
161         else if(args=="SEGV" || args=="SIGSEGV")
162                 sig = SIGSEGV;
163         else if(args=="TERM" || args=="SIGTERM")
164                 sig = SIGTERM;
165         else if(isnumrc(args))
166                 sig = lexical_cast<unsigned>(args);
167         else
168                 throw InvalidParameterValue("Invalid signal specification");
169         gldbg.get_process().resume(sig);
170 }
171
172 void CommandInterpreter::cmd_kill(const string &)
173 {
174         gldbg.get_process().kill();
175 }
176
177 void CommandInterpreter::cmd_exit(const string &)
178 {
179         if(gldbg.get_process().get_state()!=Process::INACTIVE)
180         {
181                 IO::print("Program is still running.  Kill it?\n");
182                 char *answer = readline("[y/n] ");
183                 if(answer[0]=='y')
184                 {
185                         gldbg.get_process().kill();
186                         gldbg.quit(true);
187                 }
188                 else
189                         IO::print("Not confirmed.\n");
190         }
191         else
192                 gldbg.quit(false);
193 }
194
195
196 void CommandInterpreter::Command::set_help(const string &d)
197 {
198         description = d;
199 }
200
201 void CommandInterpreter::Command::set_help(const string &d, const string &h)
202 {
203         description = d;
204         help = h;
205 }
206
207
208 CommandInterpreter::CommandAlias::CommandAlias(Command *t):
209         target(t)
210 { }
211
212 void CommandInterpreter::CommandAlias::execute(const string &a)
213 {
214         target->execute(a);
215 }