fee4e0830535d40a68021de855d35f1657582a20
[oota-llvm.git] / tools / dsymutil / dsymutil.cpp
1 //===-- dsymutil.cpp - Debug info dumping utility for llvm ----------------===//
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 program is a utility that aims to be a dropin replacement for
11 // Darwin's dsymutil.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "DebugMap.h"
16 #include "dsymutil.h"
17 #include "llvm/Support/ManagedStatic.h"
18 #include "llvm/Support/Options.h"
19 #include "llvm/Support/PrettyStackTrace.h"
20 #include "llvm/Support/Signals.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "llvm/Support/TargetSelect.h"
23 #include <string>
24
25 using namespace llvm::dsymutil;
26
27 namespace {
28 using namespace llvm::cl;
29
30 OptionCategory DsymCategory("Specific Options");
31 static opt<bool> Help("h", desc("Alias for -help"), Hidden);
32
33 static opt<std::string> InputFile(Positional, desc("<input file>"),
34                                   init("a.out"), cat(DsymCategory));
35
36 static opt<std::string>
37     OutputFileOpt("o",
38                   desc("Specify the output file. default: <input file>.dwarf"),
39                   value_desc("filename"), cat(DsymCategory));
40
41 static opt<std::string> OsoPrependPath(
42     "oso-prepend-path",
43     desc("Specify a directory to prepend to the paths of object files."),
44     value_desc("path"), cat(DsymCategory));
45
46 static opt<bool> Verbose("verbose", desc("Verbosity level"), init(false),
47                          cat(DsymCategory));
48
49 static opt<bool>
50     NoOutput("no-output",
51              desc("Do the link in memory, but do not emit the result file."),
52              init(false), cat(DsymCategory));
53
54 static opt<bool>
55     NoODR("no-odr",
56           desc("Do not use ODR (One Definition Rule) for type uniquing."),
57           init(false), cat(DsymCategory));
58
59 static opt<bool> DumpDebugMap(
60     "dump-debug-map",
61     desc("Parse and dump the debug map to standard output. Not DWARF link "
62          "will take place."),
63     init(false), cat(DsymCategory));
64
65 static opt<bool> InputIsYAMLDebugMap(
66     "y", desc("Treat the input file is a YAML debug map rather than a binary."),
67     init(false), cat(DsymCategory));
68 }
69
70 int main(int argc, char **argv) {
71   llvm::sys::PrintStackTraceOnErrorSignal();
72   llvm::PrettyStackTraceProgram StackPrinter(argc, argv);
73   llvm::llvm_shutdown_obj Shutdown;
74   LinkOptions Options;
75
76   HideUnrelatedOptions(DsymCategory);
77   llvm::cl::ParseCommandLineOptions(
78       argc, argv,
79       "manipulate archived DWARF debug symbol files.\n\n"
80       "dsymutil links the DWARF debug information found in the object files\n"
81       "for the executable <input file> by using debug symbols information\n"
82       "contained in its symbol table.\n");
83
84   if (Help)
85     PrintHelpMessage();
86
87   auto DebugMapPtrOrErr =
88       parseDebugMap(InputFile, OsoPrependPath, Verbose, InputIsYAMLDebugMap);
89
90   Options.Verbose = Verbose;
91   Options.NoOutput = NoOutput;
92   Options.NoODR = NoODR;
93
94   llvm::InitializeAllTargetInfos();
95   llvm::InitializeAllTargetMCs();
96   llvm::InitializeAllTargets();
97   llvm::InitializeAllAsmPrinters();
98
99   if (auto EC = DebugMapPtrOrErr.getError()) {
100     llvm::errs() << "error: cannot parse the debug map for \"" << InputFile
101                  << "\": " << EC.message() << '\n';
102     return 1;
103   }
104
105   if (Verbose || DumpDebugMap)
106     (*DebugMapPtrOrErr)->print(llvm::outs());
107
108   if (DumpDebugMap)
109     return 0;
110
111   std::string OutputFile;
112   if (OutputFileOpt.empty()) {
113     if (InputFile == "-")
114       OutputFile = "a.out.dwarf";
115     else
116       OutputFile = InputFile + ".dwarf";
117   } else {
118     OutputFile = OutputFileOpt;
119   }
120
121   return !linkDwarf(OutputFile, **DebugMapPtrOrErr, Options);
122 }