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