Skip to contentSkip to conversation

migration: Replace gemu_log with qemu_log

PATCH Superseded Josh Kunz 5 participants 21 messages Jan 14, 2020 – Feb 4, 2020

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Josh Kunz

author
#0 lore raw

cover migration: Replace gemu_log with qemu_log

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 two new logging masks to the qemu_log subsystem to support its use for user-mode logging. The LOG_USER mask, for miscellaneous logs, and the LOG_STRACE mask for strace-specific logging. Further, it replaces all existing uses of gemu_log with the appropriate qemu_log_mask(LOG_{USER,STRACE}, ...) based on the log message. To avoid potential backwards incompatibility, this patch series also introduces a new flag: -no-force-user-mode-logging. Since "user mode" logging was always enabled before this change, by-default the LOG_USER mask is always set. The flag is provided for users that want to disable LOG_USER. Hopefully, this flag can be removed, or turned into a no-op in the futre, and -d/QEMU_LOG can be used directly to enable user-mode logging if desired.

Unfortunately, this change introduces one small backwards incompatibility for the linux-user tree: If an explicit -D option is provided, or the QEMU_LOG_FILENAME_ environment variable is set, strace and user-mode logging statements will log to the QEMU log file instead of 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
* -no-force-user-mode-logging uses QEMU_LOG + user_mode or strace can be set in QEMU_LOG to trigger the appropriate logging.

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        |  15 ++
 linux-user/arm/cpu_loop.c |   5 +-
 linux-user/fd-trans.c     |  55 +++--
 linux-user/main.c         |  50 +++-
 linux-user/qemu.h         |   2 -
 linux-user/signal.c       |   3 +-
 linux-user/strace.c       | 479 +++++++++++++++++++-------------------
 linux-user/syscall.c      |  43 ++--
 linux-user/vm86.c         |   3 +-
 util/log.c                |   5 +
 14 files changed, 404 insertions(+), 334 deletions(-)
Signature · 1 line
2.25.0.rc1.283.g88dfdc4193-goog
author
#1 lore raw

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

This change introduces a new logging mask "LOG_USER", which is used for masking general user-mode logging. This change also switches all non-strace uses of gemu_log' in linux-user/ to use qemu_log_mask(LOG_USER, ...)' instead. This allows the user to easily log to a file, and to mask out these log messages if they desire.

---
 include/qemu/log.h        |  2 ++
 linux-user/arm/cpu_loop.c |  5 ++--
 linux-user/fd-trans.c     | 55 +++++++++++++++++++++++++--------------
 linux-user/main.c         | 24 +++++++++++++++++
 linux-user/syscall.c      | 30 ++++++++++++---------
 linux-user/vm86.c         |  3 ++-
 util/log.c                |  3 +++
 7 files changed, 86 insertions(+), 36 deletions(-)
include/qemu/log.h +2 -0
index e0f4e40628..503e4f88d5 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_USER is used for some informational user-mode logging. */
+#define LOG_USER           (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/arm/cpu_loop.c +3 -2
index 1fae90c6df..830ba8f138 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_USER,
+                                          "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/main.c +24 -0
index 8718d03ee2..c4f3de77db 100644
@@ -60,6 +60,9 @@ unsigned long mmap_min_addr;
 unsigned long guest_base;
 int have_guest_base;
 
+/* Used to implement backwards-compatibility for user-mode logging. */
+static bool force_user_mode_logging = true;
+
 /*
  * 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.
@@ -399,6 +402,11 @@ static void handle_arg_abi_call0(const char *arg)
 }
 #endif
 
+static void handle_arg_no_force_user_mode_logging(const char *arg)
+{
+    force_user_mode_logging = false;
+}
+
 static QemuPluginList plugins = QTAILQ_HEAD_INITIALIZER(plugins);
 
 #ifdef CONFIG_PLUGIN
@@ -469,6 +477,10 @@ static const struct qemu_argument arg_table[] = {
     {"xtensa-abi-call0", "QEMU_XTENSA_ABI_CALL0", false, handle_arg_abi_call0,
      "",           "assume CALL0 Xtensa ABI"},
 #endif
+    {"no-force-user-mode-logging", "", false,
+      handle_arg_no_force_user_mode_logging,
+      "",          "disable forced user-mode logging, other logging options "
+                   "will be used exactly as provided" },
     {NULL, NULL, false, NULL, NULL, NULL}
 };
 
@@ -661,6 +673,18 @@ int main(int argc, char **argv, char **envp)
 
     optind = parse_args(argc, argv);
 
+    if (force_user_mode_logging) {
+        /*
+         * Backwards Compatibility: gemu_log for non-strace messages was not
+         * configurable, and was always on. Unless the user explicitly disables
+         * forced LOG_USER, force LOG_USER into the mask.
+         */
+        qemu_add_log(LOG_USER);
+    }
+
+    qemu_log_mask(LOG_USER, "--> from user\n");
+    qemu_log_mask(LOG_STRACE, "--> from strace\n");
+
     if (!trace_init_backends()) {
         exit(1);
     }
