diff options
author | Matthias Maennich <maennich@google.com> | 2021-11-24 15:05:27 +0000 |
---|---|---|
committer | Matthias Maennich <maennich@google.com> | 2021-11-24 16:04:12 +0000 |
commit | 7b413cd0e86858aefe58b791f3e9ededa891231f (patch) | |
tree | 4b8861fee4330aafaba625f517a656309fcd104b | |
parent | dc7bd73742bc1b7c75194f47a5b74f5a50e7a3c2 (diff) | |
download | build-tools-7b413cd0e86858aefe58b791f3e9ededa891231f.tar.gz |
interceptor: use consistent non-abbreviated naming
This aligns the proto naming as well as the internal naming for
constants and variables.
Bug: 207620215
Signed-off-by: Matthias Maennich <maennich@google.com>
Change-Id: I31dbf812d48abafe3343cac8cccd5ea4727b8fb9
-rw-r--r-- | interceptor/analysis.cc | 14 | ||||
-rw-r--r-- | interceptor/interceptor.cc | 118 | ||||
-rw-r--r-- | interceptor/interceptor.h | 4 | ||||
-rw-r--r-- | interceptor/log.proto | 8 | ||||
-rw-r--r-- | interceptor/main.cc | 20 |
5 files changed, 84 insertions, 80 deletions
diff --git a/interceptor/analysis.cc b/interceptor/analysis.cc index a5a0c65..1527a43 100644 --- a/interceptor/analysis.cc +++ b/interceptor/analysis.cc @@ -38,7 +38,7 @@ struct Options { fs::path output; }; -static Options parse_args(int argc, char* argv[]) { +static Options parse_arguments(int argc, char* argv[]) { Options result; const static option opts[] = { @@ -134,7 +134,7 @@ void compdb_to_file(const interceptor::Log& log, const fs::path& output) { for (const auto& command : log.commands()) { // skip anything that is not a compiler invocation - if (!kCompilers.count(fs::path(command.args(0)).filename().native())) { + if (!kCompilers.count(fs::path(command.arguments(0)).filename().native())) { continue; } @@ -151,7 +151,8 @@ void compdb_to_file(const interceptor::Log& log, const fs::path& output) { }(); // skip preprocessor invocations - if (std::find(command.args().cbegin(), command.args().cend(), "-E") != command.args().cend()) { + if (std::find(command.arguments().cbegin(), command.arguments().cend(), "-E") != + command.arguments().cend()) { continue; } @@ -166,12 +167,13 @@ void compdb_to_file(const interceptor::Log& log, const fs::path& output) { // ok, now we have a new command auto& compile_command = *compdb.add_commands(); - compile_command.set_directory(fs::path(log.root_dir()) / command.current_dir()); + compile_command.set_directory(fs::path(log.root_directory()) / command.current_directory()); compile_command.set_file(input); if (!single_output.empty()) { compile_command.set_output(single_output); } - *compile_command.mutable_arguments() = {command.args().cbegin(), command.args().cend()}; + *compile_command.mutable_arguments() = {command.arguments().cbegin(), + command.arguments().cend()}; } } @@ -201,7 +203,7 @@ void compdb_to_file(const interceptor::Log& log, const fs::path& output) { } int main(int argc, char* argv[]) { - const auto options = parse_args(argc, argv); + const auto options = parse_arguments(argc, argv); const auto log = read_log(options.command_log); switch (options.output_format) { diff --git a/interceptor/interceptor.cc b/interceptor/interceptor.cc index 1789437..8ce5cb7 100644 --- a/interceptor/interceptor.cc +++ b/interceptor/interceptor.cc @@ -48,7 +48,7 @@ static void process_command(const char* filename, char* const argv[], char* cons static void log(const interceptor::Command&); // execute potentially modified command -static void exec(const interceptor::Command&, char* const envp[]); +static void execute(const interceptor::Command&, char* const envp[]); // OVERLOADS for LD_PRELOAD USE @@ -71,20 +71,20 @@ namespace interceptor { static Command instantiate_command(const char* program, char* const argv[], char* const envp[]) { Command result; result.set_program(program); - result.set_current_dir(fs::current_path()); + result.set_current_directory(fs::current_path()); - for (auto current_arg = argv; *current_arg; ++current_arg) { - result.add_args(*current_arg); + for (auto current_argument = argv; *current_argument; ++current_argument) { + result.add_arguments(*current_argument); } - for (auto current_env = envp; *current_env; ++current_env) { - const std::string s(*current_env); + for (auto current_env_var = envp; *current_env_var; ++current_env_var) { + const std::string s(*current_env_var); const auto pos = s.find('='); if (pos == std::string::npos) { continue; } - (*result.mutable_env_vars())[s.substr(0, pos)] = s.substr(pos + 1); + (*result.mutable_environment_variables())[s.substr(0, pos)] = s.substr(pos + 1); } return result; @@ -92,44 +92,45 @@ static Command instantiate_command(const char* program, char* const argv[], char static void make_relative(Command* command) { // determine the ROOT_DIR - std::string root_dir; - if (auto it = command->env_vars().find(kEnvRootDirectory); it != command->env_vars().cend()) { - root_dir = it->second; - if (root_dir[root_dir.size() - 1] != '/') { - root_dir += '/'; + std::string root_directory; + if (auto it = command->environment_variables().find(kEnvRootDirectory); + it != command->environment_variables().cend()) { + root_directory = it->second; + if (root_directory[root_directory.size() - 1] != '/') { + root_directory += '/'; } } else { return; } // determine the relative path to ROOT_DIR from the current working dir - std::string rel_root = fs::relative(root_dir); - if (rel_root[rel_root.size() - 1] != '/') { - rel_root += '/'; + std::string relative_root = fs::relative(root_directory); + if (relative_root[relative_root.size() - 1] != '/') { + relative_root += '/'; } - if (rel_root == "./") { - rel_root.clear(); + if (relative_root == "./") { + relative_root.clear(); } // TODO: This is generally bad as this means we can't make anything relative. // This happens if the out dir is outside of the root. - if (rel_root.find(root_dir) != std::string::npos) { + if (relative_root.find(root_directory) != std::string::npos) { return; } - command->set_current_dir(fs::relative(command->current_dir(), root_dir)); + command->set_current_directory(fs::relative(command->current_directory(), root_directory)); // replacement functor const auto replace_all = [&](auto& str) { auto pos = std::string::npos; - while ((pos = str.find(root_dir)) != std::string::npos) { - str.replace(pos, root_dir.length(), rel_root); + while ((pos = str.find(root_directory)) != std::string::npos) { + str.replace(pos, root_directory.length(), relative_root); } }; // now go and replace everything replace_all(*command->mutable_program()); - std::for_each(command->mutable_args()->begin(), command->mutable_args()->end(), replace_all); + std::for_each(command->mutable_arguments()->begin(), command->mutable_arguments()->end(), replace_all); } template <typename V> @@ -161,7 +162,7 @@ std::ostream& operator<<(std::ostream& os, const interceptor::Command& command) std::ostringstream cmd; cmd << command.program(); - for (auto I = std::next(command.args().cbegin()), E = command.args().cend(); I != E; ++I) { + for (auto I = std::next(command.arguments().cbegin()), E = command.arguments().cend(); I != E; ++I) { cmd << ' ' << escape(*I); } @@ -201,9 +202,9 @@ static void analyze(Command* command) { using Analyzer = std::function<AnalysisResult(const std::string&, const ArgVec&, const EnvMap&)>; -static AnalysisResult analyze_compiler_linker(const std::string&, const ArgVec& args, +static AnalysisResult analyze_compiler_linker(const std::string&, const ArgVec& arguments, const EnvMap&) { - static constexpr std::array kSkipNextArgs{ + static constexpr std::array kSkipNextArguments{ "-isystem", "-I", "-L", "-m", "-soname", "-z", }; static constexpr std::string_view kOutputOption = "-Wp,-MMD,"; @@ -211,53 +212,54 @@ static AnalysisResult analyze_compiler_linker(const std::string&, const ArgVec& AnalysisResult result; bool next_is_out = false; bool skip_next = false; - // skip args[0] as this is the program itself - for (auto it = args.cbegin() + 1; it != args.cend(); ++it) { - const auto& arg = *it; - if (arg == "-o") { + // skip arguments[0] as this is the program itself + for (auto it = arguments.cbegin() + 1; it != arguments.cend(); ++it) { + const auto& argument = *it; + if (argument == "-o") { next_is_out = true; continue; } if (next_is_out) { - result.outputs.push_back(arg); + result.outputs.push_back(argument); next_is_out = false; continue; } - if (arg.rfind(kOutputOption, 0) == 0) { - result.outputs.push_back(arg.substr(kOutputOption.size())); + if (argument.rfind(kOutputOption, 0) == 0) { + result.outputs.push_back(argument.substr(kOutputOption.size())); } if (skip_next) { skip_next = false; continue; } - if (std::find(kSkipNextArgs.cbegin(), kSkipNextArgs.cend(), arg) != kSkipNextArgs.cend()) { + if (std::find(kSkipNextArguments.cbegin(), kSkipNextArguments.cend(), argument) != + kSkipNextArguments.cend()) { skip_next = true; } // ignore test compilations - if (arg == "/dev/null" || arg == "-") { + if (argument == "/dev/null" || argument == "-") { return {}; } - if (arg[0] == '-') { // ignore flags + if (argument[0] == '-') { // ignore flags continue; } - result.inputs.push_back(arg); + result.inputs.push_back(argument); } return result; } -static AnalysisResult analyze_archiver(const std::string&, const ArgVec& args, const EnvMap&) { +static AnalysisResult analyze_archiver(const std::string&, const ArgVec& arguments, const EnvMap&) { AnalysisResult result; - if (args.size() < 3) { + if (arguments.size() < 3) { return result; } - // skip args[0] as this is the program itself - // skip args[1] are the archiver flags - // args[2] is the output - result.outputs.push_back(args[2]); - // args[3:] are the inputs - result.inputs.insert(result.inputs.cend(), args.cbegin() + 3, args.cend()); + // skip arguments[0] as this is the program itself + // skip arguments[1] are the archiver flags + // arguments[2] is the output + result.outputs.push_back(arguments[2]); + // arguments[3:] are the inputs + result.inputs.insert(result.inputs.cend(), arguments.cbegin() + 3, arguments.cend()); return result; } @@ -274,8 +276,8 @@ static const std::initializer_list<std::pair<std::regex, Analyzer>> analyzers{ static AnalysisResult analyze_command(const Command& command) { for (const auto& [regex, analyzer] : analyzers) { - if (std::regex_match(command.args()[0], regex)) { - return analyzer(command.program(), command.args(), command.env_vars()); + if (std::regex_match(command.arguments()[0], regex)) { + return analyzer(command.program(), command.arguments(), command.environment_variables()); } } return {}; @@ -307,11 +309,11 @@ static void process_command(const char* filename, char* const argv[], char* cons log(command); // pass down the transformed command to execve - exec(command, envp); + execute(command, envp); } static void log(const interceptor::Command& command) { - const auto& env = command.env_vars(); + const auto& env = command.environment_variables(); if (const auto env_it = env.find(kEnvCommandLog); env_it != env.cend()) { std::ofstream file; @@ -319,24 +321,24 @@ static void log(const interceptor::Command& command) { std::ofstream::out | std::ofstream::app | std::ofstream::binary); interceptor::Message message; *message.mutable_command() = command; - message.mutable_command()->clear_env_vars(); + message.mutable_command()->clear_environment_variables(); if (file.is_open()) { google::protobuf::util::SerializeDelimitedToOstream(message, &file); } } } -static void exec(const interceptor::Command& command, char* const envp[]) { - std::vector<const char*> c_args; - c_args.reserve(command.args().size() + 1); - c_args[command.args().size()] = nullptr; - for (const auto& arg : command.args()) { - c_args.push_back(arg.data()); +static void execute(const interceptor::Command& command, char* const envp[]) { + std::vector<const char*> c_arguments; + c_arguments.reserve(command.arguments().size() + 1); + c_arguments[command.arguments().size()] = nullptr; + for (const auto& arg : command.arguments()) { + c_arguments.push_back(arg.data()); } // TODO: at this point, we could free some memory that is held in Command. - // While the args vector is reused for args, we could free the EnvMap - // and the original args. + // While the arguments vector is reused for arguments, we could free + // the EnvMap and the original arguments. // does not return - old_execve(command.program().c_str(), const_cast<char**>(c_args.data()), envp); + old_execve(command.program().c_str(), const_cast<char**>(c_arguments.data()), envp); } diff --git a/interceptor/interceptor.h b/interceptor/interceptor.h index 8c97022..6bf349e 100644 --- a/interceptor/interceptor.h +++ b/interceptor/interceptor.h @@ -30,8 +30,8 @@ constexpr static auto kEnvRootDirectory = "INTERCEPTOR_root_directory"; namespace interceptor { // Some type definitions to gain some type safety -using ArgVec = std::remove_pointer_t<decltype(Command().mutable_args())>; -using EnvMap = std::remove_pointer_t<decltype(Command().mutable_env_vars())>; +using ArgVec = std::remove_pointer_t<decltype(Command().mutable_arguments())>; +using EnvMap = std::remove_pointer_t<decltype(Command().mutable_environment_variables())>; using Inputs = std::vector<std::string>; using Outputs = Inputs; diff --git a/interceptor/log.proto b/interceptor/log.proto index 8e721b3..7bd027c 100644 --- a/interceptor/log.proto +++ b/interceptor/log.proto @@ -21,9 +21,9 @@ package interceptor; // A Command as traced by intercepting an execve() invocation. message Command { string program = 1; - repeated string args = 2; - map<string, string> env_vars = 3; - string current_dir = 4; + repeated string arguments = 2; + map<string, string> environment_variables = 3; + string current_directory = 4; repeated string inputs = 5; repeated string outputs = 6; }; @@ -36,7 +36,7 @@ message Message { // The entirety of a the final log should be stored as just this Log. message Log { - string root_dir = 1; // ${ROOT_DIR} or cwd() if unset + string root_directory = 1; // ${ROOT_DIR} or cwd() if unset repeated Command commands = 2; } diff --git a/interceptor/main.cc b/interceptor/main.cc index 55b7f44..16082a7 100644 --- a/interceptor/main.cc +++ b/interceptor/main.cc @@ -90,11 +90,11 @@ static void setup_interceptor_library_path() { setenv("LD_PRELOAD", interceptor_library.c_str(), 1); } -static fs::path setup_root_dir() { - const auto root_dir = getenv("ROOT_DIR"); +static fs::path set_up_root_directory() { + const auto root_directory = getenv("ROOT_DIR"); fs::path result; - if (root_dir != nullptr) { - result = root_dir; + if (root_directory != nullptr) { + result = root_directory; } else { result = fs::current_path(); } @@ -106,11 +106,11 @@ static fs::path setup_root_dir() { class CommandLog { const decltype(Options::command_log) command_log_file_; - const fs::path root_dir_; + const fs::path root_directory_; public: - CommandLog(decltype(command_log_file_) command_log_file, const fs::path& root_dir) - : command_log_file_(std::move(command_log_file)), root_dir_(root_dir) { + CommandLog(decltype(command_log_file_) command_log_file, const fs::path& root_directory) + : command_log_file_(std::move(command_log_file)), root_directory_(root_directory) { if (command_log_file_) { setenv(kEnvCommandLog, command_log_file_->c_str(), 1); std::ofstream command_log(command_log_file_->c_str(), std::ios_base::trunc); @@ -126,7 +126,7 @@ class CommandLog { // compact the log by re-reading the individual log::Message's to combine // them to a log::Log interceptor::Log log; - log.set_root_dir(root_dir_); + log.set_root_directory(root_directory_); { std::ifstream command_log(command_log_file_->c_str(), std::ios_base::binary); @@ -152,9 +152,9 @@ int main(int argc, char* argv[]) { const auto& options = parse_args(argc, argv); setup_interceptor_library_path(); - const auto root_dir = setup_root_dir(); + const auto root_directory = set_up_root_directory(); - CommandLog command_log(options.command_log, root_dir); + CommandLog command_log(options.command_log, root_directory); // TODO: cleanly to google::protobuf::ShutdownProtobufLibrary(); |