Skip to contentSkip to conversation

migration: Replace gemu_log with qemu_log

PATCH Superseded Josh Kunz 3 participants 13 messages Jan 17, 2020 – Feb 4, 2020

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Josh Kunz

author
#0 lore raw

v2 cover migration: Replace gemu_log with qemu_log

Summary of v2 changes:
* Removed backwards-compatibility code for non-strace log statements.
* Removed new qemu_log interface for adding or removing fields from the log mask.
* Removed LOG_USER and converted all uses (except one) to LOG_UNIMP.
* One gemu_log statement was converted to an assert.
* Some style cleanup.

The linux-user and bsd-user trees both widely use a function called gemu_log (notice the 'g') for miscellaneous and strace logging. This function predates the newer qemu_log function, and has a few drawbacks compared to qemu_log:

1. Always logs to stderr, no logging redirection.
2. "Miscellaneous" logging cannot be disabled, so it may mix with guest logging.
3. Inconsistency with other parts of the QEMU codebase, and a confusing name.

The second issue is especially troubling because it can interfere with programs that expect to communicate via stderr.

This change introduces one new logging masks to the qemu_log subsystem to support its use for user-mode logging: the LOG_STRACE mask for strace-specific logging. Further, it replaces all existing uses of gemu_log with the appropriate qemu_log_mask(LOG_{UNIMP,STRACE}, ...) based on the log message.

Backwards incompatibility:
* Log messages for unimplemented user-mode features are no longer logged by default. They have to be enabled by setting the LOG_UNIMP mask.
* Log messages for strace/unimplemented user-mode features may be redirected based on -D, instead of always logging to stderr.

Tested:
* Built with clang 9 and g++ 8.3
* make check run with clang 9 build
* Verified:
* QEMU_STRACE/-strace still works for linux-user
* make vm-build-netbsd EXTRA_CONFIGURE_OPTS="--disable-system" passed.

Not tested:
* Build/logging with bsd-user. I do not have easy access to a BSD system.

Josh Kunz (4):
linux-user: Use `qemu_log' for non-strace logging
linux-user: Use `qemu_log' for strace
linux-user: remove gemu_log from the linux-user tree
bsd-user: Replace gemu_log with qemu_log

 bsd-user/main.c           |  13 +-
 bsd-user/qemu.h           |   2 -
 bsd-user/strace.c         |  32 ++-
 bsd-user/syscall.c        |  31 ++-
 include/qemu/log.h        |   2 +
 linux-user/arm/cpu_loop.c |   5 +-
 linux-user/fd-trans.c     |  55 +++--
 linux-user/main.c         |  39 ++--
 linux-user/qemu.h         |   2 -
 linux-user/signal.c       |   2 +-
 linux-user/strace.c       | 479 +++++++++++++++++++-------------------
 linux-user/syscall.c      |  68 +++---
 linux-user/vm86.c         |   3 +-
 util/log.c                |   2 +
 14 files changed, 383 insertions(+), 352 deletions(-)
Signature · 1 line
2.25.0.341.g760bfbb309-goog
author
#1 lore raw

PATCH v2 1/4 linux-user: Use `qemu_log' for non-strace logging

Since most calls to gemu_log are actually logging unimplemented features, this change replaces most non-strace calls to gemu_log with calls to qemu_log_mask(LOG_UNIMP, ...). This allows the user to easily log to a file, and to mask out these log messages if they desire.

Note: This change is slightly backwards incompatible, since now these "unimplemented" log messages will not be logged by default.

---
 linux-user/arm/cpu_loop.c |  5 ++--
 linux-user/fd-trans.c     | 55 +++++++++++++++++++++++++--------------
 linux-user/syscall.c      | 55 +++++++++++++++++++++------------------
 linux-user/vm86.c         |  3 ++-
 4 files changed, 69 insertions(+), 49 deletions(-)
linux-user/arm/cpu_loop.c +3 -2
index 1fae90c6df..cf618daa1c 100644
@@ -349,8 +349,9 @@ void cpu_loop(CPUARMState *env)
                             env->regs[0] = cpu_get_tls(env);
                             break;
                         default:
-                            gemu_log("qemu: Unsupported ARM syscall: 0x%x\n",
-                                     n);
+                            qemu_log_mask(LOG_UNIMP,
+                                          "qemu: Unsupported ARM syscall: 0x%x\n",
+                                          n);
                             env->regs[0] = -TARGET_ENOSYS;
                             break;
                         }
linux-user/fd-trans.c +35 -20

161 lines not on this page · raw message

linux-user/syscall.c +29 -26

109 lines not on this page · raw message

linux-user/vm86.c +2 -1
index 2fa7a89edc..4412522c4c 100644
@@ -402,7 +402,8 @@ int do_vm86(CPUX86State *env, long subfunction, abi_ulong vm86_addr)
     case TARGET_VM86_FREE_IRQ:
     case TARGET_VM86_GET_IRQ_BITS:
     case TARGET_VM86_GET_AND_RESET_IRQ:
-        gemu_log("qemu: unsupported vm86 subfunction (%ld)\n", subfunction);
+        qemu_log_mask(LOG_UNIMP, "qemu: unsupported vm86 subfunction (%ld)\n",
+                      subfunction);
         ret = -TARGET_EINVAL;
         goto out;
     case TARGET_VM86_PLUS_INSTALL_CHECK:
Signature · 1 line
2.25.0.341.g760bfbb309-goog
Reviewed-by on v2 1/4
#2 lore raw

Re: [PATCH v2 1/4] linux-user: Use `qemu_log' for non-strace logging

