2
0
Ficheiros
rocm-systems/source/bin/omnitrace/details.cpp
T
Jonathan R. Madsen 0da62c980e Binary instrumentation: more robust exclusion of functions used internally (#238)
## Overview

This PR attempts to increase the stability of binary rewrite and runtime instrumentation.

### Improved protection against self-instrumentation

Using ~~the binary analysis capabilities added from #229~~ the Dyninst SymtabAPI, OmniTrace now does a much better job of avoiding instrumentation of functions which are internally called by OmniTrace:

- The `omnitrace` executable searches for and parses the symbols of various libraries which are known to cause problems when instrumented
  - GNU libraries which are common to nearly every library, e.g., `"libc.so.6"`, `"libdl.so.2"`, etc., and thus are outside the scope of the users optimizations efforts
  - Libraries which OmniTrace depends on for functionality, e.g. `"libunwind.so"`, `"libgotcha.so"`, `"libroctracer64.so"`, etc.
    - OmniTrace skips instrumenting any `module_function` instance when it's member `module_name` or `function_name` variable matches the library name, source file, or function name found for that symbol (unless the user explicitly requests that it be eligible for instrumentation)
- Note: the parsing of the "internal" libraries may result in longer instrumentation time and higher memory usage. Please file an issue if either of these is found to be excessive.

### Function filters based on linkage and visibility

Added options to restrict instrumentation to certain linkage types (e.g. avoid instrumenting weak symbols) and visibility types (e.g. avoid instrumenting hidden symbols).

### Function filters based on instructions

In the past, after instrumentation, some applications instrumented by Dyninst would fail with a trap signal (e.g. #147). In several cases, it was found that this occurred whenever certain instructions were present in the function so an option was added to exclude functions based on one or more regex patterns was added. 

## Details

- generates list of "internal" libraries and attempts to find the first match via:
  - the library is already open, e.g. `dlopen(<libname>, RTLD_LAZY | RTLD_NOLOAD)`
  - searching for the library in `LD_LIBRARY_PATH`
  - searching for the library in `OMNITRACE_ROCM_PATH`, `ROCM_PATH`
  - searching the folders from `/sbin/ldconfig -p`
  - searching for the library in common places such as `/usr/local/lib`
- provides new `--linkage` command line option to restrict instrumentation to functions with particular type(s) of linkage
  -  Linkage types: `unknown`, `global`, `local`, `weak`, `unique`
- provides new `--visibility` command line option to restrict instrumentation to functions with particular type(s) of visibility 
  - Visibility types: `unknown`, `default`, `hidden`, `protected`, `internal` 
- provides new `--internal-module-include` and `--internal-function-include` command line regex options to bypass automatic exclusion from instrumentation
- provides new `--internal-library-append` command line option to specify a library should be considered internal
- provides new `--internal-library-remove` command line option to specify a library should not be considered internal
- provides new `--instruction-exclude` command line regex option to exclude functions which contain matching instructions
- provides new `--internal-library-deps` command line option to treat libraries linked to internal libraries as internal libraries
  - generally, this will only be helpful during runtime instrumentation when OmniTrace is built with an external dyninst library which is dynamically linked to boost libraries and the application is using the same boost libraries
- relaxed restrictions in `module_function::is_module_constrained()`
- relaxed restrictions in `module_function::is_routine_constrained()`
- added a few miscellaneous nullptr checks

## Miscellaneous

- Fix `LD_PRELOAD` + `OMNITRACE_DL_VERBOSE=3` issue
- Adds a sampling offload verbose message
- Improves MPI send-recv.cpp example error message
- Minor tweaks to binary library
  - `binary::get_linked_path` returns `std::optional<string>`
  - renamed `binary::symbol::read_bfd` to `binary::symbol::read_bfd_line_info`
  - `binary::get_binary_info` has param options for reading line info and included undefined symbols
- fixed another edge case instance of resource deadlock during first call to configure_settings
- improved the error log printing in `omnitrace` (does not print repeated messages)

* fix OMNITRACE_DL_VERBOSE=3 + preload issue

- join needs to handle nullptr

* sampling offload verbose message

* mpi-send-recv error message

* binary updates

- get_linked_path returns std::optional<string>
- get_binary_info accepts include_undef flag
- renamed symbol::read_bfd to symbol::read_bfd_line_info
- get_binary_info has param options for reading line info and included undefined symbols

* config updates (initialization)

- fixed another instance of resource deadlock during first call to configure_settings

* Testing fix for HIP w/o rocprofiler support

- disable rocprofiler tests when HIP enabled but OMNITRACE_USE_ROCPROFILER=OFF

* omnitrace exe: insert_instr nullptr check

* omnitrace exe: new method for determining internal constraints

- added internal-libs.cpp
- using binary::get_binary_info on various known libs used by omnitrace
- any matching func/file from symbols found in known internal libs are excluded
- relaxed restrictions in is_module_constrained
- relaxed restrictions in is_routine_constrained
- added a few safety checks

* internal libs append/remove

- options to change which libs are considered internal libraries

* omnitrace exe instruction exclude

- regex option for excluding functions containing specific instructions

* fix is_internal_constrained

* binary link map verbose message

* support constraints on linkage and visibility of symbols

* misc fixes

- fix compiler error for Ubuntu Jammy + GCC 12
- dlopen + libtbbmalloc_proxy appears to be causing issues on OpenSUSE

* Performance details + MT

- multithread processing internal info
- report timing info

* Defer parsing internal data

- wait until after address space is created

* Performance improvement finding for get_symtab_function

* fix data race in get_binary_info

* remove set_default for linkage and visibility argparse

* Parse internal libs with Dyninst::Symtab instead of binary reader

- conflicting versions of libraries for binary analysis causes problems
- expanded whole function restrictions
- expanded module_function::is_routine_constrained regex

* internal lib updates

- include memory usage info
- option to read libraries linked against internal libs: --internal-library-deps
- defer parsing internal libs data to when processing modules
2023-02-07 03:39:10 -06:00

765 linhas
24 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 "function_signature.hpp"
#include "fwd.hpp"
#include "log.hpp"
#include "omnitrace.hpp"
#include <timemory/components/rusage/components.hpp>
#include <timemory/components/timing/wall_clock.hpp>
#include <string>
#include <vector>
static int expect_error = NO_ERROR;
static int error_print = 0;
// set of whole function names to exclude
strset_t
get_whole_function_names()
{
return strset_t{
"sem_init", "sem_destroy", "sem_open", "sem_close", "sem_post", "sem_wait",
"sem_getvalue", "sem_clockwait", "sem_timedwait", "sem_trywait", "sem_unlink",
"fork", "do_futex_wait", "dl_iterate_phdr", "dlinfo", "dlopen", "dlmopen",
"dlvsym", "dlsym", "dlerror", "dladdr", "_dl_sym", "_dl_vsym", "_dl_addr",
"getenv", "setenv", "unsetenv", "printf", "fprintf", "vprintf",
"buffered_vfprintf", "vfprintf", "printf_positional", "puts", "fputs", "vfputs",
"fflush", "fwrite", "malloc", "malloc_stats", "malloc_trim", "mallopt", "calloc",
"free", "pvalloc", "valloc", "sysmalloc", "posix_memalign", "freehook",
"mallochook", "memalignhook", "mprobe", "reallochook", "mmap", "munmap", "fopen",
"fclose", "fmemopen", "fmemclose", "backtrace", "backtrace_symbols",
"backtrace_symbols_fd", "sigaddset", "sigandset", "sigdelset", "sigemptyset",
"sigfillset", "sighold", "sigisemptyset", "sigismember", "sigorset", "sigrelse",
"sigvec", "strtok", "strstr", "sbrk", "strxfrm", "atexit", "ompt_start_tool",
"nanosleep", "cfree", "tolower", "toupper", "fileno", "fileno_unlocked", "exit",
"quick_exit", "abort", "mbind", "migrate_pages", "move_pages",
"numa_migrate_pages", "numa_move_pages", "numa_alloc", "numa_alloc_local",
"numa_alloc_interleaved", "numa_alloc_onnode", "numa_realloc", "numa_free",
"round_and_return", "_init", "_fini", "_start", "__do_global_dtors_aux",
"__libc_csu_init", "__libc_csu_fini", "__hip_module_ctor", "__hip_module_dtor",
"__hipRegisterManagedVar", "__hipRegisterFunction", "__hipPushCallConfiguration",
"__hipPopCallConfiguration", "hipApiName", "enlarge_userbuf",
// below are functions which never terminate
"rocr::core::Signal::WaitAny", "rocr::core::Runtime::AsyncEventsLoop",
"rocr::core::BusyWaitSignal::WaitAcquire",
"rocr::core::BusyWaitSignal::WaitRelaxed", "rocr::HSA::hsa_signal_wait_scacquire",
"rocr::os::ThreadTrampoline", "rocr::image::ImageRuntime::CreateImageManager",
"rocr::AMD::GpuAgent::GetInfo", "rocr::HSA::hsa_agent_get_info",
"event_base_loop", "bootstrapRoot", "bootstrapNetAccept", "ncclCommInitRank",
"ncclCommInitAll", "ncclCommDestroy", "ncclCommCount", "ncclCommCuDevice",
"ncclCommUserRank", "ncclReduce", "ncclBcast", "ncclBroadcast", "ncclAllReduce",
"ncclReduceScatter", "ncclAllGather", "ncclGroupStart", "ncclGroupEnd",
"ncclSend", "ncclRecv", "ncclGather", "ncclScatter", "ncclAllToAll",
"ncclAllToAllv", "ncclSocketAccept"
};
}
//======================================================================================//
//
// Helper functions because the syntax for getting a function or module name is unwieldy
//
std::string_view
get_name(procedure_t* _func)
{
static auto _v = std::unordered_map<procedure_t*, std::string>{};
auto itr = _v.find(_func);
if(itr == _v.end())
{
_v.emplace(_func, (_func) ? _func->getDemangledName() : std::string{});
}
return _v.at(_func);
}
std::string_view
get_name(module_t* _module)
{
static auto _v = std::unordered_map<module_t*, std::string>{};
auto itr = _v.find(_module);
if(itr == _v.end())
{
char _name[FUNCNAMELEN + 1];
memset(_name, '\0', FUNCNAMELEN + 1);
if(_module)
{
_module->getFullName(_name, FUNCNAMELEN);
_v.emplace(_module, std::string{ _name });
}
else
{
_v.emplace(nullptr, std::string{});
}
}
return _v.at(_module);
}
symtab_func_t*
get_symtab_function(procedure_t* _func)
{
static auto _v = std::unordered_map<procedure_t*, symtab_func_t*>{};
auto itr = _v.find(_func);
if(itr == _v.end())
{
auto _name = _func->getName();
{
auto nitr = symtab_data.mangled_symbol_names.find(_name);
if(nitr != symtab_data.mangled_symbol_names.end())
{
_v.emplace(_func, nitr->second->getFunction());
return _v.at(_func);
}
}
for(auto& fitr : symtab_data.symbols)
{
if(_name == fitr.first->getName())
{
_v.emplace(_func, fitr.first);
return _v.at(_func);
}
}
auto _dname = _func->getDemangledName();
{
auto nitr = symtab_data.typed_func_names.find(_dname);
if(nitr != symtab_data.typed_func_names.end())
{
_v.emplace(_func, nitr->second);
return _v.at(_func);
}
}
{
auto nitr = symtab_data.typed_symbol_names.find(_dname);
if(nitr != symtab_data.typed_symbol_names.end())
{
_v.emplace(_func, nitr->second->getFunction());
return _v.at(_func);
}
}
if(_v.find(_func) == _v.end()) _v.emplace(_func, nullptr);
}
return _v.at(_func);
}
namespace
{
std::string
get_return_type(procedure_t* func)
{
if(func && func->isInstrumentable() && func->getReturnType())
return func->getReturnType()->getName();
return std::string{};
}
auto
get_parameter_types(procedure_t* func)
{
auto _param_names = std::vector<std::string>{};
if(func && func->isInstrumentable())
{
auto* _params = func->getParams();
if(_params)
{
_param_names.reserve(_params->size());
for(auto* itr : *_params)
{
std::string _name = itr->getType()->getName();
if(_name.empty()) _name = itr->getName();
_param_names.emplace_back(_name);
}
}
}
return _param_names;
}
} // namespace
//======================================================================================//
//
// We create a new name that embeds the file and line information in the name
//
function_signature
get_func_file_line_info(module_t* module, procedure_t* func)
{
using address_t = Dyninst::Address;
OMNITRACE_ADD_LOG_ENTRY("Getting function line info for", get_name(func));
auto _file_name = get_name(module);
auto _func_name = get_name(func);
auto _return_type = get_return_type(func);
auto _param_types = get_parameter_types(func);
auto _base_addr = address_t{};
auto _last_addr = address_t{};
auto _src_lines = std::vector<statement_t>{};
if(func->getAddressRange(_base_addr, _last_addr) &&
module->getSourceLines(_base_addr, _src_lines) && !_src_lines.empty())
{
auto _row = _src_lines.front().lineNumber();
return function_signature(_return_type, _func_name, _file_name, _param_types,
{ _row, 0 }, { 0, 0 }, false, true, false);
}
else
{
return function_signature(_return_type, _func_name, _file_name, _param_types,
{ 0, 0 }, { 0, 0 }, false, false, false);
}
}
//======================================================================================//
//
// Gets information (line number, filename, and column number) about
// the instrumented loop and formats it properly.
//
function_signature
get_loop_file_line_info(module_t* module, procedure_t* func, flow_graph_t*,
basic_loop_t* loopToInstrument)
{
OMNITRACE_ADD_LOG_ENTRY("Getting loop line info for", get_name(func));
auto basic_blocks = std::vector<BPatch_basicBlock*>{};
loopToInstrument->getLoopBasicBlocksExclusive(basic_blocks);
if(basic_blocks.empty()) return function_signature{ "", "", "" };
auto* _block = basic_blocks.front();
auto _base_addr = _block->getStartAddress();
auto _last_addr = _block->getEndAddress();
for(const auto& itr : basic_blocks)
{
if(itr == _block) continue;
if(itr->dominates(_block))
{
_base_addr = itr->getStartAddress();
_last_addr = itr->getEndAddress();
_block = itr;
}
}
auto _file_name = get_name(module);
auto _func_name = get_name(func);
auto _return_type = get_return_type(func);
auto _param_types = get_parameter_types(func);
auto _lines_beg = std::vector<statement_t>{};
auto _lines_end = std::vector<statement_t>{};
if(module->getSourceLines(_base_addr, _lines_beg))
{
// filename = lines[0].fileName();
int _row1 = 0;
int _col1 = 0;
for(auto& itr : _lines_beg)
{
if(itr.lineNumber() > 0)
{
_row1 = itr.lineNumber();
_col1 = itr.lineOffset();
break;
}
}
if(_row1 == 0 && _col1 == 0)
return function_signature(_return_type, _func_name, _file_name, _param_types);
int _row2 = 0;
int _col2 = 0;
for(auto& itr : _lines_beg)
{
_row2 = std::max(_row2, itr.lineNumber());
_col2 = std::max(_col2, itr.lineOffset());
}
if(_col1 < 0) _col1 = 0;
if(module->getSourceLines(_last_addr, _lines_end))
{
for(auto& itr : _lines_end)
{
_row2 = std::max(_row2, itr.lineNumber());
_col2 = std::max(_col2, itr.lineOffset());
}
if(_col2 < 0) _col2 = 0;
if(_row2 < _row1) _row1 = _row2; // Fix for wrong line numbers
return function_signature(_return_type, _func_name, _file_name, _param_types,
{ _row1, _row2 }, { _col1, _col2 }, true, true,
true);
}
else
{
return function_signature(_return_type, _func_name, _file_name, _param_types,
{ _row1, 0 }, { _col1, 0 }, true, true, false);
}
}
else
{
return function_signature(_return_type, _func_name, _file_name, _param_types,
{ 0, 0 }, { 0, 0 }, true, false, false);
}
}
//======================================================================================//
//
// Gets information (line number, filename, and column number) about
// the instrumented loop and formats it properly.
//
std::map<basic_block_t*, basic_block_signature>
get_basic_block_file_line_info(module_t* module, procedure_t* func)
{
std::map<basic_block_t*, basic_block_signature> _data{};
if(!func) return _data;
OMNITRACE_ADD_LOG_ENTRY("Getting basic block line info for", get_name(func));
auto* _cfg = func->getCFG();
auto _basic_blocks = std::set<BPatch_basicBlock*>{};
_cfg->getAllBasicBlocks(_basic_blocks);
if(_basic_blocks.empty()) return _data;
auto _file_name = get_name(module);
auto _func_name = get_name(func);
auto _return_type = get_return_type(func);
auto _param_types = get_parameter_types(func);
for(auto&& itr : _basic_blocks)
{
auto _base_addr = itr->getStartAddress();
auto _last_addr = itr->getEndAddress();
verbprintf(4,
"[%s][%s] basic_block: size = %lu: base_addr = %lu, last_addr = %lu\n",
_file_name.data(), _func_name.data(),
(unsigned long) (_last_addr - _base_addr), _base_addr, _last_addr);
auto _lines_beg = std::vector<statement_t>{};
auto _lines_end = std::vector<statement_t>{};
if(module->getSourceLines(_base_addr, _lines_beg) && !_lines_beg.empty())
{
int _row1 = _lines_beg.front().lineNumber();
int _col1 = _lines_beg.front().lineOffset();
verbprintf(4, "size of _lines_end = %lu\n",
(unsigned long) _lines_end.size());
if(module->getSourceLines(_last_addr, _lines_end) && !_lines_end.empty())
{
int _row2 = _lines_end.back().lineNumber();
int _col2 = _lines_end.back().lineOffset();
if(_row2 < _row1) std::swap(_row1, _row2);
if(_row1 == _row2 && _col2 < _col1) std::swap(_col1, _col2);
_data.emplace(
itr, basic_block_signature{
_base_addr, _last_addr,
function_signature(_return_type, _func_name, _file_name,
_param_types, { _row1, _row2 },
{ _col1, _col2 }, true, true, true) });
}
else
{
_data.emplace(itr,
basic_block_signature{
_base_addr, _last_addr,
function_signature(_return_type, _func_name, _file_name,
_param_types, { _row1, 0 },
{ _col1, 0 }, true, true, false) });
}
}
else
{
_data.emplace(itr, basic_block_signature{
_base_addr, _last_addr,
function_signature(_return_type, _func_name,
_file_name, _param_types) });
}
}
return _data;
}
//======================================================================================//
//
// We create a new name that embeds the file and line information in the name
//
std::vector<statement_t>
get_source_code(module_t* module, procedure_t* func)
{
OMNITRACE_ADD_LOG_ENTRY("Getting source code for", get_name(func));
std::vector<statement_t> _lines{};
if(!module || !func) return _lines;
auto* _cfg = func->getCFG();
std::set<BPatch_basicBlock*> _basic_blocks{};
_cfg->getAllBasicBlocks(_basic_blocks);
for(auto&& itr : _basic_blocks)
{
auto _base_addr = itr->getStartAddress();
auto _last_addr = itr->getEndAddress();
for(decltype(_base_addr) _addr = _base_addr; _addr <= _last_addr; ++_addr)
{
std::vector<statement_t> _src{};
if(module->getSourceLines(_addr, _src))
{
for(auto&& iitr : _src)
_lines.emplace_back(iitr);
}
}
}
return _lines;
}
//======================================================================================//
//
// For compatibility purposes
//
procedure_t*
find_function(image_t* app_image, const std::string& _name, const strset_t& _extra)
{
if(_name.empty()) return nullptr;
auto _find = [app_image](const std::string& _f) -> procedure_t* {
// Extract the vector of functions
std::vector<procedure_t*> _found;
auto* ret = app_image->findFunction(_f.c_str(), _found, false, true, true);
if(ret == nullptr || _found.empty()) return nullptr;
return _found.at(0);
};
procedure_t* _func = _find(_name);
auto itr = _extra.begin();
while(_func == nullptr && itr != _extra.end())
{
_func = _find(*itr);
++itr;
}
if(!_func)
{
verbprintf(1, "function: '%s' ... not found\n", _name.c_str());
}
else
{
verbprintf(1, "function: '%s' ... found\n", _name.c_str());
}
return _func;
}
//======================================================================================//
//
// Error callback routine.
//
void
errorFunc(error_level_t level, int num, const char** params)
{
error_func_real(level, num, params);
}
//======================================================================================//
//
void
error_func_real(error_level_t level, int num, const char* const* params)
{
char line[4096];
const char* msg = bpatch->getEnglishErrorString(num);
bpatch->formatErrorString(line, sizeof(line), msg, params);
OMNITRACE_ADD_LOG_ENTRY("Dyninst error function called with level", level,
":: ID# =", num, "::", line)
.force(level < BPatchInfo);
if(num == 0)
{
// conditional reporting of warnings and informational messages
if(error_print > 0)
{
if(level == BPatchInfo)
{
errprintf(2, "%s :: %i :: %s\n%s", std::to_string(level).c_str(), num,
line, tim::log::color::end());
}
else
{
verbprintf(0, "%s :: %i :: %s\n%s", std::to_string(level).c_str(), num,
line, tim::log::color::end());
}
}
}
else
{
// reporting of actual errors
if(num != expect_error)
{
verbprintf(-1, "%s :: %i :: %s\n%s", std::to_string(level).c_str(), num, line,
tim::log::color::end());
// We consider some errors fatal.
if(num == 101) throw std::runtime_error(msg);
}
}
}
//======================================================================================//
//
// Just log it
//
void
error_func_fake(error_level_t level, int num, const char* const* params)
{
char line[4096];
const char* msg = bpatch->getEnglishErrorString(num);
bpatch->formatErrorString(line, sizeof(line), msg, params);
// just log it
OMNITRACE_ADD_LOG_ENTRY("Dyninst error function called with level", level,
":: ID# =", num, "::", line)
.force(level < BPatchInfo);
}
#include "internal_libs.hpp"
#include <timemory/components/timing/wall_clock.hpp>
#include <timemory/utility/join.hpp>
using ::timemory::join::join;
//======================================================================================//
//
// Read the symtab data from Dyninst
//
void
process_modules(const std::vector<module_t*>& _app_modules)
{
parse_internal_libs_data();
auto _wc = tim::component::wall_clock{};
auto _pr = tim::component::peak_rss{};
_wc.start();
_pr.start();
for(auto* itr : _app_modules)
{
auto* _module = SymTab::convert(itr);
if(_module) symtab_data.modules.emplace_back(_module);
}
verbprintf(0, "Processing %zu modules...\n", symtab_data.modules.size());
const auto& _data = get_internal_libs_data();
auto _names = std::set<std::string_view>{};
for(const auto& itr : _data)
{
_names.emplace(itr.first);
for(const auto& ditr : itr.second)
_names.emplace(ditr.first);
}
for(auto* itr : symtab_data.modules)
{
const auto* _base_name = tim::filepath::basename(itr->fullName());
auto _real_name = tim::filepath::realpath(itr->fullName(), nullptr, false);
if(_names.count(_base_name) == 0 && _names.count(_real_name) == 0)
{
verbprintf(2, "Processing symbol table for module '%s'...\n",
itr->fullName().c_str());
}
symtab_data.functions.emplace(itr, std::vector<symtab_func_t*>{});
itr->getAllFunctions(symtab_data.functions.at(itr));
for(auto* fitr : symtab_data.functions.at(itr))
{
symtab_data.typed_func_names[tim::demangle(fitr->getName())] = fitr;
symtab_data.symbols.emplace(fitr, std::vector<symtab_symbol_t*>{});
fitr->getSymbols(symtab_data.symbols.at(fitr));
for(auto* sitr : symtab_data.symbols.at(fitr))
{
symtab_data.mangled_symbol_names[sitr->getMangledName()] = sitr;
symtab_data.typed_symbol_names[sitr->getTypedName()] = sitr;
}
}
}
_pr.stop();
_wc.stop();
verbprintf(0, "Processing %zu modules... Done (%.3f %s, %.3f %s)\n",
_app_modules.size(), _wc.get(), _wc.display_unit().c_str(), _pr.get(),
_pr.display_unit().c_str());
}
//======================================================================================//
//
// I/O assistance
//
namespace std
{
std::string
to_string(instruction_category_t _category)
{
using namespace Dyninst::InstructionAPI;
switch(_category)
{
case c_CallInsn: return "function_call";
case c_ReturnInsn: return "return";
case c_BranchInsn: return "branch";
case c_CompareInsn: return "compare";
case c_PrefetchInsn: return "prefetch";
case c_SysEnterInsn: return "sys_enter";
case c_SyscallInsn: return "sys_call";
case c_VectorInsn: return "vector";
case c_GPUKernelExitInsn: return "gpu_kernel_exit";
case c_NoCategory: return "no_category";
}
return std::string{ "unknown_category_id_" } +
std::to_string(static_cast<int>(_category));
}
std::string
to_string(error_level_t _level)
{
switch(_level)
{
case BPatchFatal:
{
return JOIN("", tim::log::color::fatal(), "FatalError");
}
case BPatchSerious:
{
return JOIN("", tim::log::color::fatal(), "SeriousError");
}
case BPatchWarning:
{
return JOIN("", tim::log::color::warning(), "Warning");
}
case BPatchInfo:
{
return JOIN("", tim::log::color::info(), "Info");
}
default: break;
}
return JOIN("", tim::log::color::warning(), "UnknownErrorLevel",
static_cast<int>(_level));
}
namespace
{
std::string&&
to_lower(std::string&& _v)
{
for(auto& itr : std::move(_v))
itr = tolower(itr);
return std::move(_v);
}
} // namespace
std::string
to_string(symbol_visibility_t _v)
{
return to_lower(SymTab::Symbol::symbolVisibility2Str(_v) + 3);
}
std::string
to_string(symbol_linkage_t _v)
{
return to_lower(SymTab::Symbol::symbolLinkage2Str(_v) + 3);
}
} // namespace std
template <typename Tp>
Tp
from_string(std::string_view _v)
{
if constexpr(std::is_same<Tp, symbol_visibility_t>::value)
{
for(const auto& itr :
{ SV_UNKNOWN, SV_DEFAULT, SV_INTERNAL, SV_HIDDEN, SV_PROTECTED })
if(_v == std::to_string(itr)) return itr;
return SV_UNKNOWN;
}
else if constexpr(std::is_same<Tp, symbol_linkage_t>::value)
{
for(const auto& itr : { SL_UNKNOWN, SL_GLOBAL, SL_LOCAL, SL_WEAK, SL_UNIQUE })
if(_v == std::to_string(itr)) return itr;
return SL_UNKNOWN;
}
else
{
static_assert(std::is_empty<Tp>::value, "Error! not defined");
return Tp{};
}
}
template symbol_visibility_t
from_string<symbol_visibility_t>(std::string_view _v);
template symbol_linkage_t
from_string<symbol_linkage_t>(std::string_view _v);
std::ostream&
operator<<(std::ostream& _os, symbol_linkage_t _v)
{
return (_os << std::to_string(_v));
}
std::ostream&
operator<<(std::ostream& _os, symbol_visibility_t _v)
{
return (_os << std::to_string(_v));
}
std::istream&
operator>>(std::istream& _is, symbol_linkage_t& _v)
{
auto _v_s = std::string{};
_is >> _v_s;
_v = from_string<symbol_linkage_t>(_v_s);
return _is;
}
std::istream&
operator>>(std::istream& _is, symbol_visibility_t& _v)
{
auto _v_s = std::string{};
_is >> _v_s;
_v = from_string<symbol_visibility_t>(_v_s);
return _is;
}