4 #include <readline/readline.h>
5 #include "breakpoint.h"
6 #include "commandinterpreter.h"
14 CommandInterpreter::CommandInterpreter(GlDbg &d):
17 register_command("help", this, &CommandInterpreter::cmd_help)
18 .set_help("Provides help on commands",
20 " Displays a list of commands\n\n"
22 " Gives detailed information on a command\n");
23 register_command("exit", this, &CommandInterpreter::cmd_exit)
24 .set_help("Ends the debugging session");
25 commands["quit"] = new CommandAlias(commands["exit"]);
27 register_command("run", this, &CommandInterpreter::cmd_run)
28 .set_help("Starts the program");
29 register_command("break", this, &CommandInterpreter::cmd_break)
30 .set_help("Sets a breakpoint",
32 " Makes program execution stop at FUNC\n");
33 register_command("unbreak", this, &CommandInterpreter::cmd_unbreak)
34 .set_help("Removes a breakpoint",
36 " Makes program execution no longer stop at FUNC\n");
37 register_command("next", this, &CommandInterpreter::cmd_next)
38 .set_help("Resumes program execution until the next function call");
39 commands["step"] = new CommandAlias(commands["next"]);
40 register_command("finish", this, &CommandInterpreter::cmd_finish)
41 .set_help("Resumes program execution until the next function return");
42 register_command("continue", this, &CommandInterpreter::cmd_continue)
43 .set_help("Resumes program execution");
44 register_command("kill", this, &CommandInterpreter::cmd_kill)
45 .set_help("Terminates the program immediately");
46 register_command("signal", this, &CommandInterpreter::cmd_signal)
47 .set_help("Resumes execution with a signal",
50 " Sends the signal identified by NUM or NAME to the program and resumes\n"
51 " execution. Currently recognized signal names are HUP, INT, TERM, SEGV\n"
56 CommandInterpreter::~CommandInterpreter()
58 for(CommandMap::iterator i=commands.begin(); i!=commands.end(); ++i)
62 void CommandInterpreter::execute(const string &cmd)
64 unsigned space = cmd.find(' ');
65 string name = cmd.substr(0, space);
66 CommandMap::const_iterator i = commands.lower_bound(name);
67 if(i==commands.end() || i->first.compare(0, name.size(), name))
68 throw runtime_error("Unknown command "+name);
71 CommandMap::const_iterator j = i;
72 if((++j)!=commands.end() && !j->first.compare(0, name.size(), name))
73 throw runtime_error("Ambiguous command "+name);
77 if(space!=string::npos)
78 args = cmd.substr(space+1);
80 i->second->execute(args);
83 void CommandInterpreter::cmd_help(const string &args)
87 for(CommandMap::const_iterator i=commands.begin(); i!=commands.end(); ++i)
88 if(!dynamic_cast<const CommandAlias *>(i->second))
89 printf("%-10s : %s\n", i->first.c_str(), i->second->get_description().c_str());
93 CommandMap::const_iterator i = commands.find(args);
95 throw runtime_error("Unknown command "+args);
97 const Command *cmd = i->second;
98 while(const CommandAlias *alias = dynamic_cast<const CommandAlias *>(cmd))
99 cmd = alias->get_target();
101 printf("%s : %s\n", i->first.c_str(), cmd->get_description().c_str());
102 if(!cmd->get_help().empty())
103 printf("\n%s", cmd->get_help().c_str());
107 void CommandInterpreter::cmd_run(const string &)
112 void CommandInterpreter::cmd_break(const string &args)
114 unsigned short func = get_function(args.c_str());
116 throw runtime_error("Unknown function");
118 gldbg.set_breakpoint(func, BREAK_CALL, 0);
121 void CommandInterpreter::cmd_unbreak(const string &args)
123 unsigned short func = get_function(args.c_str());
125 throw runtime_error("Unknown function");
127 gldbg.clear_breakpoint(func, BREAK_CALL, 0);
130 void CommandInterpreter::cmd_next(const string &)
132 gldbg.set_breakpoint(0, BREAK_CALL, 0);
133 gldbg.get_process().resume();
136 void CommandInterpreter::cmd_finish(const string &)
138 gldbg.set_breakpoint(0, BREAK_RETURN, 0);
139 gldbg.get_process().resume();
142 void CommandInterpreter::cmd_continue(const string &)
144 printf("Continuing.\n");
145 gldbg.get_process().resume();
148 void CommandInterpreter::cmd_signal(const string &args)
151 if(args=="HUP" || args=="SIGHUP")
153 else if(args=="INT" || args=="SIGINT")
155 else if(args=="ILL" || args=="SIGILL")
157 else if(args=="SEGV" || args=="SIGSEGV")
159 else if(args=="TERM" || args=="SIGTERM")
164 sig = strtoul(args.c_str(), &end, 0);
166 throw runtime_error("Invalid signal specification");
168 gldbg.get_process().resume(sig);
171 void CommandInterpreter::cmd_kill(const string &)
173 gldbg.get_process().kill();
176 void CommandInterpreter::cmd_exit(const string &)
178 if(gldbg.get_process().get_state()!=Process::INACTIVE)
180 printf("Program is still running. Kill it?\n");
181 char *answer = readline("[y/n] ");
184 gldbg.get_process().kill();
188 printf("Not confirmed.\n");
195 void CommandInterpreter::Command::set_help(const string &d)
200 void CommandInterpreter::Command::set_help(const string &d, const string &h)
207 CommandInterpreter::CommandAlias::CommandAlias(Command *t):
211 void CommandInterpreter::CommandAlias::execute(const string &a)