LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Debugger.cpp

Go to the documentation of this file.
00001 //===-- Debugger.cpp - LLVM debugger library implementation ---------------===//
00002 // 
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 // 
00008 //===----------------------------------------------------------------------===//
00009 // 
00010 // This file contains the main implementation of the LLVM debugger library.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Debugger/Debugger.h"
00015 #include "llvm/Module.h"
00016 #include "llvm/ModuleProvider.h"
00017 #include "llvm/Bytecode/Reader.h"
00018 #include "llvm/Debugger/InferiorProcess.h"
00019 #include "llvm/ADT/StringExtras.h"
00020 using namespace llvm;
00021 
00022 /// Debugger constructor - Initialize the debugger to its initial, empty, state.
00023 ///
00024 Debugger::Debugger() : Environment(0), Program(0), Process(0) {
00025 }
00026 
00027 Debugger::~Debugger() {
00028   // Killing the program could throw an exception.  We don't want to progagate
00029   // the exception out of our destructor though.
00030   try {
00031     killProgram();
00032   } catch (const char *) {
00033   } catch (const std::string &) {
00034   }
00035   
00036   unloadProgram();
00037 }
00038 
00039 /// getProgramPath - Get the path of the currently loaded program, or an
00040 /// empty string if none is loaded.
00041 std::string Debugger::getProgramPath() const {
00042   return Program ? Program->getModuleIdentifier() : "";
00043 }
00044 
00045 static Module *
00046 getMaterializedModuleProvider(const std::string &Filename) {
00047   try {
00048     std::auto_ptr<ModuleProvider> Result(getBytecodeModuleProvider(Filename));
00049     if (!Result.get()) return 0;
00050   
00051     Result->materializeModule();
00052     return Result.release()->releaseModule();
00053   } catch (...) {
00054     return 0;
00055   }
00056 }
00057 
00058 /// loadProgram - If a program is currently loaded, unload it.  Then search
00059 /// the PATH for the specified program, loading it when found.  If the
00060 /// specified program cannot be found, an exception is thrown to indicate the
00061 /// error.
00062 void Debugger::loadProgram(const std::string &Filename) {
00063   if ((Program = getMaterializedModuleProvider(Filename)) ||
00064       (Program = getMaterializedModuleProvider(Filename+".bc")))
00065     return;   // Successfully loaded the program.
00066 
00067   // Search the program path for the file...
00068   if (const char *PathS = getenv("PATH")) {
00069     std::string Path = PathS;
00070 
00071     std::string Directory = getToken(Path, ":");
00072     while (!Directory.empty()) {
00073       if ((Program = getMaterializedModuleProvider(Directory +"/"+ Filename)) ||
00074           (Program = getMaterializedModuleProvider(Directory +"/"+ Filename
00075                                                                       + ".bc")))
00076         return;   // Successfully loaded the program.
00077 
00078       Directory = getToken(Path, ":");
00079     }
00080   }
00081 
00082   throw "Could not find program '" + Filename + "'!";
00083 }
00084 
00085 /// unloadProgram - If a program is running, kill it, then unload all traces
00086 /// of the current program.  If no program is loaded, this method silently
00087 /// succeeds.
00088 void Debugger::unloadProgram() {
00089   if (!isProgramLoaded()) return;
00090   killProgram();
00091   delete Program;
00092   Program = 0;
00093 }
00094 
00095 
00096 /// createProgram - Create an instance of the currently loaded program,
00097 /// killing off any existing one.  This creates the program and stops it at
00098 /// the first possible moment.  If there is no program loaded or if there is a
00099 /// problem starting the program, this method throws an exception.
00100 void Debugger::createProgram() {
00101   if (!isProgramLoaded())
00102     throw "Cannot start program: none is loaded.";
00103 
00104   // Kill any existing program.
00105   killProgram();
00106 
00107   // Add argv[0] to the arguments vector..
00108   std::vector<std::string> Args(ProgramArguments);
00109   Args.insert(Args.begin(), getProgramPath());
00110 
00111   // Start the new program... this could throw if the program cannot be started.
00112   Process = InferiorProcess::create(Program, Args, Environment);
00113 }
00114 
00115 /// killProgram - If the program is currently executing, kill off the
00116 /// process and free up any state related to the currently running program.  If
00117 /// there is no program currently running, this just silently succeeds.
00118 void Debugger::killProgram() {
00119   // The destructor takes care of the dirty work.
00120   try {
00121     delete Process;
00122   } catch (...) {
00123     Process = 0;
00124     throw;
00125   }
00126   Process = 0;
00127 }
00128 
00129 /// stepProgram - Implement the 'step' command, continuing execution until
00130 /// the next possible stop point.
00131 void Debugger::stepProgram() {
00132   assert(isProgramRunning() && "Cannot step if the program isn't running!");
00133   try {
00134     Process->stepProgram();
00135   } catch (InferiorProcessDead &IPD) {
00136     killProgram();
00137     throw NonErrorException("The program stopped with exit code " +
00138                             itostr(IPD.getExitCode()));
00139   } catch (...) {
00140     killProgram();
00141     throw;
00142   }
00143 }
00144 
00145 /// nextProgram - Implement the 'next' command, continuing execution until
00146 /// the next possible stop point that is in the current function.
00147 void Debugger::nextProgram() {
00148   assert(isProgramRunning() && "Cannot next if the program isn't running!");
00149   try {
00150     // This should step the process.  If the process enters a function, then it
00151     // should 'finish' it.  However, figuring this out is tricky.  In
00152     // particular, the program can do any of:
00153     //  0. Not change current frame.
00154     //  1. Entering or exiting a region within the current function
00155     //     (which changes the frame ID, but which we shouldn't 'finish')
00156     //  2. Exiting the current function (which changes the frame ID)
00157     //  3. Entering a function (which should be 'finish'ed)
00158     // For this reason, we have to be very careful about when we decide to do
00159     // the 'finish'.
00160 
00161     // Get the current frame, but don't trust it.  It could change...
00162     void *CurrentFrame = Process->getPreviousFrame(0);
00163 
00164     // Don't trust the current frame: get the caller frame.
00165     void *ParentFrame  = Process->getPreviousFrame(CurrentFrame);
00166     
00167     // Ok, we have some information, run the program one step.
00168     Process->stepProgram();
00169 
00170     // Where is the new frame?  The most common case, by far is that it has not
00171     // been modified (Case #0), in which case we don't need to do anything more.
00172     void *NewFrame = Process->getPreviousFrame(0);
00173     if (NewFrame != CurrentFrame) {
00174       // Ok, the frame changed.  If we are case #1, then the parent frame will
00175       // be identical.
00176       void *NewParentFrame = Process->getPreviousFrame(NewFrame);
00177       if (ParentFrame != NewParentFrame) {
00178         // Ok, now we know we aren't case #0 or #1.  Check to see if we entered
00179         // a new function.  If so, the parent frame will be "CurrentFrame".
00180         if (CurrentFrame == NewParentFrame)
00181           Process->finishProgram(NewFrame);
00182       }
00183     }
00184 
00185   } catch (InferiorProcessDead &IPD) {
00186     killProgram();
00187     throw NonErrorException("The program stopped with exit code " +
00188                             itostr(IPD.getExitCode()));
00189   } catch (...) {
00190     killProgram();
00191     throw;
00192   }
00193 }
00194 
00195 /// finishProgram - Implement the 'finish' command, continuing execution
00196 /// until the specified frame ID returns.
00197 void Debugger::finishProgram(void *Frame) {
00198   assert(isProgramRunning() && "Cannot cont if the program isn't running!");
00199   try {
00200     Process->finishProgram(Frame);
00201   } catch (InferiorProcessDead &IPD) {
00202     killProgram();
00203     throw NonErrorException("The program stopped with exit code " +
00204                             itostr(IPD.getExitCode()));
00205   } catch (...) {
00206     killProgram();
00207     throw;
00208   }
00209 }
00210 
00211 /// contProgram - Implement the 'cont' command, continuing execution until
00212 /// the next breakpoint is encountered.
00213 void Debugger::contProgram() {
00214   assert(isProgramRunning() && "Cannot cont if the program isn't running!");
00215   try {
00216     Process->contProgram();
00217   } catch (InferiorProcessDead &IPD) {
00218     killProgram();
00219     throw NonErrorException("The program stopped with exit code " +
00220                             itostr(IPD.getExitCode()));
00221   } catch (...) {
00222     killProgram();
00223     throw;
00224   }
00225 }