For PR780:
[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 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 utility implements a simple text-mode front-end to the LLVM debugger
11 // library.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "CLIDebugger.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/Support/PluginLoader.h"
18 #include "llvm/System/Signals.h"
19 #include "llvm/LinkAllVMCore.h"
20 #include <iostream>
21
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   std::cout << "NOTE: llvm-db is known useless right now.\n";
55   try {
56     cl::ParseCommandLineOptions(argc, argv,
57                                 " llvm source-level debugger\n");
58     sys::PrintStackTraceOnErrorSignal();
59
60     if (!Quiet)
61       std::cout << "llvm-db: The LLVM source-level debugger\n";
62
63     // Merge Inputfile and InputArgs into the InputArgs list...
64     if (!InputFile.empty() && InputArgs.empty())
65       InputArgs.push_back(InputFile);
66
67     // Create the CLI debugger...
68     CLIDebugger D;
69
70     // Initialize the debugger with the command line options we read...
71     Debugger &Dbg = D.getDebugger();
72
73     // Initialize the debugger environment.
74     Dbg.initializeEnvironment(envp);
75     Dbg.setWorkingDirectory(WorkingDirectory);
76     for (unsigned i = 0, e = SourceDirectories.size(); i != e; ++i)
77       D.addSourceDirectory(SourceDirectories[i]);
78
79     if (!InputArgs.empty()) {
80       try {
81         D.fileCommand(InputArgs[0]);
82       } catch (const std::string &Error) {
83         std::cout << "Error: " << Error << "\n";
84       }
85
86       Dbg.setProgramArguments(InputArgs.begin()+1, InputArgs.end());
87     }
88
89     // Now that we have initialized the debugger, run it.
90     return D.run();
91   } catch (const std::string& msg) {
92     std::cerr << argv[0] << ": " << msg << "\n";
93   } catch (...) {
94     std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
95   }
96   return 1;
97 }