3 This file is part of gldbg
4 Copyright © 2009-2010 Mikko Rasa, Mikkosoft Productions
5 Distributed under the GPL
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"
22 CommandInterpreter::CommandInterpreter(GlDbg &d):
25 register_command("help", this, &CommandInterpreter::cmd_help)
26 .set_help("Provides help on commands",
28 " Displays a list of commands\n\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"]);
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",
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"
51 void CommandInterpreter::execute(const string &cmd)
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);
60 CommandMap::const_iterator j = i;
61 if((++j)!=commands.end() && !j->first.compare(0, name.size(), name))
62 throw KeyError("Ambiguous command", name);
66 if(space!=string::npos)
67 args = cmd.substr(space+1);
69 i->second->execute(args);
72 void CommandInterpreter::cmd_help(const string &args)
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());
82 CommandMap::const_iterator i = commands.find(args);
84 throw KeyError("Unknown command", args);
86 const Command *cmd = i->second;
87 while(const CommandAlias *alias = dynamic_cast<const CommandAlias *>(cmd))
88 cmd = alias->get_target();
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());
96 void CommandInterpreter::cmd_run(const string &)
101 void CommandInterpreter::cmd_continue(const string &)
103 IO::print("Continuing.\n");
104 gldbg.get_process().resume();
107 void CommandInterpreter::cmd_signal(const string &args)
110 if(args=="HUP" || args=="SIGHUP")
112 else if(args=="INT" || args=="SIGINT")
114 else if(args=="ILL" || args=="SIGILL")
116 else if(args=="SEGV" || args=="SIGSEGV")
118 else if(args=="TERM" || args=="SIGTERM")
120 else if(isnumrc(args))
121 sig = lexical_cast<unsigned>(args);
123 throw InvalidParameterValue("Invalid signal specification");
124 gldbg.get_process().resume(sig);
127 void CommandInterpreter::cmd_kill(const string &)
129 gldbg.get_process().kill();
132 void CommandInterpreter::cmd_exit(const string &)
134 if(gldbg.get_process().get_state()!=Process::INACTIVE)
136 IO::print("Program is still running. Kill it?\n");
137 char *answer = readline("[y/n] ");
140 gldbg.get_process().kill();
144 IO::print("Not confirmed.\n");
151 void CommandInterpreter::Command::set_help(const string &d)
156 void CommandInterpreter::Command::set_help(const string &d, const string &h)
163 CommandInterpreter::CommandAlias::CommandAlias(Command *t):
167 void CommandInterpreter::CommandAlias::execute(const string &a)