Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions include/eld/Config/GeneralOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ class GeneralOptions {

bool traceReloc(std::string const &RelocName) const;

bool tracePlugin(std::string const &PluginName) const;

bool traceLTO(void) const;

bool codegenOpts(void) const;
Expand Down Expand Up @@ -1041,6 +1043,10 @@ class GeneralOptions {

bool isSectionTracingRequested() const { return SectionTracingRequested; }

void setPluginTracingRequested() { PluginTracingRequested = true; }

bool isPluginTracingRequested() const { return PluginTracingRequested; }

// --------------Dynamic Linker-------------------------
bool hasDynamicLinker() const { return BDynamicLinker; }

Expand Down Expand Up @@ -1376,9 +1382,11 @@ class GeneralOptions {
std::vector<llvm::Regex> SymbolTrace;
std::vector<llvm::Regex> RelocTrace;
std::vector<llvm::Regex> SectionTrace;
std::vector<llvm::Regex> PluginTrace;
std::vector<std::string> SymbolsToTrace;
std::vector<std::string> SectionsToTrace;
std::vector<std::string> RelocsToTrace;
std::vector<std::string> PluginsToTrace;
std::vector<llvm::Regex> MergeStrSectionsToTrace;
MergeStrTraceType MergeStrTraceValue = MergeStrTraceType::NONE;
std::set<std::string> RelocVerify;
Expand Down Expand Up @@ -1419,6 +1427,7 @@ class GeneralOptions {
llvm::StringRef TrampolineMapFile; // TrampolineMap
bool SymbolTracingRequested = false;
bool SectionTracingRequested = false;
bool PluginTracingRequested = false;
std::vector<llvm::StringRef> RequestedTimeRegions;
DiagnosticEngine *DiagEngine = nullptr;
bool BDynamicLinker = true;
Expand Down
1 change: 1 addition & 0 deletions include/eld/Driver/GnuLinkerOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,7 @@ defm trace
"\t\t\t --trace=garbage-collection : trace linker garbage "
"collection\n"
"\t\t\t --trace=plugin : trace plugin\n"
"\t\t\t --trace=plugin=<plugin-name> : trace a single plugin\n"
"\t\t\t --trace=threads : trace threads\n"
"\t\t\t --trace=assignments : trace symbol assignments\n"
"\t\t\t --trace=command-line : trace header info\n"
Expand Down
5 changes: 5 additions & 0 deletions include/eld/PluginAPI/LinkerWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,11 @@ class DLL_A_EXPORT LinkerWrapper {
/// Returns true if user has requested verbose diagnostics.
bool isVerbose() const;

/// Returns true if this plugin is being traced, i.e. --trace=plugin
/// was given without a scope, or with a scope (--trace=plugin=<name>)
/// that matches this plugin's name.
bool isTraced() const;

eld::Expected<std::vector<plugin::OutputSection>>
getAllOutputSections() const;

Expand Down
9 changes: 7 additions & 2 deletions include/eld/Script/Plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ class Plugin {

std::string getPluginOptions() const { return PluginOptions; }

/// Returns true if diagnostics should be traced for this plugin, honoring
/// both bare --trace=plugin and scoped --trace=plugin=<name>.
bool isTraced() const;

plugin::PluginBase *getLinkerPlugin() const { return UserPluginHandle; }

void *getLibraryHandle() const { return PluginLibraryHandle; }
Expand Down Expand Up @@ -96,9 +100,10 @@ class Plugin {
bool registerPlugin(void *Handle);

// -------------- Load/Unload/Reset Plugin ------------------------
static void *loadPlugin(std::string Name, Module *Module);
static void *loadPlugin(std::string Name, Module *Module, bool IsTraced);

static bool unload(std::string Name, void *LibraryHandle, Module *Module);
static bool unload(std::string Name, void *LibraryHandle, Module *Module,
bool IsTraced);

void reset();

Expand Down
21 changes: 21 additions & 0 deletions lib/Config/GeneralOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,13 @@ eld::Expected<void> GeneralOptions::setTrace(const char *PTraceType) {
std::string Sym = TraceType.substr(Pos + 1).str();
SectionTrace.emplace_back(llvm::Regex(Sym));
SectionsToTrace.emplace_back(Sym);
} else if (TraceType.starts_with("plugin") && TraceType.contains('=')) {
setPluginTracingRequested();
TraceMe = DiagEngine->getPrinter()->TracePlugin;
size_t Pos = TraceType.find_last_of('=');
std::string PluginName = TraceType.substr(Pos + 1).str();
PluginTrace.emplace_back(llvm::Regex(PluginName));
PluginsToTrace.emplace_back(PluginName);
} else if (TraceType.starts_with("merge-strings")) {
size_t Pos = TraceType.find_last_of('=');
std::string Arg = TraceType.substr(Pos + 1).str();
Expand Down Expand Up @@ -402,6 +409,20 @@ bool GeneralOptions::traceReloc(std::string const &RelocName) const {
});
}

bool GeneralOptions::tracePlugin(std::string const &PluginName) const {
if (!DiagEngine->getPrinter()->tracePlugins())
return false;
// Bare "--trace=plugin" (no scoped names given): trace every plugin.
if (!PluginTracingRequested)
return true;
StringRef PluginRef(PluginName);
return llvm::any_of(PluginsToTrace,
[&](const std::string &S) { return S == PluginName; }) ||
llvm::any_of(PluginTrace, [&](const llvm::Regex &Regex) {
return Regex.match(PluginRef);
});
}

std::vector<llvm::StringRef> GeneralOptions::getLTOOptionsAsString() const {
std::vector<llvm::StringRef> ReturnValue;
if ((LTOOptions & LTOVerbose) == LTOVerbose)
Expand Down
5 changes: 3 additions & 2 deletions lib/Core/LinkerScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ void LinkerScript::unloadPlugins(Module *Module) {
continue;
// Run the cleanup function
H.second->cleanup();
Plugin::unload(H.first, H.second->getLibraryHandle(), Module);
Plugin::unload(H.first, H.second->getLibraryHandle(), Module,
H.second->isTraced());
if (Module->getPrinter()->isVerbose())
Diag->raise(Diag::unloaded_plugin) << H.first;
}
Expand Down Expand Up @@ -574,7 +575,7 @@ bool LinkerScript::loadPlugin(Plugin &P, Module &M) {
void *Handle = nullptr;
auto &PAL = M.getPluginActivityLog();
if (I == MLibraryToPluginMap.end()) {
Handle = Plugin::loadPlugin(ResolvedPath, &M);
Handle = Plugin::loadPlugin(ResolvedPath, &M, P.isTraced());
MLibraryToPluginMap.insert(std::make_pair(ResolvedPath, &P));
} else {
Handle = I->second->getLibraryHandle();
Expand Down
2 changes: 2 additions & 0 deletions lib/LinkerWrapper/LinkerWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,8 @@ bool LinkerWrapper::isVerbose() const {
return m_Module.getConfig().getPrinter()->isVerbose();
}

bool LinkerWrapper::isTraced() const { return m_Plugin->isTraced(); }

eld::Expected<std::vector<plugin::OutputSection>>
LinkerWrapper::getAllOutputSections() const {
CHECK_LINK_STATE(*this, "ActBeforeRuleMatching", "BeforeLayout",
Expand Down
4 changes: 2 additions & 2 deletions lib/Plugin/PluginManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ bool PluginManager::callVisitSymbolHook(LDSymbol *Sym, llvm::StringRef SymName,

void PluginManager::addSymbolVisitor(eld::Plugin *P) {
SymbolVisitors.insert(P);
if (DE.getPrinter()->tracePlugins())
if (P->isTraced())
DE.raise(Diag::trace_plugin_enable_visit_symbol) << P->getPluginName();
}

Expand All @@ -136,7 +136,7 @@ void PluginManager::setAuxiliarySymbolNameMap(
const ObjectFile::AuxiliarySymbolNameMap &AuxSymNameMap, const Plugin *P) {
ObjFile->setAuxiliarySymbolNameMap(AuxSymNameMap);
AuxSymNameMapProvider[ObjFile] = P;
if (DE.getPrinter()->tracePlugins())
if (P->isTraced())
DE.raise(Diag::trace_set_aux_sym_name_map)
<< P->getPluginName() << ObjFile->getInput()->decoratedPath();
}
Expand Down
52 changes: 29 additions & 23 deletions lib/Script/Plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ Plugin::Plugin(plugin::Plugin::Type T, std::string LibraryName,
ThisModule(Module), ThisConfig(Module.getConfig()),
IsDefaultPlugin(DefaultPlugin) {}

bool Plugin::isTraced() const {
return ThisConfig.options().tracePlugin(getPluginType());
}

std::string Plugin::resolvePath(const LinkerConfig &PConfig) {
// Library already loaded!
if (PluginLibraryHandle)
Expand Down Expand Up @@ -79,13 +83,14 @@ std::string Plugin::resolvePath(const LinkerConfig &PConfig) {

if (nullptr != NS) {
PluginLibraryName = NS->getFullPath();
if (ThisModule.getPrinter()->tracePlugins())
if (isTraced())
ThisConfig.raise(Diag::using_plugin) << PluginLibraryName << Name;
}
return PluginLibraryName;
}

void *Plugin::loadPlugin(std::string LibraryName, Module *Module) {
void *Plugin::loadPlugin(std::string LibraryName, Module *Module,
bool IsTraced) {
void *LibraryHandle = DynamicLibrary::Load(LibraryName);
DiagnosticEngine *DiagEngine = Module->getConfig().getDiagEngine();
if (!LibraryHandle) {
Expand All @@ -94,7 +99,7 @@ void *Plugin::loadPlugin(std::string LibraryName, Module *Module) {
return nullptr;
}

if (Module->getPrinter()->tracePlugins())
if (IsTraced)
DiagEngine->raise(Diag::loaded_library) << LibraryName;

return LibraryHandle;
Expand Down Expand Up @@ -151,15 +156,15 @@ bool Plugin::setFunctions() {
return false;
}

if (ThisModule.getPrinter()->tracePlugins()) {
if (isTraced()) {
ThisConfig.raise(Diag::found_register_function) << Register << LibraryName;
ThisConfig.raise(Diag::found_function_for_plugintype)
<< PluginFunc << LibraryName;
}

std::string PluginCleanupFunc = "Cleanup";
void *C = DynamicLibrary::GetFunction(PluginLibraryHandle, PluginCleanupFunc);
if (C && ThisModule.getPrinter()->tracePlugins()) {
if (C && isTraced()) {
ThisConfig.raise(Diag::found_cleanup_function)
<< PluginCleanupFunc << LibraryName;
}
Expand All @@ -182,7 +187,7 @@ bool Plugin::setFunctions() {
bool Plugin::getUserPlugin() {
std::string LibraryName = DynamicLibrary::getLibraryName(Name);

if (ThisModule.getPrinter()->tracePlugins())
if (isTraced())
ThisConfig.raise(Diag::registering_all_functions);

UserPluginHandle = (*GetPluginFunction)(getPluginType().c_str());
Expand All @@ -192,7 +197,7 @@ bool Plugin::getUserPlugin() {
return false;
}

if (ThisModule.getPrinter()->tracePlugins())
if (isTraced())
ThisConfig.raise(Diag::found_plugin_handler)
<< getPluginType() << LibraryName;

Expand All @@ -211,7 +216,7 @@ bool Plugin::init(eld::OutputTarWriter *OutputTar) {
return false;
eld::RegisterTimer T(
"Init", ThisModule.saveString(UserPluginHandle->GetName()), Stats);
if (ThisModule.getPrinter()->tracePlugins())
if (isTraced())
ThisConfig.raise(Diag::note_initializing_plugin)
<< getPluginType() << DynamicLibrary::getLibraryName(Name)
<< UserPluginHandle->GetName();
Expand All @@ -235,14 +240,14 @@ bool Plugin::run(std::vector<Plugin *> &Plugins) {
return false;
eld::RegisterTimer T(
"Run", ThisModule.saveString(UserPluginHandle->GetName()), Stats);
if (ThisModule.getPrinter()->tracePlugins())
if (isTraced())
ThisConfig.raise(Diag::running_plugin)
<< getPluginType() << DynamicLibrary::getLibraryName(Name)
<< UserPluginHandle->GetName();
Plugins.push_back(this);
Running R(this);
plugin::Plugin *P = llvm::cast<plugin::Plugin>(UserPluginHandle);
plugin::Plugin::Status S = P->Run(ThisModule.getPrinter()->tracePlugins());
plugin::Plugin::Status S = P->Run(isTraced());
if (S == plugin::Plugin::Status::ERROR || P->GetLastError()) {
ThisConfig.raise(Diag::plugin_has_error)
<< getPluginType() << DynamicLibrary::getLibraryName(Name)
Expand All @@ -266,11 +271,12 @@ bool Plugin::cleanup() {
return true;
}

bool Plugin::unload(std::string LibraryName, void *Handle, Module *Module) {
bool Plugin::unload(std::string LibraryName, void *Handle, Module *Module,
bool IsTraced) {
if (Handle) {
DynamicLibrary::Unload(Handle);

if (Module->getPrinter()->tracePlugins())
if (IsTraced)
Module->getConfig().raise(Diag::unloaded_library) << LibraryName;
}

Expand All @@ -290,7 +296,7 @@ bool Plugin::destroy() {
return false;
eld::RegisterTimer T(
"Destroy", ThisModule.saveString(UserPluginHandle->GetName()), Stats);
if (ThisModule.getPrinter()->tracePlugins())
if (isTraced())
ThisConfig.raise(Diag::plugin_destroy) << getPluginType();
plugin::Plugin *P = llvm::cast<plugin::Plugin>(UserPluginHandle);
P->Destroy();
Expand Down Expand Up @@ -332,7 +338,7 @@ bool Plugin::check() {
return false;
}

if (ThisModule.getPrinter()->tracePlugins())
if (isTraced())
ThisConfig.raise(Diag::note_plugin_version)
<< PluginMajor << PluginMinor << DynamicLibrary::getLibraryName(Name)
<< getPluginType();
Expand Down Expand Up @@ -522,7 +528,7 @@ void Plugin::callInitHook() {
plugin::LinkerPlugin *P = llvm::cast<plugin::LinkerPlugin>(UserPluginHandle);
RegisterTimer T("Init", ThisModule.saveString(UserPluginHandle->GetName()),
Stats);
if (ThisModule.getPrinter()->tracePlugins())
if (isTraced())
ThisConfig.raise(Diag::trace_plugin_init) << getPluginName();
P->Init(PluginOptions);
}
Expand All @@ -531,15 +537,15 @@ void Plugin::callDestroyHook() {
plugin::LinkerPlugin *P = llvm::cast<plugin::LinkerPlugin>(UserPluginHandle);
RegisterTimer T("Destroy", ThisModule.saveString(UserPluginHandle->GetName()),
Stats);
if (ThisModule.getPrinter()->tracePlugins())
if (isTraced())
ThisConfig.raise(Diag::trace_plugin_destroy) << getPluginName();
P->Destroy();
}

void Plugin::registerCommandLineOption(
const std::string &Option, bool HasValue,
const CommandLineOptionSpec::OptionHandlerType &OptionHandler) {
if (ThisModule.getPrinter()->tracePlugins()) {
if (isTraced()) {
if (HasValue)
ThisConfig.raise(Diag::trace_plugin_register_opt_with_val)
<< getPluginName() << Option;
Expand Down Expand Up @@ -568,7 +574,7 @@ void Plugin::callVisitSectionsHook(InputFile &IF) {
plugin::LinkerPlugin *P = llvm::cast<plugin::LinkerPlugin>(UserPluginHandle);
RegisterTimer T("VisitSections",
ThisModule.saveString(UserPluginHandle->GetName()), Stats);
if (ThisModule.getPrinter()->tracePlugins())
if (isTraced())
ThisConfig.raise(Diag::trace_plugin_visit_sections)
<< getPluginName() << IF.getInput()->decoratedPath();
P->VisitSections(plugin::InputFile(&IF));
Expand All @@ -579,7 +585,7 @@ void Plugin::callVisitSymbolHook(LDSymbol *Sym, llvm::StringRef SymName,
plugin::LinkerPlugin *P = llvm::cast<plugin::LinkerPlugin>(UserPluginHandle);
RegisterTimer T("VisitSymbol",
ThisModule.saveString(UserPluginHandle->GetName()), Stats);
if (ThisModule.getPrinter()->tracePlugins())
if (isTraced())
ThisConfig.raise(Diag::trace_plugin_visit_symbol)
<< getPluginName() << SymName;
std::unique_ptr<SymbolInfo> UpSymInfo = std::make_unique<SymbolInfo>(SymInfo);
Expand All @@ -593,7 +599,7 @@ void Plugin::callActBeforeRuleMatchingHook() {
plugin::LinkerPlugin *P = llvm::cast<plugin::LinkerPlugin>(UserPluginHandle);
RegisterTimer T(HookName, ThisModule.saveString(UserPluginHandle->GetName()),
Stats);
if (ThisModule.getPrinter()->tracePlugins())
if (isTraced())
ThisConfig.raise(Diag::trace_plugin_hook) << getPluginName() << HookName;
P->ActBeforeRuleMatching();
}
Expand All @@ -603,7 +609,7 @@ void Plugin::callActBeforeSectionMergingHook() {
plugin::LinkerPlugin *P = llvm::cast<plugin::LinkerPlugin>(UserPluginHandle);
RegisterTimer T(HookName, ThisModule.saveString(UserPluginHandle->GetName()),
Stats);
if (ThisModule.getPrinter()->tracePlugins())
if (isTraced())
ThisConfig.raise(Diag::trace_plugin_hook) << getPluginName() << HookName;
P->ActBeforeSectionMerging();
}
Expand All @@ -613,7 +619,7 @@ void Plugin::callActBeforePerformingLayoutHook() {
plugin::LinkerPlugin *P = llvm::cast<plugin::LinkerPlugin>(UserPluginHandle);
RegisterTimer T(HookName, ThisModule.saveString(UserPluginHandle->GetName()),
Stats);
if (ThisModule.getPrinter()->tracePlugins())
if (isTraced())
ThisConfig.raise(Diag::trace_plugin_hook) << getPluginName() << HookName;
P->ActBeforePerformingLayout();
}
Expand All @@ -623,7 +629,7 @@ void Plugin::callActBeforeWritingOutputHook() {
plugin::LinkerPlugin *P = llvm::cast<plugin::LinkerPlugin>(UserPluginHandle);
RegisterTimer T(HookName, ThisModule.saveString(UserPluginHandle->GetName()),
Stats);
if (ThisModule.getPrinter()->tracePlugins())
if (isTraced())
ThisConfig.raise(Diag::trace_plugin_hook) << getPluginName() << HookName;
P->ActBeforeWritingOutput();
}
Expand Down
Loading
Loading