Le 17/01/2020 à 20:28, Josh Kunz a écrit :

Quoted 7 lines

Since most calls to gemu_log are actually logging unimplemented features, this change replaces most non-strace calls to gemu_log with calls to qemu_log_mask(LOG_UNIMP, ...). This allows the user to easily log to a file, and to mask out these log messages if they desire.

Note: This change is slightly backwards incompatible, since now these "unimplemented" log messages will not be logged by default.

This is a good incompatibility as these messages were unexpected by the tools catching stderr. They don't happen on "real" systems.

...

Quoted 27 lines
linux-user/syscall.c +5 -14
index 249e4b95fc..629f3a21b5 100644
@@ -1545,20 +1545,18 @@ static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
             - sizeof(struct target_cmsghdr);
 
         space += CMSG_SPACE(len);
-        if (space > msgh->msg_controllen) {
-            space -= CMSG_SPACE(len);
-            /* This is a QEMU bug, since we allocated the payload
-             * area ourselves (unlike overflow in host-to-target
-             * conversion, which is just the guest giving us a buffer
-             * that's too small). It can't happen for the payload types
-             * we currently support; if it becomes an issue in future
-             * we would need to improve our allocation strategy to
-             * something more intelligent than "twice the size of the
-             * target buffer we're reading from".
-             */
-            gemu_log("Host cmsg overflow\n");
-            break;
-        }
+
+        /*
+         * This is a QEMU bug, since we allocated the payload
+         * area ourselves (unlike overflow in host-to-target
+         * conversion, which is just the guest giving us a buffer

linux-user/syscall.c static inline abi_long target_to_host_cmsg(struct msghdr *msgh, · line 1562

Quoted 10 lines
linux-user/syscall.c +7 -0
+         * that's too small). It can't happen for the payload types
+         * we currently support; if it becomes an issue in future
+         * we would need to improve our allocation strategy to
+         * something more intelligent than "twice the size of the
+         * target buffer we're reading from".
+         */
+        assert(space > msgh->msg_controllen && "Host cmsg overflow");
 
         if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
             cmsg->cmsg_level = SOL_SOCKET;

Could you move this to a separate patch: you are not using qemu_log() here and I'm not convinced that crashing is better than ignoring the remaining part of the buffer.

For the other changes:

Thanks,
Lauren

#3 lore raw

