Accessing the command line from an opt plugin?

anyone have a suggestion for how to inspect the opt or clang command line from inside an opt plugin? we have a situation where it would be really nice if the Alive2 plugin was able to report how it had been invoked, in order to facilitate reproducing bugs that it finds.

when opt (and other tools) starts up, the command line options are saved in a PrettyStackTracePrinter object so they can be dumped if LLVM crashes, but then this object is a private member of the InitLLVM class, hiding this information from unrelated code.

To avoid introducing more CommandLine-related global state, I suggest making it the responsibility of the driver (opt, clang) to forward the information explicitly via an extension of the plugin API.

We can’t possibly have ABI compatibility with plugins anyway (right?), so how about changing the plugin API along the lines of:

--- a/llvm/include/llvm/Passes/PassPlugin.h
+++ b/llvm/include/llvm/Passes/PassPlugin.h
@@ -33 +33 @@ class PassBuilder;
-#define LLVM_PLUGIN_API_VERSION 1
+#define LLVM_PLUGIN_API_VERSION 2
@@ -52,0 +53,4 @@ struct PassPluginLibraryInfo {
+
+  /// The callback for informing the plugin about the commandline with which the
+  /// tool that is loading the plugin has been invoked. May be nullptr.
+  void (*NotifyCommandLine)(int argc, const char **argv);
1 Like

The Windows-specific LLVM support code exposes llvm::windows::GetCommandLineArguments which is a wrapper around the system function GetCommandLineW. That is one possible alternative. Unfortunately, there does not appear to be anything analogous for other operating systems within what LLVM provides itself.

1 Like

This is janky and not portable, but you can consider /proc/self/cmdline as an equivalent API on platforms where /proc is available. Obviously, it is unparsed, but maybe that’s good enough for Alive2’s diagnostic purposes.

1 Like

ooh, you know, this might actually be good enough for our purposes, since afaik all of the heavy lifting here is going to be done on Linux machines, we don’t need all of this functionality to be widely available. Nuno and I will discuss.

also in the longer term perhaps this’ll happen: