]> git.tdb.fi Git - gldbg.git/blob - source/commandinterpreter.cpp
2a28f69acd6e06a2dc8c523ef2e58a388a62124d
[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 "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         register_command("help", this, &CommandInterpreter::cmd_help)
26                 .set_help("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         register_command("exit", this, &CommandInterpreter::cmd_exit)
32                 .set_help("Ends the debugging session");
33         commands["quit"] = new CommandAlias(commands["exit"]);
34
35         register_command("run", this, &CommandInterpreter::cmd_run)
36                 .set_help("Starts the program");
37         register_command("continue", this, &CommandInterpreter::cmd_continue)
38                 .set_help("Resumes program execution");
39         register_command("kill", this, &CommandInterpreter::cmd_kill)
40                 .set_help("Terminates the program immediately");
41         register_command("signal", this, &CommandInterpreter::cmd_signal)
42                 .set_help("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 }
50
51 void CommandInterpreter::execute(const string &cmd)
52 {
53         unsigned space = cmd.find(' ');
54         string name = cmd.substr(0, space);
55         CommandMap::const_iterator i = commands.lower_bound(name);
56         if(i==commands.end() || i->first.compare(0, name.size(), name))
57                 throw KeyError("Unknown command", name);
58         if(i->first!=name)
59         {
60                 CommandMap::const_iterator j = i;
61                 if((++j)!=commands.end() && !j->first.compare(0, name.size(), name))
62                         throw KeyError("Ambiguous command", name);
63         }
64
65         string args;
66         if(space!=string::npos)
67                 args = cmd.substr(space+1);
68
69         i->second->execute(args);
70 }
71
72 void CommandInterpreter::cmd_help(const string &args)
73 {
74         if(args.empty())
75         {
76                 for(CommandMap::const_iterator i=commands.begin(); i!=commands.end(); ++i)
77                         if(!dynamic_cast<const CommandAlias *>(i->second))
78                                 IO::print("%-10s : %s\n", i->first, i->second->get_description());
79         }
80         else
81         {
82                 CommandMap::const_iterator i = commands.find(args);
83                 if(i==commands.end())
84                         throw KeyError("Unknown command", args);
85
86                 const Command *cmd = i->second;
87                 while(const CommandAlias *alias = dynamic_cast<const CommandAlias *>(cmd))
88                         cmd = alias->get_target();
89
90                 IO::print("%s : %s\n", i->first, cmd->get_description());
91                 if(!cmd->get_help().empty())
92                         IO::print("\n%s", cmd->get_help());
93         }
94 }
95
96 void CommandInterpreter::cmd_run(const string &)
97 {
98         gldbg.launch();
99 }
100
101 void CommandInterpreter::cmd_continue(const string &)
102 {
103         IO::print("Continuing.\n");
104         gldbg.get_process().resume();
105 }
106
107 void CommandInterpreter::cmd_signal(const string &args)
108 {
109         unsigned sig = 0;
110         if(args=="HUP" || args=="SIGHUP")
111                 sig = SIGHUP;
112         else if(args=="INT" || args=="SIGINT")
113                 sig = SIGINT;
114         else if(args=="ILL" || args=="SIGILL")
115                 sig = SIGILL;
116         else if(args=="SEGV" || args=="SIGSEGV")
117                 sig = SIGSEGV;
118         else if(args=="TERM" || args=="SIGTERM")
119                 sig = SIGTERM;
120         else if(isnumrc(args))
121                 sig = lexical_cast<unsigned>(args);
122         else
123                 throw InvalidParameterValue("Invalid signal specification");
124         gldbg.get_process().resume(sig);
125 }
126
127 void CommandInterpreter::cmd_kill(const string &)
128 {
129         gldbg.get_process().kill();
130 }
131
132 void CommandInterpreter::cmd_exit(const string &)
133 {
134         if(gldbg.get_process().get_state()!=Process::INACTIVE)
135         {
136                 IO::print("Program is still running.  Kill it?\n");
137                 char *answer = readline("[y/n] ");
138                 if(answer[0]=='y')
139                 {
140                         gldbg.get_process().kill();
141                         gldbg.quit(true);
142                 }
143                 else
144                         IO::print("Not confirmed.\n");
145         }
146         else
147                 gldbg.quit(false);
148 }
149
150
151 void CommandInterpreter::Command::set_help(const string &d)
152 {
153         description = d;
154 }
155
156 void CommandInterpreter::Command::set_help(const string &d, const string &h)
157 {
158         description = d;
159         help = h;
160 }
161
162
163 CommandInterpreter::CommandAlias::CommandAlias(Command *t):
164         target(t)
165 { }
166
167 void CommandInterpreter::CommandAlias::execute(const string &a)
168 {
169         target->execute(a);
170 }