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..129ac1123 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"); @@ -1583,9 +1592,15 @@ int main(int argc, char *argv[]) wait_for_pager(); cleanup: - if (opts.logfile) + if (opts.logfile) { + char *logfile_path = NULL; fclose(logfp); + xasprintf(&logfile_path, "%s/%s", opts.dirname, opts.logfile); + copy_file(opts.logfile, logfile_path); + + free(logfile_path); + } if (opts.opt_file) free_parsed_cmdline(argv - opts.idx);