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 "llvm/Support/FileUtilities.h"
22 #include "llvm/ADT/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 sys::Path Program(Dbg.getProgramPath());
54 const sys::FileStatus *Status = Program.getFileStatus(false, &Err);
57 if (TheProgramInfo->getProgramTimeStamp() != Status->getTimestamp()) {
58 std::cout << "'" << Program << "' has changed; re-reading program.\n";
60 // Unload an existing program. This kills the program if necessary.
62 delete TheProgramInfo;
66 Dbg.loadProgram(Program.toString());
67 TheProgramInfo = new ProgramInfo(Dbg.getProgram());
70 std::cout << "Starting program: " << Dbg.getProgramPath() << "\n";
73 // There was no current frame.
77 /// printSourceLine - Print the specified line of the current source file.
78 /// If the specified line is invalid (the source file could not be loaded or
79 /// the line number is out of range), don't print anything, but return true.
80 bool CLIDebugger::printSourceLine(unsigned LineNo) {
81 assert(CurrentFile && "There is no current source file to print!");
82 const char *LineStart, *LineEnd;
83 CurrentFile->getSourceLine(LineNo-1, LineStart, LineEnd);
84 if (LineStart == 0) return true;
87 // If this is the line the program is currently stopped at, print a marker.
88 if (Dbg.isProgramRunning()) {
89 unsigned CurLineNo, CurColNo;
90 const SourceFileInfo *CurSFI;
91 getRuntimeInfo().getCurrentFrame().getSourceLocation(CurLineNo, CurColNo,
94 if (CurLineNo == LineNo && CurrentFile == &CurSFI->getSourceText())
98 std::cout << "\t" << std::string(LineStart, LineEnd) << "\n";
102 /// printProgramLocation - Print a line of the place where the current stack
103 /// frame has stopped and the source line it is on.
105 void CLIDebugger::printProgramLocation(bool PrintLocation) {
106 assert(Dbg.isProgramLoaded() && Dbg.isProgramRunning() &&
107 "Error program is not loaded and running!");
109 // Figure out where the program stopped...
110 StackFrame &SF = getRuntimeInfo().getCurrentFrame();
111 unsigned LineNo, ColNo;
112 const SourceFileInfo *FileDesc;
113 SF.getSourceLocation(LineNo, ColNo, FileDesc);
115 // If requested, print out some program information about WHERE we are.
117 // FIXME: print the current function arguments
118 if (const GlobalVariable *FuncDesc = SF.getFunctionDesc())
119 std::cout << getProgramInfo().getFunction(FuncDesc).getSymbolicName();
121 std::cout << "<unknown function>";
123 CurrentFile = &FileDesc->getSourceText();
125 std::cout << " at " << CurrentFile->getFilename() << ":" << LineNo;
126 if (ColNo) std::cout << ":" << ColNo;
130 if (printSourceLine(LineNo))
131 std::cout << "<could not load source file>\n";
133 LineListedStart = LineNo-ListSize/2+1;
134 if ((int)LineListedStart < 1) LineListedStart = 1;
135 LineListedEnd = LineListedStart+1;
139 /// eliminateRunInfo - We are about to run the program. Forget any state
140 /// about how the program used to be stopped.
141 void CLIDebugger::eliminateRunInfo() {
142 delete TheRuntimeInfo;
146 /// programStoppedSuccessfully - This method updates internal data
147 /// structures to reflect the fact that the program just executed a while,
148 /// and has successfully stopped.
149 void CLIDebugger::programStoppedSuccessfully() {
150 assert(TheRuntimeInfo==0 && "Someone forgot to release the old RuntimeInfo!");
152 TheRuntimeInfo = new RuntimeInfo(TheProgramInfo, Dbg.getRunningProcess());
154 // FIXME: if there are any breakpoints at the current location, print them as
157 // Since the program as successfully stopped, print its location.
158 void *CurrentFrame = getRuntimeInfo().getCurrentFrame().getFrameID();
159 printProgramLocation(CurrentFrame != LastCurrentFrame);
160 LastCurrentFrame = CurrentFrame;
165 /// getUnsignedIntegerOption - Get an unsigned integer number from the Val
166 /// string. Check to make sure that the string contains an unsigned integer
167 /// token, and if not, throw an exception. If isOnlyOption is set, also throw
168 /// an exception if there is extra junk at the end of the string.
169 static unsigned getUnsignedIntegerOption(const char *Msg, std::string &Val,
170 bool isOnlyOption = true) {
171 std::string Tok = getToken(Val);
172 if (Tok.empty() || (isOnlyOption && !getToken(Val).empty()))
173 throw std::string(Msg) + " expects an unsigned integer argument.";
176 unsigned Result = strtoul(Tok.c_str(), &EndPtr, 0);
177 if (EndPtr != Tok.c_str()+Tok.size())
178 throw std::string(Msg) + " expects an unsigned integer argument.";
183 /// getOptionalUnsignedIntegerOption - This method is just like
184 /// getUnsignedIntegerOption, but if the argument value is not specified, a
185 /// default is returned instead of causing an error.
187 getOptionalUnsignedIntegerOption(const char *Msg, unsigned Default,
188 std::string &Val, bool isOnlyOption = true) {
189 // Check to see if the value was specified...
190 std::string TokVal = getToken(Val);
191 if (TokVal.empty()) return Default;
193 // If it was specified, add it back to the value we are parsing...
196 // And parse normally.
197 return getUnsignedIntegerOption(Msg, Val, isOnlyOption);
201 /// parseProgramOptions - This method parses the Options string and loads it
202 /// as options to be passed to the program. This is used by the run command
203 /// and by 'set args'.
204 void CLIDebugger::parseProgramOptions(std::string &Options) {
205 // FIXME: tokenizing by whitespace is clearly incorrect. Instead we should
206 // honor quotes and other things that a shell would. Also in the future we
207 // should support redirection of standard IO.
209 std::vector<std::string> Arguments;
210 for (std::string A = getToken(Options); !A.empty(); A = getToken(Options))
211 Arguments.push_back(A);
212 Dbg.setProgramArguments(Arguments.begin(), Arguments.end());
216 //===----------------------------------------------------------------------===//
217 // Program startup and shutdown options
218 //===----------------------------------------------------------------------===//
221 /// file command - If the user specifies an option, search the PATH for the
222 /// specified program/bytecode file and load it. If the user does not specify
223 /// an option, unload the current program.
224 void CLIDebugger::fileCommand(std::string &Options) {
225 std::string Prog = getToken(Options);
226 if (!getToken(Options).empty())
227 throw "file command takes at most one argument.";
229 // Check to make sure the user knows what they are doing
230 if (Dbg.isProgramRunning() &&
231 !askYesNo("A program is already loaded. Kill it?"))
234 // Unload an existing program. This kills the program if necessary.
236 delete TheProgramInfo;
241 // If requested, start the new program.
243 std::cout << "Unloaded program.\n";
245 std::cout << "Loading program... " << std::flush;
246 Dbg.loadProgram(Prog);
247 assert(Dbg.isProgramLoaded() &&
248 "loadProgram succeeded, but not program loaded!");
249 TheProgramInfo = new ProgramInfo(Dbg.getProgram());
250 std::cout << "successfully loaded '" << Dbg.getProgramPath() << "'!\n";
255 void CLIDebugger::createCommand(std::string &Options) {
256 if (!getToken(Options).empty())
257 throw "create command does not take any arguments.";
258 if (!Dbg.isProgramLoaded()) throw "No program loaded.";
259 if (Dbg.isProgramRunning() &&
260 !askYesNo("The program is already running. Restart from the beginning?"))
263 // Start the program running.
264 startProgramRunning();
266 // The program stopped!
267 programStoppedSuccessfully();
270 void CLIDebugger::killCommand(std::string &Options) {
271 if (!getToken(Options).empty())
272 throw "kill command does not take any arguments.";
273 if (!Dbg.isProgramRunning())
274 throw "No program is currently being run.";
276 if (askYesNo("Kill the program being debugged?"))
281 void CLIDebugger::quitCommand(std::string &Options) {
282 if (!getToken(Options).empty())
283 throw "quit command does not take any arguments.";
285 if (Dbg.isProgramRunning() &&
286 !askYesNo("The program is running. Exit anyway?"))
289 // Throw exception to get out of the user-input loop.
294 //===----------------------------------------------------------------------===//
295 // Program execution commands
296 //===----------------------------------------------------------------------===//
298 void CLIDebugger::runCommand(std::string &Options) {
299 if (!Dbg.isProgramLoaded()) throw "No program loaded.";
300 if (Dbg.isProgramRunning() &&
301 !askYesNo("The program is already running. Restart from the beginning?"))
304 // Parse all of the options to the run command, which specify program
305 // arguments to run with.
306 parseProgramOptions(Options);
310 // Start the program running.
311 startProgramRunning();
313 // Start the program running...
315 contCommand(Options);
318 void CLIDebugger::contCommand(std::string &Options) {
319 if (!getToken(Options).empty()) throw "cont argument not supported yet.";
320 if (!Dbg.isProgramRunning()) throw "Program is not running.";
326 // The program stopped!
327 programStoppedSuccessfully();
330 void CLIDebugger::stepCommand(std::string &Options) {
331 if (!Dbg.isProgramRunning()) throw "Program is not running.";
333 // Figure out how many times to step.
335 getOptionalUnsignedIntegerOption("'step' command", 1, Options);
339 // Step the specified number of times.
340 for (; Amount; --Amount)
343 // The program stopped!
344 programStoppedSuccessfully();
347 void CLIDebugger::nextCommand(std::string &Options) {
348 if (!Dbg.isProgramRunning()) throw "Program is not running.";
350 getOptionalUnsignedIntegerOption("'next' command", 1, Options);
354 for (; Amount; --Amount)
357 // The program stopped!
358 programStoppedSuccessfully();
361 void CLIDebugger::finishCommand(std::string &Options) {
362 if (!getToken(Options).empty())
363 throw "finish command does not take any arguments.";
364 if (!Dbg.isProgramRunning()) throw "Program is not running.";
366 // Figure out where we are exactly. If the user requests that we return from
367 // a frame that is not the top frame, make sure we get it.
368 void *CurrentFrame = getRuntimeInfo().getCurrentFrame().getFrameID();
372 Dbg.finishProgram(CurrentFrame);
374 // The program stopped!
375 programStoppedSuccessfully();
378 //===----------------------------------------------------------------------===//
379 // Stack frame commands
380 //===----------------------------------------------------------------------===//
382 void CLIDebugger::backtraceCommand(std::string &Options) {
383 // Accepts "full", n, -n
384 if (!getToken(Options).empty())
385 throw "FIXME: bt command argument not implemented yet!";
387 RuntimeInfo &RI = getRuntimeInfo();
388 ProgramInfo &PI = getProgramInfo();
391 for (unsigned i = 0; ; ++i) {
392 StackFrame &SF = RI.getStackFrame(i);
393 std::cout << "#" << i;
394 if (i == RI.getCurrentFrameIdx())
396 std::cout << "\t" << SF.getFrameID() << " in ";
397 if (const GlobalVariable *G = SF.getFunctionDesc())
398 std::cout << PI.getFunction(G).getSymbolicName();
400 unsigned LineNo, ColNo;
401 const SourceFileInfo *SFI;
402 SF.getSourceLocation(LineNo, ColNo, SFI);
403 if (!SFI->getBaseName().empty()) {
404 std::cout << " at " << SFI->getBaseName();
406 std::cout << ":" << LineNo;
408 std::cout << ":" << ColNo;
412 // FIXME: when we support shared libraries, we should print ' from foo.so'
413 // if the stack frame is from a different object than the current one.
418 // Stop automatically when we run off the bottom of the stack.
422 void CLIDebugger::upCommand(std::string &Options) {
424 getOptionalUnsignedIntegerOption("'up' command", 1, Options);
426 RuntimeInfo &RI = getRuntimeInfo();
427 unsigned CurFrame = RI.getCurrentFrameIdx();
429 // Check to see if we go can up the specified number of frames.
431 RI.getStackFrame(CurFrame+Num);
434 throw "Initial frame selected; you cannot go up.";
436 throw "Cannot go up " + utostr(Num) + " frames!";
439 RI.setCurrentFrameIdx(CurFrame+Num);
440 printProgramLocation();
443 void CLIDebugger::downCommand(std::string &Options) {
445 getOptionalUnsignedIntegerOption("'down' command", 1, Options);
447 RuntimeInfo &RI = getRuntimeInfo();
448 unsigned CurFrame = RI.getCurrentFrameIdx();
450 // Check to see if we can go up the specified number of frames.
453 throw "Bottom (i.e., innermost) frame selected; you cannot go down.";
455 throw "Cannot go down " + utostr(Num) + " frames!";
457 RI.setCurrentFrameIdx(CurFrame-Num);
458 printProgramLocation();
461 void CLIDebugger::frameCommand(std::string &Options) {
462 RuntimeInfo &RI = getRuntimeInfo();
463 unsigned CurFrame = RI.getCurrentFrameIdx();
466 getOptionalUnsignedIntegerOption("'frame' command", CurFrame, Options);
468 // Check to see if we go to the specified frame.
469 RI.getStackFrame(Num);
471 RI.setCurrentFrameIdx(Num);
472 printProgramLocation();
476 //===----------------------------------------------------------------------===//
477 // Breakpoint related commands
478 //===----------------------------------------------------------------------===//
480 void CLIDebugger::breakCommand(std::string &Options) {
481 // Figure out where the user wants a breakpoint.
482 const SourceFile *File;
485 // Check to see if the user specified a line specifier.
486 std::string Option = getToken(Options); // strip whitespace
487 if (!Option.empty()) {
488 Options = Option + Options; // reconstruct string
490 // Parse the line specifier.
491 parseLineSpec(Options, File, LineNo);
493 // Build a line specifier for the current stack frame.
494 throw "FIXME: breaking at the current location is not implemented yet!";
497 if (!File) File = CurrentFile;
499 throw "Unknown file to place breakpoint!";
501 std::cerr << "Break: " << File->getFilename() << ":" << LineNo << "\n";
503 throw "breakpoints not implemented yet!";
506 //===----------------------------------------------------------------------===//
507 // Miscellaneous commands
508 //===----------------------------------------------------------------------===//
510 void CLIDebugger::infoCommand(std::string &Options) {
511 std::string What = getToken(Options);
513 if (What.empty() || !getToken(Options).empty()){
514 std::string infoStr("info");
515 helpCommand(infoStr);
519 if (What == "frame") {
520 } else if (What == "functions") {
521 const std::map<const GlobalVariable*, SourceFunctionInfo*> &Functions
522 = getProgramInfo().getSourceFunctions();
523 std::cout << "All defined functions:\n";
524 // FIXME: GDB groups these by source file. We could do that I guess.
525 for (std::map<const GlobalVariable*, SourceFunctionInfo*>::const_iterator
526 I = Functions.begin(), E = Functions.end(); I != E; ++I) {
527 std::cout << I->second->getSymbolicName() << "\n";
530 } else if (What == "source") {
531 if (CurrentFile == 0)
532 throw "No current source file.";
534 // Get the SourceFile information for the current file.
535 const SourceFileInfo &SF =
536 getProgramInfo().getSourceFile(CurrentFile->getDescriptor());
538 std::cout << "Current source file is: " << SF.getBaseName() << "\n"
539 << "Compilation directory is: " << SF.getDirectory() << "\n";
540 if (unsigned NL = CurrentFile->getNumLines())
541 std::cout << "Located in: " << CurrentFile->getFilename() << "\n"
542 << "Contains " << NL << " lines\n";
544 std::cout << "Could not find source file.\n";
545 std::cout << "Source language is "
546 << SF.getLanguage().getSourceLanguageName() << "\n";
548 } else if (What == "sources") {
549 const std::map<const GlobalVariable*, SourceFileInfo*> &SourceFiles =
550 getProgramInfo().getSourceFiles();
551 std::cout << "Source files for the program:\n";
552 for (std::map<const GlobalVariable*, SourceFileInfo*>::const_iterator I =
553 SourceFiles.begin(), E = SourceFiles.end(); I != E;) {
554 std::cout << I->second->getDirectory() << "/"
555 << I->second->getBaseName();
557 if (I != E) std::cout << ", ";
560 } else if (What == "target") {
561 std::cout << Dbg.getRunningProcess().getStatus();
563 // See if this is something handled by the current language.
564 if (getCurrentLanguage().printInfo(What))
567 throw "Unknown info command '" + What + "'. Try 'help info'.";
571 /// parseLineSpec - Parses a line specifier, for use by the 'list' command.
572 /// If SourceFile is returned as a void pointer, then it was not specified.
573 /// If the line specifier is invalid, an exception is thrown.
574 void CLIDebugger::parseLineSpec(std::string &LineSpec,
575 const SourceFile *&SourceFile,
580 // First, check to see if we have a : separator.
581 std::string FirstPart = getToken(LineSpec, ":");
582 std::string SecondPart = getToken(LineSpec, ":");
583 if (!getToken(LineSpec).empty()) throw "Malformed line specification!";
585 // If there is no second part, we must have either "function", "number",
586 // "+offset", or "-offset".
587 if (SecondPart.empty()) {
588 if (FirstPart.empty()) throw "Malformed line specification!";
589 if (FirstPart[0] == '+') {
590 FirstPart.erase(FirstPart.begin(), FirstPart.begin()+1);
591 // For +n, return LineListedEnd+n
592 LineNo = LineListedEnd +
593 getUnsignedIntegerOption("Line specifier '+'", FirstPart);
595 } else if (FirstPart[0] == '-') {
596 FirstPart.erase(FirstPart.begin(), FirstPart.begin()+1);
597 // For -n, return LineListedEnd-n
598 LineNo = LineListedEnd -
599 getUnsignedIntegerOption("Line specifier '-'", FirstPart);
600 if ((int)LineNo < 1) LineNo = 1;
601 } else if (FirstPart[0] == '*') {
602 throw "Address expressions not supported as source locations!";
604 // Ok, check to see if this is just a line number.
605 std::string Saved = FirstPart;
607 LineNo = getUnsignedIntegerOption("", Saved);
609 // Ok, it's not a valid line number. It must be a source-language
611 std::string Name = getToken(FirstPart);
612 if (!getToken(FirstPart).empty())
613 throw "Extra junk in line specifier after '" + Name + "'.";
614 SourceFunctionInfo *SFI =
615 getCurrentLanguage().lookupFunction(Name, getProgramInfo(),
618 throw "Unknown identifier '" + Name + "'.";
621 SFI->getSourceLocation(L, C);
622 if (L == 0) throw "Could not locate '" + Name + "'!";
624 SourceFile = &SFI->getSourceFile().getSourceText();
630 // Ok, this must be a filename qualified line number or function name.
631 // First, figure out the source filename.
632 std::string SourceFilename = getToken(FirstPart);
633 if (!getToken(FirstPart).empty())
634 throw "Invalid filename qualified source location!";
636 // Next, check to see if this is just a line number.
637 std::string Saved = SecondPart;
639 LineNo = getUnsignedIntegerOption("", Saved);
641 // Ok, it's not a valid line number. It must be a function name.
642 throw "FIXME: Filename qualified function names are not support "
643 "as line specifiers yet!";
646 // Ok, we got the line number. Now check out the source file name to make
647 // sure it's all good. If it is, return it. If not, throw exception.
648 SourceFile =&getProgramInfo().getSourceFile(SourceFilename).getSourceText();
652 void CLIDebugger::listCommand(std::string &Options) {
653 if (!Dbg.isProgramLoaded())
654 throw "No program is loaded. Use the 'file' command.";
656 // Handle "list foo," correctly, by returning " " as the second token
659 std::string FirstLineSpec = getToken(Options, ",");
660 std::string SecondLineSpec = getToken(Options, ",");
661 if (!getToken(Options, ",").empty())
662 throw "list command only expects two source location specifiers!";
664 // StartLine, EndLine - The starting and ending line numbers to print.
665 unsigned StartLine = 0, EndLine = 0;
667 if (SecondLineSpec.empty()) { // No second line specifier provided?
668 // Handle special forms like "", "+", "-", etc.
669 std::string TmpSpec = FirstLineSpec;
670 std::string Tok = getToken(TmpSpec);
671 if (getToken(TmpSpec).empty() && (Tok == "" || Tok == "+" || Tok == "-")) {
672 if (Tok == "+" || Tok == "") {
673 StartLine = LineListedEnd;
674 EndLine = StartLine + ListSize;
677 StartLine = LineListedStart-ListSize;
678 EndLine = LineListedStart;
679 if ((int)StartLine <= 0) StartLine = 1;
682 // Must be a normal line specifier.
683 const SourceFile *File;
685 parseLineSpec(FirstLineSpec, File, LineNo);
687 // If the user only specified one file specifier, we should display
688 // ListSize lines centered at the specified line.
689 if (File != 0) CurrentFile = File;
690 StartLine = LineNo - (ListSize+1)/2;
691 if ((int)StartLine <= 0) StartLine = 1;
692 EndLine = StartLine + ListSize;
696 // Parse two line specifiers...
697 const SourceFile *StartFile, *EndFile;
698 unsigned StartLineNo, EndLineNo;
699 parseLineSpec(FirstLineSpec, StartFile, StartLineNo);
700 unsigned SavedLLE = LineListedEnd;
701 LineListedEnd = StartLineNo;
703 parseLineSpec(SecondLineSpec, EndFile, EndLineNo);
705 LineListedEnd = SavedLLE;
709 // Inherit file specified by the first line spec if there was one.
710 if (EndFile == 0) EndFile = StartFile;
712 if (StartFile != EndFile)
713 throw "Start and end line specifiers are in different files!";
714 CurrentFile = StartFile;
715 StartLine = StartLineNo;
716 EndLine = EndLineNo+1;
719 assert((int)StartLine > 0 && (int)EndLine > 0 && StartLine <= EndLine &&
720 "Error reading line specifiers!");
722 // If there was no current file, and the user didn't specify one to list, we
724 if (CurrentFile == 0)
725 throw "There is no current file to list.";
727 // Remember for next time.
728 LineListedStart = StartLine;
729 LineListedEnd = StartLine;
731 for (unsigned LineNo = StartLine; LineNo != EndLine; ++LineNo) {
732 // Print the source line, unless it is invalid.
733 if (printSourceLine(LineNo))
735 LineListedEnd = LineNo+1;
738 // If we didn't print any lines, find out why.
739 if (LineListedEnd == StartLine) {
740 // See if we can read line #0 from the file, if not, we couldn't load the
742 const char *LineStart, *LineEnd;
743 CurrentFile->getSourceLine(0, LineStart, LineEnd);
745 throw "Could not load source file '" + CurrentFile->getFilename() + "'!";
747 std::cout << "<end of file>\n";
751 void CLIDebugger::setCommand(std::string &Options) {
752 std::string What = getToken(Options);
755 throw "set command expects at least two arguments.";
756 if (What == "args") {
757 parseProgramOptions(Options);
758 } else if (What == "language") {
759 std::string Lang = getToken(Options);
760 if (!getToken(Options).empty())
761 throw "set language expects one argument at most.";
763 std::cout << "The currently understood settings are:\n\n"
764 << "local or auto Automatic setting based on source file\n"
765 << "c Use the C language\n"
766 << "c++ Use the C++ language\n"
767 << "unknown Use when source language is not supported\n";
768 } else if (Lang == "local" || Lang == "auto") {
770 } else if (Lang == "c") {
771 CurrentLanguage = &SourceLanguage::getCFamilyInstance();
772 } else if (Lang == "c++") {
773 CurrentLanguage = &SourceLanguage::getCPlusPlusInstance();
774 } else if (Lang == "unknown") {
775 CurrentLanguage = &SourceLanguage::getUnknownLanguageInstance();
777 throw "Unknown language '" + Lang + "'.";
780 } else if (What == "listsize") {
781 ListSize = getUnsignedIntegerOption("'set prompt' command", Options);
782 } else if (What == "prompt") {
783 // Include any trailing whitespace or other tokens, but not leading
785 Prompt = getToken(Options); // Strip leading whitespace
786 Prompt += Options; // Keep trailing whitespace or other stuff
788 // FIXME: Try to parse this as a source-language program expression.
789 throw "Don't know how to set '" + What + "'!";
793 void CLIDebugger::showCommand(std::string &Options) {
794 std::string What = getToken(Options);
796 if (What.empty() || !getToken(Options).empty())
797 throw "show command expects one argument.";
799 if (What == "args") {
800 std::cout << "Argument list to give program when started is \"";
801 // FIXME: This doesn't print stuff correctly if the arguments have spaces in
802 // them, but currently the only way to get that is to use the --args command
803 // line argument. This should really handle escaping all hard characters as
805 for (unsigned i = 0, e = Dbg.getNumProgramArguments(); i != e; ++i)
806 std::cout << (i ? " " : "") << Dbg.getProgramArgument(i);
809 } else if (What == "language") {
810 std::cout << "The current source language is '";
812 std::cout << CurrentLanguage->getSourceLanguageName();
814 std::cout << "auto; currently "
815 << getCurrentLanguage().getSourceLanguageName();
817 } else if (What == "listsize") {
818 std::cout << "Number of source lines llvm-db will list by default is "
819 << ListSize << ".\n";
820 } else if (What == "prompt") {
821 std::cout << "llvm-db's prompt is \"" << Prompt << "\".\n";
823 throw "Unknown show command '" + What + "'. Try 'help show'.";
827 void CLIDebugger::helpCommand(std::string &Options) {
828 // Print out all of the commands in the CommandTable
829 std::string Command = getToken(Options);
830 if (!getToken(Options).empty())
831 throw "help command takes at most one argument.";
833 // Getting detailed help on a particular command?
834 if (!Command.empty()) {
835 CLICommand *C = getCommand(Command);
836 std::cout << C->getShortHelp() << ".\n" << C->getLongHelp();
838 // If there are aliases for this option, print them out.
839 const std::vector<std::string> &Names = C->getOptionNames();
840 if (Names.size() > 1) {
841 std::cout << "The '" << Command << "' command is known as: '"
843 for (unsigned i = 1, e = Names.size(); i != e; ++i)
844 std::cout << ", '" << Names[i] << "'";
849 unsigned MaxSize = 0;
850 for (std::map<std::string, CLICommand*>::iterator I = CommandTable.begin(),
851 E = CommandTable.end(); I != E; ++I)
852 if (I->first.size() > MaxSize &&
853 I->first == I->second->getPrimaryOptionName())
854 MaxSize = I->first.size();
856 // Loop over all of the commands, printing the short help version
857 for (std::map<std::string, CLICommand*>::iterator I = CommandTable.begin(),
858 E = CommandTable.end(); I != E; ++I)
859 if (I->first == I->second->getPrimaryOptionName())
860 std::cout << I->first << std::string(MaxSize - I->first.size(), ' ')
861 << " - " << I->second->getShortHelp() << "\n";