Skip to content

Commit bd9af09

Browse files
committed
[Macro] Add env variable option to dump exectuable plugin messagings
Set 'SWIFT_DUMP_PLUGIN_MESSAGING' env variable to enable it.
1 parent df35da4 commit bd9af09

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

include/swift/AST/PluginRegistry.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ class LoadedExecutablePlugin {
6464
/// Callbacks to be called when the connection is restored.
6565
llvm::SmallVector<std::function<void(void)> *, 0> onReconnect;
6666

67+
/// Flag to dump plugin messagings.
68+
bool dumpMessaging = false;
69+
6770
/// Cleanup function to call ASTGen.
6871
std::function<void(void)> cleanup;
6972

@@ -121,6 +124,8 @@ class LoadedExecutablePlugin {
121124

122125
const void *getCapability() { return capability; };
123126
void setCapability(const void *newValue) { capability = newValue; };
127+
128+
void setDumpMessaging(bool flag) { dumpMessaging = flag; }
124129
};
125130

126131
class PluginRegistry {
@@ -131,7 +136,12 @@ class PluginRegistry {
131136
llvm::StringMap<std::unique_ptr<LoadedExecutablePlugin>>
132137
LoadedPluginExecutables;
133138

139+
/// Flag to dump plugin messagings.
140+
bool dumpMessaging = false;
141+
134142
public:
143+
PluginRegistry();
144+
135145
/// Load a dynamic link library specified by \p path.
136146
/// If \p path plugin is already loaded, this returns the cached object.
137147
llvm::Expected<void *> loadLibraryPlugin(llvm::StringRef path);

lib/AST/PluginRegistry.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040

4141
using namespace swift;
4242

43+
PluginRegistry::PluginRegistry() {
44+
dumpMessaging = ::getenv("SWIFT_DUMP_PLUGIN_MESSAGING") != nullptr;
45+
}
46+
4347
llvm::Expected<void *> PluginRegistry::loadLibraryPlugin(StringRef path) {
4448
auto found = LoadedPluginLibraries.find(path);
4549
if (found != LoadedPluginLibraries.end()) {
@@ -94,6 +98,8 @@ PluginRegistry::loadExecutablePlugin(StringRef path) {
9498
auto plugin = std::unique_ptr<LoadedExecutablePlugin>(
9599
new LoadedExecutablePlugin(path, stat.getLastModificationTime()));
96100

101+
plugin->setDumpMessaging(dumpMessaging);
102+
97103
// Launch here to see if it's actually executable, and diagnose (by returning
98104
// an error) if necessary.
99105
if (auto error = plugin->spawnIfNeeded()) {
@@ -213,6 +219,10 @@ ssize_t LoadedExecutablePlugin::PluginProcess::write(const void *buf,
213219
llvm::Error LoadedExecutablePlugin::sendMessage(llvm::StringRef message) const {
214220
ssize_t writtenSize = 0;
215221

222+
if (dumpMessaging) {
223+
llvm::dbgs() << "->(plugin:" << Process->pid << ") " << message << "\n";
224+
}
225+
216226
const char *data = message.data();
217227
size_t size = message.size();
218228

@@ -269,5 +279,9 @@ llvm::Expected<std::string> LoadedExecutablePlugin::waitForNextMessage() const {
269279
message.append(buffer, readSize);
270280
}
271281

282+
if (dumpMessaging) {
283+
llvm::dbgs() << "<-(plugin:" << Process->pid << ") " << message << "\n";
284+
}
285+
272286
return message;
273287
}

lib/ASTGen/Sources/ASTGen/PluginHost.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,6 @@ struct CompilerPlugin {
103103

104104
private func sendMessage(_ message: HostToPluginMessage) throws {
105105
let hadError = try LLVMJSON.encoding(message) { (data) -> Bool in
106-
// // FIXME: Add -dump-plugin-message option?
107-
// data.withMemoryRebound(to: UInt8.self) { buffer in
108-
// print(">> " + String(decoding: buffer, as: UTF8.self))
109-
// }
110106
return Plugin_sendMessage(opaqueHandle, BridgedData(baseAddress: data.baseAddress, size: data.count))
111107
}
112108
if hadError {
@@ -122,10 +118,6 @@ struct CompilerPlugin {
122118
throw PluginError.failedToReceiveMessage
123119
}
124120
let data = UnsafeBufferPointer(start: result.baseAddress, count: result.size)
125-
// // FIXME: Add -dump-plugin-message option?
126-
// data.withMemoryRebound(to: UInt8.self) { buffer in
127-
// print("<< " + String(decoding: buffer, as: UTF8.self))
128-
// }
129121
return try LLVMJSON.decode(PluginToHostMessage.self, from: data)
130122
}
131123

0 commit comments

Comments
 (0)