blob: e8194d941f003310fb2cbf51b3fecf714cd0f12e [file] [log] [blame]
Chris Lattner2eacf262004-01-05 05:25:10 +00001//===-- Debugger.cpp - LLVM debugger library implementation ---------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner2eacf262004-01-05 05:25:10 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner2eacf262004-01-05 05:25:10 +00008//===----------------------------------------------------------------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00009//
Chris Lattner2eacf262004-01-05 05:25:10 +000010// 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 Lattner2cb1ad92007-05-06 05:18:53 +000017#include "llvm/Bitcode/ReaderWriter.h"
Chris Lattner2eacf262004-01-05 05:25:10 +000018#include "llvm/Debugger/InferiorProcess.h"
Chris Lattner2cb1ad92007-05-06 05:18:53 +000019#include "llvm/Support/MemoryBuffer.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000020#include "llvm/ADT/StringExtras.h"
Duraid Madina4547e222005-12-26 10:24:15 +000021#include <memory>
Chris Lattner2eacf262004-01-05 05:25:10 +000022using namespace llvm;
23
24/// Debugger constructor - Initialize the debugger to its initial, empty, state.
25///
26Debugger::Debugger() : Environment(0), Program(0), Process(0) {
27}
28
29Debugger::~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 &) {
36 }
Misha Brukmanedf128a2005-04-21 22:36:52 +000037
Chris Lattner2eacf262004-01-05 05:25:10 +000038 unloadProgram();
39}
40
41/// getProgramPath - Get the path of the currently loaded program, or an
42/// empty string if none is loaded.
43std::string Debugger::getProgramPath() const {
44 return Program ? Program->getModuleIdentifier() : "";
45}
46
47static Module *
48getMaterializedModuleProvider(const std::string &Filename) {
Chris Lattner4bcca0f2007-05-06 09:29:13 +000049 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;
Chris Lattner2eacf262004-01-05 05:25:10 +000054}
55
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.
60void Debugger::loadProgram(const std::string &Filename) {
61 if ((Program = getMaterializedModuleProvider(Filename)) ||
62 (Program = getMaterializedModuleProvider(Filename+".bc")))
63 return; // Successfully loaded the program.
64
65 // Search the program path for the file...
66 if (const char *PathS = getenv("PATH")) {
67 std::string Path = PathS;
68
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.
75
76 Directory = getToken(Path, ":");
77 }
78 }
79
80 throw "Could not find program '" + Filename + "'!";
81}
82
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.
86void Debugger::unloadProgram() {
87 if (!isProgramLoaded()) return;
88 killProgram();
89 delete Program;
90 Program = 0;
91}
92
93
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.
98void Debugger::createProgram() {
99 if (!isProgramLoaded())
100 throw "Cannot start program: none is loaded.";
101
102 // Kill any existing program.
103 killProgram();
104
105 // Add argv[0] to the arguments vector..
106 std::vector<std::string> Args(ProgramArguments);
107 Args.insert(Args.begin(), getProgramPath());
108
109 // Start the new program... this could throw if the program cannot be started.
110 Process = InferiorProcess::create(Program, Args, Environment);
111}
112
Chris Lattner89b19262006-03-10 22:39:48 +0000113InferiorProcess *
114InferiorProcess::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).";
117}
118
Chris Lattner2eacf262004-01-05 05:25:10 +0000119/// 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.
122void Debugger::killProgram() {
123 // The destructor takes care of the dirty work.
Chris Lattnerde31b762004-01-14 20:58:17 +0000124 try {
125 delete Process;
126 } catch (...) {
127 Process = 0;
128 throw;
129 }
Chris Lattner2eacf262004-01-05 05:25:10 +0000130 Process = 0;
131}
132
133/// stepProgram - Implement the 'step' command, continuing execution until
134/// the next possible stop point.
135void Debugger::stepProgram() {
136 assert(isProgramRunning() && "Cannot step if the program isn't running!");
137 try {
138 Process->stepProgram();
139 } catch (InferiorProcessDead &IPD) {
Chris Lattnerde31b762004-01-14 20:58:17 +0000140 killProgram();
Chris Lattner2eacf262004-01-05 05:25:10 +0000141 throw NonErrorException("The program stopped with exit code " +
142 itostr(IPD.getExitCode()));
Chris Lattnerde31b762004-01-14 20:58:17 +0000143 } catch (...) {
144 killProgram();
145 throw;
Chris Lattner2eacf262004-01-05 05:25:10 +0000146 }
147}
148
149/// nextProgram - Implement the 'next' command, continuing execution until
150/// the next possible stop point that is in the current function.
151void 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'.
164
165 // Get the current frame, but don't trust it. It could change...
166 void *CurrentFrame = Process->getPreviousFrame(0);
167
168 // Don't trust the current frame: get the caller frame.
169 void *ParentFrame = Process->getPreviousFrame(CurrentFrame);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000170
Chris Lattner2eacf262004-01-05 05:25:10 +0000171 // Ok, we have some information, run the program one step.
172 Process->stepProgram();
173
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);
186 }
187 }
188
189 } catch (InferiorProcessDead &IPD) {
Chris Lattnerde31b762004-01-14 20:58:17 +0000190 killProgram();
Chris Lattner2eacf262004-01-05 05:25:10 +0000191 throw NonErrorException("The program stopped with exit code " +
192 itostr(IPD.getExitCode()));
Chris Lattnerde31b762004-01-14 20:58:17 +0000193 } catch (...) {
194 killProgram();
195 throw;
Chris Lattner2eacf262004-01-05 05:25:10 +0000196 }
197}
198
199/// finishProgram - Implement the 'finish' command, continuing execution
200/// until the specified frame ID returns.
201void 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) {
Chris Lattnerde31b762004-01-14 20:58:17 +0000206 killProgram();
Chris Lattner2eacf262004-01-05 05:25:10 +0000207 throw NonErrorException("The program stopped with exit code " +
208 itostr(IPD.getExitCode()));
Chris Lattnerde31b762004-01-14 20:58:17 +0000209 } catch (...) {
210 killProgram();
211 throw;
Chris Lattner2eacf262004-01-05 05:25:10 +0000212 }
213}
214
215/// contProgram - Implement the 'cont' command, continuing execution until
216/// the next breakpoint is encountered.
217void Debugger::contProgram() {
218 assert(isProgramRunning() && "Cannot cont if the program isn't running!");
219 try {
220 Process->contProgram();
221 } catch (InferiorProcessDead &IPD) {
Chris Lattnerde31b762004-01-14 20:58:17 +0000222 killProgram();
Chris Lattner2eacf262004-01-05 05:25:10 +0000223 throw NonErrorException("The program stopped with exit code " +
224 itostr(IPD.getExitCode()));
Chris Lattnerde31b762004-01-14 20:58:17 +0000225 } catch (...) {
226 killProgram();
227 throw;
Chris Lattner2eacf262004-01-05 05:25:10 +0000228 }
229}