Re: [PATCH v2 1/4] linux-user: Use `qemu_log' for non-strace logging

Quoted 48 lines · Laurent Vivier
Quoted 7 lines · Josh Kunz

Since most calls to gemu_log are actually logging unimplemented features, this change replaces most non-strace calls to gemu_log with calls to qemu_log_mask(LOG_UNIMP, ...). This allows the user to easily log to a file, and to mask out these log messages if they desire.

Note: This change is slightly backwards incompatible, since now these "unimplemented" log messages will not be logged by default.

This is a good incompatibility as these messages were unexpected by the tools catching stderr. They don't happen on "real" systems.

...

Quoted 35 lines
linux-user/syscall.c +12 -14
index 249e4b95fc..629f3a21b5 100644
@@ -1545,20 +1545,18 @@ static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
             - sizeof(struct target_cmsghdr);
 
         space += CMSG_SPACE(len);
-        if (space > msgh->msg_controllen) {
-            space -= CMSG_SPACE(len);
-            /* This is a QEMU bug, since we allocated the payload
-             * area ourselves (unlike overflow in host-to-target
-             * conversion, which is just the guest giving us a buffer
-             * that's too small). It can't happen for the payload types
-             * we currently support; if it becomes an issue in future
-             * we would need to improve our allocation strategy to
-             * something more intelligent than "twice the size of the
-             * target buffer we're reading from".
-             */
-            gemu_log("Host cmsg overflow\n");
-            break;
-        }
+
+        /*
+         * This is a QEMU bug, since we allocated the payload
+         * area ourselves (unlike overflow in host-to-target
+         * conversion, which is just the guest giving us a buffer
+         * that's too small). It can't happen for the payload types
+         * we currently support; if it becomes an issue in future
+         * we would need to improve our allocation strategy to
+         * something more intelligent than "twice the size of the
+         * target buffer we're reading from".
+         */
+        assert(space > msgh->msg_controllen && "Host cmsg overflow");
 
Quoted 6 lines

linux-user/syscall.c static inline abi_long target_to_host_cmsg(struct msghdr *msgh, · line 1562

Quoted 2 lines
linux-user/syscall.c +0 -0
         if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
             cmsg->cmsg_level = SOL_SOCKET;

Could you move this to a separate patch: you are not using qemu_log() here and I'm not convinced that crashing is better than ignoring the remaining part of the buffer.

I suggested it should be an assert in the first place. It certainly makes sense to keep it in a separate patch though. I guess you could argue for:

  qemu_log_mask(LOG_UNIMP, "%s: unhandled message size");

but is it really better to partially work and continue? It seems like you would get more subtle hidden bugs.

Quoted 7 lines

For the other changes:

Thanks,
Lauren

Signature · 1 line
Alex Bennée
#4 lore raw

Re: [PATCH v2 1/4] linux-user: Use `qemu_log' for non-strace logging

Le 28/01/2020 à 17:53, Alex Bennée a écrit :

Quoted 42 lines
Quoted 39 lines · Laurent Vivier
Quoted 7 lines · Josh Kunz

Since most calls to gemu_log are actually logging unimplemented features, this change replaces most non-strace calls to gemu_log with calls to qemu_log_mask(LOG_UNIMP, ...). This allows the user to easily log to a file, and to mask out these log messages if they desire.

Note: This change is slightly backwards incompatible, since now these "unimplemented" log messages will not be logged by default.

This is a good incompatibility as these messages were unexpected by the tools catching stderr. They don't happen on "real" systems.

...

Quoted 26 lines
linux-user/syscall.c +4 -14
index 249e4b95fc..629f3a21b5 100644
@@ -1545,20 +1545,18 @@ static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
             - sizeof(struct target_cmsghdr);
 
         space += CMSG_SPACE(len);
-        if (space > msgh->msg_controllen) {
-            space -= CMSG_SPACE(len);
-            /* This is a QEMU bug, since we allocated the payload
-             * area ourselves (unlike overflow in host-to-target
-             * conversion, which is just the guest giving us a buffer
-             * that's too small). It can't happen for the payload types
-             * we currently support; if it becomes an issue in future
-             * we would need to improve our allocation strategy to
-             * something more intelligent than "twice the size of the
-             * target buffer we're reading from".
-             */
-            gemu_log("Host cmsg overflow\n");
-            break;
-        }
+
+        /*
+         * This is a QEMU bug, since we allocated the payload
+         * area ourselves (unlike overflow in host-to-target
Quoted 8 lines
Quoted 8 lines

linux-user/syscall.c static inline abi_long target_to_host_cmsg(struct msghdr *msgh, · line 1559

Quoted 8 lines
linux-user/syscall.c +8 -0
+         * conversion, which is just the guest giving us a buffer
+         * that's too small). It can't happen for the payload types
+         * we currently support; if it becomes an issue in future
+         * we would need to improve our allocation strategy to
+         * something more intelligent than "twice the size of the
+         * target buffer we're reading from".
+         */
+        assert(space > msgh->msg_controllen && "Host cmsg overflow");

Should it be in fact :

  assert(space <= msgh->msg_controllen && "Host cmsg overflow");
Quoted 7 lines
Quoted 6 lines

