Skip to content

Commit d7cd55a

Browse files
Troy Curtisstcurtiss
authored andcommitted
Add --trace=plugin=<name> scoped plugin tracing
Extend --trace=<category>[=<name>] (mirroring symbol=/section=/reloc=) to plugins, so an individual plugin's diagnostics can be enabled without tracing every plugin. GeneralOptions::tracePlugin() checks an exact-string/regex allowlist populated by the new plugin=<name> case, falling back to "trace everything" for the pre-existing bare --trace=plugin. Plugin::isTraced() wraps this check and replaces every tracePlugins()-gated call site in Plugin.cpp, PluginManager.cpp, and GNULDBackend::RunPluginsAndProcessHelper with per-plugin gating via O->prolog().getPlugin()->isTraced(). (cherry picked from commit b4b7af1) Signed-off-by: Troy Curtiss <trcurtiss@gmail.com>
1 parent 29b1f7e commit d7cd55a

7 files changed

Lines changed: 66 additions & 27 deletions

File tree

include/eld/Config/GeneralOptions.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ class GeneralOptions {
152152

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

155+
bool tracePlugin(std::string const &PluginName) const;
156+
155157
bool traceLTO(void) const;
156158

157159
bool codegenOpts(void) const;
@@ -1037,6 +1039,10 @@ class GeneralOptions {
10371039

10381040
bool isSectionTracingRequested() const { return SectionTracingRequested; }
10391041

1042+
void setPluginTracingRequested() { PluginTracingRequested = true; }
1043+
1044+
bool isPluginTracingRequested() const { return PluginTracingRequested; }
1045+
10401046
// --------------Dynamic Linker-------------------------
10411047
bool hasDynamicLinker() const { return BDynamicLinker; }
10421048

@@ -1371,9 +1377,11 @@ class GeneralOptions {
13711377
std::vector<llvm::Regex> SymbolTrace;
13721378
std::vector<llvm::Regex> RelocTrace;
13731379
std::vector<llvm::Regex> SectionTrace;
1380+
std::vector<llvm::Regex> PluginTrace;
13741381
std::vector<std::string> SymbolsToTrace;
13751382
std::vector<std::string> SectionsToTrace;
13761383
std::vector<std::string> RelocsToTrace;
1384+
std::vector<std::string> PluginsToTrace;
13771385
std::vector<llvm::Regex> MergeStrSectionsToTrace;
13781386
MergeStrTraceType MergeStrTraceValue = MergeStrTraceType::NONE;
13791387
std::set<std::string> RelocVerify;
@@ -1414,6 +1422,7 @@ class GeneralOptions {
14141422
llvm::StringRef TrampolineMapFile; // TrampolineMap
14151423
bool SymbolTracingRequested = false;
14161424
bool SectionTracingRequested = false;
1425+
bool PluginTracingRequested = false;
14171426
std::vector<llvm::StringRef> RequestedTimeRegions;
14181427
DiagnosticEngine *DiagEngine = nullptr;
14191428
bool BDynamicLinker = true;

include/eld/Driver/GnuLinkerOptions.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,7 @@ defm trace
862862
"\t\t\t --trace=garbage-collection : trace linker garbage "
863863
"collection\n"
864864
"\t\t\t --trace=plugin : trace plugin\n"
865+
"\t\t\t --trace=plugin=<plugin-name> : trace a single plugin\n"
865866
"\t\t\t --trace=threads : trace threads\n"
866867
"\t\t\t --trace=assignments : trace symbol assignments\n"
867868
"\t\t\t --trace=command-line : trace header info\n"

include/eld/Script/Plugin.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ class Plugin {
6161

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

64+
/// Returns true if diagnostics should be traced for this plugin, honoring
65+
/// both bare --trace=plugin and scoped --trace=plugin=<name>.
66+
bool isTraced() const;
67+
6468
plugin::PluginBase *getLinkerPlugin() const { return UserPluginHandle; }
6569

6670
void *getLibraryHandle() const { return PluginLibraryHandle; }

lib/Config/GeneralOptions.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,13 @@ eld::Expected<void> GeneralOptions::setTrace(const char *PTraceType) {
210210
std::string Sym = TraceType.substr(Pos + 1).str();
211211
SectionTrace.emplace_back(llvm::Regex(Sym));
212212
SectionsToTrace.emplace_back(Sym);
213+
} else if (TraceType.starts_with("plugin") && TraceType.contains('=')) {
214+
setPluginTracingRequested();
215+
TraceMe = DiagEngine->getPrinter()->TracePlugin;
216+
size_t Pos = TraceType.find_last_of('=');
217+
std::string PluginName = TraceType.substr(Pos + 1).str();
218+
PluginTrace.emplace_back(llvm::Regex(PluginName));
219+
PluginsToTrace.emplace_back(PluginName);
213220
} else if (TraceType.starts_with("merge-strings")) {
214221
size_t Pos = TraceType.find_last_of('=');
215222
std::string Arg = TraceType.substr(Pos + 1).str();
@@ -402,6 +409,20 @@ bool GeneralOptions::traceReloc(std::string const &RelocName) const {
402409
});
403410
}
404411

412+
bool GeneralOptions::tracePlugin(std::string const &PluginName) const {
413+
if (!DiagEngine->getPrinter()->tracePlugins())
414+
return false;
415+
// Bare "--trace=plugin" (no scoped names given): trace every plugin.
416+
if (!PluginTracingRequested)
417+
return true;
418+
StringRef PluginRef(PluginName);
419+
return llvm::any_of(PluginsToTrace,
420+
[&](const std::string &S) { return S == PluginName; }) ||
421+
llvm::any_of(PluginTrace, [&](const llvm::Regex &Regex) {
422+
return Regex.match(PluginRef);
423+
});
424+
}
425+
405426
std::vector<llvm::StringRef> GeneralOptions::getLTOOptionsAsString() const {
406427
std::vector<llvm::StringRef> ReturnValue;
407428
if ((LTOOptions & LTOVerbose) == LTOVerbose)

lib/Plugin/PluginManager.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ bool PluginManager::callVisitSymbolHook(LDSymbol *Sym, llvm::StringRef SymName,
111111

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

@@ -136,7 +136,7 @@ void PluginManager::setAuxiliarySymbolNameMap(
136136
const ObjectFile::AuxiliarySymbolNameMap &AuxSymNameMap, const Plugin *P) {
137137
ObjFile->setAuxiliarySymbolNameMap(AuxSymNameMap);
138138
AuxSymNameMapProvider[ObjFile] = P;
139-
if (DE.getPrinter()->tracePlugins())
139+
if (P->isTraced())
140140
DE.raise(Diag::trace_set_aux_sym_name_map)
141141
<< P->getPluginName() << ObjFile->getInput()->decoratedPath();
142142
}

lib/Script/Plugin.cpp

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ Plugin::Plugin(plugin::Plugin::Type T, std::string LibraryName,
4949
ThisModule(Module), ThisConfig(Module.getConfig()),
5050
IsDefaultPlugin(DefaultPlugin) {}
5151

52+
bool Plugin::isTraced() const {
53+
return ThisConfig.options().tracePlugin(getPluginType());
54+
}
55+
5256
std::string Plugin::resolvePath(const LinkerConfig &PConfig) {
5357
// Library already loaded!
5458
if (PluginLibraryHandle)
@@ -79,7 +83,7 @@ std::string Plugin::resolvePath(const LinkerConfig &PConfig) {
7983

8084
if (nullptr != NS) {
8185
PluginLibraryName = NS->getFullPath();
82-
if (ThisModule.getPrinter()->tracePlugins())
86+
if (isTraced())
8387
ThisConfig.raise(Diag::using_plugin) << PluginLibraryName << Name;
8488
}
8589
return PluginLibraryName;
@@ -151,15 +155,15 @@ bool Plugin::setFunctions() {
151155
return false;
152156
}
153157

154-
if (ThisModule.getPrinter()->tracePlugins()) {
158+
if (isTraced()) {
155159
ThisConfig.raise(Diag::found_register_function) << Register << LibraryName;
156160
ThisConfig.raise(Diag::found_function_for_plugintype)
157161
<< PluginFunc << LibraryName;
158162
}
159163

160164
std::string PluginCleanupFunc = "Cleanup";
161165
void *C = DynamicLibrary::GetFunction(PluginLibraryHandle, PluginCleanupFunc);
162-
if (C && ThisModule.getPrinter()->tracePlugins()) {
166+
if (C && isTraced()) {
163167
ThisConfig.raise(Diag::found_cleanup_function)
164168
<< PluginCleanupFunc << LibraryName;
165169
}
@@ -182,7 +186,7 @@ bool Plugin::setFunctions() {
182186
bool Plugin::getUserPlugin() {
183187
std::string LibraryName = DynamicLibrary::getLibraryName(Name);
184188

185-
if (ThisModule.getPrinter()->tracePlugins())
189+
if (isTraced())
186190
ThisConfig.raise(Diag::registering_all_functions);
187191

188192
UserPluginHandle = (*GetPluginFunction)(getPluginType().c_str());
@@ -192,7 +196,7 @@ bool Plugin::getUserPlugin() {
192196
return false;
193197
}
194198

195-
if (ThisModule.getPrinter()->tracePlugins())
199+
if (isTraced())
196200
ThisConfig.raise(Diag::found_plugin_handler)
197201
<< getPluginType() << LibraryName;
198202

@@ -211,7 +215,7 @@ bool Plugin::init(eld::OutputTarWriter *OutputTar) {
211215
return false;
212216
eld::RegisterTimer T(
213217
"Init", ThisModule.saveString(UserPluginHandle->GetName()), Stats);
214-
if (ThisModule.getPrinter()->tracePlugins())
218+
if (isTraced())
215219
ThisConfig.raise(Diag::note_initializing_plugin)
216220
<< getPluginType() << DynamicLibrary::getLibraryName(Name)
217221
<< UserPluginHandle->GetName();
@@ -235,14 +239,14 @@ bool Plugin::run(std::vector<Plugin *> &Plugins) {
235239
return false;
236240
eld::RegisterTimer T(
237241
"Run", ThisModule.saveString(UserPluginHandle->GetName()), Stats);
238-
if (ThisModule.getPrinter()->tracePlugins())
242+
if (isTraced())
239243
ThisConfig.raise(Diag::running_plugin)
240244
<< getPluginType() << DynamicLibrary::getLibraryName(Name)
241245
<< UserPluginHandle->GetName();
242246
Plugins.push_back(this);
243247
Running R(this);
244248
plugin::Plugin *P = llvm::cast<plugin::Plugin>(UserPluginHandle);
245-
plugin::Plugin::Status S = P->Run(ThisModule.getPrinter()->tracePlugins());
249+
plugin::Plugin::Status S = P->Run(isTraced());
246250
if (S == plugin::Plugin::Status::ERROR || P->GetLastError()) {
247251
ThisConfig.raise(Diag::plugin_has_error)
248252
<< getPluginType() << DynamicLibrary::getLibraryName(Name)
@@ -290,7 +294,7 @@ bool Plugin::destroy() {
290294
return false;
291295
eld::RegisterTimer T(
292296
"Destroy", ThisModule.saveString(UserPluginHandle->GetName()), Stats);
293-
if (ThisModule.getPrinter()->tracePlugins())
297+
if (isTraced())
294298
ThisConfig.raise(Diag::plugin_destroy) << getPluginType();
295299
plugin::Plugin *P = llvm::cast<plugin::Plugin>(UserPluginHandle);
296300
P->Destroy();
@@ -332,7 +336,7 @@ bool Plugin::check() {
332336
return false;
333337
}
334338

335-
if (ThisModule.getPrinter()->tracePlugins())
339+
if (isTraced())
336340
ThisConfig.raise(Diag::note_plugin_version)
337341
<< PluginMajor << PluginMinor << DynamicLibrary::getLibraryName(Name)
338342
<< getPluginType();
@@ -522,7 +526,7 @@ void Plugin::callInitHook() {
522526
plugin::LinkerPlugin *P = llvm::cast<plugin::LinkerPlugin>(UserPluginHandle);
523527
RegisterTimer T("Init", ThisModule.saveString(UserPluginHandle->GetName()),
524528
Stats);
525-
if (ThisModule.getPrinter()->tracePlugins())
529+
if (isTraced())
526530
ThisConfig.raise(Diag::trace_plugin_init) << getPluginName();
527531
P->Init(PluginOptions);
528532
}
@@ -531,15 +535,15 @@ void Plugin::callDestroyHook() {
531535
plugin::LinkerPlugin *P = llvm::cast<plugin::LinkerPlugin>(UserPluginHandle);
532536
RegisterTimer T("Destroy", ThisModule.saveString(UserPluginHandle->GetName()),
533537
Stats);
534-
if (ThisModule.getPrinter()->tracePlugins())
538+
if (isTraced())
535539
ThisConfig.raise(Diag::trace_plugin_destroy) << getPluginName();
536540
P->Destroy();
537541
}
538542

539543
void Plugin::registerCommandLineOption(
540544
const std::string &Option, bool HasValue,
541545
const CommandLineOptionSpec::OptionHandlerType &OptionHandler) {
542-
if (ThisModule.getPrinter()->tracePlugins()) {
546+
if (isTraced()) {
543547
if (HasValue)
544548
ThisConfig.raise(Diag::trace_plugin_register_opt_with_val)
545549
<< getPluginName() << Option;
@@ -568,7 +572,7 @@ void Plugin::callVisitSectionsHook(InputFile &IF) {
568572
plugin::LinkerPlugin *P = llvm::cast<plugin::LinkerPlugin>(UserPluginHandle);
569573
RegisterTimer T("VisitSections",
570574
ThisModule.saveString(UserPluginHandle->GetName()), Stats);
571-
if (ThisModule.getPrinter()->tracePlugins())
575+
if (isTraced())
572576
ThisConfig.raise(Diag::trace_plugin_visit_sections)
573577
<< getPluginName() << IF.getInput()->decoratedPath();
574578
P->VisitSections(plugin::InputFile(&IF));
@@ -579,7 +583,7 @@ void Plugin::callVisitSymbolHook(LDSymbol *Sym, llvm::StringRef SymName,
579583
plugin::LinkerPlugin *P = llvm::cast<plugin::LinkerPlugin>(UserPluginHandle);
580584
RegisterTimer T("VisitSymbol",
581585
ThisModule.saveString(UserPluginHandle->GetName()), Stats);
582-
if (ThisModule.getPrinter()->tracePlugins())
586+
if (isTraced())
583587
ThisConfig.raise(Diag::trace_plugin_visit_symbol)
584588
<< getPluginName() << SymName;
585589
std::unique_ptr<SymbolInfo> UpSymInfo = std::make_unique<SymbolInfo>(SymInfo);
@@ -593,7 +597,7 @@ void Plugin::callActBeforeRuleMatchingHook() {
593597
plugin::LinkerPlugin *P = llvm::cast<plugin::LinkerPlugin>(UserPluginHandle);
594598
RegisterTimer T(HookName, ThisModule.saveString(UserPluginHandle->GetName()),
595599
Stats);
596-
if (ThisModule.getPrinter()->tracePlugins())
600+
if (isTraced())
597601
ThisConfig.raise(Diag::trace_plugin_hook) << getPluginName() << HookName;
598602
P->ActBeforeRuleMatching();
599603
}
@@ -603,7 +607,7 @@ void Plugin::callActBeforeSectionMergingHook() {
603607
plugin::LinkerPlugin *P = llvm::cast<plugin::LinkerPlugin>(UserPluginHandle);
604608
RegisterTimer T(HookName, ThisModule.saveString(UserPluginHandle->GetName()),
605609
Stats);
606-
if (ThisModule.getPrinter()->tracePlugins())
610+
if (isTraced())
607611
ThisConfig.raise(Diag::trace_plugin_hook) << getPluginName() << HookName;
608612
P->ActBeforeSectionMerging();
609613
}
@@ -613,7 +617,7 @@ void Plugin::callActBeforePerformingLayoutHook() {
613617
plugin::LinkerPlugin *P = llvm::cast<plugin::LinkerPlugin>(UserPluginHandle);
614618
RegisterTimer T(HookName, ThisModule.saveString(UserPluginHandle->GetName()),
615619
Stats);
616-
if (ThisModule.getPrinter()->tracePlugins())
620+
if (isTraced())
617621
ThisConfig.raise(Diag::trace_plugin_hook) << getPluginName() << HookName;
618622
P->ActBeforePerformingLayout();
619623
}
@@ -623,7 +627,7 @@ void Plugin::callActBeforeWritingOutputHook() {
623627
plugin::LinkerPlugin *P = llvm::cast<plugin::LinkerPlugin>(UserPluginHandle);
624628
RegisterTimer T(HookName, ThisModule.saveString(UserPluginHandle->GetName()),
625629
Stats);
626-
if (ThisModule.getPrinter()->tracePlugins())
630+
if (isTraced())
627631
ThisConfig.raise(Diag::trace_plugin_hook) << getPluginName() << HookName;
628632
P->ActBeforeWritingOutput();
629633
}

lib/Target/GNULDBackend.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4626,7 +4626,7 @@ bool GNULDBackend::RunPluginsAndProcessHelper(
46264626
return false;
46274627
}
46284628

4629-
if (m_Module.getPrinter()->tracePlugins())
4629+
if (O->prolog().getPlugin()->isTraced())
46304630
config().raise(Diag::allocating_memory) << S->size() << S->name();
46314631

46324632
MemoryRegion mr(reinterpret_cast<uint8_t *>(MB.base()),
@@ -4639,10 +4639,10 @@ bool GNULDBackend::RunPluginsAndProcessHelper(
46394639
return false;
46404640
}
46414641
}
4642-
if (m_Module.getPrinter()->tracePlugins())
4642+
if (O->prolog().getPlugin()->isTraced())
46434643
config().raise(Diag::applying_relocations) << S->name();
46444644
m_Module.getLinker()->getObjLinker()->relocation(false);
4645-
if (m_Module.getPrinter()->tracePlugins())
4645+
if (O->prolog().getPlugin()->isTraced())
46464646
config().raise(Diag::syncing_relocations) << S->name();
46474647
m_Module.getLinker()->getObjLinker()->syncRelocations(
46484648
reinterpret_cast<uint8_t *>(MB.base()));
@@ -4668,7 +4668,7 @@ bool GNULDBackend::RunPluginsAndProcessHelper(
46684668
B.Name = std::string(S->name());
46694669

46704670
// Add the Memory Block.
4671-
if (m_Module.getPrinter()->tracePlugins())
4671+
if (O->prolog().getPlugin()->isTraced())
46724672
config().raise(Diag::adding_memory_blocks) << S->name();
46734673

46744674
// Add Memory Blocks.
@@ -4677,7 +4677,7 @@ bool GNULDBackend::RunPluginsAndProcessHelper(
46774677
else
46784678
FOP->AddBlocks(std::move(B));
46794679

4680-
if (m_Module.getPrinter()->tracePlugins())
4680+
if (O->prolog().getPlugin()->isTraced())
46814681
config().raise(Diag::calling_handler) << S->name();
46824682

46834683
// Run the algorithm.
@@ -4689,7 +4689,7 @@ bool GNULDBackend::RunPluginsAndProcessHelper(
46894689

46904690
auto RB = MatchSections ? VAP->GetBlocks() : FOP->GetBlocks();
46914691

4692-
if (m_Module.getPrinter()->tracePlugins())
4692+
if (O->prolog().getPlugin()->isTraced())
46934693
config().raise(Diag::plugin_returned_blocks) << RB.size() << S->name();
46944694

46954695
ELFSection *OutputSection = S;

0 commit comments

Comments
 (0)