linux-user/syscall.c +17 -13
index 171c0caef3..7e23dd6327 100644
@@ -1555,7 +1555,7 @@ static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
              * something more intelligent than "twice the size of the
              * target buffer we're reading from".
              */
-            gemu_log("Host cmsg overflow\n");
+            qemu_log_mask(LOG_USER, "Host cmsg overflow\n");
             break;
         }
 
@@ -1585,8 +1585,8 @@ static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
             __get_user(cred->uid, &target_cred->uid);
             __get_user(cred->gid, &target_cred->gid);
         } else {
-            gemu_log("Unsupported ancillary data: %d/%d\n",
-                                        cmsg->cmsg_level, cmsg->cmsg_type);
+            qemu_log_mask(LOG_USER, "Unsupported ancillary data: %d/%d\n",
+                          cmsg->cmsg_level, cmsg->cmsg_type);
             memcpy(data, target_data, len);
         }
 
@@ -1807,8 +1807,8 @@ static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
 
         default:
         unimplemented:
-            gemu_log("Unsupported ancillary data: %d/%d\n",
-                                        cmsg->cmsg_level, cmsg->cmsg_type);
+            qemu_log_mask(LOG_USER, "Unsupported ancillary data: %d/%d\n",
+                          cmsg->cmsg_level, cmsg->cmsg_type);
             memcpy(target_data, data, MIN(len, tgt_len));
             if (tgt_len > len) {
                 memset(target_data + len, 0, tgt_len - len);
@@ -2283,7 +2283,8 @@ set_timeout:
 #endif /* SOL_NETLINK */
     default:
     unimplemented:
-        gemu_log("Unsupported setsockopt level=%d optname=%d\n", level, optname);
+        qemu_log_mask(LOG_USER, "Unsupported setsockopt level=%d optname=%d\n",
+                      level, optname);
         ret = -TARGET_ENOPROTOOPT;
     }
     return ret;
@@ -2636,8 +2637,9 @@ static abi_long do_getsockopt(int sockfd, int level, int optname,
 #endif /* SOL_NETLINK */
     default:
     unimplemented:
-        gemu_log("getsockopt level=%d optname=%d not yet supported\n",
-                 level, optname);
+        qemu_log_mask(LOG_USER,
+                      "getsockopt level=%d optname=%d not yet supported\n",
+                      level, optname);
         ret = -TARGET_EOPNOTSUPP;
         break;
     }
@@ -3392,7 +3394,7 @@ static abi_long do_socketcall(int num, abi_ulong vptr)
     case TARGET_SYS_SENDMMSG: /* sockfd, msgvec, vlen, flags */
         return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 1);
     default:
-        gemu_log("Unsupported socketcall: %d\n", num);
+        qemu_log_mask(LOG_USER, "Unsupported socketcall: %d\n", num);
         return -TARGET_EINVAL;
     }
 }
@@ -4303,7 +4305,8 @@ static abi_long do_ipc(CPUArchState *cpu_env,
         ret = do_shmctl(first, second, ptr);
         break;
     default:
-	gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
+        qemu_log_mask(LOG_USER, "Unsupported ipc call: %d (version %d)\n",
+                      call, version);
 	ret = -TARGET_ENOSYS;
 	break;
     }
