Skip to content

Commit bc152fb

Browse files
authored
[llvm-debuginfod-find] Enable multicall driver (#108082)
Migrate llvm-debuginfod-find tool to use GenericOptTable. Enable multicall driver.
1 parent 3c9022c commit bc152fb

File tree

3 files changed

+115
-23
lines changed

3 files changed

+115
-23
lines changed
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
set(LLVM_LINK_COMPONENTS
2+
Option
23
Object
34
Support
45
)
6+
set(LLVM_TARGET_DEFINITIONS Opts.td)
7+
tablegen(LLVM Opts.inc -gen-opt-parser-defs)
8+
add_public_tablegen_target(DebugInfodFindOptsTableGen)
9+
510
add_llvm_tool(llvm-debuginfod-find
611
llvm-debuginfod-find.cpp
12+
DEPENDS
13+
DebugInfodFindOptsTableGen
14+
GENERATE_DRIVER
715
)
8-
target_link_libraries(llvm-debuginfod-find PRIVATE LLVMDebuginfod)
16+
if(NOT LLVM_TOOL_LLVM_DRIVER_BUILD)
17+
target_link_libraries(llvm-debuginfod-find PRIVATE LLVMDebuginfod)
18+
endif()
919
if(LLVM_INSTALL_BINUTILS_SYMLINKS)
1020
add_llvm_tool_symlink(debuginfod-find llvm-debuginfod-find)
1121
endif()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
include "llvm/Option/OptParser.td"
2+
3+
class F<string name, string help> : Flag<["-"], name>, HelpText<help>;
4+
class FF<string name, string help>: Flag<["--"], name>, HelpText<help>;
5+
class S<string name, string meta, string help>: Separate<["--"], name>, HelpText<help>, MetaVarName<meta>;
6+
7+
def help : FF<"help", "Display available options">;
8+
def : F<"h", "Alias for --help">, Alias<help>;
9+
10+
def fetch_executable : FF<"executable", "If set, fetch a binary file associated with this build id, containing the executable sections.">;
11+
def fetch_debuginfo : FF<"debuginfo", "If set, fetch a binary file associated with this build id, containing the debuginfo sections.">;
12+
def fetch_source : S<"source", "<string>", "Fetch a source file associated with this build id, which is at this relative path relative to the compilation directory.">;
13+
def dump_to_stdout : FF<"dump", "If set, dumps the contents of the fetched artifact "
14+
"to standard output. Otherwise, dumps the absolute "
15+
"path to the cached artifact on disk.">;
16+
def debug_file_directory : S<"debug-file-directory", "<string>", "Path to directory where to look for debug files.">;
17+

llvm/tools/llvm-debuginfod-find/llvm-debuginfod-find.cpp

Lines changed: 87 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,89 @@
1616
//===----------------------------------------------------------------------===//
1717

1818
#include "llvm/ADT/StringExtras.h"
19+
#include "llvm/ADT/StringRef.h"
1920
#include "llvm/Debuginfod/BuildIDFetcher.h"
2021
#include "llvm/Debuginfod/Debuginfod.h"
2122
#include "llvm/Debuginfod/HTTPClient.h"
23+
#include "llvm/Option/ArgList.h"
24+
#include "llvm/Option/Option.h"
2225
#include "llvm/Support/CommandLine.h"
2326
#include "llvm/Support/InitLLVM.h"
27+
#include "llvm/Support/LLVMDriver.h"
2428

2529
using namespace llvm;
2630

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+
/*
27102
cl::OptionCategory DebuginfodFindCategory("llvm-debuginfod-find Options");
28103
29104
cl::opt<std::string> InputBuildID(cl::Positional, cl::Required,
@@ -60,30 +135,17 @@ static cl::list<std::string> DebugFileDirectory(
60135
cl::desc("Path to directory where to look for debug files."),
61136
cl::cat(DebuginfodFindCategory));
62137
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+
*/
68139

69-
ExitOnError ExitOnErr;
140+
ExitOnError ExitOnDebuginfodFindError;
70141

71142
static std::string fetchDebugInfo(object::BuildIDRef BuildID);
72143

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);
75147
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);
87149

88150
if (FetchExecutable + FetchDebuginfo + (FetchSource != "") != 1)
89151
helpExit();
@@ -97,9 +159,10 @@ int main(int argc, char **argv) {
97159

98160
std::string Path;
99161
if (FetchSource != "")
100-
Path = ExitOnErr(getCachedOrDownloadSource(ID, FetchSource));
162+
Path =
163+
ExitOnDebuginfodFindError(getCachedOrDownloadSource(ID, FetchSource));
101164
else if (FetchExecutable)
102-
Path = ExitOnErr(getCachedOrDownloadExecutable(ID));
165+
Path = ExitOnDebuginfodFindError(getCachedOrDownloadExecutable(ID));
103166
else if (FetchDebuginfo)
104167
Path = fetchDebugInfo(ID);
105168
else
@@ -110,11 +173,13 @@ int main(int argc, char **argv) {
110173
// Print the contents of the artifact.
111174
ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(
112175
Path, /*IsText=*/false, /*RequiresNullTerminator=*/false);
113-
ExitOnErr(errorCodeToError(Buf.getError()));
176+
ExitOnDebuginfodFindError(errorCodeToError(Buf.getError()));
114177
outs() << Buf.get()->getBuffer();
115178
} else
116179
// Print the path to the cached artifact file.
117180
outs() << Path << "\n";
181+
182+
return 0;
118183
}
119184

120185
// Find a debug file in local build ID directories and via debuginfod.

0 commit comments

Comments
 (0)