Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 1 | //===-- Debugger.cpp - LLVM debugger library implementation ---------------===// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 10 | // This file contains the main implementation of the LLVM debugger library. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Debugger/Debugger.h" |
| 15 | #include "llvm/Module.h" |
| 16 | #include "llvm/ModuleProvider.h" |
Chris Lattner | 2cb1ad9 | 2007-05-06 05:18:53 +0000 | [diff] [blame] | 17 | #include "llvm/Bitcode/ReaderWriter.h" |
Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 18 | #include "llvm/Debugger/InferiorProcess.h" |
Chris Lattner | 2cb1ad9 | 2007-05-06 05:18:53 +0000 | [diff] [blame] | 19 | #include "llvm/Support/MemoryBuffer.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringExtras.h" |
Dan Gohman | d68a076 | 2009-01-05 17:59:02 +0000 | [diff] [blame^] | 21 | #include <cstdlib> |
Duraid Madina | 4547e22 | 2005-12-26 10:24:15 +0000 | [diff] [blame] | 22 | #include <memory> |
Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
| 25 | /// Debugger constructor - Initialize the debugger to its initial, empty, state. |
| 26 | /// |
| 27 | Debugger::Debugger() : Environment(0), Program(0), Process(0) { |
| 28 | } |
| 29 | |
| 30 | Debugger::~Debugger() { |
| 31 | // Killing the program could throw an exception. We don't want to progagate |
| 32 | // the exception out of our destructor though. |
| 33 | try { |
| 34 | killProgram(); |
| 35 | } catch (const char *) { |
| 36 | } catch (const std::string &) { |
| 37 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 39 | unloadProgram(); |
| 40 | } |
| 41 | |
| 42 | /// getProgramPath - Get the path of the currently loaded program, or an |
| 43 | /// empty string if none is loaded. |
| 44 | std::string Debugger::getProgramPath() const { |
| 45 | return Program ? Program->getModuleIdentifier() : ""; |
| 46 | } |
| 47 | |
| 48 | static Module * |
| 49 | getMaterializedModuleProvider(const std::string &Filename) { |
Chris Lattner | 4bcca0f | 2007-05-06 09:29:13 +0000 | [diff] [blame] | 50 | std::auto_ptr<MemoryBuffer> Buffer; |
Chris Lattner | 038112a | 2008-04-01 18:04:03 +0000 | [diff] [blame] | 51 | Buffer.reset(MemoryBuffer::getFileOrSTDIN(Filename.c_str())); |
Chris Lattner | 4bcca0f | 2007-05-06 09:29:13 +0000 | [diff] [blame] | 52 | if (Buffer.get()) |
| 53 | return ParseBitcodeFile(Buffer.get()); |
| 54 | return 0; |
Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /// loadProgram - If a program is currently loaded, unload it. Then search |
| 58 | /// the PATH for the specified program, loading it when found. If the |
| 59 | /// specified program cannot be found, an exception is thrown to indicate the |
| 60 | /// error. |
| 61 | void Debugger::loadProgram(const std::string &Filename) { |
| 62 | if ((Program = getMaterializedModuleProvider(Filename)) || |
| 63 | (Program = getMaterializedModuleProvider(Filename+".bc"))) |
| 64 | return; // Successfully loaded the program. |
| 65 | |
| 66 | // Search the program path for the file... |
| 67 | if (const char *PathS = getenv("PATH")) { |
| 68 | std::string Path = PathS; |
| 69 | |
| 70 | std::string Directory = getToken(Path, ":"); |
| 71 | while (!Directory.empty()) { |
| 72 | if ((Program = getMaterializedModuleProvider(Directory +"/"+ Filename)) || |
| 73 | (Program = getMaterializedModuleProvider(Directory +"/"+ Filename |
| 74 | + ".bc"))) |
| 75 | return; // Successfully loaded the program. |
| 76 | |
| 77 | Directory = getToken(Path, ":"); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | throw "Could not find program '" + Filename + "'!"; |
| 82 | } |
| 83 | |
| 84 | /// unloadProgram - If a program is running, kill it, then unload all traces |
| 85 | /// of the current program. If no program is loaded, this method silently |
| 86 | /// succeeds. |
| 87 | void Debugger::unloadProgram() { |
| 88 | if (!isProgramLoaded()) return; |
| 89 | killProgram(); |
| 90 | delete Program; |
| 91 | Program = 0; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | /// createProgram - Create an instance of the currently loaded program, |
| 96 | /// killing off any existing one. This creates the program and stops it at |
| 97 | /// the first possible moment. If there is no program loaded or if there is a |
| 98 | /// problem starting the program, this method throws an exception. |
| 99 | void Debugger::createProgram() { |
| 100 | if (!isProgramLoaded()) |
| 101 | throw "Cannot start program: none is loaded."; |
| 102 | |
| 103 | // Kill any existing program. |
| 104 | killProgram(); |
| 105 | |
| 106 | // Add argv[0] to the arguments vector.. |
| 107 | std::vector<std::string> Args(ProgramArguments); |
| 108 | Args.insert(Args.begin(), getProgramPath()); |
| 109 | |
| 110 | // Start the new program... this could throw if the program cannot be started. |
| 111 | Process = InferiorProcess::create(Program, Args, Environment); |
| 112 | } |
| 113 | |
Chris Lattner | 89b1926 | 2006-03-10 22:39:48 +0000 | [diff] [blame] | 114 | InferiorProcess * |
| 115 | InferiorProcess::create(Module *M, const std::vector<std::string> &Arguments, |
| 116 | const char * const *envp) { |
| 117 | throw"No supported binding to inferior processes (debugger not implemented)."; |
| 118 | } |
| 119 | |
Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 120 | /// killProgram - If the program is currently executing, kill off the |
| 121 | /// process and free up any state related to the currently running program. If |
| 122 | /// there is no program currently running, this just silently succeeds. |
| 123 | void Debugger::killProgram() { |
| 124 | // The destructor takes care of the dirty work. |
Chris Lattner | de31b76 | 2004-01-14 20:58:17 +0000 | [diff] [blame] | 125 | try { |
| 126 | delete Process; |
| 127 | } catch (...) { |
| 128 | Process = 0; |
| 129 | throw; |
| 130 | } |
Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 131 | Process = 0; |
| 132 | } |
| 133 | |
| 134 | /// stepProgram - Implement the 'step' command, continuing execution until |
| 135 | /// the next possible stop point. |
| 136 | void Debugger::stepProgram() { |
| 137 | assert(isProgramRunning() && "Cannot step if the program isn't running!"); |
| 138 | try { |
| 139 | Process->stepProgram(); |
| 140 | } catch (InferiorProcessDead &IPD) { |
Chris Lattner | de31b76 | 2004-01-14 20:58:17 +0000 | [diff] [blame] | 141 | killProgram(); |
Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 142 | throw NonErrorException("The program stopped with exit code " + |
| 143 | itostr(IPD.getExitCode())); |
Chris Lattner | de31b76 | 2004-01-14 20:58:17 +0000 | [diff] [blame] | 144 | } catch (...) { |
| 145 | killProgram(); |
| 146 | throw; |
Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | /// nextProgram - Implement the 'next' command, continuing execution until |
| 151 | /// the next possible stop point that is in the current function. |
| 152 | void Debugger::nextProgram() { |
| 153 | assert(isProgramRunning() && "Cannot next if the program isn't running!"); |
| 154 | try { |
| 155 | // This should step the process. If the process enters a function, then it |
| 156 | // should 'finish' it. However, figuring this out is tricky. In |
| 157 | // particular, the program can do any of: |
| 158 | // 0. Not change current frame. |
| 159 | // 1. Entering or exiting a region within the current function |
| 160 | // (which changes the frame ID, but which we shouldn't 'finish') |
| 161 | // 2. Exiting the current function (which changes the frame ID) |
| 162 | // 3. Entering a function (which should be 'finish'ed) |
| 163 | // For this reason, we have to be very careful about when we decide to do |
| 164 | // the 'finish'. |
| 165 | |
| 166 | // Get the current frame, but don't trust it. It could change... |
| 167 | void *CurrentFrame = Process->getPreviousFrame(0); |
| 168 | |
| 169 | // Don't trust the current frame: get the caller frame. |
| 170 | void *ParentFrame = Process->getPreviousFrame(CurrentFrame); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 171 | |
Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 172 | // Ok, we have some information, run the program one step. |
| 173 | Process->stepProgram(); |
| 174 | |
| 175 | // Where is the new frame? The most common case, by far is that it has not |
| 176 | // been modified (Case #0), in which case we don't need to do anything more. |
| 177 | void *NewFrame = Process->getPreviousFrame(0); |
| 178 | if (NewFrame != CurrentFrame) { |
| 179 | // Ok, the frame changed. If we are case #1, then the parent frame will |
| 180 | // be identical. |
| 181 | void *NewParentFrame = Process->getPreviousFrame(NewFrame); |
| 182 | if (ParentFrame != NewParentFrame) { |
| 183 | // Ok, now we know we aren't case #0 or #1. Check to see if we entered |
| 184 | // a new function. If so, the parent frame will be "CurrentFrame". |
| 185 | if (CurrentFrame == NewParentFrame) |
| 186 | Process->finishProgram(NewFrame); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | } catch (InferiorProcessDead &IPD) { |
Chris Lattner | de31b76 | 2004-01-14 20:58:17 +0000 | [diff] [blame] | 191 | killProgram(); |
Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 192 | throw NonErrorException("The program stopped with exit code " + |
| 193 | itostr(IPD.getExitCode())); |
Chris Lattner | de31b76 | 2004-01-14 20:58:17 +0000 | [diff] [blame] | 194 | } catch (...) { |
| 195 | killProgram(); |
| 196 | throw; |
Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
| 200 | /// finishProgram - Implement the 'finish' command, continuing execution |
| 201 | /// until the specified frame ID returns. |
| 202 | void Debugger::finishProgram(void *Frame) { |
| 203 | assert(isProgramRunning() && "Cannot cont if the program isn't running!"); |
| 204 | try { |
| 205 | Process->finishProgram(Frame); |
| 206 | } catch (InferiorProcessDead &IPD) { |
Chris Lattner | de31b76 | 2004-01-14 20:58:17 +0000 | [diff] [blame] | 207 | killProgram(); |
Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 208 | throw NonErrorException("The program stopped with exit code " + |
| 209 | itostr(IPD.getExitCode())); |
Chris Lattner | de31b76 | 2004-01-14 20:58:17 +0000 | [diff] [blame] | 210 | } catch (...) { |
| 211 | killProgram(); |
| 212 | throw; |
Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
| 216 | /// contProgram - Implement the 'cont' command, continuing execution until |
| 217 | /// the next breakpoint is encountered. |
| 218 | void Debugger::contProgram() { |
| 219 | assert(isProgramRunning() && "Cannot cont if the program isn't running!"); |
| 220 | try { |
| 221 | Process->contProgram(); |
| 222 | } catch (InferiorProcessDead &IPD) { |
Chris Lattner | de31b76 | 2004-01-14 20:58:17 +0000 | [diff] [blame] | 223 | killProgram(); |
Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 224 | throw NonErrorException("The program stopped with exit code " + |
| 225 | itostr(IPD.getExitCode())); |
Chris Lattner | de31b76 | 2004-01-14 20:58:17 +0000 | [diff] [blame] | 226 | } catch (...) { |
| 227 | killProgram(); |
| 228 | throw; |
Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 229 | } |
| 230 | } |