From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33040) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aiN84-00057V-NK for qemu-devel@nongnu.org; Tue, 22 Mar 2016 10:17:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aiN82-0006ka-Vc for qemu-devel@nongnu.org; Tue, 22 Mar 2016 10:17:52 -0400 Received: from mx1.redhat.com ([209.132.183.28]:55303) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aiN82-0006kS-LM for qemu-devel@nongnu.org; Tue, 22 Mar 2016 10:17:50 -0400 From: Paolo Bonzini Date: Tue, 22 Mar 2016 15:17:00 +0100 Message-Id: <1458656229-32043-21-git-send-email-pbonzini@redhat.com> In-Reply-To: <1458656229-32043-1-git-send-email-pbonzini@redhat.com> References: <1458656229-32043-1-git-send-email-pbonzini@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PULL 20/29] qemu-log: new option -dfilter to limit output List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= From: Alex Benn=C3=A9e When debugging big programs or system emulation sometimes you want both the verbosity of cpu,exec et all but don't want to generate lots of logs for unneeded stuff. This patch adds a new option -dfilter which allows you to specify interesting address ranges in the form: -dfilter 0x8000..0x8fff,0xffffffc000080000+0x200,... Then logging code can use the new qemu_log_in_addr_range() function to decide if it will output logging information for the given range. Signed-off-by: Alex Benn=C3=A9e Message-Id: <1458052224-9316-7-git-send-email-alex.bennee@linaro.org> Signed-off-by: Paolo Bonzini --- include/qemu/log.h | 2 + qemu-options.hx | 18 +++++++++ tests/Makefile | 4 ++ tests/test-logging.c | 107 +++++++++++++++++++++++++++++++++++++++++++++= ++++++ util/log.c | 87 +++++++++++++++++++++++++++++++++++++++++ vl.c | 3 ++ 6 files changed, 221 insertions(+) create mode 100644 tests/test-logging.c diff --git a/include/qemu/log.h b/include/qemu/log.h index 523c886..1d0222d 100644 --- a/include/qemu/log.h +++ b/include/qemu/log.h @@ -122,6 +122,8 @@ static inline void qemu_set_log(int log_flags) } =20 void qemu_set_log_filename(const char *filename); +void qemu_set_dfilter_ranges(const char *ranges); +bool qemu_log_in_addr_range(uint64_t addr); int qemu_str_to_log_mask(const char *str); =20 /* Print a usage message listing all the valid logging categories diff --git a/qemu-options.hx b/qemu-options.hx index 732ed8c..c8bb70c 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -3163,6 +3163,24 @@ STEXI Output log in @var{logfile} instead of to stderr ETEXI =20 +DEF("dfilter", HAS_ARG, QEMU_OPTION_DFILTER, \ + "-dfilter range,.. filter debug output to range of addresses (usefu= l for -d cpu,exec,etc..)\n", + QEMU_ARCH_ALL) +STEXI +@item -dfilter @var{range1}[,...] +@findex -dfilter +Filter debug output to that relevant to a range of target addresses. The= filter +spec can be either @var{start}+@var{size}, @var{start}-@var{size} or +@var{start}..@var{end} where @var{start} @var{end} and @var{size} are th= e +addresses and sizes required. For example: +@example + -dfilter 0x8000..0x8fff,0xffffffc000080000+0x200,0xffffffc000060000-= 0x1000 +@end example +Will dump output for any code in the 0x1000 sized block starting at 0x80= 00 and +the 0x200 sized block starting at 0xffffffc000080000 and another 0x1000 = sized +block starting at 0xffffffc00005f000. +ETEXI + DEF("L", HAS_ARG, QEMU_OPTION_L, \ "-L path set the directory for the BIOS, VGA BIOS and keymap= s\n", QEMU_ARCH_ALL) diff --git a/tests/Makefile b/tests/Makefile index 60371ca..d27e31d 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -97,6 +97,8 @@ check-unit-y +=3D tests/test-crypto-ivgen$(EXESUF) check-unit-y +=3D tests/test-crypto-afsplit$(EXESUF) check-unit-y +=3D tests/test-crypto-xts$(EXESUF) check-unit-y +=3D tests/test-crypto-block$(EXESUF) +gcov-files-test-logging-y =3D tests/test-logging.c +check-unit-y +=3D tests/test-logging$(EXESUF) =20 check-block-$(CONFIG_POSIX) +=3D tests/qemu-iotests-quick.sh =20 @@ -436,6 +438,8 @@ tests/test-timed-average$(EXESUF): tests/test-timed-a= verage.o qemu-timer.o \ tests/test-base64$(EXESUF): tests/test-base64.o \ libqemuutil.a libqemustub.a =20 +tests/test-logging$(EXESUF): tests/test-logging.o $(test-util-obj-y) + tests/test-qapi-types.c tests/test-qapi-types.h :\ $(SRC_PATH)/tests/qapi-schema/qapi-schema-test.json $(SRC_PATH)/scripts/= qapi-types.py $(qapi-py) $(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-types.py \ diff --git a/tests/test-logging.c b/tests/test-logging.c new file mode 100644 index 0000000..193fa92 --- /dev/null +++ b/tests/test-logging.c @@ -0,0 +1,107 @@ +/* + * logging unit-tests + * + * Copyright (C) 2016 Linaro Ltd. + * + * Author: Alex Benn=C3=A9e + * + * Permission is hereby granted, free of charge, to any person obtaining= a copy + * of this software and associated documentation files (the "Software"),= to deal + * in the Software without restriction, including without limitation the= rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or = sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be includ= ed in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRE= SS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILI= TY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHA= LL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR = OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISI= NG FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING= S IN + * THE SOFTWARE. + */ + +#include "qemu/osdep.h" +#include + +#include "qemu-common.h" +#include "include/qemu/log.h" + +static void test_parse_range(void) +{ + qemu_set_dfilter_ranges("0x1000+0x100"); + + g_assert_false(qemu_log_in_addr_range(0xfff)); + g_assert(qemu_log_in_addr_range(0x1000)); + g_assert(qemu_log_in_addr_range(0x1001)); + g_assert(qemu_log_in_addr_range(0x10ff)); + g_assert_false(qemu_log_in_addr_range(0x1100)); + + qemu_set_dfilter_ranges("0x1000-0x100"); + + 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"); + + 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"); + + 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"); + g_assert(qemu_log_in_addr_range(0x1050)); + g_assert(qemu_log_in_addr_range(0x2050)); + g_assert(qemu_log_in_addr_range(0x3050)); +} + +#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+onehund= red\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"); +} +#endif + +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_invalid_range/subprocess", test_pars= e_invalid_range_subprocess); + g_test_add_func("/logging/parse_invalid_range", test_parse_invalid_r= ange); + g_test_add_func("/logging/parse_zero_range/subprocess", test_parse_z= ero_range_subprocess); + g_test_add_func("/logging/parse_zero_range", test_parse_zero_range); +#endif + + return g_test_run(); +} diff --git a/util/log.c b/util/log.c index 4e69586..671b617 100644 --- a/util/log.c +++ b/util/log.c @@ -20,12 +20,16 @@ #include "qemu/osdep.h" #include "qemu-common.h" #include "qemu/log.h" +#include "qemu/range.h" +#include "qemu/error-report.h" +#include "qemu/cutils.h" #include "trace/control.h" =20 static char *logfilename; FILE *qemu_logfile; int qemu_loglevel; static int log_append =3D 0; +static GArray *debug_regions; =20 void qemu_log(const char *fmt, ...) { @@ -94,6 +98,89 @@ void qemu_set_log_filename(const char *filename) qemu_set_log(qemu_loglevel); } =20 +/* Returns true if addr is in our debug filter or no filter defined + */ +bool qemu_log_in_addr_range(uint64_t addr) +{ + if (debug_regions) { + int i =3D 0; + for (i =3D 0; i < debug_regions->len; i++) { + struct Range *range =3D &g_array_index(debug_regions, Range,= i); + if (addr >=3D range->begin && addr <=3D range->end) { + return true; + } + } + return false; + } else { + return true; + } +} + + +void qemu_set_dfilter_ranges(const char *filter_spec) +{ + gchar **ranges =3D g_strsplit(filter_spec, ",", 0); + if (ranges) { + gchar **next =3D ranges; + gchar *r =3D *next++; + debug_regions =3D g_array_sized_new(FALSE, FALSE, + sizeof(Range), g_strv_length(r= anges)); + while (r) { + char *range_op =3D strstr(r, "-"); + char *r2 =3D range_op ? range_op + 1 : NULL; + if (!range_op) { + range_op =3D strstr(r, "+"); + r2 =3D range_op ? range_op + 1 : NULL; + } + if (!range_op) { + range_op =3D strstr(r, ".."); + r2 =3D range_op ? range_op + 2 : NULL; + } + if (range_op) { + const char *e =3D NULL; + uint64_t r1val, r2val; + + if ((qemu_strtoull(r, &e, 0, &r1val) =3D=3D 0) && + (qemu_strtoull(r2, NULL, 0, &r2val) =3D=3D 0) && + r2val > 0) { + struct Range range; + + g_assert(e =3D=3D range_op); + + switch (*range_op) { + case '+': + { + range.begin =3D r1val; + range.end =3D r1val + (r2val - 1); + break; + } + case '-': + { + range.end =3D r1val; + range.begin =3D r1val - (r2val - 1); + break; + } + case '.': + range.begin =3D r1val; + range.end =3D r2val; + break; + default: + g_assert_not_reached(); + } + g_array_append_val(debug_regions, range); + + } else { + g_error("Failed to parse range in: %s", r); + } + } else { + g_error("Bad range specifier in: %s", r); + } + r =3D *next++; + } + g_strfreev(ranges); + } +} + const QEMULogItem qemu_log_items[] =3D { { CPU_LOG_TB_OUT_ASM, "out_asm", "show generated host assembly code for each compiled TB" }, diff --git a/vl.c b/vl.c index 6566da2..ae35176 100644 --- a/vl.c +++ b/vl.c @@ -3355,6 +3355,9 @@ int main(int argc, char **argv, char **envp) case QEMU_OPTION_D: log_file =3D optarg; break; + case QEMU_OPTION_DFILTER: + qemu_set_dfilter_ranges(optarg); + break; case QEMU_OPTION_s: add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT)= ; break; --=20 2.5.0