linux-user/syscall.c static inline abi_long target_to_host_cmsg(struct msghdr *msgh, · line 1562

Quoted 2 lines
linux-user/syscall.c +0 -0
         if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
             cmsg->cmsg_level = SOL_SOCKET;

Could you move this to a separate patch: you are not using qemu_log() here and I'm not convinced that crashing is better than ignoring the remaining part of the buffer.

Quoted 8 lines

I suggested it should be an assert in the first place. It certainly makes sense to keep it in a separate patch though. I guess you could argue for:

  qemu_log_mask(LOG_UNIMP, "%s: unhandled message size");

but is it really better to partially work and continue? It seems like you would get more subtle hidden bugs.

ok, you're right. crash seems to be a better solution.

So, we only need to move this change to a separate patch.

Thanks,
Laurent

author
#5 lore raw

Re: [PATCH v2 1/4] linux-user: Use `qemu_log' for non-strace logging

I've switched it to a LOG_UNIMP, similar to to the one several lines below. I will follow up with a change to switch this to an assert as recommended.

Quoted 80 lines · Laurent Vivier
Quoted 42 lines · Alex Bennée
Quoted 39 lines · Laurent Vivier
Quoted 7 lines · Josh Kunz

Since most calls to gemu_log are actually logging unimplemented features, this change replaces most non-strace calls to gemu_log with calls to qemu_log_mask(LOG_UNIMP, ...). This allows the user to easily log to a file, and to mask out these log messages if they desire.

Note: This change is slightly backwards incompatible, since now these "unimplemented" log messages will not be logged by default.

This is a good incompatibility as these messages were unexpected by the tools catching stderr. They don't happen on "real" systems.

...

Quoted 26 lines
linux-user/syscall.c +4 -14
index 249e4b95fc..629f3a21b5 100644
@@ -1545,20 +1545,18 @@ static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
             - sizeof(struct target_cmsghdr);
 
         space += CMSG_SPACE(len);
-        if (space > msgh->msg_controllen) {
-            space -= CMSG_SPACE(len);
-            /* This is a QEMU bug, since we allocated the payload
-             * area ourselves (unlike overflow in host-to-target
-             * conversion, which is just the guest giving us a buffer
-             * that's too small). It can't happen for the payload types
-             * we currently support; if it becomes an issue in future
-             * we would need to improve our allocation strategy to
-             * something more intelligent than "twice the size of the
-             * target buffer we're reading from".
-             */
-            gemu_log("Host cmsg overflow\n");
-            break;
-        }
+
+        /*
+         * This is a QEMU bug, since we allocated the payload
+         * area ourselves (unlike overflow in host-to-target
Quoted 8 lines
Quoted 8 lines

linux-user/syscall.c static inline abi_long target_to_host_cmsg(struct msghdr *msgh, · line 1559

Quoted 8 lines
linux-user/syscall.c +8 -0
+         * conversion, which is just the guest giving us a buffer
+         * that's too small). It can't happen for the payload types
+         * we currently support; if it becomes an issue in future
+         * we would need to improve our allocation strategy to
+         * something more intelligent than "twice the size of the
+         * target buffer we're reading from".
+         */
+        assert(space > msgh->msg_controllen && "Host cmsg overflow");

Should it be in fact :

  assert(space <= msgh->msg_controllen && "Host cmsg overflow");
Quoted 7 lines
Quoted 6 lines

linux-user/syscall.c static inline abi_long target_to_host_cmsg(struct msghdr *msgh, · line 1562

Quoted 2 lines
linux-user/syscall.c +0 -0
         if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
             cmsg->cmsg_level = SOL_SOCKET;

Could you move this to a separate patch: you are not using qemu_log() here and I'm not convinced that crashing is better than ignoring the remaining part of the buffer.

Quoted 8 lines

I suggested it should be an assert in the first place. It certainly makes sense to keep it in a separate patch though. I guess you could argue for:

  qemu_log_mask(LOG_UNIMP, "%s: unhandled message size");

but is it really better to partially work and continue? It seems like you would get more subtle hidden bugs.

ok, you're right. crash seems to be a better solution.

So, we only need to move this change to a separate patch.

Thanks,
Laurent

author
#6 lore raw

PATCH v2 2/4 linux-user: Use `qemu_log' for strace

In reply to Josh Kunz

This change switches linux-user strace logging to use the newer qemu_log logging subsystem rather than the older gemu_log (notice the "g") logger. qemu_log has several advantages, namely that it allows logging to a file, and provides a more unified interface for configuration of logging (via the QEMU_LOG environment variable or options).

This change introduces a new log mask: LOG_STRACE which is used for logging of user-mode strace messages.

---
 include/qemu/log.h   |   2 +
 linux-user/main.c    |  30 ++-
 linux-user/qemu.h    |   1 -
 linux-user/signal.c  |   2 +-
 linux-user/strace.c  | 479 ++++++++++++++++++++++---------------------
 linux-user/syscall.c |  13 +-
 util/log.c           |   2 +
 7 files changed, 278 insertions(+), 251 deletions(-)
include/qemu/log.h +2 -0
index e0f4e40628..f4724f7330 100644
@@ -62,6 +62,8 @@ static inline bool qemu_log_separate(void)
 #define CPU_LOG_TB_OP_IND  (1 << 16)
 #define CPU_LOG_TB_FPU     (1 << 17)
 #define CPU_LOG_PLUGIN     (1 << 18)
+/* LOG_STRACE is used for user-mode strace logging. */
+#define LOG_STRACE         (1 << 19)
 
 /* Lock output for a series of related logs.  Since this is not needed
  * for a single qemu_log / qemu_log_mask / qemu_log_mask_and_addr, we
linux-user/main.c +23 -7
index fba833aac9..8f1d07cdd6 100644
@@ -60,6 +60,19 @@ unsigned long mmap_min_addr;
 unsigned long guest_base;
 int have_guest_base;
 
+/*
+ * Used to implement backwards-compatibility for the `-strace`, and
+ * QEMU_STRACE options. Without this, the QEMU_LOG can be overwritten by
+ * -strace, or vice versa.
+ */
+static bool enable_strace;
+
+/*
+ * The last log mask given by the user in an environment variable or argument.
+ * Used to support command line arguments overriding environment variables.
+ */
+static int last_log_mask;
+
 /*
  * When running 32-on-64 we should make sure we can fit all of the possible
  * guest address space into a contiguous chunk of virtual host memory.
@@ -223,15 +236,11 @@ static void handle_arg_help(const char *arg)
 
 static void handle_arg_log(const char *arg)
 {
-    int mask;
-
-    mask = qemu_str_to_log_mask(arg);
-    if (!mask) {
+    last_log_mask = qemu_str_to_log_mask(arg);
+    if (!last_log_mask) {
         qemu_print_log_usage(stdout);
         exit(EXIT_FAILURE);
     }
-    qemu_log_needs_buffers();
-    qemu_set_log(mask);
 }
 
 static void handle_arg_dfilter(const char *arg)
@@ -375,7 +384,7 @@ static void handle_arg_singlestep(const char *arg)
 
 static void handle_arg_strace(const char *arg)
 {
-    do_strace = 1;
+    enable_strace = true;
 }
 
 static void handle_arg_version(const char *arg)
@@ -629,6 +638,7 @@ int main(int argc, char **argv, char **envp)
     int i;
     int ret;
     int execfd;
+    int log_mask;
     unsigned long max_reserved_va;
 
     error_init(argv[0]);
@@ -661,6 +671,12 @@ int main(int argc, char **argv, char **envp)
 
     optind = parse_args(argc, argv);
 
+    log_mask = last_log_mask | (enable_strace ? LOG_STRACE : 0);
+    if (log_mask) {
+        qemu_log_needs_buffers();
+        qemu_set_log(log_mask);
+    }
+
     if (!trace_init_backends()) {
         exit(1);
     }
linux-user/qemu.h +0 -1
index f6f5fe5fbb..02c6890c8a 100644
@@ -385,7 +385,6 @@ void print_syscall_ret(int num, abi_long arg1);
  * --- SIGSEGV {si_signo=SIGSEGV, si_code=SI_KERNEL, si_addr=0} ---
  */
 void print_taken_signal(int target_signum, const target_siginfo_t *tinfo);
-extern int do_strace;
 
 /* signal.c */
 void process_pending_signals(CPUArchState *cpu_env);
linux-user/signal.c +1 -1
index 5ca6d62b15..818d867b7b 100644
@@ -864,7 +864,7 @@ static void handle_pending_signal(CPUArchState *cpu_env, int sig,
         handler = sa->_sa_handler;
     }
 
-    if (do_strace) {
+    if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
         print_taken_signal(sig, &k->info);
     }
 
linux-user/strace.c +243 -236

1,259 lines not on this page · raw message

linux-user/syscall.c +7 -6
index 629f3a21b5..54e60f3807 100644
@@ -12098,14 +12098,15 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
     record_syscall_start(cpu, num, arg1,
                          arg2, arg3, arg4, arg5, arg6, arg7, arg8);
 
-    if (unlikely(do_strace)) {
+    if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
         print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
-        ret = do_syscall1(cpu_env, num, arg1, arg2, arg3, arg4,
-                          arg5, arg6, arg7, arg8);
+    }
+
+    ret = do_syscall1(cpu_env, num, arg1, arg2, arg3, arg4,
+                      arg5, arg6, arg7, arg8);
+
+    if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
         print_syscall_ret(num, ret);
-    } else {
-        ret = do_syscall1(cpu_env, num, arg1, arg2, arg3, arg4,
-                          arg5, arg6, arg7, arg8);
     }
 
     record_syscall_return(cpu, num, ret);
