Add env var to control cleanup of temp dylib files. (#7519)
This allows perf and Instruments to find the files and resolve symbols.
diff --git a/docs/developers/developing_iree/profiling_cpu_events.md b/docs/developers/developing_iree/profiling_cpu_events.md
index bf0138f..13c8ee8 100644
--- a/docs/developers/developing_iree/profiling_cpu_events.md
+++ b/docs/developers/developing_iree/profiling_cpu_events.md
@@ -33,6 +33,17 @@
switches). Anyone may use this system call to implement a profiler, but Linux
readily offers one, [`perf`](https://perf.wiki.kernel.org/index.php/Main_Page).
+### Preserving Artifacts
+
+By default IREE cleans up any temporary files it creates while running. Tools
+like perf, however, require those files exist even after the process has exited.
+The environment variable `IREE_PRESERVE_DYLIB_TEMP_FILES` can be set to preserve
+the files. This is only needed for the CPU path when using the system loader.
+
+```shell
+export IREE_PRESERVE_DYLIB_TEMP_FILES=1
+```
+
### Desktop Linux
On desktop Linux we can use
diff --git a/iree/base/internal/dynamic_library_posix.c b/iree/base/internal/dynamic_library_posix.c
index c059926..73948f6 100644
--- a/iree/base/internal/dynamic_library_posix.c
+++ b/iree/base/internal/dynamic_library_posix.c
@@ -31,6 +31,17 @@
void* handle;
};
+// Returns true if the temp files we extract should be kept after the library
+// is closed. This is useful for tooling such as perf that need to find the
+// shared libraries after the program has exited or atexit tools like tracy that
+// require the library still be loaded.
+//
+// To override:
+// IREE_PRESERVE_DYLIB_TEMP_FILES=1 iree-run-module ...
+static bool iree_dynamic_library_should_preserve_temp_files() {
+ return getenv("IREE_PRESERVE_DYLIB_TEMP_FILES") != NULL;
+}
+
// Allocate a new string from |allocator| returned in |out_file_path| containing
// a path to a unique file on the filesystem.
static iree_status_t iree_dynamic_library_make_temp_file_path(
@@ -209,8 +220,11 @@
// Unlink the temp file - it's still open by the loader but won't be
// accessible to anyone else and will be deleted once the library is
- // unloaded.
- remove(temp_path);
+ // unloaded. Note that we don't remove the file if the user requested we keep
+ // it around for tooling to access.
+ if (!iree_dynamic_library_should_preserve_temp_files()) {
+ remove(temp_path);
+ }
iree_allocator_free(allocator, temp_path);
IREE_TRACE_ZONE_END(z0);