Hold the LLVMContext by reference rather than by pointer.
[oota-llvm.git] / tools / llvm-db / llvm-db.cpp
1 //===- llvm-db.cpp - LLVM Debugger ----------------------------------------===//
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 utility implements a simple text-mode front-end to the LLVM debugger
11 // library.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "CLIDebugger.h"
16 #include "llvm/LLVMContext.h"
17 #include "llvm/Support/CommandLine.h"
18 #include "llvm/Support/ManagedStatic.h"
19 #include "llvm/Support/PrettyStackTrace.h"
20 #include "llvm/System/Signals.h"
21 #include <iostream>
22 using namespace llvm;
23
24 namespace {
25   // Command line options for specifying the program to debug and options to use
26   cl::opt<std::string>
27   InputFile(cl::desc("<program>"), cl::Positional, cl::init(""));
28
29   cl::list<std::string>
30   InputArgs("args", cl::Positional, cl::desc("<program and arguments>"),
31             cl::ZeroOrMore);
32
33   // Command line options to control various directory related stuff
34   cl::list<std::string>
35   SourceDirectories("directory", cl::value_desc("directory"),
36                     cl::desc("Add directory to the search for source files"));
37   cl::alias SDA("d", cl::desc("Alias for --directory"),
38                 cl::aliasopt(SourceDirectories));
39
40   cl::opt<std::string>
41   WorkingDirectory("cd", cl::desc("Use directory as current working directory"),
42                    cl::value_desc("directory"));
43
44   // Command line options specific to the llvm-db debugger driver
45   cl::opt<bool> Quiet("quiet", cl::desc("Do not print introductory messages"));
46   cl::alias QA1("silent", cl::desc("Alias for -quiet"), cl::aliasopt(Quiet));
47   cl::alias QA2("q", cl::desc("Alias for -quiet"), cl::aliasopt(Quiet));
48 }
49
50 //===----------------------------------------------------------------------===//
51 // main Driver function
52 //
53 int main(int argc, char **argv, char * const *envp) {
54   // Print a stack trace if we signal out.
55   sys::PrintStackTraceOnErrorSignal();
56   PrettyStackTraceProgram X(argc, argv);
57   
58   LLVMContext Context;
59   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
60   std::cout << "NOTE: llvm-db is known useless right now.\n";
61   try {
62     cl::ParseCommandLineOptions(argc, argv,
63                                 "llvm source-level debugger\n");
64
65     if (!Quiet)
66       std::cout << "llvm-db: The LLVM source-level debugger\n";
67
68     // Merge Inputfile and InputArgs into the InputArgs list...
69     if (!InputFile.empty() && InputArgs.empty())
70       InputArgs.push_back(InputFile);
71
72     // Create the CLI debugger...
73     CLIDebugger D(Context);
74
75     // Initialize the debugger with the command line options we read...
76     Debugger &Dbg = D.getDebugger();
77
78     // Initialize the debugger environment.
79     Dbg.initializeEnvironment(envp);
80     Dbg.setWorkingDirectory(WorkingDirectory);
81     for (unsigned i = 0, e = SourceDirectories.size(); i != e; ++i)
82       D.addSourceDirectory(SourceDirectories[i]);
83
84     if (!InputArgs.empty()) {
85       try {
86         D.fileCommand(InputArgs[0]);
87       } catch (const std::string &Error) {
88         std::cout << "Error: " << Error << "\n";
89       }
90
91       Dbg.setProgramArguments(InputArgs.begin()+1, InputArgs.end());
92     }
93
94     // Now that we have initialized the debugger, run it.
95     return D.run();
96   } catch (const std::string& msg) {
97     std::cerr << argv[0] << ": " << msg << "\n";
98   } catch (...) {
99     std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
100   }
101   return 1;
102 }