qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Alex Bennée" <alex.bennee@linaro.org>
To: qemu-devel@nongnu.org
Cc: dgilbert@redhat.com, crosthwaitepeter@gmail.com,
	pbonzini@redhat.com, "Alex Bennée" <alex.bennee@linaro.org>,
	aurelien@aurel32.net, rth@twiddle.net
Subject: [Qemu-devel] [PATCH v7 8/9] qemu-log: support simple pid substitution for logs
Date: Mon, 22 Feb 2016 15:59:46 +0000	[thread overview]
Message-ID: <1456156787-17509-9-git-send-email-alex.bennee@linaro.org> (raw)
In-Reply-To: <1456156787-17509-1-git-send-email-alex.bennee@linaro.org>

When debugging stuff that occurs over several forks it would be useful
not to keep overwriting the one logfile you've set-up. This allows a
simple %d to be included once in the logfile parameter which is
substituted with getpid().

As the test cases involve checking user output they need
g_test_trap_subprocess() support. As a result they are currently skipped
on Travis builds due to the older glib involved.

Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
Reviewed-by: Leandro Dorileo <l@dorileo.org>
Reviewed-by: Aurelien Jarno <aurelien@aurel32.net>

---
v5
  - add another r-b
v7
  - simpler error check as suggested by Eric Blake
  - don't g_error, just error_report (so we don't crash from monitor)
  - add some unit tests
---
 tests/test-logging.c | 39 +++++++++++++++++++++++++++++++++++++++
 util/log.c           | 22 ++++++++++++++++++++--
 2 files changed, 59 insertions(+), 2 deletions(-)

diff --git a/tests/test-logging.c b/tests/test-logging.c
index c2dae49..2bcad92 100644
--- a/tests/test-logging.c
+++ b/tests/test-logging.c
@@ -62,14 +62,53 @@ static void test_parse_range(void)
     g_assert(qemu_log_in_addr_range(0x1050));
     g_assert(qemu_log_in_addr_range(0x2050));
     g_assert(qemu_log_in_addr_range(0x3050));
+}
 
+/* As the only real failure from a bad log filename path spec is
+ * reporting to the user we have to use the g_test_trap_subprocess
+ * mechanism and check no errors reported on stderr.
+ */
+#ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
+static void test_parse_path(void)
+{
+    /* All these should work without issue */
+    if (g_test_subprocess()) {
+        qemu_set_log_filename("/tmp/qemu.log");
+        qemu_set_log_filename("/tmp/qemu-%d.log");
+        qemu_set_log_filename("/tmp/qemu.log.%d");
+        return;
+    }
+
+    g_test_trap_subprocess (NULL, 0, 0);
+    g_test_trap_assert_passed();
+    g_test_trap_assert_stdout("");
+    g_test_trap_assert_stderr("");
 }
 
+static void test_parse_invalid_path(void)
+{
+    if (g_test_subprocess()) {
+        qemu_set_log_filename("/tmp/qemu-%d%d.log");
+        return;
+    }
+
+    g_test_trap_subprocess (NULL, 0, 0);
+    g_test_trap_assert_passed();
+    g_test_trap_assert_stdout("");
+    g_test_trap_assert_stderr("Bad logfile format: /tmp/qemu-%d%d.log\n");
+}
+#endif /* CONFIG_HAS_GLIB_SUBPROCESS_TESTS */
+
 int main(int argc, char **argv)
 {
     g_test_init(&argc, &argv, NULL);
 
     g_test_add_func("/logging/parse_range", test_parse_range);
 
+#ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
+    g_test_add_func("/logging/parse_path", test_parse_path);
+    g_test_add_func("/logging/parse_invalid_path", test_parse_invalid_path);
+#endif
+
     return g_test_run();
 }
diff --git a/util/log.c b/util/log.c
index bd4a660..0ab856a 100644
--- a/util/log.c
+++ b/util/log.c
@@ -21,6 +21,7 @@
 #include "qemu-common.h"
 #include "qemu/log.h"
 #include "qemu/range.h"
+#include "qemu/error-report.h"
 #include "trace/control.h"
 
 static char *logfilename;
@@ -77,11 +78,28 @@ void do_qemu_set_log(int log_flags, bool use_own_buffers)
         qemu_log_close();
     }
 }
-
+/*
+ * Allow the user to include %d in their logfile which will be
+ * substituted with the current PID. This is useful for debugging many
+ * nested linux-user tasks but will result in lots of logs.
+ */
 void qemu_set_log_filename(const char *filename)
 {
+    char *pidstr;
     g_free(logfilename);
-    logfilename = g_strdup(filename);
+
+    pidstr = strstr(filename, "%");
+    if (pidstr) {
+        /* We only accept one %d, no other format strings */
+        if (pidstr[1] != 'd' || strchr(pidstr + 2, '%')) {
+            error_report("Bad logfile format: %s", filename);
+            logfilename = NULL;
+        } else {
+            logfilename = g_strdup_printf(filename, getpid());
+        }
+    } else {
+        logfilename = g_strdup(filename);
+    }
     qemu_log_close();
     qemu_set_log(qemu_loglevel);
 }
-- 
2.7.1

  parent reply	other threads:[~2016-02-22 16:00 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-22 15:59 [Qemu-devel] [PATCH v7 0/9] qemu-log, -dfilter and other logging tweaks Alex Bennée
2016-02-22 15:59 ` [Qemu-devel] [PATCH v7 1/9] tcg: pass down TranslationBlock to tcg_code_gen Alex Bennée
2016-02-26 20:19   ` Richard Henderson
2016-02-22 15:59 ` [Qemu-devel] [PATCH v7 2/9] qemu-log: correct help text for -d cpu Alex Bennée
2016-02-22 15:59 ` [Qemu-devel] [PATCH v7 3/9] qemu-log: Avoid function call for disabled qemu_log_mask logging Alex Bennée
2016-02-22 15:59 ` [Qemu-devel] [PATCH v7 4/9] qemu-log: Improve the "exec" TB execution logging Alex Bennée
2016-02-22 15:59 ` [Qemu-devel] [PATCH v7 5/9] qemu-log: new option -dfilter to limit output Alex Bennée
2016-02-26 20:25   ` Richard Henderson
2016-03-03 14:04     ` Alex Bennée
2016-03-03 20:56       ` Richard Henderson
2016-03-03 23:33   ` Peter Maydell
2016-02-22 15:59 ` [Qemu-devel] [PATCH v7 6/9] qemu-log: dfilter-ise exec, out_asm, op and opt_op Alex Bennée
2016-02-22 15:59 ` [Qemu-devel] [PATCH v7 7/9] target-arm: dfilter support for in_asm Alex Bennée
2016-02-26 20:20   ` Richard Henderson
2016-02-22 15:59 ` Alex Bennée [this message]
2016-02-26 20:21   ` [Qemu-devel] [PATCH v7 8/9] qemu-log: support simple pid substitution for logs Richard Henderson
2016-02-22 15:59 ` [Qemu-devel] [PATCH v7 9/9] cputlb: modernise the debug support Alex Bennée
2016-02-26 10:57 ` [Qemu-devel] [PATCH v7 0/9] qemu-log, -dfilter and other logging tweaks Alex Bennée

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1456156787-17509-9-git-send-email-alex.bennee@linaro.org \
    --to=alex.bennee@linaro.org \
    --cc=aurelien@aurel32.net \
    --cc=crosthwaitepeter@gmail.com \
    --cc=dgilbert@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=rth@twiddle.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).