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