3 This file is part of gldbg
4 Copyright © 2009-2011 Mikko Rasa, Mikkosoft Productions
5 Distributed under the GPL
11 #include <readline/readline.h>
12 #include "breakpoint.h"
13 #include "commandinterpreter.h"
15 #include "functions.h"
21 CommandInterpreter::CommandInterpreter(GlDbg &d):
24 register_command("help", this, &CommandInterpreter::cmd_help)
25 .set_help("Provides help on commands",
27 " Displays a list of commands\n\n"
29 " Gives detailed information on a command\n");
30 register_command("exit", this, &CommandInterpreter::cmd_exit)
31 .set_help("Ends the debugging session");
32 commands["quit"] = new CommandAlias(commands["exit"]);
34 register_command("run", this, &CommandInterpreter::cmd_run)
35 .set_help("Starts the program");
36 register_command("break", this, &CommandInterpreter::cmd_break)
37 .set_help("Sets a breakpoint",
39 " Makes program execution stop at FUNC\n");
40 register_command("unbreak", this, &CommandInterpreter::cmd_unbreak)
41 .set_help("Removes a breakpoint",
43 " Makes program execution no longer stop at FUNC\n");
44 register_command("next", this, &CommandInterpreter::cmd_next)
45 .set_help("Resumes program execution until the next function call");
46 commands["step"] = new CommandAlias(commands["next"]);
47 register_command("finish", this, &CommandInterpreter::cmd_finish)
48 .set_help("Resumes program execution until the next function return");
49 register_command("continue", this, &CommandInterpreter::cmd_continue)
50 .set_help("Resumes program execution");
51 register_command("kill", this, &CommandInterpreter::cmd_kill)
52 .set_help("Terminates the program immediately");
53 register_command("signal", this, &CommandInterpreter::cmd_signal)
54 .set_help("Resumes execution with a signal",
57 " Sends the signal identified by NUM or NAME to the program and resumes\n"
58 " execution. Currently recognized signal names are HUP, INT, TERM, SEGV\n"
63 void CommandInterpreter::execute(const string &cmd)
65 unsigned space = cmd.find(' ');
66 string name = cmd.substr(0, space);
67 CommandMap::const_iterator i = commands.lower_bound(name);
68 if(i==commands.end() || i->first.compare(0, name.size(), name))
69 throw runtime_error("Unknown command "+name);
72 CommandMap::const_iterator j = i;
73 if((++j)!=commands.end() && !j->first.compare(0, name.size(), name))
74 throw runtime_error("Ambiguous command "+name);
78 if(space!=string::npos)
79 args = cmd.substr(space+1);
81 i->second->execute(args);
84 void CommandInterpreter::cmd_help(const string &args)
88 for(CommandMap::const_iterator i=commands.begin(); i!=commands.end(); ++i)
89 if(!dynamic_cast<const CommandAlias *>(i->second))
90 printf("%-10s : %s\n", i->first.c_str(), i->second->get_description().c_str());
94 CommandMap::const_iterator i = commands.find(args);
96 throw runtime_error("Unknown command "+args);
98 const Command *cmd = i->second;
99 while(const CommandAlias *alias = dynamic_cast<const CommandAlias *>(cmd))
100 cmd = alias->get_target();
102 printf("%s : %s\n", i->first.c_str(), cmd->get_description().c_str());
103 if(!cmd->get_help().empty())
104 printf("\n%s", cmd->get_help().c_str());
108 void CommandInterpreter::cmd_run(const string &)
113 void CommandInterpreter::cmd_break(const string &args)
115 unsigned short func = get_function(args.c_str());
117 throw runtime_error("Unknown function");
119 gldbg.set_breakpoint(func, BREAK_CALL, 0);
122 void CommandInterpreter::cmd_unbreak(const string &args)
124 unsigned short func = get_function(args.c_str());
126 throw runtime_error("Unknown function");
128 gldbg.clear_breakpoint(func, BREAK_CALL, 0);
131 void CommandInterpreter::cmd_next(const string &)
133 gldbg.set_breakpoint(0, BREAK_CALL, 0);
134 gldbg.get_process().resume();
137 void CommandInterpreter::cmd_finish(const string &)
139 gldbg.set_breakpoint(0, BREAK_RETURN, 0);
140 gldbg.get_process().resume();
143 void CommandInterpreter::cmd_continue(const string &)
145 printf("Continuing.\n");
146 gldbg.get_process().resume();
149 void CommandInterpreter::cmd_signal(const string &args)
152 if(args=="HUP" || args=="SIGHUP")
154 else if(args=="INT" || args=="SIGINT")
156 else if(args=="ILL" || args=="SIGILL")
158 else if(args=="SEGV" || args=="SIGSEGV")
160 else if(args=="TERM" || args=="SIGTERM")
165 sig = strtoul(args.c_str(), &end, 0);
167 throw runtime_error("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 printf("Program is still running. Kill it?\n");
182 char *answer = readline("[y/n] ");
185 gldbg.get_process().kill();
189 printf("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)