@@ -5151,7 +5154,8 @@ static abi_long do_ioctl(int fd, int cmd, abi_long arg)
     ie = ioctl_entries;
     for(;;) {
         if (ie->target_cmd == 0) {
-            gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
+            qemu_log_mask(
+                LOG_USER, "Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
             return -TARGET_ENOSYS;
         }
         if (ie->target_cmd == cmd)
@@ -5217,8 +5221,8 @@ static abi_long do_ioctl(int fd, int cmd, abi_long arg)
         }
         break;
     default:
-        gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
-                 (long)cmd, arg_type[0]);
+        qemu_log_mask(LOG_USER, "Unsupported ioctl type: cmd=0x%04lx type=%d\n",
+                      (long)cmd, arg_type[0]);
         ret = -TARGET_ENOSYS;
         break;
     }
linux-user/vm86.c +2 -1
index 2fa7a89edc..0013fd8368 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_USER, "qemu: unsupported vm86 subfunction (%ld)\n",
+                      subfunction);
         ret = -TARGET_EINVAL;
         goto out;
     case TARGET_VM86_PLUS_INSTALL_CHECK:
util/log.c +3 -0
index 867264da8d..b14d17fafb 100644
@@ -328,6 +328,9 @@ const QEMULogItem qemu_log_items[] = {
 #ifdef CONFIG_PLUGIN
     { CPU_LOG_PLUGIN, "plugin", "output from TCG plugins\n"},
 #endif
+    { LOG_USER, "user_mode",
+      "log when QEMU encounters errors in the user-mode emulation layer.\n"
+      "For example, when a socket option or syscall is not implemented" },
     { 0, NULL, NULL },
 };
 
Signature · 1 line
2.25.0.rc1.283.g88dfdc4193-goog
#2 lore raw

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

Le 14/01/2020 à 04:01, Josh Kunz a écrit :

Quoted 7 lines

This change introduces a new logging mask "LOG_USER", which is used for masking general user-mode logging. This change also switches all non-strace uses of gemu_log' in linux-user/ to use qemu_log_mask(LOG_USER, ...)' instead. This allows the user to easily log to a file, and to mask out these log messages if they desire.

Quoted 9 lines
---
 include/qemu/log.h        |  2 ++
 linux-user/arm/cpu_loop.c |  5 ++--
 linux-user/fd-trans.c     | 55 +++++++++++++++++++++++++--------------
 linux-user/main.c         | 24 +++++++++++++++++
 linux-user/syscall.c      | 30 ++++++++++++---------
 linux-user/vm86.c         |  3 ++-
 util/log.c                |  3 +++
 7 files changed, 86 insertions(+), 36 deletions(-)

I think most of these log messages should use LOG_UNIMP, so no need for a new flag.

Thanks,
Laurent

#3 lore raw

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

In reply to Josh Kunz

Le 14/01/2020 à 04:01, Josh Kunz a écrit :

Quoted 7 lines

This change introduces a new logging mask "LOG_USER", which is used for masking general user-mode logging. This change also switches all non-strace uses of gemu_log' in linux-user/ to use qemu_log_mask(LOG_USER, ...)' instead. This allows the user to easily log to a file, and to mask out these log messages if they desire.

Quoted 10 lines
---
 include/qemu/log.h        |  2 ++
 linux-user/arm/cpu_loop.c |  5 ++--
 linux-user/fd-trans.c     | 55 +++++++++++++++++++++++++--------------
 linux-user/main.c         | 24 +++++++++++++++++
 linux-user/syscall.c      | 30 ++++++++++++---------
 linux-user/vm86.c         |  3 ++-
 util/log.c                |  3 +++
 7 files changed, 86 insertions(+), 36 deletions(-)

I think most of these log messages should use LOG_UNIMP, so no need for a new flag.

Thanks,
Laurent

#4 lore raw

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

In reply to Josh Kunz
Quoted 22 lines · Josh Kunz

This change introduces a new logging mask "LOG_USER", which is used for masking general user-mode logging. This change also switches all non-strace uses of gemu_log' in linux-user/ to use qemu_log_mask(LOG_USER, ...)' instead. This allows the user to easily log to a file, and to mask out these log messages if they desire.

---
 include/qemu/log.h        |  2 ++
 linux-user/arm/cpu_loop.c |  5 ++--
 linux-user/fd-trans.c     | 55 +++++++++++++++++++++++++--------------
 linux-user/main.c         | 24 +++++++++++++++++
 linux-user/syscall.c      | 30 ++++++++++++---------
 linux-user/vm86.c         |  3 ++-
 util/log.c                |  3 +++
 7 files changed, 86 insertions(+), 36 deletions(-)
