From patchwork Wed Jun 15 17:27:15 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Markus Armbruster X-Patchwork-Id: 9178999 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 3594260573 for ; Wed, 15 Jun 2016 17:27:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2353327D4A for ; Wed, 15 Jun 2016 17:27:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 17B8C27EED; Wed, 15 Jun 2016 17:27:53 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 67AA727D4A for ; Wed, 15 Jun 2016 17:27:52 +0000 (UTC) Received: from localhost ([::1]:43950 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bDEbX-0006nR-JA for patchwork-qemu-devel@patchwork.kernel.org; Wed, 15 Jun 2016 13:27:51 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57464) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bDEbA-0006j5-2j for qemu-devel@nongnu.org; Wed, 15 Jun 2016 13:27:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bDEb3-0007U0-Py for qemu-devel@nongnu.org; Wed, 15 Jun 2016 13:27:27 -0400 Received: from mx1.redhat.com ([209.132.183.28]:44320) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bDEb3-0007TL-Gh for qemu-devel@nongnu.org; Wed, 15 Jun 2016 13:27:21 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A44C2C04B309; Wed, 15 Jun 2016 17:27:19 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-116-20.ams2.redhat.com [10.36.116.20]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u5FHRHP2018825 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Wed, 15 Jun 2016 13:27:18 -0400 Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id 984A91138647; Wed, 15 Jun 2016 19:27:16 +0200 (CEST) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Wed, 15 Jun 2016 19:27:15 +0200 Message-Id: <1466011636-6112-3-git-send-email-armbru@redhat.com> In-Reply-To: <1466011636-6112-1-git-send-email-armbru@redhat.com> References: <1466011636-6112-1-git-send-email-armbru@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Wed, 15 Jun 2016 17:27:19 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 2/3] log: Fix qemu_set_dfilter_ranges() error reporting X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: pbonzini@redhat.com, alex.bennee@linaro.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP g_error() is not an acceptable way to report errors to the user: $ qemu-system-x86_64 -dfilter 1000+0 ** (process:17187): ERROR **: Failed to parse range in: 1000+0 Trace/breakpoint trap (core dumped) g_assert() isn't, either: $ qemu-system-x86_64 -dfilter 1000x+64 ** ERROR:/work/armbru/qemu/util/log.c:180:qemu_set_dfilter_ranges: assertion failed: (e == range_op) Aborted (core dumped) Convert qemu_set_dfilter_ranges() to Error. Rework its deeply nested control flow. Touch up the error messages. Call it with &error_fatal. This also permits testing without a subprocess, so do that. Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake --- include/qemu/log.h | 2 +- tests/test-logging.c | 49 ++++++++-------------- util/log.c | 113 ++++++++++++++++++++++++++------------------------- vl.c | 2 +- 4 files changed, 75 insertions(+), 91 deletions(-) diff --git a/include/qemu/log.h b/include/qemu/log.h index 234fa81..df8d041 100644 --- a/include/qemu/log.h +++ b/include/qemu/log.h @@ -107,7 +107,7 @@ extern const QEMULogItem qemu_log_items[]; void qemu_set_log(int log_flags); void qemu_log_needs_buffers(void); void qemu_set_log_filename(const char *filename); -void qemu_set_dfilter_ranges(const char *ranges); +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); diff --git a/tests/test-logging.c b/tests/test-logging.c index 5ef5bb8..e043adc 100644 --- a/tests/test-logging.c +++ b/tests/test-logging.c @@ -27,11 +27,14 @@ #include "qemu/osdep.h" #include "qemu-common.h" -#include "include/qemu/log.h" +#include "qapi/error.h" +#include "qemu/log.h" static void test_parse_range(void) { - qemu_set_dfilter_ranges("0x1000+0x100"); + Error *err = NULL; + + qemu_set_dfilter_ranges("0x1000+0x100", &error_abort); g_assert_false(qemu_log_in_addr_range(0xfff)); g_assert(qemu_log_in_addr_range(0x1000)); @@ -39,56 +42,40 @@ static void test_parse_range(void) g_assert(qemu_log_in_addr_range(0x10ff)); g_assert_false(qemu_log_in_addr_range(0x1100)); - qemu_set_dfilter_ranges("0x1000-0x100"); + qemu_set_dfilter_ranges("0x1000-0x100", &error_abort); g_assert_false(qemu_log_in_addr_range(0x1001)); g_assert(qemu_log_in_addr_range(0x1000)); g_assert(qemu_log_in_addr_range(0x0f01)); g_assert_false(qemu_log_in_addr_range(0x0f00)); - qemu_set_dfilter_ranges("0x1000..0x1100"); + qemu_set_dfilter_ranges("0x1000..0x1100", &error_abort); g_assert_false(qemu_log_in_addr_range(0xfff)); g_assert(qemu_log_in_addr_range(0x1000)); g_assert(qemu_log_in_addr_range(0x1100)); g_assert_false(qemu_log_in_addr_range(0x1101)); - qemu_set_dfilter_ranges("0x1000..0x1000"); + qemu_set_dfilter_ranges("0x1000..0x1000", &error_abort); g_assert_false(qemu_log_in_addr_range(0xfff)); g_assert(qemu_log_in_addr_range(0x1000)); g_assert_false(qemu_log_in_addr_range(0x1001)); - qemu_set_dfilter_ranges("0x1000+0x100,0x2100-0x100,0x3000..0x3100"); + qemu_set_dfilter_ranges("0x1000+0x100,0x2100-0x100,0x3000..0x3100", + &error_abort); g_assert(qemu_log_in_addr_range(0x1050)); g_assert(qemu_log_in_addr_range(0x2050)); g_assert(qemu_log_in_addr_range(0x3050)); + + qemu_set_dfilter_ranges("0x1000+onehundred", &err); + error_free_or_abort(&err); + + qemu_set_dfilter_ranges("0x1000+0", &err); + error_free_or_abort(&err); } #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS -static void test_parse_invalid_range_subprocess(void) -{ - qemu_set_dfilter_ranges("0x1000+onehundred"); -} -static void test_parse_invalid_range(void) -{ - g_test_trap_subprocess("/logging/parse_invalid_range/subprocess", 0, 0); - g_test_trap_assert_failed(); - g_test_trap_assert_stdout(""); - g_test_trap_assert_stderr("*Failed to parse range in: 0x1000+onehundred\n"); -} -static void test_parse_zero_range_subprocess(void) -{ - qemu_set_dfilter_ranges("0x1000+0"); -} -static void test_parse_zero_range(void) -{ - g_test_trap_subprocess("/logging/parse_zero_range/subprocess", 0, 0); - g_test_trap_assert_failed(); - g_test_trap_assert_stdout(""); - g_test_trap_assert_stderr("*Failed to parse range in: 0x1000+0\n"); -} - /* 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. @@ -126,10 +113,6 @@ int main(int argc, char **argv) g_test_add_func("/logging/parse_range", test_parse_range); #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS - g_test_add_func("/logging/parse_invalid_range/subprocess", test_parse_invalid_range_subprocess); - 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); diff --git a/util/log.c b/util/log.c index 6f45e0a..fcf85c6 100644 --- a/util/log.c +++ b/util/log.c @@ -22,6 +22,7 @@ #include "qemu/log.h" #include "qemu/range.h" #include "qemu/error-report.h" +#include "qapi/error.h" #include "qemu/cutils.h" #include "trace/control.h" @@ -142,75 +143,75 @@ bool qemu_log_in_addr_range(uint64_t addr) } -void qemu_set_dfilter_ranges(const char *filter_spec) +void qemu_set_dfilter_ranges(const char *filter_spec, Error **errp) { gchar **ranges = g_strsplit(filter_spec, ",", 0); + int i; if (debug_regions) { g_array_unref(debug_regions); debug_regions = NULL; } - if (ranges) { - gchar **next = ranges; - gchar *r = *next++; + debug_regions = g_array_sized_new(FALSE, FALSE, + sizeof(Range), g_strv_length(ranges)); + for (i = 0; ranges[i]; i++) { + const char *r = ranges[i]; + const char *range_op, *r2, *e; + uint64_t r1val, r2val; + struct Range range; - debug_regions = g_array_sized_new(FALSE, FALSE, - sizeof(Range), g_strv_length(ranges)); - while (r) { - char *range_op = strstr(r, "-"); - char *r2 = range_op ? range_op + 1 : NULL; - if (!range_op) { - range_op = strstr(r, "+"); - r2 = range_op ? range_op + 1 : NULL; - } - if (!range_op) { - range_op = strstr(r, ".."); - r2 = range_op ? range_op + 2 : NULL; - } - if (range_op) { - const char *e = NULL; - uint64_t r1val, r2val; - - if ((qemu_strtoull(r, &e, 0, &r1val) == 0) && - (qemu_strtoull(r2, NULL, 0, &r2val) == 0) && - r2val > 0) { - struct Range range; - - g_assert(e == range_op); + range_op = strstr(r, "-"); + r2 = range_op ? range_op + 1 : NULL; + if (!range_op) { + range_op = strstr(r, "+"); + r2 = range_op ? range_op + 1 : NULL; + } + if (!range_op) { + range_op = strstr(r, ".."); + r2 = range_op ? range_op + 2 : NULL; + } + if (!range_op) { + error_setg(errp, "Bad range specifier"); + goto out; + } - switch (*range_op) { - case '+': - { - range.begin = r1val; - range.end = r1val + (r2val - 1); - break; - } - case '-': - { - range.end = r1val; - range.begin = r1val - (r2val - 1); - break; - } - case '.': - range.begin = r1val; - range.end = r2val; - break; - default: - g_assert_not_reached(); - } - g_array_append_val(debug_regions, range); + if (qemu_strtoull(r, &e, 0, &r1val) + || e != range_op) { + error_setg(errp, "Invalid number to the left of %.*s", + (int)(r2 - range_op), range_op); + goto out; + } + if (qemu_strtoull(r2, NULL, 0, &r2val)) { + error_setg(errp, "Invalid number to the right of %.*s", + (int)(r2 - range_op), range_op); + goto out; + } + if (r2val == 0) { + error_setg(errp, "Invalid range"); + goto out; + } - } else { - g_error("Failed to parse range in: %s", r); - } - } else { - g_error("Bad range specifier in: %s", r); - } - r = *next++; + switch (*range_op) { + case '+': + range.begin = r1val; + range.end = r1val + (r2val - 1); + break; + case '-': + range.end = r1val; + range.begin = r1val - (r2val - 1); + break; + case '.': + range.begin = r1val; + range.end = r2val; + break; + default: + g_assert_not_reached(); } - g_strfreev(ranges); + g_array_append_val(debug_regions, range); } +out: + g_strfreev(ranges); } /* fflush() the log file */ diff --git a/vl.c b/vl.c index 45eff56..0a42577 100644 --- a/vl.c +++ b/vl.c @@ -3345,7 +3345,7 @@ int main(int argc, char **argv, char **envp) log_file = optarg; break; case QEMU_OPTION_DFILTER: - qemu_set_dfilter_ranges(optarg); + qemu_set_dfilter_ranges(optarg, &error_fatal); break; case QEMU_OPTION_s: add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);