LLVM API Documentation

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