blob: d759dddecd379631f00985a2928a7d87a7878174 [file] [log] [blame]
Chris Lattner7af5c122004-01-05 05:27:31 +00001//===-- Commands.cpp - Implement various commands for the CLI -------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
Chris Lattner7af5c122004-01-05 05:27:31 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
Chris Lattner7af5c122004-01-05 05:27:31 +00008//===----------------------------------------------------------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00009//
Chris Lattner7af5c122004-01-05 05:27:31 +000010// 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 Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/Support/FileUtilities.h"
Chris Lattner74382b72009-08-23 22:45:37 +000022#include "llvm/Support/raw_ostream.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/ADT/StringExtras.h"
Anton Korobeynikova27694d2008-02-20 11:27:04 +000024#include <cstdlib>
Chris Lattner7af5c122004-01-05 05:27:31 +000025using namespace llvm;
26
27/// getCurrentLanguage - Return the current source language that the user is
28/// playing around with. This is aquired from the current stack frame of a
29/// running program if one exists, but this value can be explicitly set by the
30/// user as well.
31const SourceLanguage &CLIDebugger::getCurrentLanguage() const {
32 // If the user explicitly switched languages with 'set language', use what
33 // they asked for.
34 if (CurrentLanguage) {
35 return *CurrentLanguage;
36 } else if (Dbg.isProgramRunning()) {
37 // Otherwise, if the program is running, infer the current language from it.
38 const GlobalVariable *FuncDesc =
39 getRuntimeInfo().getCurrentFrame().getFunctionDesc();
40 return getProgramInfo().getFunction(FuncDesc).getSourceFile().getLanguage();
41 } else {
42 // Otherwise, default to C like GDB apparently does.
43 return SourceLanguage::getCFamilyInstance();
44 }
45}
46
47/// startProgramRunning - If the program has been updated, reload it, then
48/// start executing the program.
49void CLIDebugger::startProgramRunning() {
50 eliminateRunInfo();
51
52 // If the program has been modified, reload it!
Reid Spencer7b57fe32007-04-08 20:06:05 +000053 sys::PathWithStatus Program(Dbg.getProgramPath());
Chris Lattner252ad032006-07-28 22:03:44 +000054 std::string Err;
Reid Spencer7b57fe32007-04-08 20:06:05 +000055 const sys::FileStatus *Status = Program.getFileStatus(false, &Err);
Reid Spencer8475ec02007-03-29 19:05:44 +000056 if (!Status)
Chris Lattner252ad032006-07-28 22:03:44 +000057 throw Err;
Reid Spencer8475ec02007-03-29 19:05:44 +000058 if (TheProgramInfo->getProgramTimeStamp() != Status->getTimestamp()) {
Chris Lattner74382b72009-08-23 22:45:37 +000059 outs() << "'" << Program.str() << "' has changed; re-reading program.\n";
Chris Lattner7af5c122004-01-05 05:27:31 +000060
61 // Unload an existing program. This kills the program if necessary.
62 Dbg.unloadProgram();
63 delete TheProgramInfo;
64 TheProgramInfo = 0;
65 CurrentFile = 0;
66
Chris Lattner74382b72009-08-23 22:45:37 +000067 Dbg.loadProgram(Program.str(), Context);
Chris Lattner7af5c122004-01-05 05:27:31 +000068 TheProgramInfo = new ProgramInfo(Dbg.getProgram());
69 }
70
Chris Lattner74382b72009-08-23 22:45:37 +000071 outs() << "Starting program: " << Dbg.getProgramPath() << "\n";
Chris Lattner7af5c122004-01-05 05:27:31 +000072 Dbg.createProgram();
73
74 // There was no current frame.
75 LastCurrentFrame = 0;
76}
77
78/// printSourceLine - Print the specified line of the current source file.
79/// If the specified line is invalid (the source file could not be loaded or
80/// the line number is out of range), don't print anything, but return true.
81bool CLIDebugger::printSourceLine(unsigned LineNo) {
82 assert(CurrentFile && "There is no current source file to print!");
83 const char *LineStart, *LineEnd;
84 CurrentFile->getSourceLine(LineNo-1, LineStart, LineEnd);
85 if (LineStart == 0) return true;
Chris Lattner74382b72009-08-23 22:45:37 +000086 outs() << LineNo;
Chris Lattner7af5c122004-01-05 05:27:31 +000087
88 // If this is the line the program is currently stopped at, print a marker.
89 if (Dbg.isProgramRunning()) {
90 unsigned CurLineNo, CurColNo;
91 const SourceFileInfo *CurSFI;
92 getRuntimeInfo().getCurrentFrame().getSourceLocation(CurLineNo, CurColNo,
93 CurSFI);
94
95 if (CurLineNo == LineNo && CurrentFile == &CurSFI->getSourceText())
Chris Lattner74382b72009-08-23 22:45:37 +000096 outs() << " ->";
Chris Lattner7af5c122004-01-05 05:27:31 +000097 }
98
Chris Lattner74382b72009-08-23 22:45:37 +000099 outs() << "\t" << std::string(LineStart, LineEnd) << "\n";
Chris Lattner7af5c122004-01-05 05:27:31 +0000100 return false;
101}
102
103/// printProgramLocation - Print a line of the place where the current stack
104/// frame has stopped and the source line it is on.
105///
106void CLIDebugger::printProgramLocation(bool PrintLocation) {
107 assert(Dbg.isProgramLoaded() && Dbg.isProgramRunning() &&
108 "Error program is not loaded and running!");
109
110 // Figure out where the program stopped...
111 StackFrame &SF = getRuntimeInfo().getCurrentFrame();
112 unsigned LineNo, ColNo;
113 const SourceFileInfo *FileDesc;
114 SF.getSourceLocation(LineNo, ColNo, FileDesc);
115
116 // If requested, print out some program information about WHERE we are.
117 if (PrintLocation) {
118 // FIXME: print the current function arguments
119 if (const GlobalVariable *FuncDesc = SF.getFunctionDesc())
Chris Lattner74382b72009-08-23 22:45:37 +0000120 outs() << getProgramInfo().getFunction(FuncDesc).getSymbolicName();
Chris Lattner7af5c122004-01-05 05:27:31 +0000121 else
Chris Lattner74382b72009-08-23 22:45:37 +0000122 outs() << "<unknown function>";
Misha Brukman3da94ae2005-04-22 00:00:37 +0000123
Chris Lattner7af5c122004-01-05 05:27:31 +0000124 CurrentFile = &FileDesc->getSourceText();
Misha Brukman3da94ae2005-04-22 00:00:37 +0000125
Chris Lattner74382b72009-08-23 22:45:37 +0000126 outs() << " at " << CurrentFile->getFilename() << ":" << LineNo;
127 if (ColNo) outs() << ":" << ColNo;
128 outs() << "\n";
Chris Lattner7af5c122004-01-05 05:27:31 +0000129 }
130
131 if (printSourceLine(LineNo))
Chris Lattner74382b72009-08-23 22:45:37 +0000132 outs() << "<could not load source file>\n";
Chris Lattner7af5c122004-01-05 05:27:31 +0000133 else {
134 LineListedStart = LineNo-ListSize/2+1;
135 if ((int)LineListedStart < 1) LineListedStart = 1;
136 LineListedEnd = LineListedStart+1;
137 }
138}
139
140/// eliminateRunInfo - We are about to run the program. Forget any state
141/// about how the program used to be stopped.
142void CLIDebugger::eliminateRunInfo() {
143 delete TheRuntimeInfo;
144 TheRuntimeInfo = 0;
145}
146
147/// programStoppedSuccessfully - This method updates internal data
148/// structures to reflect the fact that the program just executed a while,
149/// and has successfully stopped.
150void CLIDebugger::programStoppedSuccessfully() {
151 assert(TheRuntimeInfo==0 && "Someone forgot to release the old RuntimeInfo!");
152
153 TheRuntimeInfo = new RuntimeInfo(TheProgramInfo, Dbg.getRunningProcess());
154
155 // FIXME: if there are any breakpoints at the current location, print them as
156 // well.
157
158 // Since the program as successfully stopped, print its location.
159 void *CurrentFrame = getRuntimeInfo().getCurrentFrame().getFrameID();
160 printProgramLocation(CurrentFrame != LastCurrentFrame);
161 LastCurrentFrame = CurrentFrame;
162}
163
164
165
166/// getUnsignedIntegerOption - Get an unsigned integer number from the Val
167/// string. Check to make sure that the string contains an unsigned integer
168/// token, and if not, throw an exception. If isOnlyOption is set, also throw
169/// an exception if there is extra junk at the end of the string.
170static unsigned getUnsignedIntegerOption(const char *Msg, std::string &Val,
171 bool isOnlyOption = true) {
172 std::string Tok = getToken(Val);
173 if (Tok.empty() || (isOnlyOption && !getToken(Val).empty()))
174 throw std::string(Msg) + " expects an unsigned integer argument.";
Misha Brukman3da94ae2005-04-22 00:00:37 +0000175
Chris Lattner7af5c122004-01-05 05:27:31 +0000176 char *EndPtr;
177 unsigned Result = strtoul(Tok.c_str(), &EndPtr, 0);
178 if (EndPtr != Tok.c_str()+Tok.size())
179 throw std::string(Msg) + " expects an unsigned integer argument.";
180
181 return Result;
182}
183
184/// getOptionalUnsignedIntegerOption - This method is just like
185/// getUnsignedIntegerOption, but if the argument value is not specified, a
186/// default is returned instead of causing an error.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000187static unsigned
Chris Lattner7af5c122004-01-05 05:27:31 +0000188getOptionalUnsignedIntegerOption(const char *Msg, unsigned Default,
189 std::string &Val, bool isOnlyOption = true) {
190 // Check to see if the value was specified...
191 std::string TokVal = getToken(Val);
192 if (TokVal.empty()) return Default;
193
194 // If it was specified, add it back to the value we are parsing...
195 Val = TokVal+Val;
196
197 // And parse normally.
198 return getUnsignedIntegerOption(Msg, Val, isOnlyOption);
199}
Chris Lattnere1567ae2004-01-06 05:37:16 +0000200
201
202/// parseProgramOptions - This method parses the Options string and loads it
203/// as options to be passed to the program. This is used by the run command
204/// and by 'set args'.
205void CLIDebugger::parseProgramOptions(std::string &Options) {
206 // FIXME: tokenizing by whitespace is clearly incorrect. Instead we should
207 // honor quotes and other things that a shell would. Also in the future we
208 // should support redirection of standard IO.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000209
Chris Lattnere1567ae2004-01-06 05:37:16 +0000210 std::vector<std::string> Arguments;
211 for (std::string A = getToken(Options); !A.empty(); A = getToken(Options))
212 Arguments.push_back(A);
213 Dbg.setProgramArguments(Arguments.begin(), Arguments.end());
214}
Misha Brukman3da94ae2005-04-22 00:00:37 +0000215
Chris Lattner7af5c122004-01-05 05:27:31 +0000216
217//===----------------------------------------------------------------------===//
218// Program startup and shutdown options
219//===----------------------------------------------------------------------===//
220
221
222/// file command - If the user specifies an option, search the PATH for the
Gabor Greifa99be512007-07-05 17:07:56 +0000223/// specified program/bitcode file and load it. If the user does not specify
Chris Lattner7af5c122004-01-05 05:27:31 +0000224/// an option, unload the current program.
225void CLIDebugger::fileCommand(std::string &Options) {
226 std::string Prog = getToken(Options);
227 if (!getToken(Options).empty())
228 throw "file command takes at most one argument.";
229
230 // Check to make sure the user knows what they are doing
231 if (Dbg.isProgramRunning() &&
232 !askYesNo("A program is already loaded. Kill it?"))
233 return;
234
235 // Unload an existing program. This kills the program if necessary.
236 eliminateRunInfo();
237 delete TheProgramInfo;
238 TheProgramInfo = 0;
239 Dbg.unloadProgram();
240 CurrentFile = 0;
241
242 // If requested, start the new program.
243 if (Prog.empty()) {
Chris Lattner74382b72009-08-23 22:45:37 +0000244 outs() << "Unloaded program.\n";
Chris Lattner7af5c122004-01-05 05:27:31 +0000245 } else {
Chris Lattner74382b72009-08-23 22:45:37 +0000246 outs() << "Loading program... ";
247 outs().flush();
Owen Anderson8b477ed2009-07-01 16:58:40 +0000248 Dbg.loadProgram(Prog, Context);
Chris Lattner7af5c122004-01-05 05:27:31 +0000249 assert(Dbg.isProgramLoaded() &&
250 "loadProgram succeeded, but not program loaded!");
251 TheProgramInfo = new ProgramInfo(Dbg.getProgram());
Chris Lattner74382b72009-08-23 22:45:37 +0000252 outs() << "successfully loaded '" << Dbg.getProgramPath() << "'!\n";
Chris Lattner7af5c122004-01-05 05:27:31 +0000253 }
254}
255
256
257void CLIDebugger::createCommand(std::string &Options) {
258 if (!getToken(Options).empty())
259 throw "create command does not take any arguments.";
260 if (!Dbg.isProgramLoaded()) throw "No program loaded.";
261 if (Dbg.isProgramRunning() &&
262 !askYesNo("The program is already running. Restart from the beginning?"))
263 return;
264
265 // Start the program running.
266 startProgramRunning();
267
268 // The program stopped!
269 programStoppedSuccessfully();
270}
271
272void CLIDebugger::killCommand(std::string &Options) {
273 if (!getToken(Options).empty())
274 throw "kill command does not take any arguments.";
275 if (!Dbg.isProgramRunning())
276 throw "No program is currently being run.";
277
278 if (askYesNo("Kill the program being debugged?"))
279 Dbg.killProgram();
280 eliminateRunInfo();
281}
282
283void CLIDebugger::quitCommand(std::string &Options) {
284 if (!getToken(Options).empty())
285 throw "quit command does not take any arguments.";
286
287 if (Dbg.isProgramRunning() &&
288 !askYesNo("The program is running. Exit anyway?"))
289 return;
290
291 // Throw exception to get out of the user-input loop.
292 throw 0;
293}
294
295
296//===----------------------------------------------------------------------===//
297// Program execution commands
298//===----------------------------------------------------------------------===//
299
300void CLIDebugger::runCommand(std::string &Options) {
Chris Lattner7af5c122004-01-05 05:27:31 +0000301 if (!Dbg.isProgramLoaded()) throw "No program loaded.";
302 if (Dbg.isProgramRunning() &&
303 !askYesNo("The program is already running. Restart from the beginning?"))
304 return;
305
Chris Lattnere1567ae2004-01-06 05:37:16 +0000306 // Parse all of the options to the run command, which specify program
307 // arguments to run with.
308 parseProgramOptions(Options);
309
Chris Lattner7af5c122004-01-05 05:27:31 +0000310 eliminateRunInfo();
311
312 // Start the program running.
313 startProgramRunning();
314
315 // Start the program running...
316 Options = "";
317 contCommand(Options);
318}
319
320void CLIDebugger::contCommand(std::string &Options) {
321 if (!getToken(Options).empty()) throw "cont argument not supported yet.";
322 if (!Dbg.isProgramRunning()) throw "Program is not running.";
323
324 eliminateRunInfo();
325
326 Dbg.contProgram();
327
328 // The program stopped!
329 programStoppedSuccessfully();
330}
331
332void CLIDebugger::stepCommand(std::string &Options) {
333 if (!Dbg.isProgramRunning()) throw "Program is not running.";
334
335 // Figure out how many times to step.
336 unsigned Amount =
337 getOptionalUnsignedIntegerOption("'step' command", 1, Options);
338
339 eliminateRunInfo();
340
341 // Step the specified number of times.
342 for (; Amount; --Amount)
343 Dbg.stepProgram();
344
345 // The program stopped!
346 programStoppedSuccessfully();
347}
348
349void CLIDebugger::nextCommand(std::string &Options) {
350 if (!Dbg.isProgramRunning()) throw "Program is not running.";
351 unsigned Amount =
352 getOptionalUnsignedIntegerOption("'next' command", 1, Options);
353
354 eliminateRunInfo();
355
356 for (; Amount; --Amount)
357 Dbg.nextProgram();
358
359 // The program stopped!
360 programStoppedSuccessfully();
361}
362
363void CLIDebugger::finishCommand(std::string &Options) {
364 if (!getToken(Options).empty())
365 throw "finish command does not take any arguments.";
366 if (!Dbg.isProgramRunning()) throw "Program is not running.";
367
368 // Figure out where we are exactly. If the user requests that we return from
369 // a frame that is not the top frame, make sure we get it.
370 void *CurrentFrame = getRuntimeInfo().getCurrentFrame().getFrameID();
371
372 eliminateRunInfo();
373
374 Dbg.finishProgram(CurrentFrame);
375
376 // The program stopped!
377 programStoppedSuccessfully();
378}
379
380//===----------------------------------------------------------------------===//
381// Stack frame commands
382//===----------------------------------------------------------------------===//
383
384void CLIDebugger::backtraceCommand(std::string &Options) {
385 // Accepts "full", n, -n
386 if (!getToken(Options).empty())
387 throw "FIXME: bt command argument not implemented yet!";
388
389 RuntimeInfo &RI = getRuntimeInfo();
390 ProgramInfo &PI = getProgramInfo();
391
392 try {
393 for (unsigned i = 0; ; ++i) {
394 StackFrame &SF = RI.getStackFrame(i);
Chris Lattner74382b72009-08-23 22:45:37 +0000395 outs() << "#" << i;
Chris Lattner7af5c122004-01-05 05:27:31 +0000396 if (i == RI.getCurrentFrameIdx())
Chris Lattner74382b72009-08-23 22:45:37 +0000397 outs() << " ->";
398 outs() << "\t" << SF.getFrameID() << " in ";
Chris Lattner7af5c122004-01-05 05:27:31 +0000399 if (const GlobalVariable *G = SF.getFunctionDesc())
Chris Lattner74382b72009-08-23 22:45:37 +0000400 outs() << PI.getFunction(G).getSymbolicName();
Chris Lattner7af5c122004-01-05 05:27:31 +0000401
402 unsigned LineNo, ColNo;
403 const SourceFileInfo *SFI;
404 SF.getSourceLocation(LineNo, ColNo, SFI);
405 if (!SFI->getBaseName().empty()) {
Chris Lattner74382b72009-08-23 22:45:37 +0000406 outs() << " at " << SFI->getBaseName();
Chris Lattner7af5c122004-01-05 05:27:31 +0000407 if (LineNo) {
Chris Lattner74382b72009-08-23 22:45:37 +0000408 outs() << ":" << LineNo;
Chris Lattner7af5c122004-01-05 05:27:31 +0000409 if (ColNo)
Chris Lattner74382b72009-08-23 22:45:37 +0000410 outs() << ":" << ColNo;
Chris Lattner7af5c122004-01-05 05:27:31 +0000411 }
412 }
413
414 // FIXME: when we support shared libraries, we should print ' from foo.so'
415 // if the stack frame is from a different object than the current one.
416
Chris Lattner74382b72009-08-23 22:45:37 +0000417 outs() << "\n";
Chris Lattner7af5c122004-01-05 05:27:31 +0000418 }
419 } catch (...) {
420 // Stop automatically when we run off the bottom of the stack.
421 }
422}
423
424void CLIDebugger::upCommand(std::string &Options) {
425 unsigned Num =
426 getOptionalUnsignedIntegerOption("'up' command", 1, Options);
427
428 RuntimeInfo &RI = getRuntimeInfo();
429 unsigned CurFrame = RI.getCurrentFrameIdx();
430
431 // Check to see if we go can up the specified number of frames.
432 try {
433 RI.getStackFrame(CurFrame+Num);
434 } catch (...) {
435 if (Num == 1)
436 throw "Initial frame selected; you cannot go up.";
437 else
438 throw "Cannot go up " + utostr(Num) + " frames!";
439 }
440
441 RI.setCurrentFrameIdx(CurFrame+Num);
442 printProgramLocation();
443}
444
445void CLIDebugger::downCommand(std::string &Options) {
446 unsigned Num =
447 getOptionalUnsignedIntegerOption("'down' command", 1, Options);
448
449 RuntimeInfo &RI = getRuntimeInfo();
450 unsigned CurFrame = RI.getCurrentFrameIdx();
451
452 // Check to see if we can go up the specified number of frames.
Anton Korobeynikova27694d2008-02-20 11:27:04 +0000453 if (CurFrame < Num) {
Chris Lattner7af5c122004-01-05 05:27:31 +0000454 if (Num == 1)
455 throw "Bottom (i.e., innermost) frame selected; you cannot go down.";
456 else
457 throw "Cannot go down " + utostr(Num) + " frames!";
Anton Korobeynikova27694d2008-02-20 11:27:04 +0000458 }
Chris Lattner7af5c122004-01-05 05:27:31 +0000459
460 RI.setCurrentFrameIdx(CurFrame-Num);
461 printProgramLocation();
462}
463
464void CLIDebugger::frameCommand(std::string &Options) {
465 RuntimeInfo &RI = getRuntimeInfo();
466 unsigned CurFrame = RI.getCurrentFrameIdx();
467
468 unsigned Num =
469 getOptionalUnsignedIntegerOption("'frame' command", CurFrame, Options);
470
471 // Check to see if we go to the specified frame.
472 RI.getStackFrame(Num);
473
474 RI.setCurrentFrameIdx(Num);
475 printProgramLocation();
476}
477
478
479//===----------------------------------------------------------------------===//
480// Breakpoint related commands
481//===----------------------------------------------------------------------===//
482
483void CLIDebugger::breakCommand(std::string &Options) {
Chris Lattnere1567ae2004-01-06 05:37:16 +0000484 // Figure out where the user wants a breakpoint.
485 const SourceFile *File;
486 unsigned LineNo;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000487
Chris Lattnere1567ae2004-01-06 05:37:16 +0000488 // Check to see if the user specified a line specifier.
489 std::string Option = getToken(Options); // strip whitespace
490 if (!Option.empty()) {
491 Options = Option + Options; // reconstruct string
492
493 // Parse the line specifier.
Chris Lattnerf6608dd2004-01-06 23:46:17 +0000494 parseLineSpec(Options, File, LineNo);
Chris Lattnere1567ae2004-01-06 05:37:16 +0000495 } else {
496 // Build a line specifier for the current stack frame.
497 throw "FIXME: breaking at the current location is not implemented yet!";
498 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000499
Chris Lattnerf2592ce2004-02-08 00:06:20 +0000500 if (!File) File = CurrentFile;
501 if (File == 0)
502 throw "Unknown file to place breakpoint!";
503
Dan Gohman65f57c22009-07-15 16:35:29 +0000504 errs() << "Break: " << File->getFilename() << ":" << LineNo << "\n";
Misha Brukman3da94ae2005-04-22 00:00:37 +0000505
Chris Lattner7af5c122004-01-05 05:27:31 +0000506 throw "breakpoints not implemented yet!";
507}
508
509//===----------------------------------------------------------------------===//
510// Miscellaneous commands
511//===----------------------------------------------------------------------===//
512
513void CLIDebugger::infoCommand(std::string &Options) {
514 std::string What = getToken(Options);
515
Chris Lattner395236f2004-10-26 05:46:17 +0000516 if (What.empty() || !getToken(Options).empty()){
517 std::string infoStr("info");
518 helpCommand(infoStr);
519 return;
520 }
Chris Lattner7af5c122004-01-05 05:27:31 +0000521
522 if (What == "frame") {
523 } else if (What == "functions") {
524 const std::map<const GlobalVariable*, SourceFunctionInfo*> &Functions
525 = getProgramInfo().getSourceFunctions();
Chris Lattner74382b72009-08-23 22:45:37 +0000526 outs() << "All defined functions:\n";
Chris Lattner7af5c122004-01-05 05:27:31 +0000527 // FIXME: GDB groups these by source file. We could do that I guess.
528 for (std::map<const GlobalVariable*, SourceFunctionInfo*>::const_iterator
529 I = Functions.begin(), E = Functions.end(); I != E; ++I) {
Chris Lattner74382b72009-08-23 22:45:37 +0000530 outs() << I->second->getSymbolicName() << "\n";
Chris Lattner7af5c122004-01-05 05:27:31 +0000531 }
532
533 } else if (What == "source") {
534 if (CurrentFile == 0)
535 throw "No current source file.";
536
537 // Get the SourceFile information for the current file.
538 const SourceFileInfo &SF =
539 getProgramInfo().getSourceFile(CurrentFile->getDescriptor());
540
Chris Lattner74382b72009-08-23 22:45:37 +0000541 outs() << "Current source file is: " << SF.getBaseName() << "\n"
542 << "Compilation directory is: " << SF.getDirectory() << "\n";
Chris Lattner7af5c122004-01-05 05:27:31 +0000543 if (unsigned NL = CurrentFile->getNumLines())
Chris Lattner74382b72009-08-23 22:45:37 +0000544 outs() << "Located in: " << CurrentFile->getFilename() << "\n"
545 << "Contains " << NL << " lines\n";
Chris Lattner7af5c122004-01-05 05:27:31 +0000546 else
Chris Lattner74382b72009-08-23 22:45:37 +0000547 outs() << "Could not find source file.\n";
548 outs() << "Source language is "
549 << SF.getLanguage().getSourceLanguageName() << "\n";
Chris Lattner7af5c122004-01-05 05:27:31 +0000550
551 } else if (What == "sources") {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000552 const std::map<const GlobalVariable*, SourceFileInfo*> &SourceFiles =
Chris Lattner7af5c122004-01-05 05:27:31 +0000553 getProgramInfo().getSourceFiles();
Chris Lattner74382b72009-08-23 22:45:37 +0000554 outs() << "Source files for the program:\n";
Chris Lattner7af5c122004-01-05 05:27:31 +0000555 for (std::map<const GlobalVariable*, SourceFileInfo*>::const_iterator I =
556 SourceFiles.begin(), E = SourceFiles.end(); I != E;) {
Chris Lattner74382b72009-08-23 22:45:37 +0000557 outs() << I->second->getDirectory() << "/"
558 << I->second->getBaseName();
Chris Lattner7af5c122004-01-05 05:27:31 +0000559 ++I;
Chris Lattner74382b72009-08-23 22:45:37 +0000560 if (I != E) outs() << ", ";
Chris Lattner7af5c122004-01-05 05:27:31 +0000561 }
Chris Lattner74382b72009-08-23 22:45:37 +0000562 outs() << "\n";
Chris Lattner7af5c122004-01-05 05:27:31 +0000563 } else if (What == "target") {
Chris Lattner74382b72009-08-23 22:45:37 +0000564 outs() << Dbg.getRunningProcess().getStatus();
Chris Lattner7af5c122004-01-05 05:27:31 +0000565 } else {
566 // See if this is something handled by the current language.
567 if (getCurrentLanguage().printInfo(What))
568 return;
569
570 throw "Unknown info command '" + What + "'. Try 'help info'.";
571 }
572}
573
574/// parseLineSpec - Parses a line specifier, for use by the 'list' command.
575/// If SourceFile is returned as a void pointer, then it was not specified.
576/// If the line specifier is invalid, an exception is thrown.
577void CLIDebugger::parseLineSpec(std::string &LineSpec,
578 const SourceFile *&SourceFile,
579 unsigned &LineNo) {
580 SourceFile = 0;
581 LineNo = 0;
582
583 // First, check to see if we have a : separator.
584 std::string FirstPart = getToken(LineSpec, ":");
585 std::string SecondPart = getToken(LineSpec, ":");
586 if (!getToken(LineSpec).empty()) throw "Malformed line specification!";
587
588 // If there is no second part, we must have either "function", "number",
589 // "+offset", or "-offset".
590 if (SecondPart.empty()) {
591 if (FirstPart.empty()) throw "Malformed line specification!";
592 if (FirstPart[0] == '+') {
593 FirstPart.erase(FirstPart.begin(), FirstPart.begin()+1);
594 // For +n, return LineListedEnd+n
595 LineNo = LineListedEnd +
596 getUnsignedIntegerOption("Line specifier '+'", FirstPart);
597
598 } else if (FirstPart[0] == '-') {
599 FirstPart.erase(FirstPart.begin(), FirstPart.begin()+1);
600 // For -n, return LineListedEnd-n
601 LineNo = LineListedEnd -
602 getUnsignedIntegerOption("Line specifier '-'", FirstPart);
603 if ((int)LineNo < 1) LineNo = 1;
604 } else if (FirstPart[0] == '*') {
605 throw "Address expressions not supported as source locations!";
606 } else {
607 // Ok, check to see if this is just a line number.
608 std::string Saved = FirstPart;
609 try {
610 LineNo = getUnsignedIntegerOption("", Saved);
611 } catch (...) {
612 // Ok, it's not a valid line number. It must be a source-language
613 // entity name.
614 std::string Name = getToken(FirstPart);
615 if (!getToken(FirstPart).empty())
616 throw "Extra junk in line specifier after '" + Name + "'.";
Misha Brukman3da94ae2005-04-22 00:00:37 +0000617 SourceFunctionInfo *SFI =
Chris Lattner7af5c122004-01-05 05:27:31 +0000618 getCurrentLanguage().lookupFunction(Name, getProgramInfo(),
619 TheRuntimeInfo);
620 if (SFI == 0)
621 throw "Unknown identifier '" + Name + "'.";
622
623 unsigned L, C;
624 SFI->getSourceLocation(L, C);
625 if (L == 0) throw "Could not locate '" + Name + "'!";
626 LineNo = L;
627 SourceFile = &SFI->getSourceFile().getSourceText();
628 return;
629 }
630 }
631
632 } else {
633 // Ok, this must be a filename qualified line number or function name.
634 // First, figure out the source filename.
635 std::string SourceFilename = getToken(FirstPart);
636 if (!getToken(FirstPart).empty())
637 throw "Invalid filename qualified source location!";
638
639 // Next, check to see if this is just a line number.
640 std::string Saved = SecondPart;
641 try {
642 LineNo = getUnsignedIntegerOption("", Saved);
643 } catch (...) {
644 // Ok, it's not a valid line number. It must be a function name.
645 throw "FIXME: Filename qualified function names are not support "
646 "as line specifiers yet!";
647 }
648
649 // Ok, we got the line number. Now check out the source file name to make
650 // sure it's all good. If it is, return it. If not, throw exception.
651 SourceFile =&getProgramInfo().getSourceFile(SourceFilename).getSourceText();
652 }
653}
654
655void CLIDebugger::listCommand(std::string &Options) {
656 if (!Dbg.isProgramLoaded())
657 throw "No program is loaded. Use the 'file' command.";
658
659 // Handle "list foo," correctly, by returning " " as the second token
660 Options += " ";
Misha Brukman3da94ae2005-04-22 00:00:37 +0000661
Chris Lattner7af5c122004-01-05 05:27:31 +0000662 std::string FirstLineSpec = getToken(Options, ",");
663 std::string SecondLineSpec = getToken(Options, ",");
664 if (!getToken(Options, ",").empty())
665 throw "list command only expects two source location specifiers!";
666
667 // StartLine, EndLine - The starting and ending line numbers to print.
668 unsigned StartLine = 0, EndLine = 0;
669
670 if (SecondLineSpec.empty()) { // No second line specifier provided?
671 // Handle special forms like "", "+", "-", etc.
672 std::string TmpSpec = FirstLineSpec;
673 std::string Tok = getToken(TmpSpec);
674 if (getToken(TmpSpec).empty() && (Tok == "" || Tok == "+" || Tok == "-")) {
675 if (Tok == "+" || Tok == "") {
676 StartLine = LineListedEnd;
677 EndLine = StartLine + ListSize;
678 } else {
679 assert(Tok == "-");
680 StartLine = LineListedStart-ListSize;
681 EndLine = LineListedStart;
Chris Lattnere1567ae2004-01-06 05:37:16 +0000682 if ((int)StartLine <= 0) StartLine = 1;
Chris Lattner7af5c122004-01-05 05:27:31 +0000683 }
684 } else {
685 // Must be a normal line specifier.
686 const SourceFile *File;
687 unsigned LineNo;
688 parseLineSpec(FirstLineSpec, File, LineNo);
689
690 // If the user only specified one file specifier, we should display
691 // ListSize lines centered at the specified line.
692 if (File != 0) CurrentFile = File;
693 StartLine = LineNo - (ListSize+1)/2;
Chris Lattnere1567ae2004-01-06 05:37:16 +0000694 if ((int)StartLine <= 0) StartLine = 1;
Chris Lattner7af5c122004-01-05 05:27:31 +0000695 EndLine = StartLine + ListSize;
696 }
697
698 } else {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000699 // Parse two line specifiers...
Chris Lattner7af5c122004-01-05 05:27:31 +0000700 const SourceFile *StartFile, *EndFile;
701 unsigned StartLineNo, EndLineNo;
702 parseLineSpec(FirstLineSpec, StartFile, StartLineNo);
703 unsigned SavedLLE = LineListedEnd;
704 LineListedEnd = StartLineNo;
705 try {
706 parseLineSpec(SecondLineSpec, EndFile, EndLineNo);
707 } catch (...) {
708 LineListedEnd = SavedLLE;
709 throw;
710 }
711
712 // Inherit file specified by the first line spec if there was one.
713 if (EndFile == 0) EndFile = StartFile;
714
715 if (StartFile != EndFile)
716 throw "Start and end line specifiers are in different files!";
717 CurrentFile = StartFile;
718 StartLine = StartLineNo;
719 EndLine = EndLineNo+1;
720 }
721
722 assert((int)StartLine > 0 && (int)EndLine > 0 && StartLine <= EndLine &&
723 "Error reading line specifiers!");
724
725 // If there was no current file, and the user didn't specify one to list, we
726 // have an error.
727 if (CurrentFile == 0)
728 throw "There is no current file to list.";
729
730 // Remember for next time.
731 LineListedStart = StartLine;
732 LineListedEnd = StartLine;
733
734 for (unsigned LineNo = StartLine; LineNo != EndLine; ++LineNo) {
735 // Print the source line, unless it is invalid.
736 if (printSourceLine(LineNo))
737 break;
738 LineListedEnd = LineNo+1;
739 }
740
741 // If we didn't print any lines, find out why.
742 if (LineListedEnd == StartLine) {
743 // See if we can read line #0 from the file, if not, we couldn't load the
744 // file.
745 const char *LineStart, *LineEnd;
746 CurrentFile->getSourceLine(0, LineStart, LineEnd);
747 if (LineStart == 0)
748 throw "Could not load source file '" + CurrentFile->getFilename() + "'!";
749 else
Chris Lattner74382b72009-08-23 22:45:37 +0000750 outs() << "<end of file>\n";
Chris Lattner7af5c122004-01-05 05:27:31 +0000751 }
752}
753
754void CLIDebugger::setCommand(std::string &Options) {
755 std::string What = getToken(Options);
756
757 if (What.empty())
758 throw "set command expects at least two arguments.";
Chris Lattnere1567ae2004-01-06 05:37:16 +0000759 if (What == "args") {
760 parseProgramOptions(Options);
761 } else if (What == "language") {
Chris Lattner7af5c122004-01-05 05:27:31 +0000762 std::string Lang = getToken(Options);
763 if (!getToken(Options).empty())
764 throw "set language expects one argument at most.";
765 if (Lang == "") {
Chris Lattner74382b72009-08-23 22:45:37 +0000766 outs() << "The currently understood settings are:\n\n"
767 << "local or auto Automatic setting based on source file\n"
768 << "c Use the C language\n"
769 << "c++ Use the C++ language\n"
770 << "unknown Use when source language is not supported\n";
Chris Lattner7af5c122004-01-05 05:27:31 +0000771 } else if (Lang == "local" || Lang == "auto") {
772 CurrentLanguage = 0;
773 } else if (Lang == "c") {
774 CurrentLanguage = &SourceLanguage::getCFamilyInstance();
775 } else if (Lang == "c++") {
776 CurrentLanguage = &SourceLanguage::getCPlusPlusInstance();
777 } else if (Lang == "unknown") {
778 CurrentLanguage = &SourceLanguage::getUnknownLanguageInstance();
779 } else {
780 throw "Unknown language '" + Lang + "'.";
781 }
782
783 } else if (What == "listsize") {
784 ListSize = getUnsignedIntegerOption("'set prompt' command", Options);
785 } else if (What == "prompt") {
786 // Include any trailing whitespace or other tokens, but not leading
787 // whitespace.
788 Prompt = getToken(Options); // Strip leading whitespace
789 Prompt += Options; // Keep trailing whitespace or other stuff
790 } else {
791 // FIXME: Try to parse this as a source-language program expression.
792 throw "Don't know how to set '" + What + "'!";
793 }
794}
795
796void CLIDebugger::showCommand(std::string &Options) {
797 std::string What = getToken(Options);
798
799 if (What.empty() || !getToken(Options).empty())
800 throw "show command expects one argument.";
801
Chris Lattnere1567ae2004-01-06 05:37:16 +0000802 if (What == "args") {
Chris Lattner74382b72009-08-23 22:45:37 +0000803 outs() << "Argument list to give program when started is \"";
Chris Lattnere1567ae2004-01-06 05:37:16 +0000804 // FIXME: This doesn't print stuff correctly if the arguments have spaces in
805 // them, but currently the only way to get that is to use the --args command
806 // line argument. This should really handle escaping all hard characters as
807 // needed.
808 for (unsigned i = 0, e = Dbg.getNumProgramArguments(); i != e; ++i)
Chris Lattner74382b72009-08-23 22:45:37 +0000809 outs() << (i ? " " : "") << Dbg.getProgramArgument(i);
810 outs() << "\"\n";
Chris Lattnere1567ae2004-01-06 05:37:16 +0000811
812 } else if (What == "language") {
Chris Lattner74382b72009-08-23 22:45:37 +0000813 outs() << "The current source language is '";
Chris Lattner7af5c122004-01-05 05:27:31 +0000814 if (CurrentLanguage)
Chris Lattner74382b72009-08-23 22:45:37 +0000815 outs() << CurrentLanguage->getSourceLanguageName();
Chris Lattner7af5c122004-01-05 05:27:31 +0000816 else
Chris Lattner74382b72009-08-23 22:45:37 +0000817 outs() << "auto; currently "
818 << getCurrentLanguage().getSourceLanguageName();
819 outs() << "'.\n";
Chris Lattner7af5c122004-01-05 05:27:31 +0000820 } else if (What == "listsize") {
Chris Lattner74382b72009-08-23 22:45:37 +0000821 outs() << "Number of source lines llvm-db will list by default is "
822 << ListSize << ".\n";
Chris Lattner7af5c122004-01-05 05:27:31 +0000823 } else if (What == "prompt") {
Chris Lattner74382b72009-08-23 22:45:37 +0000824 outs() << "llvm-db's prompt is \"" << Prompt << "\".\n";
Chris Lattner7af5c122004-01-05 05:27:31 +0000825 } else {
826 throw "Unknown show command '" + What + "'. Try 'help show'.";
827 }
828}
829
830void CLIDebugger::helpCommand(std::string &Options) {
831 // Print out all of the commands in the CommandTable
832 std::string Command = getToken(Options);
833 if (!getToken(Options).empty())
834 throw "help command takes at most one argument.";
835
836 // Getting detailed help on a particular command?
837 if (!Command.empty()) {
838 CLICommand *C = getCommand(Command);
Chris Lattner74382b72009-08-23 22:45:37 +0000839 outs() << C->getShortHelp() << ".\n" << C->getLongHelp();
Chris Lattner7af5c122004-01-05 05:27:31 +0000840
841 // If there are aliases for this option, print them out.
842 const std::vector<std::string> &Names = C->getOptionNames();
843 if (Names.size() > 1) {
Chris Lattner74382b72009-08-23 22:45:37 +0000844 outs() << "The '" << Command << "' command is known as: '"
845 << Names[0] << "'";
Chris Lattner7af5c122004-01-05 05:27:31 +0000846 for (unsigned i = 1, e = Names.size(); i != e; ++i)
Chris Lattner74382b72009-08-23 22:45:37 +0000847 outs() << ", '" << Names[i] << "'";
848 outs() << "\n";
Chris Lattner7af5c122004-01-05 05:27:31 +0000849 }
850
851 } else {
852 unsigned MaxSize = 0;
853 for (std::map<std::string, CLICommand*>::iterator I = CommandTable.begin(),
854 E = CommandTable.end(); I != E; ++I)
855 if (I->first.size() > MaxSize &&
856 I->first == I->second->getPrimaryOptionName())
857 MaxSize = I->first.size();
858
859 // Loop over all of the commands, printing the short help version
860 for (std::map<std::string, CLICommand*>::iterator I = CommandTable.begin(),
861 E = CommandTable.end(); I != E; ++I)
862 if (I->first == I->second->getPrimaryOptionName())
Chris Lattner74382b72009-08-23 22:45:37 +0000863 outs() << I->first << std::string(MaxSize - I->first.size(), ' ')
864 << " - " << I->second->getShortHelp() << "\n";
Chris Lattner7af5c122004-01-05 05:27:31 +0000865 }
866}