include/qemu/log.h +0 -0
index e0f4e40628..503e4f88d5 100644

include/qemu/log.h static inline bool qemu_log_separate(void) · line 66

Quoted 5 lines
include/qemu/log.h +2 -0
@@ -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_USER is used for some informational user-mode logging. */
+#define LOG_USER           (1 << 19)

As Laurent said I think LOG_UNIMP is perfectly fine for stuff we haven't done. I don't think any of the cases warrant LOG_GUEST_ERROR.

linux-user/main.c unsigned long mmap_min_addr; · line 65

Quoted 12 lines
linux-user/main.c +3 -0
index 8718d03ee2..c4f3de77db 100644
@@ -60,6 +60,9 @@ unsigned long mmap_min_addr;
 unsigned long guest_base;
 int have_guest_base;
 
+/* Used to implement backwards-compatibility for user-mode logging. */
+static bool force_user_mode_logging = true;
+

I'm not sure want to bother with this. I know we like to avoid regression but isn't this all debug log stuff? If we must keep it can we invert the variable to save the initialisation.

Quoted 32 lines
linux-user/main.c +11 -0
 /*
  * 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.
@@ -399,6 +402,11 @@ static void handle_arg_abi_call0(const char *arg)
 }
 #endif
 
+static void handle_arg_no_force_user_mode_logging(const char *arg)
+{
+    force_user_mode_logging = false;
+}
+
 static QemuPluginList plugins = QTAILQ_HEAD_INITIALIZER(plugins);
 
 #ifdef CONFIG_PLUGIN
@@ -469,6 +477,10 @@ static const struct qemu_argument arg_table[] = {
     {"xtensa-abi-call0", "QEMU_XTENSA_ABI_CALL0", false, handle_arg_abi_call0,
      "",           "assume CALL0 Xtensa ABI"},
 #endif
+    {"no-force-user-mode-logging", "", false,
+      handle_arg_no_force_user_mode_logging,
+      "",          "disable forced user-mode logging, other logging options "
+                   "will be used exactly as provided" },
     {NULL, NULL, false, NULL, NULL, NULL}
 };
 
@@ -661,6 +673,18 @@ int main(int argc, char **argv, char **envp)
 
     optind = parse_args(argc, argv);
 
+    if (force_user_mode_logging) {
+        /*

linux-user/main.c int main(int argc, char **argv, char **envp) · line 687

Quoted 10 lines
linux-user/main.c +10 -0
+         * Backwards Compatibility: gemu_log for non-strace messages was not
+         * configurable, and was always on. Unless the user explicitly disables
+         * forced LOG_USER, force LOG_USER into the mask.
+         */
+        qemu_add_log(LOG_USER);
+    }
+
+    qemu_log_mask(LOG_USER, "--> from user\n");
+    qemu_log_mask(LOG_STRACE, "--> from strace\n");
+

I mean we jumped through hoops to maintain backwards compatibility and then added new output? Also LOG_STRACE doesn't exist yet.

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

Quoted 13 lines
linux-user/main.c +0 -0
     if (!trace_init_backends()) {
         exit(1);
     }
linux-user/syscall.c +1 -1
index 171c0caef3..7e23dd6327 100644
@@ -1555,7 +1555,7 @@ static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
              * something more intelligent than "twice the size of the
              * target buffer we're reading from".
              */
-            gemu_log("Host cmsg overflow\n");
+            qemu_log_mask(LOG_USER, "Host cmsg overflow\n");

I'm not sure we shouldn't just be asserting this case above. The comments imply it is a bug on our part. The rest look like good cases for LOG_UNIMP.

Signature · 1 line
Alex Bennée
author
#5 lore raw

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

Quoted 3 lines · Alex Bennée

As Laurent said I think LOG_UNIMP is perfectly fine for stuff we haven't done. I don't think any of the cases warrant LOG_GUEST_ERROR.

I've replaced LOG_USER with LOG_UNIMP. Didn't catch that mask at first, LOG_UNIMP is indeed a better fit for most of these.

Quoted 3 lines

I'm not sure want to bother with this. I know we like to avoid regression but isn't this all debug log stuff? If we must keep it can we invert the variable to save the initialisation.

I'm all for removing it. I agree that it would be pretty odd for someone to depend on debug log output in this way. I've removed this in v2. It is a backwards incompatibility though, so I'm happy to re-add the fix if people want it.

