1 //===-- Commands.cpp - Implement various commands for the CLI -------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements many builtin user commands.
12 //===----------------------------------------------------------------------===//
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"
21 #include "Support/FileUtilities.h"
22 #include "Support/StringExtras.h"
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
30 const SourceLanguage &CLIDebugger::getCurrentLanguage() const {
31 // If the user explicitly switched languages with 'set language', use what
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();
41 // Otherwise, default to C like GDB apparently does.
42 return SourceLanguage::getCFamilyInstance();
46 /// startProgramRunning - If the program has been updated, reload it, then
47 /// start executing the program.
48 void CLIDebugger::startProgramRunning() {
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";
56 // Unload an existing program. This kills the program if necessary.
58 delete TheProgramInfo;
62 Dbg.loadProgram(Program);
63 TheProgramInfo = new ProgramInfo(Dbg.getProgram());
66 std::cout << "Starting program: " << Dbg.getProgramPath() << "\n";
69 // There was no current frame.
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;
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,
90 if (CurLineNo == LineNo && CurrentFile == &CurSFI->getSourceText())
94 std::cout << "\t" << std::string(LineStart, LineEnd) << "\n";
98 /// printProgramLocation - Print a line of the place where the current stack
99 /// frame has stopped and the source line it is on.
101 void CLIDebugger::printProgramLocation(bool PrintLocation) {
102 assert(Dbg.isProgramLoaded() && Dbg.isProgramRunning() &&
103 "Error program is not loaded and running!");
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);
111 // If requested, print out some program information about WHERE we are.
113 // FIXME: print the current function arguments
114 if (const GlobalVariable *FuncDesc = SF.getFunctionDesc())
115 std::cout << getProgramInfo().getFunction(FuncDesc).getSymbolicName();
117 std::cout << "<unknown function>";
119 CurrentFile = &FileDesc->getSourceText();
121 std::cout << " at " << CurrentFile->getFilename() << ":" << LineNo;
122 if (ColNo) std::cout << ":" << ColNo << "\n";
125 if (printSourceLine(LineNo))
126 std::cout << "<could not load source file>\n";
128 LineListedStart = LineNo-ListSize/2+1;
129 if ((int)LineListedStart < 1) LineListedStart = 1;
130 LineListedEnd = LineListedStart+1;
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;
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!");
147 TheRuntimeInfo = new RuntimeInfo(TheProgramInfo, Dbg.getRunningProcess());
149 // FIXME: if there are any breakpoints at the current location, print them as
152 // Since the program as successfully stopped, print its location.
153 void *CurrentFrame = getRuntimeInfo().getCurrentFrame().getFrameID();
154 printProgramLocation(CurrentFrame != LastCurrentFrame);
155 LastCurrentFrame = CurrentFrame;
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.";
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.";
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.
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;
188 // If it was specified, add it back to the value we are parsing...
191 // And parse normally.
192 return getUnsignedIntegerOption(Msg, Val, isOnlyOption);
196 //===----------------------------------------------------------------------===//
197 // Program startup and shutdown options
198 //===----------------------------------------------------------------------===//
201 /// file command - If the user specifies an option, search the PATH for the
202 /// specified program/bytecode file and load it. If the user does not specify
203 /// an option, unload the current program.
204 void CLIDebugger::fileCommand(std::string &Options) {
205 std::string Prog = getToken(Options);
206 if (!getToken(Options).empty())
207 throw "file command takes at most one argument.";
209 // Check to make sure the user knows what they are doing
210 if (Dbg.isProgramRunning() &&
211 !askYesNo("A program is already loaded. Kill it?"))
214 // Unload an existing program. This kills the program if necessary.
216 delete TheProgramInfo;
221 // If requested, start the new program.
223 std::cout << "Unloaded program.\n";
225 std::cout << "Loading program... " << std::flush;
226 Dbg.loadProgram(Prog);
227 assert(Dbg.isProgramLoaded() &&
228 "loadProgram succeeded, but not program loaded!");
229 TheProgramInfo = new ProgramInfo(Dbg.getProgram());
230 std::cout << "success loading '" << Dbg.getProgramPath() << "'!\n";
235 void CLIDebugger::createCommand(std::string &Options) {
236 if (!getToken(Options).empty())
237 throw "create command does not take any arguments.";
238 if (!Dbg.isProgramLoaded()) throw "No program loaded.";
239 if (Dbg.isProgramRunning() &&
240 !askYesNo("The program is already running. Restart from the beginning?"))
243 // Start the program running.
244 startProgramRunning();
246 // The program stopped!
247 programStoppedSuccessfully();
250 void CLIDebugger::killCommand(std::string &Options) {
251 if (!getToken(Options).empty())
252 throw "kill command does not take any arguments.";
253 if (!Dbg.isProgramRunning())
254 throw "No program is currently being run.";
256 if (askYesNo("Kill the program being debugged?"))
261 void CLIDebugger::quitCommand(std::string &Options) {
262 if (!getToken(Options).empty())
263 throw "quit command does not take any arguments.";
265 if (Dbg.isProgramRunning() &&
266 !askYesNo("The program is running. Exit anyway?"))
269 // Throw exception to get out of the user-input loop.
274 //===----------------------------------------------------------------------===//
275 // Program execution commands
276 //===----------------------------------------------------------------------===//
278 void CLIDebugger::runCommand(std::string &Options) {
279 if (!getToken(Options).empty()) throw "run arguments not supported yet.";
280 if (!Dbg.isProgramLoaded()) throw "No program loaded.";
281 if (Dbg.isProgramRunning() &&
282 !askYesNo("The program is already running. Restart from the beginning?"))
287 // Start the program running.
288 startProgramRunning();
290 // Start the program running...
292 contCommand(Options);
295 void CLIDebugger::contCommand(std::string &Options) {
296 if (!getToken(Options).empty()) throw "cont argument not supported yet.";
297 if (!Dbg.isProgramRunning()) throw "Program is not running.";
303 // The program stopped!
304 programStoppedSuccessfully();
307 void CLIDebugger::stepCommand(std::string &Options) {
308 if (!Dbg.isProgramRunning()) throw "Program is not running.";
310 // Figure out how many times to step.
312 getOptionalUnsignedIntegerOption("'step' command", 1, Options);
316 // Step the specified number of times.
317 for (; Amount; --Amount)
320 // The program stopped!
321 programStoppedSuccessfully();
324 void CLIDebugger::nextCommand(std::string &Options) {
325 if (!Dbg.isProgramRunning()) throw "Program is not running.";
327 getOptionalUnsignedIntegerOption("'next' command", 1, Options);
331 for (; Amount; --Amount)
334 // The program stopped!
335 programStoppedSuccessfully();
338 void CLIDebugger::finishCommand(std::string &Options) {
339 if (!getToken(Options).empty())
340 throw "finish command does not take any arguments.";
341 if (!Dbg.isProgramRunning()) throw "Program is not running.";
343 // Figure out where we are exactly. If the user requests that we return from
344 // a frame that is not the top frame, make sure we get it.
345 void *CurrentFrame = getRuntimeInfo().getCurrentFrame().getFrameID();
349 Dbg.finishProgram(CurrentFrame);
351 // The program stopped!
352 programStoppedSuccessfully();
355 //===----------------------------------------------------------------------===//
356 // Stack frame commands
357 //===----------------------------------------------------------------------===//
359 void CLIDebugger::backtraceCommand(std::string &Options) {
360 // Accepts "full", n, -n
361 if (!getToken(Options).empty())
362 throw "FIXME: bt command argument not implemented yet!";
364 RuntimeInfo &RI = getRuntimeInfo();
365 ProgramInfo &PI = getProgramInfo();
368 for (unsigned i = 0; ; ++i) {
369 StackFrame &SF = RI.getStackFrame(i);
370 std::cout << "#" << i;
371 if (i == RI.getCurrentFrameIdx())
373 std::cout << "\t" << SF.getFrameID() << " in ";
374 if (const GlobalVariable *G = SF.getFunctionDesc())
375 std::cout << PI.getFunction(G).getSymbolicName();
377 unsigned LineNo, ColNo;
378 const SourceFileInfo *SFI;
379 SF.getSourceLocation(LineNo, ColNo, SFI);
380 if (!SFI->getBaseName().empty()) {
381 std::cout << " at " << SFI->getBaseName();
383 std::cout << ":" << LineNo;
385 std::cout << ":" << ColNo;
389 // FIXME: when we support shared libraries, we should print ' from foo.so'
390 // if the stack frame is from a different object than the current one.
395 // Stop automatically when we run off the bottom of the stack.
399 void CLIDebugger::upCommand(std::string &Options) {
401 getOptionalUnsignedIntegerOption("'up' command", 1, Options);
403 RuntimeInfo &RI = getRuntimeInfo();
404 unsigned CurFrame = RI.getCurrentFrameIdx();
406 // Check to see if we go can up the specified number of frames.
408 RI.getStackFrame(CurFrame+Num);
411 throw "Initial frame selected; you cannot go up.";
413 throw "Cannot go up " + utostr(Num) + " frames!";
416 RI.setCurrentFrameIdx(CurFrame+Num);
417 printProgramLocation();
420 void CLIDebugger::downCommand(std::string &Options) {
422 getOptionalUnsignedIntegerOption("'down' command", 1, Options);
424 RuntimeInfo &RI = getRuntimeInfo();
425 unsigned CurFrame = RI.getCurrentFrameIdx();
427 // Check to see if we can go up the specified number of frames.
430 throw "Bottom (i.e., innermost) frame selected; you cannot go down.";
432 throw "Cannot go down " + utostr(Num) + " frames!";
434 RI.setCurrentFrameIdx(CurFrame-Num);
435 printProgramLocation();
438 void CLIDebugger::frameCommand(std::string &Options) {
439 RuntimeInfo &RI = getRuntimeInfo();
440 unsigned CurFrame = RI.getCurrentFrameIdx();
443 getOptionalUnsignedIntegerOption("'frame' command", CurFrame, Options);
445 // Check to see if we go to the specified frame.
446 RI.getStackFrame(Num);
448 RI.setCurrentFrameIdx(Num);
449 printProgramLocation();
453 //===----------------------------------------------------------------------===//
454 // Breakpoint related commands
455 //===----------------------------------------------------------------------===//
457 void CLIDebugger::breakCommand(std::string &Options) {
458 throw "breakpoints not implemented yet!";
461 //===----------------------------------------------------------------------===//
462 // Miscellaneous commands
463 //===----------------------------------------------------------------------===//
465 void CLIDebugger::infoCommand(std::string &Options) {
466 std::string What = getToken(Options);
468 if (What.empty() || !getToken(Options).empty())
469 throw "info command expects exactly one argument.";
471 if (What == "frame") {
472 } else if (What == "functions") {
473 const std::map<const GlobalVariable*, SourceFunctionInfo*> &Functions
474 = getProgramInfo().getSourceFunctions();
475 std::cout << "All defined functions:\n";
476 // FIXME: GDB groups these by source file. We could do that I guess.
477 for (std::map<const GlobalVariable*, SourceFunctionInfo*>::const_iterator
478 I = Functions.begin(), E = Functions.end(); I != E; ++I) {
479 std::cout << I->second->getSymbolicName() << "\n";
482 } else if (What == "source") {
483 if (CurrentFile == 0)
484 throw "No current source file.";
486 // Get the SourceFile information for the current file.
487 const SourceFileInfo &SF =
488 getProgramInfo().getSourceFile(CurrentFile->getDescriptor());
490 std::cout << "Current source file is: " << SF.getBaseName() << "\n"
491 << "Compilation directory is: " << SF.getDirectory() << "\n";
492 if (unsigned NL = CurrentFile->getNumLines())
493 std::cout << "Located in: " << CurrentFile->getFilename() << "\n"
494 << "Contains " << NL << " lines\n";
496 std::cout << "Could not find source file.\n";
497 std::cout << "Source language is "
498 << SF.getLanguage().getSourceLanguageName() << "\n";
500 } else if (What == "sources") {
501 const std::map<const GlobalVariable*, SourceFileInfo*> &SourceFiles =
502 getProgramInfo().getSourceFiles();
503 std::cout << "Source files for the program:\n";
504 for (std::map<const GlobalVariable*, SourceFileInfo*>::const_iterator I =
505 SourceFiles.begin(), E = SourceFiles.end(); I != E;) {
506 std::cout << I->second->getDirectory() << "/"
507 << I->second->getBaseName();
509 if (I != E) std::cout << ", ";
512 } else if (What == "target") {
513 std::cout << Dbg.getRunningProcess().getStatus();
515 // See if this is something handled by the current language.
516 if (getCurrentLanguage().printInfo(What))
519 throw "Unknown info command '" + What + "'. Try 'help info'.";
523 /// parseLineSpec - Parses a line specifier, for use by the 'list' command.
524 /// If SourceFile is returned as a void pointer, then it was not specified.
525 /// If the line specifier is invalid, an exception is thrown.
526 void CLIDebugger::parseLineSpec(std::string &LineSpec,
527 const SourceFile *&SourceFile,
532 // First, check to see if we have a : separator.
533 std::string FirstPart = getToken(LineSpec, ":");
534 std::string SecondPart = getToken(LineSpec, ":");
535 if (!getToken(LineSpec).empty()) throw "Malformed line specification!";
537 // If there is no second part, we must have either "function", "number",
538 // "+offset", or "-offset".
539 if (SecondPart.empty()) {
540 if (FirstPart.empty()) throw "Malformed line specification!";
541 if (FirstPart[0] == '+') {
542 FirstPart.erase(FirstPart.begin(), FirstPart.begin()+1);
543 // For +n, return LineListedEnd+n
544 LineNo = LineListedEnd +
545 getUnsignedIntegerOption("Line specifier '+'", FirstPart);
547 } else if (FirstPart[0] == '-') {
548 FirstPart.erase(FirstPart.begin(), FirstPart.begin()+1);
549 // For -n, return LineListedEnd-n
550 LineNo = LineListedEnd -
551 getUnsignedIntegerOption("Line specifier '-'", FirstPart);
552 if ((int)LineNo < 1) LineNo = 1;
553 } else if (FirstPart[0] == '*') {
554 throw "Address expressions not supported as source locations!";
556 // Ok, check to see if this is just a line number.
557 std::string Saved = FirstPart;
559 LineNo = getUnsignedIntegerOption("", Saved);
561 // Ok, it's not a valid line number. It must be a source-language
563 std::string Name = getToken(FirstPart);
564 if (!getToken(FirstPart).empty())
565 throw "Extra junk in line specifier after '" + Name + "'.";
566 SourceFunctionInfo *SFI =
567 getCurrentLanguage().lookupFunction(Name, getProgramInfo(),
570 throw "Unknown identifier '" + Name + "'.";
573 SFI->getSourceLocation(L, C);
574 if (L == 0) throw "Could not locate '" + Name + "'!";
576 SourceFile = &SFI->getSourceFile().getSourceText();
582 // Ok, this must be a filename qualified line number or function name.
583 // First, figure out the source filename.
584 std::string SourceFilename = getToken(FirstPart);
585 if (!getToken(FirstPart).empty())
586 throw "Invalid filename qualified source location!";
588 // Next, check to see if this is just a line number.
589 std::string Saved = SecondPart;
591 LineNo = getUnsignedIntegerOption("", Saved);
593 // Ok, it's not a valid line number. It must be a function name.
594 throw "FIXME: Filename qualified function names are not support "
595 "as line specifiers yet!";
598 // Ok, we got the line number. Now check out the source file name to make
599 // sure it's all good. If it is, return it. If not, throw exception.
600 SourceFile =&getProgramInfo().getSourceFile(SourceFilename).getSourceText();
604 void CLIDebugger::listCommand(std::string &Options) {
605 if (!Dbg.isProgramLoaded())
606 throw "No program is loaded. Use the 'file' command.";
608 // Handle "list foo," correctly, by returning " " as the second token
611 std::string FirstLineSpec = getToken(Options, ",");
612 std::string SecondLineSpec = getToken(Options, ",");
613 if (!getToken(Options, ",").empty())
614 throw "list command only expects two source location specifiers!";
616 // StartLine, EndLine - The starting and ending line numbers to print.
617 unsigned StartLine = 0, EndLine = 0;
619 if (SecondLineSpec.empty()) { // No second line specifier provided?
620 // Handle special forms like "", "+", "-", etc.
621 std::string TmpSpec = FirstLineSpec;
622 std::string Tok = getToken(TmpSpec);
623 if (getToken(TmpSpec).empty() && (Tok == "" || Tok == "+" || Tok == "-")) {
624 if (Tok == "+" || Tok == "") {
625 StartLine = LineListedEnd;
626 EndLine = StartLine + ListSize;
629 StartLine = LineListedStart-ListSize;
630 EndLine = LineListedStart;
631 if ((int)StartLine < 0) StartLine = 1;
634 // Must be a normal line specifier.
635 const SourceFile *File;
637 parseLineSpec(FirstLineSpec, File, LineNo);
639 // If the user only specified one file specifier, we should display
640 // ListSize lines centered at the specified line.
641 if (File != 0) CurrentFile = File;
642 StartLine = LineNo - (ListSize+1)/2;
643 if ((int)StartLine < 0) StartLine = 1;
644 EndLine = StartLine + ListSize;
648 // Parse two line specifiers...
649 const SourceFile *StartFile, *EndFile;
650 unsigned StartLineNo, EndLineNo;
651 parseLineSpec(FirstLineSpec, StartFile, StartLineNo);
652 unsigned SavedLLE = LineListedEnd;
653 LineListedEnd = StartLineNo;
655 parseLineSpec(SecondLineSpec, EndFile, EndLineNo);
657 LineListedEnd = SavedLLE;
661 // Inherit file specified by the first line spec if there was one.
662 if (EndFile == 0) EndFile = StartFile;
664 if (StartFile != EndFile)
665 throw "Start and end line specifiers are in different files!";
666 CurrentFile = StartFile;
667 StartLine = StartLineNo;
668 EndLine = EndLineNo+1;
671 assert((int)StartLine > 0 && (int)EndLine > 0 && StartLine <= EndLine &&
672 "Error reading line specifiers!");
674 // If there was no current file, and the user didn't specify one to list, we
676 if (CurrentFile == 0)
677 throw "There is no current file to list.";
679 // Remember for next time.
680 LineListedStart = StartLine;
681 LineListedEnd = StartLine;
683 for (unsigned LineNo = StartLine; LineNo != EndLine; ++LineNo) {
684 // Print the source line, unless it is invalid.
685 if (printSourceLine(LineNo))
687 LineListedEnd = LineNo+1;
690 // If we didn't print any lines, find out why.
691 if (LineListedEnd == StartLine) {
692 // See if we can read line #0 from the file, if not, we couldn't load the
694 const char *LineStart, *LineEnd;
695 CurrentFile->getSourceLine(0, LineStart, LineEnd);
697 throw "Could not load source file '" + CurrentFile->getFilename() + "'!";
699 std::cout << "<end of file>\n";
703 void CLIDebugger::setCommand(std::string &Options) {
704 std::string What = getToken(Options);
707 throw "set command expects at least two arguments.";
708 if (What == "language") {
709 std::string Lang = getToken(Options);
710 if (!getToken(Options).empty())
711 throw "set language expects one argument at most.";
713 std::cout << "The currently understood settings are:\n\n"
714 << "local or auto Automatic setting based on source file\n"
715 << "c Use the C language\n"
716 << "c++ Use the C++ language\n"
717 << "unknown Use when source language is not supported\n";
718 } else if (Lang == "local" || Lang == "auto") {
720 } else if (Lang == "c") {
721 CurrentLanguage = &SourceLanguage::getCFamilyInstance();
722 } else if (Lang == "c++") {
723 CurrentLanguage = &SourceLanguage::getCPlusPlusInstance();
724 } else if (Lang == "unknown") {
725 CurrentLanguage = &SourceLanguage::getUnknownLanguageInstance();
727 throw "Unknown language '" + Lang + "'.";
730 } else if (What == "listsize") {
731 ListSize = getUnsignedIntegerOption("'set prompt' command", Options);
732 } else if (What == "prompt") {
733 // Include any trailing whitespace or other tokens, but not leading
735 Prompt = getToken(Options); // Strip leading whitespace
736 Prompt += Options; // Keep trailing whitespace or other stuff
738 // FIXME: Try to parse this as a source-language program expression.
739 throw "Don't know how to set '" + What + "'!";
743 void CLIDebugger::showCommand(std::string &Options) {
744 std::string What = getToken(Options);
746 if (What.empty() || !getToken(Options).empty())
747 throw "show command expects one argument.";
749 if (What == "language") {
750 std::cout << "The current source language is '";
752 std::cout << CurrentLanguage->getSourceLanguageName();
754 std::cout << "auto; currently "
755 << getCurrentLanguage().getSourceLanguageName();
757 } else if (What == "listsize") {
758 std::cout << "Number of source lines llvm-db will list by default is "
759 << ListSize << ".\n";
760 } else if (What == "prompt") {
761 std::cout << "llvm-db's prompt is \"" << Prompt << "\".\n";
763 throw "Unknown show command '" + What + "'. Try 'help show'.";
767 void CLIDebugger::helpCommand(std::string &Options) {
768 // Print out all of the commands in the CommandTable
769 std::string Command = getToken(Options);
770 if (!getToken(Options).empty())
771 throw "help command takes at most one argument.";
773 // Getting detailed help on a particular command?
774 if (!Command.empty()) {
775 CLICommand *C = getCommand(Command);
776 std::cout << C->getShortHelp() << ".\n" << C->getLongHelp();
778 // If there are aliases for this option, print them out.
779 const std::vector<std::string> &Names = C->getOptionNames();
780 if (Names.size() > 1) {
781 std::cout << "The '" << Command << "' command is known as: '"
783 for (unsigned i = 1, e = Names.size(); i != e; ++i)
784 std::cout << ", '" << Names[i] << "'";
789 unsigned MaxSize = 0;
790 for (std::map<std::string, CLICommand*>::iterator I = CommandTable.begin(),
791 E = CommandTable.end(); I != E; ++I)
792 if (I->first.size() > MaxSize &&
793 I->first == I->second->getPrimaryOptionName())
794 MaxSize = I->first.size();
796 // Loop over all of the commands, printing the short help version
797 for (std::map<std::string, CLICommand*>::iterator I = CommandTable.begin(),
798 E = CommandTable.end(); I != E; ++I)
799 if (I->first == I->second->getPrimaryOptionName())
800 std::cout << I->first << std::string(MaxSize - I->first.size(), ' ')
801 << " - " << I->second->getShortHelp() << "\n";