Move code to redefine INT64_{MIN,MAX} on AIX/PowerPC to a separate header,
[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 <iostream>
20
21 using namespace llvm;
22
23 namespace {
24   // Command line options for specifying the program to debug and options to use
25   cl::opt<std::string>
26   InputFile(cl::desc("<program>"), cl::Positional, cl::init(""));
27
28   cl::list<std::string>
29   InputArgs("args", cl::Positional, cl::desc("<program and arguments>"),
30             cl::ZeroOrMore);
31
32   // Command line options to control various directory related stuff
33   cl::list<std::string>
34   SourceDirectories("directory", cl::value_desc("directory"),
35                     cl::desc("Add directory to the search for source files"));
36   cl::alias SDA("d", cl::desc("Alias for --directory"),
37                 cl::aliasopt(SourceDirectories));
38   
39   cl::opt<std::string>
40   WorkingDirectory("cd", cl::desc("Use directory as current working directory"),
41                    cl::value_desc("directory"));
42
43   // Command line options specific to the llvm-db debugger driver
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   sys::PrintStackTraceOnErrorSignal();
56
57   if (!Quiet)
58     std::cout << "llvm-db: The LLVM source-level debugger\n";
59
60   // Merge Inputfile and InputArgs into the InputArgs list...
61   if (!InputFile.empty() && InputArgs.empty())
62     InputArgs.push_back(InputFile);
63
64   // Create the CLI debugger...
65   CLIDebugger D;
66
67   // Initialize the debugger with the command line options we read...
68   Debugger &Dbg = D.getDebugger();
69
70   // Initialize the debugger environment.
71   Dbg.initializeEnvironment(envp);
72   Dbg.setWorkingDirectory(WorkingDirectory);
73   for (unsigned i = 0, e = SourceDirectories.size(); i != e; ++i)
74     D.addSourceDirectory(SourceDirectories[i]);
75   
76   if (!InputArgs.empty()) {
77     try {
78       D.fileCommand(InputArgs[0]);
79     } catch (const std::string &Error) {
80       std::cout << "Error: " << Error << "\n";
81     }
82
83     Dbg.setProgramArguments(InputArgs.begin()+1, InputArgs.end());
84   }
85
86   // Now that we have initialized the debugger, run it.
87   return D.run();
88 }