gemu_log is an old logging mechanism used to implement strace logging in the bsd-user tree. It logs directly to stderr and cannot easily be redirected. This change instead causes strace to log via the qemu_log subsystem which has fine-grained logging control, and a centralized mechanism for log redirection. bsd-user does not currently implement any logging redirection options, or log masking options, but this change brings it more in line with the linux-user tree.
Signed-off-by: Josh Kunz <j...@google.com> --- bsd-user/main.c | 29 +++++++++++++---------------- bsd-user/qemu.h | 2 -- bsd-user/strace.c | 32 +++++++++++++++----------------- bsd-user/syscall.c | 31 +++++++++++++++++++------------ 4 files changed, 47 insertions(+), 47 deletions(-) diff --git a/bsd-user/main.c b/bsd-user/main.c index 770c2b267a..d024ac067f 100644 --- a/bsd-user/main.c +++ b/bsd-user/main.c @@ -55,15 +55,6 @@ enum BSDType bsd_type; by remapping the process stack directly at the right place */ unsigned long x86_stack_size = 512 * 1024; -void gemu_log(const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - vfprintf(stderr, fmt, ap); - va_end(ap); -} - #if defined(TARGET_I386) int cpu_get_pic_interrupt(CPUX86State *env) { @@ -731,6 +722,7 @@ int main(int argc, char **argv) const char *cpu_type; const char *log_file = NULL; const char *log_mask = NULL; + bool enable_strace = false; struct target_pt_regs regs1, *regs = ®s1; struct image_info info1, *info = &info1; TaskState ts1, *ts = &ts1; @@ -845,7 +837,7 @@ int main(int argc, char **argv) } else if (!strcmp(r, "singlestep")) { singlestep = 1; } else if (!strcmp(r, "strace")) { - do_strace = 1; + enable_strace = true; } else if (!strcmp(r, "trace")) { g_free(trace_file); trace_file = trace_opt_parse(optarg); @@ -854,17 +846,26 @@ int main(int argc, char **argv) } } + if (getenv("QEMU_STRACE")) { + enable_strace = true; + } + /* init debug */ qemu_log_needs_buffers(); qemu_set_log_filename(log_file, &error_fatal); - if (log_mask) { + if (log_mask || enable_strace) { int mask; mask = qemu_str_to_log_mask(log_mask); - if (!mask) { + if (log_mask && !mask) { qemu_print_log_usage(stdout); exit(1); } + + if (enable_strace) { + mask |= LOG_STRACE; + } + qemu_set_log(mask); } @@ -916,10 +917,6 @@ int main(int argc, char **argv) #endif thread_cpu = cpu; - if (getenv("QEMU_STRACE")) { - do_strace = 1; - } - target_environ = envlist_to_environ(envlist, NULL); envlist_free(envlist); diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h index 09e8aed9c7..5762e3a6e5 100644 --- a/bsd-user/qemu.h +++ b/bsd-user/qemu.h @@ -152,7 +152,6 @@ abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1, abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1, abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6); -void gemu_log(const char *fmt, ...) GCC_FMT_ATTR(1, 2); extern THREAD CPUState *thread_cpu; void cpu_loop(CPUArchState *env); char *target_strerror(int err); @@ -188,7 +187,6 @@ print_openbsd_syscall(int num, abi_long arg1, abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6); void print_openbsd_syscall_ret(int num, abi_long ret); -extern int do_strace; /* signal.c */ void process_pending_signals(CPUArchState *cpu_env); diff --git a/bsd-user/strace.c b/bsd-user/strace.c index fa66fe1ee2..6ee1510555 100644 --- a/bsd-user/strace.c +++ b/bsd-user/strace.c @@ -23,8 +23,6 @@ #include "qemu.h" -int do_strace; - /* * Utility functions */ @@ -36,17 +34,17 @@ static void print_sysctl(const struct syscallname *name, abi_long arg1, uint32_t i; int32_t *namep; - gemu_log("%s({ ", name->name); + qemu_log("%s({ ", name->name); namep = lock_user(VERIFY_READ, arg1, sizeof(int32_t) * arg2, 1); if (namep) { int32_t *p = namep; for (i = 0; i < (uint32_t)arg2; i++) { - gemu_log("%d ", tswap32(*p++)); + qemu_log("%d ", tswap32(*p++)); } unlock_user(namep, arg1, 0); } - gemu_log("}, %u, 0x" TARGET_ABI_FMT_lx ", 0x" TARGET_ABI_FMT_lx ", 0x" + qemu_log("}, %u, 0x" TARGET_ABI_FMT_lx ", 0x" TARGET_ABI_FMT_lx ", 0x" TARGET_ABI_FMT_lx ", 0x" TARGET_ABI_FMT_lx ")", (uint32_t)arg2, arg3, arg4, arg5, arg6); } @@ -62,7 +60,7 @@ static void print_execve(const struct syscallname *name, abi_long arg1, if (s == NULL) { return; } - gemu_log("%s(\"%s\",{", name->name, s); + qemu_log("%s(\"%s\",{", name->name, s); unlock_user(s, arg1, 0); for (arg_ptr_addr = arg2; ; arg_ptr_addr += sizeof(abi_ulong)) { @@ -78,11 +76,11 @@ static void print_execve(const struct syscallname *name, abi_long arg1, break; } if ((s = lock_user_string(arg_addr))) { - gemu_log("\"%s\",", s); + qemu_log("\"%s\",", s); unlock_user(s, arg_addr, 0); } } - gemu_log("NULL})"); + qemu_log("NULL})"); } static void print_ioctl(const struct syscallname *name, @@ -90,7 +88,7 @@ static void print_ioctl(const struct syscallname *name, abi_long arg5, abi_long arg6) { /* Decode the ioctl request */ - gemu_log("%s(%d, 0x%0lx { IO%s%s GRP:0x%x('%c') CMD:%d LEN:%d }, 0x" + qemu_log("%s(%d, 0x%0lx { IO%s%s GRP:0x%x('%c') CMD:%d LEN:%d }, 0x" TARGET_ABI_FMT_lx ", ...)", name->name, (int)arg1, @@ -111,9 +109,9 @@ static void print_ioctl(const struct syscallname *name, static void print_syscall_ret_addr(const struct syscallname *name, abi_long ret) { if (ret == -1) { - gemu_log(" = -1 errno=%d (%s)\n", errno, strerror(errno)); + qemu_log(" = -1 errno=%d (%s)\n", errno, strerror(errno)); } else { - gemu_log(" = 0x" TARGET_ABI_FMT_lx "\n", ret); + qemu_log(" = 0x" TARGET_ABI_FMT_lx "\n", ret); } } @@ -121,7 +119,7 @@ static void print_syscall_ret_addr(const struct syscallname *name, abi_long ret) static void print_syscall_ret_raw(struct syscallname *name, abi_long ret) { - gemu_log(" = 0x" TARGET_ABI_FMT_lx "\n", ret); + qemu_log(" = 0x" TARGET_ABI_FMT_lx "\n", ret); } #endif @@ -148,7 +146,7 @@ static void print_syscall(int num, const struct syscallname *scnames, TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld ")"; - gemu_log("%d ", getpid() ); + qemu_log("%d ", getpid()); for (i = 0; i < nscnames; i++) { if (scnames[i].nr == num) { @@ -161,13 +159,13 @@ static void print_syscall(int num, const struct syscallname *scnames, if (scnames[i].format != NULL) { format = scnames[i].format; } - gemu_log(format, scnames[i].name, arg1, arg2, arg3, arg4, arg5, + qemu_log(format, scnames[i].name, arg1, arg2, arg3, arg4, arg5, arg6); } return; } } - gemu_log("Unknown syscall %d\n", num); + qemu_log("Unknown syscall %d\n", num); } static void print_syscall_ret(int num, abi_long ret, @@ -181,10 +179,10 @@ static void print_syscall_ret(int num, abi_long ret, scnames[i].result(&scnames[i], ret); } else { if (ret < 0) { - gemu_log(" = -1 errno=" TARGET_ABI_FMT_ld " (%s)\n", -ret, + qemu_log(" = -1 errno=" TARGET_ABI_FMT_ld " (%s)\n", -ret, strerror(-ret)); } else { - gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret); + qemu_log(" = " TARGET_ABI_FMT_ld "\n", ret); } } break; diff --git a/bsd-user/syscall.c b/bsd-user/syscall.c index 0d45b654bb..53635d183e 100644 --- a/bsd-user/syscall.c +++ b/bsd-user/syscall.c @@ -321,12 +321,13 @@ abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1, void *p; #ifdef DEBUG - gemu_log("freebsd syscall %d\n", num); + qemu_log("freebsd syscall %d\n", num); #endif record_syscall_start(cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, 0, 0); - if(do_strace) + if (unlikely(qemu_loglevel_mask(LOG_STRACE))) { print_freebsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6); + } switch(num) { case TARGET_FREEBSD_NR_exit: @@ -401,10 +402,12 @@ abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1, } fail: #ifdef DEBUG - gemu_log(" = %ld\n", ret); + qemu_log(" = %ld\n", ret); #endif - if (do_strace) + + if (unlikely(qemu_loglevel_mask(LOG_STRACE))) { print_freebsd_syscall_ret(num, ret); + } record_syscall_return(cpu, num, ret); return ret; @@ -422,13 +425,14 @@ abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1, void *p; #ifdef DEBUG - gemu_log("netbsd syscall %d\n", num); + qemu_log("netbsd syscall %d\n", num); #endif record_syscall_start(cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, 0, 0); - if(do_strace) + if (unlikely(qemu_loglevel_mask(LOG_STRACE))) { print_netbsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6); + } switch(num) { case TARGET_NETBSD_NR_exit: @@ -480,10 +484,11 @@ abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1, } fail: #ifdef DEBUG - gemu_log(" = %ld\n", ret); + qemu_log(" = %ld\n", ret); #endif - if (do_strace) + if (unlikely(qemu_loglevel_mask(LOG_STRACE))) { print_netbsd_syscall_ret(num, ret); + } record_syscall_return(cpu, num, ret); return ret; @@ -501,13 +506,14 @@ abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1, void *p; #ifdef DEBUG - gemu_log("openbsd syscall %d\n", num); + qemu_log("openbsd syscall %d\n", num); #endif record_syscall_start(cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, 0, 0); - if(do_strace) + if (unlikely(qemu_loglevel_mask(LOG_STRACE))) { print_openbsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6); + } switch(num) { case TARGET_OPENBSD_NR_exit: @@ -559,10 +565,11 @@ abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1, } fail: #ifdef DEBUG - gemu_log(" = %ld\n", ret); + qemu_log(" = %ld\n", ret); #endif - if (do_strace) + if (unlikely(qemu_loglevel_mask(LOG_STRACE))) { print_openbsd_syscall_ret(num, ret); + } record_syscall_return(cpu, num, ret); return ret; -- 2.25.0.341.g760bfbb309-goog