kill off the last use of TRI::AsmName.
[llvm/avr.git] / tools / llvm-db / CLIDebugger.cpp
blob31476f7d0b79b26b13e71aeef8770df1d0d536a3
1 //===-- CLIDebugger.cpp - Command Line Interface to the Debugger ----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the main implementation of the Command Line Interface to
11 // the debugger.
13 //===----------------------------------------------------------------------===//
15 #include "CLIDebugger.h"
16 #include "CLICommand.h"
17 #include "llvm/Debugger/SourceFile.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include <iostream>
20 using namespace llvm;
22 /// CLIDebugger constructor - This initializes the debugger to its default
23 /// state, and initializes the command table.
24 ///
25 CLIDebugger::CLIDebugger(LLVMContext& ctxt)
26 : Context(ctxt), TheProgramInfo(0), TheRuntimeInfo(0),
27 Prompt("(llvm-db) "), ListSize(10) {
28 // Initialize instance variables
29 CurrentFile = 0;
30 LineListedStart = 1;
31 LineListedEnd = 1;
32 LastCurrentFrame = 0;
33 CurrentLanguage = 0;
35 CLICommand *C;
36 //===--------------------------------------------------------------------===//
37 // Program startup and shutdown options
39 addCommand("file", new BuiltinCLICommand(
40 "Use specified file as the program to be debugged",
41 "The debugger looks in the current directory and the program $PATH for the"
42 " specified LLVM program. It then unloads the currently loaded program and"
43 " loads the specified program.\n",
44 &CLIDebugger::fileCommand));
46 addCommand("create", new BuiltinCLICommand(
47 "Start the program, halting its execution in main",
48 "This command creates an instance of the current program, but stops"
49 "\nexecution immediately.\n",
50 &CLIDebugger::createCommand));
52 addCommand("kill", new BuiltinCLICommand(
53 "Kills the execution of the current program being debugged", "",
54 &CLIDebugger::killCommand));
56 addCommand("quit", new BuiltinCLICommand(
57 "Exit the debugger", "",
58 &CLIDebugger::quitCommand));
60 //===--------------------------------------------------------------------===//
61 // Program execution commands
63 addCommand("run", C = new BuiltinCLICommand(
64 "Start the program running from the beginning", "",
65 &CLIDebugger::runCommand));
66 addCommand("r", C);
68 addCommand("cont", C = new BuiltinCLICommand(
69 "Continue program being debugged until the next stop point", "",
70 &CLIDebugger::contCommand));
71 addCommand("c", C); addCommand("fg", C);
73 addCommand("step", C = new BuiltinCLICommand(
74 "Step program until it reaches a new source line", "",
75 &CLIDebugger::stepCommand));
76 addCommand("s", C);
78 addCommand("next", C = new BuiltinCLICommand(
79 "Step program until it reaches a new source line, stepping over calls", "",
80 &CLIDebugger::nextCommand));
81 addCommand("n", C);
83 addCommand("finish", new BuiltinCLICommand(
84 "Execute until the selected stack frame returns",
85 "Upon return, the value returned is printed and put in the value history.\n",
86 &CLIDebugger::finishCommand));
88 //===--------------------------------------------------------------------===//
89 // Stack frame commands
91 addCommand("backtrace", C = new BuiltinCLICommand(
92 "Print backtrace of all stack frames, or innermost COUNT frames",
93 "FIXME: describe. Takes 'n', '-n' or 'full'\n",
94 &CLIDebugger::backtraceCommand));
95 addCommand("bt", C);
97 addCommand("up", new BuiltinCLICommand(
98 "Select and print stack frame that called this one",
99 "An argument says how many frames up to go.\n",
100 &CLIDebugger::upCommand));
102 addCommand("down", new BuiltinCLICommand(
103 "Select and print stack frame called by this one",
104 "An argument says how many frames down go.\n",
105 &CLIDebugger::downCommand));
107 addCommand("frame", C = new BuiltinCLICommand(
108 "Select and print a stack frame",
109 "With no argument, print the selected stack frame. (See also 'info frame').\n"
110 "An argument specifies the frame to select.\n",
111 &CLIDebugger::frameCommand));
112 addCommand("f", C);
114 //===--------------------------------------------------------------------===//
115 // Breakpoint related commands
117 addCommand("break", C = new BuiltinCLICommand(
118 "Set breakpoint at specified line or function",
119 "FIXME: describe.\n",
120 &CLIDebugger::breakCommand));
121 addCommand("b", C);
124 //===--------------------------------------------------------------------===//
125 // Miscellaneous commands
127 addCommand("info", new BuiltinCLICommand(
128 "Generic command for showing things about the program being debugged",
129 "info functions: display information about functions in the program.\ninfo"
130 " source : display information about the current source file.\ninfo source"
131 "s : Display source file names for the program\ninfo target : print status"
132 " of inferior process\n",
133 &CLIDebugger::infoCommand));
135 addCommand("list", C = new BuiltinCLICommand(
136 "List specified function or line",
137 "FIXME: document\n",
138 &CLIDebugger::listCommand));
139 addCommand("l", C);
141 addCommand("set", new BuiltinCLICommand(
142 "Change program or debugger variable",
143 "FIXME: document\n",
144 &CLIDebugger::setCommand));
146 addCommand("show", new BuiltinCLICommand(
147 "Generic command for showing things about the debugger",
148 "FIXME: document\n",
149 &CLIDebugger::showCommand));
151 addCommand("help", C = new BuiltinCLICommand(
152 "Prints information about available commands", "",
153 &CLIDebugger::helpCommand));
154 addCommand("h", C);
158 /// addCommand - Add a command to the CommandTable, potentially displacing a
159 /// preexisting command.
160 void CLIDebugger::addCommand(const std::string &Option, CLICommand *Cmd) {
161 assert(Cmd && "Cannot set a null command!");
162 CLICommand *&CS = CommandTable[Option];
163 if (CS == Cmd) return; // noop
165 // If we already have a command, decrement the command's reference count.
166 if (CS) {
167 CS->removeOptionName(Option);
168 CS->dropRef();
170 CS = Cmd;
172 // Remember that we are using this command.
173 Cmd->addRef();
174 Cmd->addOptionName(Option);
177 static bool isValidPrefix(const std::string &Prefix, const std::string &Option){
178 return Prefix.size() <= Option.size() &&
179 Prefix == std::string(Option.begin(), Option.begin()+Prefix.size());
182 /// getCommand - This looks up the specified command using a fuzzy match.
183 /// If the string exactly matches a command or is an unambiguous prefix of a
184 /// command, it returns the command. Otherwise it throws an exception
185 /// indicating the possible ambiguous choices.
186 CLICommand *CLIDebugger::getCommand(const std::string &Command) {
188 // Look up the command in the table.
189 std::map<std::string, CLICommand*>::iterator CI =
190 CommandTable.lower_bound(Command);
192 if (Command == "") {
193 throw "Null command should not get here!";
194 } else if (CI == CommandTable.end() ||
195 !isValidPrefix(Command, CI->first)) {
196 // If this command has no relation to anything in the command table,
197 // print the error message.
198 throw "Unknown command: '" + Command +
199 "'. Use 'help' for list of commands.";
200 } else if (CI->first == Command) {
201 // We have an exact match on the command
202 return CI->second;
203 } else {
204 // Otherwise, we have a prefix match. Check to see if this is
205 // unambiguous, and if so, run it.
206 std::map<std::string, CLICommand*>::iterator CI2 = CI;
208 // If the next command is a valid completion of this one, we are
209 // ambiguous.
210 if (++CI2 != CommandTable.end() && isValidPrefix(Command, CI2->first)) {
211 std::string ErrorMsg =
212 "Ambiguous command '" + Command + "'. Options: " + CI->first;
213 for (++CI; CI != CommandTable.end() &&
214 isValidPrefix(Command, CI->first); ++CI)
215 ErrorMsg += ", " + CI->first;
216 throw ErrorMsg;
217 } else {
218 // It's an unambiguous prefix of a command, use it.
219 return CI->second;
225 /// run - Start the debugger, returning when the user exits the debugger. This
226 /// starts the main event loop of the CLI debugger.
228 int CLIDebugger::run() {
229 std::string Command;
230 std::cout << Prompt;
232 // Keep track of the last command issued, so that we can reissue it if the
233 // user hits enter as the command.
234 CLICommand *LastCommand = 0;
235 std::string LastArgs;
237 // Continue reading commands until the end of file.
238 while (getline(std::cin, Command)) {
239 std::string Arguments = Command;
241 // Split off the command from the arguments to the command.
242 Command = getToken(Arguments, " \t\n\v\f\r\\/;.*&");
244 try {
245 CLICommand *CurCommand;
247 if (Command == "") {
248 CurCommand = LastCommand;
249 Arguments = LastArgs;
250 } else {
251 CurCommand = getCommand(Command);
254 // Save the command we are running in case the user wants us to repeat it
255 // next time.
256 LastCommand = CurCommand;
257 LastArgs = Arguments;
259 // Finally, execute the command.
260 if (CurCommand)
261 CurCommand->runCommand(*this, Arguments);
263 } catch (int RetVal) {
264 // The quit command exits the command loop by throwing an integer return
265 // code.
266 return RetVal;
267 } catch (const std::string &Error) {
268 std::cout << "Error: " << Error << "\n";
269 } catch (const char *Error) {
270 std::cout << "Error: " << Error << "\n";
271 } catch (const NonErrorException &E) {
272 std::cout << E.getMessage() << "\n";
273 } catch (...) {
274 std::cout << "ERROR: Debugger caught unexpected exception!\n";
275 // Attempt to continue.
278 // Write the prompt to get the next bit of user input
279 std::cout << Prompt;
282 return 0;
286 /// askYesNo - Ask the user a question, and demand a yes/no response. If
287 /// the user says yes, return true.
289 bool CLIDebugger::askYesNo(const std::string &Message) const {
290 std::string Answer;
291 std::cout << Message << " (y or n) " << std::flush;
292 while (getline(std::cin, Answer)) {
293 std::string Val = getToken(Answer);
294 if (getToken(Answer).empty()) {
295 if (Val == "yes" || Val == "y" || Val == "YES" || Val == "Y" ||
296 Val == "Yes")
297 return true;
298 if (Val == "no" || Val == "n" || Val == "NO" || Val == "N" ||
299 Val == "No")
300 return false;
303 std::cout << "Please answer y or n.\n" << Message << " (y or n) "
304 << std::flush;
307 // Ran out of input?
308 return false;