From patchwork Mon Nov 18 21:15:23 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Robert Foley X-Patchwork-Id: 11250277 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 E43E6109A for ; Mon, 18 Nov 2019 21:17:26 +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 B9AD822310 for ; Mon, 18 Nov 2019 21:17:26 +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="h+2Qho2v" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B9AD822310 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]:39558 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iWoOg-0001p9-Ri for patchwork-qemu-devel@patchwork.kernel.org; Mon, 18 Nov 2019 16:17:22 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:35764) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iWoN8-0008R6-Rn for qemu-devel@nongnu.org; Mon, 18 Nov 2019 16:15:50 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iWoN5-0005iU-1c for qemu-devel@nongnu.org; Mon, 18 Nov 2019 16:15:46 -0500 Received: from mail-pg1-x544.google.com ([2607:f8b0:4864:20::544]:33983) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iWoN4-0005hz-Sj for qemu-devel@nongnu.org; Mon, 18 Nov 2019 16:15:42 -0500 Received: by mail-pg1-x544.google.com with SMTP id z188so10239807pgb.1 for ; Mon, 18 Nov 2019 13:15:42 -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 :mime-version:content-transfer-encoding; bh=diXnF1IPkkxinXlGIqTBKK6HjgFjUcsfRfseHjAG8V0=; b=h+2Qho2vTpunhRLfbyinfHSPOa+YoaWCod/ZWn6WgkDrMlTjLAmIGN/bNzgia5xPpk YRk7BKwtye0qLaJijKZ+PfGxmnzlLWXYqbsv0Bk3hwxphR7kSPhEUt3+nyK3w3Z/p1fL 1WtJMn6s0cGQ+pfsZW5C0KmmJ+QNXfbbQq/JRVQGl8Ao5WBxAlibD1bk+vnlpfMDAqn+ wlgj9DEZe0ZqnxsboGd8Qh/X+qVvbXWyAs765Mem3hW5tAGlbWn2YaX/kuDhEdjeAJGa jXnnDaES1oFauz2IQ50oizgSXt+2az0+WcGe5Y4GDpUFm2nW35DUOhbo+ob1ucfi1Sp7 nHRQ== 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:mime-version:content-transfer-encoding; bh=diXnF1IPkkxinXlGIqTBKK6HjgFjUcsfRfseHjAG8V0=; b=Kt4hV26hjg51C4Pes0FkFZ30FKMR5GgshlenpSDinP9elN29Wq+h1jMvEU1XhuoMyY xrPsiVJ0MKejaF9TOPognr9IB8/86/p/F2k3Gb+4PVYb1+JmB0APVuB4+C2QIlSqCDUn Fw/e4tqbchS5Ke5ZZ93anbFFMn/JNfXj/vDIoaj34EHj5szbZGfoig6qU3FrtchN/pEU EUnz+LPezvSirQVkeIqYCkPQz4KXcgMmjZoC6AJoaA0MUqF6+harEIENvyJ5oruiOlfP chiju0tN+XedjgFZcQoUVeyc8NIUDphZPHVuAQV5XCsDmPme4oLcVJoeZACmgXEn/FQs pFeA== X-Gm-Message-State: APjAAAWmCFfZ3gA8TtTiJ622D9f4Tf9qZoJVwYFjt4cXsXZDCfrkXTOu Xuivo/zW79dst4aVyx4FE9aVglbKrko= X-Google-Smtp-Source: APXvYqy+2N1iB+YkTEY1Jy2gp5MiS4ziTHhcp2pczMV9uYFIrZPhC8TlbLsoA4y6y+5U9hJVQQ0m2A== X-Received: by 2002:a62:e914:: with SMTP id j20mr1510186pfh.245.1574111741581; Mon, 18 Nov 2019 13:15:41 -0800 (PST) Received: from Rfoley-MA01.usrd.futurewei.com ([12.111.81.71]) by smtp.gmail.com with ESMTPSA id v15sm22301728pfe.44.2019.11.18.13.15.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Nov 2019 13:15:41 -0800 (PST) From: Robert Foley To: qemu-devel@nongnu.org Subject: [PATCH v3 1/6] Fix double free issue in qemu_set_log_filename(). Date: Mon, 18 Nov 2019 16:15:23 -0500 Message-Id: <20191118211528.3221-2-robert.foley@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191118211528.3221-1-robert.foley@linaro.org> References: <20191118211528.3221-1-robert.foley@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::544 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: richard.henderson@linaro.org, alex.bennee@linaro.org, robert.foley@linaro.org, peter.puhov@linaro.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" After freeing the logfilename, we set logfilename to NULL, in case of an error which returns without setting logfilename. Signed-off-by: Robert Foley Reviewed-by: Alex Bennée --- v2 - moved this change to the beginning of the patch series. --- v1 - This is new in the patch v1. --- util/log.c | 1 + 1 file changed, 1 insertion(+) diff --git a/util/log.c b/util/log.c index 1ca13059ee..4316fe74ee 100644 --- a/util/log.c +++ b/util/log.c @@ -113,6 +113,7 @@ void qemu_set_log_filename(const char *filename, Error **errp) { char *pidstr; g_free(logfilename); + logfilename = NULL; pidstr = strstr(filename, "%"); if (pidstr) { From patchwork Mon Nov 18 21:15:24 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Robert Foley X-Patchwork-Id: 11250285 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 3321D138C for ; Mon, 18 Nov 2019 21:20:30 +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 09610208CE for ; Mon, 18 Nov 2019 21:20:30 +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="w66FTDUt" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 09610208CE 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]:39632 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iWoRg-0005W8-IY for patchwork-qemu-devel@patchwork.kernel.org; Mon, 18 Nov 2019 16:20:28 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:35784) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iWoNA-0008T1-Rt for qemu-devel@nongnu.org; Mon, 18 Nov 2019 16:15:52 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iWoN6-0005jK-VE for qemu-devel@nongnu.org; Mon, 18 Nov 2019 16:15:48 -0500 Received: from mail-pg1-x542.google.com ([2607:f8b0:4864:20::542]:33300) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iWoN6-0005iw-Pv for qemu-devel@nongnu.org; Mon, 18 Nov 2019 16:15:44 -0500 Received: by mail-pg1-x542.google.com with SMTP id h27so10243136pgn.0 for ; Mon, 18 Nov 2019 13:15:44 -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 :mime-version:content-transfer-encoding; bh=OGr2NFkF8BEM5m64tXfwHwGdKFCwpo8LWb1oMEbPg1U=; b=w66FTDUtPihHMX2BHfUySI1lShJpsxlMXYzG5sImza+QNPvc9eHpRUKKkxTC51RCcL BXkPNYuARmCHdBu3VcDrbaIUlOP+iIdUx491+bqhwY8qAGfyqUojCyAWjuIVGH9s6ZBT +AJFfJtfDZ23z9IQ6PKu6GRB+CqyvFLuJXxoM/z+qmCQxq9438GZzTVBMJNV6pduxr4g DChjbE2HoXNSjldpcuL1GOEgyz1l3XuNPsK8oyPEJsVkwUR1ifDGBWKqka4/F6di252+ qOAVfZRZK84/n30BA1o6Q0XTgcdnfre5QHEVYVbqrlPNwsOME/UNZSDb9YYu63leDmqh +1kg== 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:mime-version:content-transfer-encoding; bh=OGr2NFkF8BEM5m64tXfwHwGdKFCwpo8LWb1oMEbPg1U=; b=elwJ+pu7P8VVO/0gpFCrhaN8JwQYsj5y1UdOyDxLWqNsuQTokUOX+PVxXOv3kr4D0/ fMoulmFcvWTBB34UEGYT8cSoDca/Feo9kQVGLZk+tZH9eMcF2i39J9pPgxFc7YF5bGjn 9z4oWgeJyf05fq1Li373QCY0Xy/tQV/IFgWEgpRa8nftKUpSy59DuHrfMPkEEr+zQ/x5 u7zxqpKPOmBC8LU52/j/j9tRznRCZyO/cRWph6D4ihCbsPMHNmvunDFfgLmtuKssMN7J UkddtR9RxHz9QiF/ZZxZsrx6H2oRgD7p1SnJ5+Wv3yuRf50LX6yckKtpSJdsjfaqTV1g wSvw== X-Gm-Message-State: APjAAAWADmuwkj34mGyzK0IkzDazVrSXjQY/5l/+8kGe1D4Kd7byjYDP PwerKWh0pezSHIl95xFnT/G/fNIMKFI= X-Google-Smtp-Source: APXvYqyrMd7+Mlp5RhDXACe0AzWJjc7v4uZ3R1GFYEclSaR7/PqDg+yAvoSG9QW3wzeRh+TpvYXkPQ== X-Received: by 2002:a62:1ad6:: with SMTP id a205mr1556889pfa.64.1574111743374; Mon, 18 Nov 2019 13:15:43 -0800 (PST) Received: from Rfoley-MA01.usrd.futurewei.com ([12.111.81.71]) by smtp.gmail.com with ESMTPSA id v15sm22301728pfe.44.2019.11.18.13.15.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Nov 2019 13:15:42 -0800 (PST) From: Robert Foley To: qemu-devel@nongnu.org Subject: [PATCH v3 2/6] Cleaned up flow of code in qemu_set_log(), to simplify and clarify. Date: Mon, 18 Nov 2019 16:15:24 -0500 Message-Id: <20191118211528.3221-3-robert.foley@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191118211528.3221-1-robert.foley@linaro.org> References: <20191118211528.3221-1-robert.foley@linaro.org> MIME-Version: 1.0 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: richard.henderson@linaro.org, alex.bennee@linaro.org, robert.foley@linaro.org, peter.puhov@linaro.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" Also added some explanation of the reasoning behind the branches. Signed-off-by: Robert Foley Reviewed-by: Alex Bennée --- v2 - This is new in patch v2. --- util/log.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/util/log.c b/util/log.c index 4316fe74ee..417d16ec66 100644 --- a/util/log.c +++ b/util/log.c @@ -54,12 +54,25 @@ static bool log_uses_own_buffers; /* enable or disable low levels log */ void qemu_set_log(int log_flags) { + bool need_to_open_file = false; qemu_loglevel = log_flags; #ifdef CONFIG_TRACE_LOG qemu_loglevel |= LOG_TRACE; #endif - if (!qemu_logfile && - (is_daemonized() ? logfilename != NULL : qemu_loglevel)) { + /* + * In all cases we only log if qemu_loglevel is set. + * Also: + * If not daemonized we will always log either to stderr + * or to a file (if there is a logfilename). + * If we are daemonized, + * we will only log if there is a logfilename. + */ + if (qemu_loglevel && (!is_daemonized() || logfilename)) { + need_to_open_file = true; + } + if (qemu_logfile && !need_to_open_file) { + qemu_log_close(); + } else if (!qemu_logfile && need_to_open_file) { if (logfilename) { qemu_logfile = fopen(logfilename, log_append ? "a" : "w"); if (!qemu_logfile) { @@ -93,10 +106,6 @@ void qemu_set_log(int log_flags) log_append = 1; } } - if (qemu_logfile && - (is_daemonized() ? logfilename == NULL : !qemu_loglevel)) { - qemu_log_close(); - } } void qemu_log_needs_buffers(void) From patchwork Mon Nov 18 21:15:25 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Robert Foley X-Patchwork-Id: 11250287 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 687DB109A for ; Mon, 18 Nov 2019 21:22:38 +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 3EA51222DD for ; Mon, 18 Nov 2019 21:22:38 +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="jgIhSQ3g" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3EA51222DD 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]:39666 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iWoTl-0008Hq-16 for patchwork-qemu-devel@patchwork.kernel.org; Mon, 18 Nov 2019 16:22:37 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:35796) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iWoNC-00004r-Ox for qemu-devel@nongnu.org; Mon, 18 Nov 2019 16:15:54 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iWoN8-0005ld-TB for qemu-devel@nongnu.org; Mon, 18 Nov 2019 16:15:50 -0500 Received: from mail-pf1-x442.google.com ([2607:f8b0:4864:20::442]:36194) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iWoN8-0005kY-NL for qemu-devel@nongnu.org; Mon, 18 Nov 2019 16:15:46 -0500 Received: by mail-pf1-x442.google.com with SMTP id b19so10995878pfd.3 for ; Mon, 18 Nov 2019 13:15:46 -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 :mime-version:content-transfer-encoding; bh=wafFGt3nPwzzdj7M9taqKk32I4fEwfJNruFpoO9OReo=; b=jgIhSQ3gsl9P1WMR7CPp4cIm+p+ZNAWIu6ZTkVkou/VM4I6v1aKIcJteFNx4VOhmFQ Mbpky3kMuv6GtSBaOZsumq/q9BmgutMIHjQsn3d5h0pE6bTLpssX+d+NYixyK6Ocw/rp 6dySDXZLqeXbh+55Kyh5NQCf63mM36/vNGIJ92fW6h4NdAMxXpAh+9vRayRnP03Ont0D VBhKf8Wc1uirhyaBlY3Qc6N2Yo/iyK/iDjywekwbAYWC7H+jPMUcxrINfFeWfiAkZZDB CfmW6bRtLljv2RjFl+Qz0c2ceQEIcdDmwGc2dKr+gHFcYW8EA3ld0w7TZxsFGj1MPkqJ /+ew== 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:mime-version:content-transfer-encoding; bh=wafFGt3nPwzzdj7M9taqKk32I4fEwfJNruFpoO9OReo=; b=Ncq3j9MvkfaZR4VQ5MGtaxFzARcps8IyBCAjW1WekW4ZN+Wez4caRMD2mjc0Crr4BY 0j0etJcZZKzKU6JgwCIcHBDR9Ym660LNmRAzSCF3KevUYjJM3dsvS/T0pqXUfSvyp4g3 A45HxNos4caa1fDi5F777PMQWjVApTJVwWGZmIj9U4+Ne89WkT4RFIyBB5AUw2rWfGpc gJI15JMYFDXyeHU8ij/l7o+i+BVQ8jDS4jpvhg8YaIz3u7ZlCcTb95eU1x6BfMKJSywP U6ED08xMkFPYkj7JyYPo4ATfhgEIfr5TljiRhNDm1hlKlvDyPVfdLMqBhtPSCXz8ARUN N9Ew== X-Gm-Message-State: APjAAAU8KDETdh2hg1uKCUqP2zkyT0EMu1fqUw8mEGLefrg2NsfRvTgr 7my7lu7MZkKGejqI18iK71PicP83VbU= X-Google-Smtp-Source: APXvYqwvNNEx4k70oPxbLrC1UnD3YPJQci6bNKDgbRCpd0kUlHvfGtzxIXkq+XWg0EvwyQhJLt64ww== X-Received: by 2002:a65:628f:: with SMTP id f15mr1498253pgv.91.1574111745415; Mon, 18 Nov 2019 13:15:45 -0800 (PST) Received: from Rfoley-MA01.usrd.futurewei.com ([12.111.81.71]) by smtp.gmail.com with ESMTPSA id v15sm22301728pfe.44.2019.11.18.13.15.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Nov 2019 13:15:44 -0800 (PST) From: Robert Foley To: qemu-devel@nongnu.org Subject: [PATCH v3 3/6] Add a mutex to guarantee single writer to qemu_logfile handle. Date: Mon, 18 Nov 2019 16:15:25 -0500 Message-Id: <20191118211528.3221-4-robert.foley@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191118211528.3221-1-robert.foley@linaro.org> References: <20191118211528.3221-1-robert.foley@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::442 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: richard.henderson@linaro.org, alex.bennee@linaro.org, robert.foley@linaro.org, peter.puhov@linaro.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" Also added qemu_logfile_init() for initializing the logfile mutex. Note that inside qemu_set_log() we needed to add a pair of qemu_mutex_unlock() calls in order to avoid a double lock in qemu_log_close(). This unavoidable temporary ugliness will be cleaned up in a later patch in this series. Signed-off-by: Robert Foley Reviewed-by: Alex Bennée --- v3 - Removed assert that mutex is initialized. We will rely on the constructor. - Also added details in the commit message regarding the temporary ugliness that will be cleaned up in a later patch. --- v2 - In qemu_set_log() moved location of mutex lock/unlock due to cleanup changes. --- v1 - changed qemu_logfile_init() to use __constructor__. --- util/log.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/util/log.c b/util/log.c index 417d16ec66..953a66b5a8 100644 --- a/util/log.c +++ b/util/log.c @@ -24,8 +24,10 @@ #include "qapi/error.h" #include "qemu/cutils.h" #include "trace/control.h" +#include "qemu/thread.h" static char *logfilename; +static QemuMutex qemu_logfile_mutex; FILE *qemu_logfile; int qemu_loglevel; static int log_append = 0; @@ -49,6 +51,11 @@ int qemu_log(const char *fmt, ...) return ret; } +static void __attribute__((__constructor__)) qemu_logfile_init(void) +{ + qemu_mutex_init(&qemu_logfile_mutex); +} + static bool log_uses_own_buffers; /* enable or disable low levels log */ @@ -70,7 +77,9 @@ void qemu_set_log(int log_flags) if (qemu_loglevel && (!is_daemonized() || logfilename)) { need_to_open_file = true; } + qemu_mutex_lock(&qemu_logfile_mutex); if (qemu_logfile && !need_to_open_file) { + qemu_mutex_unlock(&qemu_logfile_mutex); qemu_log_close(); } else if (!qemu_logfile && need_to_open_file) { if (logfilename) { @@ -105,6 +114,7 @@ void qemu_set_log(int log_flags) #endif log_append = 1; } + qemu_mutex_unlock(&qemu_logfile_mutex); } } @@ -240,12 +250,14 @@ void qemu_log_flush(void) /* Close the log file */ void qemu_log_close(void) { + qemu_mutex_lock(&qemu_logfile_mutex); if (qemu_logfile) { if (qemu_logfile != stderr) { fclose(qemu_logfile); } qemu_logfile = NULL; } + qemu_mutex_unlock(&qemu_logfile_mutex); } const QEMULogItem qemu_log_items[] = { From patchwork Mon Nov 18 21:15:26 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Robert Foley X-Patchwork-Id: 11250279 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 316AC109A for ; Mon, 18 Nov 2019 21:17:55 +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 EAA6122310 for ; Mon, 18 Nov 2019 21:17:54 +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="TYt2XMi3" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org EAA6122310 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]:39568 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iWoPB-00028J-FS for patchwork-qemu-devel@patchwork.kernel.org; Mon, 18 Nov 2019 16:17:53 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:35820) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iWoNF-00009M-0U for qemu-devel@nongnu.org; Mon, 18 Nov 2019 16:15:56 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iWoNB-0005mT-2v for qemu-devel@nongnu.org; Mon, 18 Nov 2019 16:15:52 -0500 Received: from mail-pg1-x543.google.com ([2607:f8b0:4864:20::543]:37967) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iWoNA-0005m5-QL for qemu-devel@nongnu.org; Mon, 18 Nov 2019 16:15:49 -0500 Received: by mail-pg1-x543.google.com with SMTP id 15so10224440pgh.5 for ; Mon, 18 Nov 2019 13:15:48 -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 :mime-version:content-transfer-encoding; bh=SzYBwosOH0RhXIU2AZPKvHTHuDB70z5449ITz+BvFqY=; b=TYt2XMi39KGb9A/5y0IUf+0NOS3PcVzcI4LafzRJc+SRKDh9yJxVYbPBBk0B07xhzy dNoAZ0bU/D16OZe8ajRIpB9aRMkxgyiq9DeE4Jaj3nhP2XXMznvfb0yPiEWkUX1ec3Up U1had5lYWIT1uIUrnq9IOPQIFI9LBRAir6QvOyz4eGUHuu2rYpwCxV7AwgMDgv/T9wHv cntPHC3mNnOLGl/RHiamodpdskqdL5PF/dD2bVevZpDSddW+90RIzu56nUpNDsmF3yfR 7TWYmXfLiWcafi6Fu0sj9xTQh3QkxUcgThsiAISWyukgcH69xztUH5AIKbDwsTBi+C9K L4mQ== 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:mime-version:content-transfer-encoding; bh=SzYBwosOH0RhXIU2AZPKvHTHuDB70z5449ITz+BvFqY=; b=ds8q6HLxwQu+acIB+/O0/C6WXymbVbXpDH9wFiWAsw6KqRYzWmABhI+Uqafm4bYeR/ Urd/nhzrCL9iRFW/qxdB9xtJiUQOcMA8keXNYUz5sWT9o8fTICG3GAN7EWFhKR3+J0at w1glfc8v1NRSahTxpMiRGzUm7MoqjkDpVweH9ZYTbb8seBQF1umeqfU8N96YN4T0lOjJ BjZ8kNqN/E4mQoks8Hvurcd2vnl0219rjMNrU8CXJ22HtUJcoIqmnh4oMv20zTZl47oh ui2EyAGvv4F4oT001Wrtm60ZPaM9PZOPrZeCUkg9X4PfZOGXclcZzWwM/XHcQwevY6CD qlYg== X-Gm-Message-State: APjAAAWbe9CSJh5z6DX5J4AuxZAR7q2me9O4LguKLc1wJ8V30AxbmfSR 0F5zTb3LfPyv6r2n/h7lg97hhWH39wk= X-Google-Smtp-Source: APXvYqx/N4gJKzxkpwDxV3dqjyt6+727cKOOJtqLaeInRJH9YOEeMDVpX25Jnkp0LZoOCpzwP8drGg== X-Received: by 2002:a63:3741:: with SMTP id g1mr1491684pgn.434.1574111747293; Mon, 18 Nov 2019 13:15:47 -0800 (PST) Received: from Rfoley-MA01.usrd.futurewei.com ([12.111.81.71]) by smtp.gmail.com with ESMTPSA id v15sm22301728pfe.44.2019.11.18.13.15.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Nov 2019 13:15:46 -0800 (PST) From: Robert Foley To: qemu-devel@nongnu.org Subject: [PATCH v3 4/6] qemu_log_lock/unlock now preserves the qemu_logfile handle. Date: Mon, 18 Nov 2019 16:15:26 -0500 Message-Id: <20191118211528.3221-5-robert.foley@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191118211528.3221-1-robert.foley@linaro.org> References: <20191118211528.3221-1-robert.foley@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::543 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: richard.henderson@linaro.org, alex.bennee@linaro.org, robert.foley@linaro.org, peter.puhov@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. Also in target/tilegx/translate.c removed the qemu_log_lock()/unlock() calls (and the log("\n")), since the translator can longjmp out of the loop if it attempts to translate an instruction in an inaccessible page. Signed-off-by: Robert Foley Reviewed-by: Alex Bennée Reviewed-by: Richard Henderson --- v3 - Changes to one use case to remove use of qemu_log_lock()/unlock(). Only change was target/tilegx/translate.c --- v1 - Moved this up in the patch sequence to be before adding RCU for qemu_logfile. --- include/qemu/log.h | 9 ++++++--- target/tilegx/translate.c | 6 ------ 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/unicore32/translate.c | 4 ++-- tcg/tcg.c | 16 ++++++++-------- 15 files changed, 39 insertions(+), 42 deletions(-) diff --git a/include/qemu/log.h b/include/qemu/log.h index a91105b2ad..a7c5b01571 100644 --- a/include/qemu/log.h +++ b/include/qemu/log.h @@ -53,14 +53,17 @@ 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) { qemu_flockfile(qemu_logfile); + return logfile->fd; } -static inline void qemu_log_unlock(void) +static inline void qemu_log_unlock(FILE *fd) { - qemu_funlockfile(qemu_logfile); + if (fd) { + qemu_funlockfile(fd); + } } /* Logging functions: */ diff --git a/target/tilegx/translate.c b/target/tilegx/translate.c index 68dd4aa2d8..abce7e1c75 100644 --- a/target/tilegx/translate.c +++ b/target/tilegx/translate.c @@ -2388,7 +2388,6 @@ 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(); qemu_log("IN: %s\n", lookup_symbol(pc_start)); } gen_tb_start(tb); @@ -2417,11 +2416,6 @@ void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns) gen_tb_end(tb, num_insns); tb->size = dc->pc - pc_start; tb->icount = num_insns; - - if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) { - qemu_log("\n"); - qemu_log_unlock(); - } } void restore_state_to_opc(CPUTLGState *env, TranslationBlock *tb, 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 bdc7d5326a..525115b041 100644 --- a/target/microblaze/translate.c +++ b/target/microblaze/translate.c @@ -1765,10 +1765,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/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 5475d49ed1..0511266d85 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 @@ -4041,11 +4041,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 @@ -4086,11 +4086,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. */ @@ -4107,11 +4107,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 Mon Nov 18 21:15:27 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Robert Foley X-Patchwork-Id: 11250281 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 1183F109A for ; Mon, 18 Nov 2019 21:17:56 +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 C956F22310 for ; Mon, 18 Nov 2019 21:17:55 +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="JvEy/RpT" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C956F22310 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]:39572 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iWoPC-0002B4-5P for patchwork-qemu-devel@patchwork.kernel.org; Mon, 18 Nov 2019 16:17:54 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:35828) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iWoNG-0000CP-Qu for qemu-devel@nongnu.org; Mon, 18 Nov 2019 16:15:58 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iWoNC-0005nI-So for qemu-devel@nongnu.org; Mon, 18 Nov 2019 16:15:54 -0500 Received: from mail-pj1-x1044.google.com ([2607:f8b0:4864:20::1044]:41824) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iWoNC-0005mz-L2 for qemu-devel@nongnu.org; Mon, 18 Nov 2019 16:15:50 -0500 Received: by mail-pj1-x1044.google.com with SMTP id gc1so1855561pjb.8 for ; Mon, 18 Nov 2019 13:15:50 -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 :mime-version:content-transfer-encoding; bh=6zFafoqTW3uPdJsr8GKkTOXriqaT1mVv+FpYjpXcCxY=; b=JvEy/RpTQtWzjgIQHY/e+anPllqfckTZGIOdSunK+2/095mKfTOPQHfQ0xbp4RaulX q3G2DdIykajgMtMx2jY7aDRZMLmJd60RYW4k9oaS9ZIx5y50muVuqlqDMzyb4lkkWHeP /KkDFPhQu6wRNW6s5syj4H77al7g2J+v9e188b3o/nTNhYdTgX24Xdpbz2oHVuLQ+807 oxfjiyjTSIf6pxlYLE18ughwTAJuWoAEaaYG/epqzhpoeRARxsttsvcU14BCXdk9eQNZ TfEyOkyIMBymXd9Pp5vz8Jk+667AbkGDciZTNmcR62o38xXOtPfPvCb45ipA5m2gFd5U oCDA== 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:mime-version:content-transfer-encoding; bh=6zFafoqTW3uPdJsr8GKkTOXriqaT1mVv+FpYjpXcCxY=; b=RZ7JF3UJYn27UuOBuQIJW159OL/mGCtU6Kx7QZfEAGfgIQAQjtCo5hq17UATxbmI+P Uwii4k6QQWznANxuMEEVCVK7RgddIln5bGu/nC1gCix7BOd4U4tsQlc5QrUXHL659Gng xn9ypLHP4uwsHxV6QzyhQLAkUQjuCCBlNPWUXjfr0uOVMsx7Tbg05qa2fFcJpjNALimP 1b1Etsnh1NcT4D0cnFSKQf/rVGvw13D+x6iL0BPnHHchwNYnbAjOX2M3Wn/OUsqe7885 5Ns8iU4ywMhc8H4fBPDEndFZk/inY6oi9XLivlEfG8p7DofhUHpxmcl1+em+HmIpJ+vS EhpA== X-Gm-Message-State: APjAAAVgNuvfo4wlOKBeV6k8lqRjDznIS8GuCkdDCplf69WYBytWVzYk z1bxMNDPROEOD6+o358NBU1TnlwOXdU= X-Google-Smtp-Source: APXvYqxN9S695Iqu/fOnlrtofypDvmYZS8bmGudUr6G3b5RDHGxi+5EhoEEpsaTwQZHyUb8qOMR5QQ== X-Received: by 2002:a17:90a:7188:: with SMTP id i8mr1397842pjk.6.1574111749238; Mon, 18 Nov 2019 13:15:49 -0800 (PST) Received: from Rfoley-MA01.usrd.futurewei.com ([12.111.81.71]) by smtp.gmail.com with ESMTPSA id v15sm22301728pfe.44.2019.11.18.13.15.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Nov 2019 13:15:48 -0800 (PST) From: Robert Foley To: qemu-devel@nongnu.org Subject: [PATCH v3 5/6] Add use of RCU for qemu_logfile. Date: Mon, 18 Nov 2019 16:15:27 -0500 Message-Id: <20191118211528.3221-6-robert.foley@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191118211528.3221-1-robert.foley@linaro.org> References: <20191118211528.3221-1-robert.foley@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::1044 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: richard.henderson@linaro.org, alex.bennee@linaro.org, robert.foley@linaro.org, peter.puhov@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 Reviewed-by: Alex Bennée --- v3 - Changes for qemu_log_lock() to unconditionally hold the rcu_read_lock() until qemu_log_unlock() - Changed qemu_log_unlock() to unconditionally rcu_read_unlock() --- v2 - No specific changes, just merging in cleanup changes in qemu_set_log(). --- v1 - Changes for review comments. - Minor changes to definition of QemuLogFile. - Changed qemu_log_separate() to fix unbalanced and remove qemu_log_enabled() check. - changed qemu_log_lock() to include else. - make qemu_logfile_free static. - use g_assert(logfile) in qemu_logfile_free. - Relocated unlock out of if/else in qemu_log_close(), and in qemu_set_log(). --- include/qemu/log.h | 41 ++++++++++++++++++++++---- util/log.c | 72 ++++++++++++++++++++++++++++++++-------------- include/exec/log.h | 33 ++++++++++++++++++--- tcg/tcg.c | 12 ++++++-- 4 files changed, 125 insertions(+), 33 deletions(-) diff --git a/include/qemu/log.h b/include/qemu/log.h index a7c5b01571..e0f4e40628 100644 --- a/include/qemu/log.h +++ b/include/qemu/log.h @@ -3,9 +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 FILE *qemu_logfile; +extern QemuLogFile *qemu_logfile; + /* * The new API: @@ -25,7 +32,16 @@ static inline bool qemu_log_enabled(void) */ static inline bool qemu_log_separate(void) { - return qemu_logfile != NULL && qemu_logfile != stderr; + QemuLogFile *logfile; + bool res = false; + + rcu_read_lock(); + logfile = atomic_rcu_read(&qemu_logfile); + if (logfile && logfile->fd != stderr) { + res = true; + } + rcu_read_unlock(); + return res; } #define CPU_LOG_TB_OUT_ASM (1 << 0) @@ -55,8 +71,15 @@ static inline bool qemu_log_separate(void) static inline FILE *qemu_log_lock(void) { - qemu_flockfile(qemu_logfile); - return logfile->fd; + QemuLogFile *logfile; + rcu_read_lock(); + logfile = atomic_rcu_read(&qemu_logfile); + if (logfile) { + qemu_flockfile(logfile->fd); + return logfile->fd; + } else { + return NULL; + } } static inline void qemu_log_unlock(FILE *fd) @@ -64,6 +87,7 @@ static inline void qemu_log_unlock(FILE *fd) if (fd) { qemu_funlockfile(fd); } + rcu_read_unlock(); } /* Logging functions: */ @@ -73,9 +97,14 @@ static inline void qemu_log_unlock(FILE *fd) 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: diff --git a/util/log.c b/util/log.c index 953a66b5a8..867264da8d 100644 --- a/util/log.c +++ b/util/log.c @@ -28,7 +28,7 @@ static char *logfilename; static QemuMutex qemu_logfile_mutex; -FILE *qemu_logfile; +QemuLogFile *qemu_logfile; int qemu_loglevel; static int log_append = 0; static GArray *debug_regions; @@ -37,10 +37,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. */ @@ -48,6 +52,7 @@ int qemu_log(const char *fmt, ...) ret = 0; } } + rcu_read_unlock(); return ret; } @@ -56,12 +61,24 @@ static void __attribute__((__constructor__)) qemu_logfile_init(void) qemu_mutex_init(&qemu_logfile_mutex); } +static void qemu_logfile_free(QemuLogFile *logfile) +{ + g_assert(logfile); + + if (logfile->fd != stderr) { + fclose(logfile->fd); + } + g_free(logfile); +} + static bool log_uses_own_buffers; /* enable or disable low levels log */ void qemu_set_log(int log_flags) { bool need_to_open_file = false; + QemuLogFile *logfile; + qemu_loglevel = log_flags; #ifdef CONFIG_TRACE_LOG qemu_loglevel |= LOG_TRACE; @@ -79,43 +96,47 @@ void qemu_set_log(int log_flags) } qemu_mutex_lock(&qemu_logfile_mutex); if (qemu_logfile && !need_to_open_file) { - qemu_mutex_unlock(&qemu_logfile_mutex); - qemu_log_close(); + logfile = qemu_logfile; + atomic_rcu_set(&qemu_logfile, NULL); + call_rcu(logfile, qemu_logfile_free, rcu); } else if (!qemu_logfile && need_to_open_file) { + 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; } - qemu_mutex_unlock(&qemu_logfile_mutex); + atomic_rcu_set(&qemu_logfile, logfile); } + qemu_mutex_unlock(&qemu_logfile_mutex); } void qemu_log_needs_buffers(void) @@ -244,18 +265,27 @@ 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(); } /* Close the log file */ void qemu_log_close(void) { + QemuLogFile *logfile; + qemu_mutex_lock(&qemu_logfile_mutex); - if (qemu_logfile) { - if (qemu_logfile != stderr) { - fclose(qemu_logfile); - } - qemu_logfile = NULL; + logfile = qemu_logfile; + + if (logfile) { + atomic_rcu_set(&qemu_logfile, NULL); + call_rcu(logfile, qemu_logfile_free, rcu); } 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 0511266d85..4f616ba38b 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 Mon Nov 18 21:15:28 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Robert Foley X-Patchwork-Id: 11250289 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 2CB24138C for ; Mon, 18 Nov 2019 21:24:36 +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 01440222DD for ; Mon, 18 Nov 2019 21:24:36 +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="Dad+0U01" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 01440222DD 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]:39702 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iWoVf-0002Hg-3T for patchwork-qemu-devel@patchwork.kernel.org; Mon, 18 Nov 2019 16:24:35 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:35832) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iWoNI-0000F5-6L for qemu-devel@nongnu.org; Mon, 18 Nov 2019 16:16:00 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iWoNE-0005nz-AR for qemu-devel@nongnu.org; Mon, 18 Nov 2019 16:15:56 -0500 Received: from mail-pj1-x1041.google.com ([2607:f8b0:4864:20::1041]:40687) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iWoNE-0005ne-4w for qemu-devel@nongnu.org; Mon, 18 Nov 2019 16:15:52 -0500 Received: by mail-pj1-x1041.google.com with SMTP id ep1so1854423pjb.7 for ; Mon, 18 Nov 2019 13:15:52 -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 :mime-version:content-transfer-encoding; bh=nB9UJp1tY01vA+O7nUtpyswqmIH2jEdG/Vc4ku6hyfs=; b=Dad+0U01jzs2l4tt+VDm7bNGm06VFMSN2gcaWsVewISGT50ZsTxtEMmdJoMNOBdLME 14wjs6q6YKCuP0/aS2uAlHSHbrJpmoXXG+m4oqjsRJr+LT2eGgIQ/eI3zdVRaBCfBeCU T6uVavywPilx9/22qrtXaVjThUjH67zAFdYRkoTfRFbAQehB2YeUghi5xhmV4S4eEQZE 3evmP65xUAmD7o2IaTnz35RkO8VVzmQvNqboJGZ57p4G/8QmcuroaGvjLNPp6IAt3xsm vE/ynRBl3wtJdw8Um/bo53S6L9eEzLSIvmM3OALDhEWB0dbiSncEsIDo3glWeNlewovV dAtg== 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:mime-version:content-transfer-encoding; bh=nB9UJp1tY01vA+O7nUtpyswqmIH2jEdG/Vc4ku6hyfs=; b=sQzpIcJOugrSKlag5rwSYQTDDe196/ChKazlpWI7co15+iUT7vMpA/5YRrwIP0mglW xOlyGgOrA5pWR/4z3WqpQRV56q+AGUinrJefAxwrw+zzcjpxq8Eu4fDhaRDY6+ltDGYL tYlJZFcsg3vRQmSi5dwIxWfb4XqwEuPO0bYE44RED/9u0QxIFZMSg3gIlPQ2rGiH3VoC DBJDULfq4Zi9VfRYUTy/9AWCwylRlWyjLvClN3ZkZ9BRZnWqyyDVye9/UAHfXatzz6s/ KJS+9zGydmqOgXF+oFOvAxmKqmsyVnWQOVhf20ollQoIF8Q5IYvz4K62VPfoUHp7ucAT avIw== X-Gm-Message-State: APjAAAWBSJgT8udLscB0NrhZ38rTlhCmWO7lw6ZGTlkWXiU25FAXmrYk rETyMhpKVJnGjTmrXpBKeW88O+nJ4tU= X-Google-Smtp-Source: APXvYqyVtr2sZWVI2ooQO2n9rEbDfeDy8xJIbUlQkz5AY9NBYyNkxCyseL2TzspWPrZRhGvpPZV/sw== X-Received: by 2002:a17:90a:a405:: with SMTP id y5mr1439885pjp.102.1574111750942; Mon, 18 Nov 2019 13:15:50 -0800 (PST) Received: from Rfoley-MA01.usrd.futurewei.com ([12.111.81.71]) by smtp.gmail.com with ESMTPSA id v15sm22301728pfe.44.2019.11.18.13.15.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Nov 2019 13:15:50 -0800 (PST) From: Robert Foley To: qemu-devel@nongnu.org Subject: [PATCH v3 6/6] Added tests for close and change of logfile. Date: Mon, 18 Nov 2019 16:15:28 -0500 Message-Id: <20191118211528.3221-7-robert.foley@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191118211528.3221-1-robert.foley@linaro.org> References: <20191118211528.3221-1-robert.foley@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::1041 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: richard.henderson@linaro.org, alex.bennee@linaro.org, robert.foley@linaro.org, peter.puhov@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 Reviewed-by: Alex Bennée --- v1 - Changes for first round of code review comments. - Added in use of g_autofree, removed the g_free()s. - Added in use of logfile2 and changed sequence of asserts. --- tests/test-logging.c | 80 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/tests/test-logging.c b/tests/test-logging.c index a12585f70a..1e646f045d 100644 --- a/tests/test-logging.c +++ b/tests/test-logging.c @@ -108,6 +108,82 @@ static void test_parse_path(gconstpointer data) error_free_or_abort(&err); } +static void test_logfile_write(gconstpointer data) +{ + QemuLogFile *logfile; + QemuLogFile *logfile2; + gchar const *dir = data; + Error *err = NULL; + g_autofree gchar *file_path; + g_autofree gchar *file_path1; + FILE *orig_fd; + + /* + * Before starting test, set log flags, to ensure the file gets + * opened below with the call to qemu_set_log_filename(). + * In cases where a logging backend other than log is used, + * this is needed. + */ + qemu_set_log(CPU_LOG_TB_OUT_ASM); + 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); + logfile2 = atomic_rcu_read(&qemu_logfile); + g_assert(logfile->fd == orig_fd); + g_assert(logfile2->fd != logfile->fd); + fprintf(logfile->fd, "%s 2nd write to file\n", __func__); + fflush(logfile->fd); + rcu_read_unlock(); +} + +static void test_logfile_lock(gconstpointer data) +{ + FILE *logfile; + gchar const *dir = data; + Error *err = NULL; + g_autofree 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); +} + /* Remove a directory and all its entries (non-recursive). */ static void rmdir_full(gchar const *root) { @@ -134,6 +210,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();