From 54ee5b3da00cdbee836a06a7e6eb319c895f2a57 Mon Sep 17 00:00:00 2001 From: Richard Henderson Date: Sun, 17 Apr 2022 11:29:41 -0700 Subject: util/log: Drop manual log buffering MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This buffering was introduced during the Paleozoic: 9fa3e853531. There has never been an explanation as to why we may not allow glibc to allocate the file buffer itself. We certainly have many other uses of mmap and malloc during user-only startup, so presumably whatever the issue was, it has been fixed during the preceeding 18 years. Reviewed-by: Alex Bennée Signed-off-by: Richard Henderson Message-Id: <20220417183019.755276-2-richard.henderson@linaro.org> --- include/qemu/log.h | 1 - 1 file changed, 1 deletion(-) (limited to 'include/qemu') diff --git a/include/qemu/log.h b/include/qemu/log.h index 5739c7e..ed59ebd 100644 --- a/include/qemu/log.h +++ b/include/qemu/log.h @@ -148,7 +148,6 @@ typedef struct QEMULogItem { 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, Error **errp); void qemu_set_dfilter_ranges(const char *ranges, Error **errp); bool qemu_log_in_addr_range(uint64_t addr); -- cgit v1.1 From e2c7c6a454c2c4221461aa04e4061dbb91b4986c Mon Sep 17 00:00:00 2001 From: Richard Henderson Date: Sun, 17 Apr 2022 11:29:43 -0700 Subject: util/log: Return bool from qemu_set_log_filename MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Per the recommendations in qapi/error.h, return false on failure. Use the return value in the monitor, the only place we aren't already passing error_fatal or error_abort. Reviewed-by: Alex Bennée Signed-off-by: Richard Henderson Message-Id: <20220417183019.755276-4-richard.henderson@linaro.org> --- include/qemu/log.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/qemu') diff --git a/include/qemu/log.h b/include/qemu/log.h index ed59ebd..fabfbe4 100644 --- a/include/qemu/log.h +++ b/include/qemu/log.h @@ -148,7 +148,7 @@ typedef struct QEMULogItem { extern const QEMULogItem qemu_log_items[]; void qemu_set_log(int log_flags); -void qemu_set_log_filename(const char *filename, Error **errp); +bool 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); -- cgit v1.1 From c5955f4ff4689b7a04cf0a1109fa97ce885876b4 Mon Sep 17 00:00:00 2001 From: Richard Henderson Date: Sun, 17 Apr 2022 11:29:44 -0700 Subject: util/log: Pass Error pointer to qemu_set_log MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Do not force exit within qemu_set_log; return bool and pass an Error value back up the stack as per usual. Reviewed-by: Alex Bennée Signed-off-by: Richard Henderson Message-Id: <20220417183019.755276-5-richard.henderson@linaro.org> --- include/qemu/log.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/qemu') diff --git a/include/qemu/log.h b/include/qemu/log.h index fabfbe4..0b892f5 100644 --- a/include/qemu/log.h +++ b/include/qemu/log.h @@ -147,7 +147,7 @@ typedef struct QEMULogItem { extern const QEMULogItem qemu_log_items[]; -void qemu_set_log(int log_flags); +bool qemu_set_log(int log_flags, Error **errp); bool 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); -- cgit v1.1 From c59fe6e536adfb2d6f3410f19d592496933f68de Mon Sep 17 00:00:00 2001 From: Richard Henderson Date: Sun, 17 Apr 2022 11:29:46 -0700 Subject: util/log: Move qemu_log_lock, qemu_log_unlock out of line MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reviewed-by: Alex Bennée Reviewed-by: Philippe Mathieu-Daudé Signed-off-by: Richard Henderson Message-Id: <20220417183019.755276-7-richard.henderson@linaro.org> --- include/qemu/log.h | 28 +++------------------------- 1 file changed, 3 insertions(+), 25 deletions(-) (limited to 'include/qemu') diff --git a/include/qemu/log.h b/include/qemu/log.h index 0b892f5..6a6b1ef 100644 --- a/include/qemu/log.h +++ b/include/qemu/log.h @@ -65,32 +65,10 @@ static inline bool qemu_log_separate(void) /* LOG_STRACE is used for user-mode strace logging. */ #define LOG_STRACE (1 << 19) -/* Lock output for a series of related logs. Since this is not needed - * for a single qemu_log / qemu_log_mask / qemu_log_mask_and_addr, we - * assume that qemu_loglevel_mask has already been tested, and that - * qemu_loglevel is never set when qemu_logfile is unset. - */ +/* Lock/unlock output. */ -static inline FILE *qemu_log_lock(void) -{ - QemuLogFile *logfile; - rcu_read_lock(); - logfile = qatomic_rcu_read(&qemu_logfile); - if (logfile) { - qemu_flockfile(logfile->fd); - return logfile->fd; - } else { - return NULL; - } -} - -static inline void qemu_log_unlock(FILE *fd) -{ - if (fd) { - qemu_funlockfile(fd); - } - rcu_read_unlock(); -} +FILE *qemu_log_lock(void); +void qemu_log_unlock(FILE *fd); /* Logging functions: */ -- cgit v1.1 From c60f599bcb9bf9256eb87c1c60add55d23f76de3 Mon Sep 17 00:00:00 2001 From: Richard Henderson Date: Sun, 17 Apr 2022 11:29:47 -0700 Subject: util/log: Rename qemu_log_lock to qemu_log_trylock MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This function can fail, which makes it more like ftrylockfile or pthread_mutex_trylock than flockfile or pthread_mutex_lock, so rename it. To closer match the other trylock functions, release rcu_read_lock along the failure path, so that qemu_log_unlock need not be called on failure. Reviewed-by: Alex Bennée Signed-off-by: Richard Henderson Message-Id: <20220417183019.755276-8-richard.henderson@linaro.org> --- include/qemu/log.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/qemu') diff --git a/include/qemu/log.h b/include/qemu/log.h index 6a6b1ef..d090faf 100644 --- a/include/qemu/log.h +++ b/include/qemu/log.h @@ -67,7 +67,7 @@ static inline bool qemu_log_separate(void) /* Lock/unlock output. */ -FILE *qemu_log_lock(void); +FILE *qemu_log_trylock(void); void qemu_log_unlock(FILE *fd); /* Logging functions: */ -- cgit v1.1 From 3fb659605f45be5abce9d54910334853482d98f7 Mon Sep 17 00:00:00 2001 From: Richard Henderson Date: Sun, 17 Apr 2022 11:29:50 -0700 Subject: util/log: Remove qemu_log_vprintf MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This function is no longer used. Reviewed-by: Alex Bennée Signed-off-by: Richard Henderson Message-Id: <20220417183019.755276-11-richard.henderson@linaro.org> --- include/qemu/log.h | 15 --------------- 1 file changed, 15 deletions(-) (limited to 'include/qemu') diff --git a/include/qemu/log.h b/include/qemu/log.h index d090faf..2d9455d 100644 --- a/include/qemu/log.h +++ b/include/qemu/log.h @@ -72,21 +72,6 @@ void qemu_log_unlock(FILE *fd); /* Logging functions: */ -/* vfprintf-like logging function - */ -static inline void G_GNUC_PRINTF(1, 0) -qemu_log_vprintf(const char *fmt, va_list va) -{ - QemuLogFile *logfile; - - rcu_read_lock(); - logfile = qatomic_rcu_read(&qemu_logfile); - if (logfile) { - vfprintf(logfile->fd, fmt, va); - } - rcu_read_unlock(); -} - /* log only if a bit is set on the current loglevel mask: * @mask: bit to check in the mask * @fmt: printf-style format string -- cgit v1.1 From 3c06a41746fd0399a182839a6a25c79f1c4ee83a Mon Sep 17 00:00:00 2001 From: Richard Henderson Date: Sun, 17 Apr 2022 11:29:57 -0700 Subject: util/log: Drop return value from qemu_log MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The only user of this feature, tcg_dump_ops, has been converted to use fprintf directly. Reviewed-by: Alex Bennée Signed-off-by: Richard Henderson Message-Id: <20220417183019.755276-18-richard.henderson@linaro.org> --- include/qemu/log-for-trace.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/qemu') diff --git a/include/qemu/log-for-trace.h b/include/qemu/log-for-trace.h index 5e41517..d47c9cd 100644 --- a/include/qemu/log-for-trace.h +++ b/include/qemu/log-for-trace.h @@ -30,6 +30,6 @@ static inline bool qemu_loglevel_mask(int mask) } /* main logging function */ -int G_GNUC_PRINTF(1, 2) qemu_log(const char *fmt, ...); +void G_GNUC_PRINTF(1, 2) qemu_log(const char *fmt, ...); #endif -- cgit v1.1 From 27ea81337f7f6d1756c8cb22626208b294bbb375 Mon Sep 17 00:00:00 2001 From: Richard Henderson Date: Sun, 17 Apr 2022 11:29:58 -0700 Subject: util/log: Mark qemu_log_trylock as G_GNUC_WARN_UNUSED_RESULT MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Now that all uses have been updated, consider a missing test of the result of qemu_log_trylock a bug and Werror. Reviewed-by: Alex Bennée Signed-off-by: Richard Henderson Message-Id: <20220417183019.755276-19-richard.henderson@linaro.org> --- include/qemu/log.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/qemu') diff --git a/include/qemu/log.h b/include/qemu/log.h index 2d9455d..3a1f3a6 100644 --- a/include/qemu/log.h +++ b/include/qemu/log.h @@ -67,7 +67,7 @@ static inline bool qemu_log_separate(void) /* Lock/unlock output. */ -FILE *qemu_log_trylock(void); +FILE *qemu_log_trylock(void) G_GNUC_WARN_UNUSED_RESULT; void qemu_log_unlock(FILE *fd); /* Logging functions: */ -- cgit v1.1 From 90f37362d7d2037a47f0c18ab4a26055acbd7b3a Mon Sep 17 00:00:00 2001 From: Richard Henderson Date: Sun, 17 Apr 2022 11:29:59 -0700 Subject: util/log: Remove qemu_log_flush MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit All uses flush output immediately before or after qemu_log_unlock. Instead of a separate call, move the flush into qemu_log_unlock. Reviewed-by: Alex Bennée Reviewed-by: Philippe Mathieu-Daudé Signed-off-by: Richard Henderson Message-Id: <20220417183019.755276-20-richard.henderson@linaro.org> --- include/qemu/log.h | 2 -- 1 file changed, 2 deletions(-) (limited to 'include/qemu') diff --git a/include/qemu/log.h b/include/qemu/log.h index 3a1f3a6..7597311 100644 --- a/include/qemu/log.h +++ b/include/qemu/log.h @@ -121,8 +121,6 @@ int qemu_str_to_log_mask(const char *str); */ void qemu_print_log_usage(FILE *f); -/* fflush() the log file */ -void qemu_log_flush(void); /* Close the log file */ void qemu_log_close(void); -- cgit v1.1 From 7fc493f8bd5b5eccf761ec9b1caa13c872e289ec Mon Sep 17 00:00:00 2001 From: Richard Henderson Date: Sun, 17 Apr 2022 11:30:05 -0700 Subject: include/qemu/log: Move entire implementation out-of-line MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move QemuLogFile, qemu_logfile, and all inline functions into qemu/log.c. No need to expose these implementation details in the api. Reviewed-by: Alex Bennée Signed-off-by: Richard Henderson Message-Id: <20220417183019.755276-26-richard.henderson@linaro.org> --- include/qemu/log.h | 38 ++++---------------------------------- 1 file changed, 4 insertions(+), 34 deletions(-) (limited to 'include/qemu') diff --git a/include/qemu/log.h b/include/qemu/log.h index 7597311..42d545f 100644 --- a/include/qemu/log.h +++ b/include/qemu/log.h @@ -3,46 +3,16 @@ /* A small part of this API is split into its own header */ #include "qemu/log-for-trace.h" -#include "qemu/rcu.h" - -typedef struct QemuLogFile { - struct rcu_head rcu; - FILE *fd; -} QemuLogFile; - -/* Private global variable, don't use */ -extern QemuLogFile *qemu_logfile; - /* * The new API: - * */ -/* Log settings checking macros: */ +/* Returns true if qemu_log() will really write somewhere. */ +bool qemu_log_enabled(void); -/* Returns true if qemu_log() will really write somewhere - */ -static inline bool qemu_log_enabled(void) -{ - return qemu_logfile != NULL; -} - -/* Returns true if qemu_log() will write somewhere else than stderr - */ -static inline bool qemu_log_separate(void) -{ - QemuLogFile *logfile; - bool res = false; - - rcu_read_lock(); - logfile = qatomic_rcu_read(&qemu_logfile); - if (logfile && logfile->fd != stderr) { - res = true; - } - rcu_read_unlock(); - return res; -} +/* Returns true if qemu_log() will write somewhere other than stderr. */ +bool qemu_log_separate(void); #define CPU_LOG_TB_OUT_ASM (1 << 0) #define CPU_LOG_TB_IN_ASM (1 << 1) -- cgit v1.1 From 144539d3605ed7da9b8c1d69f49a282f8c4348b8 Mon Sep 17 00:00:00 2001 From: Richard Henderson Date: Sun, 17 Apr 2022 11:30:07 -0700 Subject: util/log: Introduce qemu_set_log_filename_flags Provide a function to set both filename and flags at the same time. This is the common case at startup. Signed-off-by: Richard Henderson Message-Id: <20220417183019.755276-28-richard.henderson@linaro.org> --- include/qemu/log.h | 1 + 1 file changed, 1 insertion(+) (limited to 'include/qemu') diff --git a/include/qemu/log.h b/include/qemu/log.h index 42d545f..b6c7337 100644 --- a/include/qemu/log.h +++ b/include/qemu/log.h @@ -82,6 +82,7 @@ extern const QEMULogItem qemu_log_items[]; bool qemu_set_log(int log_flags, Error **errp); bool qemu_set_log_filename(const char *filename, Error **errp); +bool qemu_set_log_filename_flags(const char *name, int flags, 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); -- cgit v1.1 From ec0d1849d990ae25017c7b611a4385f4ec2cc874 Mon Sep 17 00:00:00 2001 From: Richard Henderson Date: Sun, 17 Apr 2022 11:30:11 -0700 Subject: util/log: Remove qemu_log_close MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The only real use is in cpu_abort, where we have just flushed the file via qemu_log_unlock, and are just about to force-crash the application via abort. We do not really need to close the FILE before the abort. The two uses in test-logging.c can be handled with qemu_set_log_filename_flags. Reviewed-by: Alex Bennée Signed-off-by: Richard Henderson Message-Id: <20220417183019.755276-32-richard.henderson@linaro.org> --- include/qemu/log.h | 3 --- 1 file changed, 3 deletions(-) (limited to 'include/qemu') diff --git a/include/qemu/log.h b/include/qemu/log.h index b6c7337..a325bca 100644 --- a/include/qemu/log.h +++ b/include/qemu/log.h @@ -92,7 +92,4 @@ int qemu_str_to_log_mask(const char *str); */ void qemu_print_log_usage(FILE *f); -/* Close the log file */ -void qemu_log_close(void); - #endif -- cgit v1.1 From 4e51069d679348d2617512e56e28cdc7bb34c833 Mon Sep 17 00:00:00 2001 From: Richard Henderson Date: Sun, 17 Apr 2022 11:30:19 -0700 Subject: util/log: Support per-thread log files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a new log flag, tid, to turn this feature on. Require the log filename to be set, and to contain %d. Do not allow tid to be turned off once it is on, nor let the filename be change thereafter. This avoids the need for signalling each thread to re-open on a name change. Reviewed-by: Alex Bennée Signed-off-by: Richard Henderson Message-Id: <20220417183019.755276-40-richard.henderson@linaro.org> --- include/qemu/log.h | 1 + 1 file changed, 1 insertion(+) (limited to 'include/qemu') diff --git a/include/qemu/log.h b/include/qemu/log.h index a325bca..c5643d8 100644 --- a/include/qemu/log.h +++ b/include/qemu/log.h @@ -34,6 +34,7 @@ bool qemu_log_separate(void); #define CPU_LOG_PLUGIN (1 << 18) /* LOG_STRACE is used for user-mode strace logging. */ #define LOG_STRACE (1 << 19) +#define LOG_PER_THREAD (1 << 20) /* Lock/unlock output. */ -- cgit v1.1