util/log.c +2 -0
index 867264da8d..c501b3818d 100644
@@ -328,6 +328,8 @@ const QEMULogItem qemu_log_items[] = {
 #ifdef CONFIG_PLUGIN
     { CPU_LOG_PLUGIN, "plugin", "output from TCG plugins\n"},
 #endif
+    { LOG_STRACE, "strace",
+      "log every user-mode syscall, its input, and its result" },
     { 0, NULL, NULL },
 };
 
Signature · 1 line
2.25.0.341.g760bfbb309-goog
Reviewed-by on v2 2/4
#7 lore raw

Re: [PATCH v2 2/4] linux-user: Use `qemu_log' for strace

Le 17/01/2020 à 20:28, Josh Kunz a écrit :

Quoted 20 lines

This change switches linux-user strace logging to use the newer qemu_log logging subsystem rather than the older gemu_log (notice the "g") logger. qemu_log has several advantages, namely that it allows logging to a file, and provides a more unified interface for configuration of logging (via the QEMU_LOG environment variable or options).

This change introduces a new log mask: LOG_STRACE which is used for logging of user-mode strace messages.

---
 include/qemu/log.h   |   2 +
 linux-user/main.c    |  30 ++-
 linux-user/qemu.h    |   1 -
 linux-user/signal.c  |   2 +-
 linux-user/strace.c  | 479 ++++++++++++++++++++++---------------------
 linux-user/syscall.c |  13 +-
 util/log.c           |   2 +
 7 files changed, 278 insertions(+), 251 deletions(-)

...

Quoted 16 lines
linux-user/syscall.c +4 -3
index 629f3a21b5..54e60f3807 100644
@@ -12098,14 +12098,15 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
     record_syscall_start(cpu, num, arg1,
                          arg2, arg3, arg4, arg5, arg6, arg7, arg8);
 
-    if (unlikely(do_strace)) {
+    if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
         print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
-        ret = do_syscall1(cpu_env, num, arg1, arg2, arg3, arg4,
-                          arg5, arg6, arg7, arg8);
+    }
+
+    ret = do_syscall1(cpu_env, num, arg1, arg2, arg3, arg4,

linux-user/syscall.c abi_long do_syscall(void *cpu_env, int num, abi_long arg1, · line 12112

Quoted 10 lines
linux-user/syscall.c +3 -3
+                      arg5, arg6, arg7, arg8);
+
+    if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
         print_syscall_ret(num, ret);
-    } else {
-        ret = do_syscall1(cpu_env, num, arg1, arg2, arg3, arg4,
-                          arg5, arg6, arg7, arg8);
     }
 
     record_syscall_return(cpu, num, ret);

In term of performance perhaps it sould be better to only test once for the mask as it is done before?

For the other parts:

Thanks,
Laurent

author
#8 lore raw

Re: [PATCH v2 2/4] linux-user: Use `qemu_log' for strace