Quoted 2 lines

I mean we jumped through hoops to maintain backwards compatibility and then added new output? Also LOG_STRACE doesn't exist yet.

This is leftover debug statements. I've removed it.

Quoted 3 lines

I'm not sure we shouldn't just be asserting this case above. The comments imply it is a bug on our part. The rest look like good cases for LOG_UNIMP.

Switched to an assert in v2.

author
#6 lore raw

PATCH 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).

---
 include/qemu/log.h   |  13 ++
 linux-user/main.c    |  17 +-
 linux-user/qemu.h    |   1 -
 linux-user/signal.c  |   3 +-
 linux-user/strace.c  | 479 ++++++++++++++++++++++---------------------
 linux-user/syscall.c |  13 +-
 util/log.c           |   2 +
 7 files changed, 282 insertions(+), 246 deletions(-)
include/qemu/log.h +13 -0
index 503e4f88d5..8f044c1716 100644
@@ -64,6 +64,7 @@ static inline bool qemu_log_separate(void)
 #define CPU_LOG_PLUGIN     (1 << 18)
 /* LOG_USER is used for some informational user-mode logging. */
 #define LOG_USER           (1 << 19)
+#define LOG_STRACE         (1 << 20)
 
 /* 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
@@ -154,6 +155,18 @@ void qemu_set_dfilter_ranges(const char *ranges, Error **errp);
 bool qemu_log_in_addr_range(uint64_t addr);
 int qemu_str_to_log_mask(const char *str);
 
+/* Add (union) the given "log_flags" to the current log mask. */
+static inline void qemu_add_log(int log_flags)
+{
+    qemu_set_log(qemu_loglevel | log_flags);
+}
+
+/* Remove (subtract) the given log flags from the current log mask. */
+static inline void qemu_del_log(int log_flags)
+{
+    qemu_set_log(qemu_loglevel & ~(log_flags));
+}
+
 /* Print a usage message listing all the valid logging categories
  * to the specified FILE*.
  */
linux-user/main.c +16 -1
index c4f3de77db..0bf40c4d27 100644
@@ -63,6 +63,12 @@ int have_guest_base;
 /* Used to implement backwards-compatibility for user-mode logging. */
 static bool force_user_mode_logging = true;
 
+/*
+ * Used to implement backwards-compatibility for the `-strace`, and
+ * QEMU_STRACE options.
+ */
+static bool enable_strace;
+
 /*
  * 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.
@@ -378,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)
@@ -672,6 +678,15 @@ int main(int argc, char **argv, char **envp)
     qemu_plugin_add_opts();
 
     optind = parse_args(argc, argv);
+    /*
+     * Backwards Compatability: If handle_arg_strace just enabled strace
+     * logging directly, then it could be accidentally turned off by a
+     * QEMU_LOG/-d option. To make sure that strace logging is always enabled
+     * when QEMU_STRACE/-strace is set, re-enable LOG_STRACE here.
+     */
+    if (enable_strace) {
+        qemu_add_log(LOG_STRACE);
+    }
 
     if (force_user_mode_logging) {
         /*
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 -2
index 5ca6d62b15..2ff0065804 100644
@@ -864,9 +864,8 @@ 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);
-    }
 
     if (handler == TARGET_SIG_DFL) {
         /* default handler : ignore some signal. The other are job control or fatal */
linux-user/strace.c +243 -236

1,259 lines not on this page · raw message

linux-user/syscall.c +7 -6
index 7e23dd6327..aa55040b42 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 b14d17fafb..b8ca50165d 100644
@@ -331,6 +331,8 @@ const QEMULogItem qemu_log_items[] = {
     { LOG_USER, "user_mode",
       "log when QEMU encounters errors in the user-mode emulation layer.\n"
       "For example, when a socket option or syscall is not implemented" },
+    { LOG_STRACE, "strace",
+      "log every user-mode syscall, its input, and its result" },
     { 0, NULL, NULL },
 };
 
Signature · 1 line
2.25.0.rc1.283.g88dfdc4193-goog
#7 lore raw

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

Le 14/01/2020 à 04:01, Josh Kunz a écrit :

Quoted 22 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).

