blob: 04e616227b240c0dc5381c75bc0b0a3d765508bc [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- llvm-db.cpp - LLVM Debugger ----------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5f5a5732007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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/ManagedStatic.h"
Chris Lattnere6012df2009-03-06 05:34:10 +000018#include "llvm/Support/PrettyStackTrace.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "llvm/System/Signals.h"
20#include <iostream>
21using namespace llvm;
22
23namespace {
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//
52int main(int argc, char **argv, char * const *envp) {
Chris Lattnere6012df2009-03-06 05:34:10 +000053 // Print a stack trace if we signal out.
54 sys::PrintStackTraceOnErrorSignal();
55 PrettyStackTraceProgram X(argc, argv);
56
57 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058 std::cout << "NOTE: llvm-db is known useless right now.\n";
59 try {
60 cl::ParseCommandLineOptions(argc, argv,
Dan Gohman6099df82007-10-08 15:45:12 +000061 "llvm source-level debugger\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062
63 if (!Quiet)
64 std::cout << "llvm-db: The LLVM source-level debugger\n";
65
66 // Merge Inputfile and InputArgs into the InputArgs list...
67 if (!InputFile.empty() && InputArgs.empty())
68 InputArgs.push_back(InputFile);
69
70 // Create the CLI debugger...
71 CLIDebugger D;
72
73 // Initialize the debugger with the command line options we read...
74 Debugger &Dbg = D.getDebugger();
75
76 // Initialize the debugger environment.
77 Dbg.initializeEnvironment(envp);
78 Dbg.setWorkingDirectory(WorkingDirectory);
79 for (unsigned i = 0, e = SourceDirectories.size(); i != e; ++i)
80 D.addSourceDirectory(SourceDirectories[i]);
81
82 if (!InputArgs.empty()) {
83 try {
84 D.fileCommand(InputArgs[0]);
85 } catch (const std::string &Error) {
86 std::cout << "Error: " << Error << "\n";
87 }
88
89 Dbg.setProgramArguments(InputArgs.begin()+1, InputArgs.end());
90 }
91
92 // Now that we have initialized the debugger, run it.
93 return D.run();
94 } catch (const std::string& msg) {
95 std::cerr << argv[0] << ": " << msg << "\n";
96 } catch (...) {
97 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
98 }
99 return 1;
100}