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