---
 include/qemu/log.h   |  13 ++
 linux-user/main.c    |  17 +-
 linux-user/qemu.h    |   1 -
 linux-user/signal.c  |   3 +-
 linux-user/strace.c  | 479 ++++++++++++++++++++++---------------------
 linux-user/syscall.c |  13 +-
 util/log.c           |   2 +
 7 files changed, 282 insertions(+), 246 deletions(-)
include/qemu/log.h +0 -0
index 503e4f88d5..8f044c1716 100644

include/qemu/log.h static inline bool qemu_log_separate(void) · line 67

Quoted 4 lines
include/qemu/log.h +1 -0
@@ -64,6 +64,7 @@ static inline bool qemu_log_separate(void)
 #define CPU_LOG_PLUGIN     (1 << 18)
 /* LOG_USER is used for some informational user-mode logging. */
 #define LOG_USER           (1 << 19)
+#define LOG_STRACE         (1 << 20)

Perhaps we can use flag LOG_TRACE? (cc Paolo)

include/qemu/log.h void qemu_set_dfilter_ranges(const char *ranges, Error **errp); · line 162

Quoted 12 lines
include/qemu/log.h +5 -0
 /* 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
@@ -154,6 +155,18 @@ void qemu_set_dfilter_ranges(const char *ranges, Error **errp);
 bool qemu_log_in_addr_range(uint64_t addr);
 int qemu_str_to_log_mask(const char *str);
 
+/* Add (union) the given "log_flags" to the current log mask. */
+static inline void qemu_add_log(int log_flags)
+{
+    qemu_set_log(qemu_loglevel | log_flags);
+}

This is really a special case as the flags are all given at the same time, could you use directly qemu_set_log() in main()?

include/qemu/log.h void qemu_set_dfilter_ranges(const char *ranges, Error **errp); · line 168

Quoted 5 lines
include/qemu/log.h +5 -0
+/* Remove (subtract) the given log flags from the current log mask. */
+static inline void qemu_del_log(int log_flags)
+{
+    qemu_set_log(qemu_loglevel & ~(log_flags));
+}

Unused and unneeded.

Except that, the patch seems good.

Thanks,
Laurent

#8 lore raw

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

Quoted 1 line · Laurent Vivier

Perhaps we can use flag LOG_TRACE? (cc Paolo)

No, LOG_TRACE is for trace-events tracepoints. A new logging flag is definitely the way to go.

Paolo

author
#9 lore raw

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

In reply to Laurent Vivier
Quoted 2 lines · Laurent Vivier

This is really a special case as the flags are all given at the same time, could you use directly qemu_set_log() in main()?

Fixed in v2.

Quoted 7 lines

include/qemu/log.h void qemu_set_dfilter_ranges(const char *ranges, Error **errp); · line 168

Quoted 5 lines
include/qemu/log.h +5 -0
+/* Remove (subtract) the given log flags from the current log mask. */
+static inline void qemu_del_log(int log_flags)
+{
+    qemu_set_log(qemu_loglevel & ~(log_flags));
+}

Unused and unneeded.

Removed in v2. Also removed qemu_add_log.

#10 lore raw

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

In reply to Josh Kunz
Quoted 66 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).

---
 include/qemu/log.h   |  13 ++
 linux-user/main.c    |  17 +-
 linux-user/qemu.h    |   1 -
 linux-user/signal.c  |   3 +-
 linux-user/strace.c  | 479 ++++++++++++++++++++++---------------------
 linux-user/syscall.c |  13 +-
 util/log.c           |   2 +
 7 files changed, 282 insertions(+), 246 deletions(-)
include/qemu/log.h +13 -0
index 503e4f88d5..8f044c1716 100644
@@ -64,6 +64,7 @@ static inline bool qemu_log_separate(void)
 #define CPU_LOG_PLUGIN     (1 << 18)
 /* LOG_USER is used for some informational user-mode logging. */
 #define LOG_USER           (1 << 19)
+#define LOG_STRACE         (1 << 20)
 
 /* 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
@@ -154,6 +155,18 @@ void qemu_set_dfilter_ranges(const char *ranges, Error **errp);
 bool qemu_log_in_addr_range(uint64_t addr);
 int qemu_str_to_log_mask(const char *str);
 
+/* Add (union) the given "log_flags" to the current log mask. */
+static inline void qemu_add_log(int log_flags)
+{
+    qemu_set_log(qemu_loglevel | log_flags);
+}
+
+/* Remove (subtract) the given log flags from the current log mask. */
+static inline void qemu_del_log(int log_flags)
+{
+    qemu_set_log(qemu_loglevel & ~(log_flags));
+}
+
 /* Print a usage message listing all the valid logging categories
  * to the specified FILE*.
  */