Quoted 48 lines · Laurent Vivier
Quoted 20 lines · Josh Kunz

This change switches linux-user strace logging to use the newer qemu_log logging subsystem rather than the older gemu_log (notice the "g") logger. qemu_log has several advantages, namely that it allows logging to a file, and provides a more unified interface for configuration of logging (via the QEMU_LOG environment variable or options).

This change introduces a new log mask: LOG_STRACE which is used for logging of user-mode strace messages.

---
 include/qemu/log.h   |   2 +
 linux-user/main.c    |  30 ++-
 linux-user/qemu.h    |   1 -
 linux-user/signal.c  |   2 +-
 linux-user/strace.c  | 479 ++++++++++++++++++++++---------------------
 linux-user/syscall.c |  13 +-
 util/log.c           |   2 +
 7 files changed, 278 insertions(+), 251 deletions(-)

...

Quoted 25 lines
linux-user/syscall.c +7 -6
index 629f3a21b5..54e60f3807 100644
@@ -12098,14 +12098,15 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
     record_syscall_start(cpu, num, arg1,
                          arg2, arg3, arg4, arg5, arg6, arg7, arg8);
 
-    if (unlikely(do_strace)) {
+    if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
         print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
-        ret = do_syscall1(cpu_env, num, arg1, arg2, arg3, arg4,
-                          arg5, arg6, arg7, arg8);
+    }
+
+    ret = do_syscall1(cpu_env, num, arg1, arg2, arg3, arg4,
+                      arg5, arg6, arg7, arg8);
+
+    if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
         print_syscall_ret(num, ret);
