1 //===-- Debugger.cpp - LLVM debugger library implementation ---------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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/Bytecode/Reader.h"
18 #include "llvm/Bitcode/ReaderWriter.h"
19 #include "llvm/Debugger/InferiorProcess.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/ADT/StringExtras.h"
25 static bool Bitcode = false;
27 /// Debugger constructor - Initialize the debugger to its initial, empty, state.
29 Debugger::Debugger() : Environment(0), Program(0), Process(0) {
32 Debugger::~Debugger() {
33 // Killing the program could throw an exception. We don't want to progagate
34 // the exception out of our destructor though.
37 } catch (const char *) {
38 } catch (const std::string &) {
44 /// getProgramPath - Get the path of the currently loaded program, or an
45 /// empty string if none is loaded.
46 std::string Debugger::getProgramPath() const {
47 return Program ? Program->getModuleIdentifier() : "";
51 getMaterializedModuleProvider(const std::string &Filename) {
53 return ParseBytecodeFile(Filename);
55 std::auto_ptr<MemoryBuffer> Buffer;
56 Buffer.reset(MemoryBuffer::getFileOrSTDIN(&Filename[0], Filename.size()));
58 return ParseBitcodeFile(Buffer.get());
63 /// loadProgram - If a program is currently loaded, unload it. Then search
64 /// the PATH for the specified program, loading it when found. If the
65 /// specified program cannot be found, an exception is thrown to indicate the
67 void Debugger::loadProgram(const std::string &Filename) {
68 if ((Program = getMaterializedModuleProvider(Filename)) ||
69 (Program = getMaterializedModuleProvider(Filename+".bc")))
70 return; // Successfully loaded the program.
72 // Search the program path for the file...
73 if (const char *PathS = getenv("PATH")) {
74 std::string Path = PathS;
76 std::string Directory = getToken(Path, ":");
77 while (!Directory.empty()) {
78 if ((Program = getMaterializedModuleProvider(Directory +"/"+ Filename)) ||
79 (Program = getMaterializedModuleProvider(Directory +"/"+ Filename
81 return; // Successfully loaded the program.
83 Directory = getToken(Path, ":");
87 throw "Could not find program '" + Filename + "'!";
90 /// unloadProgram - If a program is running, kill it, then unload all traces
91 /// of the current program. If no program is loaded, this method silently
93 void Debugger::unloadProgram() {
94 if (!isProgramLoaded()) return;
101 /// createProgram - Create an instance of the currently loaded program,
102 /// killing off any existing one. This creates the program and stops it at
103 /// the first possible moment. If there is no program loaded or if there is a
104 /// problem starting the program, this method throws an exception.
105 void Debugger::createProgram() {
106 if (!isProgramLoaded())
107 throw "Cannot start program: none is loaded.";
109 // Kill any existing program.
112 // Add argv[0] to the arguments vector..
113 std::vector<std::string> Args(ProgramArguments);
114 Args.insert(Args.begin(), getProgramPath());
116 // Start the new program... this could throw if the program cannot be started.
117 Process = InferiorProcess::create(Program, Args, Environment);
121 InferiorProcess::create(Module *M, const std::vector<std::string> &Arguments,
122 const char * const *envp) {
123 throw"No supported binding to inferior processes (debugger not implemented).";
126 /// killProgram - If the program is currently executing, kill off the
127 /// process and free up any state related to the currently running program. If
128 /// there is no program currently running, this just silently succeeds.
129 void Debugger::killProgram() {
130 // The destructor takes care of the dirty work.
140 /// stepProgram - Implement the 'step' command, continuing execution until
141 /// the next possible stop point.
142 void Debugger::stepProgram() {
143 assert(isProgramRunning() && "Cannot step if the program isn't running!");
145 Process->stepProgram();
146 } catch (InferiorProcessDead &IPD) {
148 throw NonErrorException("The program stopped with exit code " +
149 itostr(IPD.getExitCode()));
156 /// nextProgram - Implement the 'next' command, continuing execution until
157 /// the next possible stop point that is in the current function.
158 void Debugger::nextProgram() {
159 assert(isProgramRunning() && "Cannot next if the program isn't running!");
161 // This should step the process. If the process enters a function, then it
162 // should 'finish' it. However, figuring this out is tricky. In
163 // particular, the program can do any of:
164 // 0. Not change current frame.
165 // 1. Entering or exiting a region within the current function
166 // (which changes the frame ID, but which we shouldn't 'finish')
167 // 2. Exiting the current function (which changes the frame ID)
168 // 3. Entering a function (which should be 'finish'ed)
169 // For this reason, we have to be very careful about when we decide to do
172 // Get the current frame, but don't trust it. It could change...
173 void *CurrentFrame = Process->getPreviousFrame(0);
175 // Don't trust the current frame: get the caller frame.
176 void *ParentFrame = Process->getPreviousFrame(CurrentFrame);
178 // Ok, we have some information, run the program one step.
179 Process->stepProgram();
181 // Where is the new frame? The most common case, by far is that it has not
182 // been modified (Case #0), in which case we don't need to do anything more.
183 void *NewFrame = Process->getPreviousFrame(0);
184 if (NewFrame != CurrentFrame) {
185 // Ok, the frame changed. If we are case #1, then the parent frame will
187 void *NewParentFrame = Process->getPreviousFrame(NewFrame);
188 if (ParentFrame != NewParentFrame) {
189 // Ok, now we know we aren't case #0 or #1. Check to see if we entered
190 // a new function. If so, the parent frame will be "CurrentFrame".
191 if (CurrentFrame == NewParentFrame)
192 Process->finishProgram(NewFrame);
196 } catch (InferiorProcessDead &IPD) {
198 throw NonErrorException("The program stopped with exit code " +
199 itostr(IPD.getExitCode()));
206 /// finishProgram - Implement the 'finish' command, continuing execution
207 /// until the specified frame ID returns.
208 void Debugger::finishProgram(void *Frame) {
209 assert(isProgramRunning() && "Cannot cont if the program isn't running!");
211 Process->finishProgram(Frame);
212 } catch (InferiorProcessDead &IPD) {
214 throw NonErrorException("The program stopped with exit code " +
215 itostr(IPD.getExitCode()));
222 /// contProgram - Implement the 'cont' command, continuing execution until
223 /// the next breakpoint is encountered.
224 void Debugger::contProgram() {
225 assert(isProgramRunning() && "Cannot cont if the program isn't running!");
227 Process->contProgram();
228 } catch (InferiorProcessDead &IPD) {
230 throw NonErrorException("The program stopped with exit code " +
231 itostr(IPD.getExitCode()));