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 "breakpoint.h"
15 #include "commandinterpreter.h"
17 #include "functions.h"
24 CommandInterpreter::CommandInterpreter(GlDbg &d):
27 register_command("help", this, &CommandInterpreter::cmd_help)
28 .set_help("Provides help on commands",
30 " Displays a list of commands\n\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"]);
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",
42 " Makes program execution stop at FUNC\n");
43 register_command("unbreak", this, &CommandInterpreter::cmd_unbreak)
44 .set_help("Removes a breakpoint",
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",
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"
66 void CommandInterpreter::execute(const string &cmd)
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);
75 CommandMap::const_iterator j = i;
76 if((++j)!=commands.end() && !j->first.compare(0, name.size(), name))
77 throw KeyError("Ambiguous command", name);
81 if(space!=string::npos)
82 args = cmd.substr(space+1);
84 i->second->execute(args);
87 void CommandInterpreter::cmd_help(const string &args)
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());
97 CommandMap::const_iterator i = commands.find(args);
99 throw KeyError("Unknown command", args);
101 const Command *cmd = i->second;
102 while(const CommandAlias *alias = dynamic_cast<const CommandAlias *>(cmd))
103 cmd = alias->get_target();
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());
111 void CommandInterpreter::cmd_run(const string &)
116 void CommandInterpreter::cmd_break(const string &args)
118 unsigned short func = get_function(args.c_str());
120 throw InvalidParameterValue("Unknown function");
122 gldbg.set_breakpoint(func, BREAK_CALL);
125 void CommandInterpreter::cmd_unbreak(const string &args)
127 unsigned short func = get_function(args.c_str());
129 throw InvalidParameterValue("Unknown function");
131 gldbg.clear_breakpoint(func, BREAK_CALL);
134 void CommandInterpreter::cmd_next(const string &)
136 gldbg.set_breakpoint(0, BREAK_CALL);
137 gldbg.get_process().resume();
140 void CommandInterpreter::cmd_finish(const string &)
142 gldbg.set_breakpoint(0, BREAK_RETURN);
143 gldbg.get_process().resume();
146 void CommandInterpreter::cmd_continue(const string &)
148 IO::print("Continuing.\n");
149 gldbg.get_process().resume();
152 void CommandInterpreter::cmd_signal(const string &args)
155 if(args=="HUP" || args=="SIGHUP")
157 else if(args=="INT" || args=="SIGINT")
159 else if(args=="ILL" || args=="SIGILL")
161 else if(args=="SEGV" || args=="SIGSEGV")
163 else if(args=="TERM" || args=="SIGTERM")
165 else if(isnumrc(args))
166 sig = lexical_cast<unsigned>(args);
168 throw InvalidParameterValue("Invalid signal specification");
169 gldbg.get_process().resume(sig);
172 void CommandInterpreter::cmd_kill(const string &)
174 gldbg.get_process().kill();
177 void CommandInterpreter::cmd_exit(const string &)
179 if(gldbg.get_process().get_state()!=Process::INACTIVE)
181 IO::print("Program is still running. Kill it?\n");
182 char *answer = readline("[y/n] ");
185 gldbg.get_process().kill();
189 IO::print("Not confirmed.\n");
196 void CommandInterpreter::Command::set_help(const string &d)
201 void CommandInterpreter::Command::set_help(const string &d, const string &h)
208 CommandInterpreter::CommandAlias::CommandAlias(Command *t):
212 void CommandInterpreter::CommandAlias::execute(const string &a)