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