1 //===-- CLIDebugger.cpp - Command Line Interface to the Debugger ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the main implementation of the Command Line Interface to
13 //===----------------------------------------------------------------------===//
15 #include "CLIDebugger.h"
16 #include "CLICommand.h"
17 #include "llvm/Debugger/SourceFile.h"
18 #include "llvm/ADT/StringExtras.h"
22 /// CLIDebugger constructor - This initializes the debugger to its default
23 /// state, and initializes the command table.
25 CLIDebugger::CLIDebugger()
26 : TheProgramInfo(0), TheRuntimeInfo(0), Prompt("(llvm-db) "), ListSize(10) {
27 // Initialize instance variables
35 //===--------------------------------------------------------------------===//
36 // Program startup and shutdown options
38 addCommand("file", new BuiltinCLICommand(
39 "Use specified file as the program to be debugged",
40 "The debugger looks in the current directory and the program $PATH for the"
41 " specified LLVM program. It then unloads the currently loaded program and"
42 " loads the specified program.\n",
43 &CLIDebugger::fileCommand
));
45 addCommand("create", new BuiltinCLICommand(
46 "Start the program, halting its execution in main",
47 "This command creates an instance of the current program, but stops"
48 "\nexecution immediately.\n",
49 &CLIDebugger::createCommand
));
51 addCommand("kill", new BuiltinCLICommand(
52 "Kills the execution of the current program being debugged", "",
53 &CLIDebugger::killCommand
));
55 addCommand("quit", new BuiltinCLICommand(
56 "Exit the debugger", "",
57 &CLIDebugger::quitCommand
));
59 //===--------------------------------------------------------------------===//
60 // Program execution commands
62 addCommand("run", C
= new BuiltinCLICommand(
63 "Start the program running from the beginning", "",
64 &CLIDebugger::runCommand
));
67 addCommand("cont", C
= new BuiltinCLICommand(
68 "Continue program being debugged until the next stop point", "",
69 &CLIDebugger::contCommand
));
70 addCommand("c", C
); addCommand("fg", C
);
72 addCommand("step", C
= new BuiltinCLICommand(
73 "Step program until it reaches a new source line", "",
74 &CLIDebugger::stepCommand
));
77 addCommand("next", C
= new BuiltinCLICommand(
78 "Step program until it reaches a new source line, stepping over calls", "",
79 &CLIDebugger::nextCommand
));
82 addCommand("finish", new BuiltinCLICommand(
83 "Execute until the selected stack frame returns",
84 "Upon return, the value returned is printed and put in the value history.\n",
85 &CLIDebugger::finishCommand
));
87 //===--------------------------------------------------------------------===//
88 // Stack frame commands
90 addCommand("backtrace", C
= new BuiltinCLICommand(
91 "Print backtrace of all stack frames, or innermost COUNT frames",
92 "FIXME: describe. Takes 'n', '-n' or 'full'\n",
93 &CLIDebugger::backtraceCommand
));
96 addCommand("up", new BuiltinCLICommand(
97 "Select and print stack frame that called this one",
98 "An argument says how many frames up to go.\n",
99 &CLIDebugger::upCommand
));
101 addCommand("down", new BuiltinCLICommand(
102 "Select and print stack frame called by this one",
103 "An argument says how many frames down go.\n",
104 &CLIDebugger::downCommand
));
106 addCommand("frame", C
= new BuiltinCLICommand(
107 "Select and print a stack frame",
108 "With no argument, print the selected stack frame. (See also 'info frame').\n"
109 "An argument specifies the frame to select.\n",
110 &CLIDebugger::frameCommand
));
113 //===--------------------------------------------------------------------===//
114 // Breakpoint related commands
116 addCommand("break", C
= new BuiltinCLICommand(
117 "Set breakpoint at specified line or function",
118 "FIXME: describe.\n",
119 &CLIDebugger::breakCommand
));
123 //===--------------------------------------------------------------------===//
124 // Miscellaneous commands
126 addCommand("info", new BuiltinCLICommand(
127 "Generic command for showing things about the program being debugged",
128 "info functions: display information about functions in the program.\ninfo"
129 " source : display information about the current source file.\ninfo source"
130 "s : Display source file names for the program\ninfo target : print status"
131 " of inferior process\n",
132 &CLIDebugger::infoCommand
));
134 addCommand("list", C
= new BuiltinCLICommand(
135 "List specified function or line",
137 &CLIDebugger::listCommand
));
140 addCommand("set", new BuiltinCLICommand(
141 "Change program or debugger variable",
143 &CLIDebugger::setCommand
));
145 addCommand("show", new BuiltinCLICommand(
146 "Generic command for showing things about the debugger",
148 &CLIDebugger::showCommand
));
150 addCommand("help", C
= new BuiltinCLICommand(
151 "Prints information about available commands", "",
152 &CLIDebugger::helpCommand
));
157 /// addCommand - Add a command to the CommandTable, potentially displacing a
158 /// preexisting command.
159 void CLIDebugger::addCommand(const std::string
&Option
, CLICommand
*Cmd
) {
160 assert(Cmd
&& "Cannot set a null command!");
161 CLICommand
*&CS
= CommandTable
[Option
];
162 if (CS
== Cmd
) return; // noop
164 // If we already have a command, decrement the command's reference count.
166 CS
->removeOptionName(Option
);
171 // Remember that we are using this command.
173 Cmd
->addOptionName(Option
);
176 static bool isValidPrefix(const std::string
&Prefix
, const std::string
&Option
){
177 return Prefix
.size() <= Option
.size() &&
178 Prefix
== std::string(Option
.begin(), Option
.begin()+Prefix
.size());
181 /// getCommand - This looks up the specified command using a fuzzy match.
182 /// If the string exactly matches a command or is an unambiguous prefix of a
183 /// command, it returns the command. Otherwise it throws an exception
184 /// indicating the possible ambiguous choices.
185 CLICommand
*CLIDebugger::getCommand(const std::string
&Command
) {
187 // Look up the command in the table.
188 std::map
<std::string
, CLICommand
*>::iterator CI
=
189 CommandTable
.lower_bound(Command
);
192 throw "Null command should not get here!";
193 } else if (CI
== CommandTable
.end() ||
194 !isValidPrefix(Command
, CI
->first
)) {
195 // If this command has no relation to anything in the command table,
196 // print the error message.
197 throw "Unknown command: '" + Command
+
198 "'. Use 'help' for list of commands.";
199 } else if (CI
->first
== Command
) {
200 // We have an exact match on the command
203 // Otherwise, we have a prefix match. Check to see if this is
204 // unambiguous, and if so, run it.
205 std::map
<std::string
, CLICommand
*>::iterator CI2
= CI
;
207 // If the next command is a valid completion of this one, we are
209 if (++CI2
!= CommandTable
.end() && isValidPrefix(Command
, CI2
->first
)) {
210 std::string ErrorMsg
=
211 "Ambiguous command '" + Command
+ "'. Options: " + CI
->first
;
212 for (++CI
; CI
!= CommandTable
.end() &&
213 isValidPrefix(Command
, CI
->first
); ++CI
)
214 ErrorMsg
+= ", " + CI
->first
;
217 // It's an unambiguous prefix of a command, use it.
224 /// run - Start the debugger, returning when the user exits the debugger. This
225 /// starts the main event loop of the CLI debugger.
227 int CLIDebugger::run() {
231 // Keep track of the last command issued, so that we can reissue it if the
232 // user hits enter as the command.
233 CLICommand
*LastCommand
= 0;
234 std::string LastArgs
;
236 // Continue reading commands until the end of file.
237 while (getline(std::cin
, Command
)) {
238 std::string Arguments
= Command
;
240 // Split off the command from the arguments to the command.
241 Command
= getToken(Arguments
, " \t\n\v\f\r\\/;.*&");
244 CLICommand
*CurCommand
;
247 CurCommand
= LastCommand
;
248 Arguments
= LastArgs
;
250 CurCommand
= getCommand(Command
);
253 // Save the command we are running in case the user wants us to repeat it
255 LastCommand
= CurCommand
;
256 LastArgs
= Arguments
;
258 // Finally, execute the command.
260 CurCommand
->runCommand(*this, Arguments
);
262 } catch (int RetVal
) {
263 // The quit command exits the command loop by throwing an integer return
266 } catch (const std::string
&Error
) {
267 std::cout
<< "Error: " << Error
<< "\n";
268 } catch (const char *Error
) {
269 std::cout
<< "Error: " << Error
<< "\n";
270 } catch (const NonErrorException
&E
) {
271 std::cout
<< E
.getMessage() << "\n";
273 std::cout
<< "ERROR: Debugger caught unexpected exception!\n";
274 // Attempt to continue.
277 // Write the prompt to get the next bit of user input
285 /// askYesNo - Ask the user a question, and demand a yes/no response. If
286 /// the user says yes, return true.
288 bool CLIDebugger::askYesNo(const std::string
&Message
) const {
290 std::cout
<< Message
<< " (y or n) " << std::flush
;
291 while (getline(std::cin
, Answer
)) {
292 std::string Val
= getToken(Answer
);
293 if (getToken(Answer
).empty()) {
294 if (Val
== "yes" || Val
== "y" || Val
== "YES" || Val
== "Y" ||
297 if (Val
== "no" || Val
== "n" || Val
== "NO" || Val
== "N" ||
302 std::cout
<< "Please answer y or n.\n" << Message
<< " (y or n) "