-    } else {
-        ret = do_syscall1(cpu_env, num, arg1, arg2, arg3, arg4,
-                          arg5, arg6, arg7, arg8);
     }
 
Quoted 4 lines

linux-user/syscall.c abi_long do_syscall(void *cpu_env, int num, abi_long arg1, · line 12112

Quoted 1 line
linux-user/syscall.c +0 -0
     record_syscall_return(cpu, num, ret);

In term of performance perhaps it sould be better to only test once for the mask as it is done before?

Modern compilers will generate functionally identical sequences for test once or testing twice (which is to say, they recognize they are the same compare: https://godbolt.org/z/VyrMHf IMO testing twice is nicer to read, so I'm leaving it that way for now unless you object.

#9 lore raw

Re: [PATCH v2 2/4] linux-user: Use `qemu_log' for strace

Le 04/02/2020 à 03:55, Josh Kunz a écrit :

Quoted 54 lines · Laurent Vivier
Quoted 48 lines · Josh Kunz
Quoted 20 lines

This change switches linux-user strace logging to use the newer qemu_log logging subsystem rather than the older gemu_log (notice the "g") logger. qemu_log has several advantages, namely that it allows logging to a file, and provides a more unified interface for configuration of logging (via the QEMU_LOG environment variable or options).

This change introduces a new log mask: LOG_STRACE which is used for logging of user-mode strace messages.

---
 include/qemu/log.h   |   2 +
 linux-user/main.c    |  30 ++-
 linux-user/qemu.h    |   1 -
 linux-user/signal.c  |   2 +-
 linux-user/strace.c  | 479 ++++++++++++++++++++++---------------------
 linux-user/syscall.c |  13 +-
 util/log.c           |   2 +
 7 files changed, 278 insertions(+), 251 deletions(-)

...

Quoted 25 lines
linux-user/syscall.c +7 -6
index 629f3a21b5..54e60f3807 100644
@@ -12098,14 +12098,15 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
     record_syscall_start(cpu, num, arg1,
                          arg2, arg3, arg4, arg5, arg6, arg7, arg8);
 
-    if (unlikely(do_strace)) {
+    if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
         print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
-        ret = do_syscall1(cpu_env, num, arg1, arg2, arg3, arg4,
-                          arg5, arg6, arg7, arg8);
+    }
+
+    ret = do_syscall1(cpu_env, num, arg1, arg2, arg3, arg4,
+                      arg5, arg6, arg7, arg8);
+
+    if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
         print_syscall_ret(num, ret);
-    } else {
-        ret = do_syscall1(cpu_env, num, arg1, arg2, arg3, arg4,
-                          arg5, arg6, arg7, arg8);
     }
 
Quoted 4 lines

linux-user/syscall.c abi_long do_syscall(void *cpu_env, int num, abi_long arg1, · line 12112

Quoted 1 line
linux-user/syscall.c +0 -0
     record_syscall_return(cpu, num, ret);

In term of performance perhaps it sould be better to only test once for the mask as it is done before?

Quoted 5 lines

Modern compilers will generate functionally identical sequences for test once or testing twice (which is to say, they recognize they are the same compare: https://godbolt.org/z/VyrMHf IMO testing twice is nicer to read, so I'm leaving it that way for now unless you object.

If generated code is the same, I have no objection.

Thanks,
Laurent

author
#10 lore raw

PATCH v2 3/4 linux-user: remove gemu_log from the linux-user tree

In reply to Josh Kunz

Now that all uses have been migrated to `qemu_log' it is no longer needed.

---
 linux-user/main.c | 9 ---------
 linux-user/qemu.h | 1 -
 2 files changed, 10 deletions(-)
linux-user/main.c +0 -9
index 8f1d07cdd6..22578b1633 100644
@@ -112,15 +112,6 @@ const char *qemu_uname_release;
    by remapping the process stack directly at the right place */
 unsigned long guest_stack_size = 8 * 1024 * 1024UL;
 
-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)
 {
linux-user/qemu.h +0 -1
index 02c6890c8a..329b409e65 100644
@@ -210,7 +210,6 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
                     abi_long arg2, abi_long arg3, abi_long arg4,
                     abi_long arg5, abi_long arg6, abi_long arg7,
                     abi_long arg8);
-void gemu_log(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
 extern __thread CPUState *thread_cpu;
 void cpu_loop(CPUArchState *env);
 const char *target_strerror(int err);
Signature · 1 line
2.25.0.341.g760bfbb309-goog
Reviewed-by on v2 3/4
#11 lore raw

Re: [PATCH v2 3/4] linux-user: remove gemu_log from the linux-user tree

Le 17/01/2020 à 20:28, Josh Kunz a écrit :

Quoted 35 lines

Now that all uses have been migrated to `qemu_log' it is no longer needed.

---
 linux-user/main.c | 9 ---------
 linux-user/qemu.h | 1 -
 2 files changed, 10 deletions(-)
linux-user/main.c +0 -9
index 8f1d07cdd6..22578b1633 100644
@@ -112,15 +112,6 @@ const char *qemu_uname_release;
    by remapping the process stack directly at the right place */
 unsigned long guest_stack_size = 8 * 1024 * 1024UL;
 
-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)
 {
linux-user/qemu.h +0 -0
index 02c6890c8a..329b409e65 100644

linux-user/qemu.h abi_long do_syscall(void *cpu_env, int num, abi_long arg1, · line 215

Quoted 7 lines
linux-user/qemu.h +0 -1
@@ -210,7 +210,6 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
                     abi_long arg2, abi_long arg3, abi_long arg4,
                     abi_long arg5, abi_long arg6, abi_long arg7,
                     abi_long arg8);
-void gemu_log(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
 extern __thread CPUState *thread_cpu;
 void cpu_loop(CPUArchState *env);
 const char *target_strerror(int err);
author
#12 lore raw

PATCH v2 4/4 bsd-user: Replace gemu_log with qemu_log

In reply to Josh Kunz

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.

---
 bsd-user/main.c    | 13 ++-----------
 bsd-user/qemu.h    |  2 --
 bsd-user/strace.c  | 32 +++++++++++++++-----------------
 bsd-user/syscall.c | 31 +++++++++++++++++++------------
 4 files changed, 36 insertions(+), 42 deletions(-)
bsd-user/main.c +2 -11
index 770c2b267a..2ca975d52c 100644
@@ -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)
 {
@@ -845,7 +836,7 @@ int main(int argc, char **argv)
         } else if (!strcmp(r, "singlestep")) {
             singlestep = 1;
         } else if (!strcmp(r, "strace")) {
-            do_strace = 1;
+            qemu_add_log(LOG_STRACE);
         } else if (!strcmp(r, "trace")) {
             g_free(trace_file);
             trace_file = trace_opt_parse(optarg);
@@ -917,7 +908,7 @@ int main(int argc, char **argv)
     thread_cpu = cpu;
 
     if (getenv("QEMU_STRACE")) {
-        do_strace = 1;
+        qemu_add_log(LOG_STRACE);
     }
 
     target_environ = envlist_to_environ(envlist, NULL);
bsd-user/qemu.h +0 -2
index 09e8aed9c7..5762e3a6e5 100644
@@ -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);
bsd-user/strace.c +15 -17
index fa66fe1ee2..6ee1510555 100644
@@ -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;
bsd-user/syscall.c +19 -12
index 0d45b654bb..53635d183e 100644
@@ -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;
Signature · 1 line
2.25.0.341.g760bfbb309-goog