trace: split trace_init_file out of trace_init_backends

This is cleaner, and improves error reporting with -daemonize.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Denis V. Lunev <den@openvz.org>
Acked-by: Christian Borntraeger <borntraeger@de.ibm.com>
Message-id: 1452174932-28657-4-git-send-email-den@openvz.org
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
This commit is contained in:
Paolo Bonzini 2016-01-07 16:55:24 +03:00 committed by Stefan Hajnoczi
parent 45bd0b41bd
commit 41fc57e44e
6 changed files with 38 additions and 17 deletions

View File

@ -435,7 +435,7 @@ int main(int argc, char **argv)
} }
break; break;
case 'T': case 'T':
if (!trace_init_backends(optarg)) { if (!trace_init_backends()) {
exit(1); /* error message will have been printed */ exit(1); /* error message will have been printed */
} }
break; break;

View File

@ -145,17 +145,24 @@ void trace_init_events(const char *fname)
loc_pop(&loc); loc_pop(&loc);
} }
bool trace_init_backends(const char *file) void trace_init_file(const char *file)
{ {
#ifdef CONFIG_TRACE_SIMPLE #ifdef CONFIG_TRACE_SIMPLE
if (!st_init(file)) { st_set_trace_file(file);
fprintf(stderr, "failed to initialize simple tracing backend.\n");
return false;
}
#else #else
if (file) { if (file) {
fprintf(stderr, "error: -trace file=...: " fprintf(stderr, "error: -trace file=...: "
"option not supported by the selected tracing backends\n"); "option not supported by the selected tracing backends\n");
exit(1);
}
#endif
}
bool trace_init_backends(void)
{
#ifdef CONFIG_TRACE_SIMPLE
if (!st_init()) {
fprintf(stderr, "failed to initialize simple tracing backend.\n");
return false; return false;
} }
#endif #endif

View File

@ -157,7 +157,7 @@ static void trace_event_set_state_dynamic(TraceEvent *ev, bool state);
* *
* Returns: Whether the backends could be successfully initialized. * Returns: Whether the backends could be successfully initialized.
*/ */
bool trace_init_backends(const char *file); bool trace_init_backends(void);
/** /**
* trace_init_events: * trace_init_events:
@ -170,6 +170,17 @@ bool trace_init_backends(const char *file);
*/ */
void trace_init_events(const char *file); void trace_init_events(const char *file);
/**
* trace_init_file:
* @file: Name of trace output file; may be NULL.
* Corresponds to commandline option "-trace file=...".
*
* Record the name of the output file for the tracing backend.
* Exits if no selected backend does not support specifying the
* output file, and a non-NULL file was passed.
*/
void trace_init_file(const char *file);
#include "trace/control-internal.h" #include "trace/control-internal.h"

View File

@ -322,7 +322,7 @@ void st_set_trace_file_enabled(bool enable)
* @file The trace file name or NULL for the default name-<pid> set at * @file The trace file name or NULL for the default name-<pid> set at
* config time * config time
*/ */
bool st_set_trace_file(const char *file) void st_set_trace_file(const char *file)
{ {
st_set_trace_file_enabled(false); st_set_trace_file_enabled(false);
@ -336,7 +336,6 @@ bool st_set_trace_file(const char *file)
} }
st_set_trace_file_enabled(true); st_set_trace_file_enabled(true);
return true;
} }
void st_print_trace_file_status(FILE *stream, int (*stream_printf)(FILE *stream, const char *fmt, ...)) void st_print_trace_file_status(FILE *stream, int (*stream_printf)(FILE *stream, const char *fmt, ...))
@ -374,7 +373,7 @@ static GThread *trace_thread_create(GThreadFunc fn)
return thread; return thread;
} }
bool st_init(const char *file) bool st_init(void)
{ {
GThread *thread; GThread *thread;
@ -387,6 +386,5 @@ bool st_init(const char *file)
} }
atexit(st_flush_trace_buffer); atexit(st_flush_trace_buffer);
st_set_trace_file(file);
return true; return true;
} }

View File

@ -20,8 +20,8 @@
void st_print_trace_file_status(FILE *stream, fprintf_function stream_printf); void st_print_trace_file_status(FILE *stream, fprintf_function stream_printf);
void st_set_trace_file_enabled(bool enable); void st_set_trace_file_enabled(bool enable);
bool st_set_trace_file(const char *file); void st_set_trace_file(const char *file);
bool st_init(const char *file); bool st_init(void);
void st_flush_trace_buffer(void); void st_flush_trace_buffer(void);
typedef struct { typedef struct {

13
vl.c
View File

@ -2988,7 +2988,7 @@ int main(int argc, char **argv, char **envp)
bool userconfig = true; bool userconfig = true;
const char *log_mask = NULL; const char *log_mask = NULL;
const char *log_file = NULL; const char *log_file = NULL;
const char *trace_file = NULL; char *trace_file = NULL;
ram_addr_t maxram_size; ram_addr_t maxram_size;
uint64_t ram_slots = 0; uint64_t ram_slots = 0;
FILE *vmstate_dump_file = NULL; FILE *vmstate_dump_file = NULL;
@ -3905,7 +3905,10 @@ int main(int argc, char **argv, char **envp)
exit(1); exit(1);
} }
trace_init_events(qemu_opt_get(opts, "events")); trace_init_events(qemu_opt_get(opts, "events"));
trace_file = qemu_opt_get(opts, "file"); if (trace_file) {
g_free(trace_file);
}
trace_file = g_strdup(qemu_opt_get(opts, "file"));
qemu_opts_del(opts); qemu_opts_del(opts);
break; break;
} }
@ -4089,6 +4092,8 @@ int main(int argc, char **argv, char **envp)
exit(0); exit(0);
} }
trace_init_file(trace_file);
/* Open the logfile at this point and set the log mask if necessary. /* Open the logfile at this point and set the log mask if necessary.
*/ */
if (log_file) { if (log_file) {
@ -4106,7 +4111,7 @@ int main(int argc, char **argv, char **envp)
} }
if (!is_daemonized()) { if (!is_daemonized()) {
if (!trace_init_backends(trace_file)) { if (!trace_init_backends()) {
exit(1); exit(1);
} }
} }
@ -4653,7 +4658,7 @@ int main(int argc, char **argv, char **envp)
os_setup_post(); os_setup_post();
if (is_daemonized()) { if (is_daemonized()) {
if (!trace_init_backends(trace_file)) { if (!trace_init_backends()) {
exit(1); exit(1);
} }
} }