aboutsummaryrefslogtreecommitdiff
path: root/bolt/tools/driver
diff options
context:
space:
mode:
authorAmir Ayupov <aaupov@fb.com>2024-02-12 14:53:53 -0800
committerGitHub <noreply@github.com>2024-02-12 14:53:53 -0800
commit52cf07116bf0a8cab87b0f55176d198bcaa02575 (patch)
tree65e5bb467a7592f93a79cfa87540838516cdb434 /bolt/tools/driver
parent93cdd1b5cfa3735c599949b77e24dbfbe570441a (diff)
downloadllvm-52cf07116bf0a8cab87b0f55176d198bcaa02575.zip
llvm-52cf07116bf0a8cab87b0f55176d198bcaa02575.tar.gz
llvm-52cf07116bf0a8cab87b0f55176d198bcaa02575.tar.bz2
[BOLT][NFC] Log through JournalingStreams (#81524)
Make core BOLT functionality more friendly to being used as a library instead of in our standalone driver llvm-bolt. To accomplish this, we augment BinaryContext with journaling streams that are to be used by most BOLT code whenever something needs to be logged to the screen. Users of the library can decide if logs should be printed to a file, no file or to the screen, as before. To illustrate this, this patch adds a new option `--log-file` that allows the user to redirect BOLT logging to a file on disk or completely hide it by using `--log-file=/dev/null`. Future BOLT code should now use `BinaryContext::outs()` for printing important messages instead of `llvm::outs()`. A new test log.test enforces this by verifying that no strings are print to screen once the `--log-file` option is used. In previous patches we also added a new BOLTError class to report common and fatal errors, so code shouldn't call exit(1) now. To easily handle problems as before (by quitting with exit(1)), callers can now use `BinaryContext::logBOLTErrorsAndQuitOnFatal(Error)` whenever code needs to deal with BOLT errors. To test this, we have fatal.s that checks we are correctly quitting and printing a fatal error to the screen. Because this is a significant change by itself, not all code was yet ported. Code from Profiler libs (DataAggregator and friends) still print errors directly to screen. Co-authored-by: Rafael Auler <rafaelauler@fb.com> Test Plan: NFC
Diffstat (limited to 'bolt/tools/driver')
-rw-r--r--bolt/tools/driver/llvm-bolt.cpp26
1 files changed, 25 insertions, 1 deletions
diff --git a/bolt/tools/driver/llvm-bolt.cpp b/bolt/tools/driver/llvm-bolt.cpp
index cc215a5..9b03524 100644
--- a/bolt/tools/driver/llvm-bolt.cpp
+++ b/bolt/tools/driver/llvm-bolt.cpp
@@ -63,6 +63,11 @@ BoltProfile("b",
cl::aliasopt(InputDataFilename),
cl::cat(BoltCategory));
+cl::opt<std::string>
+ LogFile("log-file",
+ cl::desc("redirect journaling to a file instead of stdout/stderr"),
+ cl::Hidden, cl::cat(BoltCategory));
+
static cl::opt<std::string>
InputDataFilename2("data2",
cl::desc("<data file>"),
@@ -207,6 +212,24 @@ int main(int argc, char **argv) {
if (!sys::fs::exists(opts::InputFilename))
report_error(opts::InputFilename, errc::no_such_file_or_directory);
+ // Initialize journaling streams
+ raw_ostream *BOLTJournalOut = &outs();
+ raw_ostream *BOLTJournalErr = &errs();
+ // RAII obj to keep log file open throughout execution
+ std::unique_ptr<raw_fd_ostream> LogFileStream;
+ if (!opts::LogFile.empty()) {
+ std::error_code LogEC;
+ LogFileStream = std::make_unique<raw_fd_ostream>(
+ opts::LogFile, LogEC, sys::fs::OpenFlags::OF_None);
+ if (LogEC) {
+ errs() << "BOLT-ERROR: cannot open requested log file for writing: "
+ << LogEC.message() << "\n";
+ exit(1);
+ }
+ BOLTJournalOut = LogFileStream.get();
+ BOLTJournalErr = LogFileStream.get();
+ }
+
// Attempt to open the binary.
if (!opts::DiffOnly) {
Expected<OwningBinary<Binary>> BinaryOrErr =
@@ -216,7 +239,8 @@ int main(int argc, char **argv) {
Binary &Binary = *BinaryOrErr.get().getBinary();
if (auto *e = dyn_cast<ELFObjectFileBase>(&Binary)) {
- auto RIOrErr = RewriteInstance::create(e, argc, argv, ToolPath);
+ auto RIOrErr = RewriteInstance::create(e, argc, argv, ToolPath,
+ *BOLTJournalOut, *BOLTJournalErr);
if (Error E = RIOrErr.takeError())
report_error(opts::InputFilename, std::move(E));
RewriteInstance &RI = *RIOrErr.get();