Make sure to print a stack trace whenever an error signal is delivered to the
[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 "Support/CommandLine.h"
17 #include "Support/Signals.h"
18 #include <iostream>
19
20 using namespace llvm;
21
22 namespace {
23   // Command line options for specifying the program to debug and options to use
24   cl::opt<std::string>
25   InputFile(cl::desc("<program>"), cl::Positional, cl::init(""));
26
27   cl::list<std::string>
28   InputArgs("args", cl::Positional, cl::desc("<program and arguments>"),
29             cl::ZeroOrMore);
30
31   // Command line options to control various directory related stuff
32   cl::list<std::string>
33   SourceDirectories("directory", cl::value_desc("directory"),
34                     cl::desc("Add directory to the search for source files"));
35   cl::alias SDA("d", cl::desc("Alias for --directory"),
36                 cl::aliasopt(SourceDirectories));
37   
38   cl::opt<std::string>
39   WorkingDirectory("cd", cl::desc("Use directory as current working directory"),
40                    cl::value_desc("directory"));
41
42   // Command line options specific to the llvm-db debugger driver
43   cl::opt<bool> Version("version", cl::desc("Print version number and quit"));
44   cl::opt<bool> Quiet("quiet", cl::desc("Do not print introductory messages"));
45   cl::alias QA1("silent", cl::desc("Alias for -quiet"), cl::aliasopt(Quiet));
46   cl::alias QA2("q", cl::desc("Alias for -quiet"), cl::aliasopt(Quiet));
47 }
48
49 //===----------------------------------------------------------------------===//
50 // main Driver function
51 //
52 int main(int argc, char **argv, char * const *envp) {
53   cl::ParseCommandLineOptions(argc, argv,
54                               " llvm source-level debugger\n");
55   PrintStackTraceOnErrorSignal();
56
57   if (Version || !Quiet) {
58     std::cout << "llvm-db: The LLVM source-level debugger\n";
59     if (Version) return 1;
60   }
61
62   // Merge Inputfile and InputArgs into the InputArgs list...
63   if (!InputFile.empty() && InputArgs.empty())
64     InputArgs.push_back(InputFile);
65
66   // Create the CLI debugger...
67   CLIDebugger D;
68
69   // Initialize the debugger with the command line options we read...
70   Debugger &Dbg = D.getDebugger();
71
72   // Initialize the debugger environment.
73   Dbg.initializeEnvironment(envp);
74   Dbg.setWorkingDirectory(WorkingDirectory);
75   for (unsigned i = 0, e = SourceDirectories.size(); i != e; ++i)
76     D.addSourceDirectory(SourceDirectories[i]);
77   
78   if (!InputArgs.empty()) {
79     try {
80       D.fileCommand(InputArgs[0]);
81     } catch (const std::string &Error) {
82       std::cout << "Error: " << Error << "\n";
83     }
84
85     Dbg.setProgramArguments(InputArgs.begin()+1, InputArgs.end());
86   }
87
88   // Now that we have initialized the debugger, run it.
89   return D.run();
90 }