log: Fix error handling and a memory leak
cover log: Fix error handling and a memory leak
log.c has no maintainer. I can take this through my error-next branch.
Markus Armbruster (3):
log: Plug memory leak on multiple -dfilter
log: Fix qemu_set_dfilter_ranges() error reporting
log: Fix qemu_set_log_filename() error handling
bsd-user/main.c | 3 +- include/qemu/log.h | 4 +- linux-user/main.c | 3 +- monitor.c | 7 ++- tests/test-logging.c | 88 ++++++++++--------------------------- trace/control.c | 3 +- util/log.c | 122 +++++++++++++++++++++++++++------------------------ vl.c | 4 +- 8 files changed, 104 insertions(+), 130 deletions(-)
Signature · 1 line
2.5.5
PATCH 1/3 log: Plug memory leak on multiple -dfilter
-dfilter overwrites any previous filter. The overwritten filter is leaked. Leaks since the beginning (commit 3514552, v2.6.0). Free it properly.
--- util/log.c | 7 +++++++ 1 file changed, 7 insertions(+)
util/log.c +7 -0
@@ -145,9 +145,16 @@ bool qemu_log_in_addr_range(uint64_t addr)void qemu_set_dfilter_ranges (const char *filter_spec) {gchar **ranges =g_strsplit (filter_spec,"," ,0 ); + +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));while (r) {
Signature · 1 line
2.5.5
Re: [Qemu-devel] [PATCH 1/3] log: Plug memory leak on multiple -dfilter
Quoted 9 lines · Markus Armbruster
-dfilter overwrites any previous filter. The overwritten filter is leaked. Leaks since the beginning (commit 3514552, v2.6.0). Free it properly.
Signed-off-by Markus Armbruster <[email protected]>--- util/log.c | 7 +++++++ 1 file changed, 7 insertions(+)
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH 2/3 log: Fix qemu_set_dfilter_ranges() error reporting
In reply to Markus Armbrusterg_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.
--- include/qemu/log.h | 2 +- tests/test-logging.c | 49 ++++++++-------------- util/log.c | 113 ++++++++++++++++++++++++++------------------------- vl.c | 2 +- 4 files changed, 75 insertions(+), 91 deletions(-)
include/qemu/log.h +1 -1
@@ -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);
tests/test-logging.c +16 -33
93 lines not on this page · raw message
util/log.c +57 -56
138 lines not on this page · raw message
vl.c +1 -1
@@ -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 );
Signature · 1 line
2.5.5
Re: [Qemu-devel] [PATCH 2/3] log: Fix qemu_set_dfilter_ranges() error reporting
Quoted 7 lines · Markus Armbruster
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)
Quoted 6 lines
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)
I see you're trying to improve my range.h patches, and got dragged into this stuff.
Quoted 17 lines
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 <[email protected]>--- include/qemu/log.h | 2 +- tests/test-logging.c | 49 ++++++++-------------- util/log.c | 113 ++++++++++++++++++++++++++------------------------- vl.c | 2 +- 4 files changed, 75 insertions(+), 91 deletions(-)
tests/test-logging.c static void test_parse_range(void) · line 76
Quoted 6 lines
tests/test-logging.c +5
-0+qemu_set_dfilter_ranges ("0x1000+onehundred" , &err); +error_free_or_abort (&err); + +qemu_set_dfilter_ranges ("0x1000+0" , &err); +error_free_or_abort (&err); }
Maybe also worth testing "0x" and "0x1000+0x" for being invalid?
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH 2/3] log: Fix qemu_set_dfilter_ranges() error reporting
Quoted 10 lines · Eric Blake
Quoted 9 lines · Markus Armbruster
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:
Quoted 7 lines
Quoted 4 lines
$ 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)I see you're trying to improve my
range.hpatches, and got dragged into this stuff.
Yup, except I'd call it "build upon", not "improve".
Quoted 23 lines
Quoted 16 lines
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 <[email protected]>--- include/qemu/log.h | 2 +- tests/test-logging.c | 49 ++++++++-------------- util/log.c | 113 ++++++++++++++++++++++++++------------------------- vl.c | 2 +- 4 files changed, 75 insertions(+), 91 deletions(-)tests/test-logging.c static void test_parse_range(void) · line 76
Quoted 6 lines
tests/test-logging.c +5
-0+qemu_set_dfilter_ranges ("0x1000+onehundred" , &err); +error_free_or_abort (&err); + +qemu_set_dfilter_ranges ("0x1000+0" , &err); +error_free_or_abort (&err); }
Quoted 1 line
Maybe also worth testing "0x" and "0x1000+0x" for being invalid?
The former would add a test for the error handling of qemu_set_dfilter_ranges()'s other use of qemu_strtoull(). If it wasn't worth testing before my patch... But I can add a test case anyway, if I need to respin.
The latter would basically test an additional error path within qemu_strtoull().
Quoted 2 lines
Reviewed-by Eric Blake <[email protected]>
Thanks!
PATCH 3/3 log: Fix qemu_set_log_filename() error handling
In reply to Markus ArmbrusterWhen qemu_set_log_filename() detects an invalid file name, it reports an error, closes the log file (if any), and starts logging to stderr (unless daemonized or nothing is being logged).
This is wrong. Asking for an invalid log file on the command line should be fatal. Asking for one in the monitor should fail without messing up an existing logfile.
Fix by converting qemu_set_log_filename() to Error. Pass it &error_fatal, except for hmp_logfile report errors.
This also permits testing without a subprocess, so do that.
--- bsd-user/main.c | 3 ++- include/qemu/log.h | 2 +- linux-user/main.c | 3 ++- monitor.c | 7 ++++++- tests/test-logging.c | 41 ++++++++--------------------------------- trace/control.c | 3 ++- util/log.c | 6 +++--- vl.c | 2 +- 8 files changed, 25 insertions(+), 42 deletions(-)
bsd-user/main.c +2 -1
@@ -20,6 +20,7 @@#include <machine/trap.h> #include <sys/mman.h> +#include "qapi/error.h" #include "qemu.h" #include "qemu/path.h" #include "qemu/help_option.h"
@@ -848,7 +849,7 @@ int main(int argc, char **argv)/* init debug */ qemu_log_needs_buffers (); -qemu_set_log_filename (log_file); +qemu_set_log_filename (log_file, &error_fatal);if (log_mask) {int mask;
include/qemu/log.h +1 -1
@@ -106,7 +106,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_log_filename (const char *filename,Error **errp);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);
linux-user/main.c +2 -1
@@ -22,6 +22,7 @@#include <sys/syscall.h> #include <sys/resource.h> +#include "qapi/error.h" #include "qemu.h" #include "qemu/path.h" #include "qemu/cutils.h"
@@ -3846,7 +3847,7 @@ static void handle_arg_log(const char *arg)static void handle_arg_log_filename (const char *arg) { -qemu_set_log_filename (arg); +qemu_set_log_filename (arg, &error_fatal); }static void handle_arg_set_env (const char *arg)
monitor.c +6 -1
@@ -1111,7 +1111,12 @@ void qmp_client_migrate_info(const char *protocol, const char *hostname,static void hmp_logfile (Monitor *mon,const QDict *qdict) { -qemu_set_log_filename (qdict_get_str (qdict,"filename" )); +Error *err =NULL ; + +qemu_set_log_filename (qdict_get_str (qdict,"filename" ), &err); +if (err) { +error_report_err (err); + } }static void hmp_log (Monitor *mon,const QDict *qdict)
tests/test-logging.c +8 -33
57 lines not on this page · raw message
trace/control.c +2 -1
@@ -19,6 +19,7 @@#ifdef CONFIG_TRACE_LOG #include "qemu/log.h" #endif +#include "qapi/error.h" #include "qemu/error-report.h" #include "monitor/monitor.h"
@@ -187,7 +188,7 @@ void trace_init_file(const char *file)* only applies to the simple backend; use "-D" for the log backend. */ if (file) { -qemu_set_log_filename (file); +qemu_set_log_filename (file, &error_fatal); }#else if (file) {
util/log.c +3 -3
@@ -103,7 +103,7 @@ void qemu_log_needs_buffers(void)* 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) +void qemu_set_log_filename (const char *filename,Error **errp) {char *pidstr;g_free (logfilename);
@@ -112,8 +112,8 @@ void qemu_set_log_filename(const char *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 ; +error_setg (errp,"Bad logfile format: %s" , filename); +return ; }else { logfilename =g_strdup_printf (filename,getpid ()); }
vl.c +1 -1
@@ -4058,7 +4058,7 @@ int main(int argc, char **argv, char **envp)/* Open the logfile at this point and set the log mask if necessary. */ if (log_file) { -qemu_set_log_filename (log_file); +qemu_set_log_filename (log_file, &error_fatal); }if (log_mask) {
Signature · 1 line
2.5.5
Re: [Qemu-devel] [PATCH 3/3] log: Fix qemu_set_log_filename() error handling
Quoted 38 lines · Markus Armbruster
When
qemu_set_log_filename()detects an invalid file name, it reports an error, closes the log file (if any), and starts logging to stderr (unless daemonized or nothing is being logged).This is wrong. Asking for an invalid log file on the command line should be fatal. Asking for one in the monitor should fail without messing up an existing logfile.
Fix by converting
qemu_set_log_filename()to Error. Pass it&error_fatal, except forhmp_logfilereport errors.This also permits testing without a subprocess, so do that.
Signed-off-by Markus Armbruster <[email protected]>--- bsd-user/main.c | 3 ++- include/qemu/log.h | 2 +- linux-user/main.c | 3 ++- monitor.c | 7 ++++++- tests/test-logging.c | 41 ++++++++--------------------------------- trace/control.c | 3 ++- util/log.c | 6 +++--- vl.c | 2 +- 8 files changed, 25 insertions(+), 42 deletions(-)bsd-user/main.c +1
-0@@ -20,6 +20,7 @@#include <machine/trap.h> #include <sys/mman.h> +#include "qapi/error.h" #include "qemu.h" #include "qemu/path.h" #include "qemu/help_option.h"
bsd-user/main.c int main(int argc, char **argv) · line 852
Quoted 5 lines
bsd-user/main.c +1
-1@@ -848,7 +849,7 @@ int main(int argc, char **argv)/* init debug */ qemu_log_needs_buffers (); -qemu_set_log_filename (log_file); +qemu_set_log_filename (log_file, &error_fatal);
Commit race between this patch and Denis' series:
https://lists.gnu.org/archive/html/qemu-devel/2016-06/msg00515.html
Whoever goes in second will have a build failure until all the new uses are also converted.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org