Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 1 | //===-- Commands.cpp - Implement various commands for the CLI -------------===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 21c62da | 2007-12-29 20:44:31 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 10 | // This file implements many builtin user commands. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CLIDebugger.h" |
| 15 | #include "CLICommand.h" |
| 16 | #include "llvm/Debugger/ProgramInfo.h" |
| 17 | #include "llvm/Debugger/RuntimeInfo.h" |
| 18 | #include "llvm/Debugger/SourceLanguage.h" |
| 19 | #include "llvm/Debugger/SourceFile.h" |
| 20 | #include "llvm/Debugger/InferiorProcess.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 21 | #include "llvm/Support/FileUtilities.h" |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
Anton Korobeynikov | a27694d | 2008-02-20 11:27:04 +0000 | [diff] [blame] | 24 | #include <cstdlib> |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
| 27 | /// getCurrentLanguage - Return the current source language that the user is |
| 28 | /// playing around with. This is aquired from the current stack frame of a |
| 29 | /// running program if one exists, but this value can be explicitly set by the |
| 30 | /// user as well. |
| 31 | const SourceLanguage &CLIDebugger::getCurrentLanguage() const { |
| 32 | // If the user explicitly switched languages with 'set language', use what |
| 33 | // they asked for. |
| 34 | if (CurrentLanguage) { |
| 35 | return *CurrentLanguage; |
| 36 | } else if (Dbg.isProgramRunning()) { |
| 37 | // Otherwise, if the program is running, infer the current language from it. |
| 38 | const GlobalVariable *FuncDesc = |
| 39 | getRuntimeInfo().getCurrentFrame().getFunctionDesc(); |
| 40 | return getProgramInfo().getFunction(FuncDesc).getSourceFile().getLanguage(); |
| 41 | } else { |
| 42 | // Otherwise, default to C like GDB apparently does. |
| 43 | return SourceLanguage::getCFamilyInstance(); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /// startProgramRunning - If the program has been updated, reload it, then |
| 48 | /// start executing the program. |
| 49 | void CLIDebugger::startProgramRunning() { |
| 50 | eliminateRunInfo(); |
| 51 | |
| 52 | // If the program has been modified, reload it! |
Reid Spencer | 7b57fe3 | 2007-04-08 20:06:05 +0000 | [diff] [blame] | 53 | sys::PathWithStatus Program(Dbg.getProgramPath()); |
Chris Lattner | 252ad03 | 2006-07-28 22:03:44 +0000 | [diff] [blame] | 54 | std::string Err; |
Reid Spencer | 7b57fe3 | 2007-04-08 20:06:05 +0000 | [diff] [blame] | 55 | const sys::FileStatus *Status = Program.getFileStatus(false, &Err); |
Reid Spencer | 8475ec0 | 2007-03-29 19:05:44 +0000 | [diff] [blame] | 56 | if (!Status) |
Chris Lattner | 252ad03 | 2006-07-28 22:03:44 +0000 | [diff] [blame] | 57 | throw Err; |
Reid Spencer | 8475ec0 | 2007-03-29 19:05:44 +0000 | [diff] [blame] | 58 | if (TheProgramInfo->getProgramTimeStamp() != Status->getTimestamp()) { |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 59 | outs() << "'" << Program.str() << "' has changed; re-reading program.\n"; |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 60 | |
| 61 | // Unload an existing program. This kills the program if necessary. |
| 62 | Dbg.unloadProgram(); |
| 63 | delete TheProgramInfo; |
| 64 | TheProgramInfo = 0; |
| 65 | CurrentFile = 0; |
| 66 | |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 67 | Dbg.loadProgram(Program.str(), Context); |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 68 | TheProgramInfo = new ProgramInfo(Dbg.getProgram()); |
| 69 | } |
| 70 | |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 71 | outs() << "Starting program: " << Dbg.getProgramPath() << "\n"; |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 72 | Dbg.createProgram(); |
| 73 | |
| 74 | // There was no current frame. |
| 75 | LastCurrentFrame = 0; |
| 76 | } |
| 77 | |
| 78 | /// printSourceLine - Print the specified line of the current source file. |
| 79 | /// If the specified line is invalid (the source file could not be loaded or |
| 80 | /// the line number is out of range), don't print anything, but return true. |
| 81 | bool CLIDebugger::printSourceLine(unsigned LineNo) { |
| 82 | assert(CurrentFile && "There is no current source file to print!"); |
| 83 | const char *LineStart, *LineEnd; |
| 84 | CurrentFile->getSourceLine(LineNo-1, LineStart, LineEnd); |
| 85 | if (LineStart == 0) return true; |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 86 | outs() << LineNo; |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 87 | |
| 88 | // If this is the line the program is currently stopped at, print a marker. |
| 89 | if (Dbg.isProgramRunning()) { |
| 90 | unsigned CurLineNo, CurColNo; |
| 91 | const SourceFileInfo *CurSFI; |
| 92 | getRuntimeInfo().getCurrentFrame().getSourceLocation(CurLineNo, CurColNo, |
| 93 | CurSFI); |
| 94 | |
| 95 | if (CurLineNo == LineNo && CurrentFile == &CurSFI->getSourceText()) |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 96 | outs() << " ->"; |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 99 | outs() << "\t" << std::string(LineStart, LineEnd) << "\n"; |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 100 | return false; |
| 101 | } |
| 102 | |
| 103 | /// printProgramLocation - Print a line of the place where the current stack |
| 104 | /// frame has stopped and the source line it is on. |
| 105 | /// |
| 106 | void CLIDebugger::printProgramLocation(bool PrintLocation) { |
| 107 | assert(Dbg.isProgramLoaded() && Dbg.isProgramRunning() && |
| 108 | "Error program is not loaded and running!"); |
| 109 | |
| 110 | // Figure out where the program stopped... |
| 111 | StackFrame &SF = getRuntimeInfo().getCurrentFrame(); |
| 112 | unsigned LineNo, ColNo; |
| 113 | const SourceFileInfo *FileDesc; |
| 114 | SF.getSourceLocation(LineNo, ColNo, FileDesc); |
| 115 | |
| 116 | // If requested, print out some program information about WHERE we are. |
| 117 | if (PrintLocation) { |
| 118 | // FIXME: print the current function arguments |
| 119 | if (const GlobalVariable *FuncDesc = SF.getFunctionDesc()) |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 120 | outs() << getProgramInfo().getFunction(FuncDesc).getSymbolicName(); |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 121 | else |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 122 | outs() << "<unknown function>"; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 123 | |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 124 | CurrentFile = &FileDesc->getSourceText(); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 125 | |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 126 | outs() << " at " << CurrentFile->getFilename() << ":" << LineNo; |
| 127 | if (ColNo) outs() << ":" << ColNo; |
| 128 | outs() << "\n"; |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | if (printSourceLine(LineNo)) |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 132 | outs() << "<could not load source file>\n"; |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 133 | else { |
| 134 | LineListedStart = LineNo-ListSize/2+1; |
| 135 | if ((int)LineListedStart < 1) LineListedStart = 1; |
| 136 | LineListedEnd = LineListedStart+1; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /// eliminateRunInfo - We are about to run the program. Forget any state |
| 141 | /// about how the program used to be stopped. |
| 142 | void CLIDebugger::eliminateRunInfo() { |
| 143 | delete TheRuntimeInfo; |
| 144 | TheRuntimeInfo = 0; |
| 145 | } |
| 146 | |
| 147 | /// programStoppedSuccessfully - This method updates internal data |
| 148 | /// structures to reflect the fact that the program just executed a while, |
| 149 | /// and has successfully stopped. |
| 150 | void CLIDebugger::programStoppedSuccessfully() { |
| 151 | assert(TheRuntimeInfo==0 && "Someone forgot to release the old RuntimeInfo!"); |
| 152 | |
| 153 | TheRuntimeInfo = new RuntimeInfo(TheProgramInfo, Dbg.getRunningProcess()); |
| 154 | |
| 155 | // FIXME: if there are any breakpoints at the current location, print them as |
| 156 | // well. |
| 157 | |
| 158 | // Since the program as successfully stopped, print its location. |
| 159 | void *CurrentFrame = getRuntimeInfo().getCurrentFrame().getFrameID(); |
| 160 | printProgramLocation(CurrentFrame != LastCurrentFrame); |
| 161 | LastCurrentFrame = CurrentFrame; |
| 162 | } |
| 163 | |
| 164 | |
| 165 | |
| 166 | /// getUnsignedIntegerOption - Get an unsigned integer number from the Val |
| 167 | /// string. Check to make sure that the string contains an unsigned integer |
| 168 | /// token, and if not, throw an exception. If isOnlyOption is set, also throw |
| 169 | /// an exception if there is extra junk at the end of the string. |
| 170 | static unsigned getUnsignedIntegerOption(const char *Msg, std::string &Val, |
| 171 | bool isOnlyOption = true) { |
| 172 | std::string Tok = getToken(Val); |
| 173 | if (Tok.empty() || (isOnlyOption && !getToken(Val).empty())) |
| 174 | throw std::string(Msg) + " expects an unsigned integer argument."; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 175 | |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 176 | char *EndPtr; |
| 177 | unsigned Result = strtoul(Tok.c_str(), &EndPtr, 0); |
| 178 | if (EndPtr != Tok.c_str()+Tok.size()) |
| 179 | throw std::string(Msg) + " expects an unsigned integer argument."; |
| 180 | |
| 181 | return Result; |
| 182 | } |
| 183 | |
| 184 | /// getOptionalUnsignedIntegerOption - This method is just like |
| 185 | /// getUnsignedIntegerOption, but if the argument value is not specified, a |
| 186 | /// default is returned instead of causing an error. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 187 | static unsigned |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 188 | getOptionalUnsignedIntegerOption(const char *Msg, unsigned Default, |
| 189 | std::string &Val, bool isOnlyOption = true) { |
| 190 | // Check to see if the value was specified... |
| 191 | std::string TokVal = getToken(Val); |
| 192 | if (TokVal.empty()) return Default; |
| 193 | |
| 194 | // If it was specified, add it back to the value we are parsing... |
| 195 | Val = TokVal+Val; |
| 196 | |
| 197 | // And parse normally. |
| 198 | return getUnsignedIntegerOption(Msg, Val, isOnlyOption); |
| 199 | } |
Chris Lattner | e1567ae | 2004-01-06 05:37:16 +0000 | [diff] [blame] | 200 | |
| 201 | |
| 202 | /// parseProgramOptions - This method parses the Options string and loads it |
| 203 | /// as options to be passed to the program. This is used by the run command |
| 204 | /// and by 'set args'. |
| 205 | void CLIDebugger::parseProgramOptions(std::string &Options) { |
| 206 | // FIXME: tokenizing by whitespace is clearly incorrect. Instead we should |
| 207 | // honor quotes and other things that a shell would. Also in the future we |
| 208 | // should support redirection of standard IO. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 209 | |
Chris Lattner | e1567ae | 2004-01-06 05:37:16 +0000 | [diff] [blame] | 210 | std::vector<std::string> Arguments; |
| 211 | for (std::string A = getToken(Options); !A.empty(); A = getToken(Options)) |
| 212 | Arguments.push_back(A); |
| 213 | Dbg.setProgramArguments(Arguments.begin(), Arguments.end()); |
| 214 | } |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 215 | |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 216 | |
| 217 | //===----------------------------------------------------------------------===// |
| 218 | // Program startup and shutdown options |
| 219 | //===----------------------------------------------------------------------===// |
| 220 | |
| 221 | |
| 222 | /// file command - If the user specifies an option, search the PATH for the |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 223 | /// specified program/bitcode file and load it. If the user does not specify |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 224 | /// an option, unload the current program. |
| 225 | void CLIDebugger::fileCommand(std::string &Options) { |
| 226 | std::string Prog = getToken(Options); |
| 227 | if (!getToken(Options).empty()) |
| 228 | throw "file command takes at most one argument."; |
| 229 | |
| 230 | // Check to make sure the user knows what they are doing |
| 231 | if (Dbg.isProgramRunning() && |
| 232 | !askYesNo("A program is already loaded. Kill it?")) |
| 233 | return; |
| 234 | |
| 235 | // Unload an existing program. This kills the program if necessary. |
| 236 | eliminateRunInfo(); |
| 237 | delete TheProgramInfo; |
| 238 | TheProgramInfo = 0; |
| 239 | Dbg.unloadProgram(); |
| 240 | CurrentFile = 0; |
| 241 | |
| 242 | // If requested, start the new program. |
| 243 | if (Prog.empty()) { |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 244 | outs() << "Unloaded program.\n"; |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 245 | } else { |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 246 | outs() << "Loading program... "; |
| 247 | outs().flush(); |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 248 | Dbg.loadProgram(Prog, Context); |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 249 | assert(Dbg.isProgramLoaded() && |
| 250 | "loadProgram succeeded, but not program loaded!"); |
| 251 | TheProgramInfo = new ProgramInfo(Dbg.getProgram()); |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 252 | outs() << "successfully loaded '" << Dbg.getProgramPath() << "'!\n"; |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
| 256 | |
| 257 | void CLIDebugger::createCommand(std::string &Options) { |
| 258 | if (!getToken(Options).empty()) |
| 259 | throw "create command does not take any arguments."; |
| 260 | if (!Dbg.isProgramLoaded()) throw "No program loaded."; |
| 261 | if (Dbg.isProgramRunning() && |
| 262 | !askYesNo("The program is already running. Restart from the beginning?")) |
| 263 | return; |
| 264 | |
| 265 | // Start the program running. |
| 266 | startProgramRunning(); |
| 267 | |
| 268 | // The program stopped! |
| 269 | programStoppedSuccessfully(); |
| 270 | } |
| 271 | |
| 272 | void CLIDebugger::killCommand(std::string &Options) { |
| 273 | if (!getToken(Options).empty()) |
| 274 | throw "kill command does not take any arguments."; |
| 275 | if (!Dbg.isProgramRunning()) |
| 276 | throw "No program is currently being run."; |
| 277 | |
| 278 | if (askYesNo("Kill the program being debugged?")) |
| 279 | Dbg.killProgram(); |
| 280 | eliminateRunInfo(); |
| 281 | } |
| 282 | |
| 283 | void CLIDebugger::quitCommand(std::string &Options) { |
| 284 | if (!getToken(Options).empty()) |
| 285 | throw "quit command does not take any arguments."; |
| 286 | |
| 287 | if (Dbg.isProgramRunning() && |
| 288 | !askYesNo("The program is running. Exit anyway?")) |
| 289 | return; |
| 290 | |
| 291 | // Throw exception to get out of the user-input loop. |
| 292 | throw 0; |
| 293 | } |
| 294 | |
| 295 | |
| 296 | //===----------------------------------------------------------------------===// |
| 297 | // Program execution commands |
| 298 | //===----------------------------------------------------------------------===// |
| 299 | |
| 300 | void CLIDebugger::runCommand(std::string &Options) { |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 301 | if (!Dbg.isProgramLoaded()) throw "No program loaded."; |
| 302 | if (Dbg.isProgramRunning() && |
| 303 | !askYesNo("The program is already running. Restart from the beginning?")) |
| 304 | return; |
| 305 | |
Chris Lattner | e1567ae | 2004-01-06 05:37:16 +0000 | [diff] [blame] | 306 | // Parse all of the options to the run command, which specify program |
| 307 | // arguments to run with. |
| 308 | parseProgramOptions(Options); |
| 309 | |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 310 | eliminateRunInfo(); |
| 311 | |
| 312 | // Start the program running. |
| 313 | startProgramRunning(); |
| 314 | |
| 315 | // Start the program running... |
| 316 | Options = ""; |
| 317 | contCommand(Options); |
| 318 | } |
| 319 | |
| 320 | void CLIDebugger::contCommand(std::string &Options) { |
| 321 | if (!getToken(Options).empty()) throw "cont argument not supported yet."; |
| 322 | if (!Dbg.isProgramRunning()) throw "Program is not running."; |
| 323 | |
| 324 | eliminateRunInfo(); |
| 325 | |
| 326 | Dbg.contProgram(); |
| 327 | |
| 328 | // The program stopped! |
| 329 | programStoppedSuccessfully(); |
| 330 | } |
| 331 | |
| 332 | void CLIDebugger::stepCommand(std::string &Options) { |
| 333 | if (!Dbg.isProgramRunning()) throw "Program is not running."; |
| 334 | |
| 335 | // Figure out how many times to step. |
| 336 | unsigned Amount = |
| 337 | getOptionalUnsignedIntegerOption("'step' command", 1, Options); |
| 338 | |
| 339 | eliminateRunInfo(); |
| 340 | |
| 341 | // Step the specified number of times. |
| 342 | for (; Amount; --Amount) |
| 343 | Dbg.stepProgram(); |
| 344 | |
| 345 | // The program stopped! |
| 346 | programStoppedSuccessfully(); |
| 347 | } |
| 348 | |
| 349 | void CLIDebugger::nextCommand(std::string &Options) { |
| 350 | if (!Dbg.isProgramRunning()) throw "Program is not running."; |
| 351 | unsigned Amount = |
| 352 | getOptionalUnsignedIntegerOption("'next' command", 1, Options); |
| 353 | |
| 354 | eliminateRunInfo(); |
| 355 | |
| 356 | for (; Amount; --Amount) |
| 357 | Dbg.nextProgram(); |
| 358 | |
| 359 | // The program stopped! |
| 360 | programStoppedSuccessfully(); |
| 361 | } |
| 362 | |
| 363 | void CLIDebugger::finishCommand(std::string &Options) { |
| 364 | if (!getToken(Options).empty()) |
| 365 | throw "finish command does not take any arguments."; |
| 366 | if (!Dbg.isProgramRunning()) throw "Program is not running."; |
| 367 | |
| 368 | // Figure out where we are exactly. If the user requests that we return from |
| 369 | // a frame that is not the top frame, make sure we get it. |
| 370 | void *CurrentFrame = getRuntimeInfo().getCurrentFrame().getFrameID(); |
| 371 | |
| 372 | eliminateRunInfo(); |
| 373 | |
| 374 | Dbg.finishProgram(CurrentFrame); |
| 375 | |
| 376 | // The program stopped! |
| 377 | programStoppedSuccessfully(); |
| 378 | } |
| 379 | |
| 380 | //===----------------------------------------------------------------------===// |
| 381 | // Stack frame commands |
| 382 | //===----------------------------------------------------------------------===// |
| 383 | |
| 384 | void CLIDebugger::backtraceCommand(std::string &Options) { |
| 385 | // Accepts "full", n, -n |
| 386 | if (!getToken(Options).empty()) |
| 387 | throw "FIXME: bt command argument not implemented yet!"; |
| 388 | |
| 389 | RuntimeInfo &RI = getRuntimeInfo(); |
| 390 | ProgramInfo &PI = getProgramInfo(); |
| 391 | |
| 392 | try { |
| 393 | for (unsigned i = 0; ; ++i) { |
| 394 | StackFrame &SF = RI.getStackFrame(i); |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 395 | outs() << "#" << i; |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 396 | if (i == RI.getCurrentFrameIdx()) |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 397 | outs() << " ->"; |
| 398 | outs() << "\t" << SF.getFrameID() << " in "; |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 399 | if (const GlobalVariable *G = SF.getFunctionDesc()) |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 400 | outs() << PI.getFunction(G).getSymbolicName(); |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 401 | |
| 402 | unsigned LineNo, ColNo; |
| 403 | const SourceFileInfo *SFI; |
| 404 | SF.getSourceLocation(LineNo, ColNo, SFI); |
| 405 | if (!SFI->getBaseName().empty()) { |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 406 | outs() << " at " << SFI->getBaseName(); |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 407 | if (LineNo) { |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 408 | outs() << ":" << LineNo; |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 409 | if (ColNo) |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 410 | outs() << ":" << ColNo; |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
| 414 | // FIXME: when we support shared libraries, we should print ' from foo.so' |
| 415 | // if the stack frame is from a different object than the current one. |
| 416 | |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 417 | outs() << "\n"; |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 418 | } |
| 419 | } catch (...) { |
| 420 | // Stop automatically when we run off the bottom of the stack. |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | void CLIDebugger::upCommand(std::string &Options) { |
| 425 | unsigned Num = |
| 426 | getOptionalUnsignedIntegerOption("'up' command", 1, Options); |
| 427 | |
| 428 | RuntimeInfo &RI = getRuntimeInfo(); |
| 429 | unsigned CurFrame = RI.getCurrentFrameIdx(); |
| 430 | |
| 431 | // Check to see if we go can up the specified number of frames. |
| 432 | try { |
| 433 | RI.getStackFrame(CurFrame+Num); |
| 434 | } catch (...) { |
| 435 | if (Num == 1) |
| 436 | throw "Initial frame selected; you cannot go up."; |
| 437 | else |
| 438 | throw "Cannot go up " + utostr(Num) + " frames!"; |
| 439 | } |
| 440 | |
| 441 | RI.setCurrentFrameIdx(CurFrame+Num); |
| 442 | printProgramLocation(); |
| 443 | } |
| 444 | |
| 445 | void CLIDebugger::downCommand(std::string &Options) { |
| 446 | unsigned Num = |
| 447 | getOptionalUnsignedIntegerOption("'down' command", 1, Options); |
| 448 | |
| 449 | RuntimeInfo &RI = getRuntimeInfo(); |
| 450 | unsigned CurFrame = RI.getCurrentFrameIdx(); |
| 451 | |
| 452 | // Check to see if we can go up the specified number of frames. |
Anton Korobeynikov | a27694d | 2008-02-20 11:27:04 +0000 | [diff] [blame] | 453 | if (CurFrame < Num) { |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 454 | if (Num == 1) |
| 455 | throw "Bottom (i.e., innermost) frame selected; you cannot go down."; |
| 456 | else |
| 457 | throw "Cannot go down " + utostr(Num) + " frames!"; |
Anton Korobeynikov | a27694d | 2008-02-20 11:27:04 +0000 | [diff] [blame] | 458 | } |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 459 | |
| 460 | RI.setCurrentFrameIdx(CurFrame-Num); |
| 461 | printProgramLocation(); |
| 462 | } |
| 463 | |
| 464 | void CLIDebugger::frameCommand(std::string &Options) { |
| 465 | RuntimeInfo &RI = getRuntimeInfo(); |
| 466 | unsigned CurFrame = RI.getCurrentFrameIdx(); |
| 467 | |
| 468 | unsigned Num = |
| 469 | getOptionalUnsignedIntegerOption("'frame' command", CurFrame, Options); |
| 470 | |
| 471 | // Check to see if we go to the specified frame. |
| 472 | RI.getStackFrame(Num); |
| 473 | |
| 474 | RI.setCurrentFrameIdx(Num); |
| 475 | printProgramLocation(); |
| 476 | } |
| 477 | |
| 478 | |
| 479 | //===----------------------------------------------------------------------===// |
| 480 | // Breakpoint related commands |
| 481 | //===----------------------------------------------------------------------===// |
| 482 | |
| 483 | void CLIDebugger::breakCommand(std::string &Options) { |
Chris Lattner | e1567ae | 2004-01-06 05:37:16 +0000 | [diff] [blame] | 484 | // Figure out where the user wants a breakpoint. |
| 485 | const SourceFile *File; |
| 486 | unsigned LineNo; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 487 | |
Chris Lattner | e1567ae | 2004-01-06 05:37:16 +0000 | [diff] [blame] | 488 | // Check to see if the user specified a line specifier. |
| 489 | std::string Option = getToken(Options); // strip whitespace |
| 490 | if (!Option.empty()) { |
| 491 | Options = Option + Options; // reconstruct string |
| 492 | |
| 493 | // Parse the line specifier. |
Chris Lattner | f6608dd | 2004-01-06 23:46:17 +0000 | [diff] [blame] | 494 | parseLineSpec(Options, File, LineNo); |
Chris Lattner | e1567ae | 2004-01-06 05:37:16 +0000 | [diff] [blame] | 495 | } else { |
| 496 | // Build a line specifier for the current stack frame. |
| 497 | throw "FIXME: breaking at the current location is not implemented yet!"; |
| 498 | } |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 499 | |
Chris Lattner | f2592ce | 2004-02-08 00:06:20 +0000 | [diff] [blame] | 500 | if (!File) File = CurrentFile; |
| 501 | if (File == 0) |
| 502 | throw "Unknown file to place breakpoint!"; |
| 503 | |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 504 | errs() << "Break: " << File->getFilename() << ":" << LineNo << "\n"; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 505 | |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 506 | throw "breakpoints not implemented yet!"; |
| 507 | } |
| 508 | |
| 509 | //===----------------------------------------------------------------------===// |
| 510 | // Miscellaneous commands |
| 511 | //===----------------------------------------------------------------------===// |
| 512 | |
| 513 | void CLIDebugger::infoCommand(std::string &Options) { |
| 514 | std::string What = getToken(Options); |
| 515 | |
Chris Lattner | 395236f | 2004-10-26 05:46:17 +0000 | [diff] [blame] | 516 | if (What.empty() || !getToken(Options).empty()){ |
| 517 | std::string infoStr("info"); |
| 518 | helpCommand(infoStr); |
| 519 | return; |
| 520 | } |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 521 | |
| 522 | if (What == "frame") { |
| 523 | } else if (What == "functions") { |
| 524 | const std::map<const GlobalVariable*, SourceFunctionInfo*> &Functions |
| 525 | = getProgramInfo().getSourceFunctions(); |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 526 | outs() << "All defined functions:\n"; |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 527 | // FIXME: GDB groups these by source file. We could do that I guess. |
| 528 | for (std::map<const GlobalVariable*, SourceFunctionInfo*>::const_iterator |
| 529 | I = Functions.begin(), E = Functions.end(); I != E; ++I) { |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 530 | outs() << I->second->getSymbolicName() << "\n"; |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | } else if (What == "source") { |
| 534 | if (CurrentFile == 0) |
| 535 | throw "No current source file."; |
| 536 | |
| 537 | // Get the SourceFile information for the current file. |
| 538 | const SourceFileInfo &SF = |
| 539 | getProgramInfo().getSourceFile(CurrentFile->getDescriptor()); |
| 540 | |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 541 | outs() << "Current source file is: " << SF.getBaseName() << "\n" |
| 542 | << "Compilation directory is: " << SF.getDirectory() << "\n"; |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 543 | if (unsigned NL = CurrentFile->getNumLines()) |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 544 | outs() << "Located in: " << CurrentFile->getFilename() << "\n" |
| 545 | << "Contains " << NL << " lines\n"; |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 546 | else |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 547 | outs() << "Could not find source file.\n"; |
| 548 | outs() << "Source language is " |
| 549 | << SF.getLanguage().getSourceLanguageName() << "\n"; |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 550 | |
| 551 | } else if (What == "sources") { |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 552 | const std::map<const GlobalVariable*, SourceFileInfo*> &SourceFiles = |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 553 | getProgramInfo().getSourceFiles(); |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 554 | outs() << "Source files for the program:\n"; |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 555 | for (std::map<const GlobalVariable*, SourceFileInfo*>::const_iterator I = |
| 556 | SourceFiles.begin(), E = SourceFiles.end(); I != E;) { |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 557 | outs() << I->second->getDirectory() << "/" |
| 558 | << I->second->getBaseName(); |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 559 | ++I; |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 560 | if (I != E) outs() << ", "; |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 561 | } |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 562 | outs() << "\n"; |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 563 | } else if (What == "target") { |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 564 | outs() << Dbg.getRunningProcess().getStatus(); |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 565 | } else { |
| 566 | // See if this is something handled by the current language. |
| 567 | if (getCurrentLanguage().printInfo(What)) |
| 568 | return; |
| 569 | |
| 570 | throw "Unknown info command '" + What + "'. Try 'help info'."; |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | /// parseLineSpec - Parses a line specifier, for use by the 'list' command. |
| 575 | /// If SourceFile is returned as a void pointer, then it was not specified. |
| 576 | /// If the line specifier is invalid, an exception is thrown. |
| 577 | void CLIDebugger::parseLineSpec(std::string &LineSpec, |
| 578 | const SourceFile *&SourceFile, |
| 579 | unsigned &LineNo) { |
| 580 | SourceFile = 0; |
| 581 | LineNo = 0; |
| 582 | |
| 583 | // First, check to see if we have a : separator. |
| 584 | std::string FirstPart = getToken(LineSpec, ":"); |
| 585 | std::string SecondPart = getToken(LineSpec, ":"); |
| 586 | if (!getToken(LineSpec).empty()) throw "Malformed line specification!"; |
| 587 | |
| 588 | // If there is no second part, we must have either "function", "number", |
| 589 | // "+offset", or "-offset". |
| 590 | if (SecondPart.empty()) { |
| 591 | if (FirstPart.empty()) throw "Malformed line specification!"; |
| 592 | if (FirstPart[0] == '+') { |
| 593 | FirstPart.erase(FirstPart.begin(), FirstPart.begin()+1); |
| 594 | // For +n, return LineListedEnd+n |
| 595 | LineNo = LineListedEnd + |
| 596 | getUnsignedIntegerOption("Line specifier '+'", FirstPart); |
| 597 | |
| 598 | } else if (FirstPart[0] == '-') { |
| 599 | FirstPart.erase(FirstPart.begin(), FirstPart.begin()+1); |
| 600 | // For -n, return LineListedEnd-n |
| 601 | LineNo = LineListedEnd - |
| 602 | getUnsignedIntegerOption("Line specifier '-'", FirstPart); |
| 603 | if ((int)LineNo < 1) LineNo = 1; |
| 604 | } else if (FirstPart[0] == '*') { |
| 605 | throw "Address expressions not supported as source locations!"; |
| 606 | } else { |
| 607 | // Ok, check to see if this is just a line number. |
| 608 | std::string Saved = FirstPart; |
| 609 | try { |
| 610 | LineNo = getUnsignedIntegerOption("", Saved); |
| 611 | } catch (...) { |
| 612 | // Ok, it's not a valid line number. It must be a source-language |
| 613 | // entity name. |
| 614 | std::string Name = getToken(FirstPart); |
| 615 | if (!getToken(FirstPart).empty()) |
| 616 | throw "Extra junk in line specifier after '" + Name + "'."; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 617 | SourceFunctionInfo *SFI = |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 618 | getCurrentLanguage().lookupFunction(Name, getProgramInfo(), |
| 619 | TheRuntimeInfo); |
| 620 | if (SFI == 0) |
| 621 | throw "Unknown identifier '" + Name + "'."; |
| 622 | |
| 623 | unsigned L, C; |
| 624 | SFI->getSourceLocation(L, C); |
| 625 | if (L == 0) throw "Could not locate '" + Name + "'!"; |
| 626 | LineNo = L; |
| 627 | SourceFile = &SFI->getSourceFile().getSourceText(); |
| 628 | return; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | } else { |
| 633 | // Ok, this must be a filename qualified line number or function name. |
| 634 | // First, figure out the source filename. |
| 635 | std::string SourceFilename = getToken(FirstPart); |
| 636 | if (!getToken(FirstPart).empty()) |
| 637 | throw "Invalid filename qualified source location!"; |
| 638 | |
| 639 | // Next, check to see if this is just a line number. |
| 640 | std::string Saved = SecondPart; |
| 641 | try { |
| 642 | LineNo = getUnsignedIntegerOption("", Saved); |
| 643 | } catch (...) { |
| 644 | // Ok, it's not a valid line number. It must be a function name. |
| 645 | throw "FIXME: Filename qualified function names are not support " |
| 646 | "as line specifiers yet!"; |
| 647 | } |
| 648 | |
| 649 | // Ok, we got the line number. Now check out the source file name to make |
| 650 | // sure it's all good. If it is, return it. If not, throw exception. |
| 651 | SourceFile =&getProgramInfo().getSourceFile(SourceFilename).getSourceText(); |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | void CLIDebugger::listCommand(std::string &Options) { |
| 656 | if (!Dbg.isProgramLoaded()) |
| 657 | throw "No program is loaded. Use the 'file' command."; |
| 658 | |
| 659 | // Handle "list foo," correctly, by returning " " as the second token |
| 660 | Options += " "; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 661 | |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 662 | std::string FirstLineSpec = getToken(Options, ","); |
| 663 | std::string SecondLineSpec = getToken(Options, ","); |
| 664 | if (!getToken(Options, ",").empty()) |
| 665 | throw "list command only expects two source location specifiers!"; |
| 666 | |
| 667 | // StartLine, EndLine - The starting and ending line numbers to print. |
| 668 | unsigned StartLine = 0, EndLine = 0; |
| 669 | |
| 670 | if (SecondLineSpec.empty()) { // No second line specifier provided? |
| 671 | // Handle special forms like "", "+", "-", etc. |
| 672 | std::string TmpSpec = FirstLineSpec; |
| 673 | std::string Tok = getToken(TmpSpec); |
| 674 | if (getToken(TmpSpec).empty() && (Tok == "" || Tok == "+" || Tok == "-")) { |
| 675 | if (Tok == "+" || Tok == "") { |
| 676 | StartLine = LineListedEnd; |
| 677 | EndLine = StartLine + ListSize; |
| 678 | } else { |
| 679 | assert(Tok == "-"); |
| 680 | StartLine = LineListedStart-ListSize; |
| 681 | EndLine = LineListedStart; |
Chris Lattner | e1567ae | 2004-01-06 05:37:16 +0000 | [diff] [blame] | 682 | if ((int)StartLine <= 0) StartLine = 1; |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 683 | } |
| 684 | } else { |
| 685 | // Must be a normal line specifier. |
| 686 | const SourceFile *File; |
| 687 | unsigned LineNo; |
| 688 | parseLineSpec(FirstLineSpec, File, LineNo); |
| 689 | |
| 690 | // If the user only specified one file specifier, we should display |
| 691 | // ListSize lines centered at the specified line. |
| 692 | if (File != 0) CurrentFile = File; |
| 693 | StartLine = LineNo - (ListSize+1)/2; |
Chris Lattner | e1567ae | 2004-01-06 05:37:16 +0000 | [diff] [blame] | 694 | if ((int)StartLine <= 0) StartLine = 1; |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 695 | EndLine = StartLine + ListSize; |
| 696 | } |
| 697 | |
| 698 | } else { |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 699 | // Parse two line specifiers... |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 700 | const SourceFile *StartFile, *EndFile; |
| 701 | unsigned StartLineNo, EndLineNo; |
| 702 | parseLineSpec(FirstLineSpec, StartFile, StartLineNo); |
| 703 | unsigned SavedLLE = LineListedEnd; |
| 704 | LineListedEnd = StartLineNo; |
| 705 | try { |
| 706 | parseLineSpec(SecondLineSpec, EndFile, EndLineNo); |
| 707 | } catch (...) { |
| 708 | LineListedEnd = SavedLLE; |
| 709 | throw; |
| 710 | } |
| 711 | |
| 712 | // Inherit file specified by the first line spec if there was one. |
| 713 | if (EndFile == 0) EndFile = StartFile; |
| 714 | |
| 715 | if (StartFile != EndFile) |
| 716 | throw "Start and end line specifiers are in different files!"; |
| 717 | CurrentFile = StartFile; |
| 718 | StartLine = StartLineNo; |
| 719 | EndLine = EndLineNo+1; |
| 720 | } |
| 721 | |
| 722 | assert((int)StartLine > 0 && (int)EndLine > 0 && StartLine <= EndLine && |
| 723 | "Error reading line specifiers!"); |
| 724 | |
| 725 | // If there was no current file, and the user didn't specify one to list, we |
| 726 | // have an error. |
| 727 | if (CurrentFile == 0) |
| 728 | throw "There is no current file to list."; |
| 729 | |
| 730 | // Remember for next time. |
| 731 | LineListedStart = StartLine; |
| 732 | LineListedEnd = StartLine; |
| 733 | |
| 734 | for (unsigned LineNo = StartLine; LineNo != EndLine; ++LineNo) { |
| 735 | // Print the source line, unless it is invalid. |
| 736 | if (printSourceLine(LineNo)) |
| 737 | break; |
| 738 | LineListedEnd = LineNo+1; |
| 739 | } |
| 740 | |
| 741 | // If we didn't print any lines, find out why. |
| 742 | if (LineListedEnd == StartLine) { |
| 743 | // See if we can read line #0 from the file, if not, we couldn't load the |
| 744 | // file. |
| 745 | const char *LineStart, *LineEnd; |
| 746 | CurrentFile->getSourceLine(0, LineStart, LineEnd); |
| 747 | if (LineStart == 0) |
| 748 | throw "Could not load source file '" + CurrentFile->getFilename() + "'!"; |
| 749 | else |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 750 | outs() << "<end of file>\n"; |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 751 | } |
| 752 | } |
| 753 | |
| 754 | void CLIDebugger::setCommand(std::string &Options) { |
| 755 | std::string What = getToken(Options); |
| 756 | |
| 757 | if (What.empty()) |
| 758 | throw "set command expects at least two arguments."; |
Chris Lattner | e1567ae | 2004-01-06 05:37:16 +0000 | [diff] [blame] | 759 | if (What == "args") { |
| 760 | parseProgramOptions(Options); |
| 761 | } else if (What == "language") { |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 762 | std::string Lang = getToken(Options); |
| 763 | if (!getToken(Options).empty()) |
| 764 | throw "set language expects one argument at most."; |
| 765 | if (Lang == "") { |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 766 | outs() << "The currently understood settings are:\n\n" |
| 767 | << "local or auto Automatic setting based on source file\n" |
| 768 | << "c Use the C language\n" |
| 769 | << "c++ Use the C++ language\n" |
| 770 | << "unknown Use when source language is not supported\n"; |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 771 | } else if (Lang == "local" || Lang == "auto") { |
| 772 | CurrentLanguage = 0; |
| 773 | } else if (Lang == "c") { |
| 774 | CurrentLanguage = &SourceLanguage::getCFamilyInstance(); |
| 775 | } else if (Lang == "c++") { |
| 776 | CurrentLanguage = &SourceLanguage::getCPlusPlusInstance(); |
| 777 | } else if (Lang == "unknown") { |
| 778 | CurrentLanguage = &SourceLanguage::getUnknownLanguageInstance(); |
| 779 | } else { |
| 780 | throw "Unknown language '" + Lang + "'."; |
| 781 | } |
| 782 | |
| 783 | } else if (What == "listsize") { |
| 784 | ListSize = getUnsignedIntegerOption("'set prompt' command", Options); |
| 785 | } else if (What == "prompt") { |
| 786 | // Include any trailing whitespace or other tokens, but not leading |
| 787 | // whitespace. |
| 788 | Prompt = getToken(Options); // Strip leading whitespace |
| 789 | Prompt += Options; // Keep trailing whitespace or other stuff |
| 790 | } else { |
| 791 | // FIXME: Try to parse this as a source-language program expression. |
| 792 | throw "Don't know how to set '" + What + "'!"; |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | void CLIDebugger::showCommand(std::string &Options) { |
| 797 | std::string What = getToken(Options); |
| 798 | |
| 799 | if (What.empty() || !getToken(Options).empty()) |
| 800 | throw "show command expects one argument."; |
| 801 | |
Chris Lattner | e1567ae | 2004-01-06 05:37:16 +0000 | [diff] [blame] | 802 | if (What == "args") { |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 803 | outs() << "Argument list to give program when started is \""; |
Chris Lattner | e1567ae | 2004-01-06 05:37:16 +0000 | [diff] [blame] | 804 | // FIXME: This doesn't print stuff correctly if the arguments have spaces in |
| 805 | // them, but currently the only way to get that is to use the --args command |
| 806 | // line argument. This should really handle escaping all hard characters as |
| 807 | // needed. |
| 808 | for (unsigned i = 0, e = Dbg.getNumProgramArguments(); i != e; ++i) |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 809 | outs() << (i ? " " : "") << Dbg.getProgramArgument(i); |
| 810 | outs() << "\"\n"; |
Chris Lattner | e1567ae | 2004-01-06 05:37:16 +0000 | [diff] [blame] | 811 | |
| 812 | } else if (What == "language") { |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 813 | outs() << "The current source language is '"; |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 814 | if (CurrentLanguage) |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 815 | outs() << CurrentLanguage->getSourceLanguageName(); |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 816 | else |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 817 | outs() << "auto; currently " |
| 818 | << getCurrentLanguage().getSourceLanguageName(); |
| 819 | outs() << "'.\n"; |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 820 | } else if (What == "listsize") { |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 821 | outs() << "Number of source lines llvm-db will list by default is " |
| 822 | << ListSize << ".\n"; |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 823 | } else if (What == "prompt") { |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 824 | outs() << "llvm-db's prompt is \"" << Prompt << "\".\n"; |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 825 | } else { |
| 826 | throw "Unknown show command '" + What + "'. Try 'help show'."; |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | void CLIDebugger::helpCommand(std::string &Options) { |
| 831 | // Print out all of the commands in the CommandTable |
| 832 | std::string Command = getToken(Options); |
| 833 | if (!getToken(Options).empty()) |
| 834 | throw "help command takes at most one argument."; |
| 835 | |
| 836 | // Getting detailed help on a particular command? |
| 837 | if (!Command.empty()) { |
| 838 | CLICommand *C = getCommand(Command); |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 839 | outs() << C->getShortHelp() << ".\n" << C->getLongHelp(); |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 840 | |
| 841 | // If there are aliases for this option, print them out. |
| 842 | const std::vector<std::string> &Names = C->getOptionNames(); |
| 843 | if (Names.size() > 1) { |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 844 | outs() << "The '" << Command << "' command is known as: '" |
| 845 | << Names[0] << "'"; |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 846 | for (unsigned i = 1, e = Names.size(); i != e; ++i) |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 847 | outs() << ", '" << Names[i] << "'"; |
| 848 | outs() << "\n"; |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | } else { |
| 852 | unsigned MaxSize = 0; |
| 853 | for (std::map<std::string, CLICommand*>::iterator I = CommandTable.begin(), |
| 854 | E = CommandTable.end(); I != E; ++I) |
| 855 | if (I->first.size() > MaxSize && |
| 856 | I->first == I->second->getPrimaryOptionName()) |
| 857 | MaxSize = I->first.size(); |
| 858 | |
| 859 | // Loop over all of the commands, printing the short help version |
| 860 | for (std::map<std::string, CLICommand*>::iterator I = CommandTable.begin(), |
| 861 | E = CommandTable.end(); I != E; ++I) |
| 862 | if (I->first == I->second->getPrimaryOptionName()) |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 863 | outs() << I->first << std::string(MaxSize - I->first.size(), ' ') |
| 864 | << " - " << I->second->getShortHelp() << "\n"; |
Chris Lattner | 7af5c12 | 2004-01-05 05:27:31 +0000 | [diff] [blame] | 865 | } |
| 866 | } |