Debugger.cpp 7.9 KB

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