From patchwork Mon Feb 22 15:59:46 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 62605 Delivered-To: patch@linaro.org Received: by 10.112.43.199 with SMTP id y7csp1324477lbl; Mon, 22 Feb 2016 08:04:14 -0800 (PST) X-Received: by 10.129.38.10 with SMTP id m10mr15781885ywm.183.1456157054882; Mon, 22 Feb 2016 08:04:14 -0800 (PST) Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id m132si11388929ybb.175.2016.02.22.08.04.14 for (version=TLS1 cipher=AES128-SHA bits=128/128); Mon, 22 Feb 2016 08:04:14 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=qemu-devel-bounces+patch=linaro.org@nongnu.org; dkim=fail header.i=@linaro.org Received: from localhost ([::1]:49744 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aXsy6-00078T-BF for patch@linaro.org; Mon, 22 Feb 2016 11:04:14 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40669) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aXsuA-0000zu-Dw for qemu-devel@nongnu.org; Mon, 22 Feb 2016 11:00:11 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aXsu4-0001EX-G3 for qemu-devel@nongnu.org; Mon, 22 Feb 2016 11:00:10 -0500 Received: from mail-wm0-x229.google.com ([2a00:1450:400c:c09::229]:34265) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aXsu4-0001E4-76 for qemu-devel@nongnu.org; Mon, 22 Feb 2016 11:00:04 -0500 Received: by mail-wm0-x229.google.com with SMTP id b205so161912048wmb.1 for ; Mon, 22 Feb 2016 08:00:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-type:content-transfer-encoding; bh=TtnbZgoc1/vMMxuelkntWbKony1f/0e7br4TSJ1EyHY=; b=OZfSqOEPDTyKc+0z9eXwwYukcoXz+6kXe9/e3x5hqOqqnbf8okcWK0DlUpgyVpSCcT sq6uUYJeJy2PWmnqk0c6Tt7PDxXpou+p71uy5DoCtFYe3TnsKLYMcspFmVey2WOt7a6V cj7lq8g7IDJI748C9h/8XUBb192uwrj5yet2E= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-type:content-transfer-encoding; bh=TtnbZgoc1/vMMxuelkntWbKony1f/0e7br4TSJ1EyHY=; b=NJdw0UGE9Rj/6LG/sSyZ6+ahs6DD8bUKAIViW0j2BluN56cvP3TWwkJG83EyJTtIpv fCCKtyujU85p/aYcgSMnyByCFjqXvRsR+BccaZhk+fqifBImWRFUjr27SgkMd5Cazv7m Y/tqrGqQz8kPVUEzlBhvcR+V+/vslsB1v+3RltHGpCExwgg6Dpdz2uwM3SFADnkBttFj V3RYyKF0kPGbqA0w0MbuM/z1Ue1tPqzUmyBLLj17PYCJ0Er9lm6K8CvPwdcvVD/ivaQR wbqV//yUubfrxwyHGAtHfDXOpGy/os+OyxBSbJZmaKLVDToEmdm61wGOQ/OitkiVQlGL a1aQ== X-Gm-Message-State: AG10YOS7RBBhWWCq3F8kLDAJUF0g20pqMJUIanW3a2pVv2+4TUHVjZrZWEG2h9v7IwT0M7A0 X-Received: by 10.194.58.169 with SMTP id s9mr29065518wjq.52.1456156803526; Mon, 22 Feb 2016 08:00:03 -0800 (PST) Received: from zen.linaro.local ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id ks5sm25289986wjb.13.2016.02.22.07.59.55 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 22 Feb 2016 07:59:59 -0800 (PST) Received: from zen.linaroharston (localhost [127.0.0.1]) by zen.linaro.local (Postfix) with ESMTP id 121043E0340; Mon, 22 Feb 2016 15:59:53 +0000 (GMT) From: =?UTF-8?q?Alex=20Benn=C3=A9e?= To: qemu-devel@nongnu.org Date: Mon, 22 Feb 2016 15:59:46 +0000 Message-Id: <1456156787-17509-9-git-send-email-alex.bennee@linaro.org> X-Mailer: git-send-email 2.7.1 In-Reply-To: <1456156787-17509-1-git-send-email-alex.bennee@linaro.org> References: <1456156787-17509-1-git-send-email-alex.bennee@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::229 Cc: dgilbert@redhat.com, crosthwaitepeter@gmail.com, pbonzini@redhat.com, =?UTF-8?q?Alex=20Benn=C3=A9e?= , aurelien@aurel32.net, rth@twiddle.net Subject: [Qemu-devel] [PATCH v7 8/9] qemu-log: support simple pid substitution for logs X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.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 Reviewed-by: Leandro Dorileo Reviewed-by: Aurelien Jarno --- 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(-) -- 2.7.1 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); }