1 //===-- Commands.cpp - Implement various commands for the CLI -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file 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/Support/raw_ostream.h"
23 #include "llvm/ADT/StringExtras.h"
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
31 const SourceLanguage
&CLIDebugger::getCurrentLanguage() const {
32 // If the user explicitly switched languages with 'set language', use what
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();
42 // Otherwise, default to C like GDB apparently does.
43 return SourceLanguage::getCFamilyInstance();
47 /// startProgramRunning - If the program has been updated, reload it, then
48 /// start executing the program.
49 void CLIDebugger::startProgramRunning() {
52 // If the program has been modified, reload it!
53 sys::PathWithStatus
Program(Dbg
.getProgramPath());
55 const sys::FileStatus
*Status
= Program
.getFileStatus(false, &Err
);
58 if (TheProgramInfo
->getProgramTimeStamp() != Status
->getTimestamp()) {
59 outs() << "'" << Program
.str() << "' has changed; re-reading program.\n";
61 // Unload an existing program. This kills the program if necessary.
63 delete TheProgramInfo
;
67 Dbg
.loadProgram(Program
.str(), Context
);
68 TheProgramInfo
= new ProgramInfo(Dbg
.getProgram());
71 outs() << "Starting program: " << Dbg
.getProgramPath() << "\n";
74 // There was no current frame.
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;
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
,
95 if (CurLineNo
== LineNo
&& CurrentFile
== &CurSFI
->getSourceText())
99 outs() << "\t" << std::string(LineStart
, LineEnd
) << "\n";
103 /// printProgramLocation - Print a line of the place where the current stack
104 /// frame has stopped and the source line it is on.
106 void CLIDebugger::printProgramLocation(bool PrintLocation
) {
107 assert(Dbg
.isProgramLoaded() && Dbg
.isProgramRunning() &&
108 "Error program is not loaded and running!");
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
);
116 // If requested, print out some program information about WHERE we are.
118 // FIXME: print the current function arguments
119 if (const GlobalVariable
*FuncDesc
= SF
.getFunctionDesc())
120 outs() << getProgramInfo().getFunction(FuncDesc
).getSymbolicName();
122 outs() << "<unknown function>";
124 CurrentFile
= &FileDesc
->getSourceText();
126 outs() << " at " << CurrentFile
->getFilename() << ":" << LineNo
;
127 if (ColNo
) outs() << ":" << ColNo
;
131 if (printSourceLine(LineNo
))
132 outs() << "<could not load source file>\n";
134 LineListedStart
= LineNo
-ListSize
/2+1;
135 if ((int)LineListedStart
< 1) LineListedStart
= 1;
136 LineListedEnd
= LineListedStart
+1;
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
;
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!");
153 TheRuntimeInfo
= new RuntimeInfo(TheProgramInfo
, Dbg
.getRunningProcess());
155 // FIXME: if there are any breakpoints at the current location, print them as
158 // Since the program as successfully stopped, print its location.
159 void *CurrentFrame
= getRuntimeInfo().getCurrentFrame().getFrameID();
160 printProgramLocation(CurrentFrame
!= LastCurrentFrame
);
161 LastCurrentFrame
= CurrentFrame
;
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.";
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.";
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.
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
;
194 // If it was specified, add it back to the value we are parsing...
197 // And parse normally.
198 return getUnsignedIntegerOption(Msg
, Val
, isOnlyOption
);
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.
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());
217 //===----------------------------------------------------------------------===//
218 // Program startup and shutdown options
219 //===----------------------------------------------------------------------===//
222 /// file command - If the user specifies an option, search the PATH for the
223 /// specified program/bitcode file and load it. If the user does not specify
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.";
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?"))
235 // Unload an existing program. This kills the program if necessary.
237 delete TheProgramInfo
;
242 // If requested, start the new program.
244 outs() << "Unloaded program.\n";
246 outs() << "Loading program... ";
248 Dbg
.loadProgram(Prog
, Context
);
249 assert(Dbg
.isProgramLoaded() &&
250 "loadProgram succeeded, but not program loaded!");
251 TheProgramInfo
= new ProgramInfo(Dbg
.getProgram());
252 outs() << "successfully loaded '" << Dbg
.getProgramPath() << "'!\n";
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?"))
265 // Start the program running.
266 startProgramRunning();
268 // The program stopped!
269 programStoppedSuccessfully();
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.";
278 if (askYesNo("Kill the program being debugged?"))
283 void CLIDebugger::quitCommand(std::string
&Options
) {
284 if (!getToken(Options
).empty())
285 throw "quit command does not take any arguments.";
287 if (Dbg
.isProgramRunning() &&
288 !askYesNo("The program is running. Exit anyway?"))
291 // Throw exception to get out of the user-input loop.
296 //===----------------------------------------------------------------------===//
297 // Program execution commands
298 //===----------------------------------------------------------------------===//
300 void CLIDebugger::runCommand(std::string
&Options
) {
301 if (!Dbg
.isProgramLoaded()) throw "No program loaded.";
302 if (Dbg
.isProgramRunning() &&
303 !askYesNo("The program is already running. Restart from the beginning?"))
306 // Parse all of the options to the run command, which specify program
307 // arguments to run with.
308 parseProgramOptions(Options
);
312 // Start the program running.
313 startProgramRunning();
315 // Start the program running...
317 contCommand(Options
);
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.";
328 // The program stopped!
329 programStoppedSuccessfully();
332 void CLIDebugger::stepCommand(std::string
&Options
) {
333 if (!Dbg
.isProgramRunning()) throw "Program is not running.";
335 // Figure out how many times to step.
337 getOptionalUnsignedIntegerOption("'step' command", 1, Options
);
341 // Step the specified number of times.
342 for (; Amount
; --Amount
)
345 // The program stopped!
346 programStoppedSuccessfully();
349 void CLIDebugger::nextCommand(std::string
&Options
) {
350 if (!Dbg
.isProgramRunning()) throw "Program is not running.";
352 getOptionalUnsignedIntegerOption("'next' command", 1, Options
);
356 for (; Amount
; --Amount
)
359 // The program stopped!
360 programStoppedSuccessfully();
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.";
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();
374 Dbg
.finishProgram(CurrentFrame
);
376 // The program stopped!
377 programStoppedSuccessfully();
380 //===----------------------------------------------------------------------===//
381 // Stack frame commands
382 //===----------------------------------------------------------------------===//
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!";
389 RuntimeInfo
&RI
= getRuntimeInfo();
390 ProgramInfo
&PI
= getProgramInfo();
393 for (unsigned i
= 0; ; ++i
) {
394 StackFrame
&SF
= RI
.getStackFrame(i
);
396 if (i
== RI
.getCurrentFrameIdx())
398 outs() << "\t" << SF
.getFrameID() << " in ";
399 if (const GlobalVariable
*G
= SF
.getFunctionDesc())
400 outs() << PI
.getFunction(G
).getSymbolicName();
402 unsigned LineNo
, ColNo
;
403 const SourceFileInfo
*SFI
;
404 SF
.getSourceLocation(LineNo
, ColNo
, SFI
);
405 if (!SFI
->getBaseName().empty()) {
406 outs() << " at " << SFI
->getBaseName();
408 outs() << ":" << LineNo
;
410 outs() << ":" << ColNo
;
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.
420 // Stop automatically when we run off the bottom of the stack.
424 void CLIDebugger::upCommand(std::string
&Options
) {
426 getOptionalUnsignedIntegerOption("'up' command", 1, Options
);
428 RuntimeInfo
&RI
= getRuntimeInfo();
429 unsigned CurFrame
= RI
.getCurrentFrameIdx();
431 // Check to see if we go can up the specified number of frames.
433 RI
.getStackFrame(CurFrame
+Num
);
436 throw "Initial frame selected; you cannot go up.";
438 throw "Cannot go up " + utostr(Num
) + " frames!";
441 RI
.setCurrentFrameIdx(CurFrame
+Num
);
442 printProgramLocation();
445 void CLIDebugger::downCommand(std::string
&Options
) {
447 getOptionalUnsignedIntegerOption("'down' command", 1, Options
);
449 RuntimeInfo
&RI
= getRuntimeInfo();
450 unsigned CurFrame
= RI
.getCurrentFrameIdx();
452 // Check to see if we can go up the specified number of frames.
453 if (CurFrame
< Num
) {
455 throw "Bottom (i.e., innermost) frame selected; you cannot go down.";
457 throw "Cannot go down " + utostr(Num
) + " frames!";
460 RI
.setCurrentFrameIdx(CurFrame
-Num
);
461 printProgramLocation();
464 void CLIDebugger::frameCommand(std::string
&Options
) {
465 RuntimeInfo
&RI
= getRuntimeInfo();
466 unsigned CurFrame
= RI
.getCurrentFrameIdx();
469 getOptionalUnsignedIntegerOption("'frame' command", CurFrame
, Options
);
471 // Check to see if we go to the specified frame.
472 RI
.getStackFrame(Num
);
474 RI
.setCurrentFrameIdx(Num
);
475 printProgramLocation();
479 //===----------------------------------------------------------------------===//
480 // Breakpoint related commands
481 //===----------------------------------------------------------------------===//
483 void CLIDebugger::breakCommand(std::string
&Options
) {
484 // Figure out where the user wants a breakpoint.
485 const SourceFile
*File
;
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
493 // Parse the line specifier.
494 parseLineSpec(Options
, File
, LineNo
);
496 // Build a line specifier for the current stack frame.
497 throw "FIXME: breaking at the current location is not implemented yet!";
500 if (!File
) File
= CurrentFile
;
502 throw "Unknown file to place breakpoint!";
504 errs() << "Break: " << File
->getFilename() << ":" << LineNo
<< "\n";
506 throw "breakpoints not implemented yet!";
509 //===----------------------------------------------------------------------===//
510 // Miscellaneous commands
511 //===----------------------------------------------------------------------===//
513 void CLIDebugger::infoCommand(std::string
&Options
) {
514 std::string What
= getToken(Options
);
516 if (What
.empty() || !getToken(Options
).empty()){
517 std::string
infoStr("info");
518 helpCommand(infoStr
);
522 if (What
== "frame") {
523 } else if (What
== "functions") {
524 const std::map
<const GlobalVariable
*, SourceFunctionInfo
*> &Functions
525 = getProgramInfo().getSourceFunctions();
526 outs() << "All defined functions:\n";
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
) {
530 outs() << I
->second
->getSymbolicName() << "\n";
533 } else if (What
== "source") {
534 if (CurrentFile
== 0)
535 throw "No current source file.";
537 // Get the SourceFile information for the current file.
538 const SourceFileInfo
&SF
=
539 getProgramInfo().getSourceFile(CurrentFile
->getDescriptor());
541 outs() << "Current source file is: " << SF
.getBaseName() << "\n"
542 << "Compilation directory is: " << SF
.getDirectory() << "\n";
543 if (unsigned NL
= CurrentFile
->getNumLines())
544 outs() << "Located in: " << CurrentFile
->getFilename() << "\n"
545 << "Contains " << NL
<< " lines\n";
547 outs() << "Could not find source file.\n";
548 outs() << "Source language is "
549 << SF
.getLanguage().getSourceLanguageName() << "\n";
551 } else if (What
== "sources") {
552 const std::map
<const GlobalVariable
*, SourceFileInfo
*> &SourceFiles
=
553 getProgramInfo().getSourceFiles();
554 outs() << "Source files for the program:\n";
555 for (std::map
<const GlobalVariable
*, SourceFileInfo
*>::const_iterator I
=
556 SourceFiles
.begin(), E
= SourceFiles
.end(); I
!= E
;) {
557 outs() << I
->second
->getDirectory() << "/"
558 << I
->second
->getBaseName();
560 if (I
!= E
) outs() << ", ";
563 } else if (What
== "target") {
564 outs() << Dbg
.getRunningProcess().getStatus();
566 // See if this is something handled by the current language.
567 if (getCurrentLanguage().printInfo(What
))
570 throw "Unknown info command '" + What
+ "'. Try 'help info'.";
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
,
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!";
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
);
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!";
607 // Ok, check to see if this is just a line number.
608 std::string Saved
= FirstPart
;
610 LineNo
= getUnsignedIntegerOption("", Saved
);
612 // Ok, it's not a valid line number. It must be a source-language
614 std::string Name
= getToken(FirstPart
);
615 if (!getToken(FirstPart
).empty())
616 throw "Extra junk in line specifier after '" + Name
+ "'.";
617 SourceFunctionInfo
*SFI
=
618 getCurrentLanguage().lookupFunction(Name
, getProgramInfo(),
621 throw "Unknown identifier '" + Name
+ "'.";
624 SFI
->getSourceLocation(L
, C
);
625 if (L
== 0) throw "Could not locate '" + Name
+ "'!";
627 SourceFile
= &SFI
->getSourceFile().getSourceText();
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!";
639 // Next, check to see if this is just a line number.
640 std::string Saved
= SecondPart
;
642 LineNo
= getUnsignedIntegerOption("", Saved
);
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!";
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();
655 void CLIDebugger::listCommand(std::string
&Options
) {
656 if (!Dbg
.isProgramLoaded())
657 throw "No program is loaded. Use the 'file' command.";
659 // Handle "list foo," correctly, by returning " " as the second token
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!";
667 // StartLine, EndLine - The starting and ending line numbers to print.
668 unsigned StartLine
= 0, EndLine
= 0;
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
;
680 StartLine
= LineListedStart
-ListSize
;
681 EndLine
= LineListedStart
;
682 if ((int)StartLine
<= 0) StartLine
= 1;
685 // Must be a normal line specifier.
686 const SourceFile
*File
;
688 parseLineSpec(FirstLineSpec
, File
, LineNo
);
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;
694 if ((int)StartLine
<= 0) StartLine
= 1;
695 EndLine
= StartLine
+ ListSize
;
699 // Parse two line specifiers...
700 const SourceFile
*StartFile
, *EndFile
;
701 unsigned StartLineNo
, EndLineNo
;
702 parseLineSpec(FirstLineSpec
, StartFile
, StartLineNo
);
703 unsigned SavedLLE
= LineListedEnd
;
704 LineListedEnd
= StartLineNo
;
706 parseLineSpec(SecondLineSpec
, EndFile
, EndLineNo
);
708 LineListedEnd
= SavedLLE
;
712 // Inherit file specified by the first line spec if there was one.
713 if (EndFile
== 0) EndFile
= StartFile
;
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;
722 assert((int)StartLine
> 0 && (int)EndLine
> 0 && StartLine
<= EndLine
&&
723 "Error reading line specifiers!");
725 // If there was no current file, and the user didn't specify one to list, we
727 if (CurrentFile
== 0)
728 throw "There is no current file to list.";
730 // Remember for next time.
731 LineListedStart
= StartLine
;
732 LineListedEnd
= StartLine
;
734 for (unsigned LineNo
= StartLine
; LineNo
!= EndLine
; ++LineNo
) {
735 // Print the source line, unless it is invalid.
736 if (printSourceLine(LineNo
))
738 LineListedEnd
= LineNo
+1;
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
745 const char *LineStart
, *LineEnd
;
746 CurrentFile
->getSourceLine(0, LineStart
, LineEnd
);
748 throw "Could not load source file '" + CurrentFile
->getFilename() + "'!";
750 outs() << "<end of file>\n";
754 void CLIDebugger::setCommand(std::string
&Options
) {
755 std::string What
= getToken(Options
);
758 throw "set command expects at least two arguments.";
759 if (What
== "args") {
760 parseProgramOptions(Options
);
761 } else if (What
== "language") {
762 std::string Lang
= getToken(Options
);
763 if (!getToken(Options
).empty())
764 throw "set language expects one argument at most.";
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";
771 } else if (Lang
== "local" || Lang
== "auto") {
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();
780 throw "Unknown language '" + Lang
+ "'.";
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
788 Prompt
= getToken(Options
); // Strip leading whitespace
789 Prompt
+= Options
; // Keep trailing whitespace or other stuff
791 // FIXME: Try to parse this as a source-language program expression.
792 throw "Don't know how to set '" + What
+ "'!";
796 void CLIDebugger::showCommand(std::string
&Options
) {
797 std::string What
= getToken(Options
);
799 if (What
.empty() || !getToken(Options
).empty())
800 throw "show command expects one argument.";
802 if (What
== "args") {
803 outs() << "Argument list to give program when started is \"";
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
808 for (unsigned i
= 0, e
= Dbg
.getNumProgramArguments(); i
!= e
; ++i
)
809 outs() << (i
? " " : "") << Dbg
.getProgramArgument(i
);
812 } else if (What
== "language") {
813 outs() << "The current source language is '";
815 outs() << CurrentLanguage
->getSourceLanguageName();
817 outs() << "auto; currently "
818 << getCurrentLanguage().getSourceLanguageName();
820 } else if (What
== "listsize") {
821 outs() << "Number of source lines llvm-db will list by default is "
822 << ListSize
<< ".\n";
823 } else if (What
== "prompt") {
824 outs() << "llvm-db's prompt is \"" << Prompt
<< "\".\n";
826 throw "Unknown show command '" + What
+ "'. Try 'help show'.";
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.";
836 // Getting detailed help on a particular command?
837 if (!Command
.empty()) {
838 CLICommand
*C
= getCommand(Command
);
839 outs() << C
->getShortHelp() << ".\n" << C
->getLongHelp();
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) {
844 outs() << "The '" << Command
<< "' command is known as: '"
846 for (unsigned i
= 1, e
= Names
.size(); i
!= e
; ++i
)
847 outs() << ", '" << Names
[i
] << "'";
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();
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())
863 outs() << I
->first
<< std::string(MaxSize
- I
->first
.size(), ' ')
864 << " - " << I
->second
->getShortHelp() << "\n";