From 22c63a6080142fb23488f52538f7f56bac1dc587 Mon Sep 17 00:00:00 2001 From: Rihyeon Kim Date: Thu, 12 Sep 2024 10:59:12 +0900 Subject: [PATCH] Signed-off-by: Rihyeon Kim uftrace: Improve logfile creation location Ensure that when using "--logfile=", the logfile is created inside the uftrace.data directory. Additionally, when using both "--host" and "--logfile" options simultaneously on the clinet, the logfile in the uftrace.data directory is sent to the host. Fixed: #831 --- cmds/record.c | 13 ++++++++----- uftrace.c | 11 ++++++++++- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/cmds/record.c b/cmds/record.c index 54b17c786..74f9684cf 100644 --- a/cmds/record.c +++ b/cmds/record.c @@ -1423,12 +1423,15 @@ static void send_event_file(int sock, const char *dirname) send_trace_metadata(sock, dirname, "events.txt"); } -static void send_log_file(int sock, const char *logfile) +static void send_log_file(int sock, const char *dirname, const char *logfile) { - if (access(logfile, F_OK) != 0) + char *logfile_path = NULL; + xasprintf(&logfile_path, "%s/%s", dirname, logfile); + + if (access(logfile_path, F_OK) != 0) return; - send_trace_metadata(sock, NULL, (char *)logfile); + send_trace_metadata(sock, dirname, (char *)logfile); } static void update_session_maps(struct uftrace_opts *opts) @@ -2070,7 +2073,7 @@ static void write_symbol_files(struct writer_data *wd, struct uftrace_opts *opts if (opts->event) send_event_file(sock, opts->dirname); if (opts->logfile) - send_log_file(sock, opts->logfile); + send_log_file(sock, opts->dirname, opts->logfile); send_trace_end(sock); close(sock); @@ -2275,7 +2278,7 @@ int command_record(int argc, char *argv[], struct uftrace_opts *opts) check_perf_event(opts); if (!opts->nop) { - if (create_directory(opts->dirname) < 0) + if (!opts->logfile && (create_directory(opts->dirname) < 0)) return -1; xasprintf(&channel, "%s/%s", opts->dirname, ".channel"); diff --git a/uftrace.c b/uftrace.c index 7ae1426ba..e29062120 100644 --- a/uftrace.c +++ b/uftrace.c @@ -1474,7 +1474,16 @@ int main(int argc, char *argv[]) debug = 1; if (opts.logfile) { - logfp = fopen(opts.logfile, "a"); + char *logfile_path = NULL; + if (create_directory(opts.dirname) < 0) { + ret = -1; + goto cleanup; + } + + xasprintf(&logfile_path, "%s/%s", opts.dirname, opts.logfile); + logfp = fopen(logfile_path, "a"); + free(logfile_path); + if (logfp == NULL) { logfp = stderr; pr_err("cannot open log file");