From 49809a2e95ae75b17db8efe4f45066059a21781c Mon Sep 17 00:00:00 2001 From: William Whitehouse Date: Fri, 31 Jan 2025 11:14:50 +0000 Subject: [PATCH 1/4] Respect EXTERNIS_OUTPUT_DIR environment variable --- externis.cc | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/externis.cc b/externis.cc index a5864aa..765002f 100644 --- a/externis.cc +++ b/externis.cc @@ -22,6 +22,7 @@ #include #include #include +#include #include "c-family/c-pragma.h" #include "cpplib.h" @@ -106,8 +107,13 @@ bool setup_output(int argc, plugin_argument *argv) { // TODO: Validate we only compile one TU at a time. FILE *trace_file = nullptr; if (argc == 0) { - char file_template[] = "/tmp/trace_XXXXXX.json"; - int fd = mkstemps(file_template, 5); + static constexpr auto default_directory = "/tmp"; + const char* directory = getenv("EXTERNIS_OUTPUT_DIR"); + if (!directory) { + directory = default_directory; + } + std::string file_template = std::string(default_directory) + "/trace_XXXXXX.json"; + int fd = mkstemps(file_template.data(), 5); if (fd == -1) { perror("Externis mkstemps error: "); return false; From 3b04dc8abb9fb98f4a00f7382170fff994b7ddfd Mon Sep 17 00:00:00 2001 From: William Whitehouse Date: Fri, 31 Jan 2025 11:52:54 +0000 Subject: [PATCH 2/4] Respect new argument --- externis.cc | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/externis.cc b/externis.cc index 765002f..51fac99 100644 --- a/externis.cc +++ b/externis.cc @@ -22,7 +22,6 @@ #include #include #include -#include #include "c-family/c-pragma.h" #include "cpplib.h" @@ -103,38 +102,37 @@ static const char *PLUGIN_NAME = "externis"; bool setup_output(int argc, plugin_argument *argv) { const char *flag_name = "trace"; + const char *dir_flag_name = "trace-dir"; // TODO: Maybe make the default filename related to the source filename. // TODO: Validate we only compile one TU at a time. FILE *trace_file = nullptr; if (argc == 0) { - static constexpr auto default_directory = "/tmp"; - const char* directory = getenv("EXTERNIS_OUTPUT_DIR"); - if (!directory) { - directory = default_directory; - } - std::string file_template = std::string(default_directory) + "/trace_XXXXXX.json"; - int fd = mkstemps(file_template.data(), 5); + char file_template[] = "/tmp/trace_XXXXXX.json"; + int fd = mkstemps(file_template, 5); if (fd == -1) { perror("Externis mkstemps error: "); return false; } trace_file = fdopen(fd, "w"); - } else if (argc == 1) { - if (strcmp(argv[0].key, flag_name)) { - fprintf(stderr, - "Externis Error! Arguments must be -fplugin-arg-%s-%s=FILENAME", - PLUGIN_NAME, flag_name); - return false; - } + } else if (argc == 1 && !strcmp(argv[0].key, flag_name)) { trace_file = fopen(argv[0].value, "w"); if (!trace_file) { fprintf(stderr, "Externis Error! Couldn't open %s for writing\n", argv[0].value); } + } else if (argc == 1 && !strcmp(argv[0].key, dir_flag_name)) { + std::string file_template{argv[0].value}; + file_template += "/trace_XXXXXX.json"; + int fd = mkstemps(file_template, 5); + if (fd == -1) { + perror("Externis mkstemps error: "); + return false; + } + trace_file = fdopen(fd, "w"); } else { fprintf(stderr, - "Externis Error! Arguments must be -fplugin-arg-%s-%s=FILENAME\n", - PLUGIN_NAME, flag_name); + "Externis Error! Arguments must be -fplugin-arg-%s-%s=FILENAME or -fplugin-arg-%s-%s=DIRECTORY\n", + PLUGIN_NAME, flag_name, PLUGIN_NAME, dir_flag_name); return false; } if (trace_file) { From c4d40d94f60018e18042aaa08c6c171660f85a21 Mon Sep 17 00:00:00 2001 From: William Whitehouse Date: Fri, 31 Jan 2025 11:55:25 +0000 Subject: [PATCH 3/4] String trouble --- externis.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/externis.cc b/externis.cc index 51fac99..43df4ff 100644 --- a/externis.cc +++ b/externis.cc @@ -123,7 +123,7 @@ bool setup_output(int argc, plugin_argument *argv) { } else if (argc == 1 && !strcmp(argv[0].key, dir_flag_name)) { std::string file_template{argv[0].value}; file_template += "/trace_XXXXXX.json"; - int fd = mkstemps(file_template, 5); + int fd = mkstemps(file_template.data(), 5); if (fd == -1) { perror("Externis mkstemps error: "); return false; From 0ff0d9fc7b5316ef2d43d511157b1baef409e7c9 Mon Sep 17 00:00:00 2001 From: William Whitehouse Date: Fri, 31 Jan 2025 11:58:38 +0000 Subject: [PATCH 4/4] Readme --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9df5a55..f6c20c9 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,12 @@ gcc -fplugin=externis -fplugin-arg-externis-trace=SOME_PATH/ # Otherwise: gcc -fplugin=/PATH/TO/build/externis.so -fplugin-arg-externis-trace=SOME_PATH/trace.json ``` -If a trace output path is not given, a temporary file with the name +Alternatively, you can specify a directory to write the files to: +```bash +gcc -fplugin=/PATH/TO/build/externis.so -fplugin-arg-externis-trace-dir=SOME_PATH/ +``` +In which case the output will be written to SOME_PATH/trace_XXXXXX.json +If a trace output path or directory is not given, a temporary file with the name `/tmp/trace_XXXXXX.json` will be used instead. ## License & Copyright