abe35de43a
* omnitrace-run exe - ensure LD_PRELOAD for libomnitrace-dl.so - convert config options into command-line options * Update timemory submodule - updates to tsettings - updates to argparser * common environment update - throw error if get_env<bool> has empty string * config updates - minor tweaks to categories of settings * core lib update - add argparse for common handling of argument parsers * omnitrace-sample update - fix handling of --trace-file (OMNITRACE_PERFETTO_FILE) * omnitrace-run update - updated to use omnitrace::argparse functions * Tests for omnitrace-run * argparse core update - remove choices for --cpu-events and --gpu-events * remove some debugging prints * fix timemory include in argparse.cpp * always provide --hsa-interrupt option * Update source/lib/core/argparse.cpp - fix pedantic warning * Update testing - remove testing args that may not be there in some builds * roctracer/pthread_create fix - disable roctracer_data when roctracer not enabled * omnitrace-causal tweak * omnitrace-instrument: module_function tweak - allow DEFAULT_MODULE and LIBRARY_MODULE * common environment update - support get_env for enums * core: config update - Add "mode" category to OMNITRACE_MODE * Update timemory submodule - remove debug print statement * omnitrace-sample tweak - change var init * omnitrace-run testing update - use --help instead of -? * core: common.hpp - tweak header include style * core: argparser update - add_ld_preload func - launcher and command member variables in parser_data - support launcher * omnitrace-run update - clean up and reworked * libomnitrace-dl updates - require LD_PRELOAD with binary rewrite - dl::InstrumentMode - dl::get_instrumented() - verify_instrumented_preloaded() - omnitrace_set_instrumented(int) - relocated omnitrace_main from main.c to dl.cpp - omnitrace_set_env does not dlopen libomnitrace - omnitrace_set_main(func_ptr) [internal API] - OMNITRACE_HIDDEN_API -> OMNITRACE_INTERNAL_API * Update testing to new LD_PRELOAD requirements * omnitrace-instrument updates - adhere to LD_PRELOAD requirementsa - invoke omnitrace_set_instrumented - binary rewrite does not instrument main - binary rewrite does not instrument call to omnitrace_init - runtime instr does not instrument main - runtime instr does not instrument call to omnitrace_init * Bump to v1.9.0 - LD_PRELOAD requirement necessitates minor version increment * common: environment - fix ambiguous get_env calls * omnitrace-instrument update - fix issue with temporaries * omnitrace-instrument and libomnitrace-dl updates - runtime instrumentation does not work if libomnitrace-dl is preloaded * libomnitrace-dl and libpyomnitrace updates - define dl::InstrumentMode in dl.hpp - handle instrumentation via setprofile libpyomnitrace - do not push trace in omnitrace_init * omnitrace-instrument and libomnitrace-dl updates - move header to dl subdirectory - omnitrace::omnitrace-headers include omnitrace-dl folder - use InstrumentMode in omnitrace-instrument * Update workflows and scripts - Use omnitrace-run on instrumented exes * Update docs - add omnitrace-run to examples of running binary rewritten exes
360 regels
10 KiB
C++
360 regels
10 KiB
C++
// MIT License
|
|
//
|
|
// Copyright (c) 2022 Advanced Micro Devices, Inc. All Rights Reserved.
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
// copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
// SOFTWARE.
|
|
|
|
#include "omnitrace-run.hpp"
|
|
|
|
#include "common/defines.h"
|
|
#include "common/delimit.hpp"
|
|
#include "common/environment.hpp"
|
|
#include "common/join.hpp"
|
|
#include "common/setup.hpp"
|
|
#include "core/argparse.hpp"
|
|
#include "core/config.hpp"
|
|
#include "core/state.hpp"
|
|
#include "core/timemory.hpp"
|
|
|
|
#include <timemory/environment.hpp>
|
|
#include <timemory/environment/types.hpp>
|
|
#include <timemory/log/color.hpp>
|
|
#include <timemory/settings/types.hpp>
|
|
#include <timemory/settings/vsettings.hpp>
|
|
#include <timemory/signals/signal_handlers.hpp>
|
|
#include <timemory/utility/argparse.hpp>
|
|
#include <timemory/utility/console.hpp>
|
|
#include <timemory/utility/filepath.hpp>
|
|
#include <timemory/utility/join.hpp>
|
|
|
|
#include <array>
|
|
#include <cctype>
|
|
#include <chrono>
|
|
#include <cmath>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <iostream>
|
|
#include <regex>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <sys/wait.h>
|
|
#include <thread>
|
|
#include <unistd.h>
|
|
#include <vector>
|
|
|
|
namespace color = ::tim::log::color;
|
|
namespace filepath = ::tim::filepath; // NOLINT
|
|
namespace console = ::tim::utility::console;
|
|
namespace argparse = ::tim::argparse;
|
|
namespace signals = ::tim::signals;
|
|
using settings = ::omnitrace::settings;
|
|
using namespace ::timemory::join;
|
|
using ::tim::get_env;
|
|
using ::tim::log::stream;
|
|
|
|
namespace std
|
|
{
|
|
std::string
|
|
to_string(bool _v)
|
|
{
|
|
return (_v) ? "true" : "false";
|
|
}
|
|
} // namespace std
|
|
|
|
namespace
|
|
{
|
|
std::string
|
|
get_internal_libpath(const std::string& _lib)
|
|
{
|
|
auto _exe = std::string_view{ realpath("/proc/self/exe", nullptr) };
|
|
auto _pos = _exe.find_last_of('/');
|
|
auto _dir = std::string{ "./" };
|
|
if(_pos != std::string_view::npos) _dir = _exe.substr(0, _pos);
|
|
return omnitrace::common::join("/", _dir, "..", "lib", _lib);
|
|
}
|
|
|
|
parser_data_t&
|
|
get_initial_environment(parser_data_t& _data)
|
|
{
|
|
if(environ != nullptr)
|
|
{
|
|
int idx = 0;
|
|
while(environ[idx] != nullptr)
|
|
{
|
|
auto* _v = environ[idx++];
|
|
_data.initial.emplace(_v);
|
|
_data.current.emplace_back(strdup(_v));
|
|
}
|
|
}
|
|
|
|
return _data;
|
|
}
|
|
|
|
int
|
|
get_verbose(parser_data_t& _data)
|
|
{
|
|
auto& verbose = _data.verbose;
|
|
verbose = get_env("OMNITRACE_CAUSAL_VERBOSE",
|
|
get_env<int>("OMNITRACE_VERBOSE", verbose, false));
|
|
auto _debug =
|
|
get_env("OMNITRACE_CAUSAL_DEBUG", get_env<bool>("OMNITRACE_DEBUG", false, false));
|
|
if(_debug) verbose += 8;
|
|
return verbose;
|
|
}
|
|
|
|
std::string
|
|
get_realpath(const std::string& _v)
|
|
{
|
|
auto* _tmp = realpath(_v.c_str(), nullptr);
|
|
auto _ret = std::string{ _tmp };
|
|
free(_tmp);
|
|
return _ret;
|
|
}
|
|
|
|
auto
|
|
toggle_suppression(std::tuple<bool, bool> _inp)
|
|
{
|
|
auto _out =
|
|
std::make_tuple(settings::suppress_config(), settings::suppress_parsing());
|
|
std::tie(settings::suppress_config(), settings::suppress_parsing()) = _inp;
|
|
return _out;
|
|
}
|
|
|
|
// disable suppression when exe loads but store original values for restoration later
|
|
auto initial_suppression = toggle_suppression({ true, true });
|
|
} // namespace
|
|
|
|
void
|
|
print_command(const parser_data_t& _data, std::string_view _prefix)
|
|
{
|
|
auto verbose = _data.verbose;
|
|
const auto& _argv = _data.command;
|
|
if(verbose >= 1)
|
|
stream(std::cout, color::info())
|
|
<< _prefix << "Executing '" << join(array_config{ " " }, _argv) << "'...\n";
|
|
|
|
std::cerr << color::end() << std::flush;
|
|
}
|
|
|
|
void
|
|
prepare_command_for_run(char* _exe, parser_data_t& _data)
|
|
{
|
|
if(!_data.launcher.empty())
|
|
{
|
|
bool _injected = false;
|
|
auto _new_argv = std::vector<char*>{};
|
|
for(auto* itr : _data.command)
|
|
{
|
|
if(!_injected && std::regex_search(itr, std::regex{ _data.launcher }))
|
|
{
|
|
_new_argv.emplace_back(_exe);
|
|
_new_argv.emplace_back(strdup("--"));
|
|
_injected = true;
|
|
}
|
|
_new_argv.emplace_back(itr);
|
|
}
|
|
|
|
if(!_injected)
|
|
{
|
|
throw std::runtime_error(
|
|
join("", "omnitrace-run was unable to match \"", _data.launcher,
|
|
"\" to any arguments on the command line: \"",
|
|
join(array_config{ " ", "", "" }, _data.command), "\""));
|
|
}
|
|
|
|
std::swap(_data.command, _new_argv);
|
|
}
|
|
}
|
|
|
|
void
|
|
prepare_environment_for_run(parser_data_t& _data)
|
|
{
|
|
if(_data.launcher.empty())
|
|
{
|
|
omnitrace::argparse::add_ld_preload(_data);
|
|
}
|
|
}
|
|
|
|
void
|
|
print_updated_environment(parser_data_t& _data, std::string_view _prefix)
|
|
{
|
|
auto _verbose = get_verbose(_data);
|
|
|
|
if(_verbose < 0) return;
|
|
|
|
auto _env = _data.current;
|
|
const auto& _updated_envs = _data.updated;
|
|
|
|
std::sort(_env.begin(), _env.end(), [](auto* _lhs, auto* _rhs) {
|
|
if(!_lhs) return false;
|
|
if(!_rhs) return true;
|
|
return std::string_view{ _lhs } < std::string_view{ _rhs };
|
|
});
|
|
|
|
std::vector<std::string_view> _updates = {};
|
|
std::vector<std::string_view> _general = {};
|
|
|
|
for(auto* itr : _env)
|
|
{
|
|
if(itr == nullptr) continue;
|
|
|
|
auto _is_omni = (std::string_view{ itr }.find("OMNITRACE") == 0);
|
|
auto _updated = false;
|
|
for(const auto& vitr : _updated_envs)
|
|
{
|
|
if(std::string_view{ itr }.find(vitr) == 0)
|
|
{
|
|
_updated = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if(_updated)
|
|
_updates.emplace_back(itr);
|
|
else if(_verbose >= 1 && _is_omni)
|
|
_general.emplace_back(itr);
|
|
}
|
|
|
|
if(_general.size() + _updates.size() == 0 || _verbose < 0) return;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
for(auto& itr : _general)
|
|
stream(std::cerr, color::source()) << _prefix << itr << "\n";
|
|
for(auto& itr : _updates)
|
|
stream(std::cerr, color::source()) << _prefix << itr << "\n";
|
|
|
|
std::cerr << color::end() << std::flush;
|
|
}
|
|
|
|
parser_data_t&
|
|
parse_args(int argc, char** argv, parser_data_t& _parser_data)
|
|
{
|
|
get_initial_environment(_parser_data);
|
|
|
|
bool _do_parse_args = false;
|
|
for(int i = 1; i < argc; ++i)
|
|
{
|
|
auto _arg = std::string_view{ argv[i] };
|
|
if(_arg == "--" || _arg == "-?" || _arg == "-h" || _arg == "--help" ||
|
|
_arg == "--version")
|
|
_do_parse_args = true;
|
|
}
|
|
|
|
if(!_do_parse_args && argc > 1 && std::string_view{ argv[1] }.find('-') == 0)
|
|
_do_parse_args = true;
|
|
|
|
if(!_do_parse_args) return parse_command(argc, argv, _parser_data);
|
|
|
|
using parser_t = argparse::argument_parser;
|
|
using parser_err_t = typename parser_t::result_type;
|
|
|
|
toggle_suppression(initial_suppression);
|
|
omnitrace::argparse::init_parser(_parser_data);
|
|
|
|
// no need for backtraces
|
|
signals::disable_signal_detection(signals::signal_settings::get_enabled());
|
|
|
|
auto help_check = [](parser_t& p, int _argc, char** _argv) {
|
|
std::set<std::string> help_args = { "-h", "--help", "-?" };
|
|
return (p.exists("help") || _argc == 1 ||
|
|
(_argc > 1 && help_args.find(_argv[1]) != help_args.end()));
|
|
};
|
|
|
|
const auto* _desc = R"desc(
|
|
Command line interface to omnitrace configuration.
|
|
)desc";
|
|
|
|
auto parser = parser_t{ basename(argv[0]), _desc };
|
|
|
|
parser.on_error([](parser_t&, const parser_err_t& _err) {
|
|
stream(std::cerr, color::fatal()) << _err << "\n";
|
|
exit(EXIT_FAILURE);
|
|
});
|
|
|
|
parser.enable_help("", "Usage: omnitrace-run <OPTIONS> -- <COMMAND> <ARGS>");
|
|
parser.enable_version("omnitrace-run", "v" OMNITRACE_VERSION_STRING,
|
|
OMNITRACE_GIT_DESCRIBE, OMNITRACE_GIT_REVISION);
|
|
|
|
auto _cols = std::get<0>(console::get_columns());
|
|
if(_cols > parser.get_help_width() + 8)
|
|
parser.set_description_width(
|
|
std::min<int>(_cols - parser.get_help_width() - 8, 120));
|
|
|
|
// disable options related to causal profiling
|
|
_parser_data.processed_groups.emplace("causal");
|
|
|
|
omnitrace::argparse::add_core_arguments(parser, _parser_data);
|
|
omnitrace::argparse::add_extended_arguments(parser, _parser_data);
|
|
|
|
auto _inpv = std::vector<char*>{};
|
|
auto& _outv = _parser_data.command;
|
|
bool _hash = false;
|
|
for(int i = 0; i < argc; ++i)
|
|
{
|
|
if(argv[i] == nullptr)
|
|
{
|
|
continue;
|
|
}
|
|
else if(_hash)
|
|
{
|
|
_outv.emplace_back(strdup(argv[i]));
|
|
}
|
|
else if(std::string_view{ argv[i] } == "--")
|
|
{
|
|
_hash = true;
|
|
}
|
|
else
|
|
{
|
|
_inpv.emplace_back(strdup(argv[i]));
|
|
}
|
|
}
|
|
|
|
auto _cerr = parser.parse_args(_inpv.size(), _inpv.data());
|
|
if(_cerr)
|
|
{
|
|
std::cerr << _cerr.what() << std::endl;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
return _parser_data;
|
|
}
|
|
|
|
parser_data_t&
|
|
parse_command(int argc, char** argv, parser_data_t& _parser_data)
|
|
{
|
|
toggle_suppression(initial_suppression);
|
|
omnitrace::argparse::init_parser(_parser_data);
|
|
|
|
// no need for backtraces
|
|
signals::disable_signal_detection(signals::signal_settings::get_enabled());
|
|
|
|
auto& _outv = _parser_data.command;
|
|
bool _hash = false;
|
|
for(int i = 1; i < argc; ++i)
|
|
{
|
|
_outv.emplace_back(strdup(argv[i]));
|
|
}
|
|
|
|
return _parser_data;
|
|
}
|