LLVM API Documentation
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 LLVMContext& C) { 00051 std::auto_ptr<MemoryBuffer> Buffer; 00052 Buffer.reset(MemoryBuffer::getFileOrSTDIN(Filename.c_str())); 00053 if (Buffer.get()) 00054 return ParseBitcodeFile(Buffer.get(), C); 00055 return 0; 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, LLVMContext& C) { 00063 if ((Program = getMaterializedModuleProvider(Filename, C)) || 00064 (Program = getMaterializedModuleProvider(Filename+".bc", C))) 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, C)) 00074 || (Program = getMaterializedModuleProvider(Directory +"/"+ Filename 00075 + ".bc", C))) 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 InferiorProcess * 00116 InferiorProcess::create(Module *M, const std::vector<std::string> &Arguments, 00117 const char * const *envp) { 00118 throw"No supported binding to inferior processes (debugger not implemented)."; 00119 } 00120 00121 /// killProgram - If the program is currently executing, kill off the 00122 /// process and free up any state related to the currently running program. If 00123 /// there is no program currently running, this just silently succeeds. 00124 void Debugger::killProgram() { 00125 // The destructor takes care of the dirty work. 00126 try { 00127 delete Process; 00128 } catch (...) { 00129 Process = 0; 00130 throw; 00131 } 00132 Process = 0; 00133 } 00134 00135 /// stepProgram - Implement the 'step' command, continuing execution until 00136 /// the next possible stop point. 00137 void Debugger::stepProgram() { 00138 assert(isProgramRunning() && "Cannot step if the program isn't running!"); 00139 try { 00140 Process->stepProgram(); 00141 } catch (InferiorProcessDead &IPD) { 00142 killProgram(); 00143 throw NonErrorException("The program stopped with exit code " + 00144 itostr(IPD.getExitCode())); 00145 } catch (...) { 00146 killProgram(); 00147 throw; 00148 } 00149 } 00150 00151 /// nextProgram - Implement the 'next' command, continuing execution until 00152 /// the next possible stop point that is in the current function. 00153 void Debugger::nextProgram() { 00154 assert(isProgramRunning() && "Cannot next if the program isn't running!"); 00155 try { 00156 // This should step the process. If the process enters a function, then it 00157 // should 'finish' it. However, figuring this out is tricky. In 00158 // particular, the program can do any of: 00159 // 0. Not change current frame. 00160 // 1. Entering or exiting a region within the current function 00161 // (which changes the frame ID, but which we shouldn't 'finish') 00162 // 2. Exiting the current function (which changes the frame ID) 00163 // 3. Entering a function (which should be 'finish'ed) 00164 // For this reason, we have to be very careful about when we decide to do 00165 // the 'finish'. 00166 00167 // Get the current frame, but don't trust it. It could change... 00168 void *CurrentFrame = Process->getPreviousFrame(0); 00169 00170 // Don't trust the current frame: get the caller frame. 00171 void *ParentFrame = Process->getPreviousFrame(CurrentFrame); 00172 00173 // Ok, we have some information, run the program one step. 00174 Process->stepProgram(); 00175 00176 // Where is the new frame? The most common case, by far is that it has not 00177 // been modified (Case #0), in which case we don't need to do anything more. 00178 void *NewFrame = Process->getPreviousFrame(0); 00179 if (NewFrame != CurrentFrame) { 00180 // Ok, the frame changed. If we are case #1, then the parent frame will 00181 // be identical. 00182 void *NewParentFrame = Process->getPreviousFrame(NewFrame); 00183 if (ParentFrame != NewParentFrame) { 00184 // Ok, now we know we aren't case #0 or #1. Check to see if we entered 00185 // a new function. If so, the parent frame will be "CurrentFrame". 00186 if (CurrentFrame == NewParentFrame) 00187 Process->finishProgram(NewFrame); 00188 } 00189 } 00190 00191 } catch (InferiorProcessDead &IPD) { 00192 killProgram(); 00193 throw NonErrorException("The program stopped with exit code " + 00194 itostr(IPD.getExitCode())); 00195 } catch (...) { 00196 killProgram(); 00197 throw; 00198 } 00199 } 00200 00201 /// finishProgram - Implement the 'finish' command, continuing execution 00202 /// until the specified frame ID returns. 00203 void Debugger::finishProgram(void *Frame) { 00204 assert(isProgramRunning() && "Cannot cont if the program isn't running!"); 00205 try { 00206 Process->finishProgram(Frame); 00207 } catch (InferiorProcessDead &IPD) { 00208 killProgram(); 00209 throw NonErrorException("The program stopped with exit code " + 00210 itostr(IPD.getExitCode())); 00211 } catch (...) { 00212 killProgram(); 00213 throw; 00214 } 00215 } 00216 00217 /// contProgram - Implement the 'cont' command, continuing execution until 00218 /// the next breakpoint is encountered. 00219 void Debugger::contProgram() { 00220 assert(isProgramRunning() && "Cannot cont if the program isn't running!"); 00221 try { 00222 Process->contProgram(); 00223 } catch (InferiorProcessDead &IPD) { 00224 killProgram(); 00225 throw NonErrorException("The program stopped with exit code " + 00226 itostr(IPD.getExitCode())); 00227 } catch (...) { 00228 killProgram(); 00229 throw; 00230 } 00231 }