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