trace: Specify trace file name

Allow users to specify a file for trace-outputs at configuration.
Also, allow trace files to be annotated by <pid> so each qemu instance has
unique traces.

The trace file name can be passed as a config option:
--trace-file=/path/to/file
(Default: trace )
At runtime, the pid of the qemu process is appended to the filename so
that mutiple qemu instances do not have overlapping logs.

Eg : trace-1234 for qemu launched with pid 1234.

I have yet to test this on windows. getpid() is used at many places
in code(including vnc.c), so I'm hoping this would be okay too.

Edited-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
This commit is contained in:
Prerna Saxena 2010-07-13 09:26:32 +01:00 committed by Anthony Liguori
parent 1e2cf2bc45
commit 9410b56c82
2 changed files with 29 additions and 4 deletions

12
configure vendored
View File

@ -318,6 +318,7 @@ check_utests="no"
user_pie="no" user_pie="no"
zero_malloc="" zero_malloc=""
trace_backend="nop" trace_backend="nop"
trace_file="trace"
# OS specific # OS specific
if check_define __linux__ ; then if check_define __linux__ ; then
@ -522,6 +523,8 @@ for opt do
;; ;;
--trace-backend=*) trace_backend="$optarg" --trace-backend=*) trace_backend="$optarg"
;; ;;
--trace-file=*) trace_file="$optarg"
;;
--enable-gprof) gprof="yes" --enable-gprof) gprof="yes"
;; ;;
--static) --static)
@ -901,6 +904,8 @@ echo " --disable-docs disable documentation build"
echo " --disable-vhost-net disable vhost-net acceleration support" echo " --disable-vhost-net disable vhost-net acceleration support"
echo " --enable-vhost-net enable vhost-net acceleration support" echo " --enable-vhost-net enable vhost-net acceleration support"
echo " --trace-backend=B Trace backend nop simple" echo " --trace-backend=B Trace backend nop simple"
echo " --trace-file=NAME Full PATH,NAME of file to store traces"
echo " Default:trace-<pid>"
echo "" echo ""
echo "NOTE: The object files are built at the place where configure is launched" echo "NOTE: The object files are built at the place where configure is launched"
exit 1 exit 1
@ -2206,6 +2211,7 @@ echo "fdatasync $fdatasync"
echo "uuid support $uuid" echo "uuid support $uuid"
echo "vhost-net support $vhost_net" echo "vhost-net support $vhost_net"
echo "Trace backend $trace_backend" echo "Trace backend $trace_backend"
echo "Trace output file $trace_file-<pid>"
if test $sdl_too_old = "yes"; then if test $sdl_too_old = "yes"; then
echo "-> Your SDL version is too old - please upgrade to have SDL support" echo "-> Your SDL version is too old - please upgrade to have SDL support"
@ -2471,6 +2477,12 @@ echo "TRACE_BACKEND=$trace_backend" >> $config_host_mak
if test "$trace_backend" = "simple"; then if test "$trace_backend" = "simple"; then
echo "CONFIG_SIMPLE_TRACE=y" >> $config_host_mak echo "CONFIG_SIMPLE_TRACE=y" >> $config_host_mak
fi fi
# Set the appropriate trace file.
if test "$trace_backend" = "simple"; then
trace_file="\"$trace_file-%u\""
fi
echo "CONFIG_TRACE_FILE=$trace_file" >> $config_host_mak
echo "TOOLS=$tools" >> $config_host_mak echo "TOOLS=$tools" >> $config_host_mak
echo "ROMS=$roms" >> $config_host_mak echo "ROMS=$roms" >> $config_host_mak
echo "MAKE=$make" >> $config_host_mak echo "MAKE=$make" >> $config_host_mak

View File

@ -54,13 +54,26 @@ static bool write_header(FILE *fp)
return fwrite(&header, sizeof header, 1, fp) == 1; return fwrite(&header, sizeof header, 1, fp) == 1;
} }
static bool open_trace_file(void)
{
char *filename;
if (asprintf(&filename, CONFIG_TRACE_FILE, getpid()) < 0) {
return false;
}
trace_fp = fopen(filename, "w");
free(filename);
if (!trace_fp) {
return false;
}
return write_header(trace_fp);
}
static void flush_trace_buffer(void) static void flush_trace_buffer(void)
{ {
if (!trace_fp) { if (!trace_fp) {
trace_fp = fopen("trace.log", "w"); open_trace_file();
if (trace_fp) {
write_header(trace_fp);
}
} }
if (trace_fp) { if (trace_fp) {
size_t unused; /* for when fwrite(3) is declared warn_unused_result */ size_t unused; /* for when fwrite(3) is declared warn_unused_result */