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 void CommandInterpreter::execute(const string &cmd)
58 unsigned space = cmd.find(' ');
59 string name = cmd.substr(0, space);
60 CommandMap::const_iterator i = commands.lower_bound(name);
61 if(i==commands.end() || i->first.compare(0, name.size(), name))
62 throw runtime_error("Unknown command "+name);
65 CommandMap::const_iterator j = i;
66 if((++j)!=commands.end() && !j->first.compare(0, name.size(), name))
67 throw runtime_error("Ambiguous command "+name);
71 if(space!=string::npos)
72 args = cmd.substr(space+1);
74 i->second->execute(args);
77 void CommandInterpreter::cmd_help(const string &args)
81 for(CommandMap::const_iterator i=commands.begin(); i!=commands.end(); ++i)
82 if(!dynamic_cast<const CommandAlias *>(i->second))
83 printf("%-10s : %s\n", i->first.c_str(), i->second->get_description().c_str());
87 CommandMap::const_iterator i = commands.find(args);
89 throw runtime_error("Unknown command "+args);
91 const Command *cmd = i->second;
92 while(const CommandAlias *alias = dynamic_cast<const CommandAlias *>(cmd))
93 cmd = alias->get_target();
95 printf("%s : %s\n", i->first.c_str(), cmd->get_description().c_str());
96 if(!cmd->get_help().empty())
97 printf("\n%s", cmd->get_help().c_str());
101 void CommandInterpreter::cmd_run(const string &)
106 void CommandInterpreter::cmd_break(const string &args)
108 unsigned short func = get_function(args.c_str());
110 throw runtime_error("Unknown function");
112 gldbg.set_breakpoint(func, BREAK_CALL, 0);
115 void CommandInterpreter::cmd_unbreak(const string &args)
117 unsigned short func = get_function(args.c_str());
119 throw runtime_error("Unknown function");
121 gldbg.clear_breakpoint(func, BREAK_CALL, 0);
124 void CommandInterpreter::cmd_next(const string &)
126 gldbg.set_breakpoint(0, BREAK_CALL, 0);
127 gldbg.get_process().resume();
130 void CommandInterpreter::cmd_finish(const string &)
132 gldbg.set_breakpoint(0, BREAK_RETURN, 0);
133 gldbg.get_process().resume();
136 void CommandInterpreter::cmd_continue(const string &)
138 printf("Continuing.\n");
139 gldbg.get_process().resume();
142 void CommandInterpreter::cmd_signal(const string &args)
145 if(args=="HUP" || args=="SIGHUP")
147 else if(args=="INT" || args=="SIGINT")
149 else if(args=="ILL" || args=="SIGILL")
151 else if(args=="SEGV" || args=="SIGSEGV")
153 else if(args=="TERM" || args=="SIGTERM")
158 sig = strtoul(args.c_str(), &end, 0);
160 throw runtime_error("Invalid signal specification");
162 gldbg.get_process().resume(sig);
165 void CommandInterpreter::cmd_kill(const string &)
167 gldbg.get_process().kill();
170 void CommandInterpreter::cmd_exit(const string &)
172 if(gldbg.get_process().get_state()!=Process::INACTIVE)
174 printf("Program is still running. Kill it?\n");
175 char *answer = readline("[y/n] ");
178 gldbg.get_process().kill();
182 printf("Not confirmed.\n");
189 void CommandInterpreter::Command::set_help(const string &d)
194 void CommandInterpreter::Command::set_help(const string &d, const string &h)
201 CommandInterpreter::CommandAlias::CommandAlias(Command *t):
205 void CommandInterpreter::CommandAlias::execute(const string &a)