From patchwork Thu Nov 7 14:26:10 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robert Foley X-Patchwork-Id: 11233041 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 16B35139A for ; Thu, 7 Nov 2019 14:30:24 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id E027B2187F for ; Thu, 7 Nov 2019 14:30:23 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="bTQkB5b1" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E027B2187F Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:43366 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iSinm-0007bV-JA for patchwork-qemu-devel@patchwork.kernel.org; Thu, 07 Nov 2019 09:30:22 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:46931) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iSik9-0003G9-PM for qemu-devel@nongnu.org; Thu, 07 Nov 2019 09:26:38 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iSik8-0006jE-CF for qemu-devel@nongnu.org; Thu, 07 Nov 2019 09:26:37 -0500 Received: from mail-pf1-x441.google.com ([2607:f8b0:4864:20::441]:46160) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iSik8-0006it-6m for qemu-devel@nongnu.org; Thu, 07 Nov 2019 09:26:36 -0500 Received: by mail-pf1-x441.google.com with SMTP id 193so2731390pfc.13 for ; Thu, 07 Nov 2019 06:26:36 -0800 (PST) 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; bh=RQogdHqT92+3dXdjI90KovlZh55RRNpNiecef2mLhCs=; b=bTQkB5b1kQVLaHJbaOpg3d6ChmkFJ7XpGPwqyyvvUc4a9GKv+5uVZlQH/HHJKyZA4K hws1MenGAhaSB4R3C8Nh4ZB+MKSBWsEy6CPu9FCh+ZKeRi1/RycrXKyqKDAcWUKsG45c BxODsE7kLgux29NsOktO7ejQnC60H4nselrkJpbJ7fLu8++NLSF8ngwYI4SvA6Ttks3O eHnz6crl9Pq/2uNcL1JCHLb/DyDCnU8r+xfx76v4lJAdPlqEHSWOjEpFNKYSsAVmjuQC buP6wvlDtU42PsKz+PJxLDRcZglWqZsC4dVqbw+6JpAED2FyDLk8vasXogXCkRlL1J6C ZB0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=RQogdHqT92+3dXdjI90KovlZh55RRNpNiecef2mLhCs=; b=TOmghzGzCA9oBo6d63mVa5DvLILrMToDNvZUk/FEsba67GfvO8i+wSHlcW+So9WYo8 i1/+mo43y/qp+SYa9ZLucmCGRMmFtNwyaB3EFiyK3JpsQuJpLiEuXQBG3jiqqX1HpG8T 4TmjXlWMwFlB2NBLoHAVOcDZceYP5UoEKx9dLhDL+ciK+2oU2roQ9mt1IcWUGHDSZcOW NxKqTLb5joWZu773pK710pfaWjThTHdoDDFmeXBZWrpVNsusduOET83OXwwIDu30WMjT RxTvc9Q5ZmELqFxZNHVhF7ojSJ8jXTEcVJR46/jEgpQJmaxjFEnEpW4frReZ8ObQ8hde /FtQ== X-Gm-Message-State: APjAAAX9iQn8eqAkGB5WLVNGP5BSbZ+f7VICPwOpgNHSeDTtyR3CbP2c IYki5v4oDvSpKeiP8k7L7MlxpmyySB0= X-Google-Smtp-Source: APXvYqyhu+u4Q2AyWuHN1/jKV/TXS8F4g2axtpLXpD3yv1EwjlYIeBHPLC78R2IrZ7IvU4DWBLUtjg== X-Received: by 2002:a63:f246:: with SMTP id d6mr4789561pgk.368.1573136794926; Thu, 07 Nov 2019 06:26:34 -0800 (PST) Received: from Rfoley-MA01.usrd.futurewei.com ([12.111.81.71]) by smtp.gmail.com with ESMTPSA id f8sm3375502pgd.64.2019.11.07.06.26.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Nov 2019 06:26:34 -0800 (PST) From: Robert Foley To: qemu-devel@nongnu.org Subject: [PATCH 1/4] Add a mutex to guarantee single writer to qemu_logfile handle. Date: Thu, 7 Nov 2019 09:26:10 -0500 Message-Id: <20191107142613.2379-2-robert.foley@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191107142613.2379-1-robert.foley@linaro.org> References: <20191107142613.2379-1-robert.foley@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::441 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.puhov@linaro.org, alex.bennee@linaro.org, robert.foley@linaro.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" This is being added in preparation for using RCU with the logfile handle. Also added qemu_logfile_init() for initializing the logfile mutex. Signed-off-by: Robert Foley --- util/log.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/util/log.c b/util/log.c index 1ca13059ee..dff2f98c8c 100644 --- a/util/log.c +++ b/util/log.c @@ -24,8 +24,11 @@ #include "qapi/error.h" #include "qemu/cutils.h" #include "trace/control.h" +#include "qemu/thread.h" static char *logfilename; +static bool qemu_logfile_initialized; +static QemuMutex qemu_logfile_mutex; FILE *qemu_logfile; int qemu_loglevel; static int log_append = 0; @@ -49,6 +52,14 @@ int qemu_log(const char *fmt, ...) return ret; } +static void qemu_logfile_init(void) +{ + if (!qemu_logfile_initialized) { + qemu_mutex_init(&qemu_logfile_mutex); + qemu_logfile_initialized = true; + } +} + static bool log_uses_own_buffers; /* enable or disable low levels log */ @@ -58,6 +69,12 @@ void qemu_set_log(int log_flags) #ifdef CONFIG_TRACE_LOG qemu_loglevel |= LOG_TRACE; #endif + + /* Is there a better place to call this to init the logfile subsystem? */ + if (!qemu_logfile_initialized) { + qemu_logfile_init(); + } + qemu_mutex_lock(&qemu_logfile_mutex); if (!qemu_logfile && (is_daemonized() ? logfilename != NULL : qemu_loglevel)) { if (logfilename) { @@ -93,6 +110,7 @@ void qemu_set_log(int log_flags) log_append = 1; } } + qemu_mutex_unlock(&qemu_logfile_mutex); if (qemu_logfile && (is_daemonized() ? logfilename == NULL : !qemu_loglevel)) { qemu_log_close(); @@ -114,6 +132,11 @@ void qemu_set_log_filename(const char *filename, Error **errp) char *pidstr; g_free(logfilename); + /* Is there a better place to call this to init the logfile subsystem? */ + if (!qemu_logfile_initialized) { + qemu_logfile_init(); + } + pidstr = strstr(filename, "%"); if (pidstr) { /* We only accept one %d, no other format strings */ From patchwork Thu Nov 7 14:26:11 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robert Foley X-Patchwork-Id: 11233035 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 521EA139A for ; Thu, 7 Nov 2019 14:28:16 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 1775C2187F for ; Thu, 7 Nov 2019 14:28:16 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="fls26ad9" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1775C2187F Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:43322 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iSilj-0004zB-2x for patchwork-qemu-devel@patchwork.kernel.org; Thu, 07 Nov 2019 09:28:15 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:46957) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iSikC-0003KB-Cg for qemu-devel@nongnu.org; Thu, 07 Nov 2019 09:26:42 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iSikA-0006nM-Hj for qemu-devel@nongnu.org; Thu, 07 Nov 2019 09:26:40 -0500 Received: from mail-pg1-x542.google.com ([2607:f8b0:4864:20::542]:45814) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iSikA-0006lN-AP for qemu-devel@nongnu.org; Thu, 07 Nov 2019 09:26:38 -0500 Received: by mail-pg1-x542.google.com with SMTP id w11so2227073pga.12 for ; Thu, 07 Nov 2019 06:26:38 -0800 (PST) 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; bh=VcKoyPsibYcE2C8KFqaYyzY+4kEltVkSUl6/VtW6chc=; b=fls26ad9zRE9q2luErviXVyspnViwRyNAEfNVZd93JG9E1YFT+y4d9RoxIazHbIlyF DrCI4LLnktyROCheHRo2xAX+vBT8I4lhFQUsePtD7i8p4jdIpuvPvVaUiE808GLzL5bV hSpklYzO/JSLDx5qSCLWKEUVxFsgrTIhUt+7VtuqV733UMpLDPI/bFAGyvD4NY4mu9qY AV762OdbIv+7V/xV6KAt0S1N+RqAFcZ385eNIzF+lIha/v348CyrzIG9oL8seI8629Q1 JTZkYwHQAaINCnCwn6dMW86uwfaLtGRzAsYFQYlmA8+JruIdOwJVyDjBlpmJzJeKyXu3 7BRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=VcKoyPsibYcE2C8KFqaYyzY+4kEltVkSUl6/VtW6chc=; b=g+ty8fQUhyqEKDOBO6YqMGZeLe7b4YdQcwYOpwc1qo2r/NEOaI30R0c4oS2TSp6XHN RBO66OjDmkVsajsdN7MLmFEeCijF2taOINXKtwjxZGQJTS459qYUFILqKsX8a1BCgnrm qkr/awTj0M3f3xBVnG7PyftReRL2pMk9dhn84bNKQZeNnPS0b58q8+MPZKRW9iYfB4Ft ycbqb07lbXAqJX+m8JEFYP8DwelxEkAQhCexWCXejBzdOw/vIIV2b8je2G5yu1i88ZEw OVOa25f1yTqeWARXn8Za/IFHWs5MqkMS+t/HoqJTaup00csc++CSy+sg3RM3b6mL8vMQ SebA== X-Gm-Message-State: APjAAAWhGBKMKpmATt78WN4T9W/3DUttCUOiFPmvbpjDmDwLR0l1LO0V WsRXAjqj56PrmsqcdFXcngRtEqX5/MA= X-Google-Smtp-Source: APXvYqzaTlkp9GL97NUPIrB0cvie+COVl0I8yAus8rVAIOm9WzRFFOFJCfKnw/OBaBVxbFJkG34gAQ== X-Received: by 2002:a63:7b5c:: with SMTP id k28mr5037601pgn.442.1573136796566; Thu, 07 Nov 2019 06:26:36 -0800 (PST) Received: from Rfoley-MA01.usrd.futurewei.com ([12.111.81.71]) by smtp.gmail.com with ESMTPSA id f8sm3375502pgd.64.2019.11.07.06.26.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Nov 2019 06:26:36 -0800 (PST) From: Robert Foley To: qemu-devel@nongnu.org Subject: [PATCH 2/4] Add use of RCU for qemu_logfile. Date: Thu, 7 Nov 2019 09:26:11 -0500 Message-Id: <20191107142613.2379-3-robert.foley@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191107142613.2379-1-robert.foley@linaro.org> References: <20191107142613.2379-1-robert.foley@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::542 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.puhov@linaro.org, alex.bennee@linaro.org, robert.foley@linaro.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" This now allows changing the logfile while logging is active, and also solves the issue of a seg fault while changing the logfile. Any read access to the qemu_logfile handle will use the rcu_read_lock()/unlock() around the use of the handle. To fetch the handle we will use atomic_rcu_read(). We also in many cases do a check for validity of the logfile handle before using it to deal with the case where the file is closed and set to NULL. The cases where we write to the qemu_logfile will use atomic_rcu_set(). Writers will also use call_rcu() with a newly added qemu_logfile_free function for freeing/closing when readers have finished. Signed-off-by: Robert Foley --- include/qemu/log.h | 47 ++++++++++++++++++++++++---- util/log.c | 78 ++++++++++++++++++++++++++++++++++------------ include/exec/log.h | 33 +++++++++++++++++--- tcg/tcg.c | 12 +++++-- 4 files changed, 138 insertions(+), 32 deletions(-) diff --git a/include/qemu/log.h b/include/qemu/log.h index a91105b2ad..975de18e23 100644 --- a/include/qemu/log.h +++ b/include/qemu/log.h @@ -3,9 +3,17 @@ /* A small part of this API is split into its own header */ #include "qemu/log-for-trace.h" +#include "qemu/rcu.h" + +struct QemuLogFile { + struct rcu_head rcu; + FILE *fd; +}; +typedef struct QemuLogFile QemuLogFile; /* Private global variable, don't use */ -extern FILE *qemu_logfile; +extern QemuLogFile *qemu_logfile; + /* * The new API: @@ -25,7 +33,17 @@ static inline bool qemu_log_enabled(void) */ static inline bool qemu_log_separate(void) { - return qemu_logfile != NULL && qemu_logfile != stderr; + QemuLogFile *logfile; + + if (qemu_log_enabled()) { + rcu_read_lock(); + logfile = atomic_rcu_read(&qemu_logfile); + if (logfile && logfile->fd != stderr) { + return true; + } + rcu_read_unlock(); + } + return false; } #define CPU_LOG_TB_OUT_ASM (1 << 0) @@ -55,12 +73,23 @@ static inline bool qemu_log_separate(void) static inline void qemu_log_lock(void) { - qemu_flockfile(qemu_logfile); + QemuLogFile *logfile; + rcu_read_lock(); + logfile = atomic_rcu_read(&qemu_logfile); + if (logfile) { + qemu_flockfile(logfile->fd); + } + rcu_read_unlock(); } static inline void qemu_log_unlock(void) { - qemu_funlockfile(qemu_logfile); + QemuLogFile *logfile; + logfile = atomic_rcu_read(&qemu_logfile); + if (logfile) { + qemu_funlockfile(logfile->fd); + } + rcu_read_unlock(); } /* Logging functions: */ @@ -70,9 +99,14 @@ static inline void qemu_log_unlock(void) static inline void GCC_FMT_ATTR(1, 0) qemu_log_vprintf(const char *fmt, va_list va) { - if (qemu_logfile) { - vfprintf(qemu_logfile, fmt, va); + QemuLogFile *logfile; + + rcu_read_lock(); + logfile = atomic_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: @@ -129,5 +163,6 @@ void qemu_print_log_usage(FILE *f); void qemu_log_flush(void); /* Close the log file */ void qemu_log_close(void); +void qemu_logfile_free(QemuLogFile *logfile); #endif diff --git a/util/log.c b/util/log.c index dff2f98c8c..d409532b8f 100644 --- a/util/log.c +++ b/util/log.c @@ -29,7 +29,7 @@ static char *logfilename; static bool qemu_logfile_initialized; static QemuMutex qemu_logfile_mutex; -FILE *qemu_logfile; +QemuLogFile *qemu_logfile; int qemu_loglevel; static int log_append = 0; static GArray *debug_regions; @@ -38,10 +38,14 @@ static GArray *debug_regions; int qemu_log(const char *fmt, ...) { int ret = 0; - if (qemu_logfile) { + QemuLogFile *logfile; + + rcu_read_lock(); + logfile = atomic_rcu_read(&qemu_logfile); + if (logfile) { va_list ap; va_start(ap, fmt); - ret = vfprintf(qemu_logfile, fmt, ap); + ret = vfprintf(logfile->fd, fmt, ap); va_end(ap); /* Don't pass back error results. */ @@ -49,6 +53,7 @@ int qemu_log(const char *fmt, ...) ret = 0; } } + rcu_read_unlock(); return ret; } @@ -65,6 +70,8 @@ static bool log_uses_own_buffers; /* enable or disable low levels log */ void qemu_set_log(int log_flags) { + QemuLogFile *logfile; + qemu_loglevel = log_flags; #ifdef CONFIG_TRACE_LOG qemu_loglevel |= LOG_TRACE; @@ -77,43 +84,51 @@ void qemu_set_log(int log_flags) qemu_mutex_lock(&qemu_logfile_mutex); if (!qemu_logfile && (is_daemonized() ? logfilename != NULL : qemu_loglevel)) { + logfile = g_new0(QemuLogFile, 1); if (logfilename) { - qemu_logfile = fopen(logfilename, log_append ? "a" : "w"); - if (!qemu_logfile) { + logfile->fd = fopen(logfilename, log_append ? "a" : "w"); + if (!logfile->fd) { + g_free(logfile); perror(logfilename); _exit(1); } /* In case we are a daemon redirect stderr to logfile */ if (is_daemonized()) { - dup2(fileno(qemu_logfile), STDERR_FILENO); - fclose(qemu_logfile); + dup2(fileno(logfile->fd), STDERR_FILENO); + fclose(logfile->fd); /* This will skip closing logfile in qemu_log_close() */ - qemu_logfile = stderr; + logfile->fd = stderr; } } else { /* Default to stderr if no log file specified */ assert(!is_daemonized()); - qemu_logfile = stderr; + logfile->fd = stderr; } /* must avoid mmap() usage of glibc by setting a buffer "by hand" */ if (log_uses_own_buffers) { static char logfile_buf[4096]; - setvbuf(qemu_logfile, logfile_buf, _IOLBF, sizeof(logfile_buf)); + setvbuf(logfile->fd, logfile_buf, _IOLBF, sizeof(logfile_buf)); } else { #if defined(_WIN32) /* Win32 doesn't support line-buffering, so use unbuffered output. */ - setvbuf(qemu_logfile, NULL, _IONBF, 0); + setvbuf(logfile->fd, NULL, _IONBF, 0); #else - setvbuf(qemu_logfile, NULL, _IOLBF, 0); + setvbuf(logfile->fd, NULL, _IOLBF, 0); #endif log_append = 1; } + atomic_rcu_set(&qemu_logfile, logfile); } - qemu_mutex_unlock(&qemu_logfile_mutex); + logfile = qemu_logfile; + if (qemu_logfile && (is_daemonized() ? logfilename == NULL : !qemu_loglevel)) { - qemu_log_close(); + atomic_rcu_set(&qemu_logfile, NULL); + qemu_mutex_unlock(&qemu_logfile_mutex); + call_rcu(logfile, qemu_logfile_free, rcu); + } else { + qemu_mutex_unlock(&qemu_logfile_mutex); } } @@ -247,17 +262,40 @@ out: /* fflush() the log file */ void qemu_log_flush(void) { - fflush(qemu_logfile); + QemuLogFile *logfile; + + rcu_read_lock(); + logfile = atomic_rcu_read(&qemu_logfile); + if (logfile) { + fflush(logfile->fd); + } + rcu_read_unlock(); +} + +void qemu_logfile_free(QemuLogFile *logfile) +{ + if (logfile) { + if (logfile->fd != stderr) { + fclose(logfile->fd); + } + g_free(logfile); + } } /* Close the log file */ void qemu_log_close(void) { - if (qemu_logfile) { - if (qemu_logfile != stderr) { - fclose(qemu_logfile); - } - qemu_logfile = NULL; + QemuLogFile *logfile; + + qemu_mutex_lock(&qemu_logfile_mutex); + logfile = qemu_logfile; + + if (logfile) { + atomic_rcu_set(&qemu_logfile, NULL); + qemu_mutex_unlock(&qemu_logfile_mutex); + call_rcu(logfile, qemu_logfile_free, rcu); + } else { + qemu_mutex_unlock(&qemu_logfile_mutex); } } diff --git a/include/exec/log.h b/include/exec/log.h index e2cfd436e6..9bd1e4aa20 100644 --- a/include/exec/log.h +++ b/include/exec/log.h @@ -15,8 +15,15 @@ */ static inline void log_cpu_state(CPUState *cpu, int flags) { + QemuLogFile *logfile; + if (qemu_log_enabled()) { - cpu_dump_state(cpu, qemu_logfile, flags); + rcu_read_lock(); + logfile = atomic_rcu_read(&qemu_logfile); + if (logfile) { + cpu_dump_state(cpu, logfile->fd, flags); + } + rcu_read_unlock(); } } @@ -40,19 +47,37 @@ static inline void log_cpu_state_mask(int mask, CPUState *cpu, int flags) static inline void log_target_disas(CPUState *cpu, target_ulong start, target_ulong len) { - target_disas(qemu_logfile, cpu, start, len); + QemuLogFile *logfile; + rcu_read_lock(); + logfile = atomic_rcu_read(&qemu_logfile); + if (logfile) { + target_disas(logfile->fd, cpu, start, len); + } + rcu_read_unlock(); } static inline void log_disas(void *code, unsigned long size) { - disas(qemu_logfile, code, size); + QemuLogFile *logfile; + rcu_read_lock(); + logfile = atomic_rcu_read(&qemu_logfile); + if (logfile) { + disas(logfile->fd, code, size); + } + rcu_read_unlock(); } #if defined(CONFIG_USER_ONLY) /* page_dump() output to the log file: */ static inline void log_page_dump(void) { - page_dump(qemu_logfile); + QemuLogFile *logfile; + rcu_read_lock(); + logfile = atomic_rcu_read(&qemu_logfile); + if (logfile) { + page_dump(logfile->fd); + } + rcu_read_unlock(); } #endif #endif diff --git a/tcg/tcg.c b/tcg/tcg.c index 5475d49ed1..220eaac7c7 100644 --- a/tcg/tcg.c +++ b/tcg/tcg.c @@ -2114,9 +2114,17 @@ static void tcg_dump_ops(TCGContext *s, bool have_prefs) } if (have_prefs || op->life) { - for (; col < 40; ++col) { - putc(' ', qemu_logfile); + + QemuLogFile *logfile; + + rcu_read_lock(); + logfile = atomic_rcu_read(&qemu_logfile); + if (logfile) { + for (; col < 40; ++col) { + putc(' ', logfile->fd); + } } + rcu_read_unlock(); } if (op->life) { From patchwork Thu Nov 7 14:26:12 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robert Foley X-Patchwork-Id: 11233037 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 74B59139A for ; Thu, 7 Nov 2019 14:28:20 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 3B5E62187F for ; Thu, 7 Nov 2019 14:28:20 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="ovpSp5Pk" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3B5E62187F Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:43326 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iSiln-000588-3S for patchwork-qemu-devel@patchwork.kernel.org; Thu, 07 Nov 2019 09:28:19 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:46974) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iSikD-0003MP-Tn for qemu-devel@nongnu.org; Thu, 07 Nov 2019 09:26:43 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iSikC-0006oo-17 for qemu-devel@nongnu.org; Thu, 07 Nov 2019 09:26:41 -0500 Received: from mail-pl1-x641.google.com ([2607:f8b0:4864:20::641]:35304) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iSikB-0006oB-PC for qemu-devel@nongnu.org; Thu, 07 Nov 2019 09:26:39 -0500 Received: by mail-pl1-x641.google.com with SMTP id s10so1592272plp.2 for ; Thu, 07 Nov 2019 06:26:39 -0800 (PST) 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; bh=lIi54vr/HziQ7SiW+njRL+MHz75T7cGRSD/KXgMu79Q=; b=ovpSp5Pkv1JqUJsIzwnUIWpIQePLGWfHqdt9lzF630v/aGpHrwQAvBPk/mGvJtaPvq gcsVzhDQ1JGqTzW8ZQll4rpU5iGwlxroWXA7Pu0Ft+TZOc3kb1kyi4c3kUO8vCqyQi8Y B4lXXtT+4ta4tPrgN8d4n678qJQ0/lPY03V1xdM84caKD54zEIxnnXkfE0uDKRQJUG2P PSWiNVLFLg2xP4MZITpPY/IiK6z7UNnC8fcEV2lzwEi5SFWARj/ImGJYZgFF377VJK7c pVSddCyrxf0cCNTaDTChjxGUA2rwoBHS1Db+4MyB5cUAG1/4Tiz6j8dL3rfpaeYRP1bY pSRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=lIi54vr/HziQ7SiW+njRL+MHz75T7cGRSD/KXgMu79Q=; b=DwzArokrygbWnMIT7MoGqCEh4xYlRh7BmJ2J4qoIBXIP3LHlzAhbN+jkqExhbJ1AcT sQ1jjHuQCy6qssCWcD/iqKiSVdvTdGPH8Cpf0ar9kTmj3mOQwWoqmVoOajEL8P+G09c9 hXJ4y2WkdcCRgJMTLmlcdMVqmBDIbtsPbCvdnh9fJA0J6Skc98j8xLq5PG77QSqgGkxE VJIrYe7RHhtFSfYOSuGUG2QP1RcggTvZzgpoApiibW3zTeeJ5ScG1oGFREhpO06neHpT nNrNPzZxJzici9nHtA62VvdAcSrzzgdgT01oBnCtSCcfDhwuqaEC0BzPxIQaV2koi6DX Fr9g== X-Gm-Message-State: APjAAAUtk4RKBJQqX2XaRSZlebOWHaEWDyWYImjXF9U6nDj0kVH0p+CG OqGK+LhBbj1iZXClvPfoC3r1pOAi5XQ= X-Google-Smtp-Source: APXvYqysgQpkl4jydo5FFHCLaQB6BzgnQTxVQVyUgSPzh6XulkvhaUlfKimjyV9VmFhWtnKVvJ1MWw== X-Received: by 2002:a17:90a:2ec3:: with SMTP id h3mr5535811pjs.131.1573136798387; Thu, 07 Nov 2019 06:26:38 -0800 (PST) Received: from Rfoley-MA01.usrd.futurewei.com ([12.111.81.71]) by smtp.gmail.com with ESMTPSA id f8sm3375502pgd.64.2019.11.07.06.26.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Nov 2019 06:26:37 -0800 (PST) From: Robert Foley To: qemu-devel@nongnu.org Subject: [PATCH 3/4] qemu_log_lock/unlock now preserves the qemu_logfile handle. Date: Thu, 7 Nov 2019 09:26:12 -0500 Message-Id: <20191107142613.2379-4-robert.foley@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191107142613.2379-1-robert.foley@linaro.org> References: <20191107142613.2379-1-robert.foley@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::641 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.puhov@linaro.org, alex.bennee@linaro.org, robert.foley@linaro.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" qemu_log_lock() now returns a handle and qemu_log_unlock() receives a handle to unlock. This allows for changing the handle during logging and ensures the lock() and unlock() are for the same file. Signed-off-by: Robert Foley --- include/qemu/log.h | 14 +++++++------- accel/tcg/cpu-exec.c | 4 ++-- accel/tcg/translate-all.c | 4 ++-- accel/tcg/translator.c | 4 ++-- exec.c | 4 ++-- hw/net/can/can_sja1000.c | 4 ++-- net/can/can_socketcan.c | 5 ++--- target/cris/translate.c | 4 ++-- target/i386/translate.c | 5 +++-- target/lm32/translate.c | 4 ++-- target/microblaze/translate.c | 4 ++-- target/nios2/translate.c | 4 ++-- target/tilegx/translate.c | 7 ++++--- target/unicore32/translate.c | 4 ++-- tcg/tcg.c | 16 ++++++++-------- 15 files changed, 44 insertions(+), 43 deletions(-) diff --git a/include/qemu/log.h b/include/qemu/log.h index 975de18e23..3d0f47a479 100644 --- a/include/qemu/log.h +++ b/include/qemu/log.h @@ -71,25 +71,25 @@ static inline bool qemu_log_separate(void) * qemu_loglevel is never set when qemu_logfile is unset. */ -static inline void qemu_log_lock(void) +static inline FILE *qemu_log_lock(void) { QemuLogFile *logfile; rcu_read_lock(); logfile = atomic_rcu_read(&qemu_logfile); if (logfile) { qemu_flockfile(logfile->fd); + return logfile->fd; } rcu_read_unlock(); + return NULL; } -static inline void qemu_log_unlock(void) +static inline void qemu_log_unlock(FILE *fd) { - QemuLogFile *logfile; - logfile = atomic_rcu_read(&qemu_logfile); - if (logfile) { - qemu_funlockfile(logfile->fd); + if (fd) { + qemu_funlockfile(fd); + rcu_read_unlock(); } - rcu_read_unlock(); } /* Logging functions: */ diff --git a/accel/tcg/cpu-exec.c b/accel/tcg/cpu-exec.c index c01f59c743..62068d10c3 100644 --- a/accel/tcg/cpu-exec.c +++ b/accel/tcg/cpu-exec.c @@ -156,7 +156,7 @@ static inline tcg_target_ulong cpu_tb_exec(CPUState *cpu, TranslationBlock *itb) #if defined(DEBUG_DISAS) if (qemu_loglevel_mask(CPU_LOG_TB_CPU) && qemu_log_in_addr_range(itb->pc)) { - qemu_log_lock(); + FILE *logfile = qemu_log_lock(); int flags = 0; if (qemu_loglevel_mask(CPU_LOG_TB_FPU)) { flags |= CPU_DUMP_FPU; @@ -165,7 +165,7 @@ static inline tcg_target_ulong cpu_tb_exec(CPUState *cpu, TranslationBlock *itb) flags |= CPU_DUMP_CCOP; #endif log_cpu_state(cpu, flags); - qemu_log_unlock(); + qemu_log_unlock(logfile); } #endif /* DEBUG_DISAS */ diff --git a/accel/tcg/translate-all.c b/accel/tcg/translate-all.c index 9f48da9472..bb325a2bc4 100644 --- a/accel/tcg/translate-all.c +++ b/accel/tcg/translate-all.c @@ -1804,7 +1804,7 @@ TranslationBlock *tb_gen_code(CPUState *cpu, #ifdef DEBUG_DISAS if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM) && qemu_log_in_addr_range(tb->pc)) { - qemu_log_lock(); + FILE *logfile = qemu_log_lock(); qemu_log("OUT: [size=%d]\n", gen_code_size); if (tcg_ctx->data_gen_ptr) { size_t code_size = tcg_ctx->data_gen_ptr - tb->tc.ptr; @@ -1829,7 +1829,7 @@ TranslationBlock *tb_gen_code(CPUState *cpu, } qemu_log("\n"); qemu_log_flush(); - qemu_log_unlock(); + qemu_log_unlock(logfile); } #endif diff --git a/accel/tcg/translator.c b/accel/tcg/translator.c index f977682be7..603d17ff83 100644 --- a/accel/tcg/translator.c +++ b/accel/tcg/translator.c @@ -138,11 +138,11 @@ void translator_loop(const TranslatorOps *ops, DisasContextBase *db, #ifdef DEBUG_DISAS if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM) && qemu_log_in_addr_range(db->pc_first)) { - qemu_log_lock(); + FILE *logfile = qemu_log_lock(); qemu_log("----------------\n"); ops->disas_log(db, cpu); qemu_log("\n"); - qemu_log_unlock(); + qemu_log_unlock(logfile); } #endif } diff --git a/exec.c b/exec.c index ffdb518535..c994a00f10 100644 --- a/exec.c +++ b/exec.c @@ -1223,13 +1223,13 @@ void cpu_abort(CPUState *cpu, const char *fmt, ...) fprintf(stderr, "\n"); cpu_dump_state(cpu, stderr, CPU_DUMP_FPU | CPU_DUMP_CCOP); if (qemu_log_separate()) { - qemu_log_lock(); + FILE *logfile = qemu_log_lock(); qemu_log("qemu: fatal: "); qemu_log_vprintf(fmt, ap2); qemu_log("\n"); log_cpu_state(cpu, CPU_DUMP_FPU | CPU_DUMP_CCOP); qemu_log_flush(); - qemu_log_unlock(); + qemu_log_unlock(logfile); qemu_log_close(); } va_end(ap2); diff --git a/hw/net/can/can_sja1000.c b/hw/net/can/can_sja1000.c index 1f81341554..39c78faf9b 100644 --- a/hw/net/can/can_sja1000.c +++ b/hw/net/can/can_sja1000.c @@ -247,8 +247,8 @@ int can_sja_accept_filter(CanSJA1000State *s, static void can_display_msg(const char *prefix, const qemu_can_frame *msg) { int i; + FILE *logfile = qemu_log_lock(); - qemu_log_lock(); qemu_log("%s%03X [%01d] %s %s", prefix, msg->can_id & QEMU_CAN_EFF_MASK, @@ -261,7 +261,7 @@ static void can_display_msg(const char *prefix, const qemu_can_frame *msg) } qemu_log("\n"); qemu_log_flush(); - qemu_log_unlock(); + qemu_log_unlock(logfile); } static void buff2frame_pel(const uint8_t *buff, qemu_can_frame *frame) diff --git a/net/can/can_socketcan.c b/net/can/can_socketcan.c index 8a6ffad40c..29bfacd4f8 100644 --- a/net/can/can_socketcan.c +++ b/net/can/can_socketcan.c @@ -76,8 +76,7 @@ QEMU_BUILD_BUG_ON(offsetof(qemu_can_frame, data) static void can_host_socketcan_display_msg(struct qemu_can_frame *msg) { int i; - - qemu_log_lock(); + FILE *logfile = qemu_log_lock(); qemu_log("[cansocketcan]: %03X [%01d] %s %s", msg->can_id & QEMU_CAN_EFF_MASK, msg->can_dlc, @@ -89,7 +88,7 @@ static void can_host_socketcan_display_msg(struct qemu_can_frame *msg) } qemu_log("\n"); qemu_log_flush(); - qemu_log_unlock(); + qemu_log_unlock(logfile); } static void can_host_socketcan_read(void *opaque) diff --git a/target/cris/translate.c b/target/cris/translate.c index e752bd0609..cb57516a44 100644 --- a/target/cris/translate.c +++ b/target/cris/translate.c @@ -3273,11 +3273,11 @@ void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns) #if !DISAS_CRIS if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM) && qemu_log_in_addr_range(pc_start)) { - qemu_log_lock(); + FILE *logfile = qemu_log_lock(); qemu_log("--------------\n"); qemu_log("IN: %s\n", lookup_symbol(pc_start)); log_target_disas(cs, pc_start, dc->pc - pc_start); - qemu_log_unlock(); + qemu_log_unlock(logfile); } #endif #endif diff --git a/target/i386/translate.c b/target/i386/translate.c index 77e932d827..7c99ef1385 100644 --- a/target/i386/translate.c +++ b/target/i386/translate.c @@ -2502,14 +2502,15 @@ static void gen_unknown_opcode(CPUX86State *env, DisasContext *s) gen_illegal_opcode(s); if (qemu_loglevel_mask(LOG_UNIMP)) { + FILE *logfile = qemu_log_lock(); target_ulong pc = s->pc_start, end = s->pc; - qemu_log_lock(); + qemu_log("ILLOPC: " TARGET_FMT_lx ":", pc); for (; pc < end; ++pc) { qemu_log(" %02x", cpu_ldub_code(env, pc)); } qemu_log("\n"); - qemu_log_unlock(); + qemu_log_unlock(logfile); } } diff --git a/target/lm32/translate.c b/target/lm32/translate.c index 778cae1e81..73db9654d6 100644 --- a/target/lm32/translate.c +++ b/target/lm32/translate.c @@ -1137,10 +1137,10 @@ void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns) #ifdef DEBUG_DISAS if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM) && qemu_log_in_addr_range(pc_start)) { - qemu_log_lock(); + FILE *logfile = qemu_log_lock(); qemu_log("\n"); log_target_disas(cs, pc_start, dc->pc - pc_start); - qemu_log_unlock(); + qemu_log_unlock(logfile); } #endif } diff --git a/target/microblaze/translate.c b/target/microblaze/translate.c index 761f535357..cc1ad15656 100644 --- a/target/microblaze/translate.c +++ b/target/microblaze/translate.c @@ -1766,10 +1766,10 @@ void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns) #if !SIM_COMPAT if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM) && qemu_log_in_addr_range(pc_start)) { - qemu_log_lock(); + FILE *logfile = qemu_log_lock(); qemu_log("--------------\n"); log_target_disas(cs, pc_start, dc->pc - pc_start); - qemu_log_unlock(); + qemu_log_unlock(logfile); } #endif #endif diff --git a/target/nios2/translate.c b/target/nios2/translate.c index e17656e66f..82107bf270 100644 --- a/target/nios2/translate.c +++ b/target/nios2/translate.c @@ -892,11 +892,11 @@ void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns) #ifdef DEBUG_DISAS if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM) && qemu_log_in_addr_range(tb->pc)) { - qemu_log_lock(); + FILE *logfile = qemu_log_lock(); qemu_log("IN: %s\n", lookup_symbol(tb->pc)); log_target_disas(cs, tb->pc, dc->pc - tb->pc); qemu_log("\n"); - qemu_log_unlock(); + qemu_log_unlock(logfile); } #endif } diff --git a/target/tilegx/translate.c b/target/tilegx/translate.c index 68dd4aa2d8..fd406f4f71 100644 --- a/target/tilegx/translate.c +++ b/target/tilegx/translate.c @@ -2377,6 +2377,7 @@ void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns) uint64_t pc_start = tb->pc; uint64_t page_start = pc_start & TARGET_PAGE_MASK; int num_insns = 0; + FILE *logfile = NULL; dc->pc = pc_start; dc->mmuidx = 0; @@ -2388,7 +2389,7 @@ void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns) dc->zero = NULL; if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) { - qemu_log_lock(); + logfile = qemu_log_lock(); qemu_log("IN: %s\n", lookup_symbol(pc_start)); } gen_tb_start(tb); @@ -2418,9 +2419,9 @@ void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns) tb->size = dc->pc - pc_start; tb->icount = num_insns; - if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) { + if (logfile) { qemu_log("\n"); - qemu_log_unlock(); + qemu_log_unlock(logfile); } } diff --git a/target/unicore32/translate.c b/target/unicore32/translate.c index 0e01f35856..0f6891b8aa 100644 --- a/target/unicore32/translate.c +++ b/target/unicore32/translate.c @@ -1994,12 +1994,12 @@ done_generating: #ifdef DEBUG_DISAS if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM) && qemu_log_in_addr_range(pc_start)) { - qemu_log_lock(); + FILE *logfile = qemu_log_lock(); qemu_log("----------------\n"); qemu_log("IN: %s\n", lookup_symbol(pc_start)); log_target_disas(cs, pc_start, dc->pc - pc_start); qemu_log("\n"); - qemu_log_unlock(); + qemu_log_unlock(logfile); } #endif tb->size = dc->pc - pc_start; diff --git a/tcg/tcg.c b/tcg/tcg.c index 220eaac7c7..4f616ba38b 100644 --- a/tcg/tcg.c +++ b/tcg/tcg.c @@ -1085,7 +1085,7 @@ void tcg_prologue_init(TCGContext *s) #ifdef DEBUG_DISAS if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) { - qemu_log_lock(); + FILE *logfile = qemu_log_lock(); qemu_log("PROLOGUE: [size=%zu]\n", prologue_size); if (s->data_gen_ptr) { size_t code_size = s->data_gen_ptr - buf0; @@ -1110,7 +1110,7 @@ void tcg_prologue_init(TCGContext *s) } qemu_log("\n"); qemu_log_flush(); - qemu_log_unlock(); + qemu_log_unlock(logfile); } #endif @@ -4049,11 +4049,11 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb) #ifdef DEBUG_DISAS if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP) && qemu_log_in_addr_range(tb->pc))) { - qemu_log_lock(); + FILE *logfile = qemu_log_lock(); qemu_log("OP:\n"); tcg_dump_ops(s, false); qemu_log("\n"); - qemu_log_unlock(); + qemu_log_unlock(logfile); } #endif @@ -4094,11 +4094,11 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb) #ifdef DEBUG_DISAS if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_IND) && qemu_log_in_addr_range(tb->pc))) { - qemu_log_lock(); + FILE *logfile = qemu_log_lock(); qemu_log("OP before indirect lowering:\n"); tcg_dump_ops(s, false); qemu_log("\n"); - qemu_log_unlock(); + qemu_log_unlock(logfile); } #endif /* Replace indirect temps with direct temps. */ @@ -4115,11 +4115,11 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb) #ifdef DEBUG_DISAS if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_OPT) && qemu_log_in_addr_range(tb->pc))) { - qemu_log_lock(); + FILE *logfile = qemu_log_lock(); qemu_log("OP after optimization and liveness analysis:\n"); tcg_dump_ops(s, true); qemu_log("\n"); - qemu_log_unlock(); + qemu_log_unlock(logfile); } #endif From patchwork Thu Nov 7 14:26:13 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robert Foley X-Patchwork-Id: 11233043 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CB4B4139A for ; Thu, 7 Nov 2019 14:30:37 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id A1F842187F for ; Thu, 7 Nov 2019 14:30:37 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="D78OqL6s" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A1F842187F Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:43370 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iSio0-0007tS-If for patchwork-qemu-devel@patchwork.kernel.org; Thu, 07 Nov 2019 09:30:36 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:46991) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iSikE-0003PB-M9 for qemu-devel@nongnu.org; Thu, 07 Nov 2019 09:26:43 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iSikD-0006rE-FB for qemu-devel@nongnu.org; Thu, 07 Nov 2019 09:26:42 -0500 Received: from mail-il1-x141.google.com ([2607:f8b0:4864:20::141]:32891) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iSikD-0006qW-AU for qemu-devel@nongnu.org; Thu, 07 Nov 2019 09:26:41 -0500 Received: by mail-il1-x141.google.com with SMTP id m5so1983338ilq.0 for ; Thu, 07 Nov 2019 06:26:41 -0800 (PST) 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; bh=P81pi/aFpNUz2jl2QCoIMjM2nAAnpNgeMkCmUYxdLK8=; b=D78OqL6sJuO6eId6ZAti9eYygpAG4cYmxu5kVwbTyCftqLti4Y9nH5b/P+W6+s7qAP WBazpSfXp22luIACvEqGRtcI9RSCFzIujD71IO98RCFPxiGdxoQkNoMd8HU3AIwlUN/6 bsuIEjVnb1jyJOkw+kqugMW6lE+8Dc9giPfc0ub3g7vYw5yqqUhzaydKV0D/m31i5HLF uIfqjSfSeAPBU8F4AgHwEo3Vu6F/IcEiI1aVrtrKdH6o5/B2xKjrlgQd/sMgnCOuuLrM DsOeMO2QTINbOph+Qh0T+XW5r+k8AK/t4w7spWWs2WZNzMb3iEPVsh42R7+ZEVJCKJJD /GUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=P81pi/aFpNUz2jl2QCoIMjM2nAAnpNgeMkCmUYxdLK8=; b=QkEZQlAmZMBVs3SL7OqsOBVzGTWcjvwWF1O4lF6s8GiSq6/Yq4GrZInOPn1z5Kt+6Q nuFUC7tEEDl5PT0n0x5b/FflfeVi2RFzE0cIlSf3e90FHds0J+gvPnsQcvdfobi+70e9 Blv5jmMJrSb8vJ5GY4mZ3hFNQXWJ1ErPMaAsk8zPdWv4u2yWLl5ta5NJkr9tM105nuEg UElaKaLSa+RF1UrJs/axxPUVx+sw5TtC7eqGx9z/2KS5hMl20X8tAvqxGN7ZBu+MdJ4s H1VWlvjlygUILDvn0PAL8LFGnkUSSyBK+fV96dC6V7jd5vIEO0fWjiAu8UqQD4HX2qvb SdrA== X-Gm-Message-State: APjAAAW5ehilDaO2Iii26rPP3+HRn82KgNLeyuw25123sQwUk/9dR7qf ajfvlonyAogXrRxKO9pgmbVx9tcIqu8= X-Google-Smtp-Source: APXvYqxu48to3XqyROkUJm5s69O1TOuSTxy1WI+WaPKuR4PHn3My8FMlSd7rIzEHu/EiQqfq3glYKQ== X-Received: by 2002:a17:90a:7608:: with SMTP id s8mr5523373pjk.75.1573136800279; Thu, 07 Nov 2019 06:26:40 -0800 (PST) Received: from Rfoley-MA01.usrd.futurewei.com ([12.111.81.71]) by smtp.gmail.com with ESMTPSA id f8sm3375502pgd.64.2019.11.07.06.26.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Nov 2019 06:26:39 -0800 (PST) From: Robert Foley To: qemu-devel@nongnu.org Subject: [PATCH 4/4] Added tests for close and change of logfile. Date: Thu, 7 Nov 2019 09:26:13 -0500 Message-Id: <20191107142613.2379-5-robert.foley@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191107142613.2379-1-robert.foley@linaro.org> References: <20191107142613.2379-1-robert.foley@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::141 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.puhov@linaro.org, alex.bennee@linaro.org, robert.foley@linaro.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" One test ensures that the logfile handle is still valid even if the logfile is changed during logging. The other test validates that the logfile handle remains valid under the logfile lock even if the logfile is closed. Signed-off-by: Robert Foley --- tests/test-logging.c | 74 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/tests/test-logging.c b/tests/test-logging.c index a12585f70a..a3190ff92c 100644 --- a/tests/test-logging.c +++ b/tests/test-logging.c @@ -108,6 +108,76 @@ static void test_parse_path(gconstpointer data) error_free_or_abort(&err); } +static void test_logfile_write(gconstpointer data) +{ + QemuLogFile *logfile; + gchar const *dir = data; + Error *err = NULL; + gchar *file_path; + gchar *file_path1; + FILE *orig_fd; + + file_path = g_build_filename(dir, "qemu_test_log_write0.log", NULL); + file_path1 = g_build_filename(dir, "qemu_test_log_write1.log", NULL); + + /* + * Test that even if an open file handle is changed, + * our handle remains valid due to RCU. + */ + qemu_set_log_filename(file_path, &err); + g_assert(!err); + rcu_read_lock(); + logfile = atomic_rcu_read(&qemu_logfile); + orig_fd = logfile->fd; + g_assert(logfile && logfile->fd); + fprintf(logfile->fd, "%s 1st write to file\n", __func__); + fflush(logfile->fd); + + /* Change the logfile and ensure that the handle is still valid. */ + qemu_set_log_filename(file_path1, &err); + g_assert(!err); + g_assert(logfile->fd == orig_fd); + fprintf(logfile->fd, "%s 2nd write to file\n", __func__); + fflush(logfile->fd); + rcu_read_unlock(); + + g_free(file_path); + g_free(file_path1); +} + +static void test_logfile_lock(gconstpointer data) +{ + FILE *logfile; + gchar const *dir = data; + Error *err = NULL; + gchar *file_path; + + file_path = g_build_filename(dir, "qemu_test_logfile_lock0.log", NULL); + + /* + * Test the use of the logfile lock, such + * that even if an open file handle is closed, + * our handle remains valid for use due to RCU. + */ + qemu_set_log_filename(file_path, &err); + logfile = qemu_log_lock(); + g_assert(logfile); + fprintf(logfile, "%s 1st write to file\n", __func__); + fflush(logfile); + + /* + * Initiate a close file and make sure our handle remains + * valid since we still have the logfile lock. + */ + qemu_log_close(); + fprintf(logfile, "%s 2nd write to file\n", __func__); + fflush(logfile); + qemu_log_unlock(logfile); + + g_assert(!err); + g_free(file_path); +} + /* Remove a directory and all its entries (non-recursive). */ static void rmdir_full(gchar const *root) { @@ -134,6 +204,10 @@ int main(int argc, char **argv) g_test_add_func("/logging/parse_range", test_parse_range); g_test_add_data_func("/logging/parse_path", tmp_path, test_parse_path); + g_test_add_data_func("/logging/logfile_write_path", + tmp_path, test_logfile_write); + g_test_add_data_func("/logging/logfile_lock_path", + tmp_path, test_logfile_lock); rc = g_test_run();