16
16
// ===----------------------------------------------------------------------===//
17
17
18
18
#include " llvm/ADT/StringExtras.h"
19
+ #include " llvm/ADT/StringRef.h"
19
20
#include " llvm/Debuginfod/BuildIDFetcher.h"
20
21
#include " llvm/Debuginfod/Debuginfod.h"
21
22
#include " llvm/Debuginfod/HTTPClient.h"
23
+ #include " llvm/Option/ArgList.h"
24
+ #include " llvm/Option/Option.h"
22
25
#include " llvm/Support/CommandLine.h"
23
26
#include " llvm/Support/InitLLVM.h"
27
+ #include " llvm/Support/LLVMDriver.h"
24
28
25
29
using namespace llvm ;
26
30
31
+ // Command-line option boilerplate.
32
+ namespace {
33
+ enum ID {
34
+ OPT_INVALID = 0 , // This is not an option ID.
35
+ #define OPTION (...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
36
+ #include " Opts.inc"
37
+ #undef OPTION
38
+ };
39
+
40
+ #define PREFIX (NAME, VALUE ) \
41
+ static constexpr StringLiteral NAME##_init[] = VALUE; \
42
+ static constexpr ArrayRef<StringLiteral> NAME (NAME##_init, \
43
+ std::size (NAME##_init) - 1);
44
+ #include " Opts.inc"
45
+ #undef PREFIX
46
+
47
+ using namespace llvm ::opt;
48
+ static constexpr opt::OptTable::Info InfoTable[] = {
49
+ #define OPTION (...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__),
50
+ #include " Opts.inc"
51
+ #undef OPTION
52
+ };
53
+
54
+ class DebuginfodFindOptTable : public opt ::GenericOptTable {
55
+ public:
56
+ DebuginfodFindOptTable () : GenericOptTable(InfoTable) {}
57
+ };
58
+
59
+ } // end anonymous namespace
60
+
61
+ static std::string InputBuildID;
62
+ static bool FetchExecutable;
63
+ static bool FetchDebuginfo;
64
+ static std::string FetchSource;
65
+ static bool DumpToStdout;
66
+ static std::vector<std::string> DebugFileDirectory;
67
+
68
+ static void parseArgs (int argc, char **argv) {
69
+ DebuginfodFindOptTable Tbl;
70
+ llvm::StringRef ToolName = argv[0 ];
71
+ llvm::BumpPtrAllocator A;
72
+ llvm::StringSaver Saver{A};
73
+ opt::InputArgList Args =
74
+ Tbl.parseArgs (argc, argv, OPT_UNKNOWN, Saver, [&](StringRef Msg) {
75
+ llvm::errs () << Msg << ' \n ' ;
76
+ std::exit (1 );
77
+ });
78
+
79
+ if (Args.hasArg (OPT_help)) {
80
+ Tbl.printHelp (llvm::outs (),
81
+ " llvm-debuginfod-find [options] <input build_id>" ,
82
+ ToolName.str ().c_str ());
83
+ std::exit (0 );
84
+ }
85
+
86
+ InputBuildID = Args.getLastArgValue (OPT_INPUT);
87
+
88
+ FetchExecutable = Args.hasArg (OPT_fetch_executable);
89
+ FetchDebuginfo = Args.hasArg (OPT_fetch_debuginfo);
90
+ DumpToStdout = Args.hasArg (OPT_dump_to_stdout);
91
+ FetchSource = Args.getLastArgValue (OPT_fetch_source, " " );
92
+ DebugFileDirectory = Args.getAllArgValues (OPT_debug_file_directory);
93
+ }
94
+
95
+ [[noreturn]] static void helpExit () {
96
+ errs () << " Must specify exactly one of --executable, "
97
+ " --source=/path/to/file, or --debuginfo.\n " ;
98
+ exit (1 );
99
+ }
100
+
101
+ /*
27
102
cl::OptionCategory DebuginfodFindCategory("llvm-debuginfod-find Options");
28
103
29
104
cl::opt<std::string> InputBuildID(cl::Positional, cl::Required,
@@ -60,30 +135,17 @@ static cl::list<std::string> DebugFileDirectory(
60
135
cl::desc("Path to directory where to look for debug files."),
61
136
cl::cat(DebuginfodFindCategory));
62
137
63
- [[noreturn]] static void helpExit () {
64
- errs () << " Must specify exactly one of --executable, "
65
- " --source=/path/to/file, or --debuginfo." ;
66
- exit (1 );
67
- }
138
+ */
68
139
69
- ExitOnError ExitOnErr ;
140
+ ExitOnError ExitOnDebuginfodFindError ;
70
141
71
142
static std::string fetchDebugInfo (object::BuildIDRef BuildID);
72
143
73
- int main (int argc, char **argv) {
74
- InitLLVM X (argc, argv);
144
+ int llvm_debuginfod_find_main (int argc, char **argv,
145
+ const llvm::ToolContext &) {
146
+ // InitLLVM X(argc, argv);
75
147
HTTPClient::initialize ();
76
-
77
- cl::HideUnrelatedOptions ({&DebuginfodFindCategory});
78
- cl::ParseCommandLineOptions (
79
- argc, argv,
80
- " llvm-debuginfod-find: Fetch debuginfod artifacts\n\n "
81
- " This program is a frontend to the debuginfod client library. The cache "
82
- " directory, request timeout (in seconds), and debuginfod server urls are "
83
- " set by these environment variables:\n "
84
- " DEBUGINFOD_CACHE_PATH (default set by sys::path::cache_directory)\n "
85
- " DEBUGINFOD_TIMEOUT (defaults to 90s)\n "
86
- " DEBUGINFOD_URLS=[comma separated URLs] (defaults to empty)\n " );
148
+ parseArgs (argc, argv);
87
149
88
150
if (FetchExecutable + FetchDebuginfo + (FetchSource != " " ) != 1 )
89
151
helpExit ();
@@ -97,9 +159,10 @@ int main(int argc, char **argv) {
97
159
98
160
std::string Path;
99
161
if (FetchSource != " " )
100
- Path = ExitOnErr (getCachedOrDownloadSource (ID, FetchSource));
162
+ Path =
163
+ ExitOnDebuginfodFindError (getCachedOrDownloadSource (ID, FetchSource));
101
164
else if (FetchExecutable)
102
- Path = ExitOnErr (getCachedOrDownloadExecutable (ID));
165
+ Path = ExitOnDebuginfodFindError (getCachedOrDownloadExecutable (ID));
103
166
else if (FetchDebuginfo)
104
167
Path = fetchDebugInfo (ID);
105
168
else
@@ -110,11 +173,13 @@ int main(int argc, char **argv) {
110
173
// Print the contents of the artifact.
111
174
ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile (
112
175
Path, /* IsText=*/ false , /* RequiresNullTerminator=*/ false );
113
- ExitOnErr (errorCodeToError (Buf.getError ()));
176
+ ExitOnDebuginfodFindError (errorCodeToError (Buf.getError ()));
114
177
outs () << Buf.get ()->getBuffer ();
115
178
} else
116
179
// Print the path to the cached artifact file.
117
180
outs () << Path << " \n " ;
181
+
182
+ return 0 ;
118
183
}
119
184
120
185
// Find a debug file in local build ID directories and via debuginfod.
0 commit comments