]> git.tdb.fi Git - gldbg.git/blob - source/commandinterpreter.cpp
301535048bd6e8bcfe6084344b7aef248860ec65
[gldbg.git] / source / commandinterpreter.cpp
1 /* $Id$
2
3 This file is part of gldbg
4 Copyright © 2009-2011  Mikko Rasa, Mikkosoft Productions
5 Distributed under the GPL
6 */
7
8 #include <stdexcept>
9 #include <cstdlib>
10 #include <signal.h>
11 #include <readline/readline.h>
12 #include "breakpoint.h"
13 #include "commandinterpreter.h"
14 #include "enums.h"
15 #include "functions.h"
16 #include "gldbg.h"
17 #include "tracer.h"
18
19 using namespace std;
20
21 CommandInterpreter::CommandInterpreter(GlDbg &d):
22         gldbg(d)
23 {
24         register_command("help", this, &CommandInterpreter::cmd_help)
25                 .set_help("Provides help on commands",
26                         "help\n"
27                         "  Displays a list of commands\n\n"
28                         "help COMMAND\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"]);
33
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",
38                         "break FUNC\n"
39                         "  Makes program execution stop at FUNC\n");
40         register_command("unbreak", this, &CommandInterpreter::cmd_unbreak)
41                 .set_help("Removes a breakpoint",
42                         "unbreak FUNC\n"
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",
55                         "signal NUM\n"
56                         "signal NAME\n"
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"
59                         "  and TERM.\n");
60
61 }
62
63 void CommandInterpreter::execute(const string &cmd)
64 {
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);
70         if(i->first!=name)
71         {
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);
75         }
76
77         string args;
78         if(space!=string::npos)
79                 args = cmd.substr(space+1);
80
81         i->second->execute(args);
82 }
83
84 void CommandInterpreter::cmd_help(const string &args)
85 {
86         if(args.empty())
87         {
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());
91         }
92         else
93         {
94                 CommandMap::const_iterator i = commands.find(args);
95                 if(i==commands.end())
96                         throw runtime_error("Unknown command "+args);
97
98                 const Command *cmd = i->second;
99                 while(const CommandAlias *alias = dynamic_cast<const CommandAlias *>(cmd))
100                         cmd = alias->get_target();
101
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());
105         }
106 }
107
108 void CommandInterpreter::cmd_run(const string &)
109 {
110         gldbg.launch();
111 }
112
113 void CommandInterpreter::cmd_break(const string &args)
114 {
115         unsigned short func = get_function(args.c_str());
116         if(!func)
117                 throw runtime_error("Unknown function");
118
119         gldbg.set_breakpoint(func, BREAK_CALL, 0);
120 }
121
122 void CommandInterpreter::cmd_unbreak(const string &args)
123 {
124         unsigned short func = get_function(args.c_str());
125         if(!func)
126                 throw runtime_error("Unknown function");
127
128         gldbg.clear_breakpoint(func, BREAK_CALL, 0);
129 }
130
131 void CommandInterpreter::cmd_next(const string &)
132 {
133         gldbg.set_breakpoint(0, BREAK_CALL, 0);
134         gldbg.get_process().resume();
135 }
136
137 void CommandInterpreter::cmd_finish(const string &)
138 {
139         gldbg.set_breakpoint(0, BREAK_RETURN, 0);
140         gldbg.get_process().resume();
141 }
142
143 void CommandInterpreter::cmd_continue(const string &)
144 {
145         printf("Continuing.\n");
146         gldbg.get_process().resume();
147 }
148
149 void CommandInterpreter::cmd_signal(const string &args)
150 {
151         unsigned sig = 0;
152         if(args=="HUP" || args=="SIGHUP")
153                 sig = SIGHUP;
154         else if(args=="INT" || args=="SIGINT")
155                 sig = SIGINT;
156         else if(args=="ILL" || args=="SIGILL")
157                 sig = SIGILL;
158         else if(args=="SEGV" || args=="SIGSEGV")
159                 sig = SIGSEGV;
160         else if(args=="TERM" || args=="SIGTERM")
161                 sig = SIGTERM;
162         else
163         {
164                 char *end;
165                 sig = strtoul(args.c_str(), &end, 0);
166                 if(end && *end)
167                         throw runtime_error("Invalid signal specification");
168         }
169         gldbg.get_process().resume(sig);
170 }
171
172 void CommandInterpreter::cmd_kill(const string &)
173 {
174         gldbg.get_process().kill();
175 }
176
177 void CommandInterpreter::cmd_exit(const string &)
178 {
179         if(gldbg.get_process().get_state()!=Process::INACTIVE)
180         {
181                 printf("Program is still running.  Kill it?\n");
182                 char *answer = readline("[y/n] ");
183                 if(answer[0]=='y')
184                 {
185                         gldbg.get_process().kill();
186                         gldbg.quit(true);
187                 }
188                 else
189                         printf("Not confirmed.\n");
190         }
191         else
192                 gldbg.quit(false);
193 }
194
195
196 void CommandInterpreter::Command::set_help(const string &d)
197 {
198         description = d;
199 }
200
201 void CommandInterpreter::Command::set_help(const string &d, const string &h)
202 {
203         description = d;
204         help = h;
205 }
206
207
208 CommandInterpreter::CommandAlias::CommandAlias(Command *t):
209         target(t)
210 { }
211
212 void CommandInterpreter::CommandAlias::execute(const string &a)
213 {
214         target->execute(a);
215 }