]> git.tdb.fi Git - gldbg.git/blob - source/commandinterpreter.cpp
Fix several problems reported by valgrind
[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 CommandInterpreter::~CommandInterpreter()
57 {
58         for(CommandMap::iterator i=commands.begin(); i!=commands.end(); ++i)
59                 delete i->second;
60 }
61
62 void CommandInterpreter::execute(const string &cmd)
63 {
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);
69         if(i->first!=name)
70         {
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);
74         }
75
76         string args;
77         if(space!=string::npos)
78                 args = cmd.substr(space+1);
79
80         i->second->execute(args);
81 }
82
83 void CommandInterpreter::cmd_help(const string &args)
84 {
85         if(args.empty())
86         {
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());
90         }
91         else
92         {
93                 CommandMap::const_iterator i = commands.find(args);
94                 if(i==commands.end())
95                         throw runtime_error("Unknown command "+args);
96
97                 const Command *cmd = i->second;
98                 while(const CommandAlias *alias = dynamic_cast<const CommandAlias *>(cmd))
99                         cmd = alias->get_target();
100
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());
104         }
105 }
106
107 void CommandInterpreter::cmd_run(const string &)
108 {
109         gldbg.launch();
110 }
111
112 void CommandInterpreter::cmd_break(const string &args)
113 {
114         unsigned short func = get_function(args.c_str());
115         if(!func)
116                 throw runtime_error("Unknown function");
117
118         gldbg.set_breakpoint(func, BREAK_CALL, 0);
119 }
120
121 void CommandInterpreter::cmd_unbreak(const string &args)
122 {
123         unsigned short func = get_function(args.c_str());
124         if(!func)
125                 throw runtime_error("Unknown function");
126
127         gldbg.clear_breakpoint(func, BREAK_CALL, 0);
128 }
129
130 void CommandInterpreter::cmd_next(const string &)
131 {
132         gldbg.set_breakpoint(0, BREAK_CALL, 0);
133         gldbg.get_process().resume();
134 }
135
136 void CommandInterpreter::cmd_finish(const string &)
137 {
138         gldbg.set_breakpoint(0, BREAK_RETURN, 0);
139         gldbg.get_process().resume();
140 }
141
142 void CommandInterpreter::cmd_continue(const string &)
143 {
144         printf("Continuing.\n");
145         gldbg.get_process().resume();
146 }
147
148 void CommandInterpreter::cmd_signal(const string &args)
149 {
150         unsigned sig = 0;
151         if(args=="HUP" || args=="SIGHUP")
152                 sig = SIGHUP;
153         else if(args=="INT" || args=="SIGINT")
154                 sig = SIGINT;
155         else if(args=="ILL" || args=="SIGILL")
156                 sig = SIGILL;
157         else if(args=="SEGV" || args=="SIGSEGV")
158                 sig = SIGSEGV;
159         else if(args=="TERM" || args=="SIGTERM")
160                 sig = SIGTERM;
161         else
162         {
163                 char *end;
164                 sig = strtoul(args.c_str(), &end, 0);
165                 if(end && *end)
166                         throw runtime_error("Invalid signal specification");
167         }
168         gldbg.get_process().resume(sig);
169 }
170
171 void CommandInterpreter::cmd_kill(const string &)
172 {
173         gldbg.get_process().kill();
174 }
175
176 void CommandInterpreter::cmd_exit(const string &)
177 {
178         if(gldbg.get_process().get_state()!=Process::INACTIVE)
179         {
180                 printf("Program is still running.  Kill it?\n");
181                 char *answer = readline("[y/n] ");
182                 if(answer[0]=='y')
183                 {
184                         gldbg.get_process().kill();
185                         gldbg.quit(true);
186                 }
187                 else
188                         printf("Not confirmed.\n");
189         }
190         else
191                 gldbg.quit(false);
192 }
193
194
195 void CommandInterpreter::Command::set_help(const string &d)
196 {
197         description = d;
198 }
199
200 void CommandInterpreter::Command::set_help(const string &d, const string &h)
201 {
202         description = d;
203         help = h;
204 }
205
206
207 CommandInterpreter::CommandAlias::CommandAlias(Command *t):
208         target(t)
209 { }
210
211 void CommandInterpreter::CommandAlias::execute(const string &a)
212 {
213         target->execute(a);
214 }