2020#include " swift/Demangling/Demangler.h"
2121#include " swift/Runtime/AccessibleFunction.h"
2222#include " swift/Runtime/Concurrent.h"
23+ #include " swift/Runtime/EnvironmentVariables.h"
2324#include " swift/Runtime/Metadata.h"
25+ #include " swift/Threading/Once.h"
2426#include " Tracing.h"
2527
2628#include < cstdint>
@@ -98,6 +100,29 @@ static Lazy<AccessibleFunctionsState> Functions;
98100
99101} // end anonymous namespace
100102
103+ LLVM_ATTRIBUTE_UNUSED
104+ static void _dumpAccessibleFunctionRecords (void *context) {
105+ auto &S = Functions.get ();
106+
107+ fprintf (stderr, " ==== Accessible Function Records ====\n " );
108+ int count = 0 ;
109+ for (const auto §ion : S.SectionsToScan .snapshot ()) {
110+ for (auto &record : section) {
111+ auto recordName =
112+ swift::Demangle::makeSymbolicMangledNameStringRef (record.Name .get ());
113+ auto demangledRecordName =
114+ swift::Demangle::demangleSymbolAsString (recordName);
115+ fprintf (stderr, " Record name: %s\n " , recordName.data ());
116+ fprintf (stderr, " Demangled: %s\n " , demangledRecordName.c_str ());
117+ fprintf (stderr, " Function Ptr: %p\n " , record.Function .get ());
118+ fprintf (stderr, " Flags.IsDistributed: %d\n " , record.Flags .isDistributed ());
119+ ++count;
120+ }
121+ }
122+ fprintf (stderr, " Record count: %d\n " , count);
123+ fprintf (stderr, " ==== End of Accessible Function Records ====\n " );
124+ }
125+
101126static void _registerAccessibleFunctions (AccessibleFunctionsState &C,
102127 AccessibleFunctionsSection section) {
103128 C.SectionsToScan .push_back (section);
@@ -119,27 +144,6 @@ void swift::addImageAccessibleFunctionsBlockCallback(
119144 addImageAccessibleFunctionsBlockCallbackUnsafe (baseAddress, functions, size);
120145}
121146
122- // TODO(distributed): expose dumping records via a flag
123- LLVM_ATTRIBUTE_UNUSED
124- static void _dumpAccessibleFunctionRecords () {
125- auto &S = Functions.get ();
126-
127- fprintf (stderr, " ==== Accessible Function Records ====\n " );
128- int count = 0 ;
129- for (const auto §ion : S.SectionsToScan .snapshot ()) {
130- for (auto &record : section) {
131- auto recordName =
132- swift::Demangle::makeSymbolicMangledNameStringRef (record.Name .get ());
133- fprintf (stderr, " Record name: %s\n " , recordName.data ());
134- fprintf (stderr, " Function Ptr: %p\n " , record.Function .get ());
135- fprintf (stderr, " Flags.IsDistributed: %d\n " , record.Flags .isDistributed ());
136- ++count;
137- }
138- }
139- fprintf (stderr, " Record count: %d\n " , count);
140- fprintf (stderr, " ==== End of Accessible Function Records ====\n " );
141- }
142-
143147static const AccessibleFunctionRecord *
144148_searchForFunctionRecord (AccessibleFunctionsState &S, llvm::StringRef name) {
145149 auto traceState = runtime::trace::accessible_function_scan_begin (name);
@@ -160,13 +164,19 @@ const AccessibleFunctionRecord *
160164swift::runtime::swift_findAccessibleFunction (const char *targetNameStart,
161165 size_t targetNameLength) {
162166 auto &S = Functions.get ();
163-
164167 llvm::StringRef name{targetNameStart, targetNameLength};
168+
169+ if (swift::runtime::environment::SWIFT_DUMP_ACCESSIBLE_FUNCTIONS ()) {
170+ static swift::once_t dumpAccessibleFunctionsToken;
171+ swift::once (dumpAccessibleFunctionsToken, _dumpAccessibleFunctionRecords, nullptr );
172+ }
173+
165174 // Look for an existing entry.
166175 {
167176 auto snapshot = S.Cache .snapshot ();
168- if (auto E = snapshot.find (name))
177+ if (auto E = snapshot.find (name)) {
169178 return E->getRecord ();
179+ }
170180 }
171181
172182 // If entry doesn't exist (either record doesn't exist, hasn't been loaded, or
0 commit comments