Make sure to initialize the fpm in the ocaml tutorial.
[oota-llvm.git] / lib / Debugger / Debugger.cpp
index fd11f23b37ba57f8c1c5a2c65179db82d20eb3fb..77fd2ac96bea03ecaced4f74cebedccca08da951 100644 (file)
@@ -1,12 +1,12 @@
 //===-- Debugger.cpp - LLVM debugger library implementation ---------------===//
-// 
+//
 //                     The LLVM Compiler Infrastructure
 //
-// This file was developed by the LLVM research group and is distributed under
-// the University of Illinois Open Source License. See LICENSE.TXT for details.
-// 
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
 //===----------------------------------------------------------------------===//
-// 
+//
 // This file contains the main implementation of the LLVM debugger library.
 //
 //===----------------------------------------------------------------------===//
 #include "llvm/Debugger/Debugger.h"
 #include "llvm/Module.h"
 #include "llvm/ModuleProvider.h"
-#include "llvm/Bytecode/Reader.h"
+#include "llvm/Bitcode/ReaderWriter.h"
 #include "llvm/Debugger/InferiorProcess.h"
-#include "Support/StringExtras.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/ADT/StringExtras.h"
+#include <cstdlib>
+#include <memory>
 using namespace llvm;
 
 /// Debugger constructor - Initialize the debugger to its initial, empty, state.
@@ -32,7 +35,7 @@ Debugger::~Debugger() {
   } catch (const char *) {
   } catch (const std::string &) {
   }
-  
+
   unloadProgram();
 }
 
@@ -43,25 +46,22 @@ std::string Debugger::getProgramPath() const {
 }
 
 static Module *
-getMaterializedModuleProvider(const std::string &Filename) {
-  try {
-    std::auto_ptr<ModuleProvider> Result(getBytecodeModuleProvider(Filename));
-    if (!Result.get()) return 0;
-  
-    Result->materializeModule();
-    return Result.release()->releaseModule();
-  } catch (...) {
-    return 0;
-  }
+getMaterializedModuleProvider(const std::string &Filename,
+                              LLVMContext& C) {
+  std::auto_ptr<MemoryBuffer> Buffer;
+  Buffer.reset(MemoryBuffer::getFileOrSTDIN(Filename.c_str()));
+  if (Buffer.get())
+    return ParseBitcodeFile(Buffer.get(), C);
+  return 0;
 }
 
 /// loadProgram - If a program is currently loaded, unload it.  Then search
 /// the PATH for the specified program, loading it when found.  If the
 /// specified program cannot be found, an exception is thrown to indicate the
 /// error.
-void Debugger::loadProgram(const std::string &Filename) {
-  if ((Program = getMaterializedModuleProvider(Filename)) ||
-      (Program = getMaterializedModuleProvider(Filename+".bc")))
+void Debugger::loadProgram(const std::string &Filename, LLVMContext& C) {
+  if ((Program = getMaterializedModuleProvider(Filename, C)) ||
+      (Program = getMaterializedModuleProvider(Filename+".bc", C)))
     return;   // Successfully loaded the program.
 
   // Search the program path for the file...
@@ -70,9 +70,9 @@ void Debugger::loadProgram(const std::string &Filename) {
 
     std::string Directory = getToken(Path, ":");
     while (!Directory.empty()) {
-      if ((Program = getMaterializedModuleProvider(Directory +"/"+ Filename)) ||
-          (Program = getMaterializedModuleProvider(Directory +"/"+ Filename
-                                                                      + ".bc")))
+      if ((Program = getMaterializedModuleProvider(Directory +"/"+ Filename, C))
+       || (Program = getMaterializedModuleProvider(Directory +"/"+ Filename
+                                                                   + ".bc", C)))
         return;   // Successfully loaded the program.
 
       Directory = getToken(Path, ":");
@@ -112,12 +112,23 @@ void Debugger::createProgram() {
   Process = InferiorProcess::create(Program, Args, Environment);
 }
 
+InferiorProcess *
+InferiorProcess::create(Module *M, const std::vector<std::string> &Arguments,
+                        const char * const *envp) {
+  throw"No supported binding to inferior processes (debugger not implemented).";
+}
+
 /// killProgram - If the program is currently executing, kill off the
 /// process and free up any state related to the currently running program.  If
 /// there is no program currently running, this just silently succeeds.
 void Debugger::killProgram() {
   // The destructor takes care of the dirty work.
-  delete Process;
+  try {
+    delete Process;
+  } catch (...) {
+    Process = 0;
+    throw;
+  }
   Process = 0;
 }
 
@@ -128,10 +139,12 @@ void Debugger::stepProgram() {
   try {
     Process->stepProgram();
   } catch (InferiorProcessDead &IPD) {
-    delete Process;
-    Process = 0;
+    killProgram();
     throw NonErrorException("The program stopped with exit code " +
                             itostr(IPD.getExitCode()));
+  } catch (...) {
+    killProgram();
+    throw;
   }
 }
 
@@ -156,7 +169,7 @@ void Debugger::nextProgram() {
 
     // Don't trust the current frame: get the caller frame.
     void *ParentFrame  = Process->getPreviousFrame(CurrentFrame);
-    
+
     // Ok, we have some information, run the program one step.
     Process->stepProgram();
 
@@ -176,10 +189,12 @@ void Debugger::nextProgram() {
     }
 
   } catch (InferiorProcessDead &IPD) {
-    delete Process;
-    Process = 0;
+    killProgram();
     throw NonErrorException("The program stopped with exit code " +
                             itostr(IPD.getExitCode()));
+  } catch (...) {
+    killProgram();
+    throw;
   }
 }
 
@@ -190,10 +205,12 @@ void Debugger::finishProgram(void *Frame) {
   try {
     Process->finishProgram(Frame);
   } catch (InferiorProcessDead &IPD) {
-    delete Process;
-    Process = 0;
+    killProgram();
     throw NonErrorException("The program stopped with exit code " +
                             itostr(IPD.getExitCode()));
+  } catch (...) {
+    killProgram();
+    throw;
   }
 }
 
@@ -204,9 +221,11 @@ void Debugger::contProgram() {
   try {
     Process->contProgram();
   } catch (InferiorProcessDead &IPD) {
-    delete Process;
-    Process = 0;
+    killProgram();
     throw NonErrorException("The program stopped with exit code " +
                             itostr(IPD.getExitCode()));
+  } catch (...) {
+    killProgram();
+    throw;
   }
 }