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/Debugger/InferiorProcess.h"
19 #include "llvm/ADT/StringExtras.h"
22 /// Debugger constructor - Initialize the debugger to its initial, empty, state.
24 Debugger::Debugger() : Environment(0), Program(0), Process(0) {
27 Debugger::~Debugger() {
28 // Killing the program could throw an exception. We don't want to progagate
29 // the exception out of our destructor though.
32 } catch (const char *) {
33 } catch (const std::string &) {
39 /// getProgramPath - Get the path of the currently loaded program, or an
40 /// empty string if none is loaded.
41 std::string Debugger::getProgramPath() const {
42 return Program ? Program->getModuleIdentifier() : "";
46 getMaterializedModuleProvider(const std::string &Filename) {
48 std::auto_ptr<ModuleProvider> Result(getBytecodeModuleProvider(Filename));
49 if (!Result.get()) return 0;
51 Result->materializeModule();
52 return Result.release()->releaseModule();
58 /// loadProgram - If a program is currently loaded, unload it. Then search
59 /// the PATH for the specified program, loading it when found. If the
60 /// specified program cannot be found, an exception is thrown to indicate the
62 void Debugger::loadProgram(const std::string &Filename) {
63 if ((Program = getMaterializedModuleProvider(Filename)) ||
64 (Program = getMaterializedModuleProvider(Filename+".bc")))
65 return; // Successfully loaded the program.
67 // Search the program path for the file...
68 if (const char *PathS = getenv("PATH")) {
69 std::string Path = PathS;
71 std::string Directory = getToken(Path, ":");
72 while (!Directory.empty()) {
73 if ((Program = getMaterializedModuleProvider(Directory +"/"+ Filename)) ||
74 (Program = getMaterializedModuleProvider(Directory +"/"+ Filename
76 return; // Successfully loaded the program.
78 Directory = getToken(Path, ":");
82 throw "Could not find program '" + Filename + "'!";
85 /// unloadProgram - If a program is running, kill it, then unload all traces
86 /// of the current program. If no program is loaded, this method silently
88 void Debugger::unloadProgram() {
89 if (!isProgramLoaded()) return;
96 /// createProgram - Create an instance of the currently loaded program,
97 /// killing off any existing one. This creates the program and stops it at
98 /// the first possible moment. If there is no program loaded or if there is a
99 /// problem starting the program, this method throws an exception.
100 void Debugger::createProgram() {
101 if (!isProgramLoaded())
102 throw "Cannot start program: none is loaded.";
104 // Kill any existing program.
107 // Add argv[0] to the arguments vector..
108 std::vector<std::string> Args(ProgramArguments);
109 Args.insert(Args.begin(), getProgramPath());
111 // Start the new program... this could throw if the program cannot be started.
112 Process = InferiorProcess::create(Program, Args, Environment);
115 /// killProgram - If the program is currently executing, kill off the
116 /// process and free up any state related to the currently running program. If
117 /// there is no program currently running, this just silently succeeds.
118 void Debugger::killProgram() {
119 // The destructor takes care of the dirty work.
129 /// stepProgram - Implement the 'step' command, continuing execution until
130 /// the next possible stop point.
131 void Debugger::stepProgram() {
132 assert(isProgramRunning() && "Cannot step if the program isn't running!");
134 Process->stepProgram();
135 } catch (InferiorProcessDead &IPD) {
137 throw NonErrorException("The program stopped with exit code " +
138 itostr(IPD.getExitCode()));
145 /// nextProgram - Implement the 'next' command, continuing execution until
146 /// the next possible stop point that is in the current function.
147 void Debugger::nextProgram() {
148 assert(isProgramRunning() && "Cannot next if the program isn't running!");
150 // This should step the process. If the process enters a function, then it
151 // should 'finish' it. However, figuring this out is tricky. In
152 // particular, the program can do any of:
153 // 0. Not change current frame.
154 // 1. Entering or exiting a region within the current function
155 // (which changes the frame ID, but which we shouldn't 'finish')
156 // 2. Exiting the current function (which changes the frame ID)
157 // 3. Entering a function (which should be 'finish'ed)
158 // For this reason, we have to be very careful about when we decide to do
161 // Get the current frame, but don't trust it. It could change...
162 void *CurrentFrame = Process->getPreviousFrame(0);
164 // Don't trust the current frame: get the caller frame.
165 void *ParentFrame = Process->getPreviousFrame(CurrentFrame);
167 // Ok, we have some information, run the program one step.
168 Process->stepProgram();
170 // Where is the new frame? The most common case, by far is that it has not
171 // been modified (Case #0), in which case we don't need to do anything more.
172 void *NewFrame = Process->getPreviousFrame(0);
173 if (NewFrame != CurrentFrame) {
174 // Ok, the frame changed. If we are case #1, then the parent frame will
176 void *NewParentFrame = Process->getPreviousFrame(NewFrame);
177 if (ParentFrame != NewParentFrame) {
178 // Ok, now we know we aren't case #0 or #1. Check to see if we entered
179 // a new function. If so, the parent frame will be "CurrentFrame".
180 if (CurrentFrame == NewParentFrame)
181 Process->finishProgram(NewFrame);
185 } catch (InferiorProcessDead &IPD) {
187 throw NonErrorException("The program stopped with exit code " +
188 itostr(IPD.getExitCode()));
195 /// finishProgram - Implement the 'finish' command, continuing execution
196 /// until the specified frame ID returns.
197 void Debugger::finishProgram(void *Frame) {
198 assert(isProgramRunning() && "Cannot cont if the program isn't running!");
200 Process->finishProgram(Frame);
201 } catch (InferiorProcessDead &IPD) {
203 throw NonErrorException("The program stopped with exit code " +
204 itostr(IPD.getExitCode()));
211 /// contProgram - Implement the 'cont' command, continuing execution until
212 /// the next breakpoint is encountered.
213 void Debugger::contProgram() {
214 assert(isProgramRunning() && "Cannot cont if the program isn't running!");
216 Process->contProgram();
217 } catch (InferiorProcessDead &IPD) {
219 throw NonErrorException("The program stopped with exit code " +
220 itostr(IPD.getExitCode()));