linux-user/main.c +6 -0
index c4f3de77db..0bf40c4d27 100644
@@ -63,6 +63,12 @@ int have_guest_base;
 /* Used to implement backwards-compatibility for user-mode logging. */
 static bool force_user_mode_logging = true;
 
+/*
+ * Used to implement backwards-compatibility for the `-strace`, and
+ * QEMU_STRACE options.
+ */
+static bool enable_strace;
+
 /*
  * 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.

linux-user/main.c static void handle_arg_singlestep(const char *arg) · line 387

Quoted 5 lines
linux-user/main.c +1 -1
@@ -378,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;

Could we cut out the middle-man and just qemu_add_log(LOG_STRACE) here and drop the enable_strace variable.

Quoted 36 lines
linux-user/main.c +9 -0
 }
 
 static void handle_arg_version(const char *arg)
@@ -672,6 +678,15 @@ int main(int argc, char **argv, char **envp)
     qemu_plugin_add_opts();
 
     optind = parse_args(argc, argv);
+    /*
+     * Backwards Compatability: If handle_arg_strace just enabled strace
+     * logging directly, then it could be accidentally turned off by a
+     * QEMU_LOG/-d option. To make sure that strace logging is always enabled
+     * when QEMU_STRACE/-strace is set, re-enable LOG_STRACE here.
+     */
+    if (enable_strace) {
+        qemu_add_log(LOG_STRACE);
+    }
 
     if (force_user_mode_logging) {
         /*
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 +0 -0
index 5ca6d62b15..2ff0065804 100644

linux-user/signal.c static void handle_pending_signal(CPUArchState *cpu_env, int sig, · line 869

Quoted 7 lines
linux-user/signal.c +1 -2
@@ -864,9 +864,8 @@ 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);
-    }

Please don't drop the brace - c.f. CODING_STYLE.rst

Side note, print_taken_signal might want to consider using the qemu_log_lock() functionality to keep sequential qemu_log's together. I suspect you might want to do the same for syscalls.

--
Alex Bennée

author
#11 lore raw

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

Quoted 2 lines · Alex Bennée

Could we cut out the middle-man and just qemu_add_log(LOG_STRACE) here and drop the enable_strace variable.

This code has been re-factored based on Laurent's comments. In v2 enable_strace is still needed to avoid QEMU_LOG/-d overwriting the mask set by -strace.

Quoted 1 line

Please don't drop the brace - c.f. CODING_STYLE.rst

Thanks for the catch!

Quoted 3 lines

Side note, print_taken_signal might want to consider using the qemu_log_lock() functionality to keep sequential qemu_log's together. I suspect you might want to do the same for syscalls.

This is likely true, but outside the scope of this patch (this patch preserves the current bad behavior). When I get some spare time I'll try and address this in a follow up patch series.

author
#12 lore raw

PATCH 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 0bf40c4d27..945b6adf3a 100644
@@ -108,15 +108,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.rc1.283.g88dfdc4193-goog
Reviewed-by on v1 3/4
#13 lore raw

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

Quoted 4 lines · Josh Kunz

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

Quoted 37 lines
---
 linux-user/main.c | 9 ---------
 linux-user/qemu.h | 1 -
 2 files changed, 10 deletions(-)
linux-user/main.c +0 -9
index 0bf40c4d27..945b6adf3a 100644
@@ -108,15 +108,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
Alex Bennée
author
#14 lore raw

PATCH 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 7f4e3cd627..e726b0a7fb 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.rc1.283.g88dfdc4193-goog
#15 lore raw
In reply to Josh Kunz
Quoted 41 lines · Josh Kunz

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 two new logging masks to the qemu_log subsystem to support its use for user-mode logging. The LOG_USER mask, for miscellaneous logs, and the LOG_STRACE mask for strace-specific logging. Further, it replaces all existing uses of gemu_log with the appropriate qemu_log_mask(LOG_{USER,STRACE}, ...) based on the log message. To avoid potential backwards incompatibility, this patch series also introduces a new flag: -no-force-user-mode-logging. Since "user mode" logging was always enabled before this change, by-default the LOG_USER mask is always set. The flag is provided for users that want to disable LOG_USER. Hopefully, this flag can be removed, or turned into a no-op in the futre, and -d/QEMU_LOG can be used directly to enable user-mode logging if desired.

Unfortunately, this change introduces one small backwards incompatibility for the linux-user tree: If an explicit -D option is provided, or the QEMU_LOG_FILENAME_ environment variable is set, strace and user-mode logging statements will log to the QEMU log file instead of 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
* -no-force-user-mode-logging uses QEMU_LOG + user_mode or strace can be set in QEMU_LOG to trigger the appropriate logging.

Quoted 3 lines

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

The bsd-user that is in tree doesn't work. I've been trying to catch up to qemu head of tree, but I'm only up to 3.2... chances are the bsd-user changes will not change the state of things...

Warner

Josh Kunz (4):

Quoted 26 lines

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        |  15 ++
 linux-user/arm/cpu_loop.c |   5 +-
 linux-user/fd-trans.c     |  55 +++--
 linux-user/main.c         |  50 +++-
 linux-user/qemu.h         |   2 -
 linux-user/signal.c       |   3 +-
 linux-user/strace.c       | 479 +++++++++++++++++++-------------------
 linux-user/syscall.c      |  43 ++--
 linux-user/vm86.c         |   3 +-
 util/log.c                |   5 +
 14 files changed, 404 insertions(+), 334 deletions(-)

--
2.25.0.rc1.283.g88dfdc4193-goog

author
#16 lore raw
Quoted 4 lines · Warner Losh

The bsd-user that is in tree doesn't work. I've been trying to catch up to qemu head of tree, but I'm only up to 3.2... chances are the bsd-user changes will not change the state of things...

Ah good to know. Would you all prefer I don't modify it at all, to try and keep the code "pristine"? Or is it OK to keep the patch as is?

#17 lore raw
In reply to Josh Kunz

Josh Kunz <[email protected]> writes:

<snip>

Quoted 4 lines

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

If you have the time we have vm-build-netbsd:

make vm-build-netbsd EXTRA_CONFIGURE_OPTS="--disable-system"

Which will create a NetBSD image for you and run the build through it. Other images are available ;-)

Signature · 1 line
Alex Bennée
author
#18 lore raw
Quoted 9 lines · Alex Bennée

Josh Kunz <[email protected]> writes:

<snip>

Quoted 4 lines

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

Quoted 7 lines

If you have the time we have vm-build-netbsd:

make vm-build-netbsd EXTRA_CONFIGURE_OPTS="--disable-system"

Which will create a NetBSD image for you and run the build through it. Other images are available ;-)

This works, but it looks like it only runs the tests on BSD, even with configure --enable-bsd-user first. I don't see the bsd-user binaries being produced in the output of this command.

#19 lore raw
Quoted 15 lines · Josh Kunz
Quoted 13 lines · Alex Bennée

Josh Kunz <[email protected]> writes:

<snip>

Quoted 4 lines

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

If you have the time we have vm-build-netbsd:

make vm-build-netbsd EXTRA_CONFIGURE_OPTS="--disable-system"

Quoted 7 lines
Quoted 3 lines

Which will create a NetBSD image for you and run the build through it. Other images are available ;-)

This works, but it looks like it only runs the tests on BSD, even with configure --enable-bsd-user first. I don't see the bsd-user binaries being produced in the output of this command.

Ahh the default build target for the BSDs is "check" but as bsd-user doesn't have any checks it doesn't end up building. You can force it with

make vm-build-netbsd EXTRA_CONFIGURE_OPTS="--disable-system" BUILD_TARGET="all"

It would be worth plumbing in the tests/tcg tests at some point. I suspect most of the user-mode tests are more POSIX than Linux.

Signature · 1 line
Alex Bennée
author
#20 lore raw
Quoted 8 lines · Alex Bennée

Ahh the default build target for the BSDs is "check" but as bsd-user doesn't have any checks it doesn't end up building. You can force it with

make vm-build-netbsd EXTRA_CONFIGURE_OPTS="--disable-system" BUILD_TARGET="all"

It would be worth plumbing in the tests/tcg tests at some point. I suspect most of the user-mode tests are more POSIX than Linux.

Neat, thanks Alex! I've run this and verified the BSD user-mode binaries built.