From patchwork Tue Mar 15 14:30:23 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: 63873 Delivered-To: patch@linaro.org Received: by 10.112.199.169 with SMTP id jl9csp702699lbc; Tue, 15 Mar 2016 07:32:05 -0700 (PDT) X-Received: by 10.140.97.9 with SMTP id l9mr38172358qge.106.1458052325383; Tue, 15 Mar 2016 07:32:05 -0700 (PDT) Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id w107si26696685qge.8.2016.03.15.07.32.05 for (version=TLS1 cipher=AES128-SHA bits=128/128); Tue, 15 Mar 2016 07:32:05 -0700 (PDT) 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]:48593 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1afq0y-0002Sj-RW for patch@linaro.org; Tue, 15 Mar 2016 10:32:04 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48358) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1afpzi-00018s-FX for qemu-devel@nongnu.org; Tue, 15 Mar 2016 10:30:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1afpzc-0005NB-B9 for qemu-devel@nongnu.org; Tue, 15 Mar 2016 10:30:46 -0400 Received: from mail-wm0-x22a.google.com ([2a00:1450:400c:c09::22a]:36670) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1afpzc-0005N1-0n for qemu-devel@nongnu.org; Tue, 15 Mar 2016 10:30:40 -0400 Received: by mail-wm0-x22a.google.com with SMTP id l124so13306168wmf.1 for ; Tue, 15 Mar 2016 07:30:39 -0700 (PDT) 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-transfer-encoding; bh=wslRV4xd8m9HAG55H+aZMLWRqDJxEUJnD4lT4COFkr0=; b=Nu2BfNrcleCqCqEP//QSWBmCKRupfbYwto56DYKSJbQGVFACxG9FPT+6sTdj9a8ddi Q1P2pGnN7AhPvU052z5wTxGQdDEv4L6ZK6IyHoq3RKw0c8d6IwQPKathBobCNuFpApPW D/nCekRvnkle6CQYb4WkpNemQuA5NiRN4l2ig= 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-transfer-encoding; bh=wslRV4xd8m9HAG55H+aZMLWRqDJxEUJnD4lT4COFkr0=; b=W+WmddN0pJsR6EEM5T86IWcaeX4B2UFMrbC+snPnxWiz1rqR3wQCktXqNSnJiP92pv elwkL79t1mGmWcXC5FNya8rE/CYW7INMJ7pIs0jPsJP6QAJig1dT5C4854UlplEwGLVZ 1gVzsq6hajdrSTiLWHKOFE/nDtuVdFN78boYpX2uV9EyyeBubFD4IBx2bkZ/a3lu67Pz daUTdjP3HPE/7TpNCMColHkPJ3mY3jueeyFPLC1jAGa0pOpIoxFHBXCm6wukuCzzb7kI 3lqUgU/XbembaEAIOPLwZj0PQbyqM4PobIsmaf2XLaRHpdCun8xkvpFjDyHrxEr+iwBO yhNw== X-Gm-Message-State: AD7BkJJ3gr8nMtrU8BvmClNtJYHjLoYD76/BJU4NK7DAKCKqkAwR+p/IBthZMQQ9L3L9rFLE X-Received: by 10.28.173.71 with SMTP id w68mr25426916wme.88.1458052239483; Tue, 15 Mar 2016 07:30:39 -0700 (PDT) Received: from zen.linaro.local ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id ei9sm27384467wjd.40.2016.03.15.07.30.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 15 Mar 2016 07:30:37 -0700 (PDT) Received: from zen.linaroharston (localhost [127.0.0.1]) by zen.linaro.local (Postfix) with ESMTP id 195C13E04D4; Tue, 15 Mar 2016 14:30:28 +0000 (GMT) From: =?UTF-8?q?Alex=20Benn=C3=A9e?= To: qemu-devel@nongnu.org Date: Tue, 15 Mar 2016 14:30:23 +0000 Message-Id: <1458052224-9316-10-git-send-email-alex.bennee@linaro.org> X-Mailer: git-send-email 2.7.2 In-Reply-To: <1458052224-9316-1-git-send-email-alex.bennee@linaro.org> References: <1458052224-9316-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::22a Cc: pbonzini@redhat.com, crosthwaitepeter@gmail.com, =?UTF-8?q?Alex=20Benn=C3=A9e?= , aurelien@aurel32.net, rth@twiddle.net Subject: [Qemu-devel] [PATCH v9 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 Reviewed-by: Richard Henderson --- 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 v8 - added rth's r-b tag - tweak subprocess tests --- tests/test-logging.c | 36 +++++++++++++++++++++++++++++++++++- util/log.c | 22 ++++++++++++++++++++-- 2 files changed, 55 insertions(+), 3 deletions(-) -- 2.7.2 diff --git a/tests/test-logging.c b/tests/test-logging.c index 193fa92..ac8deed 100644 --- a/tests/test-logging.c +++ b/tests/test-logging.c @@ -89,7 +89,37 @@ static void test_parse_zero_range(void) g_test_trap_assert_stdout(""); g_test_trap_assert_stderr("*Failed to parse range in: 0x1000+0\n"); } -#endif + +/* 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. + */ +static void test_parse_path_subprocess(void) +{ + /* All these should work without issue */ + qemu_set_log_filename("/tmp/qemu.log"); + qemu_set_log_filename("/tmp/qemu-%d.log"); + qemu_set_log_filename("/tmp/qemu.log.%d"); +} +static void test_parse_path(void) +{ + g_test_trap_subprocess ("/logging/parse_path/subprocess", 0, 0); + g_test_trap_assert_passed(); + g_test_trap_assert_stdout(""); + g_test_trap_assert_stderr(""); +} +static void test_parse_invalid_path_subprocess(void) +{ + qemu_set_log_filename("/tmp/qemu-%d%d.log"); +} +static void test_parse_invalid_path(void) +{ + g_test_trap_subprocess ("/logging/parse_invalid_path/subprocess", 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) { @@ -101,6 +131,10 @@ int main(int argc, char **argv) g_test_add_func("/logging/parse_invalid_range", test_parse_invalid_range); g_test_add_func("/logging/parse_zero_range/subprocess", test_parse_zero_range_subprocess); g_test_add_func("/logging/parse_zero_range", test_parse_zero_range); + g_test_add_func("/logging/parse_path", test_parse_path); + g_test_add_func("/logging/parse_path/subprocess", test_parse_path_subprocess); + g_test_add_func("/logging/parse_invalid_path", test_parse_invalid_path); + g_test_add_func("/logging/parse_invalid_path/subprocess", test_parse_invalid_path_subprocess); #endif return g_test_run(); diff --git a/util/log.c b/util/log.c index 10a917b..66370d2 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; @@ -